@lightningjs/blits 1.3.0 → 1.4.0

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.
@@ -23,6 +23,15 @@ import { Log } from '../../lib/log.js'
23
23
 
24
24
  export default {
25
25
  focus: {
26
+ value: function (e) {
27
+ Log.warn('this.focus is deprecated, use this.$focus instead')
28
+ return this.$focus(e)
29
+ },
30
+ writable: false,
31
+ enumerable: true,
32
+ configurable: false,
33
+ },
34
+ $focus: {
26
35
  value: function (e) {
27
36
  this[symbols.state].hasFocus = true
28
37
  Focus.set(this, e)
@@ -54,6 +63,15 @@ export default {
54
63
  configurable: false,
55
64
  },
56
65
  select: {
66
+ value: function (ref) {
67
+ Log.warn('this.select is deprecated, use this.$select instead')
68
+ return this.$select(ref)
69
+ },
70
+ writable: false,
71
+ enumerable: true,
72
+ configurable: false,
73
+ },
74
+ $select: {
57
75
  value: function (ref) {
58
76
  let selected = null
59
77
  this[symbols.children].forEach((child) => {
@@ -77,8 +95,8 @@ export default {
77
95
  },
78
96
  trigger: {
79
97
  value: function (key) {
80
- // trigger with force set to true
81
- trigger(this[symbols.originalState], key, true)
98
+ Log.warn('this.trigger is deprecated, use this.$trigger instead')
99
+ return this.$trigger(key)
82
100
  },
83
101
  writable: false,
84
102
  enumerable: true,
@@ -86,8 +104,17 @@ export default {
86
104
  },
87
105
  $trigger: {
88
106
  value: function (key) {
89
- Log.warn('this.$trigger is deprecated, use this.trigger instead')
90
- return this.trigger(key)
107
+ let target = this[symbols.originalState]
108
+ // when dot notation used, find the nested target
109
+ if (key.indexOf('.') > -1) {
110
+ const keys = key.split('.')
111
+ key = keys.pop(keys)
112
+ for (let i = 0; i < keys.length; i++) {
113
+ target = target[keys[i]]
114
+ }
115
+ }
116
+ // trigger with force set to true
117
+ trigger(target, key, true)
91
118
  },
92
119
  writable: false,
93
120
  enumerable: true,
@@ -265,7 +265,9 @@ declare namespace Component {
265
265
  /**
266
266
  * Listen to events emitted by other components
267
267
  */
268
- $listen: (event: string, callback: (args: any) => void) => void
268
+ $listen: {
269
+ (event: string, callback: (args: any) => void, priority?: number): void;
270
+ }
269
271
 
270
272
  /**
271
273
  * Emit events that other components can listen to
@@ -300,6 +302,11 @@ declare namespace Component {
300
302
  /**
301
303
  * Set focus to the Component, optionally pass a KeyboardEvent for instant event bubbling
302
304
  */
305
+ $focus: (event?: KeyboardEvent) => void
306
+ /**
307
+ * @deprecated
308
+ * Deprecated: use `this.$focus()` instead
309
+ */
303
310
  focus: (event?: KeyboardEvent) => void
304
311
 
305
312
  /**
@@ -317,6 +324,12 @@ declare namespace Component {
317
324
  * }
318
325
  * ```
319
326
  */
327
+ $select: (ref: string) => ComponentInstance | ElementInstance
328
+
329
+ /**
330
+ * @deprecated
331
+ * Deprecated: use `this.$select()` instead
332
+ */
320
333
  select: (ref: string) => ComponentInstance | ElementInstance
321
334
 
322
335
  /**
@@ -345,6 +358,7 @@ declare namespace Component {
345
358
 
346
359
  export interface ElementInstance {
347
360
  focus?: () => void
361
+ $focus?: () => void
348
362
  }
349
363
 
350
364
  export interface ComponentConfig<Props extends string, S, M> {
package/src/component.js CHANGED
@@ -171,11 +171,24 @@ const Component = (name = required('name'), config = required('config')) => {
171
171
  // setup watchers if the components has watchers specified
172
172
  if (this[symbols.watchers]) {
173
173
  Object.keys(this[symbols.watchers]).forEach((watchKey) => {
174
- let old = this[watchKey]
174
+ let target = this
175
+ let key = watchKey
176
+ // when dot notation used, find the nested target
177
+ if (watchKey.indexOf('.') > -1) {
178
+ const keys = watchKey.split('.')
179
+ key = keys.pop()
180
+ for (let i = 0; i < keys.length; i++) {
181
+ target = target[keys[i]]
182
+ }
183
+ }
184
+
185
+ let old = this[key]
186
+
175
187
  effect((force = false) => {
176
- if (old !== this[watchKey] || force === true) {
177
- this[symbols.watchers][watchKey].apply(this, [this[watchKey], old])
178
- old = this[watchKey]
188
+ const newValue = target[key]
189
+ if (old !== newValue || force === true) {
190
+ this[symbols.watchers][watchKey].apply(this, [newValue, old])
191
+ old = newValue
179
192
  }
180
193
  })
181
194
  })
@@ -199,8 +212,8 @@ const Component = (name = required('name'), config = required('config')) => {
199
212
  }
200
213
 
201
214
  const factory = (options = {}, parentEl, parentComponent, rootComponent) => {
202
- // Register user defined plugins once on the Base object
203
- if (Base[symbols['pluginsRegistered']] === false) {
215
+ if (Base[symbols['launched']] === false) {
216
+ // Register user defined plugins once on the Base object (after launch)
204
217
  Object.keys(plugins).forEach((pluginName) => {
205
218
  const prefixedPluginName = `$${pluginName}`
206
219
  if (prefixedPluginName in Base) {
@@ -219,7 +232,12 @@ const Component = (name = required('name'), config = required('config')) => {
219
232
  configurable: false,
220
233
  })
221
234
  })
222
- Base[symbols['pluginsRegistered']] = true
235
+
236
+ // register global components once
237
+ globalComponents = components()
238
+
239
+ // mark launched true
240
+ Base[symbols['launched']] = true
223
241
  }
224
242
 
225
243
  // setup the component once per component type, using Base as the prototype
@@ -234,11 +252,6 @@ const Component = (name = required('name'), config = required('config')) => {
234
252
  config.code = codegenerator.call(config, parser(config.template, name))
235
253
  }
236
254
 
237
- // register global components once
238
- if (!globalComponents) {
239
- globalComponents = components()
240
- }
241
-
242
255
  // create an instance of the component, using base as the prototype (which contains Base)
243
256
  return component.call(Object.create(base), options, parentEl, parentComponent, rootComponent)
244
257
  }
@@ -37,7 +37,7 @@ export default () => {
37
37
  ___layout() {
38
38
  let offset = 0
39
39
 
40
- this.select('slot').children.forEach((el) => {
40
+ this.$select('slot').children.forEach((el) => {
41
41
  el.set(this.direction === 'vertical' ? 'y' : 'x', offset)
42
42
  // todo: grab width from interface, not directly from node
43
43
  offset +=
@@ -22,7 +22,7 @@ export default () => {
22
22
  ___layout() {
23
23
  let offset = 0
24
24
 
25
- this.select('slot').children.forEach((el) => {
25
+ this.$select('slot').children.forEach((el) => {
26
26
  el.set(this.direction === 'vertical' ? 'y' : 'x', offset)
27
27
  // todo: grab width from interface, not directly from node
28
28
  offset +=
@@ -55,7 +55,7 @@ export default () =>
55
55
  window.removeEventListener('hashchange', handler, false)
56
56
  },
57
57
  focus() {
58
- this.activeView && this.activeView.focus()
58
+ this.activeView && this.activeView.$focus()
59
59
  },
60
60
  },
61
61
  input: {
@@ -40,7 +40,7 @@ export default () =>
40
40
  window.removeEventListener('hashchange', handler, false)
41
41
  },
42
42
  focus() {
43
- this.activeView && this.activeView.focus()
43
+ this.activeView && this.activeView.$focus()
44
44
  },
45
45
  },
46
46
  input: {
@@ -125,6 +125,28 @@ const propsTransformer = {
125
125
  this.props['color'] = 0xffffffff
126
126
  }
127
127
  },
128
+ set fit(v) {
129
+ const resizeMode = {}
130
+
131
+ if (v === 'cover' || v === 'contain') {
132
+ this.props['textureOptions'] = { resizeMode: { type: v } }
133
+ return
134
+ }
135
+
136
+ if (typeof v === 'object' || (isObjectString(v) === true && (v = parseToObject(v)))) {
137
+ resizeMode['type'] = v.type || 'cover'
138
+
139
+ if (typeof v.position === 'number') {
140
+ resizeMode['clipY'] = resizeMode['clipX'] = v.position
141
+ }
142
+
143
+ if (typeof v.position === 'object') {
144
+ resizeMode['clipX'] = 'x' in v.position === true ? v.position.x : null
145
+ resizeMode['clipY'] = 'y' in v.position === true ? v.position.y : null
146
+ }
147
+ this.props['textureOptions'] = { resizeMode }
148
+ }
149
+ },
128
150
  set rtt(v) {
129
151
  this.props['rtt'] = v
130
152
  if (v === true && this.raw['color'] === undefined) {
@@ -183,6 +205,11 @@ const propsTransformer = {
183
205
  }
184
206
  },
185
207
  set effects(v) {
208
+ for (let i = 0; i < v.length; i++) {
209
+ if (v[i].props && v[i].props.color) {
210
+ v[i].props.color = colors.normalize(v[i].props.color)
211
+ }
212
+ }
186
213
  this.props['shader'] = renderer.createShader('DynamicShader', {
187
214
  effects: v,
188
215
  })
@@ -159,6 +159,118 @@ test('Element - Set `texture` property', (assert) => {
159
159
  assert.end()
160
160
  })
161
161
 
162
+ test('Element - Set `fit` property with a string type value', (assert) => {
163
+ assert.capture(renderer, 'createNode', () => new EventEmitter())
164
+ const el = createElement()
165
+
166
+ const fitVal = 'contain'
167
+ el.set('fit', fitVal)
168
+
169
+ assert.ok(el.node['textureOptions'] instanceof Object, 'textureOptions should be an object')
170
+ assert.ok(
171
+ el.node['textureOptions']['resizeMode'] instanceof Object,
172
+ 'resizeMode should be an object'
173
+ )
174
+ assert.equal(
175
+ el.node['textureOptions']['resizeMode']['type'],
176
+ fitVal,
177
+ 'Node resizeMode "type" parameter should be set'
178
+ )
179
+ assert.equal(
180
+ el.props.props['textureOptions']['resizeMode']['type'],
181
+ fitVal,
182
+ 'Props textureOptions parameter should be set'
183
+ )
184
+ assert.equal(el.props.raw.get('fit'), fitVal, "Props' raw map entry should be added")
185
+ assert.end()
186
+ })
187
+
188
+ test('Element - Set `fit` property with an object type value', (assert) => {
189
+ assert.capture(renderer, 'createNode', () => new EventEmitter())
190
+ const el = createElement()
191
+
192
+ const fitVal = "{type: 'cover', position: {x: 0, y:1}}"
193
+ el.set('fit', fitVal)
194
+
195
+ assert.equal(
196
+ el.node['textureOptions']['resizeMode']['type'],
197
+ 'cover',
198
+ 'Node resizeMode "type" parameter should be set'
199
+ )
200
+ assert.equal(
201
+ el.props.props['textureOptions']['resizeMode']['type'],
202
+ 'cover',
203
+ 'Props textureOptions parameter should be set'
204
+ )
205
+ assert.equal(
206
+ el.node['textureOptions']['resizeMode']['clipX'],
207
+ 0,
208
+ 'Node resizeMode "clipX" parameter should be set to correct value'
209
+ )
210
+ assert.equal(
211
+ el.node['textureOptions']['resizeMode']['clipY'],
212
+ 1,
213
+ 'Node resizeMode "clipY" parameter should be set to correct value'
214
+ )
215
+ assert.equal(el.props.raw.get('fit'), fitVal, "Props' raw map entry should be added")
216
+ assert.end()
217
+ })
218
+
219
+ test('Element - Set `fit` property with position key as a string type value', (assert) => {
220
+ assert.capture(renderer, 'createNode', () => new EventEmitter())
221
+ const el = createElement()
222
+
223
+ const fitVal = "{type: 'cover', position: 1}"
224
+ el.set('fit', fitVal)
225
+
226
+ assert.equal(
227
+ el.node['textureOptions']['resizeMode']['type'],
228
+ 'cover',
229
+ 'Node resizeMode "type" parameter should be set'
230
+ )
231
+ assert.equal(
232
+ el.props.props['textureOptions']['resizeMode']['type'],
233
+ 'cover',
234
+ 'Props textureOptions parameter should be set'
235
+ )
236
+ assert.equal(
237
+ el.node['textureOptions']['resizeMode']['clipX'],
238
+ 1,
239
+ 'Node resizeMode "clipX" parameter should be set to correct value'
240
+ )
241
+ assert.equal(
242
+ el.node['textureOptions']['resizeMode']['clipY'],
243
+ 1,
244
+ 'Node resizeMode "clipY" parameter should be set to correct value'
245
+ )
246
+ assert.equal(el.props.raw.get('fit'), fitVal, "Props' raw map entry should be added")
247
+ assert.end()
248
+ })
249
+
250
+ test('Element - Set `fit` property should not set not required keys', (assert) => {
251
+ assert.capture(renderer, 'createNode', () => new EventEmitter())
252
+ const el = createElement()
253
+
254
+ const fitVal = "{type: 'cover', dummy1: 0, dummy2: 0}"
255
+ el.set('fit', fitVal)
256
+
257
+ assert.equal(
258
+ el.node['textureOptions']['resizeMode']['type'],
259
+ 'cover',
260
+ 'Node resizeMode "type" parameter should be set'
261
+ )
262
+ assert.equal(
263
+ el.props.props['textureOptions']['resizeMode']['dummy1'],
264
+ undefined,
265
+ 'Props resizeMode "dummy1" parameter should not be set'
266
+ )
267
+ assert.equal(
268
+ el.props.props['textureOptions']['resizeMode']['dummy2'],
269
+ undefined,
270
+ 'Props resizeMode "dummy2" parameter should not be set'
271
+ )
272
+ assert.end()
273
+ })
162
274
  function createElement() {
163
275
  const el = element({}, {})
164
276
  el.populate({
@@ -16,6 +16,9 @@
16
16
  */
17
17
 
18
18
  import { RendererMain } from '@lightningjs/renderer'
19
+ import { WebGlCoreRenderer, SdfTextRenderer } from '@lightningjs/renderer/webgl'
20
+ import { CanvasCoreRenderer, CanvasTextRenderer } from '@lightningjs/renderer/canvas'
21
+
19
22
  import { Log } from '../../lib/log.js'
20
23
  import { SCREEN_RESOLUTIONS } from '../../constants.js'
21
24
  import colors from '../../lib/colors/colors.js'
@@ -24,17 +27,14 @@ import shaderLoader from './shaderLoader.js'
24
27
 
25
28
  export let renderer = {}
26
29
 
27
- export default (App, target, settings = {}) => {
28
- if ('fontLoader' in settings) {
29
- Log.warn(
30
- `
30
+ const renderEngine = (settings) => {
31
+ const renderMode = 'renderMode' in settings ? settings.renderMode : 'webgl'
31
32
 
32
- Starting version 0.9.0 of Blits, the Launch setting \`fontLoader\` is not supported / required anymore.
33
- You can remove the option from your \`src/index.js\`-file. And you can safely remove the file \`src/fontLoader.js\` from your project.
34
- `
35
- )
36
- }
33
+ if (renderMode === 'webgl') return WebGlCoreRenderer
34
+ if (renderMode === 'canvas') return CanvasCoreRenderer
35
+ }
37
36
 
37
+ export default (App, target, settings = {}) => {
38
38
  renderer = new RendererMain(
39
39
  {
40
40
  appWidth: settings.w || 1920,
@@ -55,7 +55,8 @@ You can remove the option from your \`src/index.js\`-file. And you can safely re
55
55
  // gpu memory limit, converted from mb to bytes - defaults to 200mb
56
56
  txMemByteThreshold:
57
57
  'gpuMemoryLimit' in settings ? settings.gpuMemoryLimit * 1024 * 1024 : 200 * 1024 * 1024,
58
- renderMode: 'renderMode' in settings ? settings.renderMode : 'webgl',
58
+ renderEngine: renderEngine(settings),
59
+ fontEngines: [SdfTextRenderer, CanvasTextRenderer],
59
60
  },
60
61
  target
61
62
  )
@@ -300,6 +300,14 @@ const generateForLoopCode = function (templateObject, parent) {
300
300
  ctx.renderCode.push(`parent = ${parent}`)
301
301
  }
302
302
 
303
+ const indexRegex = new RegExp(`\\$${index}(?!['\\w])`)
304
+ const indexResult = indexRegex.exec(key)
305
+ if (Array.isArray(indexResult)) {
306
+ ctx.renderCode.push(
307
+ `console.warn(" Using '${index}' in the key, like key=${key}, is not recommended")`
308
+ )
309
+ }
310
+
303
311
  const forStartCounter = counter
304
312
 
305
313
  ctx.renderCode.push(`
@@ -310,6 +318,7 @@ const generateForLoopCode = function (templateObject, parent) {
310
318
  let l = rawCollection.length
311
319
  while(l--) {
312
320
  const ${item} = rawCollection[l]
321
+ const ${index} = l
313
322
  keys.add('' + ${interpolate(key, '') || 'l'})
314
323
  }
315
324
  `)
@@ -16,40 +16,66 @@
16
16
  */
17
17
 
18
18
  const eventsMap = new Map()
19
+ const callbackCache = new Map()
19
20
 
20
21
  export default {
21
- registerListener(component, event, cb) {
22
+ registerListener(component, event, cb, priority = 0) {
22
23
  let componentsMap = eventsMap.get(event)
23
- if (!componentsMap) {
24
+ if (componentsMap === undefined) {
24
25
  componentsMap = new Map()
25
26
  eventsMap.set(event, componentsMap)
26
27
  }
27
28
 
28
29
  let components = componentsMap.get(component)
29
- if (!components) {
30
+ if (components === undefined) {
30
31
  components = new Set()
31
32
  componentsMap.set(component, components)
32
33
  }
33
34
 
34
- components.add(cb)
35
+ components.add({ cb, priority })
36
+ callbackCache.delete(event) // Invalidate the callbackCache when a new callback is added
35
37
  },
38
+
36
39
  executeListeners(event, params) {
37
40
  const componentsMap = eventsMap.get(event)
38
- if (componentsMap) {
39
- componentsMap.forEach((component) => {
40
- component.forEach((cb) => {
41
- cb(params)
42
- })
43
- })
41
+ if (componentsMap === undefined || componentsMap.size === 0) {
42
+ return true // No listeners, so execution can be considered successful
43
+ }
44
+
45
+ if (callbackCache.has(event) === false) {
46
+ const allCallbacks = []
47
+ for (const [component, components] of componentsMap) {
48
+ for (const callbackObj of components) {
49
+ allCallbacks.push({ ...callbackObj, component })
50
+ }
51
+ }
52
+ allCallbacks.sort((a, b) => b.priority - a.priority)
53
+ callbackCache.set(event, allCallbacks) // callbackCache the sorted callbacks with component context
44
54
  }
55
+
56
+ const callbacks = callbackCache.get(event)
57
+ for (let i = 0; i < callbacks.length; i++) {
58
+ const { cb, component } = callbacks[i]
59
+ const result = cb.call(component, params)
60
+ if (result === false) {
61
+ return false // Stop propagation if any listener returns false
62
+ }
63
+ }
64
+
65
+ return true // All listeners executed without stopping propagation
45
66
  },
67
+
46
68
  removeListeners(component) {
47
- eventsMap.forEach((componentMap) => {
48
- const cmp = componentMap.get(component)
49
- if (cmp) {
50
- cmp.clear()
51
- componentMap.delete(cmp)
69
+ for (const [event, componentsMap] of eventsMap) {
70
+ if (componentsMap.has(component)) {
71
+ componentsMap.delete(component)
72
+ callbackCache.delete(event) // Invalidate the callbackCache for this event
73
+
74
+ // If no more components for this event, remove the event entirely
75
+ if (componentsMap.size === 0) {
76
+ eventsMap.delete(event)
77
+ }
52
78
  }
53
- })
79
+ }
54
80
  },
55
81
  }