test3.nim 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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!\n VK!")
  85. call move(0, 40)
  86. label.text.setColor(6, 12, Color("#664fff"))
  87. label.text.setURL(18, 19, "https://vk.com")
  88. label.text.setUnderline(0, 2, true)
  89. label.text.setStrikethrough(1, 3, true)
  90. label.text.setItalic(0, true)
  91. getSceneByName("main").addChild(label)
  92. test "Button test":
  93. build:
  94. - Button btn:
  95. call setText("Press me ^^")
  96. call resize(196, 32)
  97. call move(420, 0)
  98. btn@onTouch(self, x, y):
  99. echo "clicked btn!"
  100. getSceneByName("main").addChild(btn)
  101. test "Box test":
  102. build:
  103. - Box box:
  104. call setChildAnchor(0.5, 0.5, 0.5, 0.5)
  105. call setPadding(2, 4, 8, 16)
  106. call move(420, 30)
  107. call setBackgroundColor(Color(1f, 1f, 1f))
  108. - ColorRect first:
  109. color: Color(0xff6699ff'u32)
  110. call resize(80, 80)
  111. - ColorRect second:
  112. color: Color(0xff64ffff'u32)
  113. call resize(60, 60)
  114. - ColorRect third:
  115. color: Color(0xffaa00ff'u32)
  116. getSceneByName("main").addChild(box)
  117. test "HBox test":
  118. build:
  119. - HBox hbox:
  120. call setChildAnchor(1, 1, 1, 1)
  121. call setPadding(2, 4, 8, 16)
  122. call move(520, 30)
  123. call setBackgroundColor(Color(1f, 1f, 1f))
  124. - ColorRect first:
  125. color: Color(0xff6699ff'u32)
  126. call resize(80, 80)
  127. - ColorRect second:
  128. color: Color(0xff64ffff'u32)
  129. call resize(60, 60)
  130. - ColorRect third:
  131. color: Color(0xffaa00ff'u32)
  132. getSceneByName("main").addChild(hbox)
  133. test "VBox test":
  134. build:
  135. - VBox vbox:
  136. call setChildAnchor(1, 1, 1, 1)
  137. call setPadding(2, 4, 8, 16)
  138. call move(420, 144)
  139. call setBackgroundColor(Color(1f, 1f, 1f))
  140. - ColorRect first:
  141. color: Color(0xff6699ff'u32)
  142. call resize(80, 80)
  143. - ColorRect second:
  144. color: Color(0xff64ffff'u32)
  145. call resize(60, 60)
  146. - ColorRect third:
  147. color: Color(0xffaa00ff'u32)
  148. getSceneByName("main").addChild(vbox)
  149. test "GridBox test":
  150. build:
  151. - GridBox grid:
  152. call setPadding(2, 4, 8, 16)
  153. call move(530, 144)
  154. call setRow(3)
  155. call setBackgroundColor(Color(1f, 1f, 1f))
  156. - ColorRect first(color: Color(0xff6699ff'u32))
  157. - ColorRect second(color: Color(0xff64ffff'u32))
  158. - ColorRect third(color: Color(0xffaa00ff'u32))
  159. - ColorRect fourth(color: Color(0xffcc33ff'u32))
  160. - ColorRect fifth(color: Color(0xffcc66ff'u32))
  161. - ColorRect sixth(color: Color(0xff6655ff'u32))
  162. getSceneByName("main").addChild(grid)
  163. test "EditText test":
  164. build:
  165. - EditText edit:
  166. call move(0, 150)
  167. getSceneByName("main").addChild(edit)
  168. test "Scroll test":
  169. # TODO: Fix it with glFramebuffer
  170. build:
  171. - Scroll scroll:
  172. call setAnchor(1, 0, 1, 0)
  173. - GridBox grid:
  174. call setPadding(2, 4, 8, 16)
  175. call setRow(3)
  176. call setBackgroundColor(Color(1f, 1f, 1f))
  177. - ColorRect first(color: Color(0xff6699ff'u32), rect_size: Vector2(100, 200))
  178. - ColorRect second(color: Color(0xff64ffff'u32))
  179. - ColorRect third(color: Color(0xffaa00ff'u32))
  180. - ColorRect fourth(color: Color(0xffcc33ff'u32))
  181. - ColorRect fifth(color: Color(0xffcc66ff'u32))
  182. - ColorRect sixth(color: Color(0xff6655ff'u32))
  183. getSceneByName("main").addChild(scroll)
  184. test "ProgressBar test":
  185. build:
  186. - ProgressBar bar1:
  187. progress_type: PROGRESS_BAR_HORIZONTAL
  188. indeterminate: true
  189. call setProgress(50)
  190. call move(120, 110)
  191. call resize(100, 20)
  192. - ProgressBar bar2:
  193. progress_type: PROGRESS_BAR_VERTICAL
  194. indeterminate: true
  195. call setProgress(50)
  196. call move(220, 100)
  197. call resize(20, 110)
  198. - ProgressBar bar3:
  199. progress_type: PROGRESS_BAR_CIRCLE
  200. indeterminate: true
  201. call setProgress(50)
  202. call move(320, 110)
  203. call resize(100, 100)
  204. getSceneByName("main").addChildren(bar1, bar2, bar3)
  205. test "Popup test":
  206. build:
  207. - Popup popup:
  208. call setAnchor(0, 1, 0, 1)
  209. - Label text:
  210. call setText("popup!")
  211. getSceneByName("main").getNode("control")@onProcess(self):
  212. if isActionJustPressed("enter"):
  213. popup.toggle()
  214. getSceneByName("main").addChild(popup)
  215. test "TextureButton test":
  216. build:
  217. - TextureButton button:
  218. call setNormalTexture(load("assets/button_normal.png", GL_RGBA))
  219. call setHoverTexture(load("assets/button_hover.png", GL_RGBA))
  220. call setPressTexture(load("assets/button_press.png", GL_RGBA))
  221. call resize(256, 64)
  222. call move(120, 220)
  223. call setText("Texture button")
  224. getSceneByName("main").addChild(button)
  225. test "TextureProgressBar test":
  226. build:
  227. - TextureProgressBar progress:
  228. call setProgressTexture(load("assets/texture_progress_1.png", GL_RGBA))
  229. call setBackgroundTexture(load("assets/texture_progress_0.png", GL_RGBA))
  230. call setProgress(50)
  231. call resize(256, 85)
  232. call move(100, 300)
  233. getSceneByName("main").addChild(progress)
  234. test "Counter test":
  235. build:
  236. - Counter counter:
  237. call move(360, 360)
  238. call setMaxValue(100)
  239. getSceneByName("main").addChild(counter)
  240. test "Switch test":
  241. build:
  242. - Switch switch:
  243. call move(360, 400)
  244. getSceneByName("main").addChild(switch)
  245. test "CheckBox test":
  246. build:
  247. - CheckBox check:
  248. call setText("smth checkbox")
  249. call enable()
  250. call move(700, 300)
  251. getSceneByName("main").addChild(check)
  252. test "SubWindow test":
  253. build:
  254. - SubWindow window1:
  255. call setIcon("assets/anim/0.jpg")
  256. call setTitle("subwindow")
  257. call move(500, 400)
  258. call open()
  259. getSceneByName("main").addChild(window1)
  260. test "Slider test":
  261. build:
  262. - Slider slider1:
  263. slider_type: SLIDER_HORIZONTAL
  264. call resize(100, 10)
  265. call move(600, 300)
  266. - Slider slider2:
  267. slider_type: SLIDER_VERTICAL
  268. call resize(10, 100)
  269. call move(600, 310)
  270. getSceneByName("main").addChildren(slider1, slider2)
  271. test "ToolTip test":
  272. build:
  273. - ToolTip tooltip:
  274. call showAtMouse()
  275. @onProcess():
  276. tooltip.showAtMouse()
  277. getSceneByName("main").addChild(tooltip)
  278. test "Launch window":
  279. windowLaunch()