2020-08-14 13:48:26 +00:00
import { freeCanvas , makeOffscreenBuffer } from "../../../core/buffer_utils" ;
import { globalConfig } from "../../../core/config" ;
import { Loader } from "../../../core/loader" ;
import { Vector } from "../../../core/vector" ;
2020-08-15 13:24:42 +00:00
import { MapChunkView } from "../../map_chunk_view" ;
import { THEME } from "../../theme" ;
2020-08-14 13:48:26 +00:00
import { BaseHUDPart } from "../base_hud_part" ;
/ * *
* Helper class which allows peaking through to the wires layer
* /
export class HUDLayerPreview extends BaseHUDPart {
initialize ( ) {
2020-08-15 13:24:42 +00:00
this . initializeCanvas ( ) ;
this . root . signals . aboutToDestruct . add ( ( ) => freeCanvas ( this . canvas ) ) ;
this . root . signals . resized . add ( this . initializeCanvas , this ) ;
this . previewOverlay = Loader . getSprite ( "sprites/wires/wires_preview.png" ) ;
}
/ * *
* ( re ) initializes the canvas
* /
initializeCanvas ( ) {
if ( this . canvas ) {
freeCanvas ( this . canvas ) ;
delete this . canvas ;
delete this . context ;
}
// Compute how big the preview should be
this . previewSize = Math . round (
Math . min ( 1024 , Math . min ( this . root . gameWidth , this . root . gameHeight ) * 0.8 )
) ;
const [ canvas , context ] = makeOffscreenBuffer ( this . previewSize , this . previewSize , {
2020-08-14 13:48:26 +00:00
smooth : true ,
label : "layerPeeker" ,
reusable : true ,
} ) ;
2020-08-15 13:24:42 +00:00
context . clearRect ( 0 , 0 , this . previewSize , this . previewSize ) ;
2020-08-14 13:48:26 +00:00
this . canvas = canvas ;
this . context = context ;
}
/ * *
* Prepares the canvas to render at the given worldPos and the given camera scale
*
* @ param { Vector } worldPos
* @ param { number } scale 1 / zoomLevel
* /
2021-02-26 14:20:37 +00:00
prepareCanvasForPreview ( worldPos , scale , layer ) {
2020-08-15 13:24:42 +00:00
this . context . clearRect ( 0 , 0 , this . previewSize , this . previewSize ) ;
this . context . fillStyle = THEME . map . wires . previewColor ;
this . context . fillRect ( 0 , 0 , this . previewSize , this . previewSize ) ;
2020-08-14 13:48:26 +00:00
2020-08-15 13:24:42 +00:00
const dimensions = scale * this . previewSize ;
2020-08-14 13:48:26 +00:00
const startWorldX = worldPos . x - dimensions / 2 ;
const startWorldY = worldPos . y - dimensions / 2 ;
const startTileX = Math . floor ( startWorldX / globalConfig . tileSize ) ;
const startTileY = Math . floor ( startWorldY / globalConfig . tileSize ) ;
const tileDimensions = Math . ceil ( dimensions / globalConfig . tileSize ) ;
this . context . save ( ) ;
this . context . scale ( 1 / scale , 1 / scale ) ;
this . context . translate (
startTileX * globalConfig . tileSize - startWorldX ,
startTileY * globalConfig . tileSize - startWorldY
) ;
for ( let dx = 0 ; dx < tileDimensions ; ++ dx ) {
for ( let dy = 0 ; dy < tileDimensions ; ++ dy ) {
const tileX = dx + startTileX ;
2021-02-26 14:20:37 +00:00
const tileY = dy + startTileY ; // you labed it as what is this on map_chunk I already said I am trying to make tiles use string instead of BaseItem on discord
2020-08-14 13:48:26 +00:00
2021-02-26 14:20:37 +00:00
const content = this . root . map . getLayerContentXY ( tileX , tileY , layer ) ;
2020-08-14 13:48:26 +00:00
if ( content ) {
2021-02-26 14:20:37 +00:00
MapChunkView . drawSingleOverviewTile ( {
2020-08-15 15:51:28 +00:00
context : this . context ,
x : dx * globalConfig . tileSize ,
y : dy * globalConfig . tileSize ,
entity : content ,
tileSizePixels : globalConfig . tileSize ,
} ) ;
2020-08-14 13:48:26 +00:00
}
}
}
this . context . restore ( ) ;
2020-08-15 13:24:42 +00:00
this . context . globalCompositeOperation = "destination-in" ;
this . previewOverlay . draw ( this . context , 0 , 0 , this . previewSize , this . previewSize ) ;
2020-08-14 13:48:26 +00:00
this . context . globalCompositeOperation = "source-over" ;
return this . canvas ;
}
/ * *
* Renders the preview at the given position
2021-02-26 15:41:17 +00:00
* @ param { import ( "../../../core/draw_parameters" ) . DrawParameters } parameters
2020-08-14 13:48:26 +00:00
* @ param { Vector } worldPos
* @ param { number } scale 1 / zoomLevel
* /
2021-02-26 14:20:37 +00:00
renderPreview ( parameters , worldPos , scale , layer ) {
2020-08-15 16:39:08 +00:00
if ( this . root . currentLayer !== "regular" ) {
2020-08-14 13:48:26 +00:00
// Only supporting wires right now
return ;
}
2021-02-26 14:20:37 +00:00
const canvas = this . prepareCanvasForPreview ( worldPos , scale , layer ) ;
2020-08-14 13:48:26 +00:00
2020-08-15 13:24:42 +00:00
parameters . context . globalAlpha = 0.3 ;
2020-08-14 13:48:26 +00:00
parameters . context . drawImage (
canvas ,
2020-08-15 13:24:42 +00:00
worldPos . x - ( scale * this . previewSize ) / 2 ,
worldPos . y - ( scale * this . previewSize ) / 2 ,
scale * this . previewSize ,
scale * this . previewSize
2020-08-14 13:48:26 +00:00
) ;
2020-08-15 13:24:42 +00:00
parameters . context . globalAlpha = 1 ;
2020-08-14 13:48:26 +00:00
}
}