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); }
protected virtual void LeftMouseDownToResize(Viewport2D viewport, ViewportEvent e) { State.ActiveViewport = viewport; State.Action = BoxAction.DownToResize; State.MoveStart = viewport.ScreenToWorld(e.X, viewport.Height - e.Y); State.PreTransformBoxStart = State.BoxStart; State.PreTransformBoxEnd = State.BoxEnd; }
protected virtual void LeftMouseDownToDraw(Viewport2D viewport, ViewportEvent e) { State.ActiveViewport = viewport; State.Action = BoxAction.DownToDraw; State.BoxStart = SnapIfNeeded(viewport.Expand(viewport.ScreenToWorld(e.X, viewport.Height - e.Y))); State.BoxEnd = State.BoxStart; State.Handle = ResizeHandle.BottomLeft; OnBoxChanged(); }
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); }
public void BeforeRender2D(Viewport2D viewport) { _offset = 3 / (double)viewport.Zoom; _fadeDistance = 200 / (double)viewport.Zoom; var mp = viewport.PointToClient(Control.MousePosition); _mousePos = viewport.ScreenToWorld(new Coordinate(mp.X, viewport.Height - mp.Y, 0)); GL.Enable(EnableCap.LineSmooth); GL.Begin(PrimitiveType.Lines); }
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))); }
/// <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()); }
protected virtual Coordinate GetResizeDistance(Viewport2D viewport, ViewportEvent e) { var origin = GetResizeOrigin(viewport); if (origin == null) { return(null); } var before = State.MoveStart; var after = viewport.ScreenToWorld(e.X, viewport.Height - e.Y); return(SnapIfNeeded(origin + after - before) - origin); }
private Coordinate GetResizeDistance(Viewport2D viewport, ViewportEvent e, BaseBoxTool.BoxState state, Document document) { var origin = GetResizeOrigin(viewport, state, document); if (origin == null) { return(null); } var before = state.MoveStart; var after = viewport.ScreenToWorld(e.X, viewport.Height - e.Y); return(SnapIfNeeded(origin + after - before, document) - origin); }
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); }
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()); }
/// <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; } }
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; } }
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); }
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); } }
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))); }
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)); }
private Tuple <Coordinate, Coordinate> GetBoxCoordinatesForSelectionResize(Viewport2D viewport, ViewportEvent e, BaseBoxTool.BoxState state, Document document) { if (state.Action != BaseBoxTool.BoxAction.Resizing) { return(Tuple.Create(state.BoxStart, state.BoxEnd)); } var now = SnapIfNeeded(viewport.ScreenToWorld(e.X, viewport.Height - e.Y), document); 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 == BaseBoxTool.BoxAction.Resizing && owidth != 0 && oheight != 0; switch (state.Handle) { case BaseBoxTool.ResizeHandle.TopLeft: cstart.X = Math.Min(now.X, cend.X - 1); cend.Y = Math.Max(now.Y, cstart.Y + 1); break; case BaseBoxTool.ResizeHandle.Top: cend.Y = Math.Max(now.Y, cstart.Y + 1); break; case BaseBoxTool.ResizeHandle.TopRight: cend.X = Math.Max(now.X, cstart.X + 1); cend.Y = Math.Max(now.Y, cstart.Y + 1); break; case BaseBoxTool.ResizeHandle.Left: cstart.X = Math.Min(now.X, cend.X - 1); break; case BaseBoxTool.ResizeHandle.Center: var cdiff = cend - cstart; cstart = viewport.Flatten(state.PreTransformBoxStart) + now - SnapIfNeeded(state.MoveStart, document); cend = cstart + cdiff; break; case BaseBoxTool.ResizeHandle.Right: cend.X = Math.Max(now.X, cstart.X + 1); break; case BaseBoxTool.ResizeHandle.BottomLeft: cstart.X = Math.Min(now.X, cend.X - 1); cstart.Y = Math.Min(now.Y, cend.Y - 1); break; case BaseBoxTool.ResizeHandle.Bottom: cstart.Y = Math.Min(now.Y, cend.Y - 1); break; case BaseBoxTool.ResizeHandle.BottomRight: cend.X = Math.Max(now.X, cstart.X + 1); cstart.Y = Math.Min(now.Y, cend.Y - 1); 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 BaseBoxTool.ResizeHandle.TopLeft: cstart.X -= wdiff; cend.Y += hdiff; break; case BaseBoxTool.ResizeHandle.TopRight: cend.X += wdiff; cend.Y += hdiff; break; case BaseBoxTool.ResizeHandle.BottomLeft: cstart.X -= wdiff; cstart.Y -= hdiff; break; case BaseBoxTool.ResizeHandle.BottomRight: cend.X += wdiff; cstart.Y -= hdiff; break; } } return(SnapBoxCoordinatesIfNeeded(viewport, state, document, cstart, cend)); }