Пример #1
0
 private void Awake()
 {
     if (instance != null)
     {
         Debug.LogError("Too many WorldCreatorCursor instances!");
     }
     instance        = this;
     cursorModeCount = System.Enum.GetValues(typeof(WCCursorMode)).Length;
 }
Пример #2
0
    public void PollPathDrawing()
    {
        Vector2 pos = WorldCreatorCursor.GetMousePos();

        if (pathNodes == null)
        {
            pathNodes = new List <Path.Node>();
        }

        if (Input.GetMouseButton(0))
        {
            if (dragging)
            {
                selectedNode.position = pos;
                UpdateMesh();
            }
        }

        if (Input.GetMouseButtonDown(0))
        {
            int  closest = GetClosestNodeIndex(pos);
            bool grabbed = false;
            if (closest >= 0)
            {
                float d = (pathNodes[closest].position - pos).sqrMagnitude;

                if (d < maxClickDistance * maxClickDistance)
                {
                    Debug.Log("grabbed");
                    grabbed               = true;
                    dragging              = true;
                    selectedNode          = pathNodes[closest];
                    selectedNode.position = pos;
                    UpdateMesh();
                }
            }

            if (!grabbed)
            {
                AddPoint(pos);
            }
        }
        else if (Input.GetMouseButtonUp(0))
        {
            int closest = GetClosestNodeIndex(pos, selectedNode != null ? selectedNode.ID : -1);

            if (closest >= 0)
            {
                float d = (pathNodes[closest].position - pos).sqrMagnitude;

                if (d < maxClickDistance * maxClickDistance && dragging)
                {
                    Path.Node from = selectedNode;
                    Path.Node to   = pathNodes[closest];

                    for (int i = 0; i < pathNodes.Count; i++)
                    {
                        for (int j = 0; j < pathNodes[i].children.Count; j++)
                        {
                            if (pathNodes[i].children[j] == from.ID)
                            {
                                pathNodes[i].children[j] = to.ID;
                            }
                        }
                    }

                    to.children.AddRange(from.children);

                    for (int i = 0; i < to.children.Count; i++)
                    {
                        if (to.children[i] == to.ID)
                        {
                            to.children.RemoveAt(i--);
                            continue;
                        }

                        for (int j = 0; j < to.children.Count; j++)
                        {
                            if (i != j && to.children[i] == to.children[j])
                            {
                                to.children.RemoveAt(j--);
                            }
                        }
                    }

                    pathNodes.Remove(from);

                    Debug.Log($"Combined {from.ID} to {to.ID}");

                    CleanPath();
                    UpdateMesh();
                }
            }

            dragging     = false;
            selectedNode = null;
        }

        if (Input.GetMouseButtonUp(1))
        {
            int closest = GetClosestNodeID(pos);

            if (closest >= 0)
            {
                RemoveNode(closest);
                UpdateMesh();
                Debug.Log($"Node {closest} removed!");
            }
        }

        if (Input.GetKeyDown(KeyCode.Return))
        {
            var pathData = new NodeEditorFramework.Standard.PathData();
            pathData.waypoints = new List <NodeEditorFramework.Standard.PathData.Node>();
            for (int i = 0; i < pathNodes.Count; i++)
            {
                pathData.waypoints.Add(new NodeEditorFramework.Standard.PathData.Node()
                {
                    ID       = pathNodes[i].ID,
                    position = pathNodes[i].position,
                    children = pathNodes[i].children
                });
            }

            WorldCreatorCursor.finishPath.Invoke(pathData);
            WorldCreatorCursor.instance.SetMode(WorldCreatorCursor.originalCursorMode);

            Clear();
        }
    }