Skip to content

Commit

Permalink
Extracted settings
Browse files Browse the repository at this point in the history
  • Loading branch information
lkosson committed May 1, 2023
1 parent bec441a commit e34932f
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 86 deletions.
56 changes: 25 additions & 31 deletions Canvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,8 @@ public ViewState ViewState
}
}

public bool ShowFPS { get; set; } = false;
public bool ShowCursorCoords { get; set; } = true;
public bool ShowItemCoords { get; set; } = true;
public bool ShowMinorGrid { get; set; } = true;
public bool ShowMajorGrid { get; set; } = true;
public bool ShowOriginGrid { get; set; } = true;
public bool SnapToGrid { get; set; } = true;
public bool SnapToItems { get; set; } = true;
public bool SnapToAxes { get; set; } = true;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Settings Settings { get => settings; set { settings = value; style.Settings = value; } }

private ViewState viewState;
private Matrix viewMatrix;
Expand All @@ -57,20 +50,21 @@ public ViewState ViewState
private CanvasStyle style;
private IEnumerable<CanvasItem> items;
private bool panningToSelectionSuspended;
private Settings settings;

private Interaction interaction;
private bool panningInProgress;
private bool itemAddingInProgress;

private enum Interaction { None, Select, EndMove, OffsetMove, Translate, Rotate, Scale }

private float GridMinorStep => (float)Math.Pow(10, 1 + Math.Floor(Math.Log10(ViewToAbs(10))));
private float GridMajorStep => GridMinorStep * 10;
private float HintSnapDistance => 2;
private float GridMinorStep => settings.GridMinorStep ?? (float)Math.Pow(10, 1 + Math.Floor(Math.Log10(ViewToAbs(10))));
private float GridMajorStep => settings.GridMajorStep ?? GridMinorStep * 10;

public Canvas()
{
DoubleBuffered = true;
settings = new Settings();
viewState = new ViewState();
viewMatrix = new Matrix();
inverseViewMatrix = new Matrix();
Expand Down Expand Up @@ -211,18 +205,18 @@ protected override void OnPaint(PaintEventArgs e)
sw.Stop();
var paintTime = (int)sw.ElapsedMilliseconds;

if (ShowFPS)
if (Settings.ShowFPS)
{
e.Graphics.DrawString($"{paintTime} ms\n{(paintTime == 0 ? 999 : 1000 / paintTime)} fps\n{itemCount} items\n{visCount} visible", Font, style.TextBrush, 0, 0);
}

if (ShowCursorCoords || ShowItemCoords)
if (Settings.ShowCursorCoords || Settings.ShowItemCoords)
{
var absMouse = ViewToAbs(PointToClient(MousePosition));
var coords = "";
if (ShowCursorCoords) coords += $"X={absMouse.X.ToString("0.000", CultureInfo.InvariantCulture)}, Y={absMouse.Y.ToString("0.000", CultureInfo.InvariantCulture)}";
if (ShowCursorCoords && ShowItemCoords) coords += "\n";
if (ShowItemCoords && ViewState.LastSelectedOperation != null)
if (Settings.ShowCursorCoords) coords += $"X={absMouse.X.ToString("0.000", CultureInfo.InvariantCulture)}, Y={absMouse.Y.ToString("0.000", CultureInfo.InvariantCulture)}";
if (Settings.ShowCursorCoords && Settings.ShowItemCoords) coords += "\n";
if (Settings.ShowItemCoords && ViewState.LastSelectedOperation != null)
{
coords += ViewState.LastSelectedOperation.Line.Instruction
+ " X=" + ViewState.LastSelectedOperation.AbsXStart.ToString("0.000", CultureInfo.InvariantCulture)
Expand All @@ -243,7 +237,7 @@ protected override void OnPaint(PaintEventArgs e)

private void PaintGrid(Graphics g, RectangleF absClipRect)
{
if (!ShowMajorGrid && !ShowMinorGrid && !ShowOriginGrid) return;
if (!Settings.ShowMajorGrid && !Settings.ShowMinorGrid && !Settings.ShowOriginGrid) return;

var gs = g.Save();
g.SmoothingMode = SmoothingMode.None;
Expand All @@ -258,7 +252,7 @@ private void PaintGrid(Graphics g, RectangleF absClipRect)
var horizGridStart = absArea.Top - absArea.Top % absStepMajor - (absArea.Top < 0 ? absStepMajor : 0);
var horizGridEnd = absArea.Bottom - absArea.Bottom % absStepMajor + (absArea.Bottom < 0 ? absStepMajor : 0) + absStepMajor;

if (ShowMinorGrid)
if (Settings.ShowMinorGrid)
{
for (var x = vertGridStart; x < vertGridEnd; x += absStepMinor)
g.DrawLine(style.MinorGridPen, x, absClipRect.Top, x, absClipRect.Bottom);
Expand All @@ -267,7 +261,7 @@ private void PaintGrid(Graphics g, RectangleF absClipRect)
g.DrawLine(style.MinorGridPen, absClipRect.Left, y, absClipRect.Right, y);
}

if (ShowMajorGrid)
if (Settings.ShowMajorGrid)
{
for (var x = vertGridStart; x < vertGridEnd; x += absStepMajor)
g.DrawLine(style.MajorGridPen, x, absClipRect.Top, x, absClipRect.Bottom);
Expand All @@ -276,7 +270,7 @@ private void PaintGrid(Graphics g, RectangleF absClipRect)
g.DrawLine(style.MajorGridPen, absClipRect.Left, y, absClipRect.Right, y);
}

if (ShowOriginGrid)
if (Settings.ShowOriginGrid)
{
g.DrawLine(style.OriginGridPen, 0, absClipRect.Top, 0, absClipRect.Bottom);
g.DrawLine(style.OriginGridPen, absClipRect.Left, 0, absClipRect.Right, 0);
Expand Down Expand Up @@ -334,7 +328,7 @@ private void Invalidate(CanvasItem item)
var viewBound = AbsToView(absBounds);
viewBound.Inflate(10, 10);
Invalidate(viewBound);
if (ShowFPS) Invalidate(new Rectangle(0, 0, 100, 100));
if (Settings.ShowFPS) Invalidate(new Rectangle(0, 0, 100, 100));
}

private float ViewToAbs(int dist)
Expand Down Expand Up @@ -389,12 +383,12 @@ private Vector2 Snap(Vector2 point, params Vector2[] hints)
{
if ((ModifierKeys & Keys.Shift) == Keys.Shift) return point;

var bestXHintDistance = HintSnapDistance;
var bestYHintDistance = HintSnapDistance;
var bestXHintDistance = settings.HintSnapDistance;
var bestYHintDistance = settings.HintSnapDistance;
var bestXHint = Single.NaN;
var bestYHint = Single.NaN;

if (SnapToAxes)
if (Settings.SnapToAxes)
{
foreach (var hint in hints)
{
Expand All @@ -413,7 +407,7 @@ private Vector2 Snap(Vector2 point, params Vector2[] hints)
}
}

if (SnapToItems)
if (Settings.SnapToItems)
{
foreach (var item in items)
{
Expand All @@ -430,7 +424,7 @@ private Vector2 Snap(Vector2 point, params Vector2[] hints)
if (!Single.IsNaN(bestXHint)) point = new Vector2(bestXHint, point.Y);
if (!Single.IsNaN(bestYHint)) point = new Vector2(point.X, bestYHint);

if (!SnapToGrid) return point;
if (!Settings.SnapToGrid) return point;
var step = GridMinorStep;
return new Vector2((float)Math.Floor((point.X + step / 2) / step) * step, (float)Math.Floor((point.Y + step / 2) / step) * step);
}
Expand Down Expand Up @@ -755,7 +749,7 @@ public void StartMouseRotate()

if ((ModifierKeys & Keys.Shift) == Keys.Shift) return;

var bestSnapDistance = HintSnapDistance;
var bestSnapDistance = settings.HintSnapDistance;
var bestSnap = rotationCenter;

var first = true;
Expand Down Expand Up @@ -787,7 +781,7 @@ private void UpdateMouseRotate(Point mouseLocation)
{
rotationAngle = -720f * (mouseLocation.X - mouseStart.X) / ClientSize.Width;

if (((ModifierKeys & Keys.Shift) == Keys.Shift) ^ SnapToAxes) rotationAngle = rotationAngle - (rotationAngle % 5f);
if (((ModifierKeys & Keys.Shift) == Keys.Shift) ^ Settings.SnapToAxes) rotationAngle = rotationAngle - (rotationAngle % 5f);

var matrix = Matrix3x2.CreateRotation((float)(rotationAngle * Math.PI / 180f), rotationCenter);

Expand Down Expand Up @@ -823,7 +817,7 @@ public void StartMouseScale()

if ((ModifierKeys & Keys.Shift) == Keys.Shift) return;

var bestSnapDistance = HintSnapDistance;
var bestSnapDistance = settings.HintSnapDistance;
var bestSnap = scaleCenter;

var first = true;
Expand Down Expand Up @@ -1016,7 +1010,7 @@ protected override void OnMouseMove(MouseEventArgs e)
else if (interaction == Interaction.Rotate) UpdateMouseRotate(e.Location);
else if (interaction == Interaction.Scale) UpdateMouseScale(e.Location);
else if (interaction == Interaction.None) UpdateMouseHover(e.Location);
if (ShowCursorCoords) Invalidate(new Rectangle(0, Height - 40, 500, 40));
if (Settings.ShowCursorCoords) Invalidate(new Rectangle(0, Height - 40, 500, 40));
base.OnMouseMove(e);
}
}
Expand Down
62 changes: 29 additions & 33 deletions CanvasStyle.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Globalization;
using System.Linq;
using System.Net.Mime;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

namespace GCEd
{
class CanvasStyle : IDisposable
{
public Settings Settings { get; set; }
public float PixelSize { get; protected set; }
public Pen IdlePen { get; protected set; } = default!;
public Pen ActivePen { get; protected set; } = default!;
Expand All @@ -33,6 +28,7 @@ class CanvasStyle : IDisposable

public CanvasStyle()
{
Settings = new Settings();
using var matrix = new Matrix();
ViewMatrixChanged(matrix);
}
Expand All @@ -45,22 +41,19 @@ public virtual void ViewMatrixChanged(Matrix viewMatrix)
var transformedProbeLength = Geometry.LineLength((Vector2)probe[0]);
var len = Math.Max(transformedProbeLength / probeLength, 0.0001f);
PixelSize = (float)(1 / len);
var selectedStrokeColor = Color.FromArgb(0xF5, 0xFA, 0xFF);
var strokeColor = Color.FromArgb(0xA5, 0xCA, 0xFF);
var guideColor = Color.FromArgb(0xC5, 0xC5, 0xCF);
IdlePen = new Pen(strokeColor, PixelSize) { DashStyle = DashStyle.Dash, DashPattern = new[] { 10f, 10f } };
ActivePen = new Pen(strokeColor, 3 * PixelSize);
GuidePen = new Pen(guideColor, PixelSize) { DashStyle = DashStyle.Dot };
HoveredIdlePen = new Pen(strokeColor, 4 * PixelSize) { DashStyle = DashStyle.Dash, DashPattern = new[] { 2.5f, 2.5f } };
HoveredActivePen = new Pen(strokeColor, 4 * PixelSize);
HoveredGuidePen = new Pen(guideColor, 4 * PixelSize) { DashStyle = DashStyle.Dot };
SelectedIdlePen = new Pen(selectedStrokeColor, 4 * PixelSize) { DashStyle = DashStyle.Dash, DashPattern = new[] { 2.5f, 2.5f } };
SelectedActivePen = new Pen(selectedStrokeColor, 4 * PixelSize);
SelectedGuidePen = new Pen(selectedStrokeColor, 4 * PixelSize) { DashStyle = DashStyle.Dot };
IdlePen = new Pen(Settings.ItemColor, PixelSize) { DashStyle = DashStyle.Dash, DashPattern = new[] { 10f, 10f } };
ActivePen = new Pen(Settings.ItemColor, Settings.ActiveItemThickness * PixelSize);
GuidePen = new Pen(Settings.GuideColor, Settings.GuideThickness * PixelSize) { DashStyle = DashStyle.Dot };
HoveredIdlePen = new Pen(Settings.ItemColor, Settings.SelectedItemThickness * PixelSize) { DashStyle = DashStyle.Dash, DashPattern = new[] { 2.5f, 2.5f } };
HoveredActivePen = new Pen(Settings.ItemColor, Settings.SelectedItemThickness * PixelSize);
HoveredGuidePen = new Pen(Settings.GuideColor, Settings.SelectedItemThickness * PixelSize) { DashStyle = DashStyle.Dot };
SelectedIdlePen = new Pen(Settings.SelectedItemColor, Settings.SelectedItemThickness * PixelSize) { DashStyle = DashStyle.Dash, DashPattern = new[] { 2.5f, 2.5f } };
SelectedActivePen = new Pen(Settings.SelectedItemColor, Settings.SelectedItemThickness * PixelSize);
SelectedGuidePen = new Pen(Settings.SelectedItemColor, Settings.SelectedItemThickness * PixelSize) { DashStyle = DashStyle.Dot };

MinorGridPen = new Pen(Color.FromArgb(0x30, 0x60, 0xA0), PixelSize);
MajorGridPen = new Pen(Color.FromArgb(0x60, 0xA0, 0xD0), PixelSize);
OriginGridPen = new Pen(Color.FromArgb(0xE0, 0xF0, 0xFF), 2 * PixelSize);
MinorGridPen = new Pen(Settings.MinorGridColor, Settings.MinorGridThickness * PixelSize);
MajorGridPen = new Pen(Settings.MajorGridColor, Settings.MajorGridThickness * PixelSize);
OriginGridPen = new Pen(Settings.OriginGridColor, Settings.OriginGridThickness * PixelSize);

var arrowPath = new GraphicsPath();
arrowPath.AddLines(new[] { new PointF(0f, 0f), new PointF(1f, -2f), new PointF(-1f, -2f), new PointF(0f, 0f) });
Expand All @@ -70,19 +63,22 @@ public virtual void ViewMatrixChanged(Matrix viewMatrix)
capPath.AddLine(2f, 0f, -2f, 0f);
var startCap = new CustomLineCap(null, capPath, LineCap.ArrowAnchor);

HoveredActivePen.CustomEndCap = endCap;
HoveredActivePen.CustomStartCap = startCap;
HoveredIdlePen.CustomEndCap = endCap;
HoveredIdlePen.CustomStartCap = startCap;
SelectedIdlePen.CustomEndCap = endCap;
SelectedIdlePen.CustomStartCap = startCap;
SelectedActivePen.CustomEndCap = endCap;
SelectedActivePen.CustomStartCap = startCap;
SelectionPen = new Pen(Color.FromArgb(0xAF, 0xCF, 0xFF), PixelSize);
if (Settings.ItemEndCaps)
{
HoveredActivePen.CustomEndCap = endCap;
HoveredActivePen.CustomStartCap = startCap;
HoveredIdlePen.CustomEndCap = endCap;
HoveredIdlePen.CustomStartCap = startCap;
SelectedIdlePen.CustomEndCap = endCap;
SelectedIdlePen.CustomStartCap = startCap;
SelectedActivePen.CustomEndCap = endCap;
SelectedActivePen.CustomStartCap = startCap;
}

TextBrush = new SolidBrush(Color.FromArgb(0xE5, 0xF5, 0xFF));
BackgroundBrush = new SolidBrush(Color.FromArgb(0x20, 0x4A, 0x7F));
SelectionBrush = new SolidBrush(Color.FromArgb(0x40, 0xE0, 0xEA, 0xF5));
SelectionPen = new Pen(Settings.SelectionBorderColor, PixelSize);
SelectionBrush = new SolidBrush(Settings.SelectionAreaColor);
TextBrush = new SolidBrush(Settings.TextColor);
BackgroundBrush = new SolidBrush(Settings.BackgroundColor);
}

public virtual void Dispose()
Expand Down
9 changes: 0 additions & 9 deletions MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 16 additions & 13 deletions MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ namespace GCEd
public partial class MainForm : Form
{
private ViewState viewState;
private Settings settings;

public MainForm()
{
InitializeComponent();
DoubleBuffered = true;
settings = new Settings();
canvas.Settings = settings;
viewState = new ViewState();
canvas.ViewState = viewState;
operationsList.ViewState = viewState;
Expand Down Expand Up @@ -223,43 +226,43 @@ private void Paste(bool before)

public void ToggleGrid()
{
if (canvas.ShowMinorGrid)
if (settings.ShowMinorGrid)
{
canvas.ShowMinorGrid = false;
canvas.ShowMajorGrid = false;
canvas.ShowOriginGrid = false;
settings.ShowMinorGrid = false;
settings.ShowMajorGrid = false;
settings.ShowOriginGrid = false;
}
else if (!canvas.ShowOriginGrid) canvas.ShowOriginGrid = true;
else if (!canvas.ShowMajorGrid) canvas.ShowMajorGrid = true;
else canvas.ShowMinorGrid = true;
else if (!settings.ShowOriginGrid) settings.ShowOriginGrid = true;
else if (!settings.ShowMajorGrid) settings.ShowMajorGrid = true;
else settings.ShowMinorGrid = true;
canvas.Invalidate();
}

public void ToggleSnapToGrid()
{
canvas.SnapToGrid = !canvas.SnapToGrid;
settings.SnapToGrid = !settings.SnapToGrid;
}

public void ToggleSnapToItems()
{
canvas.SnapToItems = !canvas.SnapToItems;
settings.SnapToItems = !settings.SnapToItems;
}

public void ToggleSnapToAxes()
{
canvas.SnapToAxes = !canvas.SnapToAxes;
settings.SnapToAxes = !settings.SnapToAxes;
}

public void ToggleFPS()
{
canvas.ShowFPS = !canvas.ShowFPS;
settings.ShowFPS = !settings.ShowFPS;
canvas.Invalidate();
}

public void ToggleCoords()
{
canvas.ShowCursorCoords = !canvas.ShowCursorCoords;
if (!canvas.ShowCursorCoords) canvas.ShowItemCoords = !canvas.ShowItemCoords;
settings.ShowCursorCoords = !settings.ShowCursorCoords;
if (!settings.ShowCursorCoords) settings.ShowItemCoords = !settings.ShowItemCoords;
canvas.Invalidate();
}

Expand Down
Loading

0 comments on commit e34932f

Please sign in to comment.