11import Phaser from "phaser" ;
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 ;
92const fontData = {
10- chars,
11- charsPerRow,
12- height,
13- image,
14- offset,
15- spacing,
16- width
3+ chars : `ABCDEFGHIJKLMNOPQRSTUVWXYZ 12345abcdefghijklmnopqrstuvwxyz 67890{}[]()<>$*-+=/#_%^@\\&|~?'" !,.;:` ,
4+ charsPerRow : 32 ,
5+ height : 13 ,
6+ image : "proggy_clean_inv" ,
7+ offset : { x : 5 , y : 24 } ,
8+ spacing : { x : 0 , y : 0 } ,
9+ width : 7
1710} ;
1811const fontImage = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQ4AAABKCAMAAAB90UpZAAABEVBMVEUAAADUUP1/f398e3x+fn58fHx8fH3///97e3t6e3t5eXl3d3d2dXV2dXZzc3NzdHNxcnFxcXFvb29sbGxtbW1qamprampoaGhmZWVlZmVjY2JjY2NgX2BgYGBeXV5dXl1aWlpYWFdYV1dVVFVUVFVSUVJSUlJPT05PTk9MTEtMTExISUlISUhGRkZGRkVDQ0NCQ0M/QD9AP0A9PD06OTo6Ojk2Njc2Nzc0NDQ0MzMwMDEwMDAuLi0tLS4rKysqKisnKCcnJyckJSQlJSUiIiIfHx8gHx8cHB0ZGRkaGhoXGBcXFxcUFRUVFRQTEhISEhMQEBAODg4ODQ4MCwsMDAwKCQoKCgoICAgGBgYGBQYFBATgX/oDAAADz0lEQVR4AeyVB1LEMAxFRRW9d+5/T8gfj/chNOuEXvRIpEiORfa72QdRFEVRbEx0Jx+zU8JkBbN8VVadf7McMpkzOlmGdCwzafJXZgfkaBOAjbkc6P8X5BhMEpg5s0Pmr8iR7R1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AWIUDbALq+aVGV9t4YmEa6az++CQCGgQAGht2nd1f+QLwKhG67CETzgmjdEO0HovNCdN+InhfR9yNKGVEpiGpD1PskGtJlPAcJ+yNWAAAAAElFTkSuQmCC" ;
1912const twoSpaces = / { 2 , } / g;
@@ -73,30 +66,29 @@ const WalkDisplayListObj = (obj, output = [], currentDepth = 0, maxDepth = 10, m
7366const { POSITIVE_INFINITY } = Number ;
7467const TextureEvents = Phaser . Textures . Events ;
7568const CacheEvents = Phaser . Cache . Events ;
69+ const KeyboardEvents = Phaser . Input . Keyboard . Events ;
7670const SceneEvents = Phaser . Scenes . Events ;
71+ const { KeyCodes } = Phaser . Input . Keyboard ;
7772const ParseRetroFont = Phaser . GameObjects . RetroFont . Parse ;
7873const fontTextureKey = fontData . image ;
7974const fontKey = fontData . image ;
80- let hasPendingFontImage = false ;
8175class DisplayListWatcher extends Phaser . Plugins . ScenePlugin {
8276 constructor ( scene , pluginManager ) {
8377 super ( scene , pluginManager ) ;
8478 this . camera = null ;
8579 this . controls = null ;
86- this . hideKey = null ;
8780 this . modKey = null ;
88- this . resetKey = null ;
89- this . showKey = null ;
9081 this . text = null ;
91- this . toggleKey = null ;
9282 }
9383 boot ( ) {
9484 const { cache, events, settings, textures } = this . systems ;
95- if ( ! hasPendingFontImage && ! textures . exists ( fontTextureKey ) ) {
96- hasPendingFontImage = true ;
97- textures . addBase64 ( fontKey , fontImage ) ;
85+ if ( ! textures . exists ( fontTextureKey ) ) {
86+ this . addImage ( fontKey , fontImage ) ;
9887 }
9988 textures . once ( `${ TextureEvents . ADD_KEY } ${ fontTextureKey } ` , ( ) => {
89+ if ( cache . bitmapFont . has ( fontKey ) ) {
90+ return ;
91+ }
10092 cache . bitmapFont . add ( fontKey , ParseRetroFont ( this . scene , fontData ) ) ;
10193 } ) ;
10294 if ( settings . key === "__SYSTEM" ) {
@@ -109,77 +101,55 @@ class DisplayListWatcher extends Phaser.Plugins.ScenePlugin {
109101 this . start ( ) ;
110102 }
111103 }
112- startIfFontWasAdded ( cache , key ) {
104+ // For Phaser v3.87. Avoids errors from duplicate keys.
105+ addImage ( key , url ) {
106+ const { textures } = this . systems ;
107+ if ( textures . exists ( key ) ) {
108+ }
109+ const image = new Image ( ) ;
110+ image . onload = function onload ( ) {
111+ image . onload = null ;
112+ if ( textures . exists ( key ) ) {
113+ return ;
114+ }
115+ textures . addImage ( key , image ) ;
116+ } ;
117+ image . src = url ;
118+ }
119+ onFontCacheAdded ( cache , key ) {
113120 if ( key !== fontKey ) {
114121 return ;
115122 }
116- cache . events . off ( CacheEvents . ADD , this . startIfFontWasAdded , this ) ;
123+ cache . events . off ( CacheEvents . ADD , this . onFontCacheAdded , this ) ;
117124 this . start ( ) ;
118125 }
119126 start ( ) {
120- const { cache, events, make, renderer } = this . systems ;
127+ const { cache, events, input , make, renderer } = this . systems ;
121128 const fontCache = cache . bitmapFont ;
122- const { keyboard } = this . systems . input ;
123- const { width : width2 , height : height2 } = this . systems . scale ;
129+ const keyboard = input == null ? void 0 : input . keyboard ;
130+ const { width, height } = this . systems . scale ;
124131 if ( ! fontCache . exists ( fontKey ) ) {
125- fontCache . events . on ( CacheEvents . ADD , this . startIfFontWasAdded , this ) ;
132+ fontCache . events . on ( CacheEvents . ADD , this . onFontCacheAdded , this ) ;
126133 return ;
127134 }
128- events . on ( SceneEvents . UPDATE , this . update , this ) ;
129135 events . on ( SceneEvents . RENDER , this . render , this ) ;
130- this . camera = new Phaser . Cameras . Scene2D . Camera (
131- 0 ,
132- 0 ,
133- width2 ,
134- height2
135- ) . setBounds ( 0 , 0 , POSITIVE_INFINITY , POSITIVE_INFINITY ) ;
136- this . controls = new Phaser . Cameras . Controls . FixedKeyControl ( {
137- camera : this . camera ,
138- up : keyboard . addKey ( "UP" ) ,
139- down : keyboard . addKey ( "DOWN" ) ,
140- left : keyboard . addKey ( "LEFT" ) ,
141- right : keyboard . addKey ( "RIGHT" ) ,
142- speed : 1
143- } ) ;
144- this . modKey = keyboard . addKey ( "SHIFT" ) ;
145- this . toggleKey = keyboard . addKey ( "Z" ) ;
146- this . showKey = keyboard . addKey ( "X" ) ;
147- this . hideKey = keyboard . addKey ( "C" ) ;
148- this . resetKey = keyboard . addKey ( "V" ) ;
149- this . toggleKey . on ( "down" , ( _key , event ) => {
150- if ( event . shiftKey ) {
151- this . toggle ( ) ;
152- }
153- } ) ;
154- this . showKey . on ( "down" , ( _key , event ) => {
155- if ( event . shiftKey ) {
156- this . show ( ) ;
157- }
158- } ) ;
159- this . hideKey . on ( "down" , ( _key , event ) => {
160- if ( event . shiftKey ) {
161- this . hide ( ) ;
162- }
163- } ) ;
164- this . resetKey . on ( "down" , ( _key , event ) => {
165- if ( event . shiftKey ) {
166- this . resetCamera ( ) ;
167- }
168- } ) ;
136+ this . camera = new Phaser . Cameras . Scene2D . Camera ( 0 , 0 , width , height ) . setBounds ( 0 , 0 , POSITIVE_INFINITY , POSITIVE_INFINITY ) . setRoundPixels ( true ) ;
137+ if ( keyboard ) {
138+ events . on ( SceneEvents . UPDATE , this . update , this ) ;
139+ this . addKeyboardControls ( keyboard ) ;
140+ }
169141 this . text = make . bitmapText ( { font : fontKey } , false ) ;
170142 this . renderText = renderer . type === Phaser . WEBGL ? this . text . renderWebGL : this . text . renderCanvas ;
171143 }
172144 stop ( ) {
173145 const { cache, events, input, settings } = this . systems ;
174- const { keyboard } = input ;
175- cache . bitmapFont . events . off ( CacheEvents . ADD , this . startIfFontWasAdded , this ) ;
146+ const keyboard = input == null ? void 0 : input . keyboard ;
147+ cache . bitmapFont . events . off ( CacheEvents . ADD , this . onFontCacheAdded , this ) ;
176148 events . off ( SceneEvents . UPDATE , this . update , this ) ;
177149 events . off ( SceneEvents . RENDER , this . render , this ) ;
178- keyboard . removeKey ( this . hideKey , true ) ;
179- keyboard . removeKey ( this . modKey , true ) ;
180- keyboard . removeKey ( this . resetKey , true ) ;
181- keyboard . removeKey ( this . showKey , true ) ;
182- keyboard . removeKey ( this . toggleKey , true ) ;
150+ if ( keyboard ) {
151+ keyboard . off ( KeyboardEvents . ANY_KEY_DOWN , this . onAnyKeyDown , this ) ;
152+ }
183153 if ( this . camera ) {
184154 this . camera . destroy ( ) ;
185155 }
@@ -195,11 +165,7 @@ class DisplayListWatcher extends Phaser.Plugins.ScenePlugin {
195165 }
196166 this . camera = null ;
197167 this . controls = null ;
198- this . hideKey = null ;
199168 this . modKey = null ;
200- this . resetKey = null ;
201- this . showKey = null ;
202- this . toggleKey = null ;
203169 }
204170 update ( _time , delta ) {
205171 if ( this . modKey . isDown ) {
@@ -223,16 +189,36 @@ class DisplayListWatcher extends Phaser.Plugins.ScenePlugin {
223189 0
224190 ) ;
225191 this . text . setPosition ( x , y ) . setText ( output ) ;
192+ this . camera . setBounds ( 0 , 0 , POSITIVE_INFINITY , this . text . height ) ;
226193 this . camera . preRender ( ) ;
227194 this . renderText ( renderer , this . text , this . camera ) ;
228195 }
229196 destroy ( ) {
230197 this . stop ( ) ;
198+ this . systems . events . off ( SceneEvents . START , this . start , this ) . off ( SceneEvents . SHUTDOWN , this . stop , this ) . off ( SceneEvents . DESTROY , this . destroy , this ) ;
231199 this . pluginManager = null ;
232200 this . game = null ;
233201 this . scene = null ;
234202 this . systems = null ;
235203 }
204+ onAnyKeyDown ( event ) {
205+ if ( ! this . modKey . isDown ) return ;
206+ const method = this . keyMap [ event . keyCode ] ;
207+ if ( ! method ) return ;
208+ method . call ( this ) ;
209+ }
210+ addKeyboardControls ( keyboard ) {
211+ this . controls = new Phaser . Cameras . Controls . FixedKeyControl ( {
212+ camera : this . camera ,
213+ up : keyboard . addKey ( KeyCodes . UP ) ,
214+ down : keyboard . addKey ( KeyCodes . DOWN ) ,
215+ left : keyboard . addKey ( KeyCodes . LEFT ) ,
216+ right : keyboard . addKey ( KeyCodes . RIGHT ) ,
217+ speed : 1
218+ } ) ;
219+ this . modKey = keyboard . addKey ( KeyCodes . SHIFT ) ;
220+ keyboard . on ( KeyboardEvents . ANY_KEY_DOWN , this . onAnyKeyDown , this ) ;
221+ }
236222 hide ( ) {
237223 this . text . visible = false ;
238224 }
@@ -245,7 +231,30 @@ class DisplayListWatcher extends Phaser.Plugins.ScenePlugin {
245231 resetCamera ( ) {
246232 this . camera . setScroll ( 0 , 0 ) ;
247233 }
234+ pageUp ( ) {
235+ this . camera . scrollY -= this . camera . height ;
236+ }
237+ pageDown ( ) {
238+ this . camera . scrollY += this . camera . height ;
239+ }
240+ pageStart ( ) {
241+ this . camera . scrollY = 0 ;
242+ }
243+ pageEnd ( ) {
244+ this . camera . scrollY = POSITIVE_INFINITY ;
245+ }
248246}
247+ const proto = DisplayListWatcher . prototype ;
248+ proto . keyMap = {
249+ [ KeyCodes . C ] : proto . hide ,
250+ [ KeyCodes . END ] : proto . pageEnd ,
251+ [ KeyCodes . HOME ] : proto . pageStart ,
252+ [ KeyCodes . PAGE_DOWN ] : proto . pageDown ,
253+ [ KeyCodes . PAGE_UP ] : proto . pageUp ,
254+ [ KeyCodes . V ] : proto . resetCamera ,
255+ [ KeyCodes . X ] : proto . show ,
256+ [ KeyCodes . Z ] : proto . toggle
257+ } ;
249258export {
250259 DisplayListWatcher as default
251260} ;
0 commit comments