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