Esempio n. 1
0
        public void ReloadFromData(SplineCanvasData data)
        {
            // Clear everything
            Reset();
            bulbs.Clear();

            foreach (BulbData bData in data.bulbs)
            {
                // Make bulbs at all the saved positions with the saved radii
                DraggablePoint pt = AddBulb(bData.position.GetVector());
                pt.SetSize(bData.radius);
                pt.containingCanvas = this;
                if (bData.type == PointType.Spline)
                {
                    pt.SwitchBulbType(PointType.Bulb);
                }
                else
                {
                    pt.SwitchBulbType(bData.type);
                }
            }

            foreach (SplineData sData in data.splines)
            {
                // Convert the serialized vectors to Unity vectors
                List <Vector3> posList = new List <Vector3>();
                foreach (V3Serialize v3s in sData.points)
                {
                    posList.Add(v3s.GetVector());
                }

                // Make a spline with the same control points
                CatmullRomSpline spline = CatmullRomSpline.SplineOfPoints(posList);
                AddExistingSpline(spline);

                // Set the begin/end bulbs
                if (sData.startBulb >= 0)
                {
                    spline.StartBulb = bulbs[sData.startBulb];
                }
                if (sData.endBulb >= 0)
                {
                    spline.EndBulb = bulbs[sData.endBulb];
                }
            }
        }
        // Update is called once per frame
        void Update()
        {
            if (Input.GetButtonDown("ToggleMode"))
            {
                if (currentMode == DesignerMode.Shell)
                {
                    currentMode   = DesignerMode.Curve;
                    modeText.text = "Curve mode";
                }
                else if (currentMode == DesignerMode.Curve)
                {
                    currentMode   = DesignerMode.Curvature;
                    modeText.text = "Curvature mode";
                }
                else if (currentMode == DesignerMode.Curvature)
                {
                    currentMode   = DesignerMode.Shell;
                    modeText.text = "Shell mode";
                }
            }

            RaycastHit hitInfo = new RaycastHit();

            if (Input.GetMouseButtonDown(0) && RaycastShells(Input.mousePosition, out hitInfo))
            {
                TelescopeShell selection   = hitInfo.collider.GetComponent <TelescopeShell>();
                DraggablePoint draggablePt = hitInfo.collider.GetComponent <DraggablePoint>();

                if (selection)
                {
                    SelectShell(selection);
                }

                else if (draggablePt)
                {
                    draggable     = draggablePt;
                    selectedDepth = Camera.main.WorldToScreenPoint(draggablePt.transform.position).z;
                }
            }

            else if (Input.GetKeyDown("r") && RaycastShells(Input.mousePosition, out hitInfo))
            {
                DraggablePoint draggablePt = hitInfo.collider.GetComponent <DraggablePoint>();

                if (draggablePt && draggablePt.parentSpline)
                {
                    Debug.Log("Reverse " + draggablePt.parentSpline.name);
                    draggablePt.parentSpline.Reverse();
                }
            }


            else if (Input.GetKeyDown("j") && RaycastShells(Input.mousePosition, out hitInfo))
            {
                DraggablePoint draggablePt = hitInfo.collider.GetComponent <DraggablePoint>();

                if (draggablePt)
                {
                    if (draggablePt.Type == PointType.Bulb)
                    {
                        draggablePt.SwitchBulbType(PointType.EmptyJuncture);
                    }
                    else if (draggablePt.Type == PointType.EmptyJuncture)
                    {
                        draggablePt.SwitchBulbType(PointType.Bulb);
                    }
                }
            }

            else if (Input.GetMouseButton(0) && draggable)
            {
                Vector3 clickPos = Input.mousePosition;
                clickPos.z = selectedDepth;
                Vector3 worldPos = Camera.main.ScreenToWorldPoint(clickPos);

                DraggablePoint intersectedBulb = splineCanvas.IntersectedBulb(worldPos);

                if (draggable.Type == PointType.Spline && draggable.IsEndPoint() && intersectedBulb)
                {
                    draggable.AttachToBulb(intersectedBulb);
                }
                else
                {
                    draggable.Move(worldPos);
                }
            }

            else if (Input.GetMouseButtonDown(1) && RaycastShells(Input.mousePosition, out hitInfo))
            {
                Vector3        clickPos = Input.mousePosition;
                DraggablePoint clicked  = hitInfo.collider.GetComponent <DraggablePoint>();

                if (clicked)
                {
                    if (Input.GetKey("left ctrl"))
                    {
                        clicked.Delete();
                    }
                    else
                    {
                        clicked.Duplicate();
                    }
                }

                // Store the eye-space position of the click.
                // Use eye space because we always want moving up/down to correspond
                // to bigger or smaller, regardless of camera orientation.
                lastMousePos   = clickPos;
                lastMousePos.z = selectedDepth;
                lastMousePos   = Camera.main.ScreenToViewportPoint(lastMousePos);
            }

            else if (Input.GetMouseButtonDown(2) && RaycastShells(Input.mousePosition, out hitInfo))
            {
                Vector3 clickPos = Input.mousePosition;

                // Store the eye-space position of the click.
                // Use eye space because we always want moving up/down to correspond
                // to bigger or smaller, regardless of camera orientation.
                lastMousePos   = clickPos;
                lastMousePos.z = selectedDepth;
                lastMousePos   = Camera.main.ScreenToViewportPoint(lastMousePos);
            }

            else if (Input.mouseScrollDelta.y != 0 && RaycastShells(Input.mousePosition, out hitInfo))
            {
                DraggablePoint draggablePt = hitInfo.collider.GetComponent <DraggablePoint>();
                if (draggablePt)
                {
                    float change = Input.mouseScrollDelta.y * 0.05f;
                    draggablePt.Resize(change);
                }
            }

            else if (Input.GetKeyDown("delete") && RaycastShells(Input.mousePosition, out hitInfo))
            {
                DraggablePoint draggablePt = hitInfo.collider.GetComponent <DraggablePoint>();

                if (draggablePt)
                {
                    draggablePt.Delete();
                }
            }

            else if (Input.GetKeyDown("b") && RaycastShells(Input.mousePosition, out hitInfo))
            {
                DraggablePoint draggablePt = hitInfo.collider.GetComponent <DraggablePoint>();

                if (draggablePt)
                {
                    draggablePt.ReplaceWithBulb();
                }
            }

            else if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(2))
            {
                draggable = null;
            }

            else if (Input.GetButtonDown("Cancel"))
            {
                Deselect();
            }

            else if (Input.GetButtonDown("Submit"))
            {
                Debug.Log("submit");
            }

            shootTime += Time.deltaTime;
            if (shootTime > shootDelay && Input.GetKey("z"))
            {
                ShootSphere();
            }
        }