SettingsListScreen.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import {TouchEventManager} from "../lib/TouchEventManager";
  2. export class SettingsListScreen {
  3. baseColor = 0xFFFFFF;
  4. STYLE_HEADLINE = {
  5. x: 4,
  6. w: 192-8,
  7. h: 32,
  8. color: 0xEEEEEE,
  9. align_v: hmUI.align.CENTER_V
  10. };
  11. STYLE_BG = {
  12. x: 0,
  13. y: 0,
  14. w: 192,
  15. color: 0x111111,
  16. radius: 8
  17. }
  18. start() {
  19. this.posY = 96;
  20. this.build();
  21. hmUI.createWidget(hmUI.widget.FILL_RECT, {
  22. x: 0,
  23. y: this.posY,
  24. w: 192,
  25. h: 96
  26. });
  27. }
  28. image(src, height) {
  29. hmUI.createWidget(hmUI.widget.IMG, {
  30. x: 4,
  31. y: this.posY,
  32. src
  33. });
  34. this.posY += height + 8;
  35. }
  36. h1(title) {
  37. hmUI.createWidget(hmUI.widget.TEXT, {
  38. ...this.STYLE_HEADLINE,
  39. y: this.posY,
  40. h: 48,
  41. text_size: 32,
  42. text: title
  43. });
  44. this.posY += 56;
  45. }
  46. text(text) {
  47. const metrics = hmUI.getTextLayout(text, {
  48. text_size: 20,
  49. text_width: 192-8,
  50. wrapped: true
  51. });
  52. hmUI.createWidget(hmUI.widget.TEXT, {
  53. x: 4,
  54. y: this.posY,
  55. w: 192-8,
  56. h: metrics.height,
  57. text,
  58. text_size: 20,
  59. text_style: hmUI.text_style.WRAP,
  60. color: 0xEEEEEE
  61. });
  62. this.posY += metrics.height + 8;
  63. }
  64. headline(title) {
  65. hmUI.createWidget(hmUI.widget.TEXT, {
  66. ...this.STYLE_HEADLINE,
  67. y: this.posY,
  68. text: title
  69. });
  70. this.posY += 32;
  71. }
  72. clickableItem(name, icon, click_func) {
  73. const [group, viewHeight] = this._mkBaseGroup(name);
  74. group.createWidget(hmUI.widget.IMG, {
  75. x: 10,
  76. y: 20,
  77. src: icon
  78. });
  79. this._addClickEv(group, viewHeight, click_func);
  80. }
  81. field(name, value, callback) {
  82. const group = hmUI.createWidget(hmUI.widget.GROUP, {
  83. x: 0,
  84. y: this.posY,
  85. w: 192,
  86. h: 64
  87. });
  88. group.createWidget(hmUI.widget.FILL_RECT, {
  89. ...this.STYLE_BG,
  90. h: 64
  91. });
  92. group.createWidget(hmUI.widget.TEXT, {
  93. x: 4,
  94. y: 4,
  95. w: 144,
  96. h: 24,
  97. align_v: hmUI.align.CENTER_V,
  98. text_style: hmUI.text_style.WRAP,
  99. text_size: 18,
  100. color: 0xAAAAAA,
  101. text: name
  102. });
  103. const viewValue = group.createWidget(hmUI.widget.TEXT, {
  104. x: 4,
  105. y: 28,
  106. w: 144,
  107. h: 32,
  108. align_v: hmUI.align.CENTER_V,
  109. text_style: hmUI.text_style.WRAP,
  110. text_size: 20,
  111. color: 0xFFFFFF,
  112. text: value
  113. });
  114. this._addClickEv(group, 64, callback);
  115. return (v) => viewHeight.setProperty(hmUI.prop.TEXT, v);
  116. }
  117. checkbox(name, storage, key) {
  118. let value = !!storage[key];
  119. const [group, viewHeight] = this._mkBaseGroup(name);
  120. const icon = group.createWidget(hmUI.widget.IMG, {
  121. x: 10,
  122. y: 20,
  123. src: "menu/cb_" + value + ".png"
  124. })
  125. this._addClickEv(group, viewHeight, () => {
  126. storage[key] = !storage[key];
  127. icon.setProperty(hmUI.prop.MORE, {
  128. src: "menu/cb_" + (!!storage[key]) + ".png"
  129. })
  130. });
  131. }
  132. propCheckbox(name, key, fallback) {
  133. let value = hmFS.SysProGetBool(key);
  134. if(value === undefined) value = fallback;
  135. const [group, viewHeight] = this._mkBaseGroup(name);
  136. const icon = group.createWidget(hmUI.widget.IMG, {
  137. x: 10,
  138. y: 20,
  139. src: "menu/cb_" + value + ".png"
  140. })
  141. this._addClickEv(group, viewHeight, () => {
  142. value = !value;
  143. hmFS.SysProSetBool(key, value);
  144. icon.setProperty(hmUI.prop.MORE, {
  145. src: "menu/cb_" + value + ".png"
  146. })
  147. });
  148. }
  149. _mkBaseGroup(name) {
  150. const textHeight = hmUI.getTextLayout(name, {text_size: 18, text_width: 144}).height;
  151. const viewHeight = Math.max(64, textHeight + 36);
  152. const group = hmUI.createWidget(hmUI.widget.GROUP, {
  153. x: 0,
  154. y: this.posY,
  155. w: 192,
  156. h: viewHeight
  157. });
  158. group.createWidget(hmUI.widget.FILL_RECT, {
  159. ...this.STYLE_BG,
  160. h: viewHeight
  161. });
  162. group.createWidget(hmUI.widget.TEXT, {
  163. x: 44,
  164. y: 0,
  165. w: 144,
  166. h: viewHeight,
  167. align_v: hmUI.align.CENTER_V,
  168. text_style: hmUI.text_style.WRAP,
  169. text_size: 18,
  170. color: this.baseColor,
  171. text: name
  172. });
  173. return [group, viewHeight]
  174. }
  175. _addClickEv(group, viewHeight, click_func) {
  176. const ch = group.createWidget(hmUI.widget.IMG, {
  177. x: 0,
  178. y: 0,
  179. w: 184,
  180. h: viewHeight,
  181. src: ""
  182. });
  183. const evm = new TouchEventManager(ch);
  184. evm.ontouch = click_func;
  185. this.posY += viewHeight + 8;
  186. }
  187. }