test3.nim 11 KB

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