Changeset 292


Ignore:
Timestamp:
Mar 2, 2021, 10:45:43 PM (4 years ago)
Author:
chronos
Message:
  • Modified: Store key bindings in registry.
Location:
trunk
Files:
3 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/Integrated.lpi

    r288 r292  
    333333      </Unit40>
    334334      <Unit41>
    335         <Filename Value="UKeyBindings.pas"/>
     335        <Filename Value="LocalPlayer\UKeyBindings.pas"/>
    336336        <IsPartOfProject Value="True"/>
    337337      </Unit41>
  • trunk/LocalPlayer/Term.pas

    r291 r292  
    34033403  i, j: integer;
    34043404begin
     3405  KeyBindings.LoadFromRegistry(HKEY_CURRENT_USER, AppRegistryKey + '\KeyBindings');
    34053406  UpdateKeyShortcuts;
    34063407
     
    35073508  I: Integer;
    35083509begin
     3510  KeyBindings.SaveToRegistry(HKEY_CURRENT_USER, AppRegistryKey + '\KeyBindings');
    35093511  MainFormKeyDown := nil;
    35103512  FreeAndNil(sb);
  • trunk/LocalPlayer/UKeyBindings.pas

    r291 r292  
    66
    77uses
    8   Classes, SysUtils, fgl, LCLProc, LCLType, Menus;
     8  Classes, SysUtils, fgl, LCLProc, LCLType, Menus, Registry;
    99
    1010type
     
    2323
    2424  TKeyBindings = class(TFPGObjectList<TKeyBinding>)
     25  public
    2526    function AddItem(const ShortName, FullName: string; ShortCut: TShortCut; ShortCut2: TShortCut = 0): TKeyBinding; overload;
    2627    function AddItem(const ShortName, FullName: string; ShortCutText: string; ShortCutText2: string = ''): TKeyBinding; overload;
    2728    function Search(ShortName: string): TKeyBinding;
    28     procedure LoadFromFile(FileName: string);
    29     procedure SaveToFile(FileName: string);
     29    procedure LoadFromRegistry(RootKey: HKEY; Key: string);
     30    procedure SaveToRegistry(RootKey: HKEY; Key: string);
    3031  end;
    3132
     
    151152end;
    152153
    153 procedure TKeyBindings.LoadFromFile(FileName: string);
    154 var
    155   Lines: TStringList;
     154procedure TKeyBindings.LoadFromRegistry(RootKey: HKEY; Key: string);
     155var
    156156  I: Integer;
    157   KeyBinding: TKeyBinding;
    158 begin
    159   Lines := TStringList.Create;
    160   Lines.NameValueSeparator := '=';
     157  Registry: TRegistry;
     158  Text: string;
     159begin
     160  Registry := TRegistry.Create;
     161  Registry.RootKey := RootKey;
     162  with Registry do
    161163  try
    162     Lines.LoadFromFile(FileName);
    163     for I := 0 to Lines.Count - 1 do begin
    164       KeyBinding := Search(Lines.Names[I]);
    165       if Assigned(KeyBinding) then begin
    166         KeyBinding.ShortCut := TextToShortCut(Lines.ValueFromIndex[I]);
     164    OpenKey(Key, True);
     165    for I := 0 to Count - 1 do begin
     166      Text := '';
     167      if ValueExists(Items[I].ShortName) then begin
     168        Text := ReadString(Items[I].ShortName);
     169        if Pos(',', Text) > 0 then begin
     170          Items[I].ShortCut2 := TextToShortCut(Copy(Text, Pos(',', Text) + 1, MaxInt));
     171          Items[I].ShortCut := TextToShortCut(Copy(Text, 1, Pos(',', Text) - 1));
     172        end else begin
     173          Items[I].ShortCut := TextToShortCut(Text);
     174          Items[I].ShortCut2 := 0;
     175        end;
     176      end else begin
     177        Text := ShortCutToText(Items[I].ShortCut);
     178        if Items[I].ShortCut2 <> 0 then Text := Text + ',' + ShortCutToText(Items[I].ShortCut2);
     179        WriteString(Items[I].ShortName, Text);
    167180      end;
    168181    end;
    169182  finally
    170     FreeAndNil(Lines);
    171   end;
    172 end;
    173 
    174 procedure TKeyBindings.SaveToFile(FileName: string);
    175 var
    176   Lines: TStringList;
     183    Free;
     184  end;
     185end;
     186
     187procedure TKeyBindings.SaveToRegistry(RootKey: HKEY; Key: string);
     188var
    177189  I: Integer;
    178 begin
    179   Lines := TStringList.Create;
    180   Lines.NameValueSeparator := '=';
     190  Registry: TRegistry;
     191  Text: string;
     192begin
     193  Registry := TRegistry.Create;
     194  Registry.RootKey := RootKey;
     195  with Registry do
    181196  try
     197    OpenKey(Key, True);
    182198    for I := 0 to Count - 1 do begin
    183       Lines.Add(TKeyBinding(Items[I]).ShortName + '=' + ShortCutToText(TKeyBinding(Items[I]).ShortCut));
     199      Text := ShortCutToText(Items[I].ShortCut);
     200      if Items[I].ShortCut2 <> 0 then Text := Text + ',' + ShortCutToText(Items[I].ShortCut2);
     201      WriteString(Items[I].ShortName, Text);
    184202    end;
    185     Lines.SaveToFile(FileName);
    186203  finally
    187     FreeAndNil(Lines);
     204    Free;
    188205  end;
    189206end;
  • trunk/Start.pas

    r290 r292  
    166166
    167167uses
    168   Global, Directories, Direct, ScreenTools, Inp, Back, Settings, UPixelPointer,
    169   UKeyBindings;
     168  Global, Directories, Direct, ScreenTools, Inp, Back, Settings, UPixelPointer;
    170169
    171170{$R *.lfm}
     
    226225  PlayerAutoDiff: array [1 .. 5] of integer = (1, 1, 2, 2, 3);
    227226  EnemyAutoDiff: array [1 .. 5] of integer = (4, 3, 2, 1, 1);
    228   KeyBindingsFileName = 'KeyBindings.txt';
    229227
    230228{ TMiniMap }
     
    438436  PlayerSlot: TPlayerSlot;
    439437  AIBrains: TBrains;
    440   KeyBindingsAbsoluteFileName: string;
    441438begin
    442439  PlayerSlots := TPlayerSlots.Create;
     
    449446  LoadConfig;
    450447  LoadAssets;
    451   KeyBindingsAbsoluteFileName := DataDir + DirectorySeparator + KeyBindingsFileName;
    452   if FileExists(KeyBindingsAbsoluteFileName) then KeyBindings.LoadFromFile(KeyBindingsAbsoluteFileName)
    453     else begin
    454       ForceDirectories(ExtractFileDir(KeyBindingsAbsoluteFileName));
    455       KeyBindings.SaveToFile(KeyBindingsAbsoluteFileName);
    456     end;
    457448
    458449  ActionsOffered := [maConfig, maManual, maCredits, maWeb];
     
    19791970  Shift: TShiftState);
    19801971begin
    1981   if KeyToShortCut(Key, Shift) = BHelp.ShortCut then
     1972  if KeyToShortCut(Key, Shift) = VK_F1 then
    19821973    DirectHelp(cStartHelp);
    19831974end;
Note: See TracChangeset for help on using the changeset viewer.