__init__.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. from pydrk import SceneNodeType, PropertyType, vertex, face
  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. mesh_id = api.add_node("cursor_box", SceneNodeType.RENDER_MESH)
  113. api.add_property(mesh_id, "verts", PropertyType.BUFFER)
  114. api.add_property(mesh_id, "faces", PropertyType.BUFFER)
  115. link_node(mesh_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(mesh_id, "verts", vert1 + vert2 + vert3 + vert4)
  123. api.set_property_buffer(mesh_id, "faces", face(0, 2, 1) + face(1, 2, 3))
  124. def draw_box():
  125. mesh_id = api.add_node("box", SceneNodeType.RENDER_MESH)
  126. api.add_property(mesh_id, "verts", PropertyType.BUFFER)
  127. api.add_property(mesh_id, "faces", PropertyType.BUFFER)
  128. link_node(mesh_id, "/window/chatbox_layer/outline")
  129. mesh_id = api.add_node("inner_box", SceneNodeType.RENDER_MESH)
  130. api.add_property(mesh_id, "verts", PropertyType.BUFFER)
  131. api.add_property(mesh_id, "faces", PropertyType.BUFFER)
  132. link_node(mesh_id, "/window/chatbox_layer/outline")
  133. resize_box()
  134. def resize_box():
  135. mesh_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(mesh_id, "verts", vert1 + vert2 + vert3 + vert4)
  149. api.set_property_buffer(mesh_id, "faces", face(0, 2, 1) + face(1, 2, 3))
  150. # Second mesh
  151. mesh_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(mesh_id, "verts", vert1 + vert2 + vert3 + vert4)
  160. api.set_property_buffer(mesh_id, "faces", face(0, 2, 1) + face(1, 2, 3))
  161. def draw_rounded_box():
  162. mesh_id = api.add_node("box", SceneNodeType.RENDER_MESH)
  163. api.add_property(mesh_id, "verts", PropertyType.BUFFER)
  164. api.add_property(mesh_id, "faces", PropertyType.BUFFER)
  165. link_node(mesh_id, "/window/rounded_box_layer/box")
  166. mesh_id = api.add_node("inner_box", SceneNodeType.RENDER_MESH)
  167. api.add_property(mesh_id, "verts", PropertyType.BUFFER)
  168. api.add_property(mesh_id, "faces", PropertyType.BUFFER)
  169. link_node(mesh_id, "/window/rounded_box_layer/box")
  170. resize_rounded_box()
  171. def resize_rounded_box():
  172. mesh_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(mesh_id, "verts", verts)
  214. api.set_property_buffer(mesh_id, "faces", faces)
  215. # Second mesh
  216. mesh_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(mesh_id, "verts", vert1 + vert2 + vert3 + vert4)
  225. #api.set_property_buffer(mesh_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. "rect", PropertyType.UINT32, PropertySubType.PIXEL,
  240. None,
  241. "mesh_rect", "The position and size within the layer",
  242. False, True, 4, None, None, []
  243. )
  244. api.add_property(layer_id, prop)
  245. # x
  246. api.set_property_u32(layer_id, "rect", 0, 0)
  247. # y
  248. api.set_property_u32(layer_id, "rect", 1, 0)
  249. # w
  250. #api.set_property_u32(layer_id, "rect", 2, int(3838/2))
  251. code = [["as_u32", ["/", ["load", "sw"], ["u32", 2]]]]
  252. api.set_property_expr(layer_id, "rect", 2, code)
  253. # h
  254. code = [["as_u32", ["/", ["load", "sh"], ["u32", 2]]]]
  255. api.set_property_expr(layer_id, "rect", 3, code)
  256. api.link_node(layer_id, win_id)
  257. # Add a mesh to our layer
  258. mesh_id = api.add_node("meshie", SceneNodeType.RENDER_MESH)
  259. prop = Property(
  260. "data", PropertyType.BUFFER, PropertySubType.NULL,
  261. None,
  262. "mesh_data", "The face and vertex data for the mesh",
  263. False, False, 2, None, None, []
  264. )
  265. api.add_property(mesh_id, prop)
  266. #x, y = 0.1, 0.1
  267. #w, h = 0.1, 0.1
  268. x, y, w, h = 0, 0, 1, 1
  269. #x, y, w, h = -1, 1, 2, -2
  270. vert1 = vertex(x, y, 1, 0, 0, 1, 0, 0)
  271. vert2 = vertex(x + w, y, 0, 1, 0, 1, 1, 0)
  272. vert3 = vertex(x, y + h, 0, 0, 1, 1, 0, 1)
  273. vert4 = vertex(x + w, y + h, 1, 1, 1, 1, 1, 1)
  274. verts = vert1 + vert2 + vert3 + vert4
  275. faces = face(0, 2, 1) + face(1, 2, 3)
  276. api.set_property_buf(mesh_id, "data", 0, verts)
  277. api.set_property_buf(mesh_id, "data", 1, faces)
  278. prop = Property(
  279. "rect", PropertyType.FLOAT32, PropertySubType.PIXEL,
  280. None,
  281. "mesh_rect", "The position and size within the layer",
  282. False, True, 4, None, None, []
  283. )
  284. api.add_property(mesh_id, prop)
  285. # x
  286. api.set_property_f32(mesh_id, "rect", 0, 10)
  287. # y
  288. api.set_property_f32(mesh_id, "rect", 1, 10)
  289. # w
  290. #api.set_property_f32(mesh_id, "rect", 2, 20)
  291. code = [["-", ["load", "lw"], ["f32", 10]]]
  292. api.set_property_expr(mesh_id, "rect", 2, code)
  293. # h
  294. #api.set_property_str(mesh_id, "rect", 3, "lh - 10")
  295. code = [["-", ["load", "lh"], ["f32", 10]]]
  296. api.set_property_expr(mesh_id, "rect", 3, code)
  297. api.link_node(mesh_id, layer_id)
  298. def main():
  299. draw()
  300. # DEBUG
  301. print_tree()
  302. #def main():
  303. # if True:
  304. # node_id = api.add_node("foo", SceneNodeType.WINDOW)
  305. # prop = Property(
  306. # "myprop", PropertyType.FLOAT32, PropertySubType.NULL,
  307. # None,
  308. # "myprop", "",
  309. # False, 2, None, None, []
  310. # )
  311. # api.add_property(1, prop)
  312. # api.link_node(node_id, 0)
  313. # api.set_property_f32(1, "myprop", 0, 4.0)
  314. # api.set_property_f32(1, "myprop", 1, 110.0)
  315. # print("val =", api.get_property_value(1, "myprop"))
  316. # for prop in api.get_properties(1):
  317. # print("Property:")
  318. # print(f" name = {prop.name}")
  319. # print(f" type = {prop.type}")
  320. # print(f" subtype = {prop.subtype}")
  321. # print(f" defaults = {prop.defaults}")
  322. # print(f" ui_name = {prop.ui_name}")
  323. # print(f" desc = {prop.desc}")
  324. # print(f" is_null_allowed = {prop.is_null_allowed}")
  325. # print(f" array_len = {prop.array_len}")
  326. # print(f" min_val = {prop.min_val}")
  327. # print(f" max_val = {prop.max_val}")
  328. # print(f" enum_items = {prop.enum_items}")
  329. # print()
  330. # print_tree()
  331. # #garbage_collect()
  332. #
  333. # #app = App()
  334. # #draw_box()
  335. # #draw_rounded_box()
  336. # #draw_cursor()
  337. # #reposition_cursor()
  338. # ##print_tree()
  339. # #app.run()