source: tags/1.3.6/LocalPlayer/KeyBindings.pas

Last change on this file was 615, checked in by chronos, 2 months ago
  • Added: Allow to scroll map with Ctrl+arrows/num keys.
File size: 13.9 KB
Line 
1unit KeyBindings;
2
3interface
4
5uses
6 Classes, SysUtils, Generics.Collections, Generics.Defaults, LCLProc, LCLType,
7 Registry,
8 {$IFDEF DPI}Dpi.Menus{$ELSE}Menus{$ENDIF};
9
10type
11
12 { TKeyBinding }
13
14 TKeyBinding = class
15 ShortName: string;
16 FullName: string;
17 ShortCut: TShortCut;
18 ShortCut2: TShortCut;
19 DefaultShortCut: TShortCut;
20 DefaultShortCut2: TShortCut;
21 function Test(AShortCut: TShortCut): Boolean;
22 procedure Assign(Source: TKeyBinding);
23 procedure SetDefault;
24 end;
25
26 { TKeyBindings }
27
28 TKeyBindings = class(TObjectList<TKeyBinding>)
29 private
30 public
31 function AddItem(const ShortName, FullName: string; ShortCut: TShortCut; ShortCut2: TShortCut = 0): TKeyBinding; overload;
32 function AddItem(const ShortName, FullName: string; ShortCutText: string; ShortCutText2: string = ''): TKeyBinding; overload;
33 function Search(ShortName: string): TKeyBinding;
34 procedure LoadFromRegistry(RootKey: HKEY; Key: string);
35 procedure SaveToRegistry(RootKey: HKEY; Key: string);
36 procedure LoadToStrings(Strings: TStrings);
37 procedure Assign(Source: TKeyBindings);
38 procedure ResetToDefault;
39 procedure RemoveShortCut(ShortCut: TShortCut);
40 procedure SortAlpha;
41 end;
42
43var
44 KeyBindings: TKeyBindings;
45 BEndTurn: TKeyBinding;
46 BHelp: TKeyBinding;
47 BUnitStat: TKeyBinding;
48 BCityStat: TKeyBinding;
49 BScienceStat: TKeyBinding;
50 BEUnitStat: TKeyBinding;
51 BDiagram: TKeyBinding;
52 BWonders: TKeyBinding;
53 BShips: TKeyBinding;
54 BNations: TKeyBinding;
55 BEmpire: TKeyBinding;
56 BFullScreen: TKeyBinding;
57 BResign: TKeyBinding;
58 BRandomMap: TKeyBinding;
59 BFillMap: TKeyBinding;
60 BDisbandUnit: TKeyBinding;
61 BFortify: TKeyBinding;
62 BCenterUnit: TKeyBinding;
63 BStay: TKeyBinding;
64 BNoOrders: TKeyBinding;
65 BPrevUnit: TKeyBinding;
66 BNextUnit: TKeyBinding;
67 BCancel: TKeyBinding;
68 BPillage: TKeyBinding;
69 BSelectTransport: TKeyBinding;
70 BTechTree: TKeyBinding;
71 BWait: TKeyBinding;
72 BJump: TKeyBinding;
73 BMapBtn0: TKeyBinding;
74 BMapBtn1: TKeyBinding;
75 BMapBtn4: TKeyBinding;
76 BMapBtn5: TKeyBinding;
77 BMapBtn6: TKeyBinding;
78 BSetDebugMap0: TKeyBinding;
79 BSetDebugMap1: TKeyBinding;
80 BSetDebugMap2: TKeyBinding;
81 BSetDebugMap3: TKeyBinding;
82 BSetDebugMap4: TKeyBinding;
83 BSetDebugMap5: TKeyBinding;
84 BSetDebugMap6: TKeyBinding;
85 BSetDebugMap7: TKeyBinding;
86 BSetDebugMap8: TKeyBinding;
87 BSetDebugMap9: TKeyBinding;
88 BDebugMap: TKeyBinding;
89 BLocCodes: TKeyBinding;
90 BLogDlg: TKeyBinding;
91 BNames: TKeyBinding;
92 BRun: TKeyBinding;
93 BTestMapRepaint: TKeyBinding;
94 BSetViewpoint0: TKeyBinding;
95 BSetViewpoint1: TKeyBinding;
96 BSetViewpoint2: TKeyBinding;
97 BSetViewpoint3: TKeyBinding;
98 BSetViewpoint4: TKeyBinding;
99 BSetViewpoint5: TKeyBinding;
100 BSetViewpoint6: TKeyBinding;
101 BSetViewpoint7: TKeyBinding;
102 BSetViewpoint8: TKeyBinding;
103 BSetViewpoint9: TKeyBinding;
104 BAirBase: TKeyBinding;
105 BBuildCity: TKeyBinding;
106 BEnhance: TKeyBinding;
107 BGoOn: TKeyBinding;
108 BHome: TKeyBinding;
109 BFarmClearIrrigation: TKeyBinding;
110 BLoad: TKeyBinding;
111 BAfforestMine: TKeyBinding;
112 BCanal: TKeyBinding;
113 BTrans: TKeyBinding;
114 BPollution: TKeyBinding;
115 BRailRoad: TKeyBinding;
116 BUnload: TKeyBinding;
117 BRecover: TKeyBinding;
118 BUtilize: TKeyBinding;
119 BUnitMoveLeftDown: TKeyBinding;
120 BUnitMoveDown: TKeyBinding;
121 BUnitMoveRightDown: TKeyBinding;
122 BUnitMoveRight: TKeyBinding;
123 BUnitMoveRightUp: TKeyBinding;
124 BUnitMoveUp: TKeyBinding;
125 BUnitMoveLeftUp: TKeyBinding;
126 BUnitMoveLeft: TKeyBinding;
127 BMapMoveLeftDown: TKeyBinding;
128 BMapMoveDown: TKeyBinding;
129 BMapMoveRightDown: TKeyBinding;
130 BMapMoveRight: TKeyBinding;
131 BMapMoveRightUp: TKeyBinding;
132 BMapMoveUp: TKeyBinding;
133 BMapMoveLeftUp: TKeyBinding;
134 BMapMoveLeft: TKeyBinding;
135
136
137implementation
138
139{ TKeyBinding }
140
141function TKeyBinding.Test(AShortCut: TShortCut): Boolean;
142begin
143 Result := (AShortCut = ShortCut) or (AShortCut = ShortCut2);
144end;
145
146procedure TKeyBinding.Assign(Source: TKeyBinding);
147begin
148 ShortName := Source.ShortName;
149 FullName := Source.FullName;
150 ShortCut := Source.ShortCut;
151 ShortCut2 := Source.ShortCut2;
152 DefaultShortCut := Source.DefaultShortCut;
153 DefaultShortCut2 := Source.DefaultShortCut2;
154end;
155
156procedure TKeyBinding.SetDefault;
157begin
158 ShortCut := DefaultShortCut;
159 ShortCut2 := DefaultShortCut2;
160end;
161
162{ TKeyBindings }
163
164function TKeyBindings.AddItem(const ShortName, FullName: string; ShortCut: TShortCut;
165 ShortCut2: TShortCut = 0): TKeyBinding;
166begin
167 Result := TKeyBinding.Create;
168 Result.ShortName := ShortName;
169 Result.FullName := FullName;
170 Result.ShortCut := ShortCut;
171 Result.ShortCut2 := ShortCut2;
172 Result.DefaultShortCut := ShortCut;
173 Result.DefaultShortCut2 := ShortCut2;
174 Add(Result);
175end;
176
177function TKeyBindings.AddItem(const ShortName, FullName: string;
178 ShortCutText: string; ShortCutText2: string = ''): TKeyBinding;
179begin
180 Result := AddItem(ShortName, FullName, TextToShortCut(ShortCutText),
181 TextToShortCut(ShortCutText2));
182end;
183
184function TKeyBindings.Search(ShortName: string): TKeyBinding;
185var
186 I: Integer;
187begin
188 I := 0;
189 while (I < Count) and (Items[I].ShortName <> ShortName) do Inc(I);
190 if I < Count then Result := Items[I]
191 else Result := nil;
192end;
193
194procedure TKeyBindings.LoadFromRegistry(RootKey: HKEY; Key: string);
195var
196 I: Integer;
197 Registry: TRegistry;
198 Text: string;
199begin
200 Registry := TRegistry.Create;
201 Registry.RootKey := RootKey;
202 with Registry do
203 try
204 OpenKey(Key, True);
205 for I := 0 to Count - 1 do begin
206 Text := '';
207 if ValueExists(Items[I].ShortName) then begin
208 Text := ReadString(Items[I].ShortName);
209 if Pos(',', Text) > 0 then begin
210 Items[I].ShortCut2 := TextToShortCut(Copy(Text, Pos(',', Text) + 1, MaxInt));
211 Items[I].ShortCut := TextToShortCut(Copy(Text, 1, Pos(',', Text) - 1));
212 end else begin
213 Items[I].ShortCut := TextToShortCut(Text);
214 Items[I].ShortCut2 := 0;
215 end;
216 end else begin
217 Text := ShortCutToText(Items[I].ShortCut);
218 if Items[I].ShortCut2 <> 0 then Text := Text + ',' + ShortCutToText(Items[I].ShortCut2);
219 WriteString(Items[I].ShortName, Text);
220 end;
221 end;
222 finally
223 Free;
224 end;
225end;
226
227procedure TKeyBindings.SaveToRegistry(RootKey: HKEY; Key: string);
228var
229 I: Integer;
230 Registry: TRegistry;
231 Text: string;
232begin
233 Registry := TRegistry.Create;
234 Registry.RootKey := RootKey;
235 with Registry do
236 try
237 OpenKey(Key, True);
238 for I := 0 to Count - 1 do begin
239 Text := ShortCutToText(Items[I].ShortCut);
240 if Items[I].ShortCut2 <> 0 then Text := Text + ',' + ShortCutToText(Items[I].ShortCut2);
241 WriteString(Items[I].ShortName, Text);
242 end;
243 finally
244 Free;
245 end;
246end;
247
248procedure TKeyBindings.LoadToStrings(Strings: TStrings);
249var
250 I: Integer;
251 Text: string;
252begin
253 Strings.BeginUpdate;
254 try
255 Strings.Clear;
256 for I := 0 to Count - 1 do begin
257 Text := '';
258 if Items[I].ShortCut <> 0 then
259 Text := Text + ShortCutToText(Items[I].ShortCut);
260 if Items[I].ShortCut2 <> 0 then begin
261 if Text <> '' then Text := Text + ', ';
262 Text := Text + ShortCutToText(Items[I].ShortCut2);
263 end;
264 if Text <> '' then Text := Items[I].FullName + ' (' + Text + ')'
265 else Text := Items[I].FullName;
266 Strings.Add(Text);
267 end;
268 finally
269 Strings.EndUpdate;
270 end;
271end;
272
273procedure TKeyBindings.Assign(Source: TKeyBindings);
274var
275 I: Integer;
276begin
277 while Count < Source.Count do
278 Add(TKeyBinding.Create);
279 while Count > Source.Count do
280 Delete(Count - 1);
281 for I := 0 to Count - 1 do
282 Items[I].Assign(Source.Items[I]);
283end;
284
285procedure TKeyBindings.ResetToDefault;
286var
287 I: Integer;
288begin
289 for I := 0 to Count - 1 do
290 Items[I].SetDefault;
291end;
292
293procedure TKeyBindings.RemoveShortCut(ShortCut: TShortCut);
294var
295 I: Integer;
296begin
297 for I := 0 to Count - 1 do begin
298 if Items[I].ShortCut = ShortCut then Items[I].ShortCut := 0;
299 if Items[I].ShortCut2 = ShortCut then Items[I].ShortCut2 := 0;
300 end;
301end;
302
303function CompareAlpha(constref Item1, Item2: TKeyBinding): Integer;
304begin
305 Result := CompareStr(Item1.FullName, Item2.FullName);
306end;
307
308procedure TKeyBindings.SortAlpha;
309begin
310 Sort(TComparer<TKeyBinding>.Construct(CompareAlpha));
311end;
312
313
314initialization
315
316KeyBindings := TKeyBindings.Create;
317with KeyBindings do begin
318 BEndTurn := AddItem('EndTurn', 'End turn', 'NumPlus');
319 BHelp := AddItem('Help', 'Help', 'F1');
320 BUnitStat := AddItem('UnitStat', 'Unit stat', 'F2');
321 BCityStat := AddItem('CityStat', 'City stat', 'F3');
322 BScienceStat := AddItem('ScienceStat', 'Science research', 'F4');
323 BEUnitStat := AddItem('EnemyUnitStat', 'Enemy unit stat', 'F5');
324 BDiagram := AddItem('Diagram', 'Charts', 'F6');
325 BWonders := AddItem('Wonders', 'World wonders', 'F7');
326 BShips := AddItem('Ships', 'Colonization ships', 'F8');
327 BNations := AddItem('Nations', 'Nations', 'F9');
328 BEmpire := AddItem('Empire', 'Empire', 'F10');
329 BFullScreen := AddItem('FullScreen', 'Full screen', 'F11');
330 BResign := AddItem('Resign', 'Resign', 'Ctrl+Q');
331 BRandomMap := AddItem('RandomMap', 'Random map', 'Ctrl+R');
332 BFillMap := AddItem('FillMap', 'Fill map', 'Ctrl+F');
333 BDisbandUnit := AddItem('DisbandUnit', 'Disband unit', 'Ctrl+D');
334 BFortify := AddItem('Fortify', 'Fortify', 'F');
335 BCenterUnit := AddItem('CenterUnit', 'Center', 'C');
336 BStay := AddItem('Stay', 'Stay', 'S');
337 BNoOrders := AddItem('NoOrders', 'No orders', 'Space');
338 BPrevUnit := AddItem('PrevUnit', 'Previous unit', 'Del');
339 BNextUnit := AddItem('NextUnit', 'Next unit', 'Ins');
340 BCancel := AddItem('Cancel', 'Cancel', 'Ctrl+C');
341 BPillage := AddItem('Pillage', 'Pillage', 'Ctrl+P');
342 BSelectTransport := AddItem('SelectTransport', 'Select transport', 'Ctrl+T');
343 BTechTree := AddItem('TechTree', 'Tech tree', 'T');
344 BWait := AddItem('Wait', 'Wait', 'W');
345 BJump := AddItem('Jump', 'Jump 20 turns', 'Ctrl+J');
346 BMapBtn0 := AddItem('MapButton1', 'Map button 1', '1');
347 BMapBtn1 := AddItem('MapButton2', 'Map button 2', '2');
348 BMapBtn4 := AddItem('MapButton3', 'Map button 3', '3');
349 BMapBtn5 := AddItem('MapButton4', 'Map button 4', '4');
350 BMapBtn6 := AddItem('MapButton5', 'Map button 5', '5');
351 BSetDebugMap0 := AddItem('SetDebugMap0', 'Set debug map 0', '0');
352 BSetDebugMap1 := AddItem('SetDebugMap1', 'Set debug map 1', '1');
353 BSetDebugMap2 := AddItem('SetDebugMap2', 'Set debug map 2', '2');
354 BSetDebugMap3 := AddItem('SetDebugMap3', 'Set debug map 3', '3');
355 BSetDebugMap4 := AddItem('SetDebugMap4', 'Set debug map 4', '4');
356 BSetDebugMap5 := AddItem('SetDebugMap5', 'Set debug map 5', '5');
357 BSetDebugMap6 := AddItem('SetDebugMap6', 'Set debug map 6', '6');
358 BSetDebugMap7 := AddItem('SetDebugMap7', 'Set debug map 7', '7');
359 BSetDebugMap8 := AddItem('SetDebugMap8', 'Set debug map 8', '8');
360 BSetDebugMap9 := AddItem('SetDebugMap9', 'Set debug map 9', '9');
361 BDebugMap := AddItem('DebugMap', 'Debug map', 'Ctrl+K');
362 BLocCodes := AddItem('LocCodes', 'Location codes', 'Ctrl+L');
363 BLogDlg := AddItem('LogDlg', 'Log dialog', 'Ctrl+M');
364 BNames := AddItem('ShowNames', 'Show names', 'Ctrl+N');
365 BRun := AddItem('Run', 'Run', 'Ctrl+R');
366 BTestMapRepaint := AddItem('TestMapRepaint', 'Test map reapaint', 'Ctrl+Space');
367 BSetViewpoint0 := AddItem('SetViewpoint0', 'Set viewpoint 0', 'Ctrl+0');
368 BSetViewpoint1 := AddItem('SetViewpoint1', 'Set viewpoint 1', 'Ctrl+1');
369 BSetViewpoint2 := AddItem('SetViewpoint2', 'Set viewpoint 2', 'Ctrl+2');
370 BSetViewpoint3 := AddItem('SetViewpoint3', 'Set viewpoint 3', 'Ctrl+3');
371 BSetViewpoint4 := AddItem('SetViewpoint4', 'Set viewpoint', 'Ctrl+4');
372 BSetViewpoint5 := AddItem('SetViewpoint0', 'Set viewpoint', 'Ctrl+5');
373 BSetViewpoint6 := AddItem('SetViewpoint1', 'Set viewpoint', 'Ctrl+6');
374 BSetViewpoint7 := AddItem('SetViewpoint2', 'Set viewpoint', 'Ctrl+7');
375 BSetViewpoint8 := AddItem('SetViewpoint3', 'Set viewpoint', 'Ctrl+8');
376 BSetViewpoint9 := AddItem('SetViewpoint4', 'Set viewpoint', 'Ctrl+9');
377 BAirBase := AddItem('AirBase', 'Air base', 'A');
378 BBuildCity := AddItem('BuildCity', 'Build city', 'B');
379 BEnhance := AddItem('Enhance', 'Enhance', 'E');
380 BGoOn := AddItem('GoOn', 'Go on', 'G');
381 BHome := AddItem('Home', 'Home', 'H');
382 BFarmClearIrrigation := AddItem('FarmClearIrrigation', 'Farm/Clear/Irrigation', 'I');
383 BLoad := AddItem('Load', 'Load', 'L');
384 BAfforestMine := AddItem('AfforestMine', 'Afforest/Mine', 'M');
385 BCanal := AddItem('Canal', 'Canal', 'N');
386 BTrans := AddItem('Trans', 'Trans', 'O');
387 BPollution := AddItem('Pollution', 'Pollution', 'P');
388 BRailRoad := AddItem('RailRoad', 'Rails/Road', 'R');
389 BUnload := AddItem('Unload', 'Unload', 'U');
390 BRecover := AddItem('Recover', 'Recover', 'V');
391 BUtilize := AddItem('Utilize', 'Utilize', 'Z');
392 // Unit moves
393 BUnitMoveLeftDown := AddItem('MoveLeftDown', 'Move unit left-down', 'Num1', 'End');
394 BUnitMoveDown := AddItem('MoveDown', 'Move unit down', 'Num2', 'Down');
395 BUnitMoveRightDown := AddItem('MoveRightDown', 'Move unit right-down', 'Num3', 'PgDn');
396 BUnitMoveRight := AddItem('MoveRight', 'Move unit right', 'Num6', 'Right');
397 BUnitMoveRightUp := AddItem('MoveRightUp', 'Move unit right-up', 'Num9', 'PgUp');
398 BUnitMoveUp := AddItem('MoveUp', 'Move unit up', 'Num8', 'Up');
399 BUnitMoveLeftUp := AddItem('MoveLeftUp', 'Move unit left-up', 'Num7', 'Home');
400 BUnitMoveLeft := AddItem('MoveLeft', 'Move unit left', 'Num4', 'Left');
401 // Map moves
402 BMapMoveLeftDown := AddItem('MapMoveLeftDown', 'Move map left-down', 'Ctrl+Num1', 'Ctrl+End');
403 BMapMoveDown := AddItem('MapMoveDown', 'Move map down', 'Ctrl+Num2', 'Ctrl+Down');
404 BMapMoveRightDown := AddItem('MapMoveRightDown', 'Move map right-down', 'Ctrl+Num3', 'Ctrl+PgDn');
405 BMapMoveRight := AddItem('MapMoveRight', 'Move map right', 'Ctrl+Num6', 'Ctrl+Right');
406 BMapMoveRightUp := AddItem('MapMoveRightUp', 'Move map right-up', 'Ctrl+Num9', 'Ctrl+PgUp');
407 BMapMoveUp := AddItem('MapMoveUp', 'Move map up', 'Ctrl+Num8', 'Ctrl+Up');
408 BMapMoveLeftUp := AddItem('MapMoveLeftUp', 'Move map left-up', 'Ctrl+Num7', 'Ctrl+Home');
409 BMapMoveLeft := AddItem('MapMoveLeft', 'Move map left', 'Ctrl+Num4', 'Ctrl+Left');
410
411 SortAlpha;
412end;
413
414
415finalization
416
417FreeAndNil(KeyBindings);
418
419end.
420
Note: See TracBrowser for help on using the repository browser.