test3.nim 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. echo btn.text.chars[0].color
  101. if current_theme.name == "default":
  102. changeTheme("light")
  103. else:
  104. changeTheme("default")
  105. getSceneByName("main").addChild(btn)
  106. test "Box test":
  107. build:
  108. - Box box:
  109. call setChildAnchor(0.5, 0.5, 0.5, 0.5)
  110. call setPadding(2, 4, 8, 16)
  111. call move(420, 30)
  112. call setBackgroundColor(Color(1f, 1f, 1f))
  113. - ColorRect first:
  114. color: Color(0xff6699ff'u32)
  115. call resize(80, 80)
  116. - ColorRect second:
  117. color: Color(0xff64ffff'u32)
  118. call resize(60, 60)
  119. - ColorRect third:
  120. color: Color(0xffaa00ff'u32)
  121. getSceneByName("main").addChild(box)
  122. test "HBox test":
  123. build:
  124. - HBox hbox:
  125. call setChildAnchor(1, 1, 1, 1)
  126. call setPadding(2, 4, 8, 16)
  127. call move(520, 30)
  128. call setBackgroundColor(Color(1f, 1f, 1f))
  129. - ColorRect first:
  130. color: Color(0xff6699ff'u32)
  131. call resize(80, 80)
  132. - ColorRect second:
  133. color: Color(0xff64ffff'u32)
  134. call resize(60, 60)
  135. - ColorRect third:
  136. color: Color(0xffaa00ff'u32)
  137. getSceneByName("main").addChild(hbox)
  138. test "VBox test":
  139. build:
  140. - VBox vbox:
  141. call setChildAnchor(1, 1, 1, 1)
  142. call setPadding(2, 4, 8, 16)
  143. call move(420, 144)
  144. call setBackgroundColor(Color(1f, 1f, 1f))
  145. - ColorRect first:
  146. color: Color(0xff6699ff'u32)
  147. call resize(80, 80)
  148. - ColorRect second:
  149. color: Color(0xff64ffff'u32)
  150. call resize(60, 60)
  151. - ColorRect third:
  152. color: Color(0xffaa00ff'u32)
  153. getSceneByName("main").addChild(vbox)
  154. test "GridBox test":
  155. build:
  156. - GridBox grid:
  157. call setPadding(2, 4, 8, 16)
  158. call move(530, 144)
  159. call setRow(3)
  160. call setBackgroundColor(Color(1f, 1f, 1f))
  161. - ColorRect first(color: Color(0xff6699ff'u32))
  162. - ColorRect second(color: Color(0xff64ffff'u32))
  163. - ColorRect third(color: Color(0xffaa00ff'u32))
  164. - ColorRect fourth(color: Color(0xffcc33ff'u32))
  165. - ColorRect fifth(color: Color(0xffcc66ff'u32))
  166. - ColorRect sixth(color: Color(0xff6655ff'u32))
  167. getSceneByName("main").addChild(grid)
  168. test "EditText test":
  169. build:
  170. - EditText edit:
  171. call move(0, 150)
  172. getSceneByName("main").addChild(edit)
  173. test "Scroll test":
  174. # TODO: Fix it with glFramebuffer
  175. build:
  176. - Scroll scroll:
  177. call setAnchor(1, 0, 1, 0)
  178. - GridBox grid:
  179. call setPadding(2, 4, 8, 16)
  180. call setRow(3)
  181. call setBackgroundColor(Color(1f, 1f, 1f))
  182. - ColorRect first(color: Color(0xff6699ff'u32), rect_size: Vector2(100, 200))
  183. - ColorRect second(color: Color(0xff64ffff'u32))
  184. - ColorRect third(color: Color(0xffaa00ff'u32))
  185. - ColorRect fourth(color: Color(0xffcc33ff'u32))
  186. - ColorRect fifth(color: Color(0xffcc66ff'u32))
  187. - ColorRect sixth(color: Color(0xff6655ff'u32))
  188. getSceneByName("main").addChild(scroll)
  189. test "ProgressBar test":
  190. build:
  191. - ProgressBar bar1:
  192. progress_type: PROGRESS_BAR_HORIZONTAL
  193. indeterminate: true
  194. call setProgress(50)
  195. call move(120, 110)
  196. call resize(100, 20)
  197. - ProgressBar bar2:
  198. progress_type: PROGRESS_BAR_VERTICAL
  199. indeterminate: true
  200. call setProgress(50)
  201. call move(220, 100)
  202. call resize(20, 110)
  203. - ProgressBar bar3:
  204. progress_type: PROGRESS_BAR_CIRCLE
  205. indeterminate: true
  206. call setProgress(50)
  207. call move(320, 110)
  208. call resize(100, 100)
  209. getSceneByName("main").addChildren(bar1, bar2, bar3)
  210. test "Popup test":
  211. build:
  212. - Popup popup:
  213. call setAnchor(0, 1, 0, 1)
  214. - Label text:
  215. call setText("popup!")
  216. getSceneByName("main").getNode("control")@onProcess(self):
  217. if isActionJustPressed("enter"):
  218. popup.toggle()
  219. getSceneByName("main").addChild(popup)
  220. test "TextureButton test":
  221. build:
  222. - TextureButton button:
  223. call setNormalTexture(load("assets/button_normal.png", GL_RGBA))
  224. call setHoverTexture(load("assets/button_hover.png", GL_RGBA))
  225. call setPressTexture(load("assets/button_press.png", GL_RGBA))
  226. call resize(256, 64)
  227. call move(120, 220)
  228. call setText("Texture button")
  229. getSceneByName("main").addChild(button)
  230. test "TextureProgressBar test":
  231. build:
  232. - TextureProgressBar progress:
  233. call setProgressTexture(load("assets/texture_progress_1.png", GL_RGBA))
  234. call setBackgroundTexture(load("assets/texture_progress_0.png", GL_RGBA))
  235. call setProgress(50)
  236. call resize(256, 85)
  237. call move(100, 300)
  238. getSceneByName("main").addChild(progress)
  239. test "Counter test":
  240. build:
  241. - Counter counter:
  242. call move(360, 360)
  243. call setMaxValue(100)
  244. getSceneByName("main").addChild(counter)
  245. test "Switch test":
  246. build:
  247. - Switch switch:
  248. call move(360, 400)
  249. getSceneByName("main").addChild(switch)
  250. test "CheckBox test":
  251. build:
  252. - CheckBox check:
  253. call setText("smth checkbox")
  254. call enable()
  255. call move(700, 300)
  256. getSceneByName("main").addChild(check)
  257. test "SubWindow test":
  258. build:
  259. - SubWindow window1:
  260. call setIcon("assets/anim/0.jpg")
  261. call setTitle("subwindow")
  262. call move(500, 400)
  263. call open()
  264. getSceneByName("main").addChild(window1)
  265. test "Slider test":
  266. build:
  267. - Slider slider1:
  268. slider_type: SLIDER_HORIZONTAL
  269. call resize(100, 10)
  270. call move(600, 300)
  271. - Slider slider2:
  272. slider_type: SLIDER_VERTICAL
  273. call resize(10, 100)
  274. call move(600, 310)
  275. getSceneByName("main").addChildren(slider1, slider2)
  276. test "ToolTip test":
  277. build:
  278. - ToolTip tooltip:
  279. call showAtMouse()
  280. @onProcess():
  281. tooltip.showAtMouse()
  282. getSceneByName("main").addChild(tooltip)
  283. test "Line & Bar chart test":
  284. build:
  285. - Chart my_chart:
  286. call addChartData(
  287. newChartData(
  288. @["one", "two", "three", "four", "five", "six"],
  289. @[1, 8, 18, 32, 4, 16], "myData", current_theme~accent_dark, BAR_CHART))
  290. call addChartData(
  291. newChartData(
  292. @["one", "two", "three", "four", "five", "six"],
  293. @[1, 8, 18, 32, 4, 16], "myData", current_theme~accent, LINE_CHART))
  294. call move(100, 450)
  295. call resize(320, 196)
  296. getSceneByName("main").addChildren(my_chart)
  297. test "Pie chart test":
  298. build:
  299. - Chart circle_chart:
  300. call addChartData(
  301. newChartData(
  302. @["one", "two", "three", "four", "five", "six"],
  303. @[1, 8, 18, 32, 4, 16], "myData", current_theme~accent_dark, PIE_CHART))
  304. call move(900, 450)
  305. call resize(128, 128)
  306. getSceneByName("main").addChildren(circle_chart)
  307. test "Radar chart test":
  308. build:
  309. - Chart spiderweb_chart:
  310. call addChartData(
  311. newChartData(
  312. @["one", "two", "three", "four", "five"],
  313. @[10, 24, 18, 32, 4], "myData", current_theme~accent_dark, RADAR_CHART))
  314. call move(700, 450)
  315. call resize(128, 128)
  316. getSceneByName("main").addChildren(spiderweb_chart)
  317. test "Launch window":
  318. windowLaunch()