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_) {
47      return false;
48    }
49    if (y < this.posY_) {
50      return false;
51    }
52    if (x > this.posX_ + this.posW_) {
53      return false;
54    }
55    if (y > this.posY_ + this.posH_) {
56      return false;
57    }
58    return true;
59  }
60  setBarOff(rate) {
61    if (this.type_ === 'right') {
62      this.barOff_ = this.posH_ * 2 / 3 * rate;
63    }
64    else {
65      this.barOff_ = this.posW_ * 2 / 3 * rate;
66    }
67    this.modifyBarOff(0, 0);
68  }
69  getBarOff() {
70    if (this.type_ === 'right') {
71      return this.barOff_ / (this.posH_ * 2 / 3);
72    }
73    else {
74      return this.barOff_ / (this.posW_ * 2 / 3);
75    }
76  }
77  modifyBarOff(dx, dy) {
78    if (this.type_ === 'right') {
79      this.barOff_ += dy;
80      if (this.barOff_ > this.posH_ * 2 / 3) {
81        this.barOff_ = this.posH_ * 2 / 3;
82      }
83    }
84    else {
85      this.barOff_ += dx;
86      if (this.barOff_ > this.posW_ * 2 / 3) {
87        this.barOff_ = this.posW_ * 2 / 3;
88      }
89    }
90    if (this.barOff_ < 0) {
91      this.barOff_ = 0;
92    }
93  }
94  onTouch(msg, x, y) {
95    let isIn = this.isTouchIn(x, y);
96    switch (msg) {
97      case 10:
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 11:
106        if (this.type_ === 'right') {
107          this.modifyBarOff(0, this.posH_ / 3 / 10);
108        }
109        else if (isIn) {
110          this.modifyBarOff(this.posW_ / 3 / 10, 0);
111        }
112        break;
113      case 1:
114        if (isIn) {
115          this.touchDown_ = true;
116          if (this.type_ === 'right') {
117            if (y - this.posY_ < this.barOff_ || y - this.posY_ > this.barOff_ + this.posH_ / 3) {
118              this.barOff_ = y - this.posY_ - this.posH_ / 3 / 2;
119              this.modifyBarOff(0, 0);
120            }
121          }
122          else {
123            if (x - this.posX_ < this.barOff_ || x - this.posX_ > this.barOff_ + this.posW_ / 3) {
124              this.barOff_ = x - this.posX_ - this.posW_ / 3 / 2;
125              this.modifyBarOff(0, 0);
126            }
127          }
128          this.touchPos_ = {
129            x: x,
130            y: y,
131          };
132        }
133        break;
134      case 2:
135        if (this.touchDown_) {
136          this.modifyBarOff(x - this.touchPos_.x, y - this.touchPos_.y);
137          this.touchPos_.x = x;
138          this.touchPos_.y = y;
139        }
140        break;
141      case 3:
142        this.touchDown_ = false;
143        break;
144    }
145    return isIn;
146  }
147}
148
149module.exports = {
150  XScroll
151};