1/* 2 * Copyright (c) 2023 Huawei Device 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 { Scr } = require('./XDefine'); 18const { XTools } = require('./XTools'); 19 20class RightMenu { 21 static backgroundImg_ = -1; 22 static backgroundCut_ = -1; 23 24 static popItemFocusImg_ = -1; 25 static popItemFocusCut_ = -1; 26 static MENU = null; 27 static PADDING = 16; 28 static FOCUS_ITEM_MARGIN = 4; 29 static FOCUS_ITEM_HEIGHT = 32; 30 static TEXT_SIZE = 14; 31 static isDarkBackground_ = true; 32 static Reset(detail, x, y) { 33 RightMenu.MENU = { 34 x: x == null ? XTools.MOUSE_POS.x : x, 35 y: y == null ? XTools.MOUSE_POS.y : y, 36 detail: detail, 37 needClose: false, 38 }; 39 } 40 static Append(detail) { 41 if (RightMenu.MENU) { 42 RightMenu.MENU.detail.push(...detail); 43 } 44 } 45 static Button(icon, name, hootkey, callback) { 46 return { 47 type: 0, 48 icon: icon, 49 name: name, 50 hk: hootkey, 51 cb: () => { 52 callback(this); 53 RightMenu.close(); 54 }, 55 }; 56 } 57 static Group(icon, name, objs) { 58 return { 59 type: 1, 60 icon: icon, 61 name: name, 62 open: false, 63 group: objs, 64 }; 65 } 66 static Gap() { 67 return { 68 type: 2, 69 }; 70 } 71 static close() { 72 if (RightMenu.MENU) { 73 RightMenu.MENU.needClose = true; 74 } 75 } 76 static Draw() { 77 if (RightMenu.MENU) { 78 RightMenu.DrawGroup( 79 RightMenu.MENU.detail, 80 RightMenu.MENU.x, 81 RightMenu.MENU.y 82 ); 83 if (RightMenu.MENU.needClose) { 84 RightMenu.MENU = null; 85 } 86 } 87 } 88 static MENUW = 155; 89 static DrawGroup(grp, x, y) { 90 let w = RightMenu.MENUW; 91 let l = 0; 92 for (let e of grp) { 93 if (e.type != 2) { 94 l += 1; 95 } 96 } 97 if (grp.length == 3) { 98 X2DFast.px2f.drawCut(this.backgroundCut_, x, y, 1, 0.88, 0, -1, -1); 99 } else if (grp.length == 1) { 100 X2DFast.px2f.drawCut(this.backgroundCut_, x, y, 1, 0.3, 0, -1, -1); 101 } else { 102 X2DFast.px2f.drawCut(this.backgroundCut_, x, y, 1, 1, 0, -1, -1); 103 } 104 for (let e of grp) { 105 e.rect = [x, y, w, 32]; 106 if (e.on) { 107 X2DFast.px2f.drawCut( 108 this.popItemFocusCut_, 109 x + RightMenu.FOCUS_ITEM_MARGIN, 110 y 111 ); 112 } 113 if (e.type == 2) { 114 e.rect = [x, y, w, 0]; 115 X2DFast.px2f.drawLine(x, y, x + w, y, 0xff808080, 2); 116 continue; 117 } 118 let OFFY_ = y + RightMenu.FOCUS_ITEM_HEIGHT / 2 - RightMenu.TEXT_SIZE / 2; 119 let textColor = this.isDarkBackground_ ? 0xffffffff : 0xff000000; 120 X2DFast.px2f.drawText( 121 e.name, 122 RightMenu.TEXT_SIZE, 123 x + RightMenu.PADDING, 124 OFFY_, 125 1, 126 1, 127 0, 128 0, 129 0, 130 textColor 131 ); 132 if (e.type == 0) { 133 if (e.hk) { 134 X2DFast.px2f.drawText( 135 e.hk, 136 RightMenu.TEXT_SIZE, 137 x + w, 138 OFFY_, 139 1, 140 1, 141 0, 142 -3, 143 -2, 144 0xff808080 145 ); 146 } 147 } else if (e.type == 1) { 148 if (e.open) { 149 X2DFast.px2f.drawText( 150 '<', 151 RightMenu.TEXT_SIZE, 152 x + w, 153 OFFY_, 154 1, 155 1, 156 0, 157 -3, 158 -2, 159 textColor 160 ); 161 RightMenu.DrawGroup(e.group, x + w, y); 162 } else { 163 X2DFast.px2f.drawText( 164 '>', 165 RightMenu.TEXT_SIZE, 166 x + w, 167 OFFY_, 168 1, 169 1, 170 0, 171 -3, 172 -2, 173 textColor 174 ); 175 } 176 } 177 y += 32; 178 } 179 } 180 static Touch(msg, x, y) { 181 if (RightMenu.MENU) { 182 if (RightMenu.TouchGroup(RightMenu.MENU.detail, msg, x, y)) { 183 return true; 184 } else if (msg != 2) { 185 RightMenu.MENU.needClose = true; 186 } 187 } 188 return false; 189 } 190 191 isClicked() { 192 if (this.clicked_) { 193 this.clicked_ = false; 194 return true; 195 } 196 return false; 197 } 198 199 static TouchGroup(grp, msg, x, y) { 200 for (let e of grp) { 201 e.on = false; 202 } 203 for (let e of grp) { 204 if (!e.rect) { 205 return false; 206 } 207 if (XTools.InRect(x, y, ...e.rect)) { 208 if (e.type == 1 && msg == 1) { 209 e.open = !e.open; 210 } 211 if (e.type == 2) { 212 } 213 if (e.type == 0) { 214 if (msg == 1) { 215 e.cb(); 216 } 217 } 218 e.on = true; 219 return true; 220 } 221 if (e.type == 1) { 222 if (e.open && RightMenu.TouchGroup(e.group, msg, x, y)) { 223 return true; 224 } 225 } 226 } 227 return false; 228 } 229} 230 231module.exports = { 232 RightMenu, 233}; 234