Rev 1420 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1399 | chris | 1 | using System; |
| 2 | using System.Collections.Generic; |
||
| 3 | using System.Linq; |
||
| 4 | using System.Text; |
||
| 5 | using System.Threading.Tasks; |
||
| 6 | using System.Windows.Forms; |
||
| 7 | |||
| 1418 | chris | 8 | using BauzoidNET.math; |
| 9 | using BauzoidNET.graphics; |
||
| 10 | |||
| 1399 | chris | 11 | namespace ShapeEditor.interaction |
| 12 | { |
||
| 13 | public class PolygonMode : InteractionMode |
||
| 14 | { |
||
| 1418 | chris | 15 | public static readonly Vector4 RECTANGLE_CREATE_COLOR = new Vector4(0.6f, 0.6f, 0.6f, 1.0f); |
| 1399 | chris | 16 | |
| 1418 | chris | 17 | public const int CLOSING_THRESHOLD = 2; |
| 18 | |||
| 19 | private int mLastX = 0; |
||
| 20 | private int mLastY = 0; |
||
| 21 | private bool mLeftButtonDown = false; |
||
| 22 | |||
| 23 | private List<Vector2> mVertices = new List<Vector2>(); |
||
| 24 | |||
| 1399 | chris | 25 | public PolygonMode(MainForm parent) |
| 1404 | chris | 26 | : base(parent, InteractionMode.POLYGON) |
| 1399 | chris | 27 | { |
| 28 | } |
||
| 29 | |||
| 1420 | chris | 30 | public override void MouseMove(MouseEventArgs e, bool isMouseDown = false) |
| 1399 | chris | 31 | { |
| 1418 | chris | 32 | if (e.Button == MouseButtons.Left) |
| 33 | { |
||
| 34 | if (!mLeftButtonDown) |
||
| 35 | { |
||
| 36 | // just pressed |
||
| 37 | |||
| 38 | // if coordinates are last ones, close |
||
| 1420 | chris | 39 | if (mVertices.Count >= 3) |
| 1418 | chris | 40 | { |
| 41 | Vector2 first = mVertices.First(); |
||
| 42 | if ((Math.Abs(first.x - e.X) <= CLOSING_THRESHOLD) && (Math.Abs(first.x - e.X) <= CLOSING_THRESHOLD)) |
||
| 43 | { |
||
| 44 | // close polygon |
||
| 45 | |||
| 1421 | chris | 46 | /*float x = Math.Min(mLastX, mStartX) / mParent.ZoomFactor - mParent.CurrentDoc.Sprite.transform.x / mParent.ZoomFactor; |
| 47 | float y = Math.Min(mLastY, mStartY) / mParent.ZoomFactor - mParent.CurrentDoc.Sprite.transform.y / mParent.ZoomFactor; |
||
| 48 | float w = Math.Abs(mLastX - mStartX) / mParent.ZoomFactor; |
||
| 49 | float h = Math.Abs(mLastY - mStartY) / mParent.ZoomFactor;*/ |
||
| 50 | for (int i = 0; i < mVertices.Count; i++) |
||
| 51 | { |
||
| 52 | mVertices[i].x -= mParent.CurrentDoc.Sprite.transform.x; |
||
| 53 | mVertices[i].x /= mParent.ZoomFactor; |
||
| 54 | mVertices[i].y -= mParent.CurrentDoc.Sprite.transform.y; |
||
| 55 | mVertices[i].y /= mParent.ZoomFactor; |
||
| 56 | } |
||
| 57 | |||
| 1419 | chris | 58 | mParent.CurrentDoc.AddPolygon(mVertices); |
| 1418 | chris | 59 | |
| 60 | mVertices.Clear(); |
||
| 61 | return; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 1420 | chris | 65 | if (isMouseDown) |
| 66 | { |
||
| 67 | mVertices.Add(new Vector2(e.X, e.Y)); |
||
| 68 | mLeftButtonDown = true; |
||
| 69 | } |
||
| 1418 | chris | 70 | } |
| 71 | else |
||
| 72 | { |
||
| 73 | // dragging |
||
| 74 | mVertices.Last().set(e.X, e.Y); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 1420 | chris | 78 | if ((mVertices.Count >= 3) && |
| 79 | ((Math.Abs(mVertices.First().x - e.X) <= CLOSING_THRESHOLD) && (Math.Abs(mVertices.First().x - e.X) <= CLOSING_THRESHOLD))) |
||
| 1418 | chris | 80 | { |
| 1420 | chris | 81 | mParent.GlView.Cursor = Cursors.Hand; |
| 1418 | chris | 82 | } |
| 1420 | chris | 83 | else |
| 84 | { |
||
| 85 | mParent.GlView.Cursor = Cursors.Cross; |
||
| 86 | } |
||
| 1418 | chris | 87 | |
| 88 | mLastX = e.X; |
||
| 89 | mLastY = e.Y; |
||
| 1399 | chris | 90 | } |
| 91 | |||
| 92 | public override void MouseUp(MouseEventArgs e) |
||
| 93 | { |
||
| 1418 | chris | 94 | if (e.Button == MouseButtons.Left) |
| 95 | { |
||
| 96 | if (mLeftButtonDown) |
||
| 97 | { |
||
| 98 | mVertices.Last().set(e.X, e.Y); |
||
| 99 | mLeftButtonDown = false; |
||
| 100 | } |
||
| 101 | } |
||
| 1399 | chris | 102 | } |
| 1400 | chris | 103 | |
| 104 | public override void Render() |
||
| 105 | { |
||
| 1418 | chris | 106 | for (int i = 0; i < (mVertices.Count-1); i++) |
| 107 | { |
||
| 108 | Vector2 a = mVertices.ElementAt(i); |
||
| 109 | Vector2 b = mVertices.ElementAt(i+1); |
||
| 110 | RenderUtil.drawLine(MainForm.App.getGraphics(), a.x, a.y, b.x, b.y, RECTANGLE_CREATE_COLOR); |
||
| 111 | } |
||
| 112 | |||
| 113 | if (mVertices.Count > 0) |
||
| 114 | { |
||
| 115 | Vector2 a = mVertices.Last(); |
||
| 116 | RenderUtil.drawLine(MainForm.App.getGraphics(), a.x, a.y, mLastX, mLastY, RECTANGLE_CREATE_COLOR); |
||
| 117 | } |
||
| 1400 | chris | 118 | } |
| 1399 | chris | 119 | } |
| 120 | } |