Newer
Older
Dreamsturbia-Project-IADE-Unity3D / Assets / Editor / StateNode.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.Experimental.GraphView;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
using UnityEditor;

public class StateNode : Node
{
    public ObjectField entry;
    public List<ObjectField> actions;
    public ObjectField exit;

    public State state;
    public TextField titleInputField;
    public Port outputPort;
    public Port inputPort;

    private VisualElement actionsContainer;
    public StateNode(State state)
    {
        actions = new List<ObjectField>();
        this.state = state;
    }

    public virtual void Initialize(Vector2 pos)
    {
        SetPosition(new Rect(pos, Vector2.zero));
    }

    public virtual void Draw()
    {
        titleInputField = new TextField();
        titleInputField.value = "State";

        titleContainer.Insert(0,titleInputField);
        Button addActionButton = new Button(InsertAction);
        addActionButton.text = "Add Action";
        mainContainer.Insert(1,addActionButton);
        Add(entry);

        entry = new ObjectField();
        entry.objectType = typeof(Action);
        entry.label = "Entry Action";
        entry.value = state.entryAction;
        extensionContainer.Add(entry);
        actionsContainer = new VisualElement();

        if (state.actions != null)
        {
            foreach (Action act in state.actions)
            {
                ObjectField newObjectField = new ObjectField("Actions");
                newObjectField.objectType = typeof(Action);
                newObjectField.value = act;
                actions.Add(newObjectField);
            }
        }

        foreach (ObjectField action in actions)
        {
            action.objectType = typeof(Action);
            action.label = "Actions";
            actionsContainer.Add(action);


        }


        exit = new ObjectField();
        exit.objectType = typeof(Action);
        exit.label = "Exit Action";
        exit.value = state.exitAction;
        extensionContainer.Add(actionsContainer);
        extensionContainer.Add(exit);


        outputPort = InstantiatePort(Orientation.Horizontal, Direction.Output, Port.Capacity.Multi, typeof(bool));
        outputPort.portName = "Output";
        inputPort = InstantiatePort(Orientation.Horizontal, Direction.Input, Port.Capacity.Multi, typeof(bool));
        inputPort.portName = "Input";
        
        outputContainer.Add(outputPort);
        inputContainer.Add(inputPort);
        RefreshExpandedState();
        
    }

    public VisualElement AddAction(out ObjectField objectField)
    {
        VisualElement ve = new VisualElement();
        ObjectField of = new ObjectField();
        of.objectType = typeof(Action);
        of.label = "Actions";
        ve.Add(of);

        Button removeButton = new Button(() =>
        {
            actionsContainer.Remove(ve);

        });
        removeButton.text = "Remove";
        ve.Add(removeButton);

        objectField = of;
        return ve;
    }

    public void InsertAction()
    {
        ObjectField newObjectField;
        VisualElement ve = AddAction(out newObjectField);
        actions.Add(newObjectField);
        actionsContainer.Add(ve);
      

    }

    /*  private VisualElement CreateEditorFromNodeData()
      {
          SerializedObject soEditor = new SerializedObject(state);
          var container = new VisualElement();

          var it = soEditor.GetIterator();
          if (!it.NextVisible(true))
              return container;

          //Descends through serialized property children & allows us to edit them.
          do
          {
              var propertyField = new PropertyField(it.Copy())
              { name = "PropertyField:" + it.propertyPath };

              //Bind the property so we can edit the values.
              propertyField.Bind(soEditor);
              //This ignores the label name field, it's ugly.
              if (it.propertyPath == "m_Script" && soEditor.targetObject != null)
              {
                  propertyField.SetEnabled(false);
                  propertyField.visible = false;
              }

              container.Add(propertyField);
          }
          while (it.NextVisible(false));

          return container;
      }*/




}