-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsForm.cs
More file actions
207 lines (179 loc) · 7.87 KB
/
Copy pathSettingsForm.cs
File metadata and controls
207 lines (179 loc) · 7.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (c) 2012-2026 The Hello World Writer (https://www.thehelloworldwriter.com).
// Licensed under the MIT License. See the LICENSE file in the project root for more information.
namespace GridlyDots;
/// <summary>Settings dialog for adjusting the grid overlay properties in real time.</summary>
public class SettingsForm : Form
{
/// <summary>Near-black color that avoids conflict with the pure Black transparency key.</summary>
private static readonly Color SafeBlack = Color.FromArgb(1, 1, 1);
private readonly Label dotSizeLabel = new() { Dock = DockStyle.Fill, TextAlign = ContentAlignment.MiddleLeft };
private readonly Label dotSpacingLabel = new() { Dock = DockStyle.Fill, TextAlign = ContentAlignment.MiddleLeft };
private readonly Label gridOpacityLabel = new() { Dock = DockStyle.Fill, TextAlign = ContentAlignment.MiddleLeft };
private readonly TrackBar dotSizeTrackBar = new();
private readonly TrackBar dotSpacingTrackBar = new();
private readonly TrackBar gridOpacityTrackBar = new();
private readonly Button dotColorButton = new();
private readonly ColorDialog colorDialog = new();
public SettingsForm()
{
SuspendLayout();
// Dimensions BEFORE mode (setting the mode clears dimensions)
AutoScaleDimensions = new SizeF(96F, 96F);
AutoScaleMode = AutoScaleMode.Dpi;
Text = Strings.SettingsBoxTitle;
FormBorderStyle = FormBorderStyle.FixedDialog;
StartPosition = FormStartPosition.CenterParent;
MinimizeBox = false;
MaximizeBox = false;
TopMost = true;
KeyPreview = true;
BackColor = SystemColors.Window;
AutoSize = true;
AutoSizeMode = AutoSizeMode.GrowAndShrink;
MinimumSize = new Size(480, 0);
Padding = new Padding(20);
CreateControls();
Load += OnFormLoad;
ResumeLayout(false);
PerformLayout();
}
/// <summary>Builds the UI control tree and adds it to the form.</summary>
private void CreateControls()
{
// TableLayoutPanel — 2 columns (labels 35%, sliders 65%), 5 rows
var layout = new TableLayoutPanel
{
ColumnCount = 2,
RowCount = 5,
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
Dock = DockStyle.Fill,
};
layout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 35F));
layout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 65F));
layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
// Row 0: Dot size
ConfigureTrackBar(dotSizeTrackBar, 1, 100, 10);
dotSizeTrackBar.Scroll += OnDotSizeScroll;
layout.Controls.Add(dotSizeLabel, 0, 0);
layout.Controls.Add(dotSizeTrackBar, 1, 0);
// Row 1: Dot spacing
ConfigureTrackBar(dotSpacingTrackBar, 0, 100, 10);
dotSpacingTrackBar.Scroll += OnDotSpacingScroll;
layout.Controls.Add(dotSpacingLabel, 0, 1);
layout.Controls.Add(dotSpacingTrackBar, 1, 1);
// Row 2: Grid opacity
ConfigureTrackBar(gridOpacityTrackBar, 0, 100, 10);
gridOpacityTrackBar.Scroll += OnGridOpacityScroll;
layout.Controls.Add(gridOpacityLabel, 0, 2);
layout.Controls.Add(gridOpacityTrackBar, 1, 2);
// Row 3: Dot color
var dotColorLabel = new Label
{
Text = Strings.DotColor,
Dock = DockStyle.Fill,
TextAlign = ContentAlignment.MiddleLeft,
};
dotColorButton.Size = AppConstants.ButtonMinimumSize;
dotColorButton.UseVisualStyleBackColor = false;
dotColorButton.Anchor = AnchorStyles.Left;
dotColorButton.Click += OnDotColorClick;
layout.Controls.Add(dotColorLabel, 0, 3);
layout.Controls.Add(dotColorButton, 1, 3);
// Row 4: Close button
var closeButton = new Button
{
Text = Strings.CloseButton,
DialogResult = DialogResult.Cancel,
MinimumSize = AppConstants.ButtonMinimumSize,
Anchor = AnchorStyles.Right,
};
CancelButton = closeButton;
layout.Controls.Add(closeButton, 1, 4);
Controls.Add(layout);
}
/// <summary>Configures a TrackBar with common settings.</summary>
private static void ConfigureTrackBar(TrackBar trackBar, int min, int max, int tickFrequency)
{
trackBar.Minimum = min;
trackBar.Maximum = max;
trackBar.TickFrequency = tickFrequency;
trackBar.TickStyle = TickStyle.None;
trackBar.AutoSize = false;
trackBar.Height = 30;
trackBar.Margin = new Padding(3, 8, 3, 8);
trackBar.Anchor = AnchorStyles.Left | AnchorStyles.Right;
}
/// <summary>Loads current settings from the grid form and updates the controls.</summary>
private void OnFormLoad(object? sender, EventArgs e)
{
ApplyToGridForm(form =>
{
dotSizeTrackBar.Value = form.GPainter.DotSize;
dotSpacingTrackBar.Value = form.GPainter.DotSpacing;
gridOpacityTrackBar.Value = (int)(form.Opacity * 100);
dotColorButton.BackColor = form.GPainter.DotColor;
});
UpdateLabels();
}
/// <summary>Executes an action on the owner GridForm and invalidates it to repaint the grid.</summary>
private void ApplyToGridForm(Action<GridForm> action)
{
if (Owner is GridForm gridForm)
{
action(gridForm);
gridForm.Invalidate();
}
}
/// <summary>Updates all setting labels with current track bar values.</summary>
private void UpdateLabels()
{
dotSizeLabel.Text = Strings.DotSize(dotSizeTrackBar.Value);
dotSpacingLabel.Text = Strings.DotSpacing(dotSpacingTrackBar.Value);
gridOpacityLabel.Text = Strings.GridOpacity(gridOpacityTrackBar.Value);
}
/// <summary>Applies the dot size change to the grid and updates the label.</summary>
private void OnDotSizeScroll(object? sender, EventArgs e)
{
ApplyToGridForm(form => form.GPainter.DotSize = dotSizeTrackBar.Value);
dotSizeLabel.Text = Strings.DotSize(dotSizeTrackBar.Value);
}
/// <summary>Applies the dot spacing change to the grid and updates the label.</summary>
private void OnDotSpacingScroll(object? sender, EventArgs e)
{
ApplyToGridForm(form => form.GPainter.DotSpacing = dotSpacingTrackBar.Value);
dotSpacingLabel.Text = Strings.DotSpacing(dotSpacingTrackBar.Value);
}
/// <summary>Applies the opacity change to the grid and updates the label.</summary>
private void OnGridOpacityScroll(object? sender, EventArgs e)
{
ApplyToGridForm(form => form.Opacity = gridOpacityTrackBar.Value / 100.0);
gridOpacityLabel.Text = Strings.GridOpacity(gridOpacityTrackBar.Value);
}
/// <summary>Opens the color picker and applies the selected color, substituting near-black for pure black.</summary>
private void OnDotColorClick(object? sender, EventArgs e)
{
colorDialog.Color = dotColorButton.BackColor;
if (colorDialog.ShowDialog() != DialogResult.OK)
{
return;
}
// Pure black would be invisible (matches the grid form's TransparencyKey)
Color color = colorDialog.Color == Color.Black ? SafeBlack : colorDialog.Color;
ApplyToGridForm(form => form.GPainter.DotColor = color);
dotColorButton.BackColor = color;
}
/// <summary>Disposes components not in the Controls hierarchy.</summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
colorDialog.Dispose();
}
base.Dispose(disposing);
}
}