Rev 1421 |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BauzoidNET.math;
using BauzoidNET.graphics;
namespace ShapeEditor
.interaction
{
public class PolygonMode
: InteractionMode
{
public static readonly Vector4 RECTANGLE_CREATE_COLOR
= new Vector4
(0
.6f, 0
.6f, 0
.6f, 1
.0f
);
public const int CLOSING_THRESHOLD
= 2;
private int mLastX
= 0;
private int mLastY
= 0;
private bool mLeftButtonDown
= false;
private List
<Vector2
> mVertices
= new List
<Vector2
>();
public PolygonMode
(MainForm parent
)
: base(parent, InteractionMode
.POLYGON)
{
}
public override void MouseMove
(MouseEventArgs e,
bool isMouseDown
= false)
{
if (e
.Button == MouseButtons
.Left)
{
if (!mLeftButtonDown
)
{
// just pressed
// if coordinates are last ones, close
if (mVertices
.Count >= 3)
{
Vector2 first
= mVertices
.First();
if ((Math
.Abs(first
.x - e
.X) <= CLOSING_THRESHOLD
) && (Math
.Abs(first
.x - e
.X) <= CLOSING_THRESHOLD
))
{
// close polygon
/*float x = Math.Min(mLastX, mStartX) / mParent.ZoomFactor - mParent.CurrentDoc.Sprite.transform.x / mParent.ZoomFactor;
float y = Math.Min(mLastY, mStartY) / mParent.ZoomFactor - mParent.CurrentDoc.Sprite.transform.y / mParent.ZoomFactor;
float w = Math.Abs(mLastX - mStartX) / mParent.ZoomFactor;
float h = Math.Abs(mLastY - mStartY) / mParent.ZoomFactor;*/
for (int i
= 0; i
< mVertices
.Count; i
++)
{
mVertices
[i
].x -= mParent
.CurrentDoc.Sprite.transform.x;
mVertices
[i
].x /= mParent
.ZoomFactor;
mVertices
[i
].y -= mParent
.CurrentDoc.Sprite.transform.y;
mVertices
[i
].y /= mParent
.ZoomFactor;
}
mParent
.CurrentDoc.AddPolygon(mVertices,
-1,
-1);
mVertices
.Clear();
return;
}
}
if (isMouseDown
)
{
mVertices
.Add(new Vector2
(e
.X, e
.Y));
mLeftButtonDown
= true;
}
}
else
{
// dragging
mVertices
.Last().set(e
.X, e
.Y);
}
}
if ((mVertices
.Count >= 3) &&
((Math
.Abs(mVertices
.First().x - e
.X) <= CLOSING_THRESHOLD
) && (Math
.Abs(mVertices
.First().x - e
.X) <= CLOSING_THRESHOLD
)))
{
mParent
.GlView.Cursor = Cursors
.Hand;
}
else
{
mParent
.GlView.Cursor = Cursors
.Cross;
}
mLastX
= e
.X;
mLastY
= e
.Y;
}
public override void MouseUp
(MouseEventArgs e
)
{
if (e
.Button == MouseButtons
.Left)
{
if (mLeftButtonDown
)
{
mVertices
.Last().set(e
.X, e
.Y);
mLeftButtonDown
= false;
}
}
}
public override void Render
()
{
for (int i
= 0; i
< (mVertices
.Count-1); i
++)
{
Vector2 a
= mVertices
.ElementAt(i
);
Vector2 b
= mVertices
.ElementAt(i
+1);
RenderUtil
.drawLine(MainForm
.App.getGraphics(), a
.x, a
.y, b
.x, b
.y, RECTANGLE_CREATE_COLOR
);
}
if (mVertices
.Count > 0)
{
Vector2 a
= mVertices
.Last();
RenderUtil
.drawLine(MainForm
.App.getGraphics(), a
.x, a
.y, mLastX, mLastY, RECTANGLE_CREATE_COLOR
);
}
}
}
}