__init__.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. from pydrk import SceneNodeType, PropertyType, vertex, face, serial
  2. from .print_tree import print_tree
  3. from .api import *
  4. from .gfx import Layer, add_object
  5. from . import settings
  6. import time
  7. latin = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  8. numerals = "0123456789"
  9. punct = " .,<>/\\'[]{}:`~!@#$%^&*()_+?"
  10. keycmds = [
  11. "Backspace"
  12. ]
  13. class App(EventLoop):
  14. def __init__(self):
  15. super().__init__()
  16. w = get_property("/window", "width")
  17. h = get_property("/window", "height")
  18. self.chatbox_layer = Layer("chatbox_layer")
  19. self.chatbox_layer.resize(w, h)
  20. self.chatbox_layer.add_obj("outline")
  21. self.chatbox_layer.add_obj("user_input")
  22. self.cursor_layer = Layer("cursor_layer")
  23. self.cursor_layer.add_obj("cursor")
  24. self.cursor_layer.resize(w, h)
  25. self.rounded_box_layer = Layer("rounded_box_layer")
  26. self.rounded_box_layer.add_obj("box")
  27. self.rounded_box_layer.resize(w, h)
  28. self.user_input = ""
  29. self.last_keypress_time = 0
  30. def resize_event(self, w, h):
  31. self.chatbox_layer.resize(w, h)
  32. self.cursor_layer.resize(w, h)
  33. self.rounded_box_layer.resize(w, h)
  34. resize_box()
  35. resize_rounded_box()
  36. draw_txt(self.user_input)
  37. def mouse_click(self, x, y):
  38. print(f"mouse click ({x}, {y})")
  39. def mouse_wheel(self, y):
  40. settings.ui_scale *= 1 + y/100
  41. print(settings.ui_scale)
  42. def key_down(self, keycode, keymods, repeat):
  43. #print(f"key_down: '{keycode}'")
  44. if keymods.ctrl and keycode == "=":
  45. settings.ui_scale *= 1.01
  46. print(settings.ui_scale)
  47. elif keymods.ctrl and keycode == "-":
  48. settings.ui_scale *= 0.99
  49. print(settings.ui_scale)
  50. elif keymods.ctrl and keycode == "H":
  51. print("hello")
  52. elif keymods.ctrl and keycode == "P":
  53. print_tree()
  54. elif keycode in latin:
  55. key = keycode.upper() if keymods.shift else keycode.lower()
  56. self.type_key(key, keymods, repeat)
  57. elif keycode in numerals or keycode in punct or keycode in keycmds:
  58. self.type_key(keycode, keymods, repeat)
  59. #else:
  60. # print(keycode)
  61. def type_key(self, key, _keymods, _repeat):
  62. now = time.time()
  63. if now - self.last_keypress_time < 0.2:
  64. return
  65. self.last_keypress_time = now
  66. if key == "Backspace":
  67. self.user_input = self.user_input[:-1]
  68. else:
  69. self.user_input += key
  70. draw_txt(self.user_input)
  71. def draw_txt(user_input):
  72. obj_id = add_object("/window/chatbox_layer", "user_input2")
  73. # create a new one and link it
  74. text_id = host.create_text("/font/inter-regular", "txt2", user_input, 30)
  75. link_node(text_id, obj_id)
  76. layer_w = get_property("/window/chatbox_layer", "rect_w")
  77. layer_h = get_property("/window/chatbox_layer", "rect_h")
  78. x = 20
  79. y = layer_h - 20 - 30
  80. set_property_f32("/window/chatbox_layer/user_input2", "x", x)
  81. set_property_f32("/window/chatbox_layer/user_input2", "y", y)
  82. set_property_f32("/window/chatbox_layer/user_input2/txt2", "r", 1)
  83. set_property_f32("/window/chatbox_layer/user_input2/txt2", "g", 1)
  84. set_property_f32("/window/chatbox_layer/user_input2/txt2", "b", 1)
  85. set_property_f32("/window/chatbox_layer/user_input2/txt2", "a", 1)
  86. # Switch visibility
  87. set_property_bool("/window/chatbox_layer/user_input", "is_visible", False)
  88. set_property_bool("/window/chatbox_layer/user_input2", "is_visible", True)
  89. # Remove the old object
  90. old_id = lookup_node("/window/chatbox_layer/user_input")
  91. unlink_from_parents(old_id)
  92. remove_node_recursive(old_id)
  93. rename_node("/window/chatbox_layer/user_input2/txt2", "txt")
  94. rename_node("/window/chatbox_layer/user_input2", "user_input")
  95. reposition_cursor()
  96. def reposition_cursor():
  97. layer_h = get_property("/window/chatbox_layer", "rect_h")
  98. y = layer_h - 20 - 30
  99. # Move the cursor
  100. text_id = api.lookup_node_id("/window/chatbox_layer/user_input/txt")
  101. text_px_w = 0
  102. user_input = ""
  103. if text_id is not None:
  104. text_px_w = get_property(text_id, "width")
  105. user_input = get_property(text_id, "text")
  106. x = text_px_w + 25
  107. if user_input and user_input[-1] == " ":
  108. x += 10
  109. set_property_f32("/window/cursor_layer/cursor", "x", x)
  110. set_property_f32("/window/cursor_layer/cursor", "y", y)
  111. def draw_cursor():
  112. node_id = api.add_node("cursor_box", SceneNodeType.RENDER_MESH)
  113. api.add_property(node_id, "verts", PropertyType.BUFFER)
  114. api.add_property(node_id, "faces", PropertyType.BUFFER)
  115. link_node(node_id, "/window/cursor_layer/cursor")
  116. x, y = 0, 0
  117. w, h = 20, 40
  118. vert1 = vertex(x, y, 1, 1, 1, 1, 0, 0)
  119. vert2 = vertex(x + w, y, 1, 1, 1, 1, 1, 0)
  120. vert3 = vertex(x, y + h, 1, 1, 1, 1, 0, 1)
  121. vert4 = vertex(x + w, y + h, 1, 1, 1, 1, 1, 1)
  122. api.set_property_buffer(node_id, "verts", vert1 + vert2 + vert3 + vert4)
  123. api.set_property_buffer(node_id, "faces", face(0, 2, 1) + face(1, 2, 3))
  124. def draw_box():
  125. node_id = api.add_node("box", SceneNodeType.RENDER_MESH)
  126. api.add_property(node_id, "verts", PropertyType.BUFFER)
  127. api.add_property(node_id, "faces", PropertyType.BUFFER)
  128. link_node(node_id, "/window/chatbox_layer/outline")
  129. node_id = api.add_node("inner_box", SceneNodeType.RENDER_MESH)
  130. api.add_property(node_id, "verts", PropertyType.BUFFER)
  131. api.add_property(node_id, "faces", PropertyType.BUFFER)
  132. link_node(node_id, "/window/chatbox_layer/outline")
  133. resize_box()
  134. def resize_box():
  135. node_id = api.lookup_node_id("/window/chatbox_layer/outline/box")
  136. layer_w = get_property("/window/chatbox_layer", "rect_w")
  137. layer_h = get_property("/window/chatbox_layer", "rect_h")
  138. box_h = 60
  139. # Inner padding, so box inside will be (box_h - 2*padding) px high
  140. padding = 10
  141. # Lets add a poly - must be counterclockwise
  142. x, y = 0, layer_h - box_h
  143. w, h = layer_w, box_h
  144. vert1 = vertex(x, y, 1, 1, 1, 1, 0, 0)
  145. vert2 = vertex(x + w, y, 1, 1, 1, 1, 1, 0)
  146. vert3 = vertex(x, y + h, 1, 1, 1, 1, 0, 1)
  147. vert4 = vertex(x + w, y + h, 1, 1, 1, 1, 1, 1)
  148. api.set_property_buffer(node_id, "verts", vert1 + vert2 + vert3 + vert4)
  149. api.set_property_buffer(node_id, "faces", face(0, 2, 1) + face(1, 2, 3))
  150. # Second mesh
  151. node_id = api.lookup_node_id("/window/chatbox_layer/outline/inner_box")
  152. x, y = x + padding, y + padding
  153. w -= 2*padding
  154. h -= 2*padding
  155. vert1 = vertex(x, y, 0, 0, 0, 1, 0, 0)
  156. vert2 = vertex(x + w, y, 0, 0, 0, 1, 1, 0)
  157. vert3 = vertex(x, y + h, 0, 0, 0, 1, 0, 1)
  158. vert4 = vertex(x + w, y + h, 0.3, 0.3, 0.3, 1, 1, 1)
  159. api.set_property_buffer(node_id, "verts", vert1 + vert2 + vert3 + vert4)
  160. api.set_property_buffer(node_id, "faces", face(0, 2, 1) + face(1, 2, 3))
  161. def draw_rounded_box():
  162. node_id = api.add_node("box", SceneNodeType.RENDER_MESH)
  163. api.add_property(node_id, "verts", PropertyType.BUFFER)
  164. api.add_property(node_id, "faces", PropertyType.BUFFER)
  165. link_node(node_id, "/window/rounded_box_layer/box")
  166. node_id = api.add_node("inner_box", SceneNodeType.RENDER_MESH)
  167. api.add_property(node_id, "verts", PropertyType.BUFFER)
  168. api.add_property(node_id, "faces", PropertyType.BUFFER)
  169. link_node(node_id, "/window/rounded_box_layer/box")
  170. resize_rounded_box()
  171. def resize_rounded_box():
  172. node_id = api.lookup_node_id("/window/rounded_box_layer/box/box")
  173. layer_w = get_property("/window/rounded_box_layer", "rect_w")
  174. layer_h = get_property("/window/rounded_box_layer", "rect_h")
  175. # Inner padding, so box inside will be (box_h - 2*padding) px high
  176. padding = 5
  177. bevel = 20
  178. # Lets add a poly - must be counterclockwise
  179. x, y = layer_w/4, layer_h/4
  180. w, h = layer_w/2, layer_h/2
  181. y0 = y + bevel
  182. h0 = h - 2*bevel
  183. verts = (
  184. vertex(x, y0, 1, 1, 1, 1, 0, 0) +
  185. vertex(x + w, y0, 1, 1, 1, 1, 1, 0) +
  186. vertex(x, y0 + h0, 1, 1, 1, 1, 0, 1) +
  187. vertex(x + w, y0 + h0, 1, 1, 1, 1, 1, 1)
  188. )
  189. faces = face(0, 2, 1) + face(1, 2, 3)
  190. x0 = x + bevel
  191. w0 = w - 2*bevel
  192. h0 = bevel
  193. verts += (
  194. vertex(x0, y, 1, 1, 1, 1, 0, 0) +
  195. vertex(x0 + w0, y, 1, 1, 1, 1, 1, 0) +
  196. vertex(x0, y + h0, 1, 1, 1, 1, 0, 1) +
  197. vertex(x0 + w0, y + h0, 1, 1, 1, 1, 1, 1)
  198. )
  199. o = 4
  200. faces += face(o + 0, o + 2, o + 1) + face(o + 1, o + 2, o + 3)
  201. y0 = y + h - bevel
  202. x0 = x + bevel
  203. w0 = w - 2*bevel
  204. h0 = bevel
  205. verts += (
  206. vertex(x0, y0, 1, 1, 1, 1, 0, 0) +
  207. vertex(x0 + w0, y0, 1, 1, 1, 1, 1, 0) +
  208. vertex(x0, y0 + h0, 1, 1, 1, 1, 0, 1) +
  209. vertex(x0 + w0, y0 + h0, 1, 1, 1, 1, 1, 1)
  210. )
  211. o = 8
  212. faces += face(o + 0, o + 2, o + 1) + face(o + 1, o + 2, o + 3)
  213. api.set_property_buffer(node_id, "verts", verts)
  214. api.set_property_buffer(node_id, "faces", faces)
  215. # Second mesh
  216. node_id = api.lookup_node_id("/window/rounded_box_layer/box/inner_box")
  217. x, y = x + padding, y + padding
  218. w -= 2*padding
  219. h -= 2*padding
  220. vert1 = vertex(x, y, 0, 0, 0, 1, 0, 0)
  221. vert2 = vertex(x + w, y, 0, 0, 0, 1, 1, 0)
  222. vert3 = vertex(x, y + h, 0, 0, 0, 1, 0, 1)
  223. vert4 = vertex(x + w, y + h, 0.3, 0.3, 0.3, 1, 1, 1)
  224. #api.set_property_buffer(node_id, "verts", vert1 + vert2 + vert3 + vert4)
  225. #api.set_property_buffer(node_id, "faces", face(0, 2, 1) + face(1, 2, 3))
  226. def draw():
  227. win_id = api.lookup_node_id("/window")
  228. # Add foo layer
  229. layer_id = api.add_node("foo", SceneNodeType.RENDER_LAYER)
  230. prop = Property(
  231. "is_visible", PropertyType.BOOL, PropertySubType.NULL,
  232. None,
  233. "Is Visible", "Visibility of the layer",
  234. False, False, 1, None, None, []
  235. )
  236. api.add_property(layer_id, prop)
  237. api.set_property_bool(layer_id, "is_visible", 0, True)
  238. #prop = Property(
  239. # "redraw", PropertyType.BOOL, PropertySubType.NULL,
  240. # None,
  241. # "redraw", "Redraw this layer",
  242. # False, False, 1, None, None, []
  243. #)
  244. #api.add_property(layer_id, prop)
  245. #api.set_property_bool(layer_id, "redraw", 0, True)
  246. prop = Property(
  247. "rect", PropertyType.UINT32, PropertySubType.PIXEL,
  248. None,
  249. "Rectangle", "The position and size within the layer",
  250. False, True, 4, None, None, []
  251. )
  252. api.add_property(layer_id, prop)
  253. # x
  254. api.set_property_u32(layer_id, "rect", 0, 0)
  255. # y
  256. api.set_property_u32(layer_id, "rect", 1, 0)
  257. # w
  258. #api.set_property_u32(layer_id, "rect", 2, int(3838/2))
  259. code = [["as_u32", ["/", ["load", "sw"], ["u32", 2]]]]
  260. api.set_property_expr(layer_id, "rect", 2, code)
  261. # h
  262. code = [["as_u32", ["/", ["load", "sh"], ["u32", 2]]]]
  263. api.set_property_expr(layer_id, "rect", 3, code)
  264. api.link_node(layer_id, win_id)
  265. # Add a mesh to our layer
  266. node_id = api.add_node("meshie", SceneNodeType.RENDER_MESH)
  267. prop = Property(
  268. "data", PropertyType.BUFFER, PropertySubType.NULL,
  269. None,
  270. "Mesh Data", "The face and vertex data for the mesh",
  271. False, False, 2, None, None, []
  272. )
  273. api.add_property(node_id, prop)
  274. #x, y = 0.1, 0.1
  275. #w, h = 0.1, 0.1
  276. x, y, w, h = 0, 0, 1, 1
  277. #x, y, w, h = -1, 1, 2, -2
  278. vert1 = vertex(x, y, 0, 0, 0, 1, 0, 0)
  279. vert2 = vertex(x + w, y, 0, 0, 0, 1, 1, 0)
  280. vert3 = vertex(x, y + h, 0, 0, 0, 1, 0, 1)
  281. vert4 = vertex(x + w, y + h, 0, 0, 0, 1, 1, 1)
  282. verts = vert1 + vert2 + vert3 + vert4
  283. faces = face(0, 2, 1) + face(1, 2, 3)
  284. api.set_property_buf(node_id, "data", 0, verts)
  285. api.set_property_buf(node_id, "data", 1, faces)
  286. prop = Property(
  287. "rect", PropertyType.FLOAT32, PropertySubType.PIXEL,
  288. None,
  289. "Rectangle", "The position and size within the layer",
  290. False, True, 4, None, None, []
  291. )
  292. api.add_property(node_id, prop)
  293. # x
  294. api.set_property_f32(node_id, "rect", 0, 10)
  295. # y
  296. api.set_property_f32(node_id, "rect", 1, 10)
  297. # w
  298. #api.set_property_f32(node_id, "rect", 2, 20)
  299. code = [["-", ["load", "lw"], ["f32", 10]]]
  300. api.set_property_expr(node_id, "rect", 2, code)
  301. # h
  302. #api.set_property_str(node_id, "rect", 3, "lh - 10")
  303. code = [["-", ["load", "lh"], ["f32", 10]]]
  304. api.set_property_expr(node_id, "rect", 3, code)
  305. prop = Property(
  306. "z_index", PropertyType.UINT32, PropertySubType.NULL,
  307. None,
  308. "Z-index", "Z-index: values greater than zero are deferred draws",
  309. False, False, 1, None, None, []
  310. )
  311. api.add_property(node_id, prop)
  312. api.link_node(node_id, layer_id)
  313. # Add a second mesh to our layer
  314. node_id = api.add_node("meshie2", SceneNodeType.RENDER_MESH)
  315. prop = Property(
  316. "data", PropertyType.BUFFER, PropertySubType.NULL,
  317. None,
  318. "Mesh Data", "The face and vertex data for the mesh",
  319. False, False, 2, None, None, []
  320. )
  321. api.add_property(node_id, prop)
  322. x, y, w, h = 0, 0, 1, 1
  323. vert1 = vertex(x, y, 1, 0, 1, 1, 0, 0)
  324. vert2 = vertex(x + w, y, 0.5, 0, 1, 1, 1, 0)
  325. vert3 = vertex(x, y + h, 1, 0, 0.5, 1, 0, 1)
  326. vert4 = vertex(x + w, y + h, 0.5, 1, 0.5, 1, 1, 1)
  327. verts = vert1 + vert2 + vert3 + vert4
  328. faces = face(0, 2, 1) + face(1, 2, 3)
  329. api.set_property_buf(node_id, "data", 0, verts)
  330. api.set_property_buf(node_id, "data", 1, faces)
  331. prop = Property(
  332. "rect", PropertyType.FLOAT32, PropertySubType.PIXEL,
  333. None,
  334. "Rectangle", "The position and size within the layer",
  335. False, True, 4, None, None, []
  336. )
  337. api.add_property(node_id, prop)
  338. api.set_property_f32(node_id, "rect", 0, 10)
  339. api.set_property_f32(node_id, "rect", 1, 10)
  340. api.set_property_f32(node_id, "rect", 2, 60)
  341. api.set_property_f32(node_id, "rect", 3, 60)
  342. prop = Property(
  343. "z_index", PropertyType.UINT32, PropertySubType.NULL,
  344. None,
  345. "Z-index", "Z-index: values greater than zero are deferred draws",
  346. False, False, 1, None, None, []
  347. )
  348. api.add_property(node_id, prop)
  349. api.link_node(node_id, layer_id)
  350. # Add some text
  351. node_id = api.add_node("hellowurld", SceneNodeType.RENDER_TEXT)
  352. prop = Property(
  353. "rect", PropertyType.FLOAT32, PropertySubType.PIXEL,
  354. None,
  355. "Rectangle", "The position and size within the layer",
  356. False, True, 4, None, None, []
  357. )
  358. api.add_property(node_id, prop)
  359. api.set_property_f32(node_id, "rect", 0, 10)
  360. api.set_property_f32(node_id, "rect", 1, 100)
  361. api.set_property_f32(node_id, "rect", 2, 60)
  362. api.set_property_f32(node_id, "rect", 3, 60)
  363. prop = Property(
  364. "baseline", PropertyType.FLOAT32, PropertySubType.PIXEL,
  365. None,
  366. "Baseline", "Y offset of baseline inside rect",
  367. False, True, 1, None, None, []
  368. )
  369. api.add_property(node_id, prop)
  370. api.set_property_f32(node_id, "baseline", 0, 50)
  371. prop = Property(
  372. "font_size", PropertyType.FLOAT32, PropertySubType.PIXEL,
  373. None,
  374. "Font Size", "Font Size",
  375. False, True, 1, 0.0, None, []
  376. )
  377. api.add_property(node_id, prop)
  378. api.set_property_f32(node_id, "font_size", 0, 30)
  379. prop = Property(
  380. "text", PropertyType.STR, PropertySubType.NULL,
  381. None,
  382. "Text", "Text",
  383. False, False, 1, None, None, []
  384. )
  385. api.add_property(node_id, prop)
  386. api.set_property_str(node_id, "text", 0, "hello!😁🍆jelly 🍆1234")
  387. prop = Property(
  388. "color", PropertyType.FLOAT32, PropertySubType.COLOR,
  389. None,
  390. "Color", "Color of the text",
  391. False, False, 4, 0, 1, []
  392. )
  393. api.add_property(node_id, prop)
  394. api.set_property_f32(node_id, "color", 0, 1)
  395. api.set_property_f32(node_id, "color", 1, 1)
  396. api.set_property_f32(node_id, "color", 2, 1)
  397. api.set_property_f32(node_id, "color", 3, 1)
  398. #prop = Property(
  399. # "overflow", PropertyType.ENUM, PropertySubType.NULL,
  400. # None,
  401. # "Overflow Behaviour", "Behaviour when text exceeds bounding box",
  402. # False, True, 1, None, None, [
  403. # "ScrollRight",
  404. # "OverflowRight"
  405. # ]
  406. #)
  407. #api.add_property(node_id, prop)
  408. #api.set_property_enum(node_id, "overflow", 0, "ScrollRight")
  409. prop = Property(
  410. "z_index", PropertyType.UINT32, PropertySubType.NULL,
  411. None,
  412. "Z-index", "Z-index: values greater than zero are deferred draws",
  413. False, False, 1, None, None, []
  414. )
  415. api.add_property(node_id, prop)
  416. prop = Property(
  417. "debug", PropertyType.BOOL, PropertySubType.NULL,
  418. None,
  419. "Debug", "Draw debug outlines",
  420. False, False, 1, None, None, []
  421. )
  422. api.add_property(node_id, prop)
  423. api.link_node(node_id, layer_id)
  424. # EditBox
  425. node_id = api.add_node("editz", SceneNodeType.EDIT_BOX)
  426. prop = Property(
  427. "is_active", PropertyType.BOOL, PropertySubType.NULL,
  428. None,
  429. "Is Active", "Whether the editbox is active",
  430. False, True, 1, None, None, []
  431. )
  432. api.add_property(node_id, prop)
  433. api.set_property_bool(node_id, "is_active", 0, True)
  434. prop = Property(
  435. "rect", PropertyType.FLOAT32, PropertySubType.PIXEL,
  436. None,
  437. "Rectangle", "The position and size within the layer",
  438. False, True, 4, None, None, []
  439. )
  440. api.add_property(node_id, prop)
  441. api.set_property_f32(node_id, "rect", 0, 60)
  442. api.set_property_f32(node_id, "rect", 1, 200)
  443. api.set_property_f32(node_id, "rect", 2, 300)
  444. api.set_property_f32(node_id, "rect", 3, 60)
  445. prop = Property(
  446. "baseline", PropertyType.FLOAT32, PropertySubType.PIXEL,
  447. None,
  448. "Baseline", "Y offset of baseline inside rect",
  449. False, True, 1, None, None, []
  450. )
  451. api.add_property(node_id, prop)
  452. api.set_property_f32(node_id, "baseline", 0, 50)
  453. prop = Property(
  454. "scroll", PropertyType.FLOAT32, PropertySubType.NULL,
  455. None,
  456. "Scroll", "Current scroll",
  457. False, False, 1, None, None, []
  458. )
  459. api.add_property(node_id, prop)
  460. prop = Property(
  461. "cursor_pos", PropertyType.UINT32, PropertySubType.NULL,
  462. None,
  463. "Cursor Position", "Cursor position within the text",
  464. False, False, 1, None, None, []
  465. )
  466. api.add_property(node_id, prop)
  467. prop = Property(
  468. "font_size", PropertyType.FLOAT32, PropertySubType.PIXEL,
  469. None,
  470. "Font Size", "Font Size",
  471. False, True, 1, 0.0, None, []
  472. )
  473. api.add_property(node_id, prop)
  474. api.set_property_f32(node_id, "font_size", 0, 50)
  475. prop = Property(
  476. "text", PropertyType.STR, PropertySubType.NULL,
  477. None,
  478. "Text", "Text",
  479. False, False, 1, None, None, []
  480. )
  481. api.add_property(node_id, prop)
  482. api.set_property_str(node_id, "text", 0, "hello king!😁🍆jelly 🍆1234")
  483. prop = Property(
  484. "text_color", PropertyType.FLOAT32, PropertySubType.COLOR,
  485. None,
  486. "Text Color", "Color of the text",
  487. False, False, 4, 0, 1, []
  488. )
  489. api.add_property(node_id, prop)
  490. api.set_property_f32(node_id, "text_color", 0, 1)
  491. api.set_property_f32(node_id, "text_color", 1, 1)
  492. api.set_property_f32(node_id, "text_color", 2, 1)
  493. api.set_property_f32(node_id, "text_color", 3, 1)
  494. prop = Property(
  495. "cursor_color", PropertyType.FLOAT32, PropertySubType.COLOR,
  496. None,
  497. "Cursor Color", "Color of the cursor",
  498. False, False, 4, 0, 1, []
  499. )
  500. api.add_property(node_id, prop)
  501. api.set_property_f32(node_id, "cursor_color", 0, 1)
  502. api.set_property_f32(node_id, "cursor_color", 1, 0.5)
  503. api.set_property_f32(node_id, "cursor_color", 2, 0.5)
  504. api.set_property_f32(node_id, "cursor_color", 3, 1)
  505. prop = Property(
  506. "hi_bg_color", PropertyType.FLOAT32, PropertySubType.COLOR,
  507. None,
  508. "Highlight Bg Color", "Background color for highlighted text",
  509. False, False, 4, 0, 1, []
  510. )
  511. api.add_property(node_id, prop)
  512. api.set_property_f32(node_id, "hi_bg_color", 0, 1)
  513. api.set_property_f32(node_id, "hi_bg_color", 1, 1)
  514. api.set_property_f32(node_id, "hi_bg_color", 2, 1)
  515. api.set_property_f32(node_id, "hi_bg_color", 3, 0.5)
  516. prop = Property(
  517. "selected", PropertyType.UINT32, PropertySubType.NULL,
  518. None,
  519. "Selected", "Selected range",
  520. True, False, 2, 0, None, []
  521. )
  522. api.add_property(node_id, prop)
  523. api.set_property_u32(node_id, "selected", 0, 1)
  524. api.set_property_u32(node_id, "selected", 1, 4)
  525. prop = Property(
  526. "z_index", PropertyType.UINT32, PropertySubType.NULL,
  527. None,
  528. "Z-index", "Z-index: values greater than zero are deferred draws",
  529. False, False, 1, None, None, []
  530. )
  531. api.add_property(node_id, prop)
  532. api.set_property_u32(node_id, "z_index", 0, 4)
  533. prop = Property(
  534. "debug", PropertyType.BOOL, PropertySubType.NULL,
  535. None,
  536. "Debug", "Draw debug outlines",
  537. False, False, 1, None, None, []
  538. )
  539. api.add_property(node_id, prop)
  540. api.set_property_bool(node_id, "debug", 0, True)
  541. arg_data = bytearray()
  542. serial.write_u32(arg_data, node_id)
  543. api.call_method(win_id, "create_edit_box", arg_data)
  544. api.link_node(node_id, layer_id)
  545. def main():
  546. draw()
  547. # DEBUG
  548. print_tree()
  549. #def main():
  550. # if True:
  551. # node_id = api.add_node("foo", SceneNodeType.WINDOW)
  552. # prop = Property(
  553. # "myprop", PropertyType.FLOAT32, PropertySubType.NULL,
  554. # None,
  555. # "myprop", "",
  556. # False, 2, None, None, []
  557. # )
  558. # api.add_property(1, prop)
  559. # api.link_node(node_id, 0)
  560. # api.set_property_f32(1, "myprop", 0, 4.0)
  561. # api.set_property_f32(1, "myprop", 1, 110.0)
  562. # print("val =", api.get_property_value(1, "myprop"))
  563. # for prop in api.get_properties(1):
  564. # print("Property:")
  565. # print(f" name = {prop.name}")
  566. # print(f" type = {prop.type}")
  567. # print(f" subtype = {prop.subtype}")
  568. # print(f" defaults = {prop.defaults}")
  569. # print(f" ui_name = {prop.ui_name}")
  570. # print(f" desc = {prop.desc}")
  571. # print(f" is_null_allowed = {prop.is_null_allowed}")
  572. # print(f" array_len = {prop.array_len}")
  573. # print(f" min_val = {prop.min_val}")
  574. # print(f" max_val = {prop.max_val}")
  575. # print(f" enum_items = {prop.enum_items}")
  576. # print()
  577. # print_tree()
  578. # #garbage_collect()
  579. #
  580. # #app = App()
  581. # #draw_box()
  582. # #draw_rounded_box()
  583. # #draw_cursor()
  584. # #reposition_cursor()
  585. # ##print_tree()
  586. # #app.run()