1/*
2 * Copyright (c) 2022-2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16const { X2DFast } = require('../graphics/X2DFast');
17
18class XButton {
19  constructor(x, y, w, h, name) {
20    this.pm2f_ = X2DFast.gi();
21    this.move(x, y, w, h);
22    this.name_ = name;
23    this.touchDown_ = false;
24    this.clicked_ = false;
25    this.rightClicked_ = false;
26    this.disable_ = false;
27    this.nameColor_ = 0xffffffff;
28    this.backgroundColor_ = 0xff487eb8;
29  }
30
31  move(x, y, w, h) {
32    this.posX_ = x;
33    this.posY_ = y;
34    this.posW_ = w;
35    this.posH_ = h;
36    return this;
37  }
38
39  draw() {
40    const COLOR_OFF = 0x00202020;
41    const SIZE = 14;
42    let coloroff = 0;
43    if (this.touchDown_) {
44      coloroff = COLOR_OFF;
45    }
46    this.pm2f_.fillRect(
47      this.posX_,
48      this.posY_,
49      this.posW_,
50      this.posH_,
51      this.backgroundColor_ - coloroff
52    );
53    if (this.name_ !== undefined && this.name_.length > 0)
54    {
55      let middle = 2;
56      let yOffset = 2;
57      let sw = 1;
58      let sh = 1;
59      let ra = 0;
60      let ox = -2;
61      let oy = -2;
62      this.pm2f_.drawText(this.name_, SIZE, this.posX_ + this.posW_ / middle, this.posY_ +
63      this.posH_ / middle + yOffset, sw, sh, ra, ox, oy, this.nameColor_ - coloroff);
64    }
65  }
66
67  isTouchInButton(x, y) {
68    if (x < this.posX_) {
69      return false;
70    }
71    if (y < this.posY_) {
72      return false;
73    }
74    if (x > this.posX_ + this.posW_) {
75      return false;
76    }
77    if (y > this.posY_ + this.posH_) {
78      return false;
79    }
80    return true;
81  }
82  procTouch(msg, x, y) {
83    let isIn = this.isTouchInButton(x, y);
84    switch (msg) {
85      case 1:
86        if (isIn) {
87          this.touchDown_ = true;
88        }
89        break;
90      case 2:
91        break;
92      case 3:
93        if (this.touchDown_ && isIn) {
94          this.clicked_ = true;
95        }
96        this.touchDown_ = false;
97        break;
98      case 4:
99        if (isIn) {
100          this.rightDown_ = true;
101        }
102        break;
103      case 6:
104        if (this.rightDown_ && isIn) {
105          this.rightClicked_ = true;
106        }
107        this.rightDown_ = false;
108        break;
109    }
110    return isIn;
111  }
112
113  isClicked() {
114    if (this.clicked_) {
115      this.clicked_ = false;
116      return true;
117    }
118    return false;
119  }
120
121  isRightClicked() {
122    if (this.rightClicked_) {
123      this.rightClicked_ = false;
124      return true;
125    }
126    return false;
127  }
128}
129
130module.exports = {
131  XButton,
132};
133