print_tree.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from .api import Api, PropertyType, SceneNodeType, CallArgType, Expr
  2. def print_tree(api, node_path="/", depth=None):
  3. print(node_path)
  4. print_node_info(api, node_path, depth, indent=1)
  5. def print_node_info(api, parent_path, depth, indent):
  6. if indent - 1 == depth:
  7. return
  8. ws = " "*4*indent
  9. for (child_name, child_id, child_type) in api.get_children(parent_path):
  10. match child_type:
  11. case SceneNodeType.ROOT:
  12. child_type = "root"
  13. case SceneNodeType.WINDOW:
  14. child_type = "window"
  15. case SceneNodeType.WINDOW_INPUT:
  16. child_type = "window_input"
  17. case SceneNodeType.KEYBOARD:
  18. child_type = "keyboard"
  19. case SceneNodeType.MOUSE:
  20. child_type = "mouse"
  21. case SceneNodeType.LAYER:
  22. child_type = "layer"
  23. case SceneNodeType.OBJECT:
  24. child_type = "object"
  25. case SceneNodeType.VECTOR_ART:
  26. child_type = "vector_art"
  27. case SceneNodeType.TEXT:
  28. child_type = "text"
  29. case SceneNodeType.TEXTURE:
  30. child_type = "texture"
  31. case SceneNodeType.FONTS:
  32. child_type = "fonts"
  33. case SceneNodeType.FONT:
  34. child_type = "font"
  35. case SceneNodeType.CHAT_VIEW:
  36. child_type = "chat_view"
  37. case SceneNodeType.BUTTON:
  38. child_type = "button"
  39. case SceneNodeType.SETTING:
  40. child_type = "setting"
  41. desc = f"{ws}{child_name}:{child_id}/"
  42. desc += " "*(50 - len(desc))
  43. desc += f"[{child_type}]"
  44. print(desc)
  45. if parent_path == "/":
  46. child_path = "/" + child_name
  47. else:
  48. child_path = parent_path + "/" + child_name
  49. print_node_info(api, child_path, depth, indent+1)
  50. for prop in api.get_properties(parent_path):
  51. prop_val = api.get_property_value(parent_path, prop.name)
  52. def fmt_str(pv):
  53. return pv if isinstance(pv, Expr) else f"\"{pv}\""
  54. def fmt_f32(pv):
  55. return pv if isinstance(pv, Expr) else f"{pv:.2f}"
  56. if prop.type == PropertyType.STR:
  57. prop_val = "[" + ", ".join(fmt_str(pv) for pv in prop_val) + "]"
  58. elif prop.type == PropertyType.FLOAT32:
  59. prop_val = "[" + ", ".join(fmt_f32(pv) for pv in prop_val) + "]"
  60. if len(prop_val) == 1:
  61. prop_val = prop_val[0]
  62. prop_val = f" = {prop_val}"
  63. prop_type = PropertyType.to_str(prop.type)
  64. print(f"{ws}{prop.name}: {prop_type}{prop_val}")
  65. if prop.depends:
  66. print(f"{ws} depends: {prop.depends}")
  67. for sig in api.get_signals(parent_path):
  68. print(f"{ws}~{sig}")
  69. for slot_name, slot_id in api.get_slots(parent_path, sig):
  70. print(f"{ws}- '{slot_name}' ({slot_id})")
  71. for method_name in api.get_methods(parent_path):
  72. args, results = api.get_method(parent_path, method_name)
  73. args = [f"{name}: " + CallArgType.to_str(typ) for (name, _, typ) in args]
  74. results = [f"{name}: " + CallArgType.to_str(typ) for (name, _, typ) in results]
  75. method_str = f"{method_name}(" + ", ".join(args) + ") -> (" + ", ".join(results) + ")"
  76. print(f"{ws}{method_str}")