14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ciconst { X2DFast } = require('../graphics/X2DFast');
174514f5e3Sopenharmony_ci
184514f5e3Sopenharmony_ciclass XScroll {
194514f5e3Sopenharmony_ci  constructor(options) {
204514f5e3Sopenharmony_ci    if (options['type']) {
214514f5e3Sopenharmony_ci      this.type_ = options['type'];
224514f5e3Sopenharmony_ci    }
234514f5e3Sopenharmony_ci    else {
244514f5e3Sopenharmony_ci      this.type_ = 'right';
254514f5e3Sopenharmony_ci    }
264514f5e3Sopenharmony_ci    this.barOff_ = 0;
274514f5e3Sopenharmony_ci    this.useScrH_ = false;
284514f5e3Sopenharmony_ci  }
294514f5e3Sopenharmony_ci  move(x, y, w, h) {
304514f5e3Sopenharmony_ci    this.posX_ = x;
314514f5e3Sopenharmony_ci    this.posY_ = y;
324514f5e3Sopenharmony_ci    this.posW_ = w;
334514f5e3Sopenharmony_ci    this.posH_ = h;
344514f5e3Sopenharmony_ci    return this;
354514f5e3Sopenharmony_ci  }
364514f5e3Sopenharmony_ci  draw() {
374514f5e3Sopenharmony_ci    X2DFast.gi().fillRect(this.posX_, this.posY_, this.posW_, this.posH_, 0x40808080);
384514f5e3Sopenharmony_ci    if (this.type_ === 'right') {
394514f5e3Sopenharmony_ci      X2DFast.gi().fillRect(this.posX_ + 1, this.posY_ + this.barOff_, this.posW_ - 2, this.posH_ / 3, 0x40000000);
404514f5e3Sopenharmony_ci    }
414514f5e3Sopenharmony_ci    else if (this.type_ === 'button') {
424514f5e3Sopenharmony_ci      X2DFast.gi().fillRect(this.posX_ + this.barOff_, this.posY_ + 1, this.posW_ / 3, this.posH_ - 2, 0x40000000);
434514f5e3Sopenharmony_ci    }
444514f5e3Sopenharmony_ci  }
454514f5e3Sopenharmony_ci  isTouchIn(x, y) {
464514f5e3Sopenharmony_ci    if (x < this.posX_) {
474514f5e3Sopenharmony_ci      return false;
484514f5e3Sopenharmony_ci    }
494514f5e3Sopenharmony_ci    if (y < this.posY_) {
504514f5e3Sopenharmony_ci      return false;
514514f5e3Sopenharmony_ci    }
524514f5e3Sopenharmony_ci    if (x > this.posX_ + this.posW_) {
534514f5e3Sopenharmony_ci      return false;
544514f5e3Sopenharmony_ci    }
554514f5e3Sopenharmony_ci    if (y > this.posY_ + this.posH_) {
564514f5e3Sopenharmony_ci      return false;
574514f5e3Sopenharmony_ci    }
584514f5e3Sopenharmony_ci    return true;
594514f5e3Sopenharmony_ci  }
604514f5e3Sopenharmony_ci  setBarOff(rate) {
614514f5e3Sopenharmony_ci    if (this.type_ === 'right') {
624514f5e3Sopenharmony_ci      this.barOff_ = this.posH_ * 2 / 3 * rate;
634514f5e3Sopenharmony_ci    }
644514f5e3Sopenharmony_ci    else {
654514f5e3Sopenharmony_ci      this.barOff_ = this.posW_ * 2 / 3 * rate;
664514f5e3Sopenharmony_ci    }
674514f5e3Sopenharmony_ci    this.modifyBarOff(0, 0);
684514f5e3Sopenharmony_ci  }
694514f5e3Sopenharmony_ci  getBarOff() {
704514f5e3Sopenharmony_ci    if (this.type_ === 'right') {
714514f5e3Sopenharmony_ci      return this.barOff_ / (this.posH_ * 2 / 3);
724514f5e3Sopenharmony_ci    }
734514f5e3Sopenharmony_ci    else {
744514f5e3Sopenharmony_ci      return this.barOff_ / (this.posW_ * 2 / 3);
754514f5e3Sopenharmony_ci    }
764514f5e3Sopenharmony_ci  }
774514f5e3Sopenharmony_ci  modifyBarOff(dx, dy) {
784514f5e3Sopenharmony_ci    if (this.type_ === 'right') {
794514f5e3Sopenharmony_ci      this.barOff_ += dy;
804514f5e3Sopenharmony_ci      if (this.barOff_ > this.posH_ * 2 / 3) {
814514f5e3Sopenharmony_ci        this.barOff_ = this.posH_ * 2 / 3;
824514f5e3Sopenharmony_ci      }
834514f5e3Sopenharmony_ci    }
844514f5e3Sopenharmony_ci    else {
854514f5e3Sopenharmony_ci      this.barOff_ += dx;
864514f5e3Sopenharmony_ci      if (this.barOff_ > this.posW_ * 2 / 3) {
874514f5e3Sopenharmony_ci        this.barOff_ = this.posW_ * 2 / 3;
884514f5e3Sopenharmony_ci      }
894514f5e3Sopenharmony_ci    }
904514f5e3Sopenharmony_ci    if (this.barOff_ < 0) {
914514f5e3Sopenharmony_ci      this.barOff_ = 0;
924514f5e3Sopenharmony_ci    }
934514f5e3Sopenharmony_ci  }
944514f5e3Sopenharmony_ci  onTouch(msg, x, y) {
954514f5e3Sopenharmony_ci    let isIn = this.isTouchIn(x, y);
964514f5e3Sopenharmony_ci    switch (msg) {
974514f5e3Sopenharmony_ci      case 10:
984514f5e3Sopenharmony_ci        if (this.type_ === 'right') {
994514f5e3Sopenharmony_ci          this.modifyBarOff(0, -this.posH_ / 3 / 10);
1004514f5e3Sopenharmony_ci        }
1014514f5e3Sopenharmony_ci        else if (isIn) {
1024514f5e3Sopenharmony_ci          this.modifyBarOff(-this.posW_ / 3 / 10, 0);
1034514f5e3Sopenharmony_ci        }
1044514f5e3Sopenharmony_ci        break;
1054514f5e3Sopenharmony_ci      case 11:
1064514f5e3Sopenharmony_ci        if (this.type_ === 'right') {
1074514f5e3Sopenharmony_ci          this.modifyBarOff(0, this.posH_ / 3 / 10);
1084514f5e3Sopenharmony_ci        }
1094514f5e3Sopenharmony_ci        else if (isIn) {
1104514f5e3Sopenharmony_ci          this.modifyBarOff(this.posW_ / 3 / 10, 0);
1114514f5e3Sopenharmony_ci        }
1124514f5e3Sopenharmony_ci        break;
1134514f5e3Sopenharmony_ci      case 1:
1144514f5e3Sopenharmony_ci        if (isIn) {
1154514f5e3Sopenharmony_ci          this.touchDown_ = true;
1164514f5e3Sopenharmony_ci          if (this.type_ === 'right') {
1174514f5e3Sopenharmony_ci            if (y - this.posY_ < this.barOff_ || y - this.posY_ > this.barOff_ + this.posH_ / 3) {
1184514f5e3Sopenharmony_ci              this.barOff_ = y - this.posY_ - this.posH_ / 3 / 2;
1194514f5e3Sopenharmony_ci              this.modifyBarOff(0, 0);
1204514f5e3Sopenharmony_ci            }
1214514f5e3Sopenharmony_ci          }
1224514f5e3Sopenharmony_ci          else {
1234514f5e3Sopenharmony_ci            if (x - this.posX_ < this.barOff_ || x - this.posX_ > this.barOff_ + this.posW_ / 3) {
1244514f5e3Sopenharmony_ci              this.barOff_ = x - this.posX_ - this.posW_ / 3 / 2;
1254514f5e3Sopenharmony_ci              this.modifyBarOff(0, 0);
1264514f5e3Sopenharmony_ci            }
1274514f5e3Sopenharmony_ci          }
1284514f5e3Sopenharmony_ci          this.touchPos_ = {
1294514f5e3Sopenharmony_ci            x: x,
1304514f5e3Sopenharmony_ci            y: y,
1314514f5e3Sopenharmony_ci          };
1324514f5e3Sopenharmony_ci        }
1334514f5e3Sopenharmony_ci        break;
1344514f5e3Sopenharmony_ci      case 2:
1354514f5e3Sopenharmony_ci        if (this.touchDown_) {
1364514f5e3Sopenharmony_ci          this.modifyBarOff(x - this.touchPos_.x, y - this.touchPos_.y);
1374514f5e3Sopenharmony_ci          this.touchPos_.x = x;
1384514f5e3Sopenharmony_ci          this.touchPos_.y = y;
1394514f5e3Sopenharmony_ci        }
1404514f5e3Sopenharmony_ci        break;
1414514f5e3Sopenharmony_ci      case 3:
1424514f5e3Sopenharmony_ci        this.touchDown_ = false;
1434514f5e3Sopenharmony_ci        break;
1444514f5e3Sopenharmony_ci    }
1454514f5e3Sopenharmony_ci    return isIn;
1464514f5e3Sopenharmony_ci  }
1474514f5e3Sopenharmony_ci}
1484514f5e3Sopenharmony_ci
1494514f5e3Sopenharmony_cimodule.exports = {
1504514f5e3Sopenharmony_ci  XScroll
1514514f5e3Sopenharmony_ci};