source: tags/1.2.0/UMenu.pas

Last change on this file was 77, checked in by chronos, 3 years ago
  • Added: Control icons for Pause, Play and Fast forward.
  • Modified: Image buttons changed to custom TImage control with Enabled property supported.
File size: 7.5 KB
Line 
1unit UMenu;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, Graphics, Controls, fgl, UControls;
9
10type
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
83resourcestring
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
99implementation
100
101{ TMenuItemButton }
102
103procedure TMenuItemButton.Paint;
104var
105 P: TPoint;
106begin
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);
114end;
115
116{ TMenuItemCheckBox }
117
118procedure TMenuItemCheckBox.Paint;
119var
120 OutputText: string;
121 P: TPoint;
122begin
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);
132end;
133
134{ TMenuItem }
135
136function TMenuItem.GetOutputText: string;
137begin
138 Result := Text;
139end;
140
141constructor TMenuItem.Create;
142begin
143 Enabled := True;
144 FontSize := 40;
145 FontColor := clWhite;
146end;
147
148{ TMenuItemComboBox }
149
150constructor TMenuItemComboBox.Create;
151begin
152 inherited;
153 States := TStringList.Create;
154end;
155
156destructor TMenuItemComboBox.Destroy;
157begin
158 FreeAndNil(States);
159 inherited;
160end;
161
162procedure TMenuItemComboBox.Paint;
163var
164 P: TPoint;
165begin
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);
173end;
174
175function TMenuItemComboBox.GetOutputText: string;
176begin
177 Result := Text + ': ' + States[Index];
178end;
179
180{ TMenu }
181
182procedure TMenu.MouseMove(Position: TPoint);
183var
184 I: Integer;
185begin
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;
190end;
191
192procedure TMenu.MouseUp(Button: TMouseButton; Position: TPoint);
193var
194 I: Integer;
195begin
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;
215end;
216
217procedure TMenu.Paint(Canvas: TCanvas; CanvasSize: TPoint);
218var
219 I: Integer;
220 X: Integer;
221 Y: Integer;
222 LineHeight: Integer;
223 TotalWidth: Integer;
224 TotalHeight: Integer;
225begin
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;
258end;
259
260constructor TMenu.Create;
261begin
262 Items := TMenuItems.Create;
263end;
264
265destructor TMenu.Destroy;
266begin
267 FreeAndNil(Items);
268 inherited;
269end;
270
271{ TMenuItems }
272
273function TMenuItems.AddButton(Text: string; OnClick: TNotifyEvent
274 ): TMenuItemButton;
275begin
276 Result := TMenuItemButton.Create;
277 Result.Text := Text;
278 Result.OnClick := OnClick;
279 Add(Result);
280end;
281
282function TMenuItems.AddCheckBox(Text: string; OnChanged: TNotifyEvent): TMenuItemCheckBox;
283begin
284 Result := TMenuItemCheckBox.Create;
285 Result.Text := Text;
286 Result.OnChanged := OnChanged;
287 Add(Result);
288end;
289
290function TMenuItems.AddComboBox(Text: string; States: array of string
291 ; OnChanged: TNotifyEvent): TMenuItemComboBox;
292var
293 I: Integer;
294begin
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);
301end;
302
303
304end.
305
Note: See TracBrowser for help on using the repository browser.