Changeset 385 for trunk/Settings.pas


Ignore:
Timestamp:
Apr 27, 2021, 6:42:29 PM (4 years ago)
Author:
chronos
Message:
  • Added: Allow to change key bindings from Settings dialog.
  • Added: Allow to reset settings in Settings dialog.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Settings.pas

    r352 r385  
    77uses
    88  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
    9   ScreenTools, Messg, ButtonA, Registry, fgl, Directories, DrawDlg, ButtonC;
     9  LCLProc, ScreenTools, Messg, ButtonA, Registry, fgl, Directories, DrawDlg,
     10  ButtonC, UKeyBindings, ULanguages;
    1011
    1112type
    12   TLanguage = class
    13     ShortName: string;
    14     FullName: string;
    15     Author: string;
    16   end;
    17 
    18   { TLanguages }
    19 
    20   TLanguages = class(TFPGObjectList<TLanguage>)
    21     procedure AddItem(const ShortName, FullName: string);
    22     procedure LoadToStrings(Strings: TStrings);
    23     function Search(ShortName: string): Integer;
    24   end;
    25 
    2613  { TSettingsDlg }
    2714
     
    2916    ButtonFullscreen: TButtonC;
    3017    Down2Btn: TButtonC;
    31     List: TListBox;
    32     OKBtn: TButtonA;
    33     CancelBtn: TButtonA;
     18    EditShortCutPrimary: TEdit;
     19    EditShortCutSecondary: TEdit;
     20    ListLanguages: TListBox;
     21    ListKeyBindings: TListBox;
     22    ButtonOk: TButtonA;
     23    ButtonCancel: TButtonA;
     24    ButtonReset: TButtonA;
    3425    Up2Btn: TButtonC;
    3526    procedure ButtonFullscreenClick(Sender: TObject);
    36     procedure CancelBtnClick(Sender: TObject);
     27    procedure ButtonCancelClick(Sender: TObject);
     28    procedure ButtonResetClick(Sender: TObject);
    3729    procedure Down2BtnClick(Sender: TObject);
     30    procedure EditShortCutPrimaryKeyUp(Sender: TObject; var Key: Word;
     31      Shift: TShiftState);
     32    procedure EditShortCutSecondaryKeyUp(Sender: TObject; var Key: Word;
     33      Shift: TShiftState);
     34    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    3835    procedure FormCreate(Sender: TObject);
    3936    procedure FormDestroy(Sender: TObject);
    4037    procedure FormPaint(Sender: TObject);
    4138    procedure FormShow(Sender: TObject);
    42     procedure OKBtnClick(Sender: TObject);
     39    procedure ListKeyBindingsSelectionChange(Sender: TObject; User: boolean);
     40    procedure ButtonOkClick(Sender: TObject);
    4341    procedure Up2BtnClick(Sender: TObject);
    4442  private
    4543    LocalGamma: Integer;
     44    LocalKeyBindings: TKeyBindings;
     45    CurrentKeyBinding: TKeyBinding;
     46    procedure UpdateShortCutItem;
    4647  public
    47     Languages: TLanguages;
    4848    procedure LoadData;
    4949    procedure SaveData;
     
    5353  SettingsDlg: TSettingsDlg;
    5454
     55
    5556implementation
    5657
    5758{$R *.lfm}
    5859
    59 { TLanguages }
    60 
    61 procedure TLanguages.AddItem(const ShortName, FullName: string);
    6260var
    63   Language: TLanguage;
    64 begin
    65   Language := TLanguage.Create;
    66   Language.ShortName := ShortName;
    67   Language.FullName := FullName;
    68   Add(Language);
    69 end;
    70 
    71 procedure TLanguages.LoadToStrings(Strings: TStrings);
    72 var
    73   I: Integer;
    74 begin
    75   Strings.Clear;
    76   for I := 0 to Count - 1 do
    77     Strings.Add(Items[I].FullName);
    78 end;
    79 
    80 function TLanguages.Search(ShortName: string): Integer;
    81 var
    82   I: Integer;
    83 begin
    84   I := 0;
    85   while (I < Count) and (Items[I].ShortName <> ShortName) do Inc(I);
    86   if I < Count then Result := I
    87     else Result := -1;
     61  SFullScreen, SGamma, SRestartMsg, SShortCutPrimary, SShortCutSecondary,
     62  SLanguages, SKeyBindings: string;
     63
     64procedure ReloadLanguages;
     65begin
     66  SFullScreen := Phrases.Lookup('SETTINGS', 0);
     67  SGamma := Phrases.Lookup('SETTINGS', 1);
     68  SRestartMsg := Phrases.Lookup('SETTINGS', 2);
     69  SShortCutPrimary := Phrases.Lookup('SETTINGS', 3);
     70  SShortCutSecondary := Phrases.Lookup('SETTINGS', 4);
     71  SLanguages := Phrases.Lookup('SETTINGS', 5);
     72  SKeyBindings := Phrases.Lookup('SETTINGS', 6);
    8873end;
    8974
     
    9277procedure TSettingsDlg.FormCreate(Sender: TObject);
    9378begin
     79  LocalKeyBindings := TKeyBindings.Create;
     80
    9481  Canvas.Font.Assign(UniFont[ftNormal]);
    9582  Canvas.Brush.Style := bsClear;
    9683
    97   Languages := TLanguages.Create;
    98   Languages.AddItem('', 'System');
    99   Languages.AddItem('cs', 'Czech');
    100   Languages.AddItem('de', 'German');
    101   Languages.AddItem('en', 'English');
    102   Languages.AddItem('it', 'Italian');
    103   Languages.AddItem('ru', 'Russian');
    104   Languages.AddItem('zh-Hant', 'Traditional Chinese');
    105   Languages.AddItem('zh-Hans', 'Simplified Chinese');
    106 
    107   OKBtn.Caption := Phrases.Lookup('BTN_OK');
    108   CancelBtn.Caption := Phrases.Lookup('BTN_CANCEL');
     84  ButtonOk.Caption := Phrases.Lookup('BTN_OK');
     85  ButtonCancel.Caption := Phrases.Lookup('BTN_CANCEL');
     86  ButtonReset.Caption := Phrases.Lookup('BTN_RESET');
    10987  InitButtons;
    11088end;
    11189
    112 procedure TSettingsDlg.CancelBtnClick(Sender: TObject);
     90procedure TSettingsDlg.ButtonCancelClick(Sender: TObject);
    11391begin
    11492  ModalResult := mrCancel;
     93end;
     94
     95procedure TSettingsDlg.ButtonResetClick(Sender: TObject);
     96begin
     97  ListLanguages.ItemIndex := 0;
     98  ButtonFullscreen.ButtonIndex := 3;
     99  LocalGamma := 100;
     100  ListKeyBindings.ItemIndex := -1;
     101  ListKeyBindingsSelectionChange(nil, False);
     102  LocalKeyBindings.ResetToDefault;
     103  LocalKeyBindings.LoadToStrings(ListKeyBindings.Items);
     104  Repaint;
    115105end;
    116106
     
    124114end;
    125115
     116procedure TSettingsDlg.EditShortCutPrimaryKeyUp(Sender: TObject; var Key: Word;
     117  Shift: TShiftState);
     118begin
     119  if (Sender is TEdit) and Assigned(CurrentKeyBinding) and not (Key in [16..18]) then begin
     120    CurrentKeyBinding.ShortCut := Key or
     121      (scShift * Integer(ssShift in Shift)) or
     122      (scCtrl * Integer(ssCtrl in Shift)) or
     123      (scAlt * Integer(ssAlt in Shift));
     124    EditShortCutPrimary.Text := ShortCutToText(CurrentKeyBinding.ShortCut);
     125    Key := 0;
     126    UpdateShortCutItem;
     127  end;
     128end;
     129
     130procedure TSettingsDlg.EditShortCutSecondaryKeyUp(Sender: TObject;
     131  var Key: Word; Shift: TShiftState);
     132begin
     133  if (Sender is TEdit) and Assigned(CurrentKeyBinding) and not (Key in [16..18]) then begin
     134    CurrentKeyBinding.ShortCut2 := Key or
     135      (scShift * Integer(ssShift in Shift)) or
     136      (scCtrl * Integer(ssCtrl in Shift)) or
     137      (scAlt * Integer(ssAlt in Shift));
     138    EditShortCutSecondary.Text := ShortCutToText(CurrentKeyBinding.ShortCut2);
     139    Key := 0;
     140    UpdateShortCutItem;
     141  end;
     142end;
     143
     144procedure TSettingsDlg.FormClose(Sender: TObject; var CloseAction: TCloseAction
     145  );
     146begin
     147  ListKeyBindings.ItemIndex := -1;
     148end;
     149
    126150procedure TSettingsDlg.ButtonFullscreenClick(Sender: TObject);
    127151begin
     
    131155procedure TSettingsDlg.FormDestroy(Sender: TObject);
    132156begin
    133   FreeAndNil(Languages);
     157  FreeAndNil(LocalKeyBindings);
    134158end;
    135159
    136160procedure TSettingsDlg.FormPaint(Sender: TObject);
    137 var
    138   S: string;
    139161begin
    140162  PaintBackground(self, 3, 3, ClientWidth - 6, ClientHeight - 6);
     
    144166  Frame(Canvas, 2, 2, ClientWidth - 3, ClientHeight - 3,
    145167    MainTexture.ColorBevelLight, MainTexture.ColorBevelShade);
    146   EditFrame(Canvas, List.BoundsRect, MainTexture);
    147   BtnFrame(Canvas, OKBtn.BoundsRect, MainTexture);
    148   BtnFrame(Canvas, CancelBtn.BoundsRect, MainTexture);
     168  EditFrame(Canvas, ListLanguages.BoundsRect, MainTexture);
     169  BtnFrame(Canvas, ButtonOk.BoundsRect, MainTexture);
     170  BtnFrame(Canvas, ButtonCancel.BoundsRect, MainTexture);
    149171
    150172  RFrame(Canvas, ButtonFullscreen.Left - 1, ButtonFullscreen.Top - 1,
     
    152174    MainTexture.ColorBevelLight);
    153175
    154   S := Phrases.Lookup('SETTINGS', 0);
     176  LoweredTextOut(Canvas, -2, MainTexture, ListLanguages.Left,
     177    ListLanguages.Top - 26, SLanguages);
     178  LoweredTextOut(Canvas, -2, MainTexture, ListKeyBindings.Left,
     179    ListKeyBindings.Top - 26, SKeyBindings);
    155180  LoweredTextOut(Canvas, -2, MainTexture, ButtonFullscreen.Left + 32,
    156     ButtonFullscreen.Top - 4, S);
    157 
    158   // Gamma
    159   UnderlinedTitleValue(Canvas, Phrases.Lookup('SETTINGS', 1), IntToStr(LocalGamma) + '%',
     181    ButtonFullscreen.Top - 4, SFullScreen);
     182  UnderlinedTitleValue(Canvas, SGamma, IntToStr(LocalGamma) + '%',
    160183    Up2Btn.Left - 150 - 4, Up2Btn.Top + 2, 150);
     184  LoweredTextOut(Canvas, -2, MainTexture, EditShortCutPrimary.Left,
     185    EditShortCutPrimary.Top - 26, SShortCutPrimary);
     186  LoweredTextOut(Canvas, -2, MainTexture, EditShortCutSecondary.Left,
     187    EditShortCutSecondary.Top - 26, SShortCutSecondary);
    161188end;
    162189
    163190procedure TSettingsDlg.FormShow(Sender: TObject);
    164191begin
    165   Languages.LoadToStrings(List.Items);
    166   List.Font.Color := MainTexture.ColorMark;
     192  ReloadLanguages;
     193  Languages.LoadToStrings(ListLanguages.Items);
     194  ListLanguages.Font.Color := MainTexture.ColorMark;
     195  ListKeyBindings.Font.Color := MainTexture.ColorMark;
    167196  LoadData;
    168 end;
    169 
    170 procedure TSettingsDlg.OKBtnClick(Sender: TObject);
     197  LocalKeyBindings.LoadToStrings(ListKeyBindings.Items);
     198end;
     199
     200procedure TSettingsDlg.ListKeyBindingsSelectionChange(Sender: TObject;
     201  User: boolean);
     202begin
     203  if Assigned(CurrentKeyBinding) then begin
     204    CurrentKeyBinding.ShortCut := TextToShortCut(EditShortCutPrimary.Text);
     205    CurrentKeyBinding.ShortCut2 := TextToShortCut(EditShortCutSecondary.Text);
     206  end;
     207
     208  if ListKeyBindings.ItemIndex >= 0 then
     209    CurrentKeyBinding := LocalKeyBindings[ListKeyBindings.ItemIndex]
     210    else CurrentKeyBinding := nil;
     211
     212  if Assigned(CurrentKeyBinding) then begin
     213    if CurrentKeyBinding.ShortCut <> 0 then
     214      EditShortCutPrimary.Text := ShortCutToText(CurrentKeyBinding.ShortCut)
     215      else EditShortCutPrimary.Text := '';
     216    EditShortCutPrimary.Enabled := True;
     217    if CurrentKeyBinding.ShortCut2 <> 0 then
     218      EditShortCutSecondary.Text := ShortCutToText(CurrentKeyBinding.ShortCut2)
     219      else EditShortCutSecondary.Text := '';
     220    EditShortCutSecondary.Enabled := True;
     221  end else begin
     222    EditShortCutPrimary.Text := '';
     223    EditShortCutPrimary.Enabled := False;
     224    EditShortCutSecondary.Text := '';
     225    EditShortCutSecondary.Enabled := False;
     226  end;
     227end;
     228
     229procedure TSettingsDlg.ButtonOkClick(Sender: TObject);
    171230begin
    172231  SaveData;
     
    182241end;
    183242
     243procedure TSettingsDlg.UpdateShortCutItem;
     244begin
     245  if Assigned(CurrentKeyBinding) then begin
     246    CurrentKeyBinding.ShortCut := TextToShortCut(EditShortCutPrimary.Text);
     247    CurrentKeyBinding.ShortCut2 := TextToShortCut(EditShortCutSecondary.Text);
     248    LocalKeyBindings.LoadToStrings(ListKeyBindings.Items);
     249  end;
     250end;
     251
    184252procedure TSettingsDlg.LoadData;
    185253begin
    186   List.ItemIndex := Languages.Search(LocaleCode);
    187   if (List.ItemIndex = -1) and (Languages.Count > 0) then
    188     List.ItemIndex := 0;
     254  ListLanguages.ItemIndex := Languages.Search(LocaleCode);
     255  if (ListLanguages.ItemIndex = -1) and (Languages.Count > 0) then
     256    ListLanguages.ItemIndex := 0;
    189257  if FullScreen then ButtonFullscreen.ButtonIndex := 3
    190258    else ButtonFullscreen.ButtonIndex := 2;
    191259  LocalGamma := Gamma;
     260  LocalKeyBindings.Assign(KeyBindings);
    192261end;
    193262
     
    197266begin
    198267  NeedRestart := Gamma <> LocalGamma;
    199   LocaleCode := Languages[List.ItemIndex].ShortName;
     268  LocaleCode := Languages[ListLanguages.ItemIndex].ShortName;
    200269  FullScreen := (ButtonFullscreen.ButtonIndex and 1) = 1;
    201270  Gamma := LocalGamma;
    202   if NeedRestart then SimpleMessage(Phrases.Lookup('SETTINGS', 2));
     271  if NeedRestart then SimpleMessage(SRestartMsg);
     272  KeyBindings.Assign(LocalKeyBindings);
    203273end;
    204274
Note: See TracChangeset for help on using the changeset viewer.