2014-05-02 11:37:37 +00:00
|
|
|
|
#region Namespaces
|
|
|
|
|
|
|
|
|
|
//Downloaded from
|
2013-06-25 12:35:13 +00:00
|
|
|
|
//Visual C# Kicks - http://vckicks.110mb.com
|
|
|
|
|
//The Code Project - http://www.codeproject.com
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Drawing2D;
|
|
|
|
|
using System.Windows.Forms;
|
2014-03-10 14:35:38 +00:00
|
|
|
|
using CodeImp.DoomBuilder.Geometry;
|
2013-06-25 12:35:13 +00:00
|
|
|
|
|
2014-05-02 11:37:37 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2013-06-25 12:35:13 +00:00
|
|
|
|
namespace CodeImp.DoomBuilder.GZBuilder.Controls
|
|
|
|
|
{
|
|
|
|
|
public partial class AngleControl : UserControl
|
|
|
|
|
{
|
2014-05-02 11:37:37 +00:00
|
|
|
|
#region Variables
|
|
|
|
|
|
2013-06-25 12:35:13 +00:00
|
|
|
|
private int angle;
|
2014-09-03 14:07:08 +00:00
|
|
|
|
private int angleoffset;
|
2013-06-25 12:35:13 +00:00
|
|
|
|
|
2013-09-11 09:47:53 +00:00
|
|
|
|
private Rectangle drawRegion;
|
2014-05-02 11:37:37 +00:00
|
|
|
|
private const int drawOffset = 2;
|
2013-09-11 09:47:53 +00:00
|
|
|
|
private Point origin;
|
2013-06-25 12:35:13 +00:00
|
|
|
|
|
2013-12-04 13:27:34 +00:00
|
|
|
|
//UI colors
|
|
|
|
|
private readonly Color fillColor = Color.FromArgb(90, 255, 255, 255);
|
|
|
|
|
private readonly Color fillInactiveColor = SystemColors.InactiveCaption;
|
|
|
|
|
private readonly Color outlineColor = Color.FromArgb(86, 103, 141);
|
|
|
|
|
private readonly Color outlineInactiveColor = SystemColors.InactiveBorder;
|
|
|
|
|
|
2014-05-02 11:37:37 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Properties
|
|
|
|
|
|
|
|
|
|
public delegate void AngleChangedDelegate();
|
|
|
|
|
public event AngleChangedDelegate AngleChanged;
|
|
|
|
|
|
2014-09-03 14:07:08 +00:00
|
|
|
|
public int Angle { get { return angle - angleoffset; } set { angle = value + angleoffset; this.Refresh(); } }
|
|
|
|
|
public int AngleOffset { get { return angleoffset; } set { angleoffset = value; this.Refresh(); } }
|
2014-05-02 11:37:37 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public AngleControl()
|
2013-09-11 09:47:53 +00:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
this.DoubleBuffered = true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-02 11:37:37 +00:00
|
|
|
|
#region Methods
|
2013-09-11 09:47:53 +00:00
|
|
|
|
|
2014-05-02 11:37:37 +00:00
|
|
|
|
private void setDrawRegion()
|
2013-09-11 09:47:53 +00:00
|
|
|
|
{
|
|
|
|
|
drawRegion = new Rectangle(0, 0, this.Width, this.Height);
|
|
|
|
|
drawRegion.X += 2;
|
|
|
|
|
drawRegion.Y += 2;
|
|
|
|
|
drawRegion.Width -= 4;
|
|
|
|
|
drawRegion.Height -= 4;
|
|
|
|
|
|
2014-05-02 11:37:37 +00:00
|
|
|
|
origin = new Point(drawRegion.Width / 2 + drawOffset, drawRegion.Height / 2 + drawOffset);
|
2013-09-11 09:47:53 +00:00
|
|
|
|
|
|
|
|
|
this.Refresh();
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-02 11:37:37 +00:00
|
|
|
|
private static PointF DegreesToXY(float degrees, float radius, Point origin)
|
2013-09-11 09:47:53 +00:00
|
|
|
|
{
|
|
|
|
|
PointF xy = new PointF();
|
2014-03-10 14:35:38 +00:00
|
|
|
|
float radians = degrees * Angle2D.PI / 180.0f;
|
2013-09-11 09:47:53 +00:00
|
|
|
|
|
|
|
|
|
xy.X = (float)Math.Cos(radians) * radius + origin.X;
|
|
|
|
|
xy.Y = (float)Math.Sin(-radians) * radius + origin.Y;
|
|
|
|
|
|
|
|
|
|
return xy;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-02 11:37:37 +00:00
|
|
|
|
private static int XYToDegrees(Point xy, Point origin)
|
2013-09-11 09:47:53 +00:00
|
|
|
|
{
|
2013-12-04 13:27:34 +00:00
|
|
|
|
float xDiff = xy.X - origin.X;
|
|
|
|
|
float yDiff = xy.Y - origin.Y;
|
2014-03-10 14:35:38 +00:00
|
|
|
|
return ((int)Math.Round(Math.Atan2(-yDiff, xDiff) * 180.0 / Angle2D.PI) + 360) % 360;
|
2013-09-11 09:47:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-02 11:37:37 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Events
|
|
|
|
|
|
|
|
|
|
private void AngleSelector_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
setDrawRegion();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AngleSelector_SizeChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
this.Height = this.Width; //Keep it a square
|
|
|
|
|
setDrawRegion();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnPaint(PaintEventArgs e)
|
2013-09-11 09:47:53 +00:00
|
|
|
|
{
|
|
|
|
|
Graphics g = e.Graphics;
|
|
|
|
|
|
2013-12-04 13:27:34 +00:00
|
|
|
|
Pen outline;
|
|
|
|
|
Pen needle;
|
|
|
|
|
SolidBrush fill;
|
|
|
|
|
Brush center;
|
|
|
|
|
|
|
|
|
|
if (this.Enabled) {
|
|
|
|
|
outline = new Pen(outlineColor, 2.0f);
|
|
|
|
|
fill = new SolidBrush(fillColor);
|
|
|
|
|
needle = Pens.Black;
|
|
|
|
|
center = Brushes.Black;
|
|
|
|
|
} else {
|
|
|
|
|
outline = new Pen(outlineInactiveColor, 2.0f);
|
|
|
|
|
fill = new SolidBrush(fillInactiveColor);
|
|
|
|
|
needle = Pens.DarkGray;
|
|
|
|
|
center = Brushes.DarkGray;
|
|
|
|
|
}
|
2013-09-11 09:47:53 +00:00
|
|
|
|
|
|
|
|
|
Rectangle originSquare = new Rectangle(origin.X - 1, origin.Y - 1, 3, 3);
|
|
|
|
|
|
|
|
|
|
//Draw
|
|
|
|
|
g.SmoothingMode = SmoothingMode.AntiAlias;
|
|
|
|
|
g.DrawEllipse(outline, drawRegion);
|
|
|
|
|
g.FillEllipse(fill, drawRegion);
|
2014-05-02 11:37:37 +00:00
|
|
|
|
|
|
|
|
|
if (angle != int.MinValue) {
|
|
|
|
|
PointF anglePoint = DegreesToXY(angle, origin.X - 2, origin);
|
|
|
|
|
g.DrawLine(needle, origin, anglePoint);
|
|
|
|
|
}
|
2013-09-11 09:47:53 +00:00
|
|
|
|
|
|
|
|
|
g.SmoothingMode = SmoothingMode.HighSpeed; //Make the square edges sharp
|
2013-12-04 13:27:34 +00:00
|
|
|
|
g.FillRectangle(center, originSquare);
|
2013-09-11 09:47:53 +00:00
|
|
|
|
|
|
|
|
|
fill.Dispose();
|
|
|
|
|
outline.Dispose();
|
|
|
|
|
|
|
|
|
|
base.OnPaint(e);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-02 11:37:37 +00:00
|
|
|
|
private void AngleSelector_MouseDown(object sender, MouseEventArgs e)
|
|
|
|
|
{
|
2013-12-04 13:27:34 +00:00
|
|
|
|
int thisAngle = XYToDegrees(new Point(e.X, e.Y), origin);
|
2013-09-11 09:47:53 +00:00
|
|
|
|
|
2013-12-04 13:27:34 +00:00
|
|
|
|
if (e.Button == MouseButtons.Left) {
|
|
|
|
|
thisAngle = (int)Math.Round(thisAngle / 45f) * 45;
|
2014-03-11 09:44:39 +00:00
|
|
|
|
if(thisAngle == 360) thisAngle = 0;
|
2013-12-04 13:27:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-03 14:07:08 +00:00
|
|
|
|
if(thisAngle != angle) {
|
|
|
|
|
angle = thisAngle;
|
2013-12-04 13:27:34 +00:00
|
|
|
|
if(!this.DesignMode && AngleChanged != null) AngleChanged(); //Raise event
|
2013-09-11 09:47:53 +00:00
|
|
|
|
this.Refresh();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-02 11:37:37 +00:00
|
|
|
|
private void AngleSelector_MouseMove(object sender, MouseEventArgs e)
|
|
|
|
|
{
|
2013-12-04 13:27:34 +00:00
|
|
|
|
if (e.Button == MouseButtons.Left || e.Button == MouseButtons.Right) {
|
|
|
|
|
int thisAngle = XYToDegrees(new Point(e.X, e.Y), origin);
|
2013-09-11 09:47:53 +00:00
|
|
|
|
|
2013-12-04 13:27:34 +00:00
|
|
|
|
if(e.Button == MouseButtons.Left) {
|
|
|
|
|
thisAngle = (int)Math.Round(thisAngle / 45f) * 45;
|
2014-05-02 11:37:37 +00:00
|
|
|
|
if(thisAngle == 360) thisAngle = 0;
|
2013-12-04 13:27:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-03 14:07:08 +00:00
|
|
|
|
if(thisAngle != angle) {
|
|
|
|
|
angle = thisAngle;
|
2013-12-04 13:27:34 +00:00
|
|
|
|
if(!this.DesignMode && AngleChanged != null) AngleChanged(); //Raise event
|
2013-09-11 09:47:53 +00:00
|
|
|
|
this.Refresh();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-02 11:37:37 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
2013-06-25 12:35:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|