index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. (() => {
  2. class Calculator {
  3. buttons = [];
  4. screen = null;
  5. screenData = "";
  6. currentSet = [];
  7. currentSetId = -1;
  8. buttonSets = [
  9. ["7", "8", "9", "4", "5", "6", "1", "2", "3", "0", "."],
  10. ["+", "-", "*", "/", "√", "log", "sin", "cos", "", "(", ")"],
  11. ];
  12. operations = {
  13. "+": " + ",
  14. "-": " - ",
  15. "*": " * ",
  16. "/": " / ",
  17. "√": "sqrt(",
  18. log: "log(",
  19. sin: "sin(",
  20. cos: "cos(",
  21. };
  22. start() {
  23. // Init screen
  24. this.screen = hmUI.createWidget(hmUI.widget.TEXT, {
  25. x: 8,
  26. y: 72,
  27. w: 192 - 16,
  28. h: 80,
  29. text: "",
  30. color: 0xffffff,
  31. text_size: 24,
  32. text_style: hmUI.text_style.WRAP,
  33. align_v: hmUI.align.CENTER_V,
  34. });
  35. this.screen.addEventListener(hmUI.event.CLICK_UP, () => {
  36. this.backspace();
  37. });
  38. // Init buttons
  39. for (let i = 0; i < 12; i++) {
  40. this.buttons[i] = hmUI.createWidget(hmUI.widget.BUTTON, {
  41. x: (i % 3) * 64 + 4,
  42. y: Math.floor(i / 3) * 64 + 160,
  43. w: 56,
  44. h: 56,
  45. radius: 8,
  46. text_size: 24,
  47. text: "-",
  48. normal_color: 0x222222,
  49. press_color: 0x333333,
  50. click_func: this.initButtonEvents(i),
  51. });
  52. }
  53. this.buttons[11].setProperty(hmUI.prop.MORE, {
  54. normal_color: 0x444444,
  55. press_color: 0x666666,
  56. text: "...",
  57. });
  58. this.switchSet();
  59. // Init remove and process buttons
  60. hmUI.createWidget(hmUI.widget.BUTTON, {
  61. x: 0,
  62. y: 420,
  63. w: 192,
  64. h: 70,
  65. text: "=",
  66. normal_color: 0x222222,
  67. press_color: 0x444444,
  68. text_size: 32,
  69. click_func: () => {
  70. this.process();
  71. },
  72. });
  73. hmUI.createWidget(hmUI.widget.BUTTON, {
  74. x: 0,
  75. y: 0,
  76. w: 192,
  77. h: 70,
  78. text: "Clear",
  79. text_size: 20,
  80. color: 0x999999,
  81. click_func: () => {
  82. this.clear();
  83. },
  84. });
  85. }
  86. process() {
  87. const brDiff =
  88. this._countIncl(this.screenData, "(") -
  89. this._countIncl(this.screenData, ")");
  90. if (brDiff === 0) {
  91. const sqrt = (v) => Math.sqrt(v);
  92. const log = (v) => Math.log(v);
  93. const sin = (v) => Math.sin(v);
  94. const cos = (v) => Math.cos(v);
  95. try {
  96. let result = eval(this.screenData);
  97. result = Math.round(result * 1e6) / 1e6;
  98. this.screenData = result.toString();
  99. } catch (e) {
  100. console.warn(e);
  101. this.screenData = "E";
  102. }
  103. } else {
  104. for (let i = 0; i < brDiff; i++) this.screenData += ")";
  105. }
  106. this.updateScreen();
  107. }
  108. _countIncl(str, chr) {
  109. let c = 0;
  110. for (let i in str) if (str[i] === chr) c++;
  111. return c;
  112. }
  113. switchSet() {
  114. const newId = (this.currentSetId + 1) % this.buttonSets.length;
  115. const newSet = this.buttonSets[newId];
  116. for (let i = 0; i < newSet.length; i++) {
  117. this.buttons[i].setProperty(hmUI.prop.TEXT, newSet[i]);
  118. }
  119. this.currentSetId = newId;
  120. this.currentSet = newSet;
  121. }
  122. initButtonEvents(i) {
  123. return () => {
  124. if (i == 11) return this.switchSet();
  125. let val = this.currentSet[i];
  126. if (this.operations[val]) val = this.operations[val];
  127. this.appendScreen(val);
  128. if (this.currentSetId != 0) this.switchSet();
  129. };
  130. }
  131. appendScreen(val) {
  132. this.screenData += val;
  133. this.updateScreen();
  134. }
  135. updateScreen() {
  136. this.screen.setProperty(hmUI.prop.MORE, {
  137. text: this.screenData,
  138. });
  139. }
  140. clear() {
  141. this.screenData = "";
  142. this.updateScreen();
  143. }
  144. backspace() {
  145. this.screenData = this.screenData
  146. .substring(0, this.screenData.length - 1)
  147. .trim();
  148. this.updateScreen();
  149. }
  150. }
  151. let __$$app$$__ = __$$hmAppManager$$__.currentApp;
  152. let __$$module$$__ = __$$app$$__.current;
  153. __$$module$$__.module = DeviceRuntimeCore.Page({
  154. onInit() {
  155. hmUI.setLayerScrolling(false);
  156. new Calculator().start();
  157. },
  158. });
  159. })();