main.nim 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import nodesnim
  2. import strutils
  3. Window("Calc")
  4. var
  5. main = Scene("Main")
  6. first: string = ""
  7. second: string = ""
  8. sign: string = ""
  9. vbox = VBox()
  10. result = Label("Result")
  11. buttons = GridBox("Buttons")
  12. button_7 = Button("Button 7")
  13. button_8 = Button("Button 8")
  14. button_9 = Button("Button 9")
  15. button_4 = Button("Button 4")
  16. button_5 = Button("Button 5")
  17. button_6 = Button("Button 6")
  18. button_1 = Button("Button 1")
  19. button_2 = Button("Button 2")
  20. button_3 = Button("Button 3")
  21. button_0 = Button("Button 0")
  22. button_00 = Button("Button 00")
  23. button_add = Button("Button +")
  24. button_sub = Button("Button -")
  25. button_mul = Button("Button *")
  26. button_div = Button("Button /")
  27. button_eq = Button("Button =")
  28. main.addChild(vbox)
  29. vbox.addChild(result)
  30. vbox.addChild(buttons)
  31. vbox.setChildAnchor(0.5, 0.5, 0.5, 0.5)
  32. vbox.setSizeAnchor(1, 1)
  33. buttons.setRow(4)
  34. buttons.addChild(button_7)
  35. button_7.text = "7"
  36. button_7.on_click =
  37. proc(x, y: float) =
  38. if sign == "":
  39. first &= "7"
  40. else:
  41. second &= "7"
  42. buttons.addChild(button_8)
  43. button_8.text = "8"
  44. button_8.on_click =
  45. proc(x, y: float) =
  46. if sign == "":
  47. first &= "8"
  48. else:
  49. second &= "8"
  50. buttons.addChild(button_9)
  51. button_9.text = "9"
  52. button_9.on_click =
  53. proc(x, y: float) =
  54. if sign == "":
  55. first &= "9"
  56. else:
  57. second &= "9"
  58. buttons.addChild(button_add)
  59. button_add.text = "+"
  60. button_add.on_click =
  61. proc(x, y: float) =
  62. sign = "+"
  63. buttons.addChild(button_4)
  64. button_4.text = "4"
  65. button_4.on_click =
  66. proc(x, y: float) =
  67. if sign == "":
  68. first &= "4"
  69. else:
  70. second &= "4"
  71. buttons.addChild(button_5)
  72. button_5.text = "5"
  73. button_5.on_click =
  74. proc(x, y: float) =
  75. if sign == "":
  76. first &= "5"
  77. else:
  78. second &= "5"
  79. buttons.addChild(button_6)
  80. button_6.text = "6"
  81. button_6.on_click =
  82. proc(x, y: float) =
  83. if sign == "":
  84. first &= "6"
  85. else:
  86. second &= "6"
  87. buttons.addChild(button_sub)
  88. button_sub.text = "-"
  89. button_sub.on_click =
  90. proc(x, y: float) =
  91. sign = "-"
  92. buttons.addChild(button_1)
  93. button_1.text = "1"
  94. button_1.on_click =
  95. proc(x, y: float) =
  96. if sign == "":
  97. first &= "1"
  98. else:
  99. second &= "1"
  100. buttons.addChild(button_2)
  101. button_2.text = "2"
  102. button_2.on_click =
  103. proc(x, y: float) =
  104. if sign == "":
  105. first &= "2"
  106. else:
  107. second &= "2"
  108. buttons.addChild(button_3)
  109. button_3.text = "3"
  110. button_3.on_click =
  111. proc(x, y: float) =
  112. if sign == "":
  113. first &= "3"
  114. else:
  115. second &= "3"
  116. buttons.addChild(button_mul)
  117. button_mul.text = "*"
  118. button_mul.on_click =
  119. proc(x, y: float) =
  120. sign = "*"
  121. buttons.addChild(button_0)
  122. button_0.text = "0"
  123. button_0.on_click =
  124. proc(x, y: float) =
  125. if sign == "" and first != "":
  126. first &= "0"
  127. elif sign != "/":
  128. second &= "0"
  129. buttons.addChild(button_00)
  130. button_00.text = "00"
  131. button_00.on_click =
  132. proc(x, y: float) =
  133. if sign == "" and first != "":
  134. first &= "00"
  135. elif second != "":
  136. second &= "00"
  137. buttons.addChild(button_div)
  138. button_div.text = "/"
  139. button_div.on_click =
  140. proc(x, y: float) =
  141. sign = "/"
  142. buttons.addChild(button_eq)
  143. button_eq.text = "="
  144. button_eq.on_click =
  145. proc(x, y: float) =
  146. first =
  147. if sign == "+":
  148. $(parseFloat(first) + parseFloat(second))
  149. elif sign == "-":
  150. $(parseFloat(first) - parseFloat(second))
  151. elif sign == "*":
  152. $(parseFloat(first) * parseFloat(second))
  153. elif sign == "/":
  154. $(parseFloat(first) / parseFloat(second))
  155. else:
  156. first
  157. if sign != "":
  158. second = ""
  159. sign = ""
  160. result.setTextAlign(1, 0, 1, 0)
  161. result.resize(160, 32)
  162. result.process =
  163. proc() =
  164. if sign == "":
  165. result.text = first
  166. elif second == "":
  167. result.text = first & " " & sign
  168. else:
  169. result.text = first & " " & sign & " " & second
  170. addScene(main)
  171. setMainScene("Main")
  172. windowLaunch()