-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
12c9e2f
commit 6e723e8
Showing
9 changed files
with
200 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./uiButton" | ||
export * from "./uiNode" | ||
export * from "./uiStyle" | ||
export * from "./uiText" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export class UiButton {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { | ||
Commands, | ||
LogicalButtonInput, | ||
query, | ||
res, | ||
Schedule, | ||
UiButton, | ||
UiNode, | ||
UiStyle, | ||
UiText, | ||
type World, | ||
} from "@tsukinoko-kun/ecs.ts" | ||
|
||
class Counter { | ||
public value = 0 | ||
} | ||
|
||
class CounterMarker {} | ||
|
||
function spawnUi() { | ||
Commands.spawn( | ||
new UiNode(), | ||
new UiStyle() | ||
.set("backgroundColor", "whitesmoke") | ||
.set("border", "solid 1px gray") | ||
.set("padding", "0.5rem 1rem") | ||
.set("margin", "1rem 0"), | ||
).withChildren((parent) => { | ||
parent.spawn(new UiText("a"), new UiStyle().set("color", "red")) | ||
parent.spawn(new UiText("b"), new UiStyle().set("color", "blue")) | ||
parent.spawn(new UiText("c"), new UiStyle().set("color", "green")) | ||
}) | ||
|
||
Commands.spawn(new CounterMarker(), new UiButton(), new UiText("Click me!")) | ||
} | ||
|
||
function incrementCounter() { | ||
const btn = res(LogicalButtonInput) | ||
if (btn.pressed("b")) { | ||
const counter = res(Counter) | ||
counter.value++ | ||
} | ||
} | ||
|
||
function updateButtonText() { | ||
const counter = res(Counter) | ||
for (const [text] of query([UiText], query.and(CounterMarker))) { | ||
if (counter.value === 0) { | ||
text.value = "Press 'b' to increment the counter!" | ||
} else { | ||
text.value = `Counter: ${counter.value}\nPress 'b' to increment further!` | ||
} | ||
} | ||
} | ||
|
||
export function counterPlugin(world: World) { | ||
world.insertResource(new Counter()) | ||
world.addSystem(Schedule.Start, spawnUi) | ||
world.addSystem(Schedule.Update, incrementCounter) | ||
world.addSystem(Schedule.Update, updateButtonText) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,8 @@ | ||
import { App, Commands, DefaultPlugin, HtmlPlugin, Schedule, UiNode, UiStyle, UiText } from "@tsukinoko-kun/ecs.ts" | ||
import { App, DefaultPlugin, HtmlPlugin } from "@tsukinoko-kun/ecs.ts" | ||
import { counterPlugin } from "./counter" | ||
|
||
const app = new App() | ||
|
||
app.addPlugin(DefaultPlugin) | ||
.addPlugin(HtmlPlugin("#app")) | ||
.addPlugin((world) => { | ||
world.addSystem(Schedule.Start, () => { | ||
Commands.spawn( | ||
new UiNode(), | ||
new UiStyle() | ||
.set("backgroundColor", "whitesmoke") | ||
.set("border", "solid 1px gray") | ||
.set("padding", "0.5rem 1rem") | ||
.set("margin", "1rem 0"), | ||
).withChildren((parent) => { | ||
parent.spawn(new UiText("a"), new UiStyle().set("color", "red")) | ||
parent.spawn(new UiText("b"), new UiStyle().set("color", "blue")) | ||
parent.spawn(new UiText("c"), new UiStyle().set("color", "green")) | ||
}) | ||
|
||
Commands.spawn(new UiText("hello world")) | ||
}) | ||
}) | ||
app.addPlugin(DefaultPlugin).addPlugin(HtmlPlugin("#app")).addPlugin(counterPlugin) | ||
|
||
app.run() |