Пример #1
0
 // saves the changes made to the currently placed generated level to the .levelData format
 public static void SaveLevel()
 {
     // save it
     editLevel = CreateLevelData();
     UndoManager.AddSavePoint(editLevel);
 }
Пример #2
0
        // Input Control, listens for key input, mouse/touch input and switches EditorModes / selects objects / adds vertices
        private void Update()
        {
#if UNITY_EDITOR
            if (Input.GetKeyDown(KeyCode.KeypadMinus))
            {
                if (cam.orthographicSize + keyboardZoomStep <= maxSize)
                    cam.orthographicSize += keyboardZoomStep;
            }
            else if (Input.GetKeyDown(KeyCode.KeypadPlus))
            {
                if (cam.orthographicSize - keyboardZoomStep >= minSize)
                    cam.orthographicSize -= keyboardZoomStep;
            }
            else if (Input.GetKeyDown(KeyCode.Return))
            {
                if (!UIObjectPreferences.menuOpen)
                {
                    if (LevelEditor.TryTestLevel())
                    {
                        SoundManager.ButtonClicked();
                        // animations
                    }
                }
            }
#endif
#if UNITY_ANDROID
            // No items or verticies get curretly dragged and no menu is open, thus listen for input
            if (!vertexDragged && !UIObjectPreferences.menuOpen)
            {
                // If there are two touches on the device manage the editor view (zooming/moving)
                if (Input.touchCount == 2 && UILevelEditor._instance.inventoryScrollRect.velocity.x == 0)
                {
                    // Store both touches.
                    Touch touchZero = Input.GetTouch(0);
                    Touch touchOne = Input.GetTouch(1);

                    // Find the position in the previous frame of each touch.
                    Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
                    Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;

                    // Find the magnitude of the vector (the distance) between the touches in each frame.
                    float prevTouchMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
                    float touchMag = (touchZero.position - touchOne.position).magnitude;
                    float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;

                    // Find the difference in the distances between each frame.
                    float deltaMagnitudeDiff = prevTouchMag - touchMag;

                    // fingers moving fast towards each other or fast away from each other => zooming
                    if (touchMag + panThreshold < prevTouchMag || touchMag > prevTouchMag + panThreshold)
                    {
                        // Change the orthographic size based on the change in distance between the touches.
                        float sizeToChange = deltaMagnitudeDiff * zoomSpeed;
                        if ((sizeToChange > 0 && (cam.orthographicSize + sizeToChange) < maxSize) || (sizeToChange < 0 && (cam.orthographicSize + sizeToChange) > minSize))
                        {
                            cam.orthographicSize += sizeToChange;
                        }
                    }
                    // panning => move the camera
                    else
                    {
                        Vector3 touchDeltaPosition = new Vector3(-touchZero.deltaPosition.x * Time.deltaTime * 500, -touchZero.deltaPosition.y * Time.deltaTime * 500, 0);
                        transform.position += touchDeltaPosition;
                        //transform.Translate(touchDeltaPosition.x * Time.deltaTime, touchDeltaPosition.y * Time.deltaTime, 0);
                    }
                }
                // only one finger touches the screen, control object selecting/vertex adding
                else if (Input.touchCount == 1)
                {
                    Touch touch = Input.GetTouch(0);

                    // touch click = > handle selection/ vertex adding
                    if (touch.phase == TouchPhase.Began)
                    {
                        Vector3 position = Camera.main.ScreenToWorldPoint(touch.position);

                        LevelObject l = GetLevelObjectAt(position);
                        if (l != null && l == LevelEditor.selectedObject && LevelEditor.selectedObject.objectType != LevelObject.ObjectType.moveArea)
                        {
                            objectDragged = true;
                        }

                        ClickHandler(position);
                    }

                    // touch drag => handle object movement
                    else if (touch.phase == TouchPhase.Moved)
                    {
                        // an object is selected, and it is not the movearea
                        if (objectDragged && LevelEditor.selectedObject != null && LevelEditor.selectedObject.objectType != LevelObject.ObjectType.moveArea && UILevelEditor._instance.inventoryScrollRect.velocity.x == 0)
                        {
                            Vector3 position = Camera.main.ScreenToWorldPoint(touch.position);
                            //position.y = Screen.height - position.y;
                            position.z = LevelEditor.selectedObject.transform.position.z;
                            if (VertHelper.IsInsideMesh(LevelPlacer.generatedLevel.moveArea.meshFilter.mesh, Vector3.zero, LevelPlacer.generatedLevel.moveArea.transform.InverseTransformPoint(position)))
                                LevelEditor.selectedObject.transform.position = position;
                        }
                    }

                    // touch drag end => snap the dragged object and save the changes that were made
                    else if (touch.phase == TouchPhase.Ended)
                    {
                        // an object is selected, and it is not the movearea
                        if (objectDragged && LevelEditor.selectedObject != null && LevelEditor.selectedObject.objectType != LevelObject.ObjectType.moveArea)
                        {
                            // move the selected object
                            Vector3 position = LevelEditor.selectedObject.transform.position;
                            position = VertHelper.Snap(position, false);
                            if (LevelEditor.selectedObject.transform.position != position)
                            {
                                LevelEditor.selectedObject.transform.position = position;
                                UndoManager.AddUndoPoint();
                            }
                            //itemDragged = false;
                        }
                        objectDragged = false;
                    }
                }
#if UNITY_EDITOR
                // both mouse buttons are held down => drag the editor view
                else if (Input.GetMouseButton(0) && Input.GetMouseButton(1) && UILevelEditor._instance.inventoryScrollRect.velocity.x == 0)
                {
                    Vector3 position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                    Vector3 deltaPos = new Vector3(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")) * 40f;
                    deltaPos.z = 0F;
                    transform.position += deltaPos;
                }
                // one mouse button got clicked => handle selection/vertex adding
                else if (Input.GetMouseButtonDown(0))
                {
                    Vector3 position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                    ClickHandler(position);
                }
                // one mouse button is held down (aka "dragged") => try to move selected objects
                else if (Input.GetMouseButton(0) && UILevelEditor._instance.inventoryScrollRect.velocity.x == 0)
                {
                    // an object is selected, and it is not the movearea
                    if (LevelEditor.selectedObject != null && LevelEditor.selectedObject.objectType != LevelObject.ObjectType.moveArea)
                    {
                        //itemDragged = true;
                        // move the selected object
                        Vector3 position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                        Vector3 deltaPos = new Vector3(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")) * 40f;
                        deltaPos.z = 0F;

                        Vector3 newPos = LevelEditor.selectedObject.transform.position + deltaPos;
                        if (VertHelper.IsInsideMesh(LevelPlacer.generatedLevel.moveArea.meshFilter.mesh, Vector3.zero, LevelPlacer.generatedLevel.moveArea.transform.InverseTransformPoint(newPos)))
                            LevelEditor.selectedObject.transform.position = newPos;
                    }
                }
                // a mouse drag has ended, snap the dragged object and save the changes that were made
                else if (Input.GetMouseButtonUp(0))
                {
                    // an object is selected, and it is not the movearea
                    if (LevelEditor.selectedObject != null && LevelEditor.selectedObject.objectType != LevelObject.ObjectType.moveArea)
                    {
                        // move the selected object
                        Vector3 position = LevelEditor.selectedObject.transform.position;
                        position = VertHelper.Snap(position, false);
                        if (LevelEditor.selectedObject.transform.position != position)
                        {
                            LevelEditor.selectedObject.transform.position = position;
                            UndoManager.AddUndoPoint();
                        }

                        //itemDragged = false;
                    }
                }
#endif
            }

            // lets handle special preference input when a menu is open (yaaay)
            else if (UIObjectPreferences.menuOpen)
            {
                if (UIObjectPreferences.openedMenuType == LevelObject.ObjectType.portal)
                {
                    if (Input.touchCount == 1)
                    {
                        Touch touch = Input.GetTouch(0);
                        // touch click = > handle selection/ vertex adding
                        if (touch.phase == TouchPhase.Began)
                        {
                            Vector3 position = Camera.main.ScreenToWorldPoint(touch.position);
                            ClickHandler(position);
                        }
                    }
                    else if (Input.GetMouseButtonDown(0))
                    {
                        Vector3 position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                        ClickHandler(position);
                    }
                }
            }
        }