MenuView.js 8.1 KB
Newer Older
T
init  
tuzhaofeng 已提交
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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var MenuView = (function (_super) {
    __extends(MenuView, _super);
    function MenuView(width, height, viewController) {
        var _this = _super.call(this, width, height, MenuView.MenuCanvasID) || this;
        _this._chessCount = 0;
        _this.viewController = viewController;
        _this.buttonBoderWidth = 0.5;
        _this.button1Alpha = MenuView.untouchedButtonAlpha;
        _this.button2Alpha = MenuView.untouchedButtonAlpha;
        _this.drawButtons();
        _this._statusMessage = "";
        _this.drawStatusBar();
        _this.registerEvents();
        return _this;
    }
    Object.defineProperty(MenuView.prototype, "statusMessage", {
        get: function () {
            return this._statusMessage;
        },
        set: function (s) {
            this._statusMessage = s;
            this.drawStatusBar();
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(MenuView.prototype, "chessCount", {
        get: function () {
            return this._chessCount;
        },
        set: function (x) {
            this._chessCount = x;
            this.button3.number = this.chessCount;
            this.redrawButton3();
        },
        enumerable: true,
        configurable: true
    });
    /**
     * 根据当前设定, 重绘 Menu 视图
     */
    MenuView.prototype.redrawAll = function () {
        this.drawButtons();
        this.drawStatusBar();
    };
    /**
     * (游戏开始时)初始化并绘制所有按钮
     */
    MenuView.prototype.drawButtons = function () {
        this.context.clearRect(0, 0, this.bound.width, this.bound.height / 2);
        if (!this.buttonScale) {
            this.buttonScale = 0.45;
        }
        this.buttonGap = this.bound.width / 4;
        var centerX1 = this.buttonGap / 2;
        var centerY = this.bound.height / 4;
        var radius = Math.min(this.buttonGap / 2, this.bound.height / 4) * this.buttonScale;
        this.button1 = new TwoHalfCircle(centerX1, centerY, radius, "white", "black");
        this.button2 = new TwoHalfCircle(centerX1 + this.buttonGap, centerY, radius, "rgb(225,233,243)", "rgb(30,157,255)");
        this.button3 = new TextCircle(centerX1 + this.buttonGap * 2, centerY, radius, 3);
        this.button4 = new Circle(centerX1 + this.buttonGap * 3, centerY, radius);
        this.button4.fill = true;
        this.button4.fillColor = "grey";
        this.button1.borderWidth = this.buttonBoderWidth;
        this.button2.borderWidth = this.buttonBoderWidth;
        this.button3.borderWidth = this.buttonBoderWidth;
        this.button4.borderWidth = this.buttonBoderWidth;
        this.context.save();
        this.context.globalAlpha = MenuView.untouchedButtonAlpha;
        this.button1.drawOn(this.context);
        this.button2.drawOn(this.context);
        this.button3.drawOn(this.context);
        this.button4.drawOn(this.context);
        this.context.restore();
    };
    MenuView.prototype.redrawButton1 = function () {
        this.context.clearRect(0, 0, this.buttonGap, this.bound.height / 2);
        this.context.save();
        this.context.globalAlpha = this.button1Alpha;
        this.button1.drawOn(this.context);
        this.context.restore();
    };
    MenuView.prototype.redrawButton2 = function () {
        this.context.clearRect(this.buttonGap, 0, this.buttonGap, this.bound.height / 2);
        this.context.save();
        this.context.globalAlpha = this.button2Alpha;
        this.button2.drawOn(this.context);
        this.context.restore();
    };
    MenuView.prototype.redrawButton3 = function () {
        this.context.clearRect(this.buttonGap * 2, 0, this.buttonGap, this.bound.height / 2);
        this.context.save();
        this.context.globalAlpha = this.button3Alpha;
        this.button3.drawOn(this.context);
        this.context.restore();
    };
    MenuView.prototype.redrawButton4 = function () {
        this.context.clearRect(this.buttonGap * 3, 0, this.buttonGap, this.bound.height / 2);
        this.context.save();
        this.context.globalAlpha = this.button4Alpha;
        this.button4.drawOn(this.context);
        this.context.restore();
    };
    /**
     * 在状态栏绘制状态信息
     */
    MenuView.prototype.drawStatusBar = function () {
        this.context.clearRect(0, this.bound.height / 2, this.bound.width, this.bound.height / 2);
        new TextShape(this._statusMessage, this.bound.width / 2, this.bound.height / 3 * 2, true)
            .drawOn(this.context);
    };
    MenuView.prototype.registerEvents = function () {
        var _this = this;
        /**
         * 点击菜单栏的按钮:交由控制器处理
         */
        this.addEventListener("click", function (event) {
            if (Math.pow(event.offsetX - _this.button1.centerX, 2) + Math.pow(event.offsetY - _this.button1.centerY, 2) < Math.pow(_this.button1.radius, 2)) {
                _this.viewController.changeTheme(new DefaultTheme());
            }
            if (Math.pow(event.offsetX - _this.button2.centerX, 2) + Math.pow(event.offsetY - _this.button2.centerY, 2) < Math.pow(_this.button2.radius, 2)) {
                _this.viewController.changeTheme(new VividTheme());
            }
            if (Math.pow(event.offsetX - _this.button3.centerX, 2) + Math.pow(event.offsetY - _this.button3.centerY, 2) < Math.pow(_this.button3.radius, 2)) {
                if (_this.viewController.showChessStep) {
                    _this.viewController.showChessStep = false;
                }
                else {
                    _this.viewController.showChessStep = true;
                }
            }
            if (Math.pow(event.offsetX - _this.button4.centerX, 2) + Math.pow(event.offsetY - _this.button4.centerY, 2) < Math.pow(_this.button4.radius, 2)) {
                _this.viewController.toggleDialog();
            }
        });
        /**
         * 将鼠标移动到菜单栏按钮时改变透明度:由视图自行处理
         * Todo: 每次鼠标移动都重绘是不合理的,应该设置一个额外变量 isInButton,使其仅在进按钮和出按钮时重绘
         */
        this.addEventListener("mousemove", function (event) {
            if (Math.pow(event.offsetX - _this.button1.centerX, 2) + Math.pow(event.offsetY - _this.button1.centerY, 2) < Math.pow(_this.button1.radius, 2)) {
                _this.button1Alpha = MenuView.touchedButtonAlpha;
                _this.redrawButton1();
            }
            else {
                _this.button1Alpha = MenuView.untouchedButtonAlpha;
                _this.redrawButton1();
            }
            if (Math.pow(event.offsetX - _this.button2.centerX, 2) + Math.pow(event.offsetY - _this.button2.centerY, 2) < Math.pow(_this.button2.radius, 2)) {
                _this.button2Alpha = MenuView.touchedButtonAlpha;
                _this.redrawButton2();
            }
            else {
                _this.button2Alpha = MenuView.untouchedButtonAlpha;
                _this.redrawButton2();
            }
            if (Math.pow(event.offsetX - _this.button3.centerX, 2) + Math.pow(event.offsetY - _this.button3.centerY, 2) < Math.pow(_this.button3.radius, 2)) {
                _this.button3Alpha = MenuView.touchedButtonAlpha;
                _this.redrawButton3();
            }
            else {
                _this.button3Alpha = MenuView.untouchedButtonAlpha;
                _this.redrawButton3();
            }
            if (Math.pow(event.offsetX - _this.button4.centerX, 2) + Math.pow(event.offsetY - _this.button4.centerY, 2) < Math.pow(_this.button4.radius, 2)) {
                _this.button4Alpha = MenuView.touchedButtonAlpha;
                _this.redrawButton4();
            }
            else {
                _this.button4Alpha = MenuView.untouchedButtonAlpha;
                _this.redrawButton4();
            }
        });
    };
    return MenuView;
}(AbstractCanvasView));
MenuView.MenuCanvasID = "menu";
MenuView.untouchedButtonAlpha = 0.5;
MenuView.touchedButtonAlpha = 1;
//# sourceMappingURL=MenuView.js.map