test3.nim 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. # --- Test 3. Work with Control nodes. --- #
  2. import
  3. nodesnim,
  4. unittest
  5. suite "Work with Control nodes.":
  6. test "Setup window":
  7. Window("Control test", 1280, 640)
  8. test "Setup scenes":
  9. build:
  10. - Scene main
  11. - Scene second_scene
  12. addMainScene(main)
  13. addScene(second_scene)
  14. test "Register events":
  15. addButtonAction("click", BUTTON_LEFT)
  16. addKeyAction("space", K_SPACE)
  17. addKeyAction("enter", 13)
  18. test "Control test":
  19. build:
  20. - Control control
  21. getSceneByName("main").addChild(control)
  22. test "ColorRect test":
  23. build:
  24. - ColorRect rect:
  25. color: Color(1, 0.8, 0.95, 0.8)
  26. test "Handle Control events":
  27. rect@onFocus(self):
  28. echo "focused"
  29. rect@onUnfocus(self):
  30. echo "unfocused"
  31. rect@onClick(self, x, y):
  32. echo "clicked in (", x, ", ", y, ")"
  33. rect@onRelease(self, x, y):
  34. echo "released in (", x, ", ", y, ")"
  35. rect.color.r = 1f
  36. rect@onPress(self, x, y):
  37. rect.color.r -= 0.01
  38. rect@onMouseEnter(self, x, y):
  39. rect.color.g = 0.5f
  40. rect@onMouseExit(self, x, y):
  41. rect.color.g = 0.8f
  42. getSceneByName("main").addChild(rect)
  43. test "Anchor settings test":
  44. build:
  45. - ColorRect rect1:
  46. color: Color(0.2, 0.3, 0.4)
  47. call move(40, 0)
  48. call resize(80, 80)
  49. - ColorRect rect2:
  50. color: Color(0.4, 0.3, 0.2)
  51. call setSizeAnchor(0.25, 0.5)
  52. call setAnchor(1, 1, 1, 1) # anchor to bottom-right
  53. getSceneByName("main").addChild(rect1)
  54. test "Change scenes test": # Press Space to change scene
  55. getSceneByName("main")@onProcess(self):
  56. if isActionJustPressed("space"):
  57. changeScene("second_scene")
  58. getSceneByName("second_scene")@onProcess(self):
  59. if isActionJustPressed("space"):
  60. changeScene("main")
  61. test "TextureRect test":
  62. build:
  63. - TextureRect texturerect1:
  64. texture_mode: TEXTURE_KEEP_ASPECT_RATIO
  65. texture_anchor: Anchor(0.5, 0.5, 0.5, 0.5)
  66. call setTexture(load("assets/sharp.jpg"))
  67. call resize(100, 100)
  68. call move(120, 0)
  69. - TextureRect texturerect2:
  70. texture_mode: TEXTURE_CROP
  71. texture_anchor: Anchor(0.5, 0.5, 0.5, 0.5)
  72. call setTexture(load("assets/sharp.jpg"))
  73. call resize(100, 100)
  74. call move(220, 0)
  75. - TextureRect texturerect3:
  76. texture_mode: TEXTURE_FILL_XY
  77. call setTexture(load("assets/sharp.jpg"))
  78. call resize(100, 100)
  79. call move(320, 0)
  80. getSceneByName("main").addChildren(texturerect1, texturerect2, texturerect3)
  81. test "Label test":
  82. build:
  83. - Label label:
  84. call setText("hello,\nworld!")
  85. call move(0, 40)
  86. label.text.setColor(6, 12, Color("#664fff"))
  87. label.text.setUnderline(0, 2, true)
  88. label.text.setStrikethrough(1, 3, true)
  89. label.text.setItalic(0, true)
  90. getSceneByName("main").addChild(label)
  91. test "Button test":
  92. build:
  93. - Button btn:
  94. call setText("Press me ^^")
  95. call resize(196, 32)
  96. call move(420, 0)
  97. btn@onTouch(self, x, y):
  98. echo "clicked btn!"
  99. getSceneByName("main").addChild(btn)
  100. test "Box test":
  101. build:
  102. - Box box:
  103. call setChildAnchor(0.5, 0.5, 0.5, 0.5)
  104. call setPadding(2, 4, 8, 16)
  105. call move(420, 30)
  106. call setBackgroundColor(Color(1f, 1f, 1f))
  107. - ColorRect first:
  108. color: Color(0xff6699ff'u32)
  109. call resize(80, 80)
  110. - ColorRect second:
  111. color: Color(0xff64ffff'u32)
  112. call resize(60, 60)
  113. - ColorRect third:
  114. color: Color(0xffaa00ff'u32)
  115. getSceneByName("main").addChild(box)
  116. test "HBox test":
  117. build:
  118. - HBox hbox:
  119. call setChildAnchor(1, 1, 1, 1)
  120. call setPadding(2, 4, 8, 16)
  121. call move(520, 30)
  122. call setBackgroundColor(Color(1f, 1f, 1f))
  123. - ColorRect first:
  124. color: Color(0xff6699ff'u32)
  125. call resize(80, 80)
  126. - ColorRect second:
  127. color: Color(0xff64ffff'u32)
  128. call resize(60, 60)
  129. - ColorRect third:
  130. color: Color(0xffaa00ff'u32)
  131. getSceneByName("main").addChild(hbox)
  132. test "VBox test":
  133. build:
  134. - VBox vbox:
  135. call setChildAnchor(1, 1, 1, 1)
  136. call setPadding(2, 4, 8, 16)
  137. call move(420, 144)
  138. call setBackgroundColor(Color(1f, 1f, 1f))
  139. - ColorRect first:
  140. color: Color(0xff6699ff'u32)
  141. call resize(80, 80)
  142. - ColorRect second:
  143. color: Color(0xff64ffff'u32)
  144. call resize(60, 60)
  145. - ColorRect third:
  146. color: Color(0xffaa00ff'u32)
  147. getSceneByName("main").addChild(vbox)
  148. test "GridBox test":
  149. build:
  150. - GridBox grid:
  151. call setPadding(2, 4, 8, 16)
  152. call move(530, 144)
  153. call setRow(3)
  154. call setBackgroundColor(Color(1f, 1f, 1f))
  155. - ColorRect first(color: Color(0xff6699ff'u32))
  156. - ColorRect second(color: Color(0xff64ffff'u32))
  157. - ColorRect third(color: Color(0xffaa00ff'u32))
  158. - ColorRect fourth(color: Color(0xffcc33ff'u32))
  159. - ColorRect fifth(color: Color(0xffcc66ff'u32))
  160. - ColorRect sixth(color: Color(0xff6655ff'u32))
  161. getSceneByName("main").addChild(grid)
  162. test "EditText test":
  163. build:
  164. - EditText edit:
  165. call move(0, 100)
  166. getSceneByName("main").addChild(edit)
  167. test "Scroll test":
  168. # TODO: Fix it with glFramebuffer
  169. build:
  170. - Scroll scroll:
  171. call setAnchor(1, 0, 1, 0)
  172. - GridBox grid:
  173. call setPadding(2, 4, 8, 16)
  174. call setRow(3)
  175. call setBackgroundColor(Color(1f, 1f, 1f))
  176. - ColorRect first(color: Color(0xff6699ff'u32), rect_size: Vector2(100, 200))
  177. - ColorRect second(color: Color(0xff64ffff'u32))
  178. - ColorRect third(color: Color(0xffaa00ff'u32))
  179. - ColorRect fourth(color: Color(0xffcc33ff'u32))
  180. - ColorRect fifth(color: Color(0xffcc66ff'u32))
  181. - ColorRect sixth(color: Color(0xff6655ff'u32))
  182. getSceneByName("main").addChild(scroll)
  183. test "ProgressBar test":
  184. build:
  185. - ProgressBar bar1:
  186. progress_type: PROGRESS_BAR_HORIZONTAL
  187. indeterminate: true
  188. call setProgress(50)
  189. call move(120, 110)
  190. call resize(100, 20)
  191. - ProgressBar bar2:
  192. progress_type: PROGRESS_BAR_VERTICAL
  193. indeterminate: true
  194. call setProgress(50)
  195. call move(220, 100)
  196. call resize(20, 110)
  197. - ProgressBar bar3:
  198. progress_type: PROGRESS_BAR_CIRCLE
  199. indeterminate: true
  200. call setProgress(50)
  201. call move(320, 110)
  202. call resize(100, 100)
  203. getSceneByName("main").addChildren(bar1, bar2, bar3)
  204. test "Popup test":
  205. build:
  206. - Popup popup:
  207. call setAnchor(0, 1, 0, 1)
  208. - Label text:
  209. call setText("popup!")
  210. getSceneByName("main").getNode("control")@onProcess(self):
  211. if isActionJustPressed("enter"):
  212. popup.toggle()
  213. getSceneByName("main").addChild(popup)
  214. test "TextureButton test":
  215. build:
  216. - TextureButton button:
  217. call setNormalTexture(load("assets/button_normal.png", GL_RGBA))
  218. call setHoverTexture(load("assets/button_hover.png", GL_RGBA))
  219. call setPressTexture(load("assets/button_press.png", GL_RGBA))
  220. call resize(256, 64)
  221. call move(120, 220)
  222. call setText("Texture button")
  223. getSceneByName("main").addChild(button)
  224. test "TextureProgressBar test":
  225. build:
  226. - TextureProgressBar progress:
  227. call setProgressTexture(load("assets/texture_progress_1.png", GL_RGBA))
  228. call setBackgroundTexture(load("assets/texture_progress_0.png", GL_RGBA))
  229. call setProgress(50)
  230. call resize(256, 85)
  231. call move(100, 300)
  232. getSceneByName("main").addChild(progress)
  233. test "Counter test":
  234. build:
  235. - Counter counter:
  236. call move(360, 360)
  237. call setMaxValue(100)
  238. getSceneByName("main").addChild(counter)
  239. test "Switch test":
  240. build:
  241. - Switch switch:
  242. call move(360, 400)
  243. getSceneByName("main").addChild(switch)
  244. test "CheckBox test":
  245. build:
  246. - CheckBox check:
  247. call setText("smth checkbox")
  248. call enable()
  249. call move(700, 300)
  250. getSceneByName("main").addChild(check)
  251. test "SubWindow test":
  252. build:
  253. - SubWindow window1:
  254. call setIcon("assets/anim/0.jpg")
  255. call setTitle("subwindow")
  256. call move(500, 400)
  257. call open()
  258. getSceneByName("main").addChild(window1)
  259. test "Slider test":
  260. build:
  261. - Slider slider1:
  262. slider_type: SLIDER_HORIZONTAL
  263. call resize(100, 10)
  264. call move(600, 300)
  265. - Slider slider2:
  266. slider_type: SLIDER_VERTICAL
  267. call resize(10, 100)
  268. call move(600, 310)
  269. getSceneByName("main").addChildren(slider1, slider2)
  270. test "Launch window":
  271. windowLaunch()