main.nim 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.addChilds(
  35. button_7, button_8, button_9, button_add,
  36. button_4, button_5, button_6, button_sub,
  37. button_1, button_2, button_3, button_mul,
  38. button_0, button_00, button_div, button_eq)
  39. proc number(self: ButtonRef, x, y: float) =
  40. if sign == "":
  41. first &= self.text
  42. else:
  43. second &= self.text
  44. proc on_sign(self: ButtonRef, x, y: float) =
  45. if first != "":
  46. sign = self.text
  47. button_1.text = "1"
  48. button_2.text = "2"
  49. button_3.text = "3"
  50. button_4.text = "4"
  51. button_5.text = "5"
  52. button_6.text = "6"
  53. button_7.text = "7"
  54. button_8.text = "8"
  55. button_9.text = "9"
  56. button_0.text = "0"
  57. button_00.text = "00"
  58. button_sub.text = "-"
  59. button_add.text = "+"
  60. button_mul.text = "*"
  61. button_div.text = "/"
  62. button_1.on_touch = number
  63. button_2.on_touch = number
  64. button_3.on_touch = number
  65. button_4.on_touch = number
  66. button_5.on_touch = number
  67. button_6.on_touch = number
  68. button_7.on_touch = number
  69. button_8.on_touch = number
  70. button_9.on_touch = number
  71. button_add.on_touch = on_sign
  72. button_sub.on_touch = on_sign
  73. button_mul.on_touch = on_sign
  74. button_div.on_touch = on_sign
  75. button_0@on_touch(self, x, y):
  76. if sign == "" and first != "":
  77. first &= "0"
  78. elif sign != "/":
  79. second &= "0"
  80. button_00@on_touch(self, x, y):
  81. if sign == "" and first != "":
  82. first &= "00"
  83. elif second != "":
  84. second &= "00"
  85. button_eq.text = "="
  86. button_eq@on_touch(self, x, y):
  87. first =
  88. if sign == "+":
  89. $(parseFloat(first) + parseFloat(second))
  90. elif sign == "-":
  91. $(parseFloat(first) - parseFloat(second))
  92. elif sign == "*":
  93. $(parseFloat(first) * parseFloat(second))
  94. elif sign == "/":
  95. $(parseFloat(first) / parseFloat(second))
  96. else:
  97. first
  98. if sign != "":
  99. second = ""
  100. sign = ""
  101. result.setTextAlign(1, 0, 1, 0)
  102. result.resize(160, 32)
  103. result@on_process(self):
  104. if sign == "":
  105. result.text = first
  106. elif second == "":
  107. result.text = first & " " & sign
  108. else:
  109. result.text = first & " " & sign & " " & second
  110. addScene(main)
  111. setMainScene("Main")
  112. windowLaunch()