summaryrefslogtreecommitdiff
path: root/src/scripts/views/layers/roomtext.ts
blob: 65ea07353de620348d29c9bfe627cff758581d61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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;
    }
}