forked from melonjs/melonJS
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebugPanel.js
More file actions
263 lines (218 loc) · 7.3 KB
/
Copy pathdebugPanel.js
File metadata and controls
263 lines (218 loc) · 7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* MelonJS Game Engine
* Copyright (C) 2011 - 2013, Olivier BIOT
* http://www.melonjs.org
*
* a simple debug panel plugin
* usage : me.plugin.register(debugPanel, "debug");
*
* you can then use me.plugin.debug.show() or me.plugin.debug.hide()
* to show or hide the panel, or press respectively the "S" and "H" keys.
*
* note :
* Heap Memory information is available under Chrome when using
* the "--enable-memory-info" parameter to launch Chrome
*/
(function($) {
/**
* @class
* @public
* @extends me.plugin.Base
* @memberOf me
* @constructor
*/
debugPanel = me.plugin.Base.extend(
/** @scope me.debug.Panel.prototype */
{
// Object "Game Unique Identifier"
GUID : null,
// to hold the debug options
// clickable rect area
area : {},
// panel position and size
rect : null,
// for z ordering
// make it ridiculously high
z : Infinity,
// visibility flag
visible : false,
// minimum melonJS version expected
version : "0.9.5",
/** @private */
init : function(showKey, hideKey) {
// call the parent constructor
this.parent();
this.rect = new me.Rect(new me.Vector2d(0, 0), me.video.getWidth(), 35);
// set the object GUID value
this.GUID = "debug-" + me.utils.createGUID();
// set the object entity name
this.name = "me.debugPanel";
// persistent
this.isPersistent = true;
// a floating object
this.floating = true;
// create a default font, with fixed char width
this.font = new me.Font('courier', 10, 'white');
// clickable areas
this.area.renderHitBox = new me.Rect(new me.Vector2d(160,5),15,15);
this.area.renderVelocity = new me.Rect(new me.Vector2d(165,18),15,15);
this.area.renderDirty = new me.Rect(new me.Vector2d(270,5),15,15);
this.area.renderCollisionMap = new me.Rect(new me.Vector2d(270,18),15,15);
// some internal string/length
this.help_str = "(s)how/(h)ide";
this.help_str_len = this.font.measureText(me.video.getSystemContext(), this.help_str).width;
this.fps_str_len = this.font.measureText(me.video.getSystemContext(), "00/00 fps").width;
// enable the FPS counter
me.debug.displayFPS = true;
// bind the "S" and "H" keys
me.input.bindKey(showKey || me.input.KEY.S, "show");
me.input.bindKey(hideKey || me.input.KEY.H, "hide");
// memory heap sample points
this.samples = [];
// make it visible
this.show();
},
/**
* show the debug panel
*/
show : function() {
if (!this.visible) {
// add the panel to the object pool if required
if (!me.game.getEntityByName("me.debugPanel")[0]) {
me.game.add(this, this.z);
me.game.sort();
}
// register a mouse event for the checkboxes
me.input.registerMouseEvent('mousedown', this.rect, this.onClick.bind(this), true);
// make it visible
this.visible = true;
// force repaint
me.game.repaint();
}
},
/**
* hide the debug panel
*/
hide : function() {
if (this.visible) {
// release the mouse event for the checkboxes
me.input.releaseMouseEvent('mousedown', this.rect);
// make it visible
this.visible = false;
// force repaint
me.game.repaint();
}
},
/** @private */
update : function() {
if (me.input.isKeyPressed('show')) {
this.show();
}
else if (me.input.isKeyPressed('hide')) {
this.hide();
}
return true;
},
/**
* @private
*/
getRect : function() {
return this.rect;
},
/** @private */
onClick : function() {
// check the clickable areas
if (this.area.renderHitBox.containsPoint(me.input.mouse.pos)) {
me.debug.renderHitBox = !me.debug.renderHitBox;
} else if (this.area.renderDirty.containsPoint(me.input.mouse.pos)) {
me.debug.renderDirty = !me.debug.renderDirty;
} else if (this.area.renderCollisionMap.containsPoint(me.input.mouse.pos)) {
me.debug.renderCollisionMap = !me.debug.renderCollisionMap;
/*
// not working with dynamic rendering since
// collision layer does not have allocated renderers
var layer = me.game.currentLevel.getLayerByName("collision");
if (layer && me.debug.renderCollisionMap === false) {
layer.visible = true;
me.game.add(layer);
me.debug.renderCollisionMap = true;
me.game.sort();
} else if (layer) {
layer.visible = false;
me.game.remove(layer);
me.debug.renderCollisionMap = false;
}
*/
} else if (this.area.renderVelocity.containsPoint(me.input.mouse.pos)) {
// does nothing for now, since velocity is
// rendered together with hitboxes (is a global debug flag required?)
me.debug.renderVelocity = !me.debug.renderVelocity;
}
// force repaint
me.game.repaint();
},
/** @private */
drawMemoryGraph : function (context, startX, endX) {
if (window.performance && window.performance.memory) {
var usedHeap = Number.prototype.round(window.performance.memory.usedJSHeapSize/1048576, 2);
var totalHeap = Number.prototype.round(window.performance.memory.totalJSHeapSize/1048576, 2);
var len = endX - startX;
// remove the first item
this.samples.shift();
// add a new sample (25 is the height of the graph)
this.samples[len] = (usedHeap / totalHeap) * 25;
// draw the graph
for (var x = len;x--;) {
var where = endX - (len - x);
context.beginPath();
context.strokeStyle = "lightgreen";
context.moveTo(where, 30);
context.lineTo(where, 30 - (this.samples[x] || 0));
context.stroke();
}
// display the current value
this.font.draw(context, usedHeap + '/' + totalHeap + ' MB', startX, 18);
} else {
// Heap Memory information not available
this.font.draw(context, "??/?? MB", startX, 18);
}
},
/** @private */
draw : function(context) {
context.save();
// draw the panel
context.globalAlpha = 0.5;
context.fillStyle = "black";
context.fillRect(this.rect.left, this.rect.top,
this.rect.width, this.rect.height);
context.globalAlpha = 1.0;
// # entities / draw
this.font.draw(context, "#objects : " + me.game.getObjectCount(), 5, 5);
this.font.draw(context, "#draws : " + me.game.getDrawCount(), 5, 18);
// debug checkboxes
this.font.draw(context, "?hitbox ["+ (me.debug.renderHitBox?"x":" ") +"]", 100, 5);
this.font.draw(context, "?velocity ["+ (me.debug.renderVelocity?"x":" ") +"]", 100, 18);
this.font.draw(context, "?dirtyRect ["+ (me.debug.renderDirty?"x":" ") +"]", 200, 5);
this.font.draw(context, "?col. layer ["+ (me.debug.renderCollisionMap?"x":" ") +"]", 200, 18);
// draw the memory heap usage
this.drawMemoryGraph(context, 300, this.rect.width - this.help_str_len - 5);
// some help string
this.font.draw(context, this.help_str, this.rect.width - this.help_str_len - 5, 18);
//fps counter
var fps_str = "" + me.timer.fps + "/" + me.sys.fps + " fps";
this.font.draw(context, fps_str, this.rect.width - this.fps_str_len - 5, 5);
context.restore();
},
/** @private */
onDestroyEvent : function() {
// hide the panel
this.hide();
// unbind "S" & "H"
me.input.unbindKey(me.input.KEY.S);
me.input.unbindKey(me.input.KEY.H);
}
});
/*---------------------------------------------------------*/
// END END END
/*---------------------------------------------------------*/
})(window);