summaryrefslogtreecommitdiff
path: root/src/scripts/views/layers/hover.ts
blob: b9f5509c395855fe0772d70e2983834d9276159a (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import {Layer} from "./layer";
import {MapView} from "../mapview";
import {Colors} from "../../colors";
import {DCObjectLayer} from "./dcobject";
import {CELL_SIZE} from "../../controllers/mapcontroller";


/**
 * Class responsible for rendering the hover layer.
 */
export class HoverLayer implements Layer {
    public container: createjs.Container;
    public hoverTilePosition: IGridPosition;

    private mapView: MapView;
    private hoverTile: createjs.Shape;
    private hoverRack: createjs.Container;
    private hoverPSU: createjs.Container;
    private hoverCoolingItem: createjs.Container;


    constructor(mapView: MapView) {
        this.mapView = mapView;
        this.container = new createjs.Container();

        this.initialDraw();
    }

    /**
     * Draws the hover tile to the container at its current location and with its current color.
     */
    public draw(): void {
        let color;

        if (this.mapView.roomLayer.checkHoverTileValidity(this.hoverTilePosition)) {
            color = Colors.ROOM_HOVER_VALID;
        } else {
            color = Colors.ROOM_HOVER_INVALID;
        }

        this.hoverTile.graphics.clear().beginFill(color)
            .drawRect(this.hoverTilePosition.x * CELL_SIZE, this.hoverTilePosition.y * CELL_SIZE,
                CELL_SIZE, CELL_SIZE)
            .endFill();
        if (this.hoverRack.visible) {
            this.hoverRack.x = this.hoverTilePosition.x * CELL_SIZE;
            this.hoverRack.y = this.hoverTilePosition.y * CELL_SIZE;
        } else if (this.hoverPSU.visible) {
            this.hoverPSU.x = this.hoverTilePosition.x * CELL_SIZE;
            this.hoverPSU.y = this.hoverTilePosition.y * CELL_SIZE;
        } else if (this.hoverCoolingItem.visible) {
            this.hoverCoolingItem.x = this.hoverTilePosition.x * CELL_SIZE;
            this.hoverCoolingItem.y = this.hoverTilePosition.y * CELL_SIZE;
        }
    }

    /**
     * Performs the initial drawing action.
     */
    public initialDraw(): void {
        this.container.removeAllChildren();

        this.hoverTile = new createjs.Shape();

        this.hoverTilePosition = {x: 0, y: 0};

        this.hoverTile = MapView.drawRectangle(this.hoverTilePosition, Colors.ROOM_HOVER_VALID, this.container);
        this.hoverTile.visible = false;

        this.hoverRack = DCObjectLayer.drawHoverRack(this.hoverTilePosition);
        this.hoverPSU = DCObjectLayer.drawHoverPSU(this.hoverTilePosition);
        this.hoverCoolingItem = DCObjectLayer.drawHoverCoolingItem(this.hoverTilePosition);

        this.container.addChild(this.hoverRack);
        this.container.addChild(this.hoverPSU);
        this.container.addChild(this.hoverCoolingItem);

        this.hoverRack.visible = false;
        this.hoverPSU.visible = false;
        this.hoverCoolingItem.visible = false;
    }

    /**
     * Sets the hover tile visibility to true/false.
     *
     * @param value The visibility value
     */
    public setHoverTileVisibility(value: boolean): void {
        this.hoverTile.visible = value;
        this.mapView.updateScene = true;
    }

    /**
     * Sets the hover item visibility to true/false.
     *
     * @param value The visibility value
     * @param type The type of the object to be shown
     */
    public setHoverItemVisibility(value: boolean, type?: string): void {
        if (value === true) {
            this.hoverTile.visible = true;

            this.setHoverItemVisibilities(type);
        } else {
            this.hoverTile.visible = false;
            this.hoverRack.visible = false;
            this.hoverPSU.visible = false;
            this.hoverCoolingItem.visible = false;
        }

        this.mapView.updateScene = true;
    }

    private setHoverItemVisibilities(type: string): void {
        if (type === "RACK") {
            this.hoverRack.visible = true;
            this.hoverPSU.visible = false;
            this.hoverCoolingItem.visible = false;
        } else if (type === "PSU") {
            this.hoverRack.visible = false;
            this.hoverPSU.visible = true;
            this.hoverCoolingItem.visible = false;
        } else if (type === "COOLING_ITEM") {
            this.hoverRack.visible = false;
            this.hoverPSU.visible = false;
            this.hoverCoolingItem.visible = true;
        }
    }
}