Beispiel #1
0
    /// <summary>
    /// Adds an object to the selection, changes the shader and shows and hides the appropriate canvas
    /// </summary>
    /// <param name="go">The Gameobject which is added</param>
    protected void AddObjectToSecundarySelection(GameObject go, bool invokeSelectionChangeEvent = true)
    {
        if (secundarySelectedObjects.Contains(go))
        {
            return;
        }

        Selectable selectable = go.GetComponent <Selectable>();

        if (selectable == null)
        {
            throw new Exception("You tried to Add an GameObject which hans't a Selectable component on it: (Gameobject: " + go + ")");
        }
        selectable.SetShader(secundary: true);

        secundarySelectedObjects.Add(go);

        if (SecundarySelectionContainsListeners.ContainsKey(go))
        {
            foreach (var l in SecundarySelectionContainsListeners[go])
            {
                l.Invoke(go, secundarySelectedObjects.AsReadOnly());
            }
        }
        if (invokeSelectionChangeEvent && ChangeSecundarySelectionEvent != null)
        {
            ChangeSecundarySelectionEvent.Invoke(Array.AsReadOnly(new GameObject[] { go }), Array.AsReadOnly(new GameObject[] { }), SecundarySelectedObjects);
        }
    }
Beispiel #2
0
 /// <summary>
 /// Remove an object from the selection and reverts the shader
 /// </summary>
 /// <param name="go"></param>
 /// <param name="invokeSelectionChangeEvent"></param>
 protected void RemoveObjectFromSecundarySelection(GameObject go, bool invokeSelectionChangeEvent = true)
 {
     if (secundarySelectedObjects.Remove(go))
     {
         go.GetComponent <Selectable>().RevertShader();
         if (invokeSelectionChangeEvent && ChangeSecundarySelectionEvent != null)
         {
             ChangeSecundarySelectionEvent.Invoke(Array.AsReadOnly(new GameObject[] { }), Array.AsReadOnly(new GameObject[] { go }), SecundarySelectedObjects);
         }
     }
 }
Beispiel #3
0
 /// <summary>
 /// Clears the selection and resets the shaders
 /// </summary>
 public void ClearSecundarySelection()
 {
     secundarySelectedObjects.ForEach(o =>
     {
         if (o != null)
         {
             o.GetComponent <Selectable>().RevertShader();
         }
     });
     if (ChangeSecundarySelectionEvent != null)
     {
         ChangeSecundarySelectionEvent.Invoke(Array.AsReadOnly(new GameObject[] { }), SecundarySelectedObjects, new List <GameObject>().AsReadOnly());
     }
     secundarySelectedObjects.Clear();
 }
Beispiel #4
0
    void Update()
    {
        bool mousePressed = Input.GetButton("LeftMouse") || Input.GetButton("RightMouse");
        bool mouseDown    = Input.GetButtonDown("LeftMouse") || Input.GetButtonDown("RightMouse");
        bool mouseUp      = Input.GetButtonUp("LeftMouse") || Input.GetButtonUp("RightMouse");
        bool leftMouse    = Input.GetButton("LeftMouse") || Input.GetButtonDown("LeftMouse") || Input.GetButtonUp("LeftMouse");

        //switches between the modes depending on the controll key
        if (SelectionMode != SelectionModeEnum.MultipleAdd && Input.GetButton("Control"))
        {
            SelectionMode = SelectionModeEnum.MultipleAdd;
        }
        if (SelectionMode == SelectionModeEnum.MultipleAdd && !Input.GetButton("Control"))
        {
            SelectionMode = SelectionModeEnum.MultipleClear;
        }

        //Clears the selection if the selection mode isn't addative
        if (mouseDown && SelectionMode != SelectionModeEnum.MultipleAdd && leftMouse)
        {
            ClearSelection();
            ClearSecundarySelection();
        }
        else if (mouseDown && SelectionMode != SelectionModeEnum.MultipleAdd && !leftMouse)
        {
            ClearSecundarySelection();
        }

        //saves the start position of the mouse for the selection box
        if (mouseDown && !multipleSelectionActive)
        {
            selectionStartMousePos = Input.mousePosition;
        }

        //Checks if the mouse position has changed since the user clicked the LeftMouse Button. If the distance is greater then 0.1 (a threadshold) then multiselecting is activated
        if (mousePressed &&
            Mathf.Abs(Vector2.Distance(selectionStartMousePos, Input.mousePosition)) > 0.1 && !multipleSelectionActive)
        {
            multipleSelectionActive = true;
            SelectionMode           = SelectionModeEnum.MultipleClear;   //the default selection mode
        }

        //Updates the cursor texture
        if (SelectionMode == SelectionModeEnum.MultipleAdd)
        {
            Cursor.SetCursor(cursorAddSelectionTexture, Vector2.zero, CursorMode.Auto);
        }
        else
        {
            Cursor.SetCursor(cursorSelectionTexture, Vector2.zero, CursorMode.Auto);
        }


        //In here is the main multi selection logic
        //here is the selecting and the setting of the shader logic
        if (multipleSelectionActive && mouseUp)
        {
            //precalculates the bounds
            Bounds viewportBounds = GetViewportBounds(camera, selectionStartMousePos, Input.mousePosition);
            var    addedGos       = new List <GameObject>();
            var    removedGos     = new List <GameObject>();
            //goes through every object which can be selected (
            foreach (var go in Selectable.SelectGameObjects)
            {
                if (go == null)
                {
                    continue;
                }
                if (viewportBounds.Contains(camera.WorldToViewportPoint(go.transform.position)))
                {
                    if (leftMouse)
                    {
                        if (selectedObjects.Contains(go))
                        {
                            RemoveObjectFromSelection(go, false);
                            removedGos.Add(go);
                        }
                        else
                        {
                            AddObjectToSelection(go, false);
                            addedGos.Add(go);
                        }
                    }
                    else
                    {
                        if (SecundarySelectedObjects.Contains(go))
                        {
                            RemoveObjectFromSecundarySelection(go, false);
                            removedGos.Add(go);
                        }
                        else
                        {
                            AddObjectToSecundarySelection(go, false);
                            addedGos.Add(go);
                        }
                    }
                }
            }

            if (ChangeSelectionEvent != null)
            {
                if (leftMouse)
                {
                    ChangeSelectionEvent.Invoke(addedGos.AsReadOnly(), removedGos.AsReadOnly(), SelectedObjects);
                }
                else
                {
                    ChangeSecundarySelectionEvent.Invoke(addedGos.AsReadOnly(), removedGos.AsReadOnly(), SecundarySelectedObjects);
                }
            }
        }


        //hides the last ui canvas
        if (mouseDown && !eventSystem.IsPointerOverGameObject() && leftMouse)
        {
            HideObjectUi();
        }

        //Here is the single selection in
        if (mouseUp && !multipleSelectionActive)
        {
            Ray        ray = camera.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            Debug.DrawRay(ray.origin, ray.direction * 100, Color.blue, 10);
            if (Physics.Raycast(ray, out hit, 1000.0f) && !EventSystem.current.IsPointerOverGameObject())
            {
                GameObject hittedGo = hit.collider.gameObject;

                if (hittedGo.GetComponent <Selectable>() != null)
                {
                    if (selectedObjects.Contains(hittedGo))
                    {
                        if (leftMouse)
                        {
                            RemoveObjectFromSelection(hittedGo);
                        }
                        else
                        {
                            RemoveObjectFromSecundarySelection(hittedGo);
                        }
                    }
                    else
                    {
                        if (leftMouse)
                        {
                            AddObjectToSelection(hittedGo);
                        }
                        else
                        {
                            AddObjectToSecundarySelection(hittedGo);
                        }
                    }
                }
                else
                {
                    if (!leftMouse)
                    {
                        selectedPosition = hit.point;
                        if (ChangePositionEvent != null)
                        {
                            ChangePositionEvent.Invoke(selectedPosition);
                        }
                    }
                }
            }
            else
            {
                Debug.LogWarning("Didn't hit terain with raycast!");
            }
        }

        //disables the multiple selection mode
        if (mouseUp && multipleSelectionActive)
        {
            multipleSelectionActive = false;
            SelectionMode           = SelectionModeEnum.Single;
        }
    }