feat: implement absolute scale setter

This commit is contained in:
aarthificial
2022-11-17 16:45:20 +01:00
committed by Jacob
parent d6ab0d96a8
commit 842079a654
4 changed files with 52 additions and 11 deletions

View File

@@ -4,10 +4,7 @@
"useWorkspaces": true,
"private": false,
"version": "12.0.0-alpha.2",
"ignoreChanges": [
"**/*.test.ts",
"**/*.md"
],
"ignoreChanges": ["**/*.test.ts", "**/*.md"],
"message": "ci(release): %v [skip ci]",
"conventionalCommits": true,
"createRelease": "github"

View File

@@ -384,7 +384,16 @@ export class Layout extends Node {
}
protected setAbsoluteScale(value: SignalValue<Vector2>) {
// TODO Implement setter
if (isReactive(value)) {
this.scale(() => this.getRelativeScale(value()));
} else {
this.scale(this.getRelativeScale(value));
}
}
private getRelativeScale(scale: Vector2): Vector2 {
const parentScale = this.parentTransform()?.absoluteScale() ?? Vector2.one;
return scale.div(parentScale);
}
@compound(['x', 'y'], Vector2)
@@ -566,11 +575,11 @@ export class Layout extends Node {
this.element.innerText = '';
const queue = [...this.children()];
while (queue.length) {
const child = queue.shift()!;
const child = queue.shift();
if (child instanceof Layout) {
this.element.append(child.element);
child.updateLayout();
} else {
} else if (child) {
queue.push(...child.children());
}
}

View File

@@ -188,6 +188,10 @@ export class Vector2 {
return new Vector2(this.x * vector.x, this.y * vector.y);
}
public div(vector: Vector2) {
return new Vector2(this.x / vector.x, this.y / vector.y);
}
public add(vector: Vector2) {
return new Vector2(this.x + vector.x, this.y + vector.y);
}

View File

@@ -1,13 +1,44 @@
/**
* Create an array of given length containing its own indices.
* Create an array containing a range of numbers.
*
* @example
* ```ts
* const array = range(3); // [0, 1, 2]
* ```
*
* @param length
* @param length - The length of the array.
*/
export function range(length: number): number[] {
return [...Array(length).keys()];
export function range(length: number): number[];
/**
* Create an array containing a range of numbers.
*
* @example
* ```ts
* const array = range(3, 7); // [3, 4, 5, 6]
* ```
*
* @param from - The start of the range.
* @param to - The end of the range. `to` itself is not included in the result.
*/
export function range(from: number, to: number): number[];
export function range(first: number, second?: number): number[] {
let from = 0;
let to = first;
if (second !== undefined) {
from = first;
to = second;
}
const array = [];
if (from > to) {
for (let i = from; i > to; i--) {
array.push(i);
}
} else {
for (let i = from; i < to; i++) {
array.push(i);
}
}
return array;
}