Пример #1
0
        private ClipState GetStateAtPoint(int x, int y, Viewport2D viewport)
        {
            if (_clipPlanePoint1 == null || _clipPlanePoint2 == null || _clipPlanePoint3 == null)
            {
                return(ClipState.None);
            }

            var p  = viewport.ScreenToWorld(x, y);
            var p1 = viewport.Flatten(_clipPlanePoint1);
            var p2 = viewport.Flatten(_clipPlanePoint2);
            var p3 = viewport.Flatten(_clipPlanePoint3);

            var d = 5 / viewport.Zoom;

            if (p.X >= p1.X - d && p.X <= p1.X + d && p.Y >= p1.Y - d && p.Y <= p1.Y + d)
            {
                return(ClipState.MovingPoint1);
            }
            if (p.X >= p2.X - d && p.X <= p2.X + d && p.Y >= p2.Y - d && p.Y <= p2.Y + d)
            {
                return(ClipState.MovingPoint2);
            }
            if (p.X >= p3.X - d && p.X <= p3.X + d && p.Y >= p3.Y - d && p.Y <= p3.Y + d)
            {
                return(ClipState.MovingPoint3);
            }

            return(ClipState.None);
        }
Пример #2
0
        protected override void Render2D(Viewport2D viewport)
        {
            if (_currentTool == null)
            {
                base.Render2D(viewport);
                return;
            }

            var start = viewport.Flatten(State.BoxStart);
            var end   = viewport.Flatten(State.BoxEnd);

            if (ShouldDrawBox(viewport))
            {
                RenderBox(viewport, start, end);
            }

            if (ShouldRenderResizeBox(viewport))
            {
                RenderResizeBox(viewport, start, end);
            }

            if (ShouldRenderHandles())
            {
                RenderHandles(viewport, start, end);
            }

            if (State.Action == BoxAction.Resizing && CurrentTransform.HasValue)
            {
                RenderTransformBox(viewport);
            }
            else if (ShouldDrawBox(viewport))
            {
                RenderBoxText(viewport, start, end);
            }
        }
Пример #3
0
        protected virtual void Render2D(Viewport2D viewport)
        {
            if (State.Action == BoxAction.ReadyToDraw || State.Action == BoxAction.DownToDraw)
            {
                return;
            }
            var start = viewport.Flatten(State.BoxStart);
            var end   = viewport.Flatten(State.BoxEnd);

            if (ShouldDrawBox(viewport))
            {
                RenderBox(viewport, start, end);
            }
            if (ShouldRenderSnapHandle(viewport))
            {
                RenderSnapHandle(viewport);
            }
            if (ShouldRenderResizeBox(viewport))
            {
                RenderResizeBox(viewport, start, end);
            }
            if (ShouldDrawBoxText(viewport))
            {
                RenderBoxText(viewport, start, end);
            }
        }
Пример #4
0
        private Tuple <Coordinate, Coordinate> SnapBoxCoordinatesIfNeeded(Viewport2D viewport, BaseBoxTool.BoxState state, Document document, Coordinate start, Coordinate end)
        {
            if (state.Action == BaseBoxTool.BoxAction.Resizing && state.Handle == BaseBoxTool.ResizeHandle.Center)
            {
                // Pick the corner to snap
                var ms     = state.MoveStart;
                var pts    = viewport.Flatten(state.PreTransformBoxStart);
                var pte    = viewport.Flatten(state.PreTransformBoxEnd);
                var ss     = SnapIfNeeded(start, document);
                var se     = SnapIfNeeded(end, document);
                var middle = (pts + pte) / 2;
                var delta  = ss - start;
                if (ms.Y > middle.Y)
                {
                    // Top
                    delta.Y = se.Y - end.Y;
                }
                if (ms.X > middle.X)
                {
                    // Right
                    delta.X = se.X - end.X;
                }
                start += delta;
                end   += delta;
            }

            var cstart = viewport.Expand(start) + viewport.GetUnusedCoordinate(state.BoxStart);
            var cend   = viewport.Expand(end) + viewport.GetUnusedCoordinate(state.BoxEnd);

            return(Tuple.Create(cstart, cend));
        }
Пример #5
0
        protected virtual Coordinate GetResizeOrigin(Viewport2D viewport)
        {
            if (State.Action != BoxAction.Resizing || State.Handle != ResizeHandle.Center)
            {
                return(null);
            }
            var st     = viewport.Flatten(State.PreTransformBoxStart);
            var ed     = viewport.Flatten(State.PreTransformBoxEnd);
            var points = new[] { st, ed, new Coordinate(st.X, ed.Y, 0), new Coordinate(ed.X, st.Y, 0) };

            return(points.OrderBy(x => (State.MoveStart - x).LengthSquared()).First());
        }
Пример #6
0
        public void Render2D(Viewport2D viewport, MapObject o)
        {
            if (viewport.Zoom < 0.5m)
            {
                return;
            }

            var entityData = o.GetEntityData();

            if (entityData == null)
            {
                return;
            }

            var angles = entityData.GetPropertyCoordinate("angles");

            if (angles == null)
            {
                return;
            }

            //angles = new Coordinate(DMath.DegreesToRadians(angles.Z), DMath.DegreesToRadians(angles.X), DMath.DegreesToRadians(angles.Y));

            Matrix pitch = Matrix.Rotation(DataStructures.Geometric.Quaternion.EulerAngles(DMath.DegreesToRadians(angles.X), 0, 0));
            Matrix yaw   = Matrix.Rotation(DataStructures.Geometric.Quaternion.EulerAngles(0, 0, -DMath.DegreesToRadians(angles.Y)));
            Matrix roll  = Matrix.Rotation(DataStructures.Geometric.Quaternion.EulerAngles(0, DMath.DegreesToRadians(angles.Z), 0));

            var m = new UnitMatrixMult(yaw * roll * pitch);

            var min = Math.Min(o.BoundingBox.Width, Math.Min(o.BoundingBox.Height, o.BoundingBox.Length));
            var p1  = viewport.Flatten(o.BoundingBox.Center);
            var p2  = p1 + viewport.Flatten(m.Transform(Coordinate.UnitY)) * min * 0.4m;

            var multiplier = 4 / viewport.Zoom;
            var dir        = (p2 - p1).Normalise();
            var cp         = new Coordinate(-dir.Y, dir.X, 0).Normalise();

            GL.Color4(Color.FromArgb(255, o.Colour));

            GL.Begin(PrimitiveType.Lines);
            Coord(p1);
            Coord(p2);
            GL.End();

            GL.Begin(PrimitiveType.Triangles);
            Coord(p2 - (dir * 2 - cp) * multiplier);
            Coord(p2 - (dir * 2 + cp) * multiplier);
            Coord(p2);
            GL.End();
        }
Пример #7
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;
            }
        }
Пример #8
0
        public void Render2D(Viewport2D viewport, MapObject o)
        {
            if (viewport.Zoom < 0.5m)
            {
                return;
            }

            var entityData = o.GetEntityData();

            if (entityData == null)
            {
                return;
            }

            var angles = entityData.GetPropertyCoordinate("angles");

            if (angles == null)
            {
                return;
            }

            angles = new Coordinate(DMath.DegreesToRadians(angles.Z), DMath.DegreesToRadians(angles.X), DMath.DegreesToRadians(angles.Y));
            var m = new UnitMatrixMult(Matrix4.CreateRotationX((float)angles.X) * Matrix4.CreateRotationY((float)angles.Y) * Matrix4.CreateRotationZ((float)angles.Z));

            var min = Math.Min(o.BoundingBox.Width, Math.Min(o.BoundingBox.Height, o.BoundingBox.Length));
            var p1  = viewport.Flatten(o.BoundingBox.Center);
            //Extend direction
            //var p2 = p1 + viewport.Flatten(m.Transform(Coordinate.UnitX)) * min * 0.4m;
            var p2 = p1 + viewport.Flatten(m.Transform(Coordinate.UnitX)) * min * 7.5m;

            var multiplier = 4 / viewport.Zoom;
            var dir        = (p2 - p1).Normalise();
            var cp         = new Coordinate(-dir.Y, dir.X, 0).Normalise();

            GL.Color4(Color.FromArgb(255, o.Colour));

            GL.Begin(PrimitiveType.Lines);
            Coord(p1);
            Coord(p2);
            GL.End();

            GL.Begin(PrimitiveType.Triangles);
            Coord(p2 - (dir * 2 - cp) * multiplier);
            Coord(p2 - (dir * 2 + cp) * multiplier);
            Coord(p2);
            GL.End();
        }
Пример #9
0
        protected override void Render2D(Viewport2D viewport)
        {
            if (_currentTool == null)
            {
                base.Render2D(viewport);
                return;
            }

            var box = Document.Selection.GetSelectionBoundingBox();

            State.BoxStart = box.Start;
            State.BoxEnd   = box.End;

            var start = viewport.Flatten(State.BoxStart);
            var end   = viewport.Flatten(State.BoxEnd);

            if (ShouldDrawBox(viewport))
            {
                RenderBox(viewport, start, end);
            }

            if (ShouldRenderSnapHandle(viewport))
            {
                RenderSnapHandle(viewport);
            }

            if (ShouldRenderResizeBox(viewport))
            {
                RenderResizeBox(viewport, start, end);
            }

            if (_currentTool != _tools.FirstOrDefault(x => x.GetType() == typeof(MoveTool)))
            {
                RenderHandles(viewport, start, end);
            }

            if (State.Action == BoxAction.Resizing && CurrentTransform.HasValue)
            {
                RenderTransformBox(viewport);
            }
            else if (ShouldDrawBox(viewport))
            {
                RenderBoxText(viewport, start, end);
            }
            return;
        }
Пример #10
0
        private Coordinate GetResizeOrigin(Viewport2D viewport, BaseBoxTool.BoxState state, Document document)
        {
            if (state.Action != BaseBoxTool.BoxAction.Resizing || state.Handle != BaseBoxTool.ResizeHandle.Center)
            {
                return(null);
            }
            var sel = document.Selection.GetSelectedParents().ToList();

            if (sel.Count == 1 && sel[0] is Entity && !sel[0].HasChildren)
            {
                return(viewport.Flatten(((Entity)sel[0]).Origin));
            }
            var st     = viewport.Flatten(state.PreTransformBoxStart);
            var ed     = viewport.Flatten(state.PreTransformBoxEnd);
            var points = new[] { st, ed, new Coordinate(st.X, ed.Y, 0), new Coordinate(ed.X, st.Y, 0) };

            return(points.OrderBy(x => (state.MoveStart - x).LengthSquared()).First());
        }
Пример #11
0
        private void Render2D(Viewport2D viewport, Document document)
        {
            var start = viewport.Flatten(document.Map.CordonBounds.Start);
            var end   = viewport.Flatten(document.Map.CordonBounds.End);

            var min = viewport.ScreenToWorld(0, 0);
            var max = viewport.ScreenToWorld(viewport.Width, viewport.Height);

            GL.Color4(Color.FromArgb(128, Color.Purple));
            GL.Begin(PrimitiveType.Quads);

            GL.Vertex3(min.DX, min.DY, 0);
            GL.Vertex3(max.DX, min.DY, 0);
            GL.Vertex3(max.DX, start.DY, 0);
            GL.Vertex3(min.DX, start.DY, 0);

            GL.Vertex3(min.DX, end.DY, 0);
            GL.Vertex3(max.DX, end.DY, 0);
            GL.Vertex3(max.DX, max.DY, 0);
            GL.Vertex3(min.DX, max.DY, 0);

            GL.Vertex3(min.DX, start.DY, 0);
            GL.Vertex3(start.DX, start.DY, 0);
            GL.Vertex3(start.DX, end.DY, 0);
            GL.Vertex3(min.DX, end.DY, 0);

            GL.Vertex3(end.DX, start.DY, 0);
            GL.Vertex3(max.DX, start.DY, 0);
            GL.Vertex3(max.DX, end.DY, 0);
            GL.Vertex3(end.DX, end.DY, 0);

            GL.End();


            GL.LineWidth(2);
            GL.Begin(PrimitiveType.LineLoop);
            GL.Color3(Color.Red);
            GL.Vertex3(start.DX, start.DY, start.DZ);
            GL.Vertex3(end.DX, start.DY, start.DZ);
            GL.Vertex3(end.DX, end.DY, start.DZ);
            GL.Vertex3(start.DX, end.DY, start.DZ);
            GL.End();
            GL.LineWidth(1);
        }
Пример #12
0
        private void Render2D(Viewport2D viewport)
        {
            var pp = viewport.Flatten(_pivotPoint);

            GL.Begin(PrimitiveType.Lines);
            GL.Color3(Color.Cyan);
            GLX.Circle(new Vector2d(pp.DX, pp.DY), 4, (double)viewport.Zoom);
            GL.Color3(Color.White);
            GLX.Circle(new Vector2d(pp.DX, pp.DY), 8, (double)viewport.Zoom);
            GL.End();
        }
Пример #13
0
        private static Coordinate GetOriginForTransform(Viewport2D viewport, BaseBoxTool.BoxState state)
        {
            decimal x      = 0;
            decimal y      = 0;
            var     cstart = viewport.Flatten(state.PreTransformBoxStart);
            var     cend   = viewport.Flatten(state.PreTransformBoxEnd);

            switch (state.Handle)
            {
            case BaseBoxTool.ResizeHandle.TopLeft:
            case BaseBoxTool.ResizeHandle.Top:
            case BaseBoxTool.ResizeHandle.TopRight:
            case BaseBoxTool.ResizeHandle.Left:
            case BaseBoxTool.ResizeHandle.Right:
                y = cstart.Y;
                break;

            case BaseBoxTool.ResizeHandle.BottomLeft:
            case BaseBoxTool.ResizeHandle.Bottom:
            case BaseBoxTool.ResizeHandle.BottomRight:
                y = cend.Y;
                break;
            }
            switch (state.Handle)
            {
            case BaseBoxTool.ResizeHandle.Top:
            case BaseBoxTool.ResizeHandle.TopRight:
            case BaseBoxTool.ResizeHandle.Right:
            case BaseBoxTool.ResizeHandle.BottomRight:
            case BaseBoxTool.ResizeHandle.Bottom:
                x = cstart.X;
                break;

            case BaseBoxTool.ResizeHandle.TopLeft:
            case BaseBoxTool.ResizeHandle.Left:
            case BaseBoxTool.ResizeHandle.BottomLeft:
                x = cend.X;
                break;
            }
            return(viewport.Expand(new Coordinate(x, y, 0)));
        }
Пример #14
0
        public override void Render2D(Viewport2D viewport)
        {
            var pos = viewport.Flatten(_origin.Coordinate);

            GL.Color3(Color.Cyan);
            GL.Begin(PrimitiveType.Lines);
            GLX.Circle(new Vector2d(pos.DX, pos.DY), 8, (double)viewport.Zoom);
            GL.End();
            GL.Begin(PrimitiveType.Points);
            GL.Vertex2(pos.DX, pos.DY);
            GL.End();
        }
Пример #15
0
 protected override Coordinate GetResizeOrigin(Viewport2D viewport)
 {
     if (State.Action == BoxAction.Resizing && State.Handle == ResizeHandle.Center && !Document.Selection.IsEmpty())
     {
         var sel = Document.Selection.GetSelectedParents().ToList();
         if (sel.Count == 1 && sel[0] is Entity && !sel[0].HasChildren)
         {
             return(viewport.Flatten(((Entity)sel[0]).Origin));
         }
     }
     return(base.GetResizeOrigin(viewport));
 }
Пример #16
0
        protected virtual void MouseHoverWhenDrawn(Viewport2D viewport, ViewportEvent e)
        {
            var now    = viewport.ScreenToWorld(e.X, viewport.Height - e.Y);
            var start  = viewport.Flatten(State.BoxStart);
            var end    = viewport.Flatten(State.BoxEnd);
            var handle = GetHandle(now, start, end, HandleWidth / viewport.Zoom);

            if (handle.HasValue)
            {
                viewport.Cursor      = CursorForHandle(handle.Value);
                State.Handle         = handle.Value;
                State.Action         = BoxAction.ReadyToResize;
                State.ActiveViewport = viewport;
            }
            else
            {
                viewport.Cursor      = Cursors.Default;
                State.Action         = BoxAction.Drawn;
                State.ActiveViewport = null;
            }
        }
Пример #17
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)));
        }
Пример #18
0
        /// <summary>
        /// Get the VM points at the provided coordinate, ordered from top to bottom (for the supplied viewport).
        /// </summary>
        /// <param name="x">The X coordinate</param>
        /// <param name="y">The Y coordinate</param>
        /// <param name="viewport">The viewport</param>
        /// <returns>The points ordered from top to bottom, or an empty set if no points were found</returns>
        public List <VMPoint> GetVerticesAtPoint(int x, int y, Viewport2D viewport)
        {
            var p = viewport.ScreenToWorld(x, y);
            var d = 5 / viewport.Zoom; // Tolerance value = 5 pixels

            // Order by the unused coordinate in the view (which is the up axis) descending to get the "closest" point
            return((from point in Points
                    let c = viewport.Flatten(point.Coordinate)
                            where p.X >= c.X - d && p.X <= c.X + d && p.Y >= c.Y - d && p.Y <= c.Y + d
                            let unused = viewport.GetUnusedCoordinate(point.Coordinate)
                                         orderby unused.X + unused.Y + unused.Z descending
                                         select point).ToList());
        }
Пример #19
0
        private State GetStateAtPoint(int x, int y, Viewport2D viewport, out Camera activeCamera)
        {
            var d = 5 / viewport.Zoom;

            foreach (var cam in GetCameras())
            {
                var p    = viewport.ScreenToWorld(x, y);
                var pos  = viewport.Flatten(cam.EyePosition);
                var look = viewport.Flatten(cam.LookPosition);
                activeCamera = cam;
                if (p.X >= pos.X - d && p.X <= pos.X + d && p.Y >= pos.Y - d && p.Y <= pos.Y + d)
                {
                    return(State.MovingPosition);
                }
                if (p.X >= look.X - d && p.X <= look.X + d && p.Y >= look.Y - d && p.Y <= look.Y + d)
                {
                    return(State.MovingLook);
                }
            }

            activeCamera = null;
            return(State.None);
        }
Пример #20
0
        private bool MouseOverPivot(Viewport2D vp, ViewportEvent e)
        {
            if (Document.Selection.IsEmpty())
            {
                return(false);
            }

            var pivot = vp.WorldToScreen(vp.Flatten(_pivotPoint));
            var x     = e.X;
            var y     = vp.Height - e.Y;

            return(pivot.X > x - 8 && pivot.X < x + 8 &&
                   pivot.Y > y - 8 && pivot.Y < y + 8);
        }
Пример #21
0
        public void Render2D(Viewport2D viewport, MapObject o)
        {
            if (viewport.Zoom < 1)
            {
                return;
            }

            var entityData = o.GetEntityData();

            if (entityData == null)
            {
                return;
            }

            var start = viewport.WorldToScreen(viewport.Flatten(o.BoundingBox.Start));
            var end   = viewport.WorldToScreen(viewport.Flatten(o.BoundingBox.End));

            if (start.X >= viewport.Width || end.X <= 0 || start.Y >= viewport.Height || end.Y <= 0)
            {
                return;
            }

            var text     = entityData.Name;
            var nameProp = entityData.GetPropertyValue("targetname");

            if (!String.IsNullOrWhiteSpace(nameProp))
            {
                text += ": " + nameProp;
            }

            // Center the text horizontally
            var wid    = _printer.Measure(text, _printerFont, new RectangleF(0, 0, viewport.Width, viewport.Height));
            var cx     = (float)(start.X + (end.X - start.X) / 2);
            var bounds = new RectangleF(cx - wid.BoundingBox.Width / 2, viewport.Height - (float)end.Y - _printerFont.Height - 6, viewport.Width, viewport.Height);

            _printer.Print(text, _printerFont, o.Colour, bounds);
        }
Пример #22
0
        public override List <VMPoint> GetVerticesAtPoint(int x, int y, Viewport2D viewport)
        {
            var verts = MainTool.GetVerticesAtPoint(x, y, viewport);

            var p = viewport.ScreenToWorld(x, y);
            var d = 8 / viewport.Zoom; // Tolerance value = 8 pixels
            var c = viewport.Flatten(_origin.Coordinate);

            if (p.X >= c.X - d && p.X <= c.X + d && p.Y >= c.Y - d && p.Y <= c.Y + d)
            {
                verts.Insert(0, _origin);
            }

            return(verts);
        }
Пример #23
0
 public void ViewportRightClick(Viewport2D vp, ViewportEvent e)
 {
     ViewportContextMenu.Instance.AddNonSelectionItems(_document, vp);
     if (!_document.Selection.IsEmpty() && !_document.Selection.InFaceSelection && ToolManager.ActiveTool is SelectTool)
     {
         var selectionBoundingBox = _document.Selection.GetSelectionBoundingBox();
         var point = vp.ScreenToWorld(e.X, vp.Height - e.Y);
         var start = vp.Flatten(selectionBoundingBox.Start);
         var end   = vp.Flatten(selectionBoundingBox.End);
         if (point.X >= start.X && point.X <= end.X && point.Y >= start.Y && point.Y <= end.Y)
         {
             // Clicked inside the selection bounds
             ViewportContextMenu.Instance.AddSelectionItems(_document, vp);
         }
     }
     if (ToolManager.ActiveTool != null)
     {
         ToolManager.ActiveTool.OverrideViewportContextMenu(ViewportContextMenu.Instance, vp, e);
     }
     if (ViewportContextMenu.Instance.Items.Count > 0)
     {
         ViewportContextMenu.Instance.Show(vp, e.X, e.Y);
     }
 }
Пример #24
0
        private void RenderTransformBox(Viewport2D viewport)
        {
            if (!CurrentTransform.HasValue)
            {
                return;
            }

            var box   = new Box(State.PreTransformBoxStart, State.PreTransformBoxEnd);
            var trans = CreateMatrixMultTransformation(CurrentTransform.Value);

            box = box.Transform(trans);
            var s = viewport.Flatten(box.Start);
            var e = viewport.Flatten(box.End);

            GL.Enable(EnableCap.LineStipple);
            GL.LineStipple(10, 0xAAAA);
            GL.Begin(PrimitiveType.Lines);
            GL.Color4(Color.FromArgb(64, BoxColour));

            Coord(s.DX, s.DY, e.DZ);
            Coord(e.DX, s.DY, e.DZ);

            Coord(s.DX, e.DY, e.DZ);
            Coord(e.DX, e.DY, e.DZ);

            Coord(s.DX, s.DY, e.DZ);
            Coord(s.DX, e.DY, e.DZ);

            Coord(e.DX, s.DY, e.DZ);
            Coord(e.DX, e.DY, e.DZ);

            GL.End();
            GL.Disable(EnableCap.LineStipple);

            RenderBoxText(viewport, s, e);
        }
Пример #25
0
        protected virtual void RenderSnapHandle(Viewport2D viewport)
        {
            var start = GetResizeOrigin(viewport);

            if (start == null)
            {
                return;
            }
            const int size = 6;
            var       dist = (double)(size / viewport.Zoom);

            var origin = start + viewport.Flatten(State.BoxStart - State.PreTransformBoxStart);

            GL.Begin(PrimitiveType.Lines);
            GL.Color4(GetRenderSnapHandleColour());
            Coord(origin.DX - dist, origin.DY + dist, 0);
            Coord(origin.DX + dist, origin.DY - dist, 0);
            Coord(origin.DX + dist, origin.DY + dist, 0);
            Coord(origin.DX - dist, origin.DY - dist, 0);
            GL.End();
        }
Пример #26
0
        protected override void Render2D(Viewport2D vp)
        {
            base.Render2D(vp);

            if (_currentTool != null)
            {
                _currentTool.Render2D(vp);
            }

            // Render out the solid previews
            GL.Color3(Color.Pink);
            Matrix.Push();
            var matrix = vp.GetModelViewMatrix();

            GL.MultMatrix(ref matrix);
            MapObjectRenderer.DrawWireframe(_copies.Keys.SelectMany(x => x.Faces), true, false);
            Matrix.Pop();

            // Draw in order by the unused coordinate (the up axis for this viewport)
            var ordered = (from point in Points
                           where (point.IsMidPoint && _showPoints != ShowPoints.Vertices) || (!point.IsMidPoint && _showPoints != ShowPoints.Midpoints)
                           let unused = vp.GetUnusedCoordinate(point.Coordinate)
                                        orderby point.IsSelected, unused.X + unused.Y + unused.Z
                           select point).ToList();
            // Render out the point handles
            var z = (double)vp.Zoom;

            GL.Begin(BeginMode.Quads);
            foreach (var point in ordered)
            {
                var c = vp.Flatten(point.Coordinate);
                GL.Color3(Color.Black);
                GLX.Square(new Vector2d(c.DX, c.DY), 4, z, true);
                GL.Color3(point.GetColour());
                GLX.Square(new Vector2d(c.DX, c.DY), 3, z, true);
            }
            GL.End();
        }
Пример #27
0
        public void Render2D(Viewport2D viewport, MapObject o)
        {
            if (CBRE.Settings.Select.CenterHandlesActiveViewportOnly && !viewport.IsFocused)
            {
                return;
            }
            var    center = viewport.Flatten(o.BoundingBox.Center);
            double a      = 192;

            if (CBRE.Settings.Select.CenterHandlesFollowCursor)
            {
                var dist = (double)(center - _mousePos).VectorMagnitude();
                if (dist >= _fadeDistance)
                {
                    return;
                }
                a = 192 * ((_fadeDistance - dist) / _fadeDistance);
            }
            GL.Color4(Color.FromArgb((int)a, o.Colour));
            GL.Vertex2(center.DX - _offset, center.DY - _offset);
            GL.Vertex2(center.DX + _offset, center.DY + _offset);
            GL.Vertex2(center.DX - _offset, center.DY + _offset);
            GL.Vertex2(center.DX + _offset, center.DY - _offset);
        }
Пример #28
0
        public override Matrix4?GetTransformationMatrix(Viewport2D viewport, ViewportEvent e, BaseBoxTool.BoxState state, Document doc, IEnumerable <Widget> activeWidgets)
        {
            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)));
        }
Пример #29
0
        protected Coordinate SnapToSelection(Coordinate c, Viewport2D vp)
        {
            if (!Document.Map.SnapToGrid)
            {
                return(c);
            }

            var snap = (Select.SnapStyle == SnapStyle.SnapOnAlt && KeyboardState.Alt) ||
                       (Select.SnapStyle == SnapStyle.SnapOffAlt && !KeyboardState.Alt);

            if (!snap)
            {
                return(c);
            }

            var snapped = c.Snap(Document.Map.GridSpacing);

            if (Document.Selection.InFaceSelection || Document.Selection.IsEmpty())
            {
                return(snapped);
            }

            // Try and snap the the selection box center
            var selBox    = Document.Selection.GetSelectionBoundingBox();
            var selCenter = vp.Flatten(selBox.Center);

            if (DMath.Abs(selCenter.X - c.X) < selBox.Width / 10 && DMath.Abs(selCenter.Y - c.Y) < selBox.Height / 10)
            {
                return(selCenter);
            }

            var objects = Document.Selection.GetSelectedObjects().ToList();

            // Try and snap to an object center
            foreach (var mo in objects)
            {
                if (!(mo is Entity) && !(mo is Solid))
                {
                    continue;
                }
                var center = vp.Flatten(mo.BoundingBox.Center);
                if (DMath.Abs(center.X - c.X) >= mo.BoundingBox.Width / 10)
                {
                    continue;
                }
                if (DMath.Abs(center.Y - c.Y) >= mo.BoundingBox.Height / 10)
                {
                    continue;
                }
                return(center);
            }

            // Get all the edges of the selected objects
            var lines = objects.SelectMany(x =>
            {
                if (x is Entity)
                {
                    return(x.BoundingBox.GetBoxLines());
                }
                if (x is Solid)
                {
                    return(((Solid)x).Faces.SelectMany(f => f.GetLines()));
                }
                return(new Line[0]);
            }).Select(x => new Line(vp.Flatten(x.Start), vp.Flatten(x.End))).ToList();

            // Try and snap to an edge
            var closest = snapped;

            foreach (var line in lines)
            {
                // if the line and the grid are in the same spot, return the snapped point
                if (line.ClosestPoint(snapped).EquivalentTo(snapped))
                {
                    return(snapped);
                }

                // Test for corners and midpoints within a 10% tolerance
                var pointTolerance = (line.End - line.Start).VectorMagnitude() / 10;
                if ((line.Start - c).VectorMagnitude() < pointTolerance)
                {
                    return(line.Start);
                }
                if ((line.End - c).VectorMagnitude() < pointTolerance)
                {
                    return(line.End);
                }

                var center = (line.Start + line.End) / 2;
                if ((center - c).VectorMagnitude() < pointTolerance)
                {
                    return(center);
                }

                // If the line is closer to the grid point, return the line
                var lineSnap = line.ClosestPoint(c);
                if ((closest - c).VectorMagnitude() > (lineSnap - c).VectorMagnitude())
                {
                    closest = lineSnap;
                }
            }
            return(closest);
        }
Пример #30
0
        protected Tuple <Coordinate, Coordinate> GetResizedBoxCoordinates(Viewport2D viewport, ViewportEvent e)
        {
            if (State.Action != BoxAction.Resizing && State.Action != BoxAction.Drawing)
            {
                return(Tuple.Create(State.BoxStart, State.BoxEnd));
            }
            var now    = SnapIfNeeded(viewport.ScreenToWorld(e.X, viewport.Height - e.Y));
            var cstart = viewport.Flatten(State.BoxStart);
            var cend   = viewport.Flatten(State.BoxEnd);

            // Proportional scaling
            var ostart       = viewport.Flatten(State.PreTransformBoxStart ?? Coordinate.Zero);
            var oend         = viewport.Flatten(State.PreTransformBoxEnd ?? Coordinate.Zero);
            var owidth       = oend.X - ostart.X;
            var oheight      = oend.Y - ostart.Y;
            var proportional = KeyboardState.Ctrl && State.Action == BoxAction.Resizing && owidth != 0 && oheight != 0;

            switch (State.Handle)
            {
            case ResizeHandle.TopLeft:
                cstart.X = now.X;
                cend.Y   = now.Y;
                break;

            case ResizeHandle.Top:
                cend.Y = now.Y;
                break;

            case ResizeHandle.TopRight:
                cend.X = now.X;
                cend.Y = now.Y;
                break;

            case ResizeHandle.Left:
                cstart.X = now.X;
                break;

            case ResizeHandle.Center:
                var cdiff    = cend - cstart;
                var distance = GetResizeDistance(viewport, e);
                if (distance == null)
                {
                    cstart = viewport.Flatten(State.PreTransformBoxStart) + now - SnapIfNeeded(State.MoveStart);
                }
                else
                {
                    cstart = viewport.Flatten(State.PreTransformBoxStart) + distance;
                }
                cend = cstart + cdiff;
                break;

            case ResizeHandle.Right:
                cend.X = now.X;
                break;

            case ResizeHandle.BottomLeft:
                cstart.X = now.X;
                cstart.Y = now.Y;
                break;

            case ResizeHandle.Bottom:
                cstart.Y = now.Y;
                break;

            case ResizeHandle.BottomRight:
                cend.X   = now.X;
                cstart.Y = now.Y;
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            if (proportional)
            {
                var nwidth  = cend.X - cstart.X;
                var nheight = cend.Y - cstart.Y;
                var mult    = Math.Max(nwidth / owidth, nheight / oheight);
                var pwidth  = owidth * mult;
                var pheight = oheight * mult;
                var wdiff   = pwidth - nwidth;
                var hdiff   = pheight - nheight;
                switch (State.Handle)
                {
                case ResizeHandle.TopLeft:
                    cstart.X -= wdiff;
                    cend.Y   += hdiff;
                    break;

                case ResizeHandle.TopRight:
                    cend.X += wdiff;
                    cend.Y += hdiff;
                    break;

                case ResizeHandle.BottomLeft:
                    cstart.X -= wdiff;
                    cstart.Y -= hdiff;
                    break;

                case ResizeHandle.BottomRight:
                    cend.X   += wdiff;
                    cstart.Y -= hdiff;
                    break;
                }
            }
            cstart = viewport.Expand(cstart) + viewport.GetUnusedCoordinate(State.BoxStart);
            cend   = viewport.Expand(cend) + viewport.GetUnusedCoordinate(State.BoxEnd);
            return(Tuple.Create(cstart, cend));
        }