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L+FBm7isz739HZYiuNB67gaIfhpToxJJ1Hb4mjr//KnCQ2VMZDZxbIcnIoj9F7f/YN8Ch9VfwU9jGx20g4MgSbaO5pIK4OeDbPQMrSU+kjpBE66hsVxptcExMWsbkABMW41qkywJZm7hSNSkONRTqeysBMVQoxzE1bUxan/RWlOAgleKwHFAcykLrUo1iX94DtThmZkMBhr/hZTioxWFcEhyqaNXzGEcbzTI3LziGDhb1SbwzihaDKsCBN1SHY2FRgiWTShM+rGJF63HECVlM+n0C/EatSUGEWTImy9rScmcDItWH8umU38pqNxcEpG+AY229bv+M6wPhoE8BsLHZWPhto5Gt+9FspDPcnfdv9H+ZEKCXkNna1nPTXJBF3h3JqAtYxhAJYnyUxDi6YrOz2xYHBcGsLujUui8cCHsOfePY2y/AYZYkrIhR4XEAQIjjyRweR1XIaaMe1cFhPQ7RsBj37I4A7oY2FuRJmz5b9Q/Ie8ZxdOzmryCLAyT5inqXmjEX7sQ4VL5KzdlnKXJy2q53GKEV9w5ZcNnDuJyIZdpAOM7OW+aOlIgvvaRzqF2Po66hP7WoLi5bjiwApReJrETNmHm0+t7RHsfVVf91B4ovS5AWIUDbALq+aVGV9t4YmEa6az++CQCGgQAGht2nd1f+QLwKhG67CETzgmjdEO0HovNCdN+InhfR9yNKGVEpiGpD1PskGtJlPAcJ+yNWAAAAAElFTkSuQmCC"; |
| 3 | +const textureKey = "proggy_clean_inv"; |
| 4 | +const GetObjectDescription = (obj, depth) => { |
| 5 | + const { type } = obj; |
| 6 | + let output; |
| 7 | + let count = null; |
| 8 | + if (type === "Blitter") { |
| 9 | + count = obj.children.list.length; |
| 10 | + } else if (type === "ParticleEmitter") { |
| 11 | + count = obj.getParticleCount(); |
| 12 | + } else if (obj.list) { |
| 13 | + count = obj.list.length; |
| 14 | + } |
| 15 | + const countStr = count === null ? "" : `(${count})`; |
| 16 | + if (type === "DisplayList") { |
| 17 | + output = `${obj.type} ${obj.name} ${countStr}`; |
| 18 | + } else { |
| 19 | + const posStr = `(${obj.x.toFixed(1)}, ${obj.y.toFixed(1)})`; |
| 20 | + const visible = obj.visible ? "+" : "-"; |
| 21 | + const indent = " ".repeat(2 * depth); |
| 22 | + output = `${indent}${visible} ${obj.type} ${obj.name} ${posStr} ${obj.depth} ${countStr}`; |
| 23 | + } |
| 24 | + return output; |
| 25 | +}; |
| 26 | +const WalkDisplayListObj = (obj, output = [], depth = 0) => { |
| 27 | + output.push(GetObjectDescription(obj, depth)); |
| 28 | + if (obj.list) { |
| 29 | + depth += 1; |
| 30 | + for (const child of obj.list) { |
| 31 | + WalkDisplayListObj(child, output, depth); |
| 32 | + } |
| 33 | + } |
| 34 | + return output; |
| 35 | +}; |
| 36 | +class DisplayListWatcher extends Phaser.Plugins.ScenePlugin { |
| 37 | + boot() { |
| 38 | + this.systems.textures.addBase64(textureKey, proggyCleanInv); |
| 39 | + if (this.systems.settings.key === "__SYSTEM") { |
| 40 | + return; |
| 41 | + } |
| 42 | + const events = this.systems.events; |
| 43 | + events.on("start", this.start, this); |
| 44 | + events.on("shutdown", this.stop, this); |
| 45 | + events.on("update", this.update, this); |
| 46 | + events.on("render", this.render, this); |
| 47 | + events.on("destroy", this.destroy, this); |
| 48 | + } |
| 49 | + start() { |
| 50 | + const { keyboard } = this.systems.input; |
| 51 | + const { width, height } = this.systems.scale; |
| 52 | + this.camera = new Phaser.Cameras.Scene2D.Camera( |
| 53 | + 0, |
| 54 | + 0, |
| 55 | + width, |
| 56 | + height |
| 57 | + ).setBounds(0, 0, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY); |
| 58 | + this.controls = new Phaser.Cameras.Controls.FixedKeyControl({ |
| 59 | + camera: this.camera, |
| 60 | + up: keyboard.addKey("UP"), |
| 61 | + down: keyboard.addKey("DOWN"), |
| 62 | + left: keyboard.addKey("LEFT"), |
| 63 | + right: keyboard.addKey("RIGHT"), |
| 64 | + speed: 1 |
| 65 | + }); |
| 66 | + this.shiftKey = keyboard.addKey("SHIFT"); |
| 67 | + this.zKey = keyboard.addKey("Z"); |
| 68 | + this.xKey = keyboard.addKey("X"); |
| 69 | + this.cKey = keyboard.addKey("C"); |
| 70 | + this.vKey = keyboard.addKey("V"); |
| 71 | + this.zKey.on("down", (key, event) => { |
| 72 | + if (event.shiftKey) { |
| 73 | + this.toggle(); |
| 74 | + } |
| 75 | + }); |
| 76 | + this.xKey.on("down", (key, event) => { |
| 77 | + if (event.shiftKey) { |
| 78 | + this.show(); |
| 79 | + } |
| 80 | + }); |
| 81 | + this.cKey.on("down", (key, event) => { |
| 82 | + if (event.shiftKey) { |
| 83 | + this.hide(); |
| 84 | + } |
| 85 | + }); |
| 86 | + this.vKey.on("down", (key, event) => { |
| 87 | + if (event.shiftKey) { |
| 88 | + this.resetCamera(); |
| 89 | + } |
| 90 | + }); |
| 91 | + this.text = this.systems.make.bitmapText({ font: "__FONT" }, false); |
| 92 | + this.renderText = this.systems.renderer.type === Phaser.WEBGL ? this.text.renderWebGL : this.text.renderCanvas; |
| 93 | + } |
| 94 | + stop() { |
| 95 | + if (this.text) { |
| 96 | + this.text.destroy(); |
| 97 | + this.text = null; |
| 98 | + } |
| 99 | + } |
| 100 | + update(time, delta) { |
| 101 | + if (this.shiftKey.isDown) { |
| 102 | + this.controls.update(delta); |
| 103 | + } |
| 104 | + } |
| 105 | + render() { |
| 106 | + if (!this.text.visible) { |
| 107 | + return; |
| 108 | + } |
| 109 | + const maxLines = 100; |
| 110 | + const { displayList, renderer, scenePlugin, settings } = this.systems; |
| 111 | + const x = 256 * scenePlugin.getIndex(this.scene); |
| 112 | + const y = 0; |
| 113 | + const output = WalkDisplayListObj( |
| 114 | + { |
| 115 | + name: settings.key, |
| 116 | + type: "DisplayList", |
| 117 | + list: displayList.list |
| 118 | + }, |
| 119 | + [], |
| 120 | + 0 |
| 121 | + ); |
| 122 | + const fullLength = output.length; |
| 123 | + if (fullLength > maxLines) { |
| 124 | + output.length = maxLines; |
| 125 | + output.push(`[ ... ${fullLength - maxLines} more ]`); |
| 126 | + } |
| 127 | + this.text.setPosition(x, y).setText(output); |
| 128 | + this.camera.preRender(); |
| 129 | + this.renderText(renderer, this.text, this.camera); |
| 130 | + } |
| 131 | + sceneDestroy() { |
| 132 | + this.stop(); |
| 133 | + } |
| 134 | + hide() { |
| 135 | + this.text.visible = false; |
| 136 | + } |
| 137 | + show() { |
| 138 | + this.text.visible = true; |
| 139 | + } |
| 140 | + toggle() { |
| 141 | + this.text.visible = !this.text.visible; |
| 142 | + } |
| 143 | + resetCamera() { |
| 144 | + this.camera.setScroll(0, 0); |
| 145 | + } |
| 146 | +} |
| 147 | +export { |
| 148 | + DisplayListWatcher as default |
| 149 | +}; |
0 commit comments