diff options
Diffstat (limited to 'src/scripts/views/layers/roomtext.ts')
| -rw-r--r-- | src/scripts/views/layers/roomtext.ts | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/scripts/views/layers/roomtext.ts b/src/scripts/views/layers/roomtext.ts new file mode 100644 index 00000000..65ea0735 --- /dev/null +++ b/src/scripts/views/layers/roomtext.ts @@ -0,0 +1,68 @@ +import {MapView} from "../mapview"; +import {Colors} from "../../colors"; +import {Util} from "../../util"; +import {Layer} from "./layer"; +import {CELL_SIZE} from "../../controllers/mapcontroller"; + + +export class RoomTextLayer implements Layer { + private static TEXT_PADDING = 4; + + public container: createjs.Container; + + private mapView: MapView; + + + constructor(mapView: MapView) { + this.mapView = mapView; + this.container = new createjs.Container(); + + this.draw(); + } + + public draw(): void { + this.container.removeAllChildren(); + + this.mapView.currentDatacenter.rooms.forEach((room: IRoom) => { + if (room.name !== "" && room.roomType !== "") { + this.renderTextOverlay(room); + } + }); + } + + public setVisibility(value: boolean): void { + this.mapView.animate(this.container, {alpha: value === true ? 1 : 0}); + } + + /** + * Draws a name and type overlay over the given room. + */ + private renderTextOverlay(room: IRoom): void { + if (room.name === null || room.tiles.length === 0) { + return; + } + + let textPos = Util.calculateRoomNamePosition(room); + + let bottomY = this.renderText(room.name, "12px Arial", textPos, + textPos.topLeft.y * CELL_SIZE + RoomTextLayer.TEXT_PADDING); + this.renderText("Type: " + Util.toSentenceCase(room.roomType), "10px Arial", textPos, bottomY + 5); + } + + private renderText(text: string, font: string, textPos: IRoomNamePos, startY: number): number { + let name = new createjs.Text(text, font, Colors.ROOM_NAME_COLOR); + + if (name.getMeasuredWidth() > textPos.length * CELL_SIZE - RoomTextLayer.TEXT_PADDING * 2) { + name.scaleX = name.scaleY = (textPos.length * CELL_SIZE - RoomTextLayer.TEXT_PADDING * 2) / + name.getMeasuredWidth(); + } + + // Position the text to the top left of the selected tile + name.x = textPos.topLeft.x * CELL_SIZE + RoomTextLayer.TEXT_PADDING; + name.y = startY; + + this.container.addChild(name); + + return name.y + name.getMeasuredHeight() * name.scaleY; + } +} |
