Skip to content

Commit c42f990

Browse files
author
Martin Hunt
committed
Merge pull request #229 from gameclosure/refactor-simulator
Refactor simulator
2 parents 175896c + 1a1ce26 commit c42f990

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+7883
-3232
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* globals CONFIG, GC, logger */
2+
3+
var dispatchWindowEvent = function(eventName) {
4+
var e = document.createEvent('Event');
5+
e.initEvent(eventName, true, true);
6+
window.dispatchEvent(e);
7+
};
8+
9+
exports.onLaunch = function () {
10+
var deviceId = CONFIG.simulator.deviceId;
11+
var deviceType = CONFIG.simulator.deviceType;
12+
var deviceInfo = CONFIG.simulator.deviceInfo;
13+
14+
import .simulateDevice;
15+
simulateDevice.simulate(deviceInfo);
16+
17+
import devkit.debugging;
18+
var channel = devkit.debugging.getChannel('devkit-simulator');
19+
20+
var _isHomeScreen = false;
21+
channel.on('button:home', function () {
22+
var app = GC.app;
23+
24+
_isHomeScreen = !_isHomeScreen;
25+
if (_isHomeScreen) {
26+
dispatchWindowEvent('pagehide');
27+
app.engine.pause();
28+
29+
var canvas = document.getElementsByTagName('canvas');
30+
if (canvas.length) {
31+
canvas = canvas[0];
32+
if (canvas.getContext) {
33+
var ctx = canvas.getContext('2d');
34+
ctx.fillStyle = '#000000';
35+
ctx.fillRect(0, 0, canvas.width, canvas.height);
36+
}
37+
}
38+
} else {
39+
dispatchWindowEvent('pageshow');
40+
app.engine.resume();
41+
}
42+
});
43+
44+
channel.on('button:back', function (evt) {
45+
GLOBAL.NATIVE.onBackButton && GLOBAL.NATIVE.onBackButton(evt);
46+
});
47+
48+
channel.on('screenshot', function (req) {
49+
devkit.debugging.screenshot(function (err, res) {
50+
if (err) {
51+
req.error(err);
52+
} else {
53+
req.send(res);
54+
}
55+
});
56+
});
57+
58+
channel.on('mute', function (evt) {
59+
console.log('MUTE', evt.shouldMute);
60+
import AudioManager;
61+
AudioManager.muteAll(evt.shouldMute);
62+
});
63+
64+
channel.on('pause', function () { exports.setPaused(true); });
65+
channel.on('resume', function () { exports.setPaused(false); });
66+
channel.on('step', function () { GC.app.engine.stepFrame(); });
67+
68+
logger.log('waiting for devkit simulator client...');
69+
70+
import device;
71+
72+
return channel.connect()
73+
.timeout(1000)
74+
.then(function () {
75+
logger.log('simulator client connected!');
76+
channel.emit('handshake', {
77+
deviceId: deviceId,
78+
deviceType: deviceType,
79+
userAgent: navigator.userAgent,
80+
screen: {
81+
width: device.screen.width,
82+
height: device.screen.height
83+
}
84+
});
85+
});
86+
};
87+
88+
var _isPaused = false;
89+
exports.setPaused = function (isPaused) {
90+
_isPaused = isPaused;
91+
92+
var app = GC.app;
93+
if (_isPaused) {
94+
console.log("app paused");
95+
app.engine.pause();
96+
} else {
97+
app.engine.resume();
98+
}
99+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "devkit-simulator-client",
3+
"version": "0.0.1",
4+
"devkit": {
5+
"extensions": {
6+
"devkit-simulator": "client"
7+
}
8+
}
9+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @license
3+
* This file is part of the Game Closure SDK.
4+
*
5+
* The Game Closure SDK is free software: you can redistribute it and/or modify
6+
* it under the terms of the Mozilla Public License v. 2.0 as published by Mozilla.
7+
8+
* The Game Closure SDK is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* Mozilla Public License v. 2.0 for more details.
12+
13+
* You should have received a copy of the Mozilla Public License v. 2.0
14+
* along with the Game Closure SDK. If not, see <http://mozilla.org/MPL/2.0/>.
15+
*/
16+
17+
exports.simulate = function (params) {
18+
if (params.userAgent) {
19+
var navigator = window.navigator;
20+
var shim = window.navigator = {};
21+
for (var i in navigator) {
22+
shim[i] = navigator[i];
23+
}
24+
25+
shim.userAgent = params.userAgent;
26+
}
27+
28+
window.devicePixelRatio = params.devicePixelRatio || 1;
29+
30+
import device;
31+
32+
var deviceName = params.name.toLowerCase();
33+
34+
var isNative = params.target == "native-android" || params.target == "native-ios";
35+
var isMobileBrowser = params.target == "browser-desktop" || params.target == "browser-mobile";
36+
37+
if (isMobileBrowser) {
38+
device.isMobileBrowser = true;
39+
// device.setUseDOM(true);
40+
}
41+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
devkit view inspector
2+
=====================
3+
4+
development
5+
-----------
6+
7+
- run `npm install` to install the dev dependencies
8+
- run `gulp` to start watching files
9+
10+
deployment
11+
----------
12+
13+
After editing files, run `gulp dist` to generate production files. Commit the
14+
result.
15+
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/**
2+
* @license
3+
* This file is part of the Game Closure SDK.
4+
*
5+
* The Game Closure SDK is free software: you can redistribute it and/or modify
6+
* it under the terms of the Mozilla Public License v. 2.0 as published by Mozilla.
7+
8+
* The Game Closure SDK is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* Mozilla Public License v. 2.0 for more details.
12+
13+
* You should have received a copy of the Mozilla Public License v. 2.0
14+
* along with the Game Closure SDK. If not, see <http://mozilla.org/MPL/2.0/>.
15+
*/
16+
17+
import device;
18+
import devkit.debugging;
19+
20+
module.exports = Class(function () {
21+
22+
// store the views we need to render
23+
this._highlightViewUID = null;
24+
this.setHighlight = function (uid) { this._highlightViewUID = uid; };
25+
26+
this._selectedViewUID = null;
27+
this.setSelected = function (uid) { this._selectedViewUID = uid; };
28+
29+
var tick = 0;
30+
var maxColor = 255;
31+
var minColor = 100;
32+
var _ctx;
33+
34+
// render the highlighted view
35+
var _now = Date.now()
36+
function renderHighlight (pos, ctx) {
37+
if (!ctx) { return; }
38+
ctx.save();
39+
ctx.translate(pos.x, pos.y);
40+
ctx.rotate(pos.r);
41+
42+
// pulsate the blue
43+
tick += -(_now - (_now = Date.now()));
44+
45+
var weight = (Math.sin(2 * Math.PI * tick / 1000) + 1) / 2;
46+
var val = (weight * (maxColor - minColor)) + minColor | 0;
47+
48+
49+
var color = '0,' + (val * 0.7 | 0) + ',' + (val | 0);
50+
51+
ctx.strokeStyle = 'rgba(' + color + ', 0.7)';
52+
ctx.strokeRect(0, 0, pos.width, pos.height);
53+
ctx.fillStyle = 'rgba(' + color + ', 0.6)';
54+
ctx.fillRect(0, 0, pos.width, pos.height);
55+
56+
//draw the cross hair
57+
//ctx.fillStyle = 'rgb(' + val + ',' + val + ',' + val +')';
58+
var opacity = weight * (1 - 0.4) + 0.4;
59+
ctx.fillStyle = 'rgba(255,255,255,' + (opacity.toFixed(2)) +')';
60+
ctx.translate(pos.anchorX, pos.anchorY);
61+
ctx.rotate(tick / 500);
62+
63+
ctx.fillRect(-0.5, -7, 1, 14);
64+
ctx.fillRect(-7, -0.5, 14, 1);
65+
66+
ctx.restore();
67+
}
68+
69+
function renderSelected (pos, ctx) {
70+
if (!ctx) { return; }
71+
72+
ctx.save();
73+
ctx.translate(pos.x, pos.y);
74+
ctx.rotate(pos.r);
75+
ctx.strokeStyle = "red";
76+
ctx.lineWidth = 1;
77+
ctx.strokeRect(-0.5, -0.5, pos.width + 1, pos.height + 1);
78+
ctx.restore();
79+
}
80+
81+
if (!device.isMobile) {
82+
var element = document.createElement('x');
83+
var documentElement = document.documentElement;
84+
var getComputedStyle = window.getComputedStyle;
85+
var supportsPointerEvents = false;
86+
if (element && ('pointerEvents' in element.style)) {
87+
element.style.pointerEvents = 'auto';
88+
element.style.pointerEvents = 'x';
89+
documentElement.appendChild(element);
90+
supportsPointerEvents = getComputedStyle && getComputedStyle(element, '').pointerEvents === 'auto';
91+
documentElement.removeChild(element);
92+
}
93+
94+
if (device.isSimulator && document.body && document.body.appendChild && supportsPointerEvents) {
95+
var canvas = new (device.get('Canvas'))();
96+
canvas.style.cssText = 'position: absolute; left: 0; top: 0; z-index: 1000; pointer-events: none;';
97+
document.body.appendChild(canvas);
98+
99+
_ctx = canvas.getContext('2d');
100+
}
101+
}
102+
103+
// used to drive renderer separately when app is paused
104+
this.startTick = function () {
105+
this.stopTick();
106+
this._tick = setInterval(bind(this, 'render', _ctx), 1000 / 30);
107+
};
108+
109+
// used to stop renderer's timer when app is unpaused
110+
this.stopTick = function () {
111+
if (this._tick) {
112+
clearInterval(this._tick);
113+
}
114+
};
115+
116+
this.setEnabled = function (isEnabled) {
117+
this._isEnabled = isEnabled;
118+
if (_ctx) {
119+
_ctx.clear();
120+
}
121+
};
122+
123+
this.render = function (ctx) {
124+
if (!ctx) { return; }
125+
// if (!this._isEnabled) { return; }
126+
127+
// on simulated devices, we have our own canvas
128+
// so size it to fit the screen (clearing it too)
129+
if (_ctx) {
130+
ctx = _ctx;
131+
ctx.canvas.width = device.screen.width;
132+
ctx.canvas.height = device.screen.height;
133+
}
134+
135+
// render highlighted views
136+
var view, pos;
137+
if (this._highlightViewUID !== null) {
138+
view = devkit.debugging.getViewById(this._highlightViewUID);
139+
pos = view && view.getPosition();
140+
if (pos) {
141+
renderHighlight(pos, ctx);
142+
}
143+
}
144+
145+
// render selected views
146+
if (this._selectedViewUID !== null) {
147+
view = devkit.debugging.getViewById(this._selectedViewUID);
148+
pos = view && view.getPosition();
149+
if (pos) {
150+
renderSelected(pos, ctx);
151+
}
152+
}
153+
};
154+
});

0 commit comments

Comments
 (0)