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YiuNB67gaIfhpToxJJ1Hb4mjr//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
17+ } ;
18+ const fontImage = "data:image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2VMZDZxbIcnIoj9F7f/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 ) => {
521 const { type } = obj ;
622 let output ;
723 let count = null ;
@@ -16,45 +32,108 @@ const GetObjectDescription = (obj, depth) => {
1632 if ( type === "DisplayList" ) {
1733 output = `${ obj . type } ${ obj . name } ${ countStr } ` ;
1834 } else {
19- const posStr = `(${ obj . x . toFixed ( 1 ) } , ${ obj . y . toFixed ( 1 ) } )` ;
2035 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 } ` ;
2338 }
39+ output = output . replace ( twoSpaces , " " ) . trimEnd ( ) ;
2440 return output ;
2541} ;
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+ }
3269 }
3370 }
3471 return output ;
3572} ;
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 ;
3681class 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+ }
3793 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+ } ) ;
39105 if ( this . systems . settings . key === "__SYSTEM" ) {
40106 return ;
41107 }
42108 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 ( ) ;
48119 }
49120 start ( ) {
121+ const { cache, events, make, renderer } = this . systems ;
122+ const fontCache = cache . bitmapFont ;
50123 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 ) ;
52131 this . camera = new Phaser . Cameras . Scene2D . Camera (
53132 0 ,
54133 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 ) ;
58137 this . controls = new Phaser . Cameras . Controls . FixedKeyControl ( {
59138 camera : this . camera ,
60139 up : keyboard . addKey ( "UP" ) ,
@@ -63,42 +142,68 @@ class DisplayListWatcher extends Phaser.Plugins.ScenePlugin {
63142 right : keyboard . addKey ( "RIGHT" ) ,
64143 speed : 1
65144 } ) ;
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 ) => {
72151 if ( event . shiftKey ) {
73152 this . toggle ( ) ;
74153 }
75154 } ) ;
76- this . xKey . on ( "down" , ( key , event ) => {
155+ this . showKey . on ( "down" , ( key , event ) => {
77156 if ( event . shiftKey ) {
78157 this . show ( ) ;
79158 }
80159 } ) ;
81- this . cKey . on ( "down" , ( key , event ) => {
160+ this . hideKey . on ( "down" , ( key , event ) => {
82161 if ( event . shiftKey ) {
83162 this . hide ( ) ;
84163 }
85164 } ) ;
86- this . vKey . on ( "down" , ( key , event ) => {
165+ this . resetKey . on ( "down" , ( key , event ) => {
87166 if ( event . shiftKey ) {
88167 this . resetCamera ( ) ;
89168 }
90169 } ) ;
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 ;
93172 }
94173 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+ }
95194 if ( this . text ) {
96195 this . text . destroy ( ) ;
97- this . text = null ;
98196 }
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 ;
99204 }
100205 update ( time , delta ) {
101- if ( this . shiftKey . isDown ) {
206+ if ( this . modKey . isDown ) {
102207 this . controls . update ( delta ) ;
103208 }
104209 }
@@ -128,8 +233,12 @@ class DisplayListWatcher extends Phaser.Plugins.ScenePlugin {
128233 this . camera . preRender ( ) ;
129234 this . renderText ( renderer , this . text , this . camera ) ;
130235 }
131- sceneDestroy ( ) {
236+ destroy ( ) {
132237 this . stop ( ) ;
238+ this . pluginManager = null ;
239+ this . game = null ;
240+ this . scene = null ;
241+ this . systems = null ;
133242 }
134243 hide ( ) {
135244 this . text . visible = false ;
0 commit comments