-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeSettingsDialog.pas
More file actions
143 lines (120 loc) · 3.88 KB
/
Copy pathMazeSettingsDialog.pas
File metadata and controls
143 lines (120 loc) · 3.88 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
unit MazeSettingsDialog;
interface
uses
System.SysUtils, System.Classes, System.TypInfo, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
Maze.Settings;
type
/// <summary>
/// Dialog allowing the user to choose the maze generation algorithm.
/// </summary>
TfrmMazeSettings = class(TForm)
pnlButtons: TPanel;
btnOK: TButton;
btnCancel: TButton;
lblAlgorithm: TLabel;
cbAlgorithm: TComboBox;
procedure FormCreate(Sender: TObject);
private
FMazeSettings: TMazeSettings;
/// <summary>
/// Populates the algorithm combo box using the display names mapped from <c>TMazeAlgorithm</c>.
/// When <c>TMazeAlgorithm</c> is extended, update <c>AlgorithmDisplayNames</c> accordingly.
/// </summary>
procedure PopulateAlgorithms;
/// <summary>
/// Finds the combo box index corresponding to the given algorithm, or -1 if none matches.
/// </summary>
function FindIndexForAlgorithm(const AAlgorithm: TMazeAlgorithm): Integer;
/// <summary>
/// Safely resolves the algorithm stored at the given combo box index.
/// Returns <c>True</c> only when the stored object is non-nil and represents a valid enum value.
/// </summary>
function TryGetAlgorithmAtIndex(const AIndex: Integer; out AAlgorithm: TMazeAlgorithm): Boolean;
public
/// <summary>
/// Shows the dialog for the given settings instance and updates it if the user confirms.
/// </summary>
class function Execute(const AMazeSettings: TMazeSettings): Boolean;
end;
var
frmMazeSettings: TfrmMazeSettings;
implementation
{$R *.dfm}
{ TfrmMazeSettings }
class function TfrmMazeSettings.Execute(const AMazeSettings: TMazeSettings): Boolean;
begin
Result := False;
if not Assigned(AMazeSettings) then begin
Exit;
end;
var LDialog := TfrmMazeSettings.Create(nil);
try
LDialog.FMazeSettings := AMazeSettings;
var LIndex := LDialog.FindIndexForAlgorithm(AMazeSettings.Algorithm);
if LIndex >= 0 then begin
LDialog.cbAlgorithm.ItemIndex := LIndex;
end else begin
LDialog.cbAlgorithm.ItemIndex := -1;
end;
Result := LDialog.ShowModal = mrOK;
if Result then begin
var LSelectedAlgorithm: TMazeAlgorithm;
if LDialog.TryGetAlgorithmAtIndex(LDialog.cbAlgorithm.ItemIndex, LSelectedAlgorithm) then begin
AMazeSettings.Algorithm := LSelectedAlgorithm;
end;
end;
finally
LDialog.Free;
end;
end;
procedure TfrmMazeSettings.FormCreate(Sender: TObject);
begin
PopulateAlgorithms;
end;
function TfrmMazeSettings.FindIndexForAlgorithm(const AAlgorithm: TMazeAlgorithm): Integer;
begin
Result := -1;
for var I := 0 to cbAlgorithm.Items.Count - 1 do begin
var LAlgorithm: TMazeAlgorithm;
if TryGetAlgorithmAtIndex(I, LAlgorithm) and (LAlgorithm = AAlgorithm) then begin
Result := I;
Break;
end;
end;
end;
function TfrmMazeSettings.TryGetAlgorithmAtIndex(const AIndex: Integer; out AAlgorithm: TMazeAlgorithm): Boolean;
begin
Result := False;
AAlgorithm := Low(TMazeAlgorithm);
if (AIndex < 0) or (AIndex >= cbAlgorithm.Items.Count) then begin
Exit;
end;
var LObj := cbAlgorithm.Items.Objects[AIndex];
if LObj = nil then begin
Exit;
end;
var LValue := NativeInt(LObj);
if (LValue < Ord(Low(TMazeAlgorithm))) or (LValue > Ord(High(TMazeAlgorithm))) then begin
Exit;
end;
AAlgorithm := TMazeAlgorithm(LValue);
Result := True;
end;
procedure TfrmMazeSettings.PopulateAlgorithms;
const
AlgorithmDisplayNames: array[TMazeAlgorithm] of string = (
'Recursive Backtracking',
'Prim''s Algorithm'
);
begin
cbAlgorithm.Items.BeginUpdate;
try
cbAlgorithm.Items.Clear;
for var LAlgorithm := Low(TMazeAlgorithm) to High(TMazeAlgorithm) do begin
cbAlgorithm.Items.AddObject(AlgorithmDisplayNames[LAlgorithm], TObject(NativeInt(Ord(LAlgorithm))));
end;
finally
cbAlgorithm.Items.EndUpdate;
end;
end;
end.