public static bool UpdateSelection(SelectionType selectionType, HashSet <SurfaceReference> surfaces)
        {
            bool modified = false;

            if (selectionType == SelectionType.Replace)
            {
                modified = DeselectAllInternal();
            }

            // TODO: handle replace in a single step (using 'Set') to detect modifications

            if (surfaces != null)
            {
                if (selectionType == SelectionType.Subtractive)
                {
                    modified = DeselectInternal(surfaces) || modified;
                }
                else
                {
                    modified = SelectInternal(surfaces) || modified;
                }
            }

            UnityEditor.Selection.selectionChanged -= OnSelectionChanged;
            UnityEditor.Selection.objects           = SelectedGameObjects.ToArray();
            UnityEditor.Selection.selectionChanged += OnSelectionChanged;
            if (modified && selectionChanged != null)
            {
                selectionChanged.Invoke();
            }
            return(modified);
        }
Example #2
0
        private IGameObject[] GetSelectedObjects(Vector2?mousePos = null)
        {
            IGameObject[] objs;

            if (mousePos == null)
            {
                objs = GameObject.FindAll(Utils.RenderableRectangle(SelectionBounds), _world.ActiveCamera.Position, _world.ActiveCamera.InvZoom);
            }
            else
            {
                IGameObject obj = GameObject.GameObjects.FirstOrDefault(o => o.BoundsF.Contains((Vector2)mousePos));
                if (obj == null)
                {
                    objs = new IGameObject[0];
                }
                else
                if (SelectedGameObjects.Contains(obj))
                {
                    objs = SelectedGameObjects;
                }
                else
                {
                    objs = new [] { obj }
                };
            }

            // Update selected objects variable in console
            _console.SetSelectedObjects(objs);

            return(objs);
        }
        public static bool UpdateSelection(SelectionType selectionType, HashSet <SurfaceReference> surfaces)
        {
            bool modified = false;

            if (selectionType == SelectionType.Replace)
            {
                modified = DeselectAllInternal();
            }

            // TODO: handle replace in a single step (using 'Set') to detect modifications

            if (surfaces != null)
            {
                if (selectionType == SelectionType.Subtractive)
                {
                    modified = DeselectInternal(surfaces) || modified;
                }
                else
                {
                    modified = SelectInternal(surfaces) || modified;
                }
            }

            UnityEditor.Selection.selectionChanged -= OnSelectionChanged;

            if (SelectedGameObjects.Count == 0)
            {
                // To prevent the EditorTool from exiting the moment we deselect all surfaces, we leave one object 'selected'
                var selected = UnityEditor.Selection.GetFiltered <ChiselNode>(SelectionMode.Deep | SelectionMode.Editable);
                if (selected.Length > 0)
                {
                    UnityEditor.Selection.activeObject = selected[0];
                }
            }
            else
            {
                UnityEditor.Selection.objects = SelectedGameObjects.ToArray();
            }

            UnityEditor.Selection.selectionChanged += OnSelectionChanged;
            if (modified && selectionChanged != null)
            {
                selectionChanged.Invoke();
            }
            return(modified);
        }
Example #4
0
        public override void Update(GameTime gameTime)
        {
            Frame++;
            ElapsedTime = gameTime;

            base.Update(gameTime);

            // Update overlays
            foreach (var child in _gui.Screen.Desktop.Children)
            {
                var overlay = child as Overlay;

                if (overlay != null)
                {
                    overlay.Update(gameTime);
                }
            }

            Vector2    mousePos      = _input.MousePos;
            Vector2    mouseWorldPos = _world.ActiveCamera.ScreenToWorld(mousePos);
            RectangleF minimapBounds = MinimapOverlay.Minimap.GetAbsoluteBounds();

            // Move minimap viewport with left mouse button.
            // Do not check this if the overlay is hidden.
            if (_gui.Visible &&
                SelectionBounds == Rectangle.Empty &&
                _world.ActiveCamera.CameraMode != CameraMode.Follow &&
                _input.MouseState.LeftButton == ButtonState.Pressed &&
                minimapBounds.Contains(mousePos))
            {
                Vector2 minimapPos = mousePos;
                minimapPos.X -= minimapBounds.Left;
                minimapPos.Y -= minimapBounds.Top;

                var minimapBoundsInt = new Rectangle((int)minimapBounds.X, (int)minimapBounds.Y, (int)minimapBounds.Width, (int)minimapBounds.Height);

                Vector2 newViewportPos = _world.ActiveCamera.MinimapToWorldCoordinates(minimapPos, minimapBoundsInt);
                newViewportPos.X -= _world.ActiveCamera.Viewport.Width / 2f;
                newViewportPos.Y -= _world.ActiveCamera.Viewport.Height / 2f;

                _world.ActiveCamera.Position = newViewportPos;
            }

            // Clear context menu
            if (ActiveContextMenu != null &&
                (_input.IsMouseButtonPressed(MouseButtons.Left) || _input.IsMouseButtonPressed(MouseButtons.Right)) &&
                !ActiveContextMenu.GetAbsoluteBounds().Contains(mousePos))
            {
                HideContextMenu();
            }

            // Selection region controls
            if (ActiveContextMenu == null &&
                _input.IsMouseButtonPressed(MouseButtons.Left) && !minimapBounds.Contains(mousePos))
            {
                SelectionBounds = new Rectangle((int)mousePos.X, (int)mousePos.Y, 0, 0);
            }
            else if (_input.IsMouseButtonReleased(MouseButtons.Left) &&
                     SelectionBounds != Rectangle.Empty)
            {
                SelectedGameObjects = GetSelectedObjects();

                SelectionBounds = Rectangle.Empty;
            }

            // Update selection bounds if mouse is pressed
            if (_input.IsMouseButtonDown(MouseButtons.Left) && SelectionBounds != Rectangle.Empty)
            {
                var bounds = SelectionBounds;

                int width  = (int)mousePos.X - bounds.X,
                    height = (int)mousePos.Y - bounds.Y;

                SelectionBounds = new Rectangle(bounds.X, bounds.Y, width, height);
            }

            // Show context menu with right mouse click
            if (_input.IsMouseButtonReleased(MouseButtons.Right))
            {
                // If objects are selected and user right clicks while not hovering
                // any of the selected objects, deselect them all.
                if (SelectedGameObjects.Length > 0)
                {
                    bool containsMouse = SelectedGameObjects.Any(o => o.BoundsF.Contains(mouseWorldPos));
                    if (!containsMouse)
                    {
                        SelectedGameObjects = new IGameObject[0];
                    }
                }

                SelectedGameObjects = GetSelectedObjects(mouseWorldPos);

                DisplayContextMenu(mousePos, mouseWorldPos);
            }

            // Do not process any of the commands if the console is open
            if (_console.Window.IsClosed)
            {
                // Enable/disable drawing of the collision tiles layer
                if (_input.IsKeyPressed(Keys.C))
                {
                    _world.ActiveCamera.World.RenderCollisionTiles = !_world.ActiveCamera.World.RenderCollisionTiles;
                }

                // Enable/disable grid lines with the G key
                if (_input.IsKeyPressed(Keys.G))
                {
                    RenderGrid = !RenderGrid;
                }

                // Enable/disable grid lines with the O key
                if (_input.IsKeyPressed(Keys.O))
                {
                    RenderOverlay = !RenderOverlay;
                }

                // Enable/disable region borders with the R key
                if (_input.IsKeyPressed(Keys.R))
                {
                    RenderRegionBorders = !RenderRegionBorders;
                }
            }
        }