main.nim 3.0 KB

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