14514f5e3Sopenharmony_ci/* 24514f5e3Sopenharmony_ci * Copyright (c) 2023 Huawei Device 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 XButton { 194514f5e3Sopenharmony_ci constructor(x, y, w, h, name) { 204514f5e3Sopenharmony_ci this.pm2f_ = X2DFast.gi(); 214514f5e3Sopenharmony_ci this.move(x, y, w, h); 224514f5e3Sopenharmony_ci this.name_ = name; 234514f5e3Sopenharmony_ci this.touchDown_ = false; 244514f5e3Sopenharmony_ci this.clicked_ = false; 254514f5e3Sopenharmony_ci this.rightClicked_ = false; 264514f5e3Sopenharmony_ci this.disable_ = false; 274514f5e3Sopenharmony_ci this.nameColor_ = 0xffffffff; 284514f5e3Sopenharmony_ci this.backgroundColor_ = 0xff487eb8; 294514f5e3Sopenharmony_ci } 304514f5e3Sopenharmony_ci 314514f5e3Sopenharmony_ci move(x, y, w, h) { 324514f5e3Sopenharmony_ci this.posX_ = x; 334514f5e3Sopenharmony_ci this.posY_ = y; 344514f5e3Sopenharmony_ci this.posW_ = w; 354514f5e3Sopenharmony_ci this.posH_ = h; 364514f5e3Sopenharmony_ci return this; 374514f5e3Sopenharmony_ci } 384514f5e3Sopenharmony_ci 394514f5e3Sopenharmony_ci draw() { 404514f5e3Sopenharmony_ci let coloroff = 0; 414514f5e3Sopenharmony_ci if (this.touchDown_) coloroff = 0x00202020; 424514f5e3Sopenharmony_ci this.pm2f_.fillRect( 434514f5e3Sopenharmony_ci this.posX_, 444514f5e3Sopenharmony_ci this.posY_, 454514f5e3Sopenharmony_ci this.posW_, 464514f5e3Sopenharmony_ci this.posH_, 474514f5e3Sopenharmony_ci this.backgroundColor_ - coloroff 484514f5e3Sopenharmony_ci ); 494514f5e3Sopenharmony_ci if (this.name_ != undefined && this.name_.length > 0) 504514f5e3Sopenharmony_ci this.pm2f_.drawText( 514514f5e3Sopenharmony_ci this.name_, 524514f5e3Sopenharmony_ci 14, 534514f5e3Sopenharmony_ci this.posX_ + this.posW_ / 2, 544514f5e3Sopenharmony_ci this.posY_ + this.posH_ / 2 + 2, 554514f5e3Sopenharmony_ci 1, 564514f5e3Sopenharmony_ci 1, 574514f5e3Sopenharmony_ci 0, 584514f5e3Sopenharmony_ci -2, 594514f5e3Sopenharmony_ci -2, 604514f5e3Sopenharmony_ci this.nameColor_ - coloroff 614514f5e3Sopenharmony_ci ); 624514f5e3Sopenharmony_ci } 634514f5e3Sopenharmony_ci 644514f5e3Sopenharmony_ci isTouchIn(x, y) { 654514f5e3Sopenharmony_ci if (x < this.posX_) return false; 664514f5e3Sopenharmony_ci if (y < this.posY_) return false; 674514f5e3Sopenharmony_ci if (x > this.posX_ + this.posW_) return false; 684514f5e3Sopenharmony_ci if (y > this.posY_ + this.posH_) return false; 694514f5e3Sopenharmony_ci return true; 704514f5e3Sopenharmony_ci } 714514f5e3Sopenharmony_ci onTouch(msg, x, y) { 724514f5e3Sopenharmony_ci let isIn = this.isTouchIn(x, y); 734514f5e3Sopenharmony_ci switch (msg) { 744514f5e3Sopenharmony_ci case 1: 754514f5e3Sopenharmony_ci if (isIn) { 764514f5e3Sopenharmony_ci this.touchDown_ = true; 774514f5e3Sopenharmony_ci } 784514f5e3Sopenharmony_ci break; 794514f5e3Sopenharmony_ci case 2: 804514f5e3Sopenharmony_ci break; 814514f5e3Sopenharmony_ci case 3: 824514f5e3Sopenharmony_ci if (this.touchDown_ && isIn) { 834514f5e3Sopenharmony_ci if (this.onClicked_) { 844514f5e3Sopenharmony_ci this.onClicked_(); 854514f5e3Sopenharmony_ci } 864514f5e3Sopenharmony_ci else { 874514f5e3Sopenharmony_ci this.clicked_ = true; 884514f5e3Sopenharmony_ci } 894514f5e3Sopenharmony_ci } 904514f5e3Sopenharmony_ci this.touchDown_ = false; 914514f5e3Sopenharmony_ci break; 924514f5e3Sopenharmony_ci case 4: 934514f5e3Sopenharmony_ci if (isIn) { 944514f5e3Sopenharmony_ci this.rightDown_ = true; 954514f5e3Sopenharmony_ci } 964514f5e3Sopenharmony_ci break; 974514f5e3Sopenharmony_ci case 6: 984514f5e3Sopenharmony_ci if (this.rightDown_ && isIn) { 994514f5e3Sopenharmony_ci this.rightClicked_ = true; 1004514f5e3Sopenharmony_ci } 1014514f5e3Sopenharmony_ci this.rightDown_ = false; 1024514f5e3Sopenharmony_ci break; 1034514f5e3Sopenharmony_ci } 1044514f5e3Sopenharmony_ci return isIn; 1054514f5e3Sopenharmony_ci } 1064514f5e3Sopenharmony_ci 1074514f5e3Sopenharmony_ci isClicked() { 1084514f5e3Sopenharmony_ci if (this.clicked_) { 1094514f5e3Sopenharmony_ci this.clicked_ = false; 1104514f5e3Sopenharmony_ci return true; 1114514f5e3Sopenharmony_ci } 1124514f5e3Sopenharmony_ci return false; 1134514f5e3Sopenharmony_ci } 1144514f5e3Sopenharmony_ci 1154514f5e3Sopenharmony_ci isRightClicked() { 1164514f5e3Sopenharmony_ci if (this.rightClicked_) { 1174514f5e3Sopenharmony_ci this.rightClicked_ = false; 1184514f5e3Sopenharmony_ci return true; 1194514f5e3Sopenharmony_ci } 1204514f5e3Sopenharmony_ci return false; 1214514f5e3Sopenharmony_ci } 1224514f5e3Sopenharmony_ci} 1234514f5e3Sopenharmony_ci 1244514f5e3Sopenharmony_cimodule.exports = { 1254514f5e3Sopenharmony_ci XButton, 1264514f5e3Sopenharmony_ci}; 127