Example #1
0
        public override void MouseDown(ViewportBase viewport, ViewportEvent e)
        {
            var vp = viewport as Viewport3D;
            if (vp == null || e.Button != MouseButtons.Left) return;

            // Do selection
            e.Handled = true;
            var ray = vp.CastRayFromScreen(e.X, e.Y);
            var hits = MainTool.GetCopies().Where(x => x.BoundingBox.IntersectsWith(ray));
            var clickedFace = hits.SelectMany(f => f.Faces)
                .Select(x => new { Item = x, Intersection = x.GetIntersectionPoint(ray) })
                .Where(x => x.Intersection != null)
                .OrderBy(x => (x.Intersection - ray.Start).VectorMagnitude())
                .Select(x => x.Item)
                .FirstOrDefault();

            var faces = new List<Face>();
            if (clickedFace != null)
            {
                if (KeyboardState.Shift) faces.AddRange(clickedFace.Parent.Faces);
                else faces.Add(clickedFace);
            }

            if (!KeyboardState.Ctrl) _selection.Clear();
            _selection.AddRange(faces);

            UpdateSelection();
        }
 public void KeyDown(ViewportEvent e)
 {
     if (e.KeyCode == Keys.Space)
     {
         Viewport.Cursor = Cursors.SizeAll;
         e.Handled = true;
     }
     var str = e.KeyCode.ToString();
     if (str.StartsWith("NumPad") || str.StartsWith("D"))
     {
         var last = str.Last();
         if (Char.IsDigit(last))
         {
             var press = (int) Char.GetNumericValue(last);
             if (press >= 0 && press <= 9)
             {
                 if (press == 0) press = 10;
                 var num = Math.Max(press - 6, 6 - press);
                 var pow = (decimal) Math.Pow(2, num);
                 var zoom = press < 6 ? 1 / pow : pow;
                 Viewport2D.Zoom = zoom;
                 Mediator.Publish(EditorMediator.ViewZoomChanged, Viewport2D.Zoom);
             }
         }
     }
 }
Example #3
0
        public override Matrix4? GetTransformationMatrix(Viewport2D viewport, ViewportEvent e, BaseBoxTool.BoxState state, Document doc, IEnumerable<Widget> activeWidgets)
        {
            var origin = viewport.ZeroUnusedCoordinate((state.PreTransformBoxStart + state.PreTransformBoxEnd) / 2);
            var rw = activeWidgets.OfType<RotationWidget>().FirstOrDefault();
            if (rw != null) origin = rw.GetPivotPoint();

            var forigin = viewport.Flatten(origin);

            var origv = (state.MoveStart - forigin).Normalise();
            var newv = (viewport.ScreenToWorld(e.X, viewport.Height - e.Y) - forigin).Normalise();

            var angle = DMath.Acos(Math.Max(-1, Math.Min(1, origv.Dot(newv))));
            if ((origv.Cross(newv).Z < 0)) angle = 2 * DMath.PI - angle;

            var shf = KeyboardState.Shift;
            var def = Select.RotationStyle;
            var snap = (def == RotationStyle.SnapOnShift && shf) || (def == RotationStyle.SnapOffShift && !shf);
            if (snap)
            {
                var deg = angle * (180 / DMath.PI);
                var rnd = Math.Round(deg / 15) * 15;
                angle = rnd * (DMath.PI / 180);
            }

            Matrix4 rotm;
            if (viewport.Direction == Viewport2D.ViewDirection.Top) rotm = Matrix4.CreateRotationZ((float)angle);
            else if (viewport.Direction == Viewport2D.ViewDirection.Front) rotm = Matrix4.CreateRotationX((float)angle);
            else rotm = Matrix4.CreateRotationY((float)-angle); // The Y axis rotation goes in the reverse direction for whatever reason

            var mov = Matrix4.CreateTranslation((float)-origin.X, (float)-origin.Y, (float)-origin.Z);
            var rot = Matrix4.Mult(mov, rotm);
            return Matrix4.Mult(rot, Matrix4.Invert(mov));
        }
Example #4
0
        public void KeyDown(ViewportEvent e)
        {
            if (e.KeyCode == Keys.Space)
            {
                Viewport.Cursor = Cursors.SizeAll;
                if (!Sledge.Settings.View.Camera2DPanRequiresMouseClick)
                {
                    Viewport.Capture = true;
                    var p = e.Sender.PointToClient(Cursor.Position);
                    _mouseDown = new Coordinate(p.X, Viewport2D.Height - p.Y, 0);
                }
                e.Handled = true;
            }

            var moveAllowed = DocumentManager.CurrentDocument != null &&
                              (DocumentManager.CurrentDocument.Selection.IsEmpty()
                               || !Sledge.Settings.Select.ArrowKeysNudgeSelection);
            if (moveAllowed)
            {
                var shift = new Coordinate(0, 0, 0);

                switch (e.KeyCode)
                {
                    case Keys.Left:
                        shift.X = -Viewport.Width / Viewport2D.Zoom / 4;
                        break;
                    case Keys.Right:
                        shift.X = Viewport.Width / Viewport2D.Zoom / 4;
                        break;
                    case Keys.Up:
                        shift.Y = Viewport.Height / Viewport2D.Zoom / 4;
                        break;
                    case Keys.Down:
                        shift.Y = -Viewport.Height / Viewport2D.Zoom / 4;
                        break;
                }

                Viewport2D.Position += shift;
            }

            var str = e.KeyCode.ToString();
            if (str.StartsWith("NumPad") || str.StartsWith("D"))
            {
                var last = str.Last();
                if (Char.IsDigit(last))
                {
                    var press = (int) Char.GetNumericValue(last);
                    if (press >= 0 && press <= 9)
                    {
                        if (press == 0) press = 10;
                        var num = Math.Max(press - 6, 6 - press);
                        var pow = (decimal) Math.Pow(2, num);
                        var zoom = press < 6 ? 1 / pow : pow;
                        Viewport2D.Zoom = zoom;
                        Mediator.Publish(EditorMediator.ViewZoomChanged, Viewport2D.Zoom);
                    }
                }
            }
        }
Example #5
0
 public void MouseDown(ViewportEvent e)
 {
     if (_showing)
     {
         _menu.Show(Viewport, new Point(e.X, e.Y));
         e.Handled = true;
     }
 }
Example #6
0
 public override void MouseDown(ViewportBase viewport, ViewportEvent e)
 {
     if (!(viewport is Viewport3D) || (e.Button != MouseButtons.Left && e.Button != MouseButtons.Right)) return;
     _multiplier = e.Button == MouseButtons.Left ? 1 : -1;
     //PaintCurrentPoint((Viewport3D) viewport);
     _mouseDown = true;
     _moveCount = 0;
 }
 public void KeyUp(ViewportEvent e)
 {
     if (e.KeyCode == Keys.Space)
     {
         Viewport.Cursor = Cursors.Default;
         e.Handled = true;
     }
 }
Example #8
0
 public override void KeyDown(ViewportBase viewport, ViewportEvent e)
 {
     switch (e.KeyCode)
     {
         case Keys.Enter:
             CreateEntity(_location);
             _state = EntityState.None;
             break;
         case Keys.Escape:
             _state = EntityState.None;
             break;
     }
 }
Example #9
0
        public override void MouseMove(ViewportBase viewport, ViewportEvent e)
        {
            var vp = viewport as Viewport3D;
            if (vp == null) return;

            _mousePos.X = e.X;
            _mousePos.Y = e.Y;

            if (_currentPoint == null) return;

            if (_mouseDown)
            {
                _moveCount++;
                //PaintCurrentPoint(vp);
            }
        }
Example #10
0
        public override void MouseDown(ViewportBase viewport, ViewportEvent e)
        {
            return;

            var vp = viewport as Viewport3D;
            if (vp == null || (e.Button != MouseButtons.Left && e.Button != MouseButtons.Right)) return;

            var ray = vp.CastRayFromScreen(e.X, e.Y);
            var hits = Document.Map.WorldSpawn.GetAllNodesIntersectingWith(ray).OfType<Solid>();
            var clickedFace = hits.SelectMany(f => f.Faces)
                .Select(x => new { Item = x, Intersection = x.GetIntersectionPoint(ray) })
                .Where(x => x.Intersection != null)
                .OrderBy(x => (x.Intersection - ray.Start).VectorMagnitude())
                .Select(x => x.Item)
                .FirstOrDefault();
            // TODO: Select
            //TextureItem itemToSelect = null;
            // if ((behaviour == SelectBehaviour.Select || behaviour == SelectBehaviour.LiftSelect) && !KeyboardState.Ctrl)
            // {
            //     Selection.Clear();
            // }
            //if (clickedFace != null)
            //{
            //    var faces = new List<Face>();
            //    if (KeyboardState.Shift) faces.AddRange(clickedFace.Parent.Faces);
            //    else faces.Add(clickedFace);
            //    if (behaviour == SelectBehaviour.Select || behaviour == SelectBehaviour.LiftSelect)
            //    {
            //        foreach (var face in faces)
            //        {
            //            if (face.IsSelected) Selection.Deselect(face);
            //            else Selection.Select(face);
            //        }
            //    }
            //    if (behaviour == SelectBehaviour.Lift || behaviour == SelectBehaviour.LiftSelect)
            //    {
            //        var tex = faces.Where(face => face.Texture.Texture != null).FirstOrDefault();
            //        itemToSelect = tex != null ? TexturePackage.GetItem(tex.Texture.Name) : null;
            //    }
            //}
            //Document.UpdateDisplayLists();
            //_form.SelectionChanged();
            //if (itemToSelect != null)
            //{
            //    _form.SelectTexture(itemToSelect);
            //}
        }
Example #11
0
        public override Matrix4? GetTransformationMatrix(Viewport2D viewport, ViewportEvent e, BaseBoxTool.BoxState state, Document doc)
        {
            var shearUpDown = state.Handle == BaseBoxTool.ResizeHandle.Left || state.Handle == BaseBoxTool.ResizeHandle.Right;
            var shearTopRight = state.Handle == BaseBoxTool.ResizeHandle.Top || state.Handle == BaseBoxTool.ResizeHandle.Right;

            var nsmd = viewport.ScreenToWorld(e.X, viewport.Height - e.Y) - state.MoveStart;
            var mouseDiff = SnapIfNeeded(nsmd, doc);
            if (KeyboardState.Shift)
            {
                mouseDiff = doc.Snap(nsmd, doc.Map.GridSpacing / 2);
            }

            var relative = viewport.Flatten(state.PreTransformBoxEnd - state.PreTransformBoxStart);
            var shearOrigin = (shearTopRight) ? state.PreTransformBoxStart : state.PreTransformBoxEnd;

            var shearAmount = new Coordinate(mouseDiff.X / relative.Y, mouseDiff.Y / relative.X, 0);
            if (!shearTopRight) shearAmount *= -1;

            var shearMatrix = Matrix4.Identity;
            var sax = (float)shearAmount.X;
            var say = (float)shearAmount.Y;

            switch (viewport.Direction)
            {
                case Viewport2D.ViewDirection.Top:
                    if (shearUpDown) shearMatrix.M12 = say;
                    else shearMatrix.M21 = sax;
                    break;
                case Viewport2D.ViewDirection.Front:
                    if (shearUpDown) shearMatrix.M23 = say;
                    else shearMatrix.M32 = sax;
                    break;
                case Viewport2D.ViewDirection.Side:
                    if (shearUpDown) shearMatrix.M13 = say;
                    else shearMatrix.M31 = sax;
                    break;
            }

            var stran = Matrix4.CreateTranslation((float)-shearOrigin.X, (float)-shearOrigin.Y, (float)-shearOrigin.Z);
            var shear = Matrix4.Mult(stran, shearMatrix);
            return Matrix4.Mult(shear, Matrix4.Invert(stran));
        }
Example #12
0
        public override void MouseDown(ViewportBase viewport, ViewportEvent e)
        {
            var vp = viewport as Viewport3D;
            if (vp == null) return;

            // Get the ray that is cast from the clicked point along the viewport frustrum
            var ray = vp.CastRayFromScreen(e.X, e.Y);

            // Grab all the elements that intersect with the ray
            var hits = Document.Map.WorldSpawn.GetAllNodesIntersectingWith(ray);

            // Sort the list of intersecting elements by distance from ray origin and grab the first hit
            var hit = hits
                .Select(x => new {Item = x, Intersection = GetIntersectionPoint(x, ray)})
                .Where(x => x.Intersection != null)
                .OrderBy(x => (x.Intersection - ray.Start).VectorMagnitude())
                .FirstOrDefault();

            if (hit == null) return; // Nothing was clicked

            CreateDecal(hit.Intersection);
        }
Example #13
0
        public override void MouseDown(ViewportBase viewport, ViewportEvent ve)
        {
            if (viewport is Viewport2D)
            {
                var vp2 = (Viewport2D)viewport;
                if (ve.Button == MouseButtons.Left && MouseOverPivot(vp2, ve))
                {
                    _movingPivot = true;
                    ve.Handled = true;
                }
                return;
            }

            var vp = viewport as Viewport3D;
            if (vp == null || vp != _activeViewport) return;

            if (ve.Button != MouseButtons.Left || _mouseOver == CircleType.None) return;
            _mouseDown = _mouseOver;
            _mouseDownPoint = new Coordinate(ve.X, vp.Height - ve.Y, 0);
            _mouseMovePoint = null;
            ve.Handled = true;
            vp.AquireInputLock(this);
        }
Example #14
0
 public override Matrix4? GetTransformationMatrix(Viewport2D viewport, ViewportEvent e, BaseBoxTool.BoxState state, Document doc)
 {
     var coords = GetBoxCoordinatesForSelectionResize(viewport, e, state, doc);
     state.BoxStart = coords.Item1;
     state.BoxEnd = coords.Item2;
     Matrix4 resizeMatrix;
     if (state.Handle == BaseBoxTool.ResizeHandle.Center)
     {
         var movement = state.BoxStart - state.PreTransformBoxStart;
         resizeMatrix = Matrix4.CreateTranslation((float)movement.X, (float)movement.Y, (float)movement.Z);
     }
     else
     {
         var resize = (state.PreTransformBoxStart - state.BoxStart) +
                      (state.BoxEnd - state.PreTransformBoxEnd);
         resize = resize.ComponentDivide(state.PreTransformBoxEnd - state.PreTransformBoxStart);
         resize += new Coordinate(1, 1, 1);
         var offset = -GetOriginForTransform(viewport, state);
         var trans = Matrix4.CreateTranslation((float)offset.X, (float)offset.Y, (float)offset.Z);
         var scale = Matrix4.Mult(trans, Matrix4.Scale((float)resize.X, (float)resize.Y, (float)resize.Z));
         resizeMatrix = Matrix4.Mult(scale, Matrix4.Invert(trans));
     }
     return resizeMatrix;
 }
Example #15
0
 public override void MouseLeave(ViewportBase viewport, ViewportEvent e)
 {
     _activeViewport = null;
 }
Example #16
0
 protected override void LeftMouseDownToDraw(Viewport2D viewport, ViewportEvent e)
 {
     base.LeftMouseDownToDraw(viewport, e);
     if (_lastBox == null) return;
     State.BoxStart += viewport.GetUnusedCoordinate(_lastBox.Start);
     State.BoxEnd += viewport.GetUnusedCoordinate(_lastBox.End);
     _updatePreview = true;
 }
Example #17
0
 public override void OverrideViewportContextMenu(ViewportContextMenu menu, Viewport2D vp, ViewportEvent e)
 {
     menu.Items.Clear();
     if (State.Handle == ResizeHandle.Center)
     {
         var item = new ToolStripMenuItem("Create Object");
         item.Click += (sender, args) => BoxDrawnConfirm(vp);
         menu.Items.Add(item);
     }
 }
Example #18
0
 private bool WidgetAction(Action<Widget, ViewportBase, ViewportEvent> action, ViewportBase viewport, ViewportEvent ev)
 {
     if (_widgets == null) return false;
     foreach (var widget in _widgets)
     {
         action(widget, viewport, ev);
         if (ev != null && ev.Handled) return true;
     }
     return false;
 }
Example #19
0
        private MapObject SelectionTest(Viewport2D viewport, ViewportEvent e)
        {
            // Create a box to represent the click, with a tolerance level
            var unused = viewport.GetUnusedCoordinate(new Coordinate(100000, 100000, 100000));
            var tolerance = 4 / viewport.Zoom; // Selection tolerance of four pixels
            var used = viewport.Expand(new Coordinate(tolerance, tolerance, 0));
            var add = used + unused;
            var click = viewport.Expand(viewport.ScreenToWorld(e.X, viewport.Height - e.Y));
            var box = new Box(click - add, click + add);

            var centerHandles = Sledge.Settings.Select.DrawCenterHandles;
            var centerOnly = Sledge.Settings.Select.ClickSelectByCenterHandlesOnly;
            // Get the first element that intersects with the box, selecting or deselecting as needed
            return Document.Map.WorldSpawn.GetAllNodesIntersecting2DLineTest(box, centerHandles, centerOnly).FirstOrDefault();
        }
Example #20
0
 private Matrix4? GetTransformMatrix(Viewport2D viewport, ViewportEvent e)
 {
     if (_currentTool == null) return null;
     return State.Handle == ResizeHandle.Center
                ? _tools.OfType<ResizeTool>().First().GetTransformationMatrix(viewport, e, State, Document, _widgets)
                : _currentTool.GetTransformationMatrix(viewport, e, State, Document, _widgets);
 }
Example #21
0
 protected override void MouseMove3D(Viewport3D viewport, ViewportEvent e)
 {
     base.MouseMove3D(viewport, e);
 }
Example #22
0
 public void MouseUp(ViewportEvent e)
 {
     if (e.Button == MouseButtons.Right && Viewport is Viewport2D) Mediator.Publish(EditorMediator.ViewportRightClick, new object[] {Viewport, e});
     if (!ShouldRelayEvent(ToolManager.ActiveTool)) return;
     ToolManager.ActiveTool.MouseUp(Viewport, e);
 }
Example #23
0
 public override void MouseEnter(ViewportBase viewport, ViewportEvent e)
 {
     _activeViewport = viewport;
 }
Example #24
0
 public override void KeyUp(ViewportBase viewport, ViewportEvent e)
 {
 }
Example #25
0
 public override void MouseDoubleClick(ViewportBase viewport, ViewportEvent e)
 {
 }
Example #26
0
 /// <summary>
 /// Once the mouse is released in the 3D view, the 3D select cycle has finished.
 /// </summary>
 /// <param name="viewport">The 3D viewport</param>
 /// <param name="e">The mouse event</param>
 protected override void MouseUp3D(Viewport3D viewport, ViewportEvent e)
 {
     IntersectingObjectsFor3DSelection = null;
     ChosenItemFor3DSelection = null;
 }
Example #27
0
 public void KeyUp(ViewportEvent e)
 {
     if (!ShouldRelayEvent(ToolManager.ActiveTool)) return;
     ToolManager.ActiveTool.KeyUp(Viewport, e);
 }
Example #28
0
        protected override void MouseDraggingToResize(Viewport2D viewport, ViewportEvent e)
        {
            if (_currentTool == null)
            {
                base.MouseDraggingToResize(viewport, e);
                return;
            }

            State.Action = BoxAction.Resizing;
            CurrentTransform = GetTransformMatrix(viewport, e);
            if (CurrentTransform.HasValue)
            {
                Document.SetSelectListTransform(CurrentTransform.Value);
                var box = new Box(State.PreTransformBoxStart, State.PreTransformBoxEnd);
                var trans = CreateMatrixMultTransformation(CurrentTransform.Value);
                Mediator.Publish(EditorMediator.SelectionBoxChanged, box.Transform(trans));
            }
            else
            {
                //OnBoxChanged();
            }
        }
Example #29
0
 public void MouseWheel(ViewportEvent e)
 {
     if (!ShouldRelayEvent(ToolManager.ActiveTool)) return;
     ToolManager.ActiveTool.MouseWheel(Viewport, e);
 }
Example #30
0
        /// <summary>
        /// When the mouse is hovering over the box, do collision tests against the handles and change the cursor if needed.
        /// </summary>
        /// <param name="viewport">The viewport</param>
        /// <param name="e">The mouse event</param>
        protected override void MouseHoverWhenDrawn(Viewport2D viewport, ViewportEvent e)
        {
            if (_currentTool == null)
            {
                base.MouseHoverWhenDrawn(viewport, e);
                return;
            }

            var padding = 7 / viewport.Zoom;

            viewport.Cursor = Cursors.Default;
            State.Action = BoxAction.Drawn;
            State.ActiveViewport = null;

            var now = viewport.ScreenToWorld(e.X, viewport.Height - e.Y);
            var start = viewport.Flatten(State.BoxStart);
            var end = viewport.Flatten(State.BoxEnd);

            var ccs = new Coordinate(Math.Min(start.X, end.X), Math.Min(start.Y, end.Y), 0);
            var cce = new Coordinate(Math.Max(start.X, end.X), Math.Max(start.Y, end.Y), 0);

            // Check center handle
            if (now.X > ccs.X && now.X < cce.X && now.Y > ccs.Y && now.Y < cce.Y)
            {
                State.Handle = ResizeHandle.Center;
                State.ActiveViewport = viewport;
                State.Action = BoxAction.ReadyToResize;
                viewport.Cursor = CursorForHandle(State.Handle);
                return;
            }

            // Check other handles
            foreach (var handle in _currentTool.GetHandles(start, end, viewport.Zoom).Where(x => _currentTool.FilterHandle(x.Item1)))
            {
                var x = handle.Item2;
                var y = handle.Item3;
                if (now.X < x - padding || now.X > x + padding || now.Y < y - padding || now.Y > y + padding) continue;
                State.Handle = handle.Item1;
                State.ActiveViewport = viewport;
                State.Action = BoxAction.ReadyToResize;
                viewport.Cursor = CursorForHandle(State.Handle);
                return;
            }
        }