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;
|
2017-01-04 13:28:36 +00:00
|
|
|
|
using System.ComponentModel;
|
2013-06-25 12:35:13 +00:00
|
|
|
|
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
|
|
|
|
|
|
2016-04-27 09:13:07 +00:00
|
|
|
|
namespace CodeImp.DoomBuilder.Controls
|
2013-06-25 12:35:13 +00:00
|
|
|
|
{
|
2016-04-27 09:13:07 +00:00
|
|
|
|
public partial class AngleControlEx : UserControl
|
2013-06-25 12:35:13 +00:00
|
|
|
|
{
|
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;
|
2014-09-24 08:48:13 +00:00
|
|
|
|
private const int markScaler = 5;
|
2013-09-11 09:47:53 +00:00
|
|
|
|
private Point origin;
|
2013-06-25 12:35:13 +00:00
|
|
|
|
|
2016-06-15 22:02:51 +00:00
|
|
|
|
private bool doomangleclamping;
|
|
|
|
|
|
2013-12-04 13:27:34 +00:00
|
|
|
|
//UI colors
|
2014-09-24 08:48:13 +00:00
|
|
|
|
private readonly Color fillColor = SystemColors.Window;
|
|
|
|
|
private readonly Color fillInactiveColor = SystemColors.Control;
|
2014-09-24 11:47:52 +00:00
|
|
|
|
private readonly Color outlineColor = SystemColors.WindowFrame;
|
2014-09-24 08:48:13 +00:00
|
|
|
|
private readonly Color outlineInactiveColor = SystemColors.ControlDarkDark;
|
|
|
|
|
private readonly Color needleColor = SystemColors.ControlText;
|
|
|
|
|
private readonly Color needleInactiveColor = SystemColors.ControlDarkDark;
|
|
|
|
|
private readonly Color marksColor = SystemColors.ActiveBorder;
|
|
|
|
|
private readonly Color marksInactiveColor = SystemColors.ControlDark;
|
2013-12-04 13:27:34 +00:00
|
|
|
|
|
2014-05-02 11:37:37 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Properties
|
|
|
|
|
|
2014-12-24 18:32:24 +00:00
|
|
|
|
public event EventHandler AngleChanged;
|
2014-05-02 11:37:37 +00:00
|
|
|
|
|
2017-01-04 13:28:36 +00:00
|
|
|
|
public int Angle { get { return (angle == NO_ANGLE ? NO_ANGLE : angle - angleoffset); } set {
|
|
|
|
|
if(LicenseManager.UsageMode != LicenseUsageMode.Designtime)
|
|
|
|
|
{
|
|
|
|
|
angle = (value == NO_ANGLE ? NO_ANGLE : value + angleoffset);
|
|
|
|
|
this.Refresh();
|
|
|
|
|
}
|
|
|
|
|
} }
|
2014-09-03 14:07:08 +00:00
|
|
|
|
public int AngleOffset { get { return angleoffset; } set { angleoffset = value; this.Refresh(); } }
|
2016-06-15 22:02:51 +00:00
|
|
|
|
public bool DoomAngleClamping { get { return doomangleclamping; } set { doomangleclamping = value; } }
|
2015-12-28 15:01:53 +00:00
|
|
|
|
public const int NO_ANGLE = int.MinValue;
|
2014-05-02 11:37:37 +00:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2016-04-27 09:13:07 +00:00
|
|
|
|
public AngleControlEx()
|
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-12-03 23:15:26 +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)
|
|
|
|
|
{
|
2014-12-03 23:15:26 +00:00
|
|
|
|
SetDrawRegion();
|
2014-05-02 11:37:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AngleSelector_SizeChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
2014-09-24 08:48:13 +00:00
|
|
|
|
this.Height = this.Width; // Keep it there and keep it square!
|
2014-12-03 23:15:26 +00:00
|
|
|
|
SetDrawRegion();
|
2014-05-02 11:37:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2014-09-24 08:48:13 +00:00
|
|
|
|
Pen marks;
|
2013-12-04 13:27:34 +00:00
|
|
|
|
SolidBrush fill;
|
|
|
|
|
Brush center;
|
|
|
|
|
|
2015-12-28 15:01:53 +00:00
|
|
|
|
if(this.Enabled)
|
2014-10-17 11:55:08 +00:00
|
|
|
|
{
|
2013-12-04 13:27:34 +00:00
|
|
|
|
outline = new Pen(outlineColor, 2.0f);
|
|
|
|
|
fill = new SolidBrush(fillColor);
|
2014-09-24 08:48:13 +00:00
|
|
|
|
needle = new Pen(needleColor);
|
|
|
|
|
center = new SolidBrush(needleColor);
|
|
|
|
|
marks = new Pen(marksColor);
|
2014-10-17 11:55:08 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-12-04 13:27:34 +00:00
|
|
|
|
outline = new Pen(outlineInactiveColor, 2.0f);
|
|
|
|
|
fill = new SolidBrush(fillInactiveColor);
|
2014-09-24 08:48:13 +00:00
|
|
|
|
needle = new Pen(needleInactiveColor);
|
|
|
|
|
center = new SolidBrush(needleInactiveColor);
|
|
|
|
|
marks = new Pen(marksInactiveColor);
|
2013-12-04 13:27:34 +00:00
|
|
|
|
}
|
2013-09-11 09:47:53 +00:00
|
|
|
|
|
|
|
|
|
Rectangle originSquare = new Rectangle(origin.X - 1, origin.Y - 1, 3, 3);
|
|
|
|
|
|
2014-09-24 08:48:13 +00:00
|
|
|
|
//Draw circle
|
2013-09-11 09:47:53 +00:00
|
|
|
|
g.SmoothingMode = SmoothingMode.AntiAlias;
|
|
|
|
|
g.DrawEllipse(outline, drawRegion);
|
|
|
|
|
g.FillEllipse(fill, drawRegion);
|
2014-05-02 11:37:37 +00:00
|
|
|
|
|
2014-09-24 08:48:13 +00:00
|
|
|
|
// Draw angle marks
|
|
|
|
|
int offset = this.Height / markScaler;
|
2014-10-17 11:55:08 +00:00
|
|
|
|
for(int i = 0; i < 360; i += 45)
|
|
|
|
|
{
|
2014-09-24 08:48:13 +00:00
|
|
|
|
PointF p1 = DegreesToXY(i, origin.X - 6, origin);
|
|
|
|
|
PointF p2 = DegreesToXY(i, origin.X - offset, origin);
|
|
|
|
|
g.DrawLine(marks, p1, p2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Draw needle
|
2014-10-17 11:55:08 +00:00
|
|
|
|
if(angle != NO_ANGLE)
|
|
|
|
|
{
|
2014-09-24 08:48:13 +00:00
|
|
|
|
PointF anglePoint = DegreesToXY(angle, origin.X - 4, origin);
|
2014-05-02 11:37:37 +00:00
|
|
|
|
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
|
|
|
|
|
2015-05-27 10:34:25 +00:00
|
|
|
|
//mxd. Dispose brushes
|
2013-09-11 09:47:53 +00:00
|
|
|
|
fill.Dispose();
|
2015-05-27 10:34:25 +00:00
|
|
|
|
center.Dispose();
|
2013-09-11 09:47:53 +00:00
|
|
|
|
outline.Dispose();
|
2015-05-27 10:34:25 +00:00
|
|
|
|
marks.Dispose();
|
|
|
|
|
needle.Dispose();
|
2013-09-11 09:47:53 +00:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-12-28 15:01:53 +00:00
|
|
|
|
if(e.Button == MouseButtons.Left)
|
2014-10-17 11:55:08 +00:00
|
|
|
|
{
|
2013-12-04 13:27:34 +00:00
|
|
|
|
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-10-17 11:55:08 +00:00
|
|
|
|
if(thisAngle != angle)
|
|
|
|
|
{
|
2014-09-03 14:07:08 +00:00
|
|
|
|
angle = thisAngle;
|
2014-12-24 18:32:24 +00:00
|
|
|
|
if(!this.DesignMode && AngleChanged != null) AngleChanged(this, EventArgs.Empty); //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)
|
|
|
|
|
{
|
2015-12-28 15:01:53 +00:00
|
|
|
|
if(e.Button == MouseButtons.Left || e.Button == MouseButtons.Right)
|
2014-10-17 11:55:08 +00:00
|
|
|
|
{
|
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
|
|
|
|
|
2016-06-15 22:02:51 +00:00
|
|
|
|
if(e.Button == MouseButtons.Left || doomangleclamping)
|
2014-10-17 11:55:08 +00:00
|
|
|
|
{
|
2013-12-04 13:27:34 +00:00
|
|
|
|
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-10-17 11:55:08 +00:00
|
|
|
|
if(thisAngle != angle)
|
|
|
|
|
{
|
2014-09-03 14:07:08 +00:00
|
|
|
|
angle = thisAngle;
|
2014-12-24 18:32:24 +00:00
|
|
|
|
if(!this.DesignMode && AngleChanged != null) AngleChanged(this, EventArgs.Empty); //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
|
|
|
|
}
|
|
|
|
|
}
|