Subversion Repositories AndroidProjects

Rev

Rev 1418 | 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
 
30
        public override void MouseMove(MouseEventArgs e)
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
39
                    if (mVertices.Count > 0)
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
 
1419 chris 46
                            mParent.CurrentDoc.AddPolygon(mVertices);
1418 chris 47
 
48
                            mVertices.Clear();
49
                            return;
50
                        }
51
                    }
52
 
53
                    mVertices.Add(new Vector2(e.X, e.Y));
54
                }
55
                else
56
                {
57
                    // dragging
58
                    mVertices.Last().set(e.X, e.Y);
59
                }
60
            }
61
 
62
            mParent.GlView.Cursor = Cursors.Cross;
63
 
64
            if (mVertices.Count > 0)
65
            {
66
                Vector2 first = mVertices.First();
67
                if ((Math.Abs(first.x - e.X) <= CLOSING_THRESHOLD) && (Math.Abs(first.x - e.X) <= CLOSING_THRESHOLD))
68
                {
69
                    mParent.GlView.Cursor = Cursors.Hand;
70
                }
71
            }
72
 
73
            mLastX = e.X;
74
            mLastY = e.Y;
1399 chris 75
        }
76
 
77
        public override void MouseUp(MouseEventArgs e)
78
        {
1418 chris 79
            if (e.Button == MouseButtons.Left)
80
            {
81
                if (mLeftButtonDown)
82
                {
83
                    mVertices.Last().set(e.X, e.Y);
84
                    mLeftButtonDown = false;
85
                }
86
            }
1399 chris 87
        }
1400 chris 88
 
89
        public override void Render()
90
        {
1418 chris 91
            for (int i = 0; i < (mVertices.Count-1); i++)
92
            {
93
                Vector2 a = mVertices.ElementAt(i);
94
                Vector2 b = mVertices.ElementAt(i+1);
95
                RenderUtil.drawLine(MainForm.App.getGraphics(), a.x, a.y, b.x, b.y, RECTANGLE_CREATE_COLOR);
96
            }
97
 
98
            if (mVertices.Count > 0)
99
            {
100
                Vector2 a = mVertices.Last();
101
                RenderUtil.drawLine(MainForm.App.getGraphics(), a.x, a.y, mLastX, mLastY, RECTANGLE_CREATE_COLOR);
102
            }
1400 chris 103
        }
1399 chris 104
    }
105
}