test3.nim 11 KB

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