ScreenBoard.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. const CAPS_SYM = "CAPS";
  2. const SB_DATASETS = {
  3. "123": [
  4. "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"
  5. ],
  6. "EN": [
  7. [".", ",", "-", "?", "!", ":", ";", "(", ")", "1"],
  8. ["a", "b", "c", "2"],
  9. ["d", "e", "f", "3"],
  10. ["g", "h", "i", "4"],
  11. ["j", "k", "l", "5"],
  12. ["m", "n", "o", "6"],
  13. ["p", "q", "r", "s", "7"],
  14. ["t", "u", "v", "8"],
  15. ["w", "x", "y", "z", "9"],
  16. [" ", "0"],
  17. "⇪"
  18. ],
  19. "RU": [
  20. [".", ",", "?", "!", ":", ";", "(", ")", "1"],
  21. ["а", "б", "в", "г", "2"],
  22. ["д", "е", "ё", "ж", "з", "3"],
  23. ["и", "й", "к", "л", "4"],
  24. ["м", "н", "о", "п", "5"],
  25. ["р", "с", "т", "у", "6"],
  26. ["ф", "х", "ц", "ч", "7"],
  27. ["ш", "щ", "ъ", "ы", "8"],
  28. ["ь", "э", "ю", "я", "9"],
  29. [" ", "0"],
  30. "⇪"
  31. ]
  32. }
  33. export class ScreenBoard {
  34. isSubpage = false;
  35. currentContents = [];
  36. _displayValue = "";
  37. _modeList = [];
  38. _currentMode = "";
  39. _capsState = false;
  40. constructor(mode) {
  41. this._build();
  42. if(typeof mode == "string") {
  43. this.setMode(mode);
  44. } else {
  45. this._modeList = mode;
  46. this.setMode(mode[0]);
  47. }
  48. }
  49. get value() {
  50. return this._displayValue;
  51. }
  52. set value(v) {
  53. this._valueScreen.setProperty(hmUI.prop.TEXT, this.displayFormat(v));
  54. this._displayValue = v;
  55. }
  56. set title(v) {
  57. this._titleBox.setProperty(hmUI.prop.TEXT, v);
  58. }
  59. set confirmButtonText(v) {
  60. this._confirmBtn.setProperty(hmUI.prop.TEXT, v);
  61. }
  62. get visible() {
  63. return this.group.getProperty(hmUI.prop.VISIBLE);
  64. }
  65. set visible(v) {
  66. this.group.setProperty(hmUI.prop.VISIBLE, v);
  67. }
  68. onConfirm() {
  69. console.log("override me");
  70. }
  71. displayFormat(v) {
  72. return v;
  73. }
  74. setMode(mode) {
  75. const data = SB_DATASETS[mode];
  76. this.rootData = data;
  77. this.isSubpage = false;
  78. this._currentMode = mode;
  79. this._loadButtons(data);
  80. if(this._modeList.length > 0) {
  81. this._buttons[10].setProperty(hmUI.prop.TEXT, mode);
  82. }
  83. }
  84. _build() {
  85. this.group = hmUI.createWidget(hmUI.widget.GROUP, {
  86. x: 0,
  87. y: 0,
  88. w: 192,
  89. h: 490
  90. });
  91. this.group.createWidget(hmUI.widget.FILL_RECT, {
  92. x: 0,
  93. y: 0,
  94. w: 192,
  95. h: 490,
  96. color: 0x0
  97. });
  98. this._titleBox = this.group.createWidget(hmUI.widget.TEXT, {
  99. x: 24,
  100. y: 0,
  101. w: 144,
  102. h: 96,
  103. align_h: hmUI.align.CENTER_H,
  104. align_v: hmUI.align.CENTER_V,
  105. text_style: hmUI.text_style.WRAP,
  106. text: "Input",
  107. color: 0xAAAAAA
  108. })
  109. this.group.createWidget(hmUI.widget.IMG, {
  110. x: 160,
  111. y: 80,
  112. w: 32,
  113. h: 104,
  114. pos_x: 4,
  115. pos_y: 40,
  116. alpha: 120,
  117. src: "backspace.png"
  118. }).addEventListener(hmUI.event.CLICK_UP, () => this._backspace());
  119. this._valueScreen = this.group.createWidget(hmUI.widget.TEXT, {
  120. x: 0,
  121. y: 80,
  122. w: 160,
  123. h: 104,
  124. text: "",
  125. text_size: 22,
  126. color: 0xFFFFFF,
  127. align_v: hmUI.align.CENTER_V,
  128. text_style: hmUI.text_style.WRAP
  129. });
  130. this._confirmBtn = this.group.createWidget(hmUI.widget.BUTTON, {
  131. x: 0,
  132. y: 420,
  133. w: 192,
  134. h: 70,
  135. text: "Confirm",
  136. color: 0xFFFFFF,
  137. normal_color: 0x111111,
  138. press_color: 0x222222,
  139. click_func: () => this.onConfirm(this.value)
  140. });
  141. this._buttons = [];
  142. for(let i = 0; i < 12; i++) {
  143. this._mkButton(i);
  144. }
  145. }
  146. _mkButton(i) {
  147. this._buttons.push(this.group.createWidget(hmUI.widget.BUTTON, {
  148. x: 64 * (i % 3),
  149. y: 192 + 52 * Math.floor(i / 3),
  150. w: 64,
  151. h: 52,
  152. text: "",
  153. text_size: 22,
  154. normal_color: 0x050505,
  155. press_color: 0x111111,
  156. color: 0xCCCCCC,
  157. click_func: () => this._handleClick(i)
  158. }));
  159. }
  160. _loadButtons(data) {
  161. for(let i = 0; i < 11; i++) {
  162. let value = "", content = "";
  163. if(i < data.length) {
  164. content = data[i];
  165. if(typeof data[i] != "string") {
  166. for(let j = 0; j < 3; j++) {
  167. if(j >= data[i].length) break;
  168. value += data[i][j];
  169. }
  170. } else {
  171. value = data[i];
  172. }
  173. }
  174. value = value.replace(" ", "_");
  175. if(i === 10) i = 11;
  176. this._buttons[i].setProperty(hmUI.prop.TEXT, value);
  177. this.currentContents[i] = content;
  178. }
  179. }
  180. _handleClick(i) {
  181. let val = this.currentContents[i];
  182. // Tecn btns
  183. if(i == 10)
  184. return this._modeSwitch();
  185. if(val == CAPS_SYM)
  186. return this._switchCaps();
  187. if(typeof val != "string") {
  188. this.isSubpage = true;
  189. this._loadButtons(val);
  190. return;
  191. }
  192. if(this._capsState) val = val.toUpperCase();
  193. this.value += val;
  194. if(this.isSubpage) {
  195. this._loadButtons(this.rootData);
  196. this.isSubpage = false;
  197. }
  198. }
  199. _switchCaps() {
  200. this._capsState = !this._capsState;
  201. hmUI.showToast({
  202. text: this._capsState ? "ABC" : "abc"
  203. });
  204. }
  205. _modeSwitch() {
  206. if(this._modeList.length < 1) return;
  207. const cur = this._modeList.indexOf(this._currentMode);
  208. const next = (cur + 1) % this._modeList.length;
  209. this.setMode(this._modeList[next]);
  210. }
  211. _backspace() {
  212. this.value = this.value.substring(0, this.value.length-1);
  213. }
  214. }