예제 #1
0
파일: Serial.cs 프로젝트: jweather/kneditor
    public static void loadFile()
    {
        string path = FileBrowser.OpenSingleFile("Open File", "", "json");

        if (!File.Exists(path))
        {
            Debug.Log("no such file");
            return;
        }
        string json = File.ReadAllText(path);
        World  w    = JsonUtility.FromJson <World>(json);

        script.the.clearStage();
        foreach (var wn in w.nodes)
        {
            var obj = Node.Create();
            obj.transform.position = wn.position;
            if (script.the.cursor == null)
            {
                script.the.setCursor(obj.gameObject);
            }
        }
        foreach (var wr in w.rods)
        {
            var obj = Rod.Create(wr.size);
            obj.transform.position = wr.position;
            obj.transform.rotation = wr.rotation;
        }
    }
예제 #2
0
파일: script.cs 프로젝트: jweather/kneditor
    void construct(Vector3 direction)
    {
        direction.Normalize();

        // direction is relative to camera's look vector
        direction = camAngles * direction;

        int   size = rodSize;
        float max  = Mathf.Max(Mathf.Abs(direction.x), Mathf.Max(Mathf.Abs(direction.y), Mathf.Abs(direction.z)));

        if (autoDiag && max < 0.9 && size < maxRodSize)
        {
            size++; // diagonal
        }
        Vector3 origin = cursor.transform.position + direction * 10f;
        Vector3 target = origin + direction * rodUnits[size];
        Vector3 rodC   = (origin + target) / 2;

        UndoFrame undo = new UndoFrame(cursor, true);

        // existing rod?
        Collider[] hits = Physics.OverlapSphere(rodC, 5f, rodMask);
        if (hits.Length == 0)
        {
            var rod = Rod.Create(size);
            rod.transform.position = rodC;
            rod.transform.up       = target - origin;
            undo.objs.Add(rod);

            if (symmetry == symMirror)
            {
                // do we need to check for duplicate rods here?
                rod = Rod.Create(size);
                rod.transform.position = Vector3.Reflect(rodC, Vector3.right);
                rod.transform.up       = Vector3.Reflect(target - origin, Vector3.right);
                undo.objs.Add(rod);
            }
            else if (symmetry == sym4Rotate)
            {
                for (float theta = 90f; theta < 360; theta += 90f)
                {
                    rod = Rod.Create(size);
                    rod.transform.position = Quaternion.AngleAxis(theta, Vector3.forward) * rodC;
                    rod.transform.up       = Quaternion.AngleAxis(theta, Vector3.forward) * (target - origin);
                    undo.objs.Add(rod);
                }
            }
        }
        else
        {
            if (Input.GetKey(KeyCode.Delete))
            {
                undo = new UndoFrame(cursor, false);
                undo.objs.Add(hits[0].gameObject);
                undoStack.Push(undo);
                hits[0].gameObject.SetActive(false);

                deleteHandled = true;
                return;
            }
        }

        if (Input.GetKey(KeyCode.Delete))
        {
            // not trying to create something
            return;
        }

        cursor.GetComponent <Node>().autoAssign(); // update old cursor for new connectors

        // existing node?
        hits = Physics.OverlapSphere(target, 5f, nodeMask);
        if (hits.Length == 0)
        {
            var     node = Node.Create();
            Vector3 pos  = target + direction * 10f;
            node.transform.position = pos;
            if (!Input.GetKey(KeyCode.LeftShift))
            {
                setCursor(node);
            }

            undo.objs.Add(node);
            if (symmetry == symMirror)
            {
                Vector3 symPos = Vector3.Reflect(pos, Vector3.right);
                if (symPos != pos)
                {
                    node = Node.Create(symPos);
                    if (node)
                    {
                        undo.objs.Add(node);
                    }
                }
            }
            else if (symmetry == sym4Rotate)
            {
                for (float theta = 90f; theta < 360; theta += 90f)
                {
                    Vector3 symPos = Quaternion.AngleAxis(theta, Vector3.forward) * pos;
                    if (symPos != pos)
                    {
                        node = Node.Create(symPos);
                        if (node)
                        {
                            undo.objs.Add(node);
                        }
                    }
                }
            }
        }
        else
        {
            if (!Input.GetKey(KeyCode.LeftShift))
            {
                setCursor(hits[0].gameObject);
            }
        }

        cursor.GetComponent <Node>().autoAssign(); // update new cursor for new connectors

        if (undo.objs.Count > 0)
        {
            undoStack.Push(undo);
        }
    }