canvasengine 2.0.0-beta.6 → 2.0.0-beta.60
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/DebugRenderer-DkjTAc48.js +1384 -0
- package/dist/DebugRenderer-DkjTAc48.js.map +1 -0
- package/dist/components/Button.d.ts +185 -0
- package/dist/components/Button.d.ts.map +1 -0
- package/dist/components/Canvas.d.ts +17 -0
- package/dist/components/Canvas.d.ts.map +1 -0
- package/dist/components/DOMElement.d.ts +54 -0
- package/dist/components/DOMElement.d.ts.map +1 -0
- package/dist/components/DOMSprite.d.ts +127 -0
- package/dist/components/DOMSprite.d.ts.map +1 -0
- package/dist/components/FocusContainer.d.ts +129 -0
- package/dist/components/FocusContainer.d.ts.map +1 -0
- package/dist/components/Graphic.d.ts +64 -0
- package/dist/components/Graphic.d.ts.map +1 -0
- package/dist/components/Joystick.d.ts +36 -0
- package/dist/components/Joystick.d.ts.map +1 -0
- package/dist/components/NineSliceSprite.d.ts +16 -0
- package/dist/components/NineSliceSprite.d.ts.map +1 -0
- package/dist/components/ParticleEmitter.d.ts +4 -0
- package/dist/components/ParticleEmitter.d.ts.map +1 -0
- package/dist/components/Scene.d.ts +2 -0
- package/dist/components/Scene.d.ts.map +1 -0
- package/dist/components/Text.d.ts +25 -0
- package/dist/components/Text.d.ts.map +1 -0
- package/dist/components/TilingSprite.d.ts +17 -0
- package/dist/components/TilingSprite.d.ts.map +1 -0
- package/dist/components/Video.d.ts +14 -0
- package/dist/components/Video.d.ts.map +1 -0
- package/dist/components/index.d.ts +20 -0
- package/dist/components/index.d.ts.map +1 -0
- package/dist/components/types/DisplayObject.d.ts +118 -0
- package/dist/components/types/DisplayObject.d.ts.map +1 -0
- package/dist/components/types/MouseEvent.d.ts +4 -0
- package/dist/components/types/MouseEvent.d.ts.map +1 -0
- package/dist/components/types/Spritesheet.d.ts +248 -0
- package/dist/components/types/Spritesheet.d.ts.map +1 -0
- package/dist/components/types/index.d.ts +4 -0
- package/dist/components/types/index.d.ts.map +1 -0
- package/dist/directives/Controls.d.ts +112 -0
- package/dist/directives/Controls.d.ts.map +1 -0
- package/dist/directives/ControlsBase.d.ts +199 -0
- package/dist/directives/ControlsBase.d.ts.map +1 -0
- package/dist/directives/Drag.d.ts +69 -0
- package/dist/directives/Drag.d.ts.map +1 -0
- package/dist/directives/Flash.d.ts +116 -0
- package/dist/directives/Flash.d.ts.map +1 -0
- package/dist/directives/FocusNavigation.d.ts +52 -0
- package/dist/directives/FocusNavigation.d.ts.map +1 -0
- package/dist/directives/FogVisibility.d.ts +47 -0
- package/dist/directives/FogVisibility.d.ts.map +1 -0
- package/dist/directives/GamepadControls.d.ts +224 -0
- package/dist/directives/GamepadControls.d.ts.map +1 -0
- package/dist/directives/JoystickControls.d.ts +171 -0
- package/dist/directives/JoystickControls.d.ts.map +1 -0
- package/dist/directives/KeyboardControls.d.ts +219 -0
- package/dist/directives/KeyboardControls.d.ts.map +1 -0
- package/dist/directives/Scheduler.d.ts +35 -0
- package/dist/directives/Scheduler.d.ts.map +1 -0
- package/dist/directives/Shake.d.ts +98 -0
- package/dist/directives/Shake.d.ts.map +1 -0
- package/dist/directives/Sound.d.ts +25 -0
- package/dist/directives/Sound.d.ts.map +1 -0
- package/dist/directives/Transition.d.ts +10 -0
- package/dist/directives/Transition.d.ts.map +1 -0
- package/dist/directives/ViewportCull.d.ts +11 -0
- package/dist/directives/ViewportCull.d.ts.map +1 -0
- package/dist/directives/ViewportFollow.d.ts +18 -0
- package/dist/directives/ViewportFollow.d.ts.map +1 -0
- package/dist/directives/index.d.ts +14 -0
- package/dist/directives/index.d.ts.map +1 -0
- package/dist/dist-BOOc43Qm.js +778 -0
- package/dist/dist-BOOc43Qm.js.map +1 -0
- package/dist/engine/FocusManager.d.ts +174 -0
- package/dist/engine/FocusManager.d.ts.map +1 -0
- package/dist/engine/animation.d.ts +72 -0
- package/dist/engine/animation.d.ts.map +1 -0
- package/dist/engine/bootstrap.d.ts +48 -0
- package/dist/engine/bootstrap.d.ts.map +1 -0
- package/dist/engine/directive.d.ts +13 -0
- package/dist/engine/directive.d.ts.map +1 -0
- package/dist/engine/reactive.d.ts +134 -0
- package/dist/engine/reactive.d.ts.map +1 -0
- package/dist/engine/signal.d.ts +71 -0
- package/dist/engine/signal.d.ts.map +1 -0
- package/dist/engine/trigger.d.ts +54 -0
- package/dist/engine/trigger.d.ts.map +1 -0
- package/dist/engine/utils.d.ts +89 -0
- package/dist/engine/utils.d.ts.map +1 -0
- package/dist/hooks/addContext.d.ts +2 -0
- package/dist/hooks/addContext.d.ts.map +1 -0
- package/dist/hooks/useFocus.d.ts +60 -0
- package/dist/hooks/useFocus.d.ts.map +1 -0
- package/dist/hooks/useProps.d.ts +42 -0
- package/dist/hooks/useProps.d.ts.map +1 -0
- package/dist/hooks/useRef.d.ts +4 -0
- package/dist/hooks/useRef.d.ts.map +1 -0
- package/dist/index.d.ts +19 -1107
- package/dist/index.d.ts.map +1 -0
- package/dist/index.global.js +5 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +9768 -3135
- package/dist/index.js.map +1 -1
- package/dist/utils/Ease.d.ts +17 -0
- package/dist/utils/Ease.d.ts.map +1 -0
- package/dist/utils/GlobalAssetLoader.d.ts +141 -0
- package/dist/utils/GlobalAssetLoader.d.ts.map +1 -0
- package/dist/utils/RadialGradient.d.ts +57 -0
- package/dist/utils/RadialGradient.d.ts.map +1 -0
- package/dist/utils/functions.d.ts +2 -0
- package/dist/utils/functions.d.ts.map +1 -0
- package/dist/utils/tabindex.d.ts +16 -0
- package/dist/utils/tabindex.d.ts.map +1 -0
- package/package.json +15 -9
- package/src/components/Button.ts +399 -0
- package/src/components/Canvas.ts +62 -46
- package/src/components/Container.ts +21 -2
- package/src/components/DOMContainer.ts +379 -0
- package/src/components/DOMElement.ts +556 -0
- package/src/components/DOMSprite.ts +1040 -0
- package/src/components/DisplayObject.ts +419 -201
- package/src/components/FocusContainer.ts +368 -0
- package/src/components/Graphic.ts +227 -66
- package/src/components/Joystick.ts +363 -0
- package/src/components/Mesh.ts +222 -0
- package/src/components/NineSliceSprite.ts +4 -1
- package/src/components/ParticleEmitter.ts +12 -8
- package/src/components/Sprite.ts +418 -52
- package/src/components/Text.ts +125 -18
- package/src/components/Viewport.ts +122 -63
- package/src/components/index.ts +9 -2
- package/src/components/types/DisplayObject.ts +53 -5
- package/src/components/types/Spritesheet.ts +0 -118
- package/src/directives/Controls.ts +254 -0
- package/src/directives/ControlsBase.ts +267 -0
- package/src/directives/Drag.ts +357 -52
- package/src/directives/Flash.ts +419 -0
- package/src/directives/FocusNavigation.ts +113 -0
- package/src/directives/FogVisibility.ts +273 -0
- package/src/directives/GamepadControls.ts +537 -0
- package/src/directives/JoystickControls.ts +396 -0
- package/src/directives/KeyboardControls.ts +85 -430
- package/src/directives/Scheduler.ts +12 -4
- package/src/directives/Shake.ts +298 -0
- package/src/directives/Sound.ts +94 -31
- package/src/directives/ViewportFollow.ts +40 -9
- package/src/directives/index.ts +13 -6
- package/src/engine/FocusManager.ts +510 -0
- package/src/engine/animation.ts +175 -21
- package/src/engine/bootstrap.ts +93 -3
- package/src/engine/directive.ts +4 -4
- package/src/engine/reactive.ts +979 -176
- package/src/engine/signal.ts +113 -25
- package/src/engine/trigger.ts +34 -7
- package/src/engine/utils.ts +19 -3
- package/src/hooks/useFocus.ts +91 -0
- package/src/hooks/useProps.ts +1 -1
- package/src/index.ts +8 -2
- package/src/types/pixi-cull.d.ts +7 -0
- package/src/utils/GlobalAssetLoader.ts +257 -0
- package/src/utils/functions.ts +7 -0
- package/src/utils/tabindex.ts +70 -0
- package/testing/index.ts +35 -4
- package/tsconfig.json +18 -0
- package/vite.config.ts +39 -0
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import { effect, signal, computed, isSignal, Signal } from "@signe/reactive";
|
|
2
|
+
import { FederatedPointerEvent } from "pixi.js";
|
|
3
|
+
import { h } from "../engine/signal";
|
|
4
|
+
import { useDefineProps } from "../hooks/useProps";
|
|
5
|
+
import { Container } from "./Container";
|
|
6
|
+
import { Rect, Circle, Ellipse } from "./Graphic";
|
|
7
|
+
import { Text } from "./Text";
|
|
8
|
+
import { ControlsDirective } from "../directives/Controls";
|
|
9
|
+
import { JoystickControls } from "../directives/JoystickControls";
|
|
10
|
+
import { Element } from "../engine/reactive";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Button states for visual feedback
|
|
14
|
+
*/
|
|
15
|
+
export enum ButtonState {
|
|
16
|
+
Normal = "normal",
|
|
17
|
+
Hover = "hover",
|
|
18
|
+
Pressed = "pressed",
|
|
19
|
+
Disabled = "disabled"
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Button style configuration for different visual approaches
|
|
24
|
+
*/
|
|
25
|
+
export interface ButtonStyle {
|
|
26
|
+
/** Background color for each state */
|
|
27
|
+
backgroundColor?: {
|
|
28
|
+
[ButtonState.Normal]?: string;
|
|
29
|
+
[ButtonState.Hover]?: string;
|
|
30
|
+
[ButtonState.Pressed]?: string;
|
|
31
|
+
[ButtonState.Disabled]?: string;
|
|
32
|
+
};
|
|
33
|
+
/** Border configuration */
|
|
34
|
+
border?: {
|
|
35
|
+
color?: string;
|
|
36
|
+
width?: number;
|
|
37
|
+
radius?: number;
|
|
38
|
+
};
|
|
39
|
+
/** Text styling */
|
|
40
|
+
text?: {
|
|
41
|
+
color?: string;
|
|
42
|
+
fontSize?: number;
|
|
43
|
+
fontFamily?: string;
|
|
44
|
+
};
|
|
45
|
+
/** Sprite textures for each state (alternative to backgroundColor) */
|
|
46
|
+
textures?: {
|
|
47
|
+
[ButtonState.Normal]?: string;
|
|
48
|
+
[ButtonState.Hover]?: string;
|
|
49
|
+
[ButtonState.Pressed]?: string;
|
|
50
|
+
[ButtonState.Disabled]?: string;
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Properties for the Button component
|
|
56
|
+
*/
|
|
57
|
+
export interface ButtonProps {
|
|
58
|
+
/** Button text content */
|
|
59
|
+
text?: string;
|
|
60
|
+
/** Button disabled state */
|
|
61
|
+
disabled?: boolean;
|
|
62
|
+
/** Click event handler */
|
|
63
|
+
click?: (event: FederatedPointerEvent) => void;
|
|
64
|
+
/** Hover enter event handler */
|
|
65
|
+
hoverEnter?: (event: FederatedPointerEvent) => void;
|
|
66
|
+
/** Hover leave event handler */
|
|
67
|
+
hoverLeave?: (event: FederatedPointerEvent) => void;
|
|
68
|
+
/** Press down event handler */
|
|
69
|
+
pressDown?: (event: FederatedPointerEvent) => void;
|
|
70
|
+
/** Press up event handler */
|
|
71
|
+
pressUp?: (event: FederatedPointerEvent) => void;
|
|
72
|
+
/** Visual style configuration */
|
|
73
|
+
style?: ButtonStyle;
|
|
74
|
+
/** Button width */
|
|
75
|
+
width?: number;
|
|
76
|
+
/** Button height */
|
|
77
|
+
height?: number;
|
|
78
|
+
/** Button position X */
|
|
79
|
+
x?: number;
|
|
80
|
+
/** Button position Y */
|
|
81
|
+
y?: number;
|
|
82
|
+
/** Button alpha/opacity */
|
|
83
|
+
alpha?: number;
|
|
84
|
+
/** Button visibility */
|
|
85
|
+
visible?: boolean;
|
|
86
|
+
/** Button cursor */
|
|
87
|
+
cursor?: string;
|
|
88
|
+
/** Controls instance to automatically apply button events to (e.g., ControlsDirective or JoystickControls) */
|
|
89
|
+
controls?: ControlsDirective | JoystickControls | any;
|
|
90
|
+
/** Name of the control to trigger with applyControl when button is clicked */
|
|
91
|
+
controlName?: string;
|
|
92
|
+
/** Shape of the button background: 'rect', 'circle', or 'ellipse' */
|
|
93
|
+
shape?: 'rect' | 'circle' | 'ellipse';
|
|
94
|
+
/** Custom background component or element (replaces default background if provided) */
|
|
95
|
+
background?: Element | any;
|
|
96
|
+
/** Custom children components for button content (takes priority over text if provided) */
|
|
97
|
+
children?: Element[];
|
|
98
|
+
/** Focus index for the button */
|
|
99
|
+
tabindex?: number | Signal<number>;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Creates a Button component with interactive states and customizable styling.
|
|
104
|
+
*
|
|
105
|
+
* This component provides a fully interactive button with visual feedback
|
|
106
|
+
* for different states (normal, hover, pressed, disabled). It supports both
|
|
107
|
+
* sprite-based and graphics-based rendering approaches.
|
|
108
|
+
*
|
|
109
|
+
* The button is built using a Container with background and text elements,
|
|
110
|
+
* providing reactive state management and event handling.
|
|
111
|
+
*
|
|
112
|
+
* ## Features
|
|
113
|
+
*
|
|
114
|
+
* - **Controls Integration**: Automatically trigger controls via `applyControl` when clicked
|
|
115
|
+
* - **Multiple Shapes**: Support for rect, circle, and ellipse shapes
|
|
116
|
+
* - **Custom Content**: Use children components for custom button content
|
|
117
|
+
* - **Custom Background**: Provide a custom background component
|
|
118
|
+
*
|
|
119
|
+
* @param props - Button configuration including text, styling, controls, shape, and event handlers
|
|
120
|
+
* @returns A reactive Button component
|
|
121
|
+
* @example
|
|
122
|
+
* ```typescript
|
|
123
|
+
* // Simple button with text and click handler
|
|
124
|
+
* const simpleButton = Button({
|
|
125
|
+
* text: "Click Me",
|
|
126
|
+
* click: () => console.log("Button clicked!"),
|
|
127
|
+
* width: 150,
|
|
128
|
+
* height: 50
|
|
129
|
+
* });
|
|
130
|
+
*
|
|
131
|
+
* // Button with controls integration
|
|
132
|
+
* const jumpButton = Button({
|
|
133
|
+
* text: "Jump",
|
|
134
|
+
* controls: controlsInstance,
|
|
135
|
+
* controlName: "jump",
|
|
136
|
+
* width: 120,
|
|
137
|
+
* height: 40
|
|
138
|
+
* });
|
|
139
|
+
*
|
|
140
|
+
* // Circular button
|
|
141
|
+
* const circleButton = Button({
|
|
142
|
+
* text: "Action",
|
|
143
|
+
* shape: "circle",
|
|
144
|
+
* width: 100,
|
|
145
|
+
* height: 100
|
|
146
|
+
* });
|
|
147
|
+
*
|
|
148
|
+
* // Button with custom content (children)
|
|
149
|
+
* const customButton = Button({
|
|
150
|
+
* shape: "circle",
|
|
151
|
+
* width: 80,
|
|
152
|
+
* height: 80,
|
|
153
|
+
* children: [
|
|
154
|
+
* h(Sprite, { image: "icon.png", width: 50, height: 50 })
|
|
155
|
+
* ]
|
|
156
|
+
* });
|
|
157
|
+
*
|
|
158
|
+
* // Styled button with custom colors
|
|
159
|
+
* const styledButton = Button({
|
|
160
|
+
* text: "Styled Button",
|
|
161
|
+
* style: {
|
|
162
|
+
* backgroundColor: {
|
|
163
|
+
* normal: "#28a745",
|
|
164
|
+
* hover: "#218838",
|
|
165
|
+
* pressed: "#1e7e34",
|
|
166
|
+
* disabled: "#6c757d"
|
|
167
|
+
* },
|
|
168
|
+
* border: {
|
|
169
|
+
* radius: 8,
|
|
170
|
+
* width: 2,
|
|
171
|
+
* color: "#ffffff"
|
|
172
|
+
* },
|
|
173
|
+
* text: {
|
|
174
|
+
* fontSize: 18,
|
|
175
|
+
* color: "#ffffff"
|
|
176
|
+
* }
|
|
177
|
+
* }
|
|
178
|
+
* });
|
|
179
|
+
*
|
|
180
|
+
* // Sprite-based button
|
|
181
|
+
* const spriteButton = Button({
|
|
182
|
+
* text: "Play Game",
|
|
183
|
+
* style: {
|
|
184
|
+
* textures: {
|
|
185
|
+
* normal: "/assets/button-normal.png",
|
|
186
|
+
* hover: "/assets/button-hover.png",
|
|
187
|
+
* pressed: "/assets/button-pressed.png"
|
|
188
|
+
* }
|
|
189
|
+
* }
|
|
190
|
+
* });
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
export function Button(props: ButtonProps) {
|
|
194
|
+
// Internal state signals
|
|
195
|
+
const currentState = signal(ButtonState.Normal);
|
|
196
|
+
const isPressed = signal(false);
|
|
197
|
+
const isHovered = signal(false);
|
|
198
|
+
|
|
199
|
+
// Define reactive props with defaults
|
|
200
|
+
const defineProps = useDefineProps(props);
|
|
201
|
+
const { text, disabled, width, height, style, shape, controlName } = defineProps({
|
|
202
|
+
text: {
|
|
203
|
+
type: String,
|
|
204
|
+
default: ""
|
|
205
|
+
},
|
|
206
|
+
disabled: {
|
|
207
|
+
type: Boolean,
|
|
208
|
+
default: false
|
|
209
|
+
},
|
|
210
|
+
width: {
|
|
211
|
+
type: Number,
|
|
212
|
+
default: 120
|
|
213
|
+
},
|
|
214
|
+
height: {
|
|
215
|
+
type: Number,
|
|
216
|
+
default: 40
|
|
217
|
+
},
|
|
218
|
+
style: {
|
|
219
|
+
type: Object,
|
|
220
|
+
default: () => ({})
|
|
221
|
+
},
|
|
222
|
+
shape: {
|
|
223
|
+
type: String,
|
|
224
|
+
default: "rect"
|
|
225
|
+
},
|
|
226
|
+
controlName: {
|
|
227
|
+
type: String,
|
|
228
|
+
default: undefined
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
// Helper function to get controls instance (handles signals like Joystick)
|
|
233
|
+
const getControls = () => {
|
|
234
|
+
if (!props.controls) return null;
|
|
235
|
+
if (isSignal(props.controls)) {
|
|
236
|
+
return props.controls();
|
|
237
|
+
}
|
|
238
|
+
return props.controls;
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
// Update button state based on disabled and interaction states
|
|
242
|
+
effect(() => {
|
|
243
|
+
const isDisabled = disabled();
|
|
244
|
+
const pressed = isPressed();
|
|
245
|
+
const hovered = isHovered();
|
|
246
|
+
|
|
247
|
+
if (isDisabled) {
|
|
248
|
+
currentState.set(ButtonState.Disabled);
|
|
249
|
+
} else if (pressed) {
|
|
250
|
+
currentState.set(ButtonState.Pressed);
|
|
251
|
+
} else if (hovered) {
|
|
252
|
+
currentState.set(ButtonState.Hover);
|
|
253
|
+
} else {
|
|
254
|
+
currentState.set(ButtonState.Normal);
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
// Event handlers
|
|
259
|
+
const eventHandlers = {
|
|
260
|
+
pointerenter: (event: FederatedPointerEvent) => {
|
|
261
|
+
if (!disabled()) {
|
|
262
|
+
isHovered.set(true);
|
|
263
|
+
props.hoverEnter?.(event);
|
|
264
|
+
}
|
|
265
|
+
},
|
|
266
|
+
pointerleave: (event: FederatedPointerEvent) => {
|
|
267
|
+
isHovered.set(false);
|
|
268
|
+
isPressed.set(false);
|
|
269
|
+
props.hoverLeave?.(event);
|
|
270
|
+
},
|
|
271
|
+
pointerdown: async (event: FederatedPointerEvent) => {
|
|
272
|
+
if (!disabled()) {
|
|
273
|
+
isPressed.set(true);
|
|
274
|
+
props.pressDown?.(event);
|
|
275
|
+
|
|
276
|
+
// Apply control if controls and controlName are provided
|
|
277
|
+
const controls = getControls();
|
|
278
|
+
const name = controlName();
|
|
279
|
+
if (controls && name && controls.applyControl) {
|
|
280
|
+
await controls.applyControl(name, true);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
},
|
|
284
|
+
pointerup: async (event: FederatedPointerEvent) => {
|
|
285
|
+
if (!disabled() && isPressed()) {
|
|
286
|
+
isPressed.set(false);
|
|
287
|
+
props.pressUp?.(event);
|
|
288
|
+
|
|
289
|
+
// Apply control release if controls and controlName are provided
|
|
290
|
+
const controls = getControls();
|
|
291
|
+
const name = controlName();
|
|
292
|
+
if (controls && name && controls.applyControl) {
|
|
293
|
+
await controls.applyControl(name, false);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
},
|
|
297
|
+
pointertap: async (event: FederatedPointerEvent) => {
|
|
298
|
+
if (!disabled()) {
|
|
299
|
+
props.click?.(event);
|
|
300
|
+
|
|
301
|
+
// Apply control if controls and controlName are provided (press and release)
|
|
302
|
+
const controls = getControls();
|
|
303
|
+
const name = controlName();
|
|
304
|
+
if (controls && name && controls.applyControl) {
|
|
305
|
+
await controls.applyControl(name);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
// Generate background element
|
|
312
|
+
const getBackgroundElement = () => {
|
|
313
|
+
// If custom background is provided, use it
|
|
314
|
+
if (props.background) {
|
|
315
|
+
return props.background;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// Otherwise, use shape-based background
|
|
319
|
+
const currentShape = shape();
|
|
320
|
+
const bgColor = computed(() => {
|
|
321
|
+
const currentStyle = style();
|
|
322
|
+
const backgroundColor = currentStyle.backgroundColor || {
|
|
323
|
+
[ButtonState.Normal]: "#007bff",
|
|
324
|
+
[ButtonState.Hover]: "#0056b3",
|
|
325
|
+
[ButtonState.Pressed]: "#004085",
|
|
326
|
+
[ButtonState.Disabled]: "#6c757d"
|
|
327
|
+
};
|
|
328
|
+
const state = currentState();
|
|
329
|
+
return backgroundColor[state] || backgroundColor[ButtonState.Normal];
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
if (currentShape === 'circle') {
|
|
333
|
+
// For circle, use the smaller dimension as radius
|
|
334
|
+
const radius = computed(() => Math.min(width(), height()) / 2);
|
|
335
|
+
return h(Circle, {
|
|
336
|
+
radius: radius,
|
|
337
|
+
x: computed(() => width() / 2),
|
|
338
|
+
y: computed(() => height() / 2),
|
|
339
|
+
color: bgColor
|
|
340
|
+
});
|
|
341
|
+
} else if (currentShape === 'ellipse') {
|
|
342
|
+
return h(Ellipse, {
|
|
343
|
+
width: width,
|
|
344
|
+
height: height,
|
|
345
|
+
color: bgColor
|
|
346
|
+
});
|
|
347
|
+
} else {
|
|
348
|
+
// Default: rect
|
|
349
|
+
return h(Rect, {
|
|
350
|
+
width: width,
|
|
351
|
+
height: height,
|
|
352
|
+
color: bgColor
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
// Generate content element(s)
|
|
358
|
+
const getContentElements = () => {
|
|
359
|
+
// If children are provided, use them (priority over text)
|
|
360
|
+
if (props.children && props.children.length > 0) {
|
|
361
|
+
return props.children;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// Otherwise, use text
|
|
365
|
+
return [
|
|
366
|
+
h(Text, {
|
|
367
|
+
text: text,
|
|
368
|
+
x: computed(() => width() / 2),
|
|
369
|
+
y: computed(() => height() / 2),
|
|
370
|
+
anchor: { x: 0.5, y: 0.5 },
|
|
371
|
+
style: computed(() => {
|
|
372
|
+
const currentStyle = style();
|
|
373
|
+
const textStyle = currentStyle.text || {};
|
|
374
|
+
return {
|
|
375
|
+
fontSize: textStyle.fontSize || 16,
|
|
376
|
+
fontFamily: textStyle.fontFamily || "Arial",
|
|
377
|
+
fill: textStyle.color || "#ffffff"
|
|
378
|
+
};
|
|
379
|
+
})()
|
|
380
|
+
})
|
|
381
|
+
];
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
// Return Container with h() children
|
|
385
|
+
return h(Container, {
|
|
386
|
+
x: props.x,
|
|
387
|
+
y: props.y,
|
|
388
|
+
width: props.width,
|
|
389
|
+
height: props.height,
|
|
390
|
+
alpha: props.alpha,
|
|
391
|
+
visible: props.visible,
|
|
392
|
+
cursor: props.cursor || "pointer",
|
|
393
|
+
tabindex: props.tabindex,
|
|
394
|
+
...eventHandlers
|
|
395
|
+
}, [
|
|
396
|
+
getBackgroundElement(),
|
|
397
|
+
...getContentElements()
|
|
398
|
+
]);
|
|
399
|
+
}
|
package/src/components/Canvas.ts
CHANGED
|
@@ -1,25 +1,30 @@
|
|
|
1
1
|
import { effect, Signal, signal } from "@signe/reactive";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
2
|
+
import { Application, Container } from "pixi.js";
|
|
3
|
+
import {
|
|
4
|
+
Props,
|
|
5
|
+
createComponent,
|
|
6
|
+
registerComponent,
|
|
7
|
+
Element,
|
|
8
|
+
} from "../engine/reactive";
|
|
5
9
|
import { useProps } from "../hooks/useProps";
|
|
6
10
|
import { ComponentInstance, DisplayObject } from "./DisplayObject";
|
|
7
11
|
import { ComponentFunction } from "../engine/signal";
|
|
8
12
|
import { SignalOrPrimitive } from "./types";
|
|
9
13
|
import { Size } from "./types/DisplayObject";
|
|
10
14
|
import { Scheduler, Tick } from "../directives/Scheduler";
|
|
15
|
+
import { GlobalAssetLoader } from "../utils/GlobalAssetLoader";
|
|
11
16
|
|
|
12
17
|
interface CanvasElement extends Element<ComponentInstance> {
|
|
13
|
-
render: (rootElement: HTMLElement) => void;
|
|
18
|
+
render: (rootElement: HTMLElement, app?: Application) => void;
|
|
14
19
|
directives: {
|
|
15
|
-
tick: Scheduler
|
|
20
|
+
tick: Scheduler;
|
|
16
21
|
};
|
|
17
22
|
propObservables: {
|
|
18
|
-
tick: Signal<Tick
|
|
23
|
+
tick: Signal<Tick>;
|
|
19
24
|
};
|
|
20
25
|
}
|
|
21
26
|
|
|
22
|
-
registerComponent("Canvas", class Canvas extends DisplayObject(Container) {});
|
|
27
|
+
registerComponent("Canvas", class Canvas extends DisplayObject(Container) { });
|
|
23
28
|
|
|
24
29
|
export interface CanvasProps extends Props {
|
|
25
30
|
cursorStyles?: () => any;
|
|
@@ -30,33 +35,28 @@ export interface CanvasProps extends Props {
|
|
|
30
35
|
isRoot?: boolean;
|
|
31
36
|
tick?: any;
|
|
32
37
|
class?: SignalOrPrimitive<string>;
|
|
38
|
+
background?: string;
|
|
33
39
|
}
|
|
34
40
|
|
|
35
41
|
export const Canvas: ComponentFunction<CanvasProps> = async (props = {}) => {
|
|
36
42
|
let { cursorStyles, width, height, class: className } = useProps(props);
|
|
37
|
-
const Yoga = await loadYoga();
|
|
38
43
|
|
|
39
|
-
if (!props.width) width = signal<Size>(800)
|
|
40
|
-
if (!props.height) height = signal<Size>(600)
|
|
41
|
-
|
|
42
|
-
const renderer = await autoDetectRenderer({
|
|
43
|
-
...props,
|
|
44
|
-
width: width?.(),
|
|
45
|
-
height: height?.(),
|
|
46
|
-
});
|
|
44
|
+
if (!props.width) width = signal<Size>(800);
|
|
45
|
+
if (!props.height) height = signal<Size>(600);
|
|
47
46
|
|
|
48
47
|
const canvasSize = signal({
|
|
49
|
-
width:
|
|
50
|
-
height:
|
|
48
|
+
width: 0,
|
|
49
|
+
height: 0,
|
|
51
50
|
});
|
|
52
51
|
|
|
53
52
|
props.isRoot = true;
|
|
53
|
+
const globalLoader = new GlobalAssetLoader();
|
|
54
54
|
const options: CanvasProps = {
|
|
55
55
|
...props,
|
|
56
56
|
context: {
|
|
57
|
-
Yoga,
|
|
58
|
-
renderer,
|
|
59
57
|
canvasSize,
|
|
58
|
+
app: signal(null),
|
|
59
|
+
globalLoader,
|
|
60
60
|
},
|
|
61
61
|
width: width?.(),
|
|
62
62
|
height: height?.(),
|
|
@@ -69,10 +69,21 @@ export const Canvas: ComponentFunction<CanvasProps> = async (props = {}) => {
|
|
|
69
69
|
frame: 0,
|
|
70
70
|
deltaRatio: 1,
|
|
71
71
|
});
|
|
72
|
+
} else {
|
|
73
|
+
options.context!.tick = props.tick;
|
|
72
74
|
}
|
|
75
|
+
|
|
76
|
+
// Register the tick signal globally so animatedSignal can use it by default
|
|
77
|
+
(globalThis as any).__CANVAS_ENGINE_TICK__ = options.context!.tick;
|
|
78
|
+
|
|
73
79
|
const canvasElement = createComponent("Canvas", options) as CanvasElement;
|
|
74
80
|
|
|
75
|
-
canvasElement.render = (rootElement: HTMLElement) => {
|
|
81
|
+
canvasElement.render = (rootElement: HTMLElement, app?: Application) => {
|
|
82
|
+
if (!app) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const renderer = app.renderer;
|
|
76
87
|
const canvasEl = renderer.view.canvas as HTMLCanvasElement;
|
|
77
88
|
|
|
78
89
|
(globalThis as any).__PIXI_STAGE__ = canvasElement.componentInstance;
|
|
@@ -85,6 +96,34 @@ export const Canvas: ComponentFunction<CanvasProps> = async (props = {}) => {
|
|
|
85
96
|
renderer.render(canvasElement.componentInstance as any);
|
|
86
97
|
});
|
|
87
98
|
|
|
99
|
+
app.stage = canvasElement.componentInstance as any;
|
|
100
|
+
|
|
101
|
+
app.stage.layout = {
|
|
102
|
+
width: app.screen.width,
|
|
103
|
+
height: app.screen.height,
|
|
104
|
+
justifyContent: props.justifyContent,
|
|
105
|
+
alignItems: props.alignItems,
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
canvasSize.set({ width: app.screen.width, height: app.screen.height })
|
|
109
|
+
|
|
110
|
+
app.renderer.on('resize', (width: number, height: number) => {
|
|
111
|
+
canvasSize.set({ width, height });
|
|
112
|
+
|
|
113
|
+
if (app.stage.layout) {
|
|
114
|
+
app.stage.layout = {
|
|
115
|
+
width,
|
|
116
|
+
height
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
if (props.tickStart !== false) canvasElement.directives.tick.start();
|
|
122
|
+
|
|
123
|
+
app.ticker.add(() => {
|
|
124
|
+
canvasElement.propObservables!.tick();
|
|
125
|
+
});
|
|
126
|
+
|
|
88
127
|
if (cursorStyles) {
|
|
89
128
|
effect(() => {
|
|
90
129
|
renderer.events.cursorStyles = cursorStyles();
|
|
@@ -97,37 +136,14 @@ export const Canvas: ComponentFunction<CanvasProps> = async (props = {}) => {
|
|
|
97
136
|
});
|
|
98
137
|
}
|
|
99
138
|
|
|
100
|
-
const
|
|
101
|
-
let w, h;
|
|
102
|
-
if (width?.() === "100%" && height?.() === "100%") {
|
|
103
|
-
const parent = canvasEl.parentElement;
|
|
104
|
-
w = parent ? parent.clientWidth : window.innerWidth;
|
|
105
|
-
h = parent ? parent.clientHeight : window.innerHeight;
|
|
106
|
-
} else {
|
|
107
|
-
w = width?.() ?? canvasEl.offsetWidth;
|
|
108
|
-
h = height?.() ?? canvasEl.offsetHeight;
|
|
109
|
-
}
|
|
110
|
-
renderer.resize(w, h);
|
|
111
|
-
canvasSize.set({ width: w, height: h });
|
|
112
|
-
canvasElement.componentInstance.setWidth(w)
|
|
113
|
-
canvasElement.componentInstance.setHeight(h)
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
// Listen for window resize events
|
|
117
|
-
window.addEventListener("resize", resizeCanvas);
|
|
118
|
-
|
|
119
|
-
// Check if a canvas already exists in the rootElement
|
|
120
|
-
const existingCanvas = rootElement.querySelector('canvas');
|
|
139
|
+
const existingCanvas = rootElement.querySelector("canvas");
|
|
121
140
|
if (existingCanvas) {
|
|
122
|
-
// If it exists, replace it with the new canvas
|
|
123
141
|
rootElement.replaceChild(canvasEl, existingCanvas);
|
|
124
142
|
} else {
|
|
125
|
-
// If it doesn't exist, append the new canvas
|
|
126
143
|
rootElement.appendChild(canvasEl);
|
|
127
144
|
}
|
|
128
145
|
|
|
129
|
-
|
|
130
|
-
resizeCanvas();
|
|
146
|
+
options.context!.app.set(app)
|
|
131
147
|
};
|
|
132
148
|
|
|
133
149
|
return canvasElement;
|
|
@@ -4,6 +4,7 @@ import { DisplayObject } from "./DisplayObject";
|
|
|
4
4
|
import { ComponentFunction } from "../engine/signal";
|
|
5
5
|
import { DisplayObjectProps } from "./types/DisplayObject";
|
|
6
6
|
import { setObservablePoint } from "../engine/utils";
|
|
7
|
+
import { isPercent } from "../utils/functions";
|
|
7
8
|
|
|
8
9
|
interface ContainerProps extends DisplayObjectProps {
|
|
9
10
|
sortableChildren?: boolean;
|
|
@@ -25,8 +26,8 @@ export class CanvasContainer extends DisplayObject(PixiContainer) {
|
|
|
25
26
|
this.sortableChildren = props.sortableChildren;
|
|
26
27
|
}
|
|
27
28
|
}
|
|
28
|
-
onMount(args) {
|
|
29
|
-
super.onMount(args);
|
|
29
|
+
async onMount(args) {
|
|
30
|
+
await super.onMount(args);
|
|
30
31
|
const { componentInstance, props } = args;
|
|
31
32
|
const { pixiChildren } = props;
|
|
32
33
|
if (pixiChildren) {
|
|
@@ -34,6 +35,22 @@ export class CanvasContainer extends DisplayObject(PixiContainer) {
|
|
|
34
35
|
componentInstance.addChild(child);
|
|
35
36
|
});
|
|
36
37
|
}
|
|
38
|
+
|
|
39
|
+
// Listen to layout events to update displayWidth and displayHeight with computed values
|
|
40
|
+
const isWidthPercentage = isPercent(props.width);
|
|
41
|
+
const isHeightPercentage = isPercent(props.height);
|
|
42
|
+
|
|
43
|
+
if (isWidthPercentage || isHeightPercentage) {
|
|
44
|
+
this.on('layout', (event) => {
|
|
45
|
+
const layoutBox = event.computedLayout;
|
|
46
|
+
if (isWidthPercentage && layoutBox.width !== undefined) {
|
|
47
|
+
this.displayWidth.set(layoutBox.width);
|
|
48
|
+
}
|
|
49
|
+
if (isHeightPercentage && layoutBox.height !== undefined) {
|
|
50
|
+
this.displayHeight.set(layoutBox.height);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
37
54
|
}
|
|
38
55
|
}
|
|
39
56
|
|
|
@@ -42,5 +59,7 @@ export interface CanvasContainer extends DisplayObjectProps {}
|
|
|
42
59
|
registerComponent("Container", CanvasContainer);
|
|
43
60
|
|
|
44
61
|
export const Container: ComponentFunction<ContainerProps> = (props) => {
|
|
62
|
+
// Ensure component is registered (useful in tests if module cache differs)
|
|
63
|
+
registerComponent("Container", CanvasContainer);
|
|
45
64
|
return createComponent("Container", props);
|
|
46
65
|
};
|