wgge 0.1.10 → 0.1.11

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.
@@ -1,5 +1,9 @@
1
1
  import NodeWithEvents from "./event/NodeWithEvents";
2
2
 
3
+ export const EVENT_REMOVE_ME = 'remove-me';
4
+
5
+ export const EVENT_DIRTY = 'dirty';
6
+
3
7
  export default class ModelBase extends NodeWithEvents {
4
8
 
5
9
  /**
@@ -51,7 +55,7 @@ export default class ModelBase extends NodeWithEvents {
51
55
  makeDirty() {
52
56
  if (!this.isDirty) {
53
57
  this.isDirty = true;
54
- this.triggerEvent('dirty');
58
+ this.triggerEvent(EVENT_DIRTY);
55
59
  }
56
60
  }
57
61
 
@@ -68,11 +72,11 @@ export default class ModelBase extends NodeWithEvents {
68
72
  }
69
73
 
70
74
  addOnDirtyListener(handler) {
71
- this.addEventListener('dirty', handler);
75
+ this.addEventListener(EVENT_DIRTY, handler);
72
76
  }
73
77
 
74
78
  removeOnDirtyListener(handler) {
75
- this.removeEventListener('dirty', handler);
79
+ this.removeEventListener(EVENT_DIRTY, handler);
76
80
  }
77
81
 
78
82
  subscribeToOnDirtyEvent(node) {
@@ -85,6 +89,10 @@ export default class ModelBase extends NodeWithEvents {
85
89
  return node;
86
90
  }
87
91
 
92
+ removeMyself() {
93
+ this.triggerEvent(EVENT_REMOVE_ME, this);
94
+ }
95
+
88
96
  equalsTo(ch) {
89
97
  return this === ch;
90
98
  }
@@ -2,6 +2,7 @@ import NullableNode from "../value/NullableNode";
2
2
  import Collection from "../../Collection";
3
3
  import ArrayHelper from "../../helper/ArrayHelper";
4
4
  import ObjectModel from "../ObjectModel";
5
+ import {EVENT_REMOVE_ME} from "../ModelBase";
5
6
 
6
7
  export default class ModelNodeCollection extends ObjectModel {
7
8
 
@@ -26,8 +27,13 @@ export default class ModelNodeCollection extends ObjectModel {
26
27
  events.forEach((event) => this.children.addEventListener(event, (param) => this.triggerEvent(event, param)));
27
28
  this.children.addOnAddListener((child) => this.onChildAdded(child));
28
29
  this.children.addOnRemoveListener((child) => this.onChildRemoved(child));
29
- this.childDirtyHandler = (ch) => this.onChildDirty(ch);
30
- this.childRequestedRemoveHandler = (ch) => this.remove(ch);
30
+ this.childRequestedRemoveHandler = (ch) => {
31
+ if (!ch) {
32
+ console.warn("You triggered remove-me event without passing reference to the removed element!");
33
+ return;
34
+ }
35
+ this.remove(ch);
36
+ }
31
37
 
32
38
  this.selectedNode = this.addProperty('selectedNode', new NullableNode(null, false));
33
39
 
@@ -183,8 +189,8 @@ export default class ModelNodeCollection extends ObjectModel {
183
189
  */
184
190
  onChildAdded(child) {
185
191
  this.makeDirty();
186
- child.addOnDirtyListener(this.childDirtyHandler);
187
- child.addEventListener('remove-me', this.childRequestedRemoveHandler);
192
+ this.subscribeToOnDirtyEvent(child);
193
+ child.addEventListener(EVENT_REMOVE_ME, this.childRequestedRemoveHandler);
188
194
  }
189
195
 
190
196
  /**
@@ -193,8 +199,8 @@ export default class ModelNodeCollection extends ObjectModel {
193
199
  */
194
200
  onChildRemoved(child) {
195
201
  this.makeDirty();
196
- child.removeOnDirtyListener(this.childDirtyHandler);
197
- child.removeEventListener('remove-me', this.childRequestedRemoveHandler);
202
+ this.unsubscribeFromOnDirtyEvent(child);
203
+ child.removeEventListener(EVENT_REMOVE_ME, this.childRequestedRemoveHandler);
198
204
  if (this.selectedNode.equalsTo(child)) {
199
205
  this.selectedNode.set(null);
200
206
  }
@@ -29,4 +29,9 @@ export default class NodeWithEvents {
29
29
  }
30
30
  }
31
31
 
32
+ static isNodeWithEvents(node) {
33
+ if (!node) return false;
34
+ return (typeof node === 'object' && typeof node.addEventListener === 'function');
35
+ }
36
+
32
37
  }
@@ -1,4 +1,5 @@
1
1
  import DirtyValue from "./DirtyValue";
2
+ import {EVENT_REMOVE_ME} from "../ModelBase";
2
3
 
3
4
  export default class NullableNode extends DirtyValue {
4
5
  nodeFactory;
@@ -12,6 +13,8 @@ export default class NullableNode extends DirtyValue {
12
13
  super(null, persistent);
13
14
 
14
15
  this.nodeFactory = nodeFactory;
16
+
17
+ this.childRequestedRemoveHandler = () => this.set(null);
15
18
  }
16
19
 
17
20
  /**
@@ -21,12 +24,14 @@ export default class NullableNode extends DirtyValue {
21
24
  set(value) {
22
25
  if (this.isSet()) {
23
26
  this.unsubscribeFromOnDirtyEvent(this.value);
27
+ this.value.removeEventListener(EVENT_REMOVE_ME, this.childRequestedRemoveHandler);
24
28
  }
25
29
 
26
30
  super.set(value);
27
31
 
28
32
  if (this.isSet()) {
29
33
  this.subscribeToOnDirtyEvent(this.value);
34
+ this.value.addEventListener(EVENT_REMOVE_ME, this.childRequestedRemoveHandler);
30
35
  }
31
36
  }
32
37
 
@@ -4,6 +4,10 @@ import BoolValue from "../../core/model/value/BoolValue";
4
4
 
5
5
  const CLICK_TIMEOUT = 250;
6
6
 
7
+ export const EVENT_LEFT_CLICK = 'left-click';
8
+
9
+ export const EVENT_RIGHT_CLICK = 'right-click';
10
+
7
11
  export default class ControlsModel extends ObjectModel {
8
12
 
9
13
  /**
@@ -41,7 +45,7 @@ export default class ControlsModel extends ObjectModel {
41
45
  this.leftClickTime = time;
42
46
  } else {
43
47
  if ((time - this.leftClickTime) < CLICK_TIMEOUT) {
44
- this.triggerEvent('left-click', this.mouseCoordinates.clone());
48
+ this.triggerEvent(EVENT_LEFT_CLICK, this.mouseCoordinates.clone());
45
49
  }
46
50
  }
47
51
  }
@@ -52,7 +56,7 @@ export default class ControlsModel extends ObjectModel {
52
56
  this.rightClickTime = time;
53
57
  } else {
54
58
  if ((time - this.rightClickTime) < CLICK_TIMEOUT) {
55
- this.triggerEvent('right-click', this.mouseCoordinates.clone());
59
+ this.triggerEvent(EVENT_RIGHT_CLICK, this.mouseCoordinates.clone());
56
60
  }
57
61
  }
58
62
  }
@@ -167,6 +167,14 @@ export default class AssetCache extends NodeWithEvents {
167
167
  }
168
168
  }
169
169
 
170
+ loadAsset(uri, onLoaded) {
171
+ this.getAsset(uri, onLoaded);
172
+ }
173
+
174
+ loadImage(url, onLoaded) {
175
+ this.getAsset(url, onLoaded);
176
+ }
177
+
170
178
  loadMaterial(materialId, onLoaded) {
171
179
  this.getAsset(`mat/${materialId}`, onLoaded);
172
180
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wgge",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "description": "Web GL Game Engine",
5
5
  "main": "index.js",
6
6
  "dependencies": {