Skip to content

Commit

Permalink
Create an easy way to wrap a layout.
Browse files Browse the repository at this point in the history
  • Loading branch information
samwho committed Apr 21, 2024
1 parent adbd5ed commit 263e199
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pixijs-layout",
"version": "0.1.25",
"version": "0.2.0",
"description": "",
"main": "./out/index.js",
"types": "./out/index.d.ts",
Expand Down
Binary file added screenshots/indirect-hstack.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/indirect-vstack.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions src/LayoutContainer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { DisplayObject, Rectangle } from "pixi.js-legacy";
import { Partitioner } from "./Partitioner";

export class LayoutContainer extends Partitioner {
constructor(child: Partitioner) {
super(child);
}

override *partition(
_objects: DisplayObject[],
space: Rectangle,
): Generator<Rectangle> {
yield space;
}

override arrange(screen: Rectangle): void {
super.arrange(screen);
}
}
9 changes: 9 additions & 0 deletions src/Partitioner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ export abstract class Partitioner extends Container implements Positioner {
this.zIndex = children
.map((child) => child.zIndex)
.reduce((a, b) => Math.min(a, b), Infinity);

let hasInteractiveChildren = false;
for (const child of children) {
if (child.interactiveChildren) {
hasInteractiveChildren = true;
break;
}
}
this.interactiveChildren = hasInteractiveChildren;
}

leaves(fn: (l: LeafComponent) => LeafComponent): this {
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Grid } from "./Grid";
import { Positioner } from "./Positioner";
import { Partitioner } from "./Partitioner";
import { LayoutSize } from "./LayoutSize";
import { LayoutContainer } from "./LayoutContainer";

export {
Stack,
Expand All @@ -14,4 +15,5 @@ export {
Positioner,
Partitioner,
LayoutSize,
LayoutContainer,
};
36 changes: 34 additions & 2 deletions tests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Container } from "pixi.js-legacy";
import { Grid, HStack, Stack, VStack } from "./src";
import { Container, DisplayObject } from "pixi.js-legacy";
import { Grid, HStack, LayoutContainer, Stack, VStack } from "./src";
import { componentTest, circle, appTest, rect, tube } from "./test-utils";

componentTest("1x2-hstack", () =>
Expand Down Expand Up @@ -405,3 +405,35 @@ componentTest("layout-size", () =>
.leaves((leaf) => leaf.fit())
.debug(),
);

class IndirectHStack extends LayoutContainer {
constructor(...children: DisplayObject[]) {
super(HStack(...children));
}
}

componentTest("indirect-hstack", () =>
VStack(
new IndirectHStack(circle(), circle(), circle()),
new IndirectHStack(circle(), circle(), circle()),
new IndirectHStack(circle(), circle(), circle()),
)
.debug()
.leaves((leaf) => leaf.fit().padding(5)),
);

class IndirectVStack extends LayoutContainer {
constructor(...children: DisplayObject[]) {
super(VStack(...children));
}
}

componentTest("indirect-vstack", () =>
HStack(
new IndirectVStack(circle(), circle(), circle()),
new IndirectVStack(circle(), circle(), circle()),
new IndirectVStack(circle(), circle(), circle()),
)
.debug()
.leaves((leaf) => leaf.fit().padding(5)),
);

0 comments on commit 263e199

Please sign in to comment.