@safe-engine/pixi 1.0.2 → 7.0.1

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.
Files changed (48) hide show
  1. package/.github/workflows/npm-publish.yml +35 -0
  2. package/README.md +4 -4
  3. package/dist/app.d.ts +3 -2
  4. package/dist/app.d.ts.map +1 -1
  5. package/dist/app.js +41 -81
  6. package/dist/components/GUIComponent.d.ts +15 -14
  7. package/dist/components/GUIComponent.d.ts.map +1 -1
  8. package/dist/components/GUIComponent.js +68 -138
  9. package/dist/components/NodeComp.d.ts +6 -5
  10. package/dist/components/NodeComp.d.ts.map +1 -1
  11. package/dist/components/NodeComp.js +223 -305
  12. package/dist/components/RenderComponent.d.ts +8 -7
  13. package/dist/components/RenderComponent.d.ts.map +1 -1
  14. package/dist/components/RenderComponent.js +25 -56
  15. package/dist/core/Color.js +1 -1
  16. package/dist/core/LoadingBar.d.ts +9 -1
  17. package/dist/core/LoadingBar.d.ts.map +1 -1
  18. package/dist/core/LoadingBar.js +50 -46
  19. package/dist/core/Size.js +3 -6
  20. package/dist/core/Vec2.d.ts +20 -0
  21. package/dist/core/Vec2.d.ts.map +1 -0
  22. package/dist/core/Vec2.js +70 -0
  23. package/dist/helper/html-text-parser.js +34 -34
  24. package/dist/helper/utils.d.ts +1 -1
  25. package/dist/helper/utils.d.ts.map +1 -1
  26. package/dist/helper/utils.js +20 -24
  27. package/dist/index.d.ts +4 -6
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.js +4 -18
  30. package/dist/systems/GUISystem.d.ts +2 -2
  31. package/dist/systems/GUISystem.d.ts.map +1 -1
  32. package/dist/systems/GUISystem.js +39 -40
  33. package/dist/systems/RenderSystem.d.ts +0 -1
  34. package/dist/systems/RenderSystem.d.ts.map +1 -1
  35. package/dist/systems/RenderSystem.js +24 -35
  36. package/package.json +4 -3
  37. package/src/app.ts +53 -0
  38. package/src/components/GUIComponent.ts +141 -0
  39. package/src/components/NodeComp.ts +416 -0
  40. package/src/components/RenderComponent.ts +66 -0
  41. package/src/core/Color.ts +3 -0
  42. package/src/core/LoadingBar.ts +63 -0
  43. package/src/core/Size.ts +21 -0
  44. package/src/helper/html-text-parser.ts +364 -0
  45. package/src/index.ts +7 -0
  46. package/src/systems/GUISystem.ts +80 -0
  47. package/src/systems/RenderSystem.ts +70 -0
  48. package/tsconfig.json +24 -0
@@ -0,0 +1,416 @@
1
+ import { BaseNode, ComponentType, EnhancedComponent } from '@safe-engine/core'
2
+ import { Constructor, Entity } from 'entityx-ts'
3
+ import remove from 'lodash/remove'
4
+ import { Color, ColorSource, Container, Point, Sprite } from 'pixi.js'
5
+ import { Action, actionManager, Animation } from 'pixi-action-ease'
6
+
7
+ import { Size } from '../core/Size'
8
+ import { ProgressBarComp } from './GUIComponent'
9
+
10
+ export type EventCallbackType = (...args) => void
11
+ export interface EventMap {
12
+ [key: string]: [EventCallbackType]
13
+ }
14
+
15
+ type TouchEVentCallback = (target: { location: Point }) => void
16
+
17
+ export class NodeComp<C extends Container = Container> implements BaseNode<C> {
18
+ entity: Entity
19
+ instance: C
20
+ events: EventMap = {}
21
+ data: { [key: string]: any } = {}
22
+ parent: NodeComp
23
+ children: NodeComp[] = []
24
+ actionsList: Animation[] = []
25
+ // offset: cc.Point = cc.v2(0, 0);
26
+ name: string
27
+ private lastMove: { x: number; y: number }
28
+ private _group = 0
29
+
30
+ onTouchStart?: TouchEVentCallback
31
+ onTouchMove?: TouchEVentCallback
32
+ onTouchEnd?: TouchEVentCallback
33
+ onTouchCancel?: TouchEVentCallback
34
+
35
+ setOnTouchStart(cb: TouchEVentCallback) {
36
+ this.onTouchStart = cb
37
+ this.instance.on('touchstart', (event) => {
38
+ const { global } = event
39
+ this.onTouchStart({ location: global })
40
+ })
41
+ }
42
+
43
+ setOnTouchMove(cb: TouchEVentCallback) {
44
+ this.onTouchMove = cb
45
+ this.instance.on('touchmove', (event) => {
46
+ const { global } = event
47
+ this.onTouchMove({ location: global })
48
+ })
49
+ }
50
+
51
+ setOnTouchEnd(cb: TouchEVentCallback) {
52
+ this.onTouchEnd = cb
53
+ this.instance.on('touchend', (event) => {
54
+ const { global } = event
55
+ this.onTouchEnd({ location: global })
56
+ })
57
+ }
58
+
59
+ setOnTouchCancel(cb: TouchEVentCallback) {
60
+ this.onTouchCancel = cb
61
+ this.instance.on('touchcancel', (event) => {
62
+ const { global } = event
63
+ this.onTouchCancel({ location: global })
64
+ })
65
+ }
66
+
67
+ constructor(instance: C, entity: Entity) {
68
+ this.entity = entity
69
+ this.instance = instance
70
+ this.instance.eventMode = 'static'
71
+ }
72
+
73
+ get uuid() {
74
+ return this.entity.id
75
+ }
76
+
77
+ get position(): Point {
78
+ return this.getPosition()
79
+ }
80
+
81
+ set position(val: Point) {
82
+ this.setPosition(val.x, val.y)
83
+ }
84
+
85
+ get x() {
86
+ return this.instance.x
87
+ }
88
+
89
+ set x(val: number) {
90
+ this.instance.x = val
91
+ }
92
+
93
+ get y() {
94
+ return this.instance.y
95
+ }
96
+
97
+ set y(val: number) {
98
+ this.instance.y = val
99
+ }
100
+
101
+ // get scale() {
102
+ // return this.instance.scale
103
+ // }
104
+
105
+ set scale(val: number) {
106
+ this.instance.scale = new Point(val, val)
107
+ }
108
+
109
+ get scaleX() {
110
+ return this.instance.scale.x
111
+ }
112
+
113
+ set scaleX(val: number) {
114
+ this.instance.scale.x = val
115
+ }
116
+
117
+ get scaleY() {
118
+ return this.instance.y
119
+ }
120
+
121
+ set scaleY(val: number) {
122
+ this.instance.y = val
123
+ }
124
+
125
+ get anchorX() {
126
+ if (this.instance instanceof Sprite)
127
+ return (this.instance as Sprite).anchor.x
128
+ return 0
129
+ }
130
+
131
+ set anchorX(val: number) {
132
+ if (this.instance instanceof Sprite) this.instance.anchor.x = val
133
+ }
134
+
135
+ get anchorY() {
136
+ if (this.instance instanceof Sprite)
137
+ return (this.instance as Sprite).anchor.y
138
+ return 0
139
+ } ß
140
+
141
+ set anchorY(val: number) {
142
+ if (this.instance instanceof Sprite) this.instance.anchor.y = val
143
+ }
144
+
145
+ /** rotation is in radians */
146
+ get rotation() {
147
+ return this.instance.rotation
148
+ }
149
+ /** rotation is in radians */
150
+ set rotation(val: number) {
151
+ this.instance.rotation = val
152
+ }
153
+
154
+ /** angle is in degrees. */
155
+ get angle() {
156
+ return this.instance.angle
157
+ }
158
+ /** angle is in degrees. */
159
+ set angle(val: number) {
160
+ this.instance.angle = val
161
+ }
162
+
163
+ get color() {
164
+ if (this.instance instanceof Sprite)
165
+ return (this.instance as Sprite).tint
166
+ return 0xffffff
167
+ }
168
+
169
+ set color(val: ColorSource) {
170
+ if (this.instance instanceof Sprite) this.instance.tint = val
171
+ }
172
+
173
+ get opacity() {
174
+ return this.instance.alpha
175
+ }
176
+
177
+ set opacity(val: number) {
178
+ this.instance.alpha = val
179
+ }
180
+
181
+ get active() {
182
+ return this.instance.visible && !this.instance.destroyed
183
+ }
184
+
185
+ set active(val: boolean) {
186
+ this.instance.visible = val
187
+ }
188
+
189
+ get group() {
190
+ return this._group
191
+ }
192
+
193
+ set group(val: number) {
194
+ this._group = val
195
+ }
196
+
197
+ get width() {
198
+ return this.instance.width
199
+ }
200
+
201
+ set width(val) {
202
+ this.instance.width = val
203
+ }
204
+
205
+ get height() {
206
+ return this.instance.height
207
+ }
208
+
209
+ set height(val) {
210
+ this.instance.height = val
211
+ }
212
+
213
+ get zIndex() {
214
+ return this.instance.zIndex
215
+ }
216
+
217
+ set zIndex(val) {
218
+ this.instance.zIndex = val
219
+ }
220
+
221
+ get childrenCount() {
222
+ return this.children.length
223
+ }
224
+
225
+ addComponent<T extends ComponentType>(instance): T {
226
+ return this.entity.assign(instance)
227
+ }
228
+
229
+ getComponent<T extends ComponentType>(component: Constructor<T>): T {
230
+ return this.entity.getComponent(component)
231
+ }
232
+
233
+ getComponentsInChildren<T extends ComponentType>(component: Constructor<T>): T[] {
234
+ if (!this.children.length) {
235
+ return []
236
+ }
237
+ const listHave = this.children.filter((child) => {
238
+ return child.getComponent(component)
239
+ })
240
+ return listHave.map((node) => node.getComponent(component))
241
+ }
242
+
243
+ getComponentInChildren<T extends ComponentType>(component: Constructor<T>): T {
244
+ return this.getComponentsInChildren(component)[0]
245
+ }
246
+
247
+ convertToNodeSpace(point: Point) {
248
+ return this.instance.toLocal(point)
249
+ }
250
+
251
+ convertToNodeSpaceAR(point: Point) {
252
+ return this.instance.toLocal(point)
253
+ }
254
+
255
+ convertToWorldSpaceAR(point: Point) {
256
+ return this.instance.toGlobal(point)
257
+ }
258
+
259
+ getPosition(): Point {
260
+ return this.instance.position
261
+ }
262
+
263
+ setPosition(x: number | Point, y?: number) {
264
+ if (typeof x !== 'number') {
265
+ this.x = x.x
266
+ this.y = x.y
267
+ } else {
268
+ this.x = x
269
+ this.y = y
270
+ }
271
+ }
272
+
273
+ setRotation(deg: number) {
274
+ this.instance.rotation = deg
275
+ }
276
+
277
+ getRotation() {
278
+ return this.instance.rotation
279
+ }
280
+
281
+ // setAnchorPoint(point: number | cc.Point, y?: number) {
282
+ // this.instance.setAnchorPoint(point, y)
283
+ // }
284
+
285
+ // getAnchorPoint() {
286
+ // return this.instance.getAnchorPoint()
287
+ // }
288
+
289
+ // getBoundingBox() {
290
+ // const box = this.instance.getBoundingBox()
291
+ // box.contains = function (point) {
292
+ // return this.x <= point.x && this.x + this.width >= point.x && this.y <= point.y && this.y + this.height >= point.y
293
+ // }
294
+ // return box
295
+ // }
296
+
297
+ getContentSize(): Size {
298
+ return this.instance.getBounds()
299
+ }
300
+
301
+ // setContentSize(size: cc.Size | number, height?: number) {
302
+ // this.instance.setContentSize(size, height)
303
+ // if (this.instance instanceof cc.ClippingNode) {
304
+ // const hw = ((size as any).width || size) * 0.5
305
+ // const hh = ((size as any).height || height) * 0.5
306
+ // const stencil = new cc.DrawNode()
307
+ // const rectangle = [cc.p(-hw, -hh), cc.p(hw, -hh), cc.p(hw, hh), cc.p(-hw, hh)]
308
+ // stencil.drawPoly(rectangle, cc.Color.WHITE, 0, cc.Color.WHITE)
309
+ // // stencil.drawDot(cc.p(-height * 0.5, -height * 0.5), height, cc.Color.WHITE);
310
+ // this.instance.stencil = stencil
311
+ // }
312
+ // }
313
+
314
+ setColor(color: Color) {
315
+ if (this.instance instanceof Sprite)
316
+ (this.instance as Sprite).tint = color
317
+ }
318
+
319
+ setScale(scaleX: number, scaleY?: number) {
320
+ this.instance.scale.x = scaleX
321
+ this.instance.scale.x = scaleY || scaleX
322
+ }
323
+
324
+ runAction(act: Action) {
325
+ const animation = actionManager.runAction(this.instance as any, act)
326
+ this.actionsList.push(animation)
327
+ }
328
+
329
+ stopAllActions() {
330
+ this.actionsList.forEach((act) => {
331
+ actionManager.cancelAction(act)
332
+ })
333
+ this.actionsList = []
334
+ }
335
+
336
+ pauseAllActions() {
337
+ this.actionsList.forEach((anim: Animation) => {
338
+ anim.isPause = true
339
+ })
340
+ }
341
+
342
+ resumeAllActions() {
343
+ this.actionsList.forEach((anim: Animation) => {
344
+ anim.isPause = false
345
+ })
346
+ }
347
+
348
+ destroy() {
349
+ if (this.parent) {
350
+ remove(this.parent.children, ({ entity }) => entity.id === this.entity.id)
351
+ }
352
+ this.children.forEach((child) => {
353
+ child.destroy()
354
+ })
355
+ this.parent = null
356
+ this.entity.destroy()
357
+ this.stopAllActions()
358
+ this.instance.destroy()
359
+ }
360
+
361
+ removeFromParent() {
362
+ this.active = false
363
+ this.stopAllActions()
364
+ this.instance.removeFromParent()
365
+ }
366
+
367
+ addChild(child: NodeComp, zOrder?: number) {
368
+ child.parent = this
369
+ child.active = true
370
+ this.children.push(child)
371
+ this.instance.addChild(child.instance)
372
+ if (zOrder) child.zIndex = zOrder
373
+ }
374
+
375
+ destroyAllChildren() {
376
+ this.children.forEach((child) => {
377
+ child.destroy()
378
+ })
379
+ }
380
+
381
+ on(name: string, callback: EventCallbackType, target?: any) {
382
+ const bound = target ? callback.bind(target) : callback
383
+ if (this.events[name]) {
384
+ this.events[name].push(bound)
385
+ } else {
386
+ this.events[name] = [bound]
387
+ }
388
+ }
389
+
390
+ off(name: string) {
391
+ this.events[name] = undefined
392
+ }
393
+
394
+ emit(name: string, ...params: any) {
395
+ if (this.events[name]) {
396
+ this.events[name].forEach((fc) => fc(...params))
397
+ }
398
+ }
399
+
400
+ resolveComponent(component: EnhancedComponent<NodeComp>) {
401
+ if ((component.constructor as any).hasRender) {
402
+ this.addChild(component.node)
403
+ } else {
404
+ this.addComponent(component)
405
+ if (component instanceof ProgressBarComp) {
406
+ this.addChild(component.node)
407
+ }
408
+ }
409
+ }
410
+ getData<T>(key: string): T {
411
+ return this.data[key]
412
+ }
413
+ setData<T>(key: string, val: T) {
414
+ this.data[key] = val
415
+ }
416
+ }
@@ -0,0 +1,66 @@
1
+ import { ComponentX } from '@safe-engine/core'
2
+ import { ColorSource, Graphics, Point, Sprite, Texture, TextureSource } from 'pixi.js'
3
+
4
+ import { LoadingBar, LoadingBarMode } from '../core/LoadingBar'
5
+ import { SpriteTypes } from '../systems/RenderSystem'
6
+ import { NodeComp } from './NodeComp'
7
+
8
+ export class NodeRender extends ComponentX<NodeComp> {
9
+ nodeName?: string
10
+ }
11
+
12
+ export class SpriteRender extends ComponentX<NodeComp<Sprite>> {
13
+ public spriteFrame: TextureSource
14
+ public type: SpriteTypes
15
+ public fillType: LoadingBarMode = LoadingBarMode.BAR
16
+ public fillRange = 1
17
+ public fillCenter: Point
18
+ loadingBar: LoadingBar
19
+
20
+ // set fillStart(val: number) {
21
+ // if (this.node.instance instanceof cc.ProgressTimer) {
22
+ // this.node.instance.setMidpoint(cc.v2(val, val));
23
+ // }
24
+ // }
25
+
26
+ setFillRange(val: number) {
27
+ if (this.loadingBar) {
28
+ this.loadingBar.progress = val
29
+ }
30
+ }
31
+
32
+ getSpriteFrame() {
33
+ return this.spriteFrame
34
+ }
35
+
36
+ setSpriteFrame(frame) {
37
+ this.spriteFrame = frame
38
+ const sprite = this.node.instance as Sprite
39
+ // if (this.node.instance instanceof cc.Sprite) {
40
+ sprite.texture = Texture.from(frame)
41
+ // sprite.texture.rotate = 8
42
+ // } else if (this.node.instance instanceof ccui.ImageView) {
43
+ // if (this.texType) {
44
+ // this.node.instance.loadTexture(frame, this.texType);
45
+ // } else {
46
+ // this.node.instance.loadTexture(frame);
47
+ // }
48
+ // const sprite = new cc.Sprite(frame);
49
+ // this.node.setContentSize(sprite.getContentSize());
50
+ // } else if (this.node.instance instanceof ccui.Button) {
51
+ // this.node.instance.loadTextureNormal(frame);
52
+ // }
53
+ }
54
+ }
55
+
56
+ export class GraphicsRender extends ComponentX<NodeComp<Graphics>> {
57
+ lineWidth = 2
58
+ strokeColor: ColorSource
59
+ fillColor: ColorSource
60
+ }
61
+
62
+ export class MaskRender extends ComponentX<NodeComp> {
63
+ type: number
64
+ segments: number
65
+ inverted: boolean
66
+ }
@@ -0,0 +1,3 @@
1
+ export function Color4B(r: number, g: number, b: number, a: number) {
2
+ return ({r, g, b, a})
3
+ }
@@ -0,0 +1,63 @@
1
+ import { Container, Graphics, Point, Sprite, Texture } from 'pixi.js'
2
+
3
+ export enum LoadingBarMode {
4
+ BAR,
5
+ RADIAL,
6
+ }
7
+
8
+ export class LoadingBar extends Graphics {
9
+ spriteComp: Sprite
10
+ mode: LoadingBarMode
11
+ fillCenter = new Point(0.5, 0.5)
12
+ constructor(mode: LoadingBarMode, spriteComp: Sprite) {
13
+ super()
14
+ this.spriteComp = spriteComp
15
+ this.mode = mode || LoadingBarMode.BAR
16
+ this.beginFill(0xffffff)
17
+ this.drawRect(0, 0, spriteComp.width, spriteComp.height)
18
+ spriteComp.mask = this
19
+ spriteComp.addChild(this)
20
+ }
21
+
22
+ set progress(val: number) {
23
+ this.clear()
24
+ this.beginFill(0xffffff)
25
+ if (this.mode === LoadingBarMode.BAR) {
26
+ const spriteComp = this.spriteComp
27
+ this.drawRect(0, 0, spriteComp.width * val, spriteComp.height)
28
+ // console.log('new length', spriteComp.width)
29
+ this.x = -spriteComp.width * 0.5
30
+ this.y = -spriteComp.height * 0.5
31
+ }
32
+ }
33
+ }
34
+
35
+ export class ProgressTimer extends Container {
36
+ graphics: Graphics
37
+ spriteComp: Sprite
38
+ mode: LoadingBarMode
39
+ fillCenter = new Point(0.5, 0.5)
40
+ constructor(mode: LoadingBarMode, spriteFrame: string) {
41
+ super()
42
+ const texture = Texture.from(spriteFrame)
43
+ this.spriteComp = Sprite.from(texture)
44
+ this.graphics = new Graphics()
45
+ this.mode = mode || LoadingBarMode.BAR
46
+ this.graphics.beginFill(0xffffff)
47
+ this.graphics.drawRect(0, 0, this.spriteComp.width, this.spriteComp.height)
48
+ this.spriteComp.mask = this.graphics
49
+ this.addChild(this.graphics)
50
+ this.addChild(this.spriteComp)
51
+ // this.graphics.x = -this.spriteComp.width * this.fillCenter.x
52
+ // this.graphics.y = -this.spriteComp.height * this.fillCenter.y
53
+ }
54
+
55
+ set progress(val: number) {
56
+ this.graphics.clear()
57
+ if (this.mode === LoadingBarMode.BAR) {
58
+ this.graphics.beginFill(0xffffff)
59
+ this.graphics.drawRect(0, 0, this.spriteComp.width * val, this.spriteComp.height)
60
+ // console.log('new length', this.width)
61
+ }
62
+ }
63
+ }
@@ -0,0 +1,21 @@
1
+ class _Size {
2
+ width: number
3
+ height: number
4
+ static ZERO
5
+ constructor(width = 0, height = 0) {
6
+ if (!(this instanceof _Size)) {
7
+ return new _Size(width, height)
8
+ }
9
+ if (height === undefined) {
10
+ this.width = (width as any).width
11
+ this.height = (width as any).height
12
+ }
13
+ this.width = width
14
+ this.height = height
15
+ }
16
+ }
17
+
18
+ export type Size = _Size
19
+ export function Size(x?: number, y?: number): Size {
20
+ return new _Size(x, y)
21
+ }