source: tags/1.3.9/Settings.pas

Last change on this file was 725, checked in by chronos, 3 weeks ago
File size: 14.5 KB
Line 
1unit Settings;
2
3interface
4
5uses
6 Classes, SysUtils, FileUtil, Dialogs, LCLProc, ScreenTools, Messg, ButtonA,
7 Directories, DrawDlg, ButtonC, KeyBindings, Languages, ListBoxEx,
8 {$IFDEF DPI}Dpi.Forms, Dpi.Controls, Dpi.Graphics, Dpi.StdCtrls{$ELSE}
9 Forms, Controls, Graphics, StdCtrls{$ENDIF}, Controls;
10
11type
12 { TSettingsDlg }
13
14 TSettingsDlg = class(TDrawDlg)
15 ButtonMusic: TButtonC;
16 ButtonMusicVolumeDown: TButtonC;
17 ButtonMusicVolumeUp: TButtonC;
18 ButtonFullscreen: TButtonC;
19 ButtonCustomDpi: TButtonC;
20 ButtonGammaDown: TButtonC;
21 ButtonDpiDown: TButtonC;
22 EditShortCutPrimary: TEdit;
23 EditShortCutSecondary: TEdit;
24 ListLanguages: TListBoxEx;
25 ListKeyBindings: TListBoxEx;
26 ButtonOk: TButtonA;
27 ButtonCancel: TButtonA;
28 ButtonReset: TButtonA;
29 ButtonGammaUp: TButtonC;
30 ButtonDpiUp: TButtonC;
31 procedure ButtonCustomDpiClick(Sender: TObject);
32 procedure ButtonDpiDownClick(Sender: TObject);
33 procedure ButtonDpiUpClick(Sender: TObject);
34 procedure ButtonFullscreenClick(Sender: TObject);
35 procedure ButtonCancelClick(Sender: TObject);
36 procedure ButtonMusicClick(Sender: TObject);
37 procedure ButtonMusicVolumeDownClick(Sender: TObject);
38 procedure ButtonMusicVolumeUpClick(Sender: TObject);
39 procedure ButtonResetClick(Sender: TObject);
40 procedure ButtonGammaDownClick(Sender: TObject);
41 procedure EditShortCutPrimaryKeyUp(Sender: TObject; var Key: Word;
42 Shift: TShiftState);
43 procedure EditShortCutSecondaryKeyUp(Sender: TObject; var Key: Word;
44 Shift: TShiftState);
45 procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
46 procedure FormCreate(Sender: TObject);
47 procedure FormDestroy(Sender: TObject);
48 procedure FormPaint(Sender: TObject);
49 procedure FormShow(Sender: TObject);
50 procedure ListKeyBindingsKeyDown(Sender: TObject; var Key: Word;
51 Shift: TShiftState);
52 procedure ListKeyBindingsSelectionChange(Sender: TObject; User: Boolean);
53 procedure ButtonOkClick(Sender: TObject);
54 procedure ButtonGammaUpClick(Sender: TObject);
55 private
56 LocalGamma: Integer;
57 LocalKeyBindings: TKeyBindings;
58 LocalMusicVolume: Integer;
59 CurrentKeyBinding: TKeyBinding;
60 LocalDpi: Integer;
61 procedure UpdateShortCutItem;
62 function GetLocalCustomDpiEnabled: Boolean;
63 function GetLocalMusicEnabled: Boolean;
64 procedure UpdateInterface;
65 public
66 procedure LoadData;
67 procedure SaveData;
68 end;
69
70
71const
72 DpiMin = 100;
73 DpiMax = 500;
74 DpiStep = 25;
75 MusicVolumeMin = 0;
76 MusicVolumeMax = 100;
77 MusicVolumeStep = 10;
78
79
80implementation
81
82{$R *.lfm}
83
84uses
85 Start;
86
87var
88 SFullScreen, SGamma, SRestartMsg, SShortCutPrimary, SShortCutSecondary,
89 SLanguages, SKeyBindings, SCustomDpi, SDpi, SMusic, SMusicVolume: string;
90
91procedure ReloadLanguages;
92begin
93 SFullScreen := Phrases.Lookup('SETTINGS', 0);
94 SGamma := Phrases.Lookup('SETTINGS', 1);
95 SRestartMsg := Phrases.Lookup('SETTINGS', 2);
96 SShortCutPrimary := Phrases.Lookup('SETTINGS', 3);
97 SShortCutSecondary := Phrases.Lookup('SETTINGS', 4);
98 SLanguages := Phrases.Lookup('SETTINGS', 5);
99 SKeyBindings := Phrases.Lookup('SETTINGS', 6);
100 SCustomDpi := Phrases.Lookup('SETTINGS', 7);
101 SDpi := Phrases.Lookup('SETTINGS', 8);
102 SMusic := Phrases.Lookup('SETTINGS', 9);
103 SMusicVolume := Phrases.Lookup('SETTINGS', 10);
104end;
105
106{ TSettingsDlg }
107
108procedure TSettingsDlg.FormCreate(Sender: TObject);
109begin
110 Color := clBlack;
111 LocalKeyBindings := TKeyBindings.Create;
112
113 Canvas.Font.Assign(UniFont[ftNormal]);
114 Canvas.Brush.Style := TBrushStyle.bsClear;
115
116 ButtonOk.Caption := Phrases.Lookup('BTN_OK');
117 ButtonCancel.Caption := Phrases.Lookup('BTN_CANCEL');
118 ButtonReset.Caption := Phrases.Lookup('BTN_RESET');
119 InitButtons;
120end;
121
122procedure TSettingsDlg.ButtonCancelClick(Sender: TObject);
123begin
124 ModalResult := mrCancel;
125end;
126
127procedure TSettingsDlg.ButtonMusicClick(Sender: TObject);
128begin
129 ButtonMusic.ButtonIndex := ButtonMusic.ButtonIndex xor 1;
130 UpdateInterface;
131end;
132
133procedure TSettingsDlg.ButtonMusicVolumeDownClick(Sender: TObject);
134begin
135 Dec(LocalMusicVolume, MusicVolumeStep);
136 if LocalMusicVolume < MusicVolumeMin then LocalMusicVolume := MusicVolumeMin;
137 Invalidate;
138end;
139
140procedure TSettingsDlg.ButtonMusicVolumeUpClick(Sender: TObject);
141begin
142 Inc(LocalMusicVolume, MusicVolumeStep);
143 if LocalMusicVolume > MusicVolumeMax then LocalMusicVolume := MusicVolumeMax;
144 Invalidate;
145end;
146
147procedure TSettingsDlg.ButtonResetClick(Sender: TObject);
148begin
149 // TODO: Better to have default values on single place
150 ListLanguages.ItemIndex := 0;
151 ButtonFullscreen.ButtonIndex := 3;
152 ButtonMusic.ButtonIndex := 3;
153 LocalMusicVolume := 50;
154 ButtonCustomDpi.ButtonIndex := 2;
155 LocalDpi := 100;
156 LocalGamma := 100;
157 ListKeyBindings.ItemIndex := -1;
158 ListKeyBindingsSelectionChange(nil, False);
159 LocalKeyBindings.ResetToDefault;
160 LocalKeyBindings.LoadToStrings(ListKeyBindings.Items);
161 UpdateInterface;
162 Repaint;
163end;
164
165procedure TSettingsDlg.ButtonGammaDownClick(Sender: TObject);
166begin
167 if LocalGamma > 50 then
168 begin
169 Dec(LocalGamma);
170 Invalidate;
171 end;
172end;
173
174procedure TSettingsDlg.EditShortCutPrimaryKeyUp(Sender: TObject; var Key: Word;
175 Shift: TShiftState);
176begin
177 if (Sender is TEdit) and Assigned(CurrentKeyBinding) and not (Key in [16..18]) then begin
178 CurrentKeyBinding.ShortCut := Key or
179 (scShift * Integer(ssShift in Shift)) or
180 (scCtrl * Integer(ssCtrl in Shift)) or
181 (scAlt * Integer(ssAlt in Shift));
182 EditShortCutPrimary.Text := ShortCutToText(CurrentKeyBinding.ShortCut);
183 Key := 0;
184 UpdateShortCutItem;
185 end;
186end;
187
188procedure TSettingsDlg.EditShortCutSecondaryKeyUp(Sender: TObject;
189 var Key: Word; Shift: TShiftState);
190begin
191 if (Sender is TEdit) and Assigned(CurrentKeyBinding) and not (Key in [16..18]) then begin
192 CurrentKeyBinding.ShortCut2 := Key or
193 (scShift * Integer(ssShift in Shift)) or
194 (scCtrl * Integer(ssCtrl in Shift)) or
195 (scAlt * Integer(ssAlt in Shift));
196 EditShortCutSecondary.Text := ShortCutToText(CurrentKeyBinding.ShortCut2);
197 Key := 0;
198 UpdateShortCutItem;
199 end;
200end;
201
202procedure TSettingsDlg.FormClose(Sender: TObject; var CloseAction: TCloseAction
203 );
204begin
205 ListKeyBindings.ItemIndex := -1;
206end;
207
208procedure TSettingsDlg.ButtonFullscreenClick(Sender: TObject);
209begin
210 ButtonFullscreen.ButtonIndex := ButtonFullscreen.ButtonIndex xor 1;
211end;
212
213procedure TSettingsDlg.ButtonDpiDownClick(Sender: TObject);
214begin
215 Dec(LocalDpi, DpiStep);
216 if LocalDpi < DpiMin then LocalDpi := DpiMin;
217 Invalidate;
218end;
219
220procedure TSettingsDlg.ButtonCustomDpiClick(Sender: TObject);
221begin
222 ButtonCustomDpi.ButtonIndex := ButtonCustomDpi.ButtonIndex xor 1;
223 UpdateInterface;
224end;
225
226procedure TSettingsDlg.ButtonDpiUpClick(Sender: TObject);
227begin
228 Inc(LocalDpi, DpiStep);
229 if LocalDpi > DpiMax then LocalDpi := DpiMax;
230 Invalidate;
231end;
232
233procedure TSettingsDlg.FormDestroy(Sender: TObject);
234begin
235 FreeAndNil(LocalKeyBindings);
236end;
237
238procedure TSettingsDlg.FormPaint(Sender: TObject);
239const
240 TextDistanceX = 20;
241 UpDownTextWidth = 120;
242begin
243 PaintBackground(Canvas, 3, 3, ClientWidth - 6, ClientHeight - 6,
244 ClientWidth, ClientHeight);
245 Frame(Canvas, 0, 0, ClientWidth - 1, ClientHeight - 1, 0, 0);
246 Frame(Canvas, 1, 1, ClientWidth - 2, ClientHeight - 2,
247 MainTexture.ColorBevelLight, MainTexture.ColorBevelShade);
248 Frame(Canvas, 2, 2, ClientWidth - 3, ClientHeight - 3,
249 MainTexture.ColorBevelLight, MainTexture.ColorBevelShade);
250 EditFrame(Canvas, EditShortCutPrimary.BoundsRect, MainTexture);
251 EditFrame(Canvas, EditShortCutSecondary.BoundsRect, MainTexture);
252 EditFrame(Canvas, ListLanguages.BoundsRect, MainTexture);
253 BtnFrame(Canvas, ButtonOk.BoundsRect, MainTexture);
254 BtnFrame(Canvas, ButtonCancel.BoundsRect, MainTexture);
255
256 RFrame(Canvas, ButtonFullscreen.Left - 1, ButtonFullscreen.Top - 1,
257 ButtonFullscreen.Left + 12, ButtonFullscreen.Top + 12, MainTexture.ColorBevelShade,
258 MainTexture.ColorBevelLight);
259 {$IFDEF DPI}
260 RFrame(Canvas, ButtonCustomDpi.Left - 1, ButtonCustomDpi.Top - 1,
261 ButtonCustomDpi.Left + 12, ButtonCustomDpi.Top + 12, MainTexture.ColorBevelShade,
262 MainTexture.ColorBevelLight);
263 LoweredTextOut(Canvas, -2, MainTexture, ButtonCustomDpi.Left + TextDistanceX,
264 ButtonCustomDpi.Top - 4, SCustomDpi);
265 UnderlinedTitleValue(Canvas, SDpi, IntToStr(LocalDpi) + '%',
266 ButtonDpiUp.Left - UpDownTextWidth - 4, ButtonDpiUp.Top + 2, UpDownTextWidth);
267 {$ENDIF}
268
269 RFrame(Canvas, ButtonMusic.Left - 1, ButtonMusic.Top - 1,
270 ButtonMusic.Left + 12, ButtonMusic.Top + 12, MainTexture.ColorBevelShade,
271 MainTexture.ColorBevelLight);
272 LoweredTextOut(Canvas, -2, MainTexture, ButtonMusic.Left + TextDistanceX,
273 ButtonMusic.Top - 4, SMusic);
274 UnderlinedTitleValue(Canvas, SMusicVolume, IntToStr(LocalMusicVolume) + '%',
275 ButtonMusicVolumeUp.Left - UpDownTextWidth - 4, ButtonMusicVolumeUp.Top + 2, UpDownTextWidth);
276
277 LoweredTextOut(Canvas, -2, MainTexture, ListLanguages.Left,
278 ListLanguages.Top - 26, SLanguages);
279 LoweredTextOut(Canvas, -2, MainTexture, ListKeyBindings.Left,
280 ListKeyBindings.Top - 26, SKeyBindings);
281 LoweredTextOut(Canvas, -2, MainTexture, ButtonFullscreen.Left + TextDistanceX,
282 ButtonFullscreen.Top - 4, SFullScreen);
283 UnderlinedTitleValue(Canvas, SGamma, IntToStr(LocalGamma) + '%',
284 ButtonGammaUp.Left - UpDownTextWidth - 4, ButtonGammaUp.Top + 2, UpDownTextWidth);
285 LoweredTextOut(Canvas, -2, MainTexture, EditShortCutPrimary.Left,
286 EditShortCutPrimary.Top - 26, SShortCutPrimary);
287 LoweredTextOut(Canvas, -2, MainTexture, EditShortCutSecondary.Left,
288 EditShortCutSecondary.Top - 26, SShortCutSecondary);
289end;
290
291procedure TSettingsDlg.FormShow(Sender: TObject);
292begin
293 Caption := Phrases2.Lookup('ACTIONHEADER_CONFIG');
294 ReloadLanguages;
295 StartDlg.Translator.LanguageListToStrings(ListLanguages.Items);
296 ListLanguages.Font.Color := MainTexture.ColorMark;
297 ListKeyBindings.Font.Color := MainTexture.ColorMark;
298 LoadData;
299 LocalKeyBindings.LoadToStrings(ListKeyBindings.Items);
300 EditShortCutPrimary.Font.Color := MainTexture.ColorMark;
301 EditShortCutSecondary.Font.Color := MainTexture.ColorMark;
302 {$IFDEF DPI}
303 ButtonCustomDpi.Visible := True;
304 ButtonDpiDown.Visible := True;
305 ButtonDpiUp.Visible := True;
306 {$ELSE}
307 ButtonCustomDpi.Visible := False;
308 ButtonDpiDown.Visible := False;
309 ButtonDpiUp.Visible := False;
310 {$ENDIF}
311 UpdateInterface;
312 Gtk2DisableControlStyling(EditShortCutPrimary);
313 Gtk2DisableControlStyling(EditShortCutSecondary);
314end;
315
316procedure TSettingsDlg.ListKeyBindingsKeyDown(Sender: TObject; var Key: Word;
317 Shift: TShiftState);
318begin
319 KeyDown(Key, Shift);
320end;
321
322procedure TSettingsDlg.ListKeyBindingsSelectionChange(Sender: TObject;
323 User: Boolean);
324begin
325 if Assigned(CurrentKeyBinding) then begin
326 CurrentKeyBinding.ShortCut := TextToShortCut(EditShortCutPrimary.Text);
327 CurrentKeyBinding.ShortCut2 := TextToShortCut(EditShortCutSecondary.Text);
328 end;
329
330 if ListKeyBindings.ItemIndex >= 0 then
331 CurrentKeyBinding := LocalKeyBindings[ListKeyBindings.ItemIndex]
332 else CurrentKeyBinding := nil;
333
334 if Assigned(CurrentKeyBinding) then begin
335 if CurrentKeyBinding.ShortCut <> 0 then
336 EditShortCutPrimary.Text := ShortCutToText(CurrentKeyBinding.ShortCut)
337 else EditShortCutPrimary.Text := '';
338 EditShortCutPrimary.Enabled := True;
339 if CurrentKeyBinding.ShortCut2 <> 0 then
340 EditShortCutSecondary.Text := ShortCutToText(CurrentKeyBinding.ShortCut2)
341 else EditShortCutSecondary.Text := '';
342 EditShortCutSecondary.Enabled := True;
343 end else begin
344 EditShortCutPrimary.Text := '';
345 EditShortCutPrimary.Enabled := False;
346 EditShortCutSecondary.Text := '';
347 EditShortCutSecondary.Enabled := False;
348 end;
349end;
350
351procedure TSettingsDlg.ButtonOkClick(Sender: TObject);
352begin
353 SaveData;
354 ModalResult := mrOk;
355end;
356
357procedure TSettingsDlg.ButtonGammaUpClick(Sender: TObject);
358begin
359 if LocalGamma < 150 then begin
360 Inc(LocalGamma);
361 Invalidate;
362 end;
363end;
364
365procedure TSettingsDlg.UpdateShortCutItem;
366begin
367 if Assigned(CurrentKeyBinding) then begin
368 if CurrentKeyBinding.ShortCut > 0 then
369 LocalKeyBindings.RemoveShortCut(CurrentKeyBinding.ShortCut);
370 if CurrentKeyBinding.ShortCut2 > 0 then
371 LocalKeyBindings.RemoveShortCut(CurrentKeyBinding.ShortCut2);
372 CurrentKeyBinding.ShortCut := TextToShortCut(EditShortCutPrimary.Text);
373 CurrentKeyBinding.ShortCut2 := TextToShortCut(EditShortCutSecondary.Text);
374 LocalKeyBindings.LoadToStrings(ListKeyBindings.Items);
375 end;
376end;
377
378function TSettingsDlg.GetLocalCustomDpiEnabled: Boolean;
379begin
380 Result := (ButtonCustomDpi.ButtonIndex and 1) = 1;
381end;
382
383function TSettingsDlg.GetLocalMusicEnabled: Boolean;
384begin
385 Result := (ButtonMusic.ButtonIndex and 1) = 1;
386end;
387
388procedure TSettingsDlg.UpdateInterface;
389begin
390 ButtonDpiUp.Enabled := GetLocalCustomDpiEnabled;
391 ButtonDpiDown.Enabled := GetLocalCustomDpiEnabled;
392 ButtonMusicVolumeDown.Enabled := GetLocalMusicEnabled;
393 ButtonMusicVolumeUp.Enabled := GetLocalMusicEnabled;
394end;
395
396procedure TSettingsDlg.LoadData;
397begin
398 StartDlg.Translator.Language := StartDlg.Translator.Languages.SearchByCode(LocaleCode);
399 StartDlg.Translator.LanguageListToStrings(ListLanguages.Items, False);
400 ListLanguages.ItemIndex := ListLanguages.Items.IndexOfObject(StartDlg.Translator.Language);
401 if ListLanguages.ItemIndex = -1 then ListLanguages.ItemIndex := 0;
402 if FullScreen then ButtonFullscreen.ButtonIndex := 3
403 else ButtonFullscreen.ButtonIndex := 2;
404 if MusicEnabled then ButtonMusic.ButtonIndex := 3
405 else ButtonMusic.ButtonIndex := 2;
406 LocalMusicVolume := Round(MusicVolume * 100);
407 LocalGamma := Gamma;
408 LocalKeyBindings.Assign(KeyBindings.KeyBindings);
409 {$IFDEF DPI}
410 if CustomDpiEnabled then ButtonCustomDpi.ButtonIndex := 3
411 else ButtonCustomDpi.ButtonIndex := 2;
412 LocalDpi := Round(CustomDpi * 100 / 96);
413 {$ENDIF}
414end;
415
416procedure TSettingsDlg.SaveData;
417var
418 NeedRestart: Boolean;
419begin
420 NeedRestart := (Gamma <> LocalGamma) or (CustomDpiEnabled <> GetLocalCustomDpiEnabled) or
421 ((GetLocalCustomDpiEnabled and (LocalDpi <> CustomDpi)));
422 if ListLanguages.ItemIndex <> -1 then begin
423 StartDlg.Translator.Language := TLanguage(ListLanguages.Items.Objects[ListLanguages.ItemIndex]);
424 LocaleCode := StartDlg.Translator.Language.Code;
425 end else begin
426 StartDlg.Translator.Language := nil;
427 LocaleCode := '';
428 end;
429 FullScreen := (ButtonFullscreen.ButtonIndex and 1) = 1;
430 MusicEnabled := GetLocalMusicEnabled;
431 MusicVolume := LocalMusicVolume / 100;
432 Gamma := LocalGamma;
433 ScreenTools.CustomDpiEnabled := CustomDpiEnabled;
434 if NeedRestart then SimpleMessage(SRestartMsg);
435 KeyBindings.KeyBindings.Assign(LocalKeyBindings);
436 {$IFDEF DPI}
437 CustomDpiEnabled := GetLocalCustomDpiEnabled;
438 CustomDpi := Round(LocalDpi * 96 / 100);
439 {$ENDIF}
440end;
441
442end.
443
444
Note: See TracBrowser for help on using the repository browser.