Newer
Older
Dreamsturbia-Project-IADE-Unity3D / Assets / Editor / StateNode.cs
@Rackday Rackday on 21 Aug 2024 4 KB Project Added
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor.Experimental.GraphView;
  5. using UnityEditor.UIElements;
  6. using UnityEngine.UIElements;
  7. using UnityEditor;
  8. public class StateNode : Node
  9. {
  10. public ObjectField entry;
  11. public List<ObjectField> actions;
  12. public ObjectField exit;
  13. public State state;
  14. public TextField titleInputField;
  15. public Port outputPort;
  16. public Port inputPort;
  17. private VisualElement actionsContainer;
  18. public StateNode(State state)
  19. {
  20. actions = new List<ObjectField>();
  21. this.state = state;
  22. }
  23. public virtual void Initialize(Vector2 pos)
  24. {
  25. SetPosition(new Rect(pos, Vector2.zero));
  26. }
  27. public virtual void Draw()
  28. {
  29. titleInputField = new TextField();
  30. titleInputField.value = "State";
  31. titleContainer.Insert(0,titleInputField);
  32. Button addActionButton = new Button(InsertAction);
  33. addActionButton.text = "Add Action";
  34. mainContainer.Insert(1,addActionButton);
  35. Add(entry);
  36. entry = new ObjectField();
  37. entry.objectType = typeof(Action);
  38. entry.label = "Entry Action";
  39. entry.value = state.entryAction;
  40. extensionContainer.Add(entry);
  41. actionsContainer = new VisualElement();
  42. if (state.actions != null)
  43. {
  44. foreach (Action act in state.actions)
  45. {
  46. ObjectField newObjectField = new ObjectField("Actions");
  47. newObjectField.objectType = typeof(Action);
  48. newObjectField.value = act;
  49. actions.Add(newObjectField);
  50. }
  51. }
  52. foreach (ObjectField action in actions)
  53. {
  54. action.objectType = typeof(Action);
  55. action.label = "Actions";
  56. actionsContainer.Add(action);
  57. }
  58. exit = new ObjectField();
  59. exit.objectType = typeof(Action);
  60. exit.label = "Exit Action";
  61. exit.value = state.exitAction;
  62. extensionContainer.Add(actionsContainer);
  63. extensionContainer.Add(exit);
  64. outputPort = InstantiatePort(Orientation.Horizontal, Direction.Output, Port.Capacity.Multi, typeof(bool));
  65. outputPort.portName = "Output";
  66. inputPort = InstantiatePort(Orientation.Horizontal, Direction.Input, Port.Capacity.Multi, typeof(bool));
  67. inputPort.portName = "Input";
  68. outputContainer.Add(outputPort);
  69. inputContainer.Add(inputPort);
  70. RefreshExpandedState();
  71. }
  72. public VisualElement AddAction(out ObjectField objectField)
  73. {
  74. VisualElement ve = new VisualElement();
  75. ObjectField of = new ObjectField();
  76. of.objectType = typeof(Action);
  77. of.label = "Actions";
  78. ve.Add(of);
  79. Button removeButton = new Button(() =>
  80. {
  81. actionsContainer.Remove(ve);
  82. });
  83. removeButton.text = "Remove";
  84. ve.Add(removeButton);
  85. objectField = of;
  86. return ve;
  87. }
  88. public void InsertAction()
  89. {
  90. ObjectField newObjectField;
  91. VisualElement ve = AddAction(out newObjectField);
  92. actions.Add(newObjectField);
  93. actionsContainer.Add(ve);
  94. }
  95. /* private VisualElement CreateEditorFromNodeData()
  96. {
  97. SerializedObject soEditor = new SerializedObject(state);
  98. var container = new VisualElement();
  99. var it = soEditor.GetIterator();
  100. if (!it.NextVisible(true))
  101. return container;
  102. //Descends through serialized property children & allows us to edit them.
  103. do
  104. {
  105. var propertyField = new PropertyField(it.Copy())
  106. { name = "PropertyField:" + it.propertyPath };
  107. //Bind the property so we can edit the values.
  108. propertyField.Bind(soEditor);
  109. //This ignores the label name field, it's ugly.
  110. if (it.propertyPath == "m_Script" && soEditor.targetObject != null)
  111. {
  112. propertyField.SetEnabled(false);
  113. propertyField.visible = false;
  114. }
  115. container.Add(propertyField);
  116. }
  117. while (it.NextVisible(false));
  118. return container;
  119. }*/
  120. }