source: branches/zoom/Settings.pas

Last change on this file was 717, checked in by chronos, 11 days ago

Merged revision(s) 705-716 from trunk:

  • Fixed: Subversion properties.
  • Fixed: Better start screen location calculation for lower than 800x600 resolutions to make visible entire window.
  • Fixed: Memory overflow in minimap drawing with some higher DPI settings.
  • Fixed: Crashes with mini map drawing in Start screen with some DPI resolutions.
  • Added: Enter key confirms battle dialog window.
  • Fixed: Potential battle dialog drawing issue.
  • Modified: Code cleanup.
  • Fixed: Disable auto selection of input text in message dialog.
  • Fixed: Edit boxes to have black background and blue selection.
  • Fixed: Automatically create Saved and Maps user data directories if they don't exist yet.
  • Modified: Code cleanup.
  • Fixed: Wrong unit models opened after click on Military report foreign units.
  • Modified: Do not store .png file extension in graphics names in book files. This is for compatibility with older C-evo versions.
  • Fixed: Support both books with graphics name with and without .png file extensions.
File size: 14.6 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
70var
71 SettingsDlg: TSettingsDlg;
72
73const
74 DpiMin = 100;
75 DpiMax = 500;
76 DpiStep = 25;
77 MusicVolumeMin = 0;
78 MusicVolumeMax = 100;
79 MusicVolumeStep = 10;
80
81
82implementation
83
84{$R *.lfm}
85
86uses
87 Start;
88
89var
90 SFullScreen, SGamma, SRestartMsg, SShortCutPrimary, SShortCutSecondary,
91 SLanguages, SKeyBindings, SCustomDpi, SDpi, SMusic, SMusicVolume: string;
92
93procedure ReloadLanguages;
94begin
95 SFullScreen := Phrases.Lookup('SETTINGS', 0);
96 SGamma := Phrases.Lookup('SETTINGS', 1);
97 SRestartMsg := Phrases.Lookup('SETTINGS', 2);
98 SShortCutPrimary := Phrases.Lookup('SETTINGS', 3);
99 SShortCutSecondary := Phrases.Lookup('SETTINGS', 4);
100 SLanguages := Phrases.Lookup('SETTINGS', 5);
101 SKeyBindings := Phrases.Lookup('SETTINGS', 6);
102 SCustomDpi := Phrases.Lookup('SETTINGS', 7);
103 SDpi := Phrases.Lookup('SETTINGS', 8);
104 SMusic := Phrases.Lookup('SETTINGS', 9);
105 SMusicVolume := Phrases.Lookup('SETTINGS', 10);
106end;
107
108{ TSettingsDlg }
109
110procedure TSettingsDlg.FormCreate(Sender: TObject);
111begin
112 Color := clBlack;
113 LocalKeyBindings := TKeyBindings.Create;
114
115 Canvas.Font.Assign(UniFont[ftNormal]);
116 Canvas.Brush.Style := TBrushStyle.bsClear;
117
118 ButtonOk.Caption := Phrases.Lookup('BTN_OK');
119 ButtonCancel.Caption := Phrases.Lookup('BTN_CANCEL');
120 ButtonReset.Caption := Phrases.Lookup('BTN_RESET');
121 InitButtons;
122end;
123
124procedure TSettingsDlg.ButtonCancelClick(Sender: TObject);
125begin
126 ModalResult := mrCancel;
127end;
128
129procedure TSettingsDlg.ButtonMusicClick(Sender: TObject);
130begin
131 ButtonMusic.ButtonIndex := ButtonMusic.ButtonIndex xor 1;
132 UpdateInterface;
133end;
134
135procedure TSettingsDlg.ButtonMusicVolumeDownClick(Sender: TObject);
136begin
137 Dec(LocalMusicVolume, MusicVolumeStep);
138 if LocalMusicVolume < MusicVolumeMin then LocalMusicVolume := MusicVolumeMin;
139 Invalidate;
140end;
141
142procedure TSettingsDlg.ButtonMusicVolumeUpClick(Sender: TObject);
143begin
144 Inc(LocalMusicVolume, MusicVolumeStep);
145 if LocalMusicVolume > MusicVolumeMax then LocalMusicVolume := MusicVolumeMax;
146 Invalidate;
147end;
148
149procedure TSettingsDlg.ButtonResetClick(Sender: TObject);
150begin
151 // TODO: Better to have default values on single place
152 ListLanguages.ItemIndex := 0;
153 ButtonFullscreen.ButtonIndex := 3;
154 ButtonMusic.ButtonIndex := 3;
155 LocalMusicVolume := 50;
156 ButtonCustomDpi.ButtonIndex := 2;
157 LocalDpi := 100;
158 LocalGamma := 100;
159 ListKeyBindings.ItemIndex := -1;
160 ListKeyBindingsSelectionChange(nil, False);
161 LocalKeyBindings.ResetToDefault;
162 LocalKeyBindings.LoadToStrings(ListKeyBindings.Items);
163 UpdateInterface;
164 Repaint;
165end;
166
167procedure TSettingsDlg.ButtonGammaDownClick(Sender: TObject);
168begin
169 if LocalGamma > 50 then
170 begin
171 Dec(LocalGamma);
172 Invalidate;
173 end;
174end;
175
176procedure TSettingsDlg.EditShortCutPrimaryKeyUp(Sender: TObject; var Key: Word;
177 Shift: TShiftState);
178begin
179 if (Sender is TEdit) and Assigned(CurrentKeyBinding) and not (Key in [16..18]) then begin
180 CurrentKeyBinding.ShortCut := Key or
181 (scShift * Integer(ssShift in Shift)) or
182 (scCtrl * Integer(ssCtrl in Shift)) or
183 (scAlt * Integer(ssAlt in Shift));
184 EditShortCutPrimary.Text := ShortCutToText(CurrentKeyBinding.ShortCut);
185 Key := 0;
186 UpdateShortCutItem;
187 end;
188end;
189
190procedure TSettingsDlg.EditShortCutSecondaryKeyUp(Sender: TObject;
191 var Key: Word; Shift: TShiftState);
192begin
193 if (Sender is TEdit) and Assigned(CurrentKeyBinding) and not (Key in [16..18]) then begin
194 CurrentKeyBinding.ShortCut2 := Key or
195 (scShift * Integer(ssShift in Shift)) or
196 (scCtrl * Integer(ssCtrl in Shift)) or
197 (scAlt * Integer(ssAlt in Shift));
198 EditShortCutSecondary.Text := ShortCutToText(CurrentKeyBinding.ShortCut2);
199 Key := 0;
200 UpdateShortCutItem;
201 end;
202end;
203
204procedure TSettingsDlg.FormClose(Sender: TObject; var CloseAction: TCloseAction
205 );
206begin
207 ListKeyBindings.ItemIndex := -1;
208end;
209
210procedure TSettingsDlg.ButtonFullscreenClick(Sender: TObject);
211begin
212 ButtonFullscreen.ButtonIndex := ButtonFullscreen.ButtonIndex xor 1;
213end;
214
215procedure TSettingsDlg.ButtonDpiDownClick(Sender: TObject);
216begin
217 Dec(LocalDpi, DpiStep);
218 if LocalDpi < DpiMin then LocalDpi := DpiMin;
219 Invalidate;
220end;
221
222procedure TSettingsDlg.ButtonCustomDpiClick(Sender: TObject);
223begin
224 ButtonCustomDpi.ButtonIndex := ButtonCustomDpi.ButtonIndex xor 1;
225 UpdateInterface;
226end;
227
228procedure TSettingsDlg.ButtonDpiUpClick(Sender: TObject);
229begin
230 Inc(LocalDpi, DpiStep);
231 if LocalDpi > DpiMax then LocalDpi := DpiMax;
232 Invalidate;
233end;
234
235procedure TSettingsDlg.FormDestroy(Sender: TObject);
236begin
237 FreeAndNil(LocalKeyBindings);
238end;
239
240procedure TSettingsDlg.FormPaint(Sender: TObject);
241const
242 TextDistanceX = 20;
243 UpDownTextWidth = 120;
244begin
245 PaintBackground(Canvas, 3, 3, ClientWidth - 6, ClientHeight - 6,
246 ClientWidth, ClientHeight);
247 Frame(Canvas, 0, 0, ClientWidth - 1, ClientHeight - 1, 0, 0);
248 Frame(Canvas, 1, 1, ClientWidth - 2, ClientHeight - 2,
249 MainTexture.ColorBevelLight, MainTexture.ColorBevelShade);
250 Frame(Canvas, 2, 2, ClientWidth - 3, ClientHeight - 3,
251 MainTexture.ColorBevelLight, MainTexture.ColorBevelShade);
252 EditFrame(Canvas, EditShortCutPrimary.BoundsRect, MainTexture);
253 EditFrame(Canvas, EditShortCutSecondary.BoundsRect, MainTexture);
254 EditFrame(Canvas, ListLanguages.BoundsRect, MainTexture);
255 BtnFrame(Canvas, ButtonOk.BoundsRect, MainTexture);
256 BtnFrame(Canvas, ButtonCancel.BoundsRect, MainTexture);
257
258 RFrame(Canvas, ButtonFullscreen.Left - 1, ButtonFullscreen.Top - 1,
259 ButtonFullscreen.Left + 12, ButtonFullscreen.Top + 12, MainTexture.ColorBevelShade,
260 MainTexture.ColorBevelLight);
261 {$IFDEF DPI}
262 RFrame(Canvas, ButtonCustomDpi.Left - 1, ButtonCustomDpi.Top - 1,
263 ButtonCustomDpi.Left + 12, ButtonCustomDpi.Top + 12, MainTexture.ColorBevelShade,
264 MainTexture.ColorBevelLight);
265 LoweredTextOut(Canvas, -2, MainTexture, ButtonCustomDpi.Left + TextDistanceX,
266 ButtonCustomDpi.Top - 4, SCustomDpi);
267 UnderlinedTitleValue(Canvas, SDpi, IntToStr(LocalDpi) + '%',
268 ButtonDpiUp.Left - UpDownTextWidth - 4, ButtonDpiUp.Top + 2, UpDownTextWidth);
269 {$ENDIF}
270
271 RFrame(Canvas, ButtonMusic.Left - 1, ButtonMusic.Top - 1,
272 ButtonMusic.Left + 12, ButtonMusic.Top + 12, MainTexture.ColorBevelShade,
273 MainTexture.ColorBevelLight);
274 LoweredTextOut(Canvas, -2, MainTexture, ButtonMusic.Left + TextDistanceX,
275 ButtonMusic.Top - 4, SMusic);
276 UnderlinedTitleValue(Canvas, SMusicVolume, IntToStr(LocalMusicVolume) + '%',
277 ButtonMusicVolumeUp.Left - UpDownTextWidth - 4, ButtonMusicVolumeUp.Top + 2, UpDownTextWidth);
278
279 LoweredTextOut(Canvas, -2, MainTexture, ListLanguages.Left,
280 ListLanguages.Top - 26, SLanguages);
281 LoweredTextOut(Canvas, -2, MainTexture, ListKeyBindings.Left,
282 ListKeyBindings.Top - 26, SKeyBindings);
283 LoweredTextOut(Canvas, -2, MainTexture, ButtonFullscreen.Left + TextDistanceX,
284 ButtonFullscreen.Top - 4, SFullScreen);
285 UnderlinedTitleValue(Canvas, SGamma, IntToStr(LocalGamma) + '%',
286 ButtonGammaUp.Left - UpDownTextWidth - 4, ButtonGammaUp.Top + 2, UpDownTextWidth);
287 LoweredTextOut(Canvas, -2, MainTexture, EditShortCutPrimary.Left,
288 EditShortCutPrimary.Top - 26, SShortCutPrimary);
289 LoweredTextOut(Canvas, -2, MainTexture, EditShortCutSecondary.Left,
290 EditShortCutSecondary.Top - 26, SShortCutSecondary);
291end;
292
293procedure TSettingsDlg.FormShow(Sender: TObject);
294begin
295 Caption := Phrases2.Lookup('ACTIONHEADER_CONFIG');
296 ReloadLanguages;
297 StartDlg.Translator.LanguageListToStrings(ListLanguages.Items);
298 ListLanguages.Font.Color := MainTexture.ColorMark;
299 ListKeyBindings.Font.Color := MainTexture.ColorMark;
300 LoadData;
301 LocalKeyBindings.LoadToStrings(ListKeyBindings.Items);
302 EditShortCutPrimary.Font.Color := MainTexture.ColorMark;
303 EditShortCutSecondary.Font.Color := MainTexture.ColorMark;
304 {$IFDEF DPI}
305 ButtonCustomDpi.Visible := True;
306 ButtonDpiDown.Visible := True;
307 ButtonDpiUp.Visible := True;
308 {$ELSE}
309 ButtonCustomDpi.Visible := False;
310 ButtonDpiDown.Visible := False;
311 ButtonDpiUp.Visible := False;
312 {$ENDIF}
313 UpdateInterface;
314 Gtk2DisableControlStyling(EditShortCutPrimary);
315 Gtk2DisableControlStyling(EditShortCutSecondary);
316end;
317
318procedure TSettingsDlg.ListKeyBindingsKeyDown(Sender: TObject; var Key: Word;
319 Shift: TShiftState);
320begin
321 KeyDown(Key, Shift);
322end;
323
324procedure TSettingsDlg.ListKeyBindingsSelectionChange(Sender: TObject;
325 User: Boolean);
326begin
327 if Assigned(CurrentKeyBinding) then begin
328 CurrentKeyBinding.ShortCut := TextToShortCut(EditShortCutPrimary.Text);
329 CurrentKeyBinding.ShortCut2 := TextToShortCut(EditShortCutSecondary.Text);
330 end;
331
332 if ListKeyBindings.ItemIndex >= 0 then
333 CurrentKeyBinding := LocalKeyBindings[ListKeyBindings.ItemIndex]
334 else CurrentKeyBinding := nil;
335
336 if Assigned(CurrentKeyBinding) then begin
337 if CurrentKeyBinding.ShortCut <> 0 then
338 EditShortCutPrimary.Text := ShortCutToText(CurrentKeyBinding.ShortCut)
339 else EditShortCutPrimary.Text := '';
340 EditShortCutPrimary.Enabled := True;
341 if CurrentKeyBinding.ShortCut2 <> 0 then
342 EditShortCutSecondary.Text := ShortCutToText(CurrentKeyBinding.ShortCut2)
343 else EditShortCutSecondary.Text := '';
344 EditShortCutSecondary.Enabled := True;
345 end else begin
346 EditShortCutPrimary.Text := '';
347 EditShortCutPrimary.Enabled := False;
348 EditShortCutSecondary.Text := '';
349 EditShortCutSecondary.Enabled := False;
350 end;
351end;
352
353procedure TSettingsDlg.ButtonOkClick(Sender: TObject);
354begin
355 SaveData;
356 ModalResult := mrOk;
357end;
358
359procedure TSettingsDlg.ButtonGammaUpClick(Sender: TObject);
360begin
361 if LocalGamma < 150 then begin
362 Inc(LocalGamma);
363 Invalidate;
364 end;
365end;
366
367procedure TSettingsDlg.UpdateShortCutItem;
368begin
369 if Assigned(CurrentKeyBinding) then begin
370 if CurrentKeyBinding.ShortCut > 0 then
371 LocalKeyBindings.RemoveShortCut(CurrentKeyBinding.ShortCut);
372 if CurrentKeyBinding.ShortCut2 > 0 then
373 LocalKeyBindings.RemoveShortCut(CurrentKeyBinding.ShortCut2);
374 CurrentKeyBinding.ShortCut := TextToShortCut(EditShortCutPrimary.Text);
375 CurrentKeyBinding.ShortCut2 := TextToShortCut(EditShortCutSecondary.Text);
376 LocalKeyBindings.LoadToStrings(ListKeyBindings.Items);
377 end;
378end;
379
380function TSettingsDlg.GetLocalCustomDpiEnabled: Boolean;
381begin
382 Result := (ButtonCustomDpi.ButtonIndex and 1) = 1;
383end;
384
385function TSettingsDlg.GetLocalMusicEnabled: Boolean;
386begin
387 Result := (ButtonMusic.ButtonIndex and 1) = 1;
388end;
389
390procedure TSettingsDlg.UpdateInterface;
391begin
392 ButtonDpiUp.Enabled := GetLocalCustomDpiEnabled;
393 ButtonDpiDown.Enabled := GetLocalCustomDpiEnabled;
394 ButtonMusicVolumeDown.Enabled := GetLocalMusicEnabled;
395 ButtonMusicVolumeUp.Enabled := GetLocalMusicEnabled;
396end;
397
398procedure TSettingsDlg.LoadData;
399begin
400 StartDlg.Translator.Language := StartDlg.Translator.Languages.SearchByCode(LocaleCode);
401 StartDlg.Translator.LanguageListToStrings(ListLanguages.Items, False);
402 ListLanguages.ItemIndex := ListLanguages.Items.IndexOfObject(StartDlg.Translator.Language);
403 if ListLanguages.ItemIndex = -1 then ListLanguages.ItemIndex := 0;
404 if FullScreen then ButtonFullscreen.ButtonIndex := 3
405 else ButtonFullscreen.ButtonIndex := 2;
406 if MusicEnabled then ButtonMusic.ButtonIndex := 3
407 else ButtonMusic.ButtonIndex := 2;
408 LocalMusicVolume := Round(MusicVolume * 100);
409 LocalGamma := Gamma;
410 LocalKeyBindings.Assign(KeyBindings.KeyBindings);
411 {$IFDEF DPI}
412 if CustomDpiEnabled then ButtonCustomDpi.ButtonIndex := 3
413 else ButtonCustomDpi.ButtonIndex := 2;
414 LocalDpi := Round(CustomDpi * 100 / 96);
415 {$ENDIF}
416end;
417
418procedure TSettingsDlg.SaveData;
419var
420 NeedRestart: Boolean;
421begin
422 NeedRestart := (Gamma <> LocalGamma) or (CustomDpiEnabled <> GetLocalCustomDpiEnabled) or
423 ((GetLocalCustomDpiEnabled and (LocalDpi <> CustomDpi)));
424 if ListLanguages.ItemIndex <> -1 then begin
425 StartDlg.Translator.Language := TLanguage(ListLanguages.Items.Objects[ListLanguages.ItemIndex]);
426 LocaleCode := StartDlg.Translator.Language.Code;
427 end else begin
428 StartDlg.Translator.Language := nil;
429 LocaleCode := '';
430 end;
431 FullScreen := (ButtonFullscreen.ButtonIndex and 1) = 1;
432 MusicEnabled := GetLocalMusicEnabled;
433 MusicVolume := LocalMusicVolume / 100;
434 Gamma := LocalGamma;
435 ScreenTools.CustomDpiEnabled := CustomDpiEnabled;
436 if NeedRestart then SimpleMessage(SRestartMsg);
437 KeyBindings.KeyBindings.Assign(LocalKeyBindings);
438 {$IFDEF DPI}
439 CustomDpiEnabled := GetLocalCustomDpiEnabled;
440 CustomDpi := Round(LocalDpi * 96 / 100);
441 {$ENDIF}
442end;
443
444end.
445
446
Note: See TracBrowser for help on using the repository browser.