• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 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 XScroll {
19  constructor(options) {
20    if (options['type']) {
21      this.type_ = options['type'];
22    }
23    else {
24      this.type_ = 'right';
25    }
26    this.barOff_ = 0;
27    this.useScrH_ = false;
28  }
29  move(x, y, w, h) {
30    this.posX_ = x;
31    this.posY_ = y;
32    this.posW_ = w;
33    this.posH_ = h;
34    return this;
35  }
36  draw() {
37    X2DFast.gi().fillRect(this.posX_, this.posY_, this.posW_, this.posH_, 0x40808080);
38    if (this.type_ === 'right') {
39      X2DFast.gi().fillRect(this.posX_ + 1, this.posY_ + this.barOff_, this.posW_ - 2, this.posH_ / 3, 0x40000000);
40    }
41    else if (this.type_ === 'button') {
42      X2DFast.gi().fillRect(this.posX_ + this.barOff_, this.posY_ + 1, this.posW_ / 3, this.posH_ - 2, 0x40000000);
43    }
44  }
45  isTouchIn(x, y) {
46    if (x < this.posX_) return false;
47    if (y < this.posY_) return false;
48    if (x > this.posX_ + this.posW_) return false;
49    if (y > this.posY_ + this.posH_) return false;
50    return true;
51  }
52  setBarOff(rate) {
53    if (this.type_ === 'right') {
54      this.barOff_ = this.posH_ * 2 / 3 * rate;
55    }
56    else {
57      this.barOff_ = this.posW_ * 2 / 3 * rate;
58    }
59    this.modifyBarOff(0, 0);
60  }
61  getBarOff() {
62    if (this.type_ === 'right') {
63      return this.barOff_ / (this.posH_ * 2 / 3);
64    }
65    else {
66      return this.barOff_ / (this.posW_ * 2 / 3);
67    }
68  }
69  modifyBarOff(dx, dy) {
70    if (this.type_ === 'right') {
71      this.barOff_ += dy;
72      if (this.barOff_ > this.posH_ * 2 / 3) {
73        this.barOff_ = this.posH_ * 2 / 3;
74      }
75    }
76    else {
77      this.barOff_ += dx;
78      if (this.barOff_ > this.posW_ * 2 / 3) {
79        this.barOff_ = this.posW_ * 2 / 3;
80      }
81    }
82    if (this.barOff_ < 0) {
83      this.barOff_ = 0;
84    }
85  }
86  onTouch(msg, x, y) {
87    let isIn = this.isTouchIn(x, y);
88    switch (msg) {
89      case 10:
90        if (this.type_ === 'right') {
91          this.modifyBarOff(0, -this.posH_ / 3 / 10);
92        }
93        else if (isIn) {
94          this.modifyBarOff(-this.posW_ / 3 / 10, 0);
95        }
96        break;
97      case 11:
98        if (this.type_ === 'right') {
99          this.modifyBarOff(0, this.posH_ / 3 / 10);
100        }
101        else if (isIn) {
102          this.modifyBarOff(this.posW_ / 3 / 10, 0);
103        }
104        break;
105      case 1:
106        if (isIn) {
107          this.touchDown_ = true;
108          if (this.type_ === 'right') {
109            if (y - this.posY_ < this.barOff_ || y - this.posY_ > this.barOff_ + this.posH_ / 3) {
110              this.barOff_ = y - this.posY_ - this.posH_ / 3 / 2;
111              this.modifyBarOff(0, 0);
112            }
113          }
114          else {
115            if (x - this.posX_ < this.barOff_ || x - this.posX_ > this.barOff_ + this.posW_ / 3) {
116              this.barOff_ = x - this.posX_ - this.posW_ / 3 / 2;
117              this.modifyBarOff(0, 0);
118            }
119          }
120          this.touchPos_ = {
121            x: x,
122            y: y,
123          }
124        }
125        break;
126      case 2:
127        if (this.touchDown_) {
128          this.modifyBarOff(x - this.touchPos_.x, y - this.touchPos_.y);
129          this.touchPos_.x = x;
130          this.touchPos_.y = y;
131        }
132        break;
133      case 3:
134        this.touchDown_ = false;
135        break;
136    }
137    return isIn;
138  }
139}
140
141module.exports = {
142  XScroll
143}