1- import ColourDecider from "../utilities/colourdecider" ;
21import * as Environment from "../environment" ;
3- import Graphics from "./graphics" ;
4- import * as Logger from "../utilities/logger" ;
52import Options from "../models/options" ;
3+ import ColourDecider from "../utilities/colourdecider" ;
4+ import * as Logger from "../utilities/logger" ;
65import PeepFinder from "../utilities/peepfinder" ;
6+ import Graphics from "./graphics" ;
77
88export default class MapWindow {
99 onUpdate ?: ( ) => void ;
@@ -24,7 +24,9 @@ export default class MapWindow {
2424 // Map information
2525 private mapColours : number [ ] [ ] = [ ] ;
2626
27- private mapSize : number = 0 ;
27+ private mapWidth : number = 0 ;
28+
29+ private mapHeight : number = 0 ;
2830
2931 private peepFinder : PeepFinder = new PeepFinder ( ) ;
3032
@@ -45,7 +47,9 @@ export default class MapWindow {
4547 } ;
4648
4749 private createWindow ( ) : Window {
48- this . mapSize = map . size . x - 2 ; // Size is stored as 2 bigger than it really is for some reason
50+ // Size is stored as 2 bigger than it really is to have a one-tile margin
51+ this . mapWidth = map . size . x - 2 ;
52+ this . mapHeight = map . size . y - 2 ;
4953
5054 const btnScaleDown : ButtonDesc = {
5155 type : "button" ,
@@ -95,7 +99,7 @@ export default class MapWindow {
9599 image : 5169 , // SPR_ROTATE_ARROW
96100 onClick : ( ) : void => {
97101 this . rotation = ( this . rotation + 1 ) % 4 ;
98- this . draw ( ) ;
102+ this . changeSize ( ) ;
99103 }
100104 } ;
101105
@@ -251,22 +255,23 @@ export default class MapWindow {
251255 }
252256 } ;
253257
254- const mapWidgetSize = this . tileSize * this . mapSize ;
258+ const mapWidgetWidth = this . tileSize * this . mapWidth ;
259+ const mapWidgetHeight = this . tileSize * this . mapHeight ;
255260
256261 const mapWidget : ButtonDesc = {
257262 x : this . margin ,
258263 y : btnScaleDown . y + btnScaleDown . height + this . margin ,
259264 type : "button" ,
260- width : mapWidgetSize ,
261- height : mapWidgetSize ,
265+ width : mapWidgetWidth ,
266+ height : mapWidgetHeight ,
262267 name : "mapWidget" ,
263268 image : this . mapImageId
264269 } ;
265270
266271 const window = ui . openWindow ( {
267272 classification : Environment . namespace ,
268273 title : `${ Environment . pluginName } (v${ Environment . pluginVersion } )` ,
269- width : this . margin * 2 + this . tileSize * this . mapSize ,
274+ width : this . margin * 2 + mapWidgetWidth ,
270275 height : mapWidget . y + mapWidget . height + this . margin ,
271276 maxHeight : 10000 ,
272277 maxWidth : 10000 ,
@@ -322,16 +327,16 @@ export default class MapWindow {
322327 const start = new Date ( ) . getTime ( ) ;
323328
324329 if ( this . options . showPeeps ) {
325- this . peepFinder . loadPeepData ( this . mapSize ) ;
330+ this . peepFinder . loadPeepData ( this . mapWidth , this . mapHeight ) ;
326331 }
327332
328333 this . mapColours = [ ] ;
329- for ( let x = 0 ; x < this . mapSize ; x += 1 ) {
334+ for ( let x = 0 ; x < this . mapWidth ; x += 1 ) {
330335 this . mapColours [ x ] = [ ] ;
331336 }
332337
333- for ( let x = 0 ; x < this . mapSize ; x += 1 ) {
334- for ( let y = 0 ; y < this . mapSize ; y += 1 ) {
338+ for ( let x = 0 ; x < this . mapWidth ; x += 1 ) {
339+ for ( let y = 0 ; y < this . mapHeight ; y += 1 ) {
335340 this . mapColours [ x ] [ y ] = ColourDecider . getColourAtTile ( x , y , this . options , this . peepFinder ) ;
336341 }
337342 }
@@ -347,13 +352,15 @@ export default class MapWindow {
347352 }
348353
349354 const mapWidget = < ButtonWidget > this . window . findWidget ( "mapWidget" ) ;
350- const mapWidgetSize = this . tileSize * this . mapSize ;
351- mapWidget . width = mapWidgetSize ;
352- mapWidget . height = mapWidgetSize ;
355+ const isRotated = this . rotation % 2 !== 0 ;
356+ const mapWidgetWidth = this . tileSize * ( isRotated ? this . mapHeight : this . mapWidth ) ;
357+ const mapWidgetHeight = this . tileSize * ( isRotated ? this . mapWidth : this . mapHeight ) ;
358+ mapWidget . width = mapWidgetWidth ;
359+ mapWidget . height = mapWidgetHeight ;
353360
354361 const btnShowPeeps = ( this . window . widgets . filter ( ( w ) => w . name === "showPeeps" ) [ 0 ] as ButtonWidget ) ;
355- this . window . minWidth = Math . max ( mapWidget . x + mapWidgetSize + this . margin , btnShowPeeps . x + btnShowPeeps . width + this . margin ) ;
356- this . window . minHeight = mapWidget . y + mapWidgetSize + this . margin ;
362+ this . window . minWidth = Math . max ( mapWidget . x + mapWidgetWidth + this . margin , btnShowPeeps . x + btnShowPeeps . width + this . margin ) ;
363+ this . window . minHeight = mapWidget . y + mapWidgetHeight + this . margin ;
357364
358365 this . window . width = this . window . minWidth ;
359366 this . window . height = this . window . minHeight ;
@@ -365,30 +372,31 @@ export default class MapWindow {
365372 initializeImage ( ) {
366373 this . mapImageId = Graphics . allocateImage ( < RawPixelData > {
367374 type : "raw" ,
368- height : this . mapSize ,
369- width : this . mapSize ,
375+ height : this . mapHeight ,
376+ width : this . mapWidth ,
370377 data : new Uint8Array ( 0 )
371378 } ) ?? 0 ;
372379
373380 if ( this . window !== undefined ) {
374381 const mapWidget = < ButtonWidget > this . window . findWidget ( "mapWidget" ) ;
375- mapWidget . width = this . mapSize * this . tileSize ;
376- mapWidget . height = this . mapSize * this . tileSize ;
382+ mapWidget . width = this . mapWidth * this . tileSize ;
383+ mapWidget . height = this . mapHeight * this . tileSize ;
377384 mapWidget . image = this . mapImageId ?? 0 ;
378385 }
379386 }
380387
381388 draw ( ) {
382- const scaledMap = MapWindow . scaleMap ( this . mapColours , this . tileSize ) ;
383- const rotatedMap = MapWindow . rotateMap ( scaledMap , this . rotation ) ;
389+ const rotatedMap = MapWindow . rotateMap ( this . mapColours , this . rotation ) ;
390+ const scaledMap = MapWindow . scaleMap ( rotatedMap , this . tileSize ) ;
391+ const finalMap = scaledMap ;
384392
385393 const start = new Date ( ) . getTime ( ) ;
386394 Logger . debug ( "Reducing map..." ) ;
387395
388396 const flattenedColours : number [ ] = [ ] ;
389- for ( let i = 0 ; i < rotatedMap . length ; i += 1 ) {
390- for ( let j = 0 ; j < rotatedMap [ i ] . length ; j += 1 ) {
391- flattenedColours . push ( rotatedMap [ i ] [ j ] ) ;
397+ for ( let y = 0 ; y < finalMap [ 0 ] . length ; y += 1 ) {
398+ for ( let x = 0 ; x < finalMap . length ; x += 1 ) {
399+ flattenedColours . push ( finalMap [ x ] [ y ] ) ;
392400 }
393401 }
394402
@@ -398,31 +406,57 @@ export default class MapWindow {
398406
399407 Graphics . setPixelData ( this . mapImageId , < RawPixelData > {
400408 type : "raw" ,
401- height : this . mapSize * this . tileSize ,
402- width : this . mapSize * this . tileSize ,
409+ height : finalMap [ 0 ] . length ,
410+ width : finalMap . length ,
403411 data : new Uint8Array ( flattenedColours )
404412 } ) ;
405413 }
406414
407415 static rotateMap ( input : number [ ] [ ] , rotation : number ) : number [ ] [ ] {
408416 const start = new Date ( ) . getTime ( ) ;
409417 Logger . debug ( "Rotating map..." ) ;
410- const returnValue : number [ ] [ ] = [ ] ;
411- for ( let x = 0 ; x < input . length ; x += 1 ) {
412- returnValue [ x ] = [ ] ;
413- }
414418
415- for ( let x = 0 ; x < input . length ; x += 1 ) {
416- for ( let y = 0 ; y < input . length ; y += 1 ) {
417- let colour : number ;
418- switch ( rotation ) {
419- case 1 : colour = input [ - y + input . length - 1 ] [ x ] ; break ;
420- case 2 : colour = input [ - x + input . length - 1 ] [ - y + input . length - 1 ] ; break ;
421- case 3 : colour = input [ y ] [ - x + input . length - 1 ] ; break ;
422- default : colour = input [ x ] [ y ] ; break ;
419+ var numRows = input . length ;
420+ var numCols = input [ 0 ] . length ;
421+ var returnValue : number [ ] [ ] ;
422+
423+ switch ( rotation ) {
424+ case 1 :
425+ returnValue = [ ] ;
426+ for ( var x = 0 ; x < numCols ; x ++ ) {
427+ returnValue [ x ] = [ ] ;
428+ for ( var y = 0 ; y < numRows ; y ++ ) {
429+ returnValue [ x ] [ y ] = input [ y ] [ numCols - x - 1 ] ;
430+ }
423431 }
424- returnValue [ x ] [ y ] = colour ;
425- }
432+ break ;
433+ case 2 :
434+ returnValue = [ ] ;
435+ for ( var x = 0 ; x < numRows ; x ++ ) {
436+ returnValue [ x ] = [ ] ;
437+ for ( var y = 0 ; y < numCols ; y ++ ) {
438+ returnValue [ x ] [ y ] = input [ numRows - x - 1 ] [ numCols - y - 1 ] ;
439+ }
440+ }
441+ break ;
442+ case 3 :
443+ returnValue = [ ] ;
444+ for ( var x = 0 ; x < numCols ; x ++ ) {
445+ returnValue [ x ] = [ ] ;
446+ for ( var y = 0 ; y < numRows ; y ++ ) {
447+ returnValue [ x ] [ y ] = input [ numRows - y - 1 ] [ x ] ;
448+ }
449+ }
450+ break ;
451+ default :
452+ returnValue = [ ] ;
453+ for ( var x = 0 ; x < numRows ; x ++ ) {
454+ returnValue [ x ] = [ ] ;
455+ for ( var y = 0 ; y < numCols ; y ++ ) {
456+ returnValue [ x ] [ y ] = input [ x ] [ y ] ;
457+ }
458+ }
459+ break ;
426460 }
427461
428462 const finish = new Date ( ) . getTime ( ) ;
@@ -443,7 +477,7 @@ export default class MapWindow {
443477 }
444478
445479 for ( let x = 0 ; x < mapData . length ; x += 1 ) {
446- for ( let y = 0 ; y < mapData . length ; y += 1 ) {
480+ for ( let y = 0 ; y < mapData [ 0 ] . length ; y += 1 ) {
447481 for ( let cx = 0 ; cx < tileSize ; cx += 1 ) {
448482 for ( let cy = 0 ; cy < tileSize ; cy += 1 ) {
449483 returnValue [ x * tileSize + cx ] [ y * tileSize + cy ] = mapData [ x ] [ y ] ;
0 commit comments