Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean destroy #327

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,12 @@ class Component {

this.scene._removeComponent(this);

/**
* Fired when this Component is destroyed.
* @event destroyed
*/
this.fire("destroyed", this.destroyed = true);

// Memory leak avoidance
this._attached = {};
this._attachments = null;
Expand All @@ -1066,12 +1072,6 @@ class Component {
this._eventCallDepth = 0;
this._adoptees = null;
this._updateScheduled = false;

/**
* Fired when this Component is destroyed.
* @event destroyed
*/
this.fire("destroyed", this.destroyed = true);
}
}

Expand Down
67 changes: 6 additions & 61 deletions src/controls/cameraControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,11 +588,6 @@ class CameraControl extends Component {
const mousePos = math.vec2();
let panToMouse = false;

let ctrlDown = false;
let altDown = false;
let shiftDown = false;
const keyDown = {};

const EPSILON = 0.001;

const getEyeLookDist = (function () {
Expand Down Expand Up @@ -852,36 +847,6 @@ class CameraControl extends Component {
}
}

document.addEventListener("keyDown", function (e) {
if (!self._active) {
return;
}
if (e.target.tagName !== "INPUT" && e.target.tagName !== "TEXTAREA") {
ctrlDown = e.ctrlKey || e.keyCode === 17 || e.metaKey; // !important, treat Windows or Mac Command Key as ctrl
altDown = e.altKey || e.keyCode === 18;
shiftDown = e.keyCode === 16;
keyDown[e.keyCode] = true;
}
}, true);

document.addEventListener("keyup", function (e) {
if (!self._active) {
return;
}
if (e.target.tagName !== "INPUT" && e.target.tagName !== "TEXTAREA") {
if (e.ctrlKey || e.keyCode === 17) {
ctrlDown = false;
}
if (e.altKey || e.keyCode === 18) {
altDown = false;
}
if (e.keyCode === 16) {
shiftDown = false;
}
keyDown[e.keyCode] = false;
}
});

// Mouse camera rotate, pan and zoom

(function () {
Expand Down Expand Up @@ -929,29 +894,7 @@ class CameraControl extends Component {
}
});

canvas.addEventListener("mouseup", function (e) {
if (!self._active) {
return;
}
switch (e.which) {
case 1: // Left button
mouseDownLeft = false;
break;
case 2: // Middle/both buttons
mouseDownMiddle = false;
break;
case 3: // Right button
mouseDownRight = false;
break;
default:
break;
}
down = false;
xDelta = 0;
yDelta = 0;
});

document.addEventListener("mouseup", function (e) {
document.addEventListener("mouseup", self._mouseUpListener=function (e) {
if (!self._active) {
return;
}
Expand Down Expand Up @@ -1019,7 +962,7 @@ class CameraControl extends Component {
return;
}

const panning = shiftDown || mouseDownRight;
const panning = input.keyDown[input.KEY_SHIFT] || mouseDownRight;

if (panning) {

Expand Down Expand Up @@ -1068,7 +1011,7 @@ class CameraControl extends Component {
return;
}
const elapsed = e.deltaTime;
if (!self.ctrlDown && !self.altDown) {
if (!input.ctrlDown && !input.altDown) {
const wkey = input.keyDown[input.KEY_ADD];
const skey = input.keyDown[input.KEY_SUBTRACT];
if (wkey || skey) {
Expand Down Expand Up @@ -1662,7 +1605,7 @@ class CameraControl extends Component {
up: new Float32Array(3)
};

document.addEventListener("keydown", function (e) {
document.addEventListener("keydown", self._cameraAxisKeyDownListener=function (e) {

if (!self._active) {
return;
Expand Down Expand Up @@ -1801,6 +1744,8 @@ class CameraControl extends Component {

destroy() {
this.active = false;
document.removeEventListener("keydown",this._cameraAxisKeyDownListener);
document.removeEventListener("mouseup", this._mouseUpListener);
super.destroy();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const core = {
scene.clear();
} else {
scene.destroy();
delete core.scenes[scene.id];
// delete core.scenes[scene.id];
}
}
}
Expand Down
61 changes: 26 additions & 35 deletions src/input/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -1760,23 +1760,19 @@ class Input extends Component {

if (e.target.tagName !== "INPUT" && e.target.tagName !== "TEXTAREA") {

if (e.ctrlKey) {
self.ctrlDown = true;
self.ctrlDown = e.ctrlKey || e.metaKey;// !important, treat Windows or Mac Command Key as ctrl
self.altDown = e.altKey;

} else if (e.altKey) {
self.altDown = true;

} else {
if (e.keyCode !== self.KEY_CTRL && e.keyCode !== self.KEY_ALT)
self.keyDown[e.keyCode] = true;

/**
* Fired whenever a key is pressed while the parent
* {{#crossLink "Scene"}}Scene{{/crossLink}}'s {{#crossLink "Canvas"}}Canvas{{/crossLink}} has input focus.
* @event keydown
* @param value {Number} The key code, for example {{#crossLink "Input/KEY_LEFT_ARROW:property"}}{{/crossLink}},
*/
self.fire("keydown", e.keyCode, true);
}
/**
* Fired whenever a key is pressed while the parent
* {{#crossLink "Scene"}}Scene{{/crossLink}}'s {{#crossLink "Canvas"}}Canvas{{/crossLink}} has input focus.
* @event keydown
* @param value {Number} The key code, for example {{#crossLink "Input/KEY_LEFT_ARROW:property"}}{{/crossLink}},
*/
self.fire("keydown", e.keyCode, true);
}

if (self.mouseover) {
Expand All @@ -1793,23 +1789,19 @@ class Input extends Component {

if (e.target.tagName !== "INPUT" && e.target.tagName !== "TEXTAREA") {

if (e.ctrlKey) {
self.ctrlDown = false;

} else if (e.altKey) {
self.altDown = false;
self.ctrlDown = e.ctrlKey || e.metaKey;
self.altDown = e.altKey;

} else {
if (e.keyCode !== self.KEY_CTRL && e.keyCode !== self.KEY_ALT)
self.keyDown[e.keyCode] = false;

/**
* Fired whenever a key is released while the parent
* {{#crossLink "Scene"}}Scene{{/crossLink}}'s {{#crossLink "Canvas"}}Canvas{{/crossLink}} has input focus.
* @event keyup
* @param value {Number} The key code, for example {{#crossLink "Input/KEY_LEFT_ARROW:property"}}{{/crossLink}},
*/
self.fire("keyup", e.keyCode, true);
}
/**
* Fired whenever a key is released while the parent
* {{#crossLink "Scene"}}Scene{{/crossLink}}'s {{#crossLink "Canvas"}}Canvas{{/crossLink}} has input focus.
* @event keyup
* @param value {Number} The key code, for example {{#crossLink "Input/KEY_LEFT_ARROW:property"}}{{/crossLink}},
*/
self.fire("keyup", e.keyCode, true);
}
});

Expand Down Expand Up @@ -1851,7 +1843,6 @@ class Input extends Component {
self.fire("mouseleave", coords, true);
});


cfg.element.addEventListener("mousedown", this._mouseDownListener = function (e) {

if (!self.enabled) {
Expand Down Expand Up @@ -2210,26 +2201,26 @@ class Input extends Component {
}

destroy() {
super.destroy();
// Prevent memory leak when destroying canvas/WebGL context
document.removeEventListener("keydown", this._keyDownListener);
document.removeEventListener("keydown", this._keyDownListener,true);
document.removeEventListener("keyup", this._keyUpListener);
this._element.removeEventListener("mouseenter", this._mouseEnterListener);
this._element.removeEventListener("mouseleave", this._mouseLeaveListener);
this._element.removeEventListener("mousedown", this._mouseDownListener);
document.removeEventListener("mouseup", this._mouseDownListener);
document.removeEventListener("mouseup", this._mouseUpListener,true);
document.removeEventListener("dblclick", this._dblClickListener);
this._element.removeEventListener("mousemove", this._mouseMoveListener);
this._element.removeEventListener("wheel", this._mouseWheelListener);
if (window.OrientationChangeEvent) {
window.removeEventListener('orientationchange', this._orientationchangedListener);
window.removeEventListener('orientationchange', this._orientationchangedListener,false);
}
if (window.DeviceMotionEvent) {
window.removeEventListener('devicemotion', this._deviceMotionListener);
window.removeEventListener('devicemotion', this._deviceMotionListener,false);
}
if (window.DeviceOrientationEvent) {
window.addEventListener("deviceorientation", this._deviceOrientListener);
window.removeEventListener("deviceorientation", this._deviceOrientListener,false);
}
super.destroy();
}
}

Expand Down