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'); 17const HEIGHT = 20; 18 19class XSelect { 20 constructor(list, default_) { 21 this.pm2f_ = X2DFast.gi(); 22 this.resetList(list, default_); 23 this.open_ = false; 24 this.nameColor_ = 0xffffffff; 25 this.backgroundColor_ = 0xff313131; 26 this.tmpSelect_ = -1; 27 this.selectCallback = null; 28 } 29 resetList(list, default_) { 30 this.list_ = list; 31 this.default_ = default_; 32 } 33 move(x, y, w, h) { 34 this.posX_ = x; 35 this.posY_ = y; 36 this.posW_ = w; 37 this.posH_ = h; 38 return this; 39 } 40 setColor(background, forground) { 41 this.nameColor_ = forground; 42 this.backgroundColor_ = background; 43 } 44 draw() { 45 let x = this.posX_; 46 let y = this.posY_; 47 let w = this.posW_; 48 let h = this.posH_; 49 let model = 3; 50 51 this.pm2f_.fillRect(x, y, w, h, this.backgroundColor_); 52 let name = '...'; 53 if (this.default_.indexOf('\\') !== -1) { 54 let list = this.default_.split('\\'); 55 if (list.length > model) { 56 for (let i = list.length - model; i < list.length; i++) { 57 name += list[i]; 58 if (i !== list.length - 1) { 59 name += '\\'; 60 } 61 } 62 } else { 63 name = this.default_; 64 } 65 } else { 66 name = this.default_; 67 } 68 this.pm2f_.drawText(name, 16, x, y, 1, 1, 0, -1, -1, this.nameColor_); 69 if (this.open_) { 70 this.pm2f_.fillRect(x, y + h, w, HEIGHT * this.list_.length, this.backgroundColor_); 71 for (let i in this.list_) { 72 if (i === this.tmpSelect_) { 73 this.pm2f_.fillRect(x, y + h + i * HEIGHT, w, HEIGHT, this.backgroundColor_); 74 } 75 if (this.list_[i] === this.default_) { 76 this.pm2f_.fillRect(x, y + h + i * HEIGHT, w, HEIGHT, this.backgroundColor_); 77 } 78 let name1 = '...'; 79 if (this.list_[i].indexOf('\\') !== -1) { 80 let list = this.list_[i].split('\\'); 81 if (list.length > model) { 82 for (let k = list.length - model; k < list.length; k++) { 83 name1 += list[k]; 84 if (k !== list.length - 1) { 85 name1 += '\\'; 86 } 87 } 88 } else { 89 name1 = this.list_[i]; 90 } 91 } else { 92 name1 = this.list_[i]; 93 } 94 this.pm2f_.drawText( 95 name1, 96 16, 97 x, 98 y + h + i * 20, 99 1, 100 1, 101 0, 102 -1, 103 -1, 104 this.nameColor_ 105 ); 106 } 107 } 108 } 109 isTouchIn(x, y) { 110 if (x < this.posX_) { 111 return false; 112 } 113 if (y < this.posY_) { 114 return false; 115 } 116 if (x > this.posX_ + this.posW_) { 117 return false; 118 } 119 if (y > this.posY_ + this.posH_ + (this.open_ ? HEIGHT * this.list_.length : 0)) { 120 return false; 121 } 122 return true; 123 } 124 procTouch(msg, x, y) { 125 let isIn = this.isTouchIn(x, y); 126 switch (msg) { 127 case 1: 128 if (!isIn) { 129 break; 130 } 131 if (!this.open_) { 132 this.open_ = true; 133 break; 134 } 135 if (this.tmpSelect_ >= 0 && this.tmpSelect_ <= this.list_.length) { 136 if (this.default_ !== this.list_[this.tmpSelect_]) { 137 this.default_ = this.list_[this.tmpSelect_]; 138 if (this.selectCallback !== null) { 139 this.selectCallback(this.default_); 140 } 141 } 142 } 143 this.open_ = false; 144 break; 145 case 2: 146 if (this.open_) { 147 this.tmpSelect_ = parseInt((y - this.posY_ - this.posH_) / 20); 148 } 149 break; 150 case 3: 151 break; 152 } 153 return isIn; 154 } 155 registCallback(func) { 156 this.selectCallback = func; 157 } 158} 159 160module.exports = { 161 XSelect, 162}; 163