• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    let coloroff = 0;
41    if (this.touchDown_) coloroff = 0x00202020;
42    this.pm2f_.fillRect(
43      this.posX_,
44      this.posY_,
45      this.posW_,
46      this.posH_,
47      this.backgroundColor_ - coloroff
48    );
49    if (this.name_ != undefined && this.name_.length > 0)
50      this.pm2f_.drawText(
51        this.name_,
52        14,
53        this.posX_ + this.posW_ / 2,
54        this.posY_ + this.posH_ / 2 + 2,
55        1,
56        1,
57        0,
58        -2,
59        -2,
60        this.nameColor_ - coloroff
61      );
62  }
63
64  isTouchIn(x, y) {
65    if (x < this.posX_) return false;
66    if (y < this.posY_) return false;
67    if (x > this.posX_ + this.posW_) return false;
68    if (y > this.posY_ + this.posH_) return false;
69    return true;
70  }
71  onTouch(msg, x, y) {
72    let isIn = this.isTouchIn(x, y);
73    switch (msg) {
74      case 1:
75        if (isIn) {
76          this.touchDown_ = true;
77        }
78        break;
79      case 2:
80        break;
81      case 3:
82        if (this.touchDown_ && isIn) {
83          if (this.onClicked_) {
84            this.onClicked_();
85          }
86          else {
87            this.clicked_ = true;
88          }
89        }
90        this.touchDown_ = false;
91        break;
92      case 4:
93        if (isIn) {
94          this.rightDown_ = true;
95        }
96        break;
97      case 6:
98        if (this.rightDown_ && isIn) {
99          this.rightClicked_ = true;
100        }
101        this.rightDown_ = false;
102        break;
103    }
104    return isIn;
105  }
106
107  isClicked() {
108    if (this.clicked_) {
109      this.clicked_ = false;
110      return true;
111    }
112    return false;
113  }
114
115  isRightClicked() {
116    if (this.rightClicked_) {
117      this.rightClicked_ = false;
118      return true;
119    }
120    return false;
121  }
122}
123
124module.exports = {
125  XButton,
126};
127