Skip to content

Commit eec45aa

Browse files
author
Farhad Ghayour
committed
Clean: Allow for turning various events on/off
1 parent 4502c8f commit eec45aa

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

core/Node.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ Node.prototype.removeComponent = function removeComponent (component) {
670670
*
671671
* @param {String} eventName the name of the event
672672
*
673-
* @return {undefined} undefined
673+
* @return {Node} this
674674
*/
675675
Node.prototype.removeUIEvent = function removeUIEvent (eventName) {
676676
var UIEvents = this.getUIEvents();
@@ -685,6 +685,7 @@ Node.prototype.removeUIEvent = function removeUIEvent (eventName) {
685685
if (component && component.onRemoveUIEvent) component.onRemoveUIEvent(eventName);
686686
}
687687
}
688+
return this;
688689
};
689690

690691
/**
@@ -696,7 +697,7 @@ Node.prototype.removeUIEvent = function removeUIEvent (eventName) {
696697
*
697698
* @param {String} eventName the name of the event
698699
*
699-
* @return {undefined} undefined
700+
* @return {Node} this
700701
*/
701702
Node.prototype.addUIEvent = function addUIEvent (eventName) {
702703
var UIEvents = this.getUIEvents();
@@ -711,6 +712,7 @@ Node.prototype.addUIEvent = function addUIEvent (eventName) {
711712
if (component && component.onAddUIEvent) component.onAddUIEvent(eventName);
712713
}
713714
}
715+
return this;
714716
};
715717

716718
/**

renderers/Context.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ Context.prototype.getRootSize = function getRootSize() {
147147
Context.prototype.initWebGL = function initWebGL() {
148148
this.canvas = document.createElement('canvas');
149149
this._rootEl.appendChild(this.canvas);
150-
this.WebGLRenderer = new WebGLRenderer(this.canvas, this._compositor, this.DOMLayerEl);
150+
this.WebGLRenderer = new WebGLRenderer(this.canvas, this._compositor, this._domLayerEl);
151151
this.updateSize();
152152
};
153153

webgl-renderables/Mesh.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ Mesh.prototype.onOpacityChange = function onOpacityChange (opacity) {
594594
*
595595
* @param {String} UIEvent UIEvent to be subscribed to (e.g. `click`)
596596
*
597-
* @return {undefined} undefined
597+
* @return {Mesh} this
598598
*/
599599
Mesh.prototype.onAddUIEvent = function onAddUIEvent(UIEvent) {
600600
if (this._UIEvents.indexOf(UIEvent) === -1) {
@@ -613,7 +613,7 @@ Mesh.prototype.onAddUIEvent = function onAddUIEvent(UIEvent) {
613613
*
614614
* @param {String} UIEvent UIEvent to be removed (e.g. `click`)
615615
*
616-
* @return {undefined} undefined
616+
* @return {Mesh} this
617617
*/
618618
Mesh.prototype.onRemoveUIEvent = function onRemoveUIEvent(UIEvent) {
619619
var index = this._UIEvents.indexOf(UIEvent);

webgl-renderers/WebGLRenderer.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,29 @@ function WebGLRenderer(canvas, compositor, eventDiv) {
141141
/**
142142
* WebGL Picking by caputing events (e.g. 'click') using the
143143
* main famous HTML element for capturing events.
144+
*
145+
* TODO: Famous config file should contain these options for flexibility.
144146
*/
145-
var ev;
146147
this.listeners = {};
147148
this.meshIds = 0;
148-
this.eventsMap = ['click', 'dblclick', 'mousewheel', 'touchstart',
149-
'keyup', 'keydown', 'mousedown', 'mouseup',
150-
'scroll', 'select', 'touchend', 'wheel'];
149+
this.eventsMap = {
150+
click: true,
151+
dblclick: true,
152+
mousewheel: false,
153+
touchstart: true,
154+
keyup: true,
155+
keydown: true,
156+
mousedown: false,
157+
mouseup: false,
158+
scroll: false,
159+
select: false,
160+
touchend: false,
161+
wheel: false
162+
};
151163

152-
var len = this.eventsMap.length;
153-
for(var i = 0; i < len; i++) {
154-
ev = this.eventsMap[i];
164+
// If event is tracked, set the listener
165+
for(var ev in this.eventsMap) {
166+
if (!this.eventsMap[ev]) continue;
155167
this.listeners[ev] = [];
156168
eventDiv.addEventListener(ev, function(e) {
157169
_this.handleEvent(e);
@@ -232,12 +244,10 @@ WebGLRenderer.prototype.checkEvent = function checkEvent(x, y, type) {
232244
* @return {undefined} undefined
233245
*/
234246
WebGLRenderer.prototype.subscribe = function subscribe(path, type) {
235-
if (this.eventsMap.indexOf(type) === -1) {
236-
return false;
247+
if (this.eventsMap[type]) {
248+
var mesh = this.meshRegistry[path];
249+
this.listeners[type][mesh.id] = mesh;
237250
}
238-
var mesh = this.meshRegistry[path];
239-
this.listeners[type][mesh.id] = mesh;
240-
return this;
241251
};
242252

243253
/**
@@ -252,12 +262,10 @@ WebGLRenderer.prototype.subscribe = function subscribe(path, type) {
252262
* @return {undefined} undefined
253263
*/
254264
WebGLRenderer.prototype.unsubscribe = function unsubscribe(path, type) {
255-
if (this.eventsMap.indexOf(type) === -1) {
256-
return false;
265+
if (this.eventsMap[type]) {
266+
var mesh = this.meshRegistry[path];
267+
this.listeners[type].splice(mesh.id, 1);
257268
}
258-
var mesh = this.meshRegistry[path];
259-
this.listeners[type].splice(mesh.id, 1);
260-
return this;
261269
};
262270

263271
/**

0 commit comments

Comments
 (0)