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z739HZYiuNB67gaIfhpToxJJ1Hb4mjr//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 ) => {
2
+ const chars = `ABCDEFGHIJKLMNOPQRSTUVWXYZ 12345abcdefghijklmnopqrstuvwxyz 67890{}[]()<>$*-+=/#_%^@\\&|~?'" !,.;:` ;
3
+ const charsPerRow = 32 ;
4
+ const height = 13 ;
5
+ const image = "proggy_clean_inv" ;
6
+ const offset = { "x" : 5 , "y" : 24 } ;
7
+ const spacing = { "x" : 0 , "y" : 0 } ;
8
+ const width = 7 ;
9
+ const fontData = {
10
+ chars,
11
+ charsPerRow,
12
+ height,
13
+ image,
14
+ offset,
15
+ spacing,
16
+ width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IfhpToxJJ1Hb4mjr//KnCQ2VMZDZxbIcnIoj9F7f/YN8Ch9VfwU9jGx20g4MgSbaO5pIK4OeDbPQMrSU+kjpBE66hsVxptcExMWsbkABMW41qkywJZm7hSNSkONRTqeysBMVQoxzE1bUxan/RWlOAgleKwHFAcykLrUo1iX94DtThmZkMBhr/hZTioxWFcEhyqaNXzGEcbzTI3LziGDhb1SbwzihaDKsCBN1SHY2FRgiWTShM+rGJF63HECVlM+n0C/EatSUGEWTImy9rScmcDItWH8umU38pqNxcEpG+AY229bv+M6wPhoE8BsLHZWPhto5Gt+9FspDPcnfdv9H+ZEKCXkNna1nPTXJBF3h3JqAtYxhAJYnyUxDi6YrOz2xYHBcGsLujUui8cCHsOfePY2y/AYZYkrIhR4XEAQIjjyRweR1XIaaMe1cFhPQ7RsBj37I4A7oY2FuRJmz5b9Q/Ie8ZxdOzmryCLAyT5inqXmjEX7sQ4VL5KzdlnKXJy2q53GKEV9w5ZcNnDuJyIZdpAOM7OW+aOlIgvvaRzqF2Po66hP7WoLi5bjiwApReJrETNmHm0+t7RHsfVVf91B4ovS5AWIUDbALq+aVGV9t4YmEa6az++CQCGgQAGht2nd1f+QLwKhG67CETzgmjdEO0HovNCdN+InhfR9yNKGVEpiGpD1PskGtJlPAcJ+yNWAAAAAElFTkSuQmCC" ;
19
+ const twoSpaces = / { 2 , } / g;
20
+ const GetObjectDescription = ( obj , precision = 1 ) => {
5
21
const { type } = obj ;
6
22
let output ;
7
23
let count = null ;
@@ -16,45 +32,108 @@ const GetObjectDescription = (obj, depth) => {
16
32
if ( type === "DisplayList" ) {
17
33
output = `${ obj . type } ${ obj . name } ${ countStr } ` ;
18
34
} else {
19
- const posStr = `(${ obj . x . toFixed ( 1 ) } , ${ obj . y . toFixed ( 1 ) } )` ;
20
35
const visible = obj . visible ? "+" : "-" ;
21
- const indent = " " . repeat ( 2 * depth ) ;
22
- output = `${ indent } ${ visible } ${ obj . type } ${ obj . name } ${ posStr } ${ obj . depth } ${ countStr } ` ;
36
+ const pos = typeof obj . x === "number" ? `( ${ obj . x . toFixed ( precision ) } , ${ obj . y . toFixed ( precision ) } )` : "" ;
37
+ output = `${ visible } ${ obj . type } ${ obj . name } ${ pos } ${ obj . depth . toFixed ( precision ) } ${ countStr } ` ;
23
38
}
39
+ output = output . replace ( twoSpaces , " " ) . trimEnd ( ) ;
24
40
return output ;
25
41
} ;
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 ) ;
42
+ const indentMap = { } ;
43
+ for ( let i = 0 ; i <= 11 ; i ++ ) {
44
+ indentMap [ i ] = " " . repeat ( 2 * i ) ;
45
+ }
46
+ const WalkDisplayListObj = ( obj , output = [ ] , currentDepth = 0 , maxDepth = 10 , maxLength = 1e3 ) => {
47
+ output . push ( ( indentMap [ currentDepth ] ?? "! " ) + GetObjectDescription ( obj ) ) ;
48
+ const { list } = obj ;
49
+ if ( list ) {
50
+ currentDepth += 1 ;
51
+ const listLength = list . length ;
52
+ const currentIndent = indentMap [ currentDepth ] ?? "! " ;
53
+ if ( currentDepth > maxDepth ) {
54
+ if ( listLength > 0 ) {
55
+ output . push ( `${ currentIndent } [ ${ listLength } more ... ]` ) ;
56
+ }
57
+ return output ;
58
+ }
59
+ let remainingChildren = listLength ;
60
+ for ( const child of list ) {
61
+ WalkDisplayListObj ( child , output , currentDepth , maxDepth , maxLength ) ;
62
+ remainingChildren -= 1 ;
63
+ if ( output . length >= maxLength ) {
64
+ if ( remainingChildren > 0 ) {
65
+ output . push ( `${ currentIndent } [ ${ remainingChildren } more ... ]` ) ;
66
+ }
67
+ return output ;
68
+ }
32
69
}
33
70
}
34
71
return output ;
35
72
} ;
73
+ const { POSITIVE_INFINITY } = Number ;
74
+ const TextureEvents = Phaser . Textures . Events ;
75
+ const CacheEvents = Phaser . Cache . Events ;
76
+ const SceneEvents = Phaser . Scenes . Events ;
77
+ const ParseRetroFont = Phaser . GameObjects . RetroFont . Parse ;
78
+ const fontTextureKey = fontData . image ;
79
+ const fontKey = fontData . image ;
80
+ let hasPendingFontImage = false ;
36
81
class DisplayListWatcher extends Phaser . Plugins . ScenePlugin {
82
+ constructor ( scene , pluginManager ) {
83
+ super ( scene , pluginManager ) ;
84
+ this . camera = null ;
85
+ this . controls = null ;
86
+ this . hideKey = null ;
87
+ this . modKey = null ;
88
+ this . resetKey = null ;
89
+ this . showKey = null ;
90
+ this . text = null ;
91
+ this . toggleKey = null ;
92
+ }
37
93
boot ( ) {
38
- this . systems . textures . addBase64 ( textureKey , proggyCleanInv ) ;
94
+ const { textures } = this . systems ;
95
+ if ( ! hasPendingFontImage && ! textures . exists ( fontTextureKey ) ) {
96
+ hasPendingFontImage = true ;
97
+ textures . addBase64 ( fontKey , fontImage ) ;
98
+ }
99
+ textures . once ( `${ TextureEvents . ADD_KEY } ${ fontTextureKey } ` , ( ) => {
100
+ this . systems . cache . bitmapFont . add (
101
+ fontKey ,
102
+ ParseRetroFont ( this . scene , fontData )
103
+ ) ;
104
+ } ) ;
39
105
if ( this . systems . settings . key === "__SYSTEM" ) {
40
106
return ;
41
107
}
42
108
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 ) ;
109
+ events . on ( SceneEvents . START , this . start , this ) ;
110
+ events . on ( SceneEvents . SHUTDOWN , this . stop , this ) ;
111
+ events . on ( SceneEvents . DESTROY , this . destroy , this ) ;
112
+ }
113
+ startIfFontWasAdded ( cache , key ) {
114
+ if ( key !== fontKey ) {
115
+ return ;
116
+ }
117
+ cache . events . off ( CacheEvents . ADD , this . startIfFontWasAdded , this ) ;
118
+ this . start ( ) ;
48
119
}
49
120
start ( ) {
121
+ const { cache, events, make, renderer } = this . systems ;
122
+ const fontCache = cache . bitmapFont ;
50
123
const { keyboard } = this . systems . input ;
51
- const { width, height } = this . systems . scale ;
124
+ const { width : width2 , height : height2 } = this . systems . scale ;
125
+ if ( ! fontCache . exists ( fontKey ) ) {
126
+ fontCache . events . on ( CacheEvents . ADD , this . startIfFontWasAdded , this ) ;
127
+ return ;
128
+ }
129
+ events . on ( SceneEvents . UPDATE , this . update , this ) ;
130
+ events . on ( SceneEvents . RENDER , this . render , this ) ;
52
131
this . camera = new Phaser . Cameras . Scene2D . Camera (
53
132
0 ,
54
133
0 ,
55
- width ,
56
- height
57
- ) . setBounds ( 0 , 0 , Number . POSITIVE_INFINITY , Number . POSITIVE_INFINITY ) ;
134
+ width2 ,
135
+ height2
136
+ ) . setBounds ( 0 , 0 , POSITIVE_INFINITY , POSITIVE_INFINITY ) ;
58
137
this . controls = new Phaser . Cameras . Controls . FixedKeyControl ( {
59
138
camera : this . camera ,
60
139
up : keyboard . addKey ( "UP" ) ,
@@ -63,42 +142,68 @@ class DisplayListWatcher extends Phaser.Plugins.ScenePlugin {
63
142
right : keyboard . addKey ( "RIGHT" ) ,
64
143
speed : 1
65
144
} ) ;
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 ) => {
145
+ this . modKey = keyboard . addKey ( "SHIFT" ) ;
146
+ this . toggleKey = keyboard . addKey ( "Z" ) ;
147
+ this . showKey = keyboard . addKey ( "X" ) ;
148
+ this . hideKey = keyboard . addKey ( "C" ) ;
149
+ this . resetKey = keyboard . addKey ( "V" ) ;
150
+ this . toggleKey . on ( "down" , ( key , event ) => {
72
151
if ( event . shiftKey ) {
73
152
this . toggle ( ) ;
74
153
}
75
154
} ) ;
76
- this . xKey . on ( "down" , ( key , event ) => {
155
+ this . showKey . on ( "down" , ( key , event ) => {
77
156
if ( event . shiftKey ) {
78
157
this . show ( ) ;
79
158
}
80
159
} ) ;
81
- this . cKey . on ( "down" , ( key , event ) => {
160
+ this . hideKey . on ( "down" , ( key , event ) => {
82
161
if ( event . shiftKey ) {
83
162
this . hide ( ) ;
84
163
}
85
164
} ) ;
86
- this . vKey . on ( "down" , ( key , event ) => {
165
+ this . resetKey . on ( "down" , ( key , event ) => {
87
166
if ( event . shiftKey ) {
88
167
this . resetCamera ( ) ;
89
168
}
90
169
} ) ;
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 ;
170
+ this . text = make . bitmapText ( { font : fontKey } , false ) ;
171
+ this . renderText = renderer . type === Phaser . WEBGL ? this . text . renderWebGL : this . text . renderCanvas ;
93
172
}
94
173
stop ( ) {
174
+ const { cache, events, input, settings } = this . systems ;
175
+ const { keyboard } = input ;
176
+ cache . bitmapFont . events . off ( CacheEvents . ADD , this . startIfFontWasAdded , this ) ;
177
+ events . off ( SceneEvents . UPDATE , this . update , this ) ;
178
+ events . off ( SceneEvents . RENDER , this . render , this ) ;
179
+ keyboard . removeKey ( this . hideKey , true ) ;
180
+ keyboard . removeKey ( this . modKey , true ) ;
181
+ keyboard . removeKey ( this . resetKey , true ) ;
182
+ keyboard . removeKey ( this . showKey , true ) ;
183
+ keyboard . removeKey ( this . toggleKey , true ) ;
184
+ if ( this . camera ) {
185
+ this . camera . destroy ( ) ;
186
+ }
187
+ if ( this . controls ) {
188
+ const { left, right, up, down } = this . controls ;
189
+ keyboard . removeKey ( left ) ;
190
+ keyboard . removeKey ( right ) ;
191
+ keyboard . removeKey ( up ) ;
192
+ keyboard . removeKey ( down ) ;
193
+ }
95
194
if ( this . text ) {
96
195
this . text . destroy ( ) ;
97
- this . text = null ;
98
196
}
197
+ this . camera = null ;
198
+ this . controls = null ;
199
+ this . hideKey = null ;
200
+ this . modKey = null ;
201
+ this . resetKey = null ;
202
+ this . showKey = null ;
203
+ this . toggleKey = null ;
99
204
}
100
205
update ( time , delta ) {
101
- if ( this . shiftKey . isDown ) {
206
+ if ( this . modKey . isDown ) {
102
207
this . controls . update ( delta ) ;
103
208
}
104
209
}
@@ -128,8 +233,12 @@ class DisplayListWatcher extends Phaser.Plugins.ScenePlugin {
128
233
this . camera . preRender ( ) ;
129
234
this . renderText ( renderer , this . text , this . camera ) ;
130
235
}
131
- sceneDestroy ( ) {
236
+ destroy ( ) {
132
237
this . stop ( ) ;
238
+ this . pluginManager = null ;
239
+ this . game = null ;
240
+ this . scene = null ;
241
+ this . systems = null ;
133
242
}
134
243
hide ( ) {
135
244
this . text . visible = false ;
0 commit comments