1 | unit UMenu;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, Graphics, Controls, fgl, UControls;
|
---|
9 |
|
---|
10 | type
|
---|
11 | TMenuItemKind = (ikButton, ikComboBox, ikCheckBox);
|
---|
12 |
|
---|
13 | { TMenuItem }
|
---|
14 |
|
---|
15 | TMenuItem = class(TControl)
|
---|
16 | BackgroundColor: TColor;
|
---|
17 | BackgroundSelectedColor: TColor;
|
---|
18 | Kind: TMenuItemKind;
|
---|
19 | Text: string;
|
---|
20 | Selected: Boolean;
|
---|
21 | FontSize: Integer;
|
---|
22 | FontColor: Integer;
|
---|
23 | function GetOutputText: string; virtual;
|
---|
24 | constructor Create; override;
|
---|
25 | end;
|
---|
26 |
|
---|
27 | { TMenuItemCheckBox }
|
---|
28 |
|
---|
29 | TMenuItemCheckBox = class(TMenuItem)
|
---|
30 | private
|
---|
31 | FOnChanged: TNotifyEvent;
|
---|
32 | public
|
---|
33 | Checked: Boolean;
|
---|
34 | procedure Paint; override;
|
---|
35 | property OnChanged: TNotifyEvent read FOnChanged write FOnChanged;
|
---|
36 | end;
|
---|
37 |
|
---|
38 | { TMenuItemComboBox }
|
---|
39 |
|
---|
40 | TMenuItemComboBox = class(TMenuItem)
|
---|
41 | private
|
---|
42 | FOnChanged: TNotifyEvent;
|
---|
43 | public
|
---|
44 | Index: Integer;
|
---|
45 | States: TStringList;
|
---|
46 | constructor Create; override;
|
---|
47 | destructor Destroy; override;
|
---|
48 | procedure Paint; override;
|
---|
49 | function GetOutputText: string; override;
|
---|
50 | property OnChanged: TNotifyEvent read FOnChanged write FOnChanged;
|
---|
51 | end;
|
---|
52 |
|
---|
53 | { TMenuItemButton }
|
---|
54 |
|
---|
55 | TMenuItemButton = class(TMenuItem)
|
---|
56 | public
|
---|
57 | procedure Paint; override;
|
---|
58 | end;
|
---|
59 |
|
---|
60 | { TMenuItems }
|
---|
61 |
|
---|
62 | TMenuItems = class(TFPGObjectList<TMenuItem>)
|
---|
63 | function AddButton(Text: string; OnClick: TNotifyEvent): TMenuItemButton;
|
---|
64 | function AddCheckBox(Text: string; OnChanged: TNotifyEvent): TMenuItemCheckBox;
|
---|
65 | function AddComboBox(Text: string; States: array of string; OnChanged:
|
---|
66 | TNotifyEvent): TMenuItemComboBox;
|
---|
67 | end;
|
---|
68 |
|
---|
69 | TMenu = class
|
---|
70 | private
|
---|
71 | FOnExit: TNotifyEvent;
|
---|
72 | public
|
---|
73 | Items: TMenuItems;
|
---|
74 | Parent: TMenu;
|
---|
75 | procedure MouseMove(Position: TPoint);
|
---|
76 | procedure MouseUp(Button: TMouseButton; Position: TPoint);
|
---|
77 | procedure Paint(Canvas: TCanvas; CanvasSize: TPoint);
|
---|
78 | constructor Create;
|
---|
79 | destructor Destroy; override;
|
---|
80 | property OnExit: TNotifyEvent read FOnExit write FOnExit;
|
---|
81 | end;
|
---|
82 |
|
---|
83 | resourcestring
|
---|
84 | SPlay = 'Play';
|
---|
85 | SOptions = 'Options';
|
---|
86 | SExit = 'Exit';
|
---|
87 | SBigMetro = 'Big Metro';
|
---|
88 | SDarkMode = 'Dark mode';
|
---|
89 | SLanguage = 'Language';
|
---|
90 | SCzech = 'Czech';
|
---|
91 | SEnglish = 'English';
|
---|
92 | SBack = 'Back';
|
---|
93 | SAutomatic = 'Automatic';
|
---|
94 | SFullScreen = 'Full screen';
|
---|
95 | SContinue = 'Continue';
|
---|
96 | SRestart = 'Try again';
|
---|
97 |
|
---|
98 |
|
---|
99 | implementation
|
---|
100 |
|
---|
101 | { TMenuItemButton }
|
---|
102 |
|
---|
103 | procedure TMenuItemButton.Paint;
|
---|
104 | var
|
---|
105 | P: TPoint;
|
---|
106 | begin
|
---|
107 | P := Point(Bounds.Left, Bounds.Top);
|
---|
108 | if Selected then Canvas.Brush.Color := BackgroundSelectedColor
|
---|
109 | else Canvas.Brush.Color := BackgroundColor;
|
---|
110 | if Canvas.Brush.Color = clNone then Canvas.Brush.Style := bsClear
|
---|
111 | else Canvas.Brush.Style := bsSolid;
|
---|
112 | Bounds := Classes.Bounds(P.X, P.Y, Canvas.TextWidth(Text), Canvas.TextHeight(Text));
|
---|
113 | Canvas.TextOut(P.X, P.Y, Text);
|
---|
114 | end;
|
---|
115 |
|
---|
116 | { TMenuItemCheckBox }
|
---|
117 |
|
---|
118 | procedure TMenuItemCheckBox.Paint;
|
---|
119 | var
|
---|
120 | OutputText: string;
|
---|
121 | P: TPoint;
|
---|
122 | begin
|
---|
123 | P := Point(Bounds.Left, Bounds.Top);
|
---|
124 | OutputText := Text;
|
---|
125 | if Checked then OutputText := '✓' + OutputText;
|
---|
126 | if Selected then Canvas.Brush.Color := BackgroundSelectedColor
|
---|
127 | else Canvas.Brush.Color := BackgroundColor;
|
---|
128 | if Canvas.Brush.Color = clNone then Canvas.Brush.Style := bsClear
|
---|
129 | else Canvas.Brush.Style := bsSolid;
|
---|
130 | Bounds := Classes.Bounds(P.X, P.Y, Canvas.TextWidth(OutputText), Canvas.TextHeight(OutputText));
|
---|
131 | Canvas.TextOut(P.X, P.Y, OutputText);
|
---|
132 | end;
|
---|
133 |
|
---|
134 | { TMenuItem }
|
---|
135 |
|
---|
136 | function TMenuItem.GetOutputText: string;
|
---|
137 | begin
|
---|
138 | Result := Text;
|
---|
139 | end;
|
---|
140 |
|
---|
141 | constructor TMenuItem.Create;
|
---|
142 | begin
|
---|
143 | Enabled := True;
|
---|
144 | FontSize := 40;
|
---|
145 | FontColor := clWhite;
|
---|
146 | end;
|
---|
147 |
|
---|
148 | { TMenuItemComboBox }
|
---|
149 |
|
---|
150 | constructor TMenuItemComboBox.Create;
|
---|
151 | begin
|
---|
152 | inherited;
|
---|
153 | States := TStringList.Create;
|
---|
154 | end;
|
---|
155 |
|
---|
156 | destructor TMenuItemComboBox.Destroy;
|
---|
157 | begin
|
---|
158 | FreeAndNil(States);
|
---|
159 | inherited;
|
---|
160 | end;
|
---|
161 |
|
---|
162 | procedure TMenuItemComboBox.Paint;
|
---|
163 | var
|
---|
164 | P: TPoint;
|
---|
165 | begin
|
---|
166 | P := Point(Bounds.Left, Bounds.Top);
|
---|
167 | if Selected then Canvas.Brush.Color := BackgroundSelectedColor
|
---|
168 | else Canvas.Brush.Color := BackgroundColor;
|
---|
169 | if Canvas.Brush.Color = clNone then Canvas.Brush.Style := bsClear
|
---|
170 | else Canvas.Brush.Style := bsSolid;
|
---|
171 | Bounds := Classes.Bounds(P.X, P.Y, Canvas.TextWidth(GetOutputText), Canvas.TextHeight(GetOutputText));
|
---|
172 | Canvas.TextOut(P.X, P.Y, GetOutputText);
|
---|
173 | end;
|
---|
174 |
|
---|
175 | function TMenuItemComboBox.GetOutputText: string;
|
---|
176 | begin
|
---|
177 | Result := Text + ': ' + States[Index];
|
---|
178 | end;
|
---|
179 |
|
---|
180 | { TMenu }
|
---|
181 |
|
---|
182 | procedure TMenu.MouseMove(Position: TPoint);
|
---|
183 | var
|
---|
184 | I: Integer;
|
---|
185 | begin
|
---|
186 | for I := 0 to Items.Count - 1 do begin
|
---|
187 | if Items[I].Enabled then
|
---|
188 | Items[I].Selected := Items[I].Bounds.Contains(Position);
|
---|
189 | end;
|
---|
190 | end;
|
---|
191 |
|
---|
192 | procedure TMenu.MouseUp(Button: TMouseButton; Position: TPoint);
|
---|
193 | var
|
---|
194 | I: Integer;
|
---|
195 | begin
|
---|
196 | for I := 0 to Items.Count - 1 do begin
|
---|
197 | if Items[I].Bounds.Contains(Position) then begin
|
---|
198 | if (Items[I] is TMenuItemButton) then begin
|
---|
199 | if Assigned(TMenuItemButton(Items[I]).OnClick) then
|
---|
200 | TMenuItemButton(Items[I]).OnClick(Items[I]);
|
---|
201 | end else
|
---|
202 | if (Items[I] is TMenuItemCheckBox) then begin
|
---|
203 | (Items[I] as TMenuItemCheckBox).Checked := not (Items[I] as TMenuItemCheckBox).Checked;
|
---|
204 | if Assigned((Items[I] as TMenuItemCheckBox).FOnChanged) then
|
---|
205 | (Items[I] as TMenuItemCheckBox).FOnChanged(Items[I]);
|
---|
206 | end else
|
---|
207 | if (Items[I] is TMenuItemComboBox) then begin
|
---|
208 | (Items[I] as TMenuItemComboBox).Index := ((Items[I] as TMenuItemComboBox).Index + 1) mod
|
---|
209 | (Items[I] as TMenuItemComboBox).States.Count;
|
---|
210 | if Assigned((Items[I] as TMenuItemComboBox).FOnChanged) then
|
---|
211 | (Items[I] as TMenuItemComboBox).FOnChanged(Items[I]);
|
---|
212 | end;
|
---|
213 | end;
|
---|
214 | end;
|
---|
215 | end;
|
---|
216 |
|
---|
217 | procedure TMenu.Paint(Canvas: TCanvas; CanvasSize: TPoint);
|
---|
218 | var
|
---|
219 | I: Integer;
|
---|
220 | X: Integer;
|
---|
221 | Y: Integer;
|
---|
222 | LineHeight: Integer;
|
---|
223 | TotalWidth: Integer;
|
---|
224 | TotalHeight: Integer;
|
---|
225 | begin
|
---|
226 | with Canvas do begin
|
---|
227 | TotalWidth := 0;
|
---|
228 | TotalHeight := 0;
|
---|
229 |
|
---|
230 | // Calculate total dimensions for centering
|
---|
231 | Font.Style := [fsBold];
|
---|
232 | Brush.Style := bsSolid;
|
---|
233 | for I := 0 to Items.Count - 1 do begin
|
---|
234 | Font.Size := Items[I].FontSize;
|
---|
235 | if TotalWidth < TextWidth(Items[I].GetOutputText) then
|
---|
236 | TotalWidth := TextWidth(Items[I].GetOutputText);
|
---|
237 | LineHeight := Round(TextHeight('I') * 1.1);
|
---|
238 | TotalHeight := TotalHeight + LineHeight;
|
---|
239 | end;
|
---|
240 |
|
---|
241 | X := (CanvasSize.X - TotalWidth) div 2;
|
---|
242 | Y := (CanvasSize.Y - TotalHeight) div 2;
|
---|
243 |
|
---|
244 | // Menu items
|
---|
245 | Font.Style := [fsBold];
|
---|
246 | Brush.Style := bsSolid;
|
---|
247 | for I := 0 to Items.Count - 1 do begin
|
---|
248 | Font.Size := Items[I].FontSize;
|
---|
249 | Font.Color := Items[I].FontColor;
|
---|
250 | Items[I].Bounds.Left := X;
|
---|
251 | Items[I].Bounds.Top := Y;
|
---|
252 | Items[I].Canvas := Canvas;
|
---|
253 | Items[I].Paint;
|
---|
254 | LineHeight := Round(TextHeight('I') * 1.1);
|
---|
255 | Inc(Y, LineHeight);
|
---|
256 | end;
|
---|
257 | end;
|
---|
258 | end;
|
---|
259 |
|
---|
260 | constructor TMenu.Create;
|
---|
261 | begin
|
---|
262 | Items := TMenuItems.Create;
|
---|
263 | end;
|
---|
264 |
|
---|
265 | destructor TMenu.Destroy;
|
---|
266 | begin
|
---|
267 | FreeAndNil(Items);
|
---|
268 | inherited;
|
---|
269 | end;
|
---|
270 |
|
---|
271 | { TMenuItems }
|
---|
272 |
|
---|
273 | function TMenuItems.AddButton(Text: string; OnClick: TNotifyEvent
|
---|
274 | ): TMenuItemButton;
|
---|
275 | begin
|
---|
276 | Result := TMenuItemButton.Create;
|
---|
277 | Result.Text := Text;
|
---|
278 | Result.OnClick := OnClick;
|
---|
279 | Add(Result);
|
---|
280 | end;
|
---|
281 |
|
---|
282 | function TMenuItems.AddCheckBox(Text: string; OnChanged: TNotifyEvent): TMenuItemCheckBox;
|
---|
283 | begin
|
---|
284 | Result := TMenuItemCheckBox.Create;
|
---|
285 | Result.Text := Text;
|
---|
286 | Result.OnChanged := OnChanged;
|
---|
287 | Add(Result);
|
---|
288 | end;
|
---|
289 |
|
---|
290 | function TMenuItems.AddComboBox(Text: string; States: array of string
|
---|
291 | ; OnChanged: TNotifyEvent): TMenuItemComboBox;
|
---|
292 | var
|
---|
293 | I: Integer;
|
---|
294 | begin
|
---|
295 | Result := TMenuItemComboBox.Create;
|
---|
296 | Result.Text := Text;
|
---|
297 | Result.OnChanged := OnChanged;
|
---|
298 | for I := 0 to Length(States) - 1 do
|
---|
299 | Result.States.Add(States[I]);
|
---|
300 | Add(Result);
|
---|
301 | end;
|
---|
302 |
|
---|
303 |
|
---|
304 | end.
|
---|
305 |
|
---|