__init__.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 main():
  227. if True:
  228. node_id = api.add_node("foo", SceneNodeType.WINDOW)
  229. prop = Property(
  230. "myprop", PropertyType.FLOAT32, PropertySubType.NULL,
  231. None,
  232. "myprop", "",
  233. False, 2, None, None, []
  234. )
  235. api.add_property(1, prop)
  236. api.link_node(node_id, 0)
  237. api.set_property_f32(1, "myprop", 0, 4.0)
  238. api.set_property_f32(1, "myprop", 1, 110.0)
  239. print("val =", api.get_property_value(1, "myprop"))
  240. for prop in api.get_properties(1):
  241. print("Property:")
  242. print(f" name = {prop.name}")
  243. print(f" type = {prop.type}")
  244. print(f" subtype = {prop.subtype}")
  245. print(f" defaults = {prop.defaults}")
  246. print(f" ui_name = {prop.ui_name}")
  247. print(f" desc = {prop.desc}")
  248. print(f" is_null_allowed = {prop.is_null_allowed}")
  249. print(f" array_len = {prop.array_len}")
  250. print(f" min_val = {prop.min_val}")
  251. print(f" max_val = {prop.max_val}")
  252. print(f" enum_items = {prop.enum_items}")
  253. print()
  254. print_tree()
  255. #garbage_collect()
  256. #app = App()
  257. #draw_box()
  258. #draw_rounded_box()
  259. #draw_cursor()
  260. #reposition_cursor()
  261. ##print_tree()
  262. #app.run()