| 1 |
{ |
| 2 |
Copyright 2003-2022 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Key and mouse shortcuts (TInputShortcut) and global management of them. |
| 17 |
|
| 18 |
See https://castle-engine.io/manual_key_mouse.php for overview. |
| 19 |
|
| 20 |
TInputShortcut instance represents a key/mouse shortcut. |
| 21 |
Instances of this class are spread throughout the engine. |
| 22 |
We have two different ways of using TInputShortcut instance: |
| 23 |
|
| 24 |
@unorderedList( |
| 25 |
@item( |
| 26 |
@bold(Global) |
| 27 |
|
| 28 |
TInputShortcut instance with TInputShortcut.Group <> igLocal |
| 29 |
is called "global". Such instance is automatically (at construction) |
| 30 |
added to InputsAll and InputsGroup(Group) lists. |
| 31 |
|
| 32 |
The purpose of such global input map is to be able to detect key conflicts, |
| 33 |
be able to restore whole input map to default, |
| 34 |
load/save them to the user config file, and so on. |
| 35 |
For example @link(CastleViewport.Input_Interact) is global. |
| 36 |
|
| 37 |
Global shortcuts are owned (they will be freed by) this unit |
| 38 |
(more specifically, they will be freed by InputsAll). |
| 39 |
When creating them, pass @nil to the Owner parameter |
| 40 |
of constructor TInputShortcut.Create. |
| 41 |
This implies that InputsAll and InputsGroup(Group) lists will never shrink, |
| 42 |
which is useful --- once added, shortcuts will not disappear. |
| 43 |
Global TInputShortcut instances are always in practice also global variables. |
| 44 |
) |
| 45 |
|
| 46 |
@item( |
| 47 |
@bold(Local) |
| 48 |
|
| 49 |
TInputShortcut instance with TInputShortcut.Group = igLocal |
| 50 |
is called "local". It means it's a normal component, |
| 51 |
it's not automatically present on any global list, |
| 52 |
so it doesn't conflict with other global shortcuts. |
| 53 |
|
| 54 |
For example TCastleWalkNavigation contains a number of them like |
| 55 |
TCastleWalkNavigation.Input_Forward. |
| 56 |
) |
| 57 |
) |
| 58 |
|
| 59 |
You create new inputs by simply constructing new TInputShortcut instances. |
| 60 |
Make sure you add all global inputs before calling @code(Config.Load), |
| 61 |
as some functionality assumes that all shortcuts are already added |
| 62 |
at the time @code(Config.Load) is called. |
| 63 |
The engine units themselves never call @code(Config.Load), it is left |
| 64 |
to the final application. } |
| 65 |
unit CastleInputs; |
| 66 |
|
| 67 |
{$I castleconf.inc} |
| 68 |
|
| 69 |
interface |
| 70 |
|
| 71 |
uses Classes, Generics.Collections, |
| 72 |
CastleKeysMouse, CastleUtils, CastleClassUtils, |
| 73 |
CastleXMLConfig {$ifndef CASTLE_STRICT_CLI} , CastleUIControls {$endif}; |
| 74 |
|
| 75 |
type |
| 76 |
{ Type of input, for TInputShortcut.Group. } |
| 77 |
TInputGroup = (igLocal, igBasic, igItems, igOther); |
| 78 |
TInputGroupNotLocal = igBasic..High(TInputGroup); |
| 79 |
|
| 80 |
{ A keyboard and/or mouse shortcut for activating some action. |
| 81 |
|
| 82 |
Action may be activated by: |
| 83 |
@unorderedList( |
| 84 |
@itemSpacing compact |
| 85 |
@item at most two key shortcuts, |
| 86 |
@item and one mouse button shortcut, |
| 87 |
@item and one character shortcut, |
| 88 |
@item and one mouse wheel shortcut. |
| 89 |
) |
| 90 |
|
| 91 |
The difference between @italic(key shortcut) and @italic(character shortcut): |
| 92 |
@italic("Key") is something that can be expressed as TKey value. |
| 93 |
@italic("KeyString") is an UTF-8 character that can be expressed as String value. |
| 94 |
|
| 95 |
They are keys like "control key" (keyCtrl) or "shift key" (keyShift) |
| 96 |
that cannot be expressed (on their own) as KeyString. |
| 97 |
KeyString is sometimes more, sometimes less specific than Key: |
| 98 |
character "A" (upper letter "a") |
| 99 |
is activated by pressing "a", but only when Shift is pressed or CapsLock is on |
| 100 |
(window system / GUI toolkit may also allow other ways to input characters). |
| 101 |
|
| 102 |
Name of this component is additionally used if this is a global |
| 103 |
input shortcut (with Group <> igLocal, see CastleInputs unit documentation): |
| 104 |
it is used for config file entries and for the CastleScript |
| 105 |
@code(shortcut()) function |
| 106 |
[https://castle-engine.io/castle_script.php#function_shortcut] . |
| 107 |
Any valid Pascal identifier will be Ok for these purposes. } |
| 108 |
TInputShortcut = class(TComponent) |
| 109 |
private |
| 110 |
FKey1: TKey; |
| 111 |
FKey2: TKey; |
| 112 |
FKeyString: String; |
| 113 |
FMouseButtonUse: boolean; |
| 114 |
FMouseButton: TCastleMouseButton; |
| 115 |
FMouseButtonCheckModifiers: TModifierKeys; |
| 116 |
FMouseButtonModifiers: TModifierKeys; |
| 117 |
FMouseButton2Use: boolean; |
| 118 |
FMouseButton2: TCastleMouseButton; |
| 119 |
FMouseButton2CheckModifiers: TModifierKeys; |
| 120 |
FMouseButton2Modifiers: TModifierKeys; |
| 121 |
FMouseWheel: TMouseWheelDirection; |
| 122 |
|
| 123 |
FDefaultKey1: TKey; |
| 124 |
FDefaultKey2: TKey; |
| 125 |
FDefaultKeyString: String; |
| 126 |
FDefaultMouseButtonUse: boolean; |
| 127 |
FDefaultMouseButton: TCastleMouseButton; |
| 128 |
FDefaultMouseButtonCheckModifiers: TModifierKeys; |
| 129 |
FDefaultMouseButtonModifiers: TModifierKeys; |
| 130 |
FDefaultMouseButton2Use: boolean; |
| 131 |
FDefaultMouseButton2: TCastleMouseButton; |
| 132 |
FDefaultMouseButton2CheckModifiers: TModifierKeys; |
| 133 |
FDefaultMouseButton2Modifiers: TModifierKeys; |
| 134 |
FDefaultMouseWheel: TMouseWheelDirection; |
| 135 |
|
| 136 |
FCaption: string; |
| 137 |
FGroup: TInputGroup; |
| 138 |
FGroupOrder: Integer; |
| 139 |
{ Index of InputsAll. For now this is useful only for sorting, |
| 140 |
to decide order when GroupOrder is equal between two items. } |
| 141 |
Index: Integer; |
| 142 |
|
| 143 |
procedure SetKey1(const Value: TKey); |
| 144 |
procedure SetKey2(const Value: TKey); |
| 145 |
procedure SetKeyString(const Value: String); |
| 146 |
function GetCharacter: Char; |
| 147 |
procedure SetCharacter(const AValue: Char); |
| 148 |
procedure SetMouseButtonUse(const Value: boolean); |
| 149 |
procedure SetMouseButton(const Value: TCastleMouseButton); |
| 150 |
procedure SetMouseButtonCheckModifiers(const Value: TModifierKeys); |
| 151 |
procedure SetMouseButtonModifiers(const Value: TModifierKeys); |
| 152 |
procedure SetMouseButton2Use(const Value: boolean); |
| 153 |
procedure SetMouseButton2(const Value: TCastleMouseButton); |
| 154 |
procedure SetMouseButton2CheckModifiers(const Value: TModifierKeys); |
| 155 |
procedure SetMouseButton2Modifiers(const Value: TModifierKeys); |
| 156 |
procedure SetMouseWheel(const Value: TMouseWheelDirection); |
| 157 |
protected |
| 158 |
{ Called always right after the shortcut value changed (like key or mouse buton). |
| 159 |
Called only when the current values changed, |
| 160 |
not called when just the default values changed. } |
| 161 |
procedure Changed; virtual; |
| 162 |
public |
| 163 |
{ Constructor that always creates local shortcuts (with Group = igLocal). |
| 164 |
Caption and Name are left empty (they do not have to be set for |
| 165 |
local shortcuts; although you may wish to later assign Name anyway, |
| 166 |
if you use this as sub-component in Lazarus). } |
| 167 |
constructor Create(AOwner: TComponent); overload; override; |
| 168 |
|
| 169 |
{ Flexible constructor that allows to set Group and choose global or local |
| 170 |
shortcut. } |
| 171 |
constructor Create(const AOwner: TComponent; |
| 172 |
const ACaption: string; |
| 173 |
const AName: string; |
| 174 |
const AGroup: TInputGroup); reintroduce; overload; |
| 175 |
|
| 176 |
procedure MakeDefault; |
| 177 |
|
| 178 |
{ Assigns to this object the default values from Source. } |
| 179 |
procedure AssignFromDefault(Source: TInputShortcut); |
| 180 |
|
| 181 |
{ Copy Source properties to this object. |
| 182 |
It always copies current properties (Key1, MouseButton etc.), |
| 183 |
and only optionally (if CopyDefaults) also copies the DefaultXxx properties. } |
| 184 |
procedure Assign(Source: TInputShortcut; CopyDefaults: boolean); reintroduce; overload; |
| 185 |
|
| 186 |
{ Set keys/mouse buttons of this shortcut. |
| 187 |
|
| 188 |
Sets both current and default properties (e.g. both @link(Key1) and |
| 189 |
@link(DefaultKey1) to the same value). |
| 190 |
Note that, right after using this method, saving the input to a config file |
| 191 |
(using TInputShortcutList.SaveToConfig) will actually just "clear" |
| 192 |
the input information from the config file (because we do not save |
| 193 |
the value when it is equal to the default). |
| 194 |
Use @link(AssignCurrent) to only assign the curent value, leaving default |
| 195 |
untouched. } |
| 196 |
procedure Assign( |
| 197 |
const AKey1: TKey; |
| 198 |
const AKey2: TKey = keyNone; |
| 199 |
AKeyString: String = ''; |
| 200 |
const AMouseButtonUse: boolean = false; |
| 201 |
const AMouseButton: TCastleMouseButton = buttonLeft; |
| 202 |
const AMouseWheel: TMouseWheelDirection = mwNone); reintroduce; overload; |
| 203 |
|
| 204 |
{ Set keys/mouse buttons of this shortcut. |
| 205 |
Sets only the "current" properties (e.g. it changes @link(Key1), |
| 206 |
leaving @link(DefaultKey1) unmodified). } |
| 207 |
procedure AssignCurrent( |
| 208 |
const AKey1: TKey; |
| 209 |
const AKey2: TKey = keyNone; |
| 210 |
AKeyString: String = ''; |
| 211 |
const AMouseButtonUse: boolean = false; |
| 212 |
const AMouseButton: TCastleMouseButton = buttonLeft; |
| 213 |
const AMouseWheel: TMouseWheelDirection = mwNone); |
| 214 |
|
| 215 |
{ Make this input impossible to activate by the user. |
| 216 |
This sets both keys to keyNone, KeyString to '', |
| 217 |
both MouseButtonUse and MouseButton2Use to @false, |
| 218 |
and MouseWheel to mwNone. } |
| 219 |
procedure MakeClear(const ClearAlsoDefaultState: boolean = false); |
| 220 |
|
| 221 |
{ Given a set of currently pressed keys and mouse buttons, |
| 222 |
decide whether this input is currently pressed. } |
| 223 |
function IsPressed(const Pressed: TKeysPressed; |
| 224 |
const MousePressed: TCastleMouseButtons): boolean; overload; |
| 225 |
|
| 226 |
{$ifndef CASTLE_STRICT_CLI} |
| 227 |
{ Looking at Container's currently pressed keys and mouse buttons, |
| 228 |
decide whether this input is currently pressed. } |
| 229 |
function IsPressed(const Container: TCastleContainer): boolean; overload; |
| 230 |
{$endif} |
| 231 |
|
| 232 |
{ Check does given Key or AKeyString correspond to this input shortcut. |
| 233 |
If Key = keyNone and AString = '', result is always @false. } |
| 234 |
function IsKey(const Key: TKey; AKeyString: String): boolean; |
| 235 |
|
| 236 |
{ Check does given mouse button correspond to this input shortcut. } |
| 237 |
function IsMouseButton(const AMouseButton: TCastleMouseButton; |
| 238 |
const ModifiersDown: TModifierKeys): boolean; overload; |
| 239 |
|
| 240 |
function IsMouseButton(const AMouseButton: TCastleMouseButton): boolean; overload; |
| 241 |
deprecated 'use overloaded version with additional ModifiersDown parameter'; |
| 242 |
|
| 243 |
function IsMouseWheel(const AMouseWheel: TMouseWheelDirection): boolean; |
| 244 |
|
| 245 |
{ Check does given event (key press, mouse button press, mouse wheel) |
| 246 |
activates this shortcut. } |
| 247 |
function IsEvent(const Event: TInputPressRelease): boolean; overload; |
| 248 |
|
| 249 |
function IsEvent(const AKey: TKey; AKeyString: String; |
| 250 |
const AMousePress: boolean; const AMouseButton: TCastleMouseButton; |
| 251 |
const AMouseWheel: TMouseWheelDirection; |
| 252 |
const ModifiersDown: TModifierKeys = []): boolean; overload; |
| 253 |
deprecated 'use IsEvent(TInputPressRelease)'; |
| 254 |
|
| 255 |
{ Describe the current value (which key, mouse buttons and such) of this |
| 256 |
shortcut. If there is no way to press this shortcut (all properties |
| 257 |
like Key1 and such are empty, like after MakeClear), we will use NoneString. |
| 258 |
|
| 259 |
The overloaded version without NoneString parameter will |
| 260 |
assume that NoneString should describe the shortcut @link(Caption). |
| 261 |
This way, if user cleared (deleted all key/mouse buttons assigned) |
| 262 |
the shortcut in the game configuration, and we show him a message like: |
| 263 |
|
| 264 |
@longCode(# 'Press ' + Input.Description + ' to do something' #) |
| 265 |
|
| 266 |
then user will see it as @code('Press "use" key to do something') |
| 267 |
and will know that (s)he should configure the "use" key. |
| 268 |
@groupBegin } |
| 269 |
function Description(const NoneString: string): string; overload; |
| 270 |
function Description: string; overload; |
| 271 |
{ @groupEnd } |
| 272 |
|
| 273 |
{ Modifier keys that are relevant to recognize this shortcut. } |
| 274 |
function Modifiers: TModifierKeys; |
| 275 |
|
| 276 |
{ Nice name to show user. With spaces, localized characters etc. } |
| 277 |
property Caption: string read FCaption; |
| 278 |
|
| 279 |
{ Group of the global shortcut, or igLocal indicating a local shortcut. |
| 280 |
Games may use this group to better show the keys configuration for user, |
| 281 |
presenting together keys from the same group. } |
| 282 |
property Group: TInputGroup read FGroup; |
| 283 |
|
| 284 |
{ Order of the shortcut within it's @link(Group). |
| 285 |
The order may be important, as menus may show InputsGroup |
| 286 |
to user in this order. |
| 287 |
This order is applied (the group is actually sorted by GroupOrder) |
| 288 |
when reading config file. |
| 289 |
For equal GroupOrder, the order of creation (equal to the order |
| 290 |
on InputsAll list) decides which is first. } |
| 291 |
property GroupOrder: Integer read FGroupOrder write FGroupOrder; |
| 292 |
|
| 293 |
{ Add to shortcut new key or mouse button or mouse wheel. } |
| 294 |
procedure Add(const NewEvent: TInputPressRelease); |
| 295 |
|
| 296 |
{ Load a particular input from a config file. |
| 297 |
Use this to load what was previously saved with @link(SaveToConfig). } |
| 298 |
procedure LoadFromConfig(const Config: TCastleConfig; ConfigPath: String); |
| 299 |
|
| 300 |
{ Save a particular input to a config file. |
| 301 |
This creates an XML element named @code(Name) under the indicated ConfigPath |
| 302 |
in the config file. |
| 303 |
|
| 304 |
Note: It is often easier to group your controls in TInputShortcutList, |
| 305 |
and call @link(TInputShortcutList.SaveToConfig) to save everything. } |
| 306 |
procedure SaveToConfig(const Config: TCastleConfig; ConfigPath: String); |
| 307 |
published |
| 308 |
{ Key/mouse properties on TInputShortcut are declared without |
| 309 |
"default" specifier, to always save them in Lazarus LFM file. |
| 310 |
Reason: various class (TCastleExamineNavigation, TCastleWalkNavigation, TCastleViewport) |
| 311 |
create them setting different default values. |
| 312 |
If we would declare that default for Key1 is keyNone, |
| 313 |
then you couldn't set in Lazarus e.g. TCastleWalkNavigation.Input_Forward.Key1 to keyNone. |
| 314 |
Such keyNone would not be saved to LFM (since it would equal Key1 default |
| 315 |
value), but when reading the LFM back it would change into keyArrowUp |
| 316 |
(because TCastleWalkNavigation creates Input_Forward with keyArrowUp by default). } |
| 317 |
|
| 318 |
{ Key shortcuts for given command. You can set any of them to keyNone |
| 319 |
to indicate that no key is assigned. |
| 320 |
@groupBegin } |
| 321 |
property Key1: TKey read FKey1 write SetKey1; |
| 322 |
property Key2: TKey read FKey2 write SetKey2; |
| 323 |
{ @groupEnd } |
| 324 |
|
| 325 |
{ Character shortcut for given command, may be UTF-8 character (multi-byte). |
| 326 |
You can set this to '' to indicate that no character shortcut is assigned. } |
| 327 |
property KeyString: String read FKeyString write SetKeyString; |
| 328 |
|
| 329 |
{$ifdef FPC} |
| 330 |
property Character: Char read GetCharacter write SetCharacter; |
| 331 |
deprecated 'use KeyString'; |
| 332 |
{$endif} |
| 333 |
|
| 334 |
{ Mouse shortcut for given command. |
| 335 |
Only relevant if MouseButtonUse is @true. |
| 336 |
Then we check for MouseButton being pressed. |
| 337 |
|
| 338 |
MouseButtonCheckModifiers determines what subset of modifiers (Ctrl, Shift, Alt) |
| 339 |
to check, whether they match MouseButtonModifiers. For example, |
| 340 |
MouseButtonCheckModifiers = [mkShift, mkCtrl] |
| 341 |
and MouseButtonModifiers = [mkCtrl] means that you have to press Ctrl, |
| 342 |
and you cannot keep Shift pressed, to activate this shortcut. |
| 343 |
|
| 344 |
By default MouseButtonCheckModifiers and MouseButtonModifiers are both empty sets, |
| 345 |
which means that any state of modifiers is OK. |
| 346 |
|
| 347 |
@groupBegin } |
| 348 |
property MouseButtonUse: boolean read FMouseButtonUse write SetMouseButtonUse; |
| 349 |
property MouseButton: TCastleMouseButton read FMouseButton write SetMouseButton; |
| 350 |
property MouseButtonCheckModifiers: TModifierKeys read FMouseButtonCheckModifiers write SetMouseButtonCheckModifiers; |
| 351 |
property MouseButtonModifiers: TModifierKeys read FMouseButtonModifiers write SetMouseButtonModifiers; |
| 352 |
{ @groupEnd } |
| 353 |
|
| 354 |
{ Alternative mouse shortcut for given command. |
| 355 |
Only relevant if MouseButton2Use is @true. |
| 356 |
All properties work analogously to MouseButtonUse, MouseButton, |
| 357 |
MouseButtonCheckModifiers, MouseButtonModifiers. |
| 358 |
@groupBegin } |
| 359 |
property MouseButton2Use: boolean read FMouseButton2Use write SetMouseButton2Use; |
| 360 |
property MouseButton2: TCastleMouseButton read FMouseButton2 write SetMouseButton2; |
| 361 |
property MouseButton2CheckModifiers: TModifierKeys read FMouseButton2CheckModifiers write SetMouseButton2CheckModifiers; |
| 362 |
property MouseButton2Modifiers: TModifierKeys read FMouseButton2Modifiers write SetMouseButton2Modifiers; |
| 363 |
{ @groupEnd } |
| 364 |
|
| 365 |
{ Mouse wheel to activate this command. Note that mouse wheels cannot be |
| 366 |
continuously pressed (our method IsPressed doesn't look at it), |
| 367 |
so this is only suitable for commands that work in steps |
| 368 |
(not continuously). } |
| 369 |
property MouseWheel: TMouseWheelDirection read FMouseWheel |
| 370 |
write SetMouseWheel; |
| 371 |
|
| 372 |
{ Default values for properties key/mouse. |
| 373 |
You can change them --- this will change what MakeDefault does. |
| 374 |
|
| 375 |
Note that setting these properties doesn't automatically set |
| 376 |
corresponding "current" property. E.g. @code(DefaultKey1 := keySpace;) |
| 377 |
doesn't change the value of Key1 property --- only DefaultKey1 |
| 378 |
changes. You can explicitly change Key1 property, or just call |
| 379 |
MakeDefault afterwards, if you want this to happen. |
| 380 |
@groupBegin } |
| 381 |
property DefaultKey1: TKey read FDefaultKey1 write FDefaultKey1; |
| 382 |
property DefaultKey2: TKey read FDefaultKey2 write FDefaultKey2; |
| 383 |
property DefaultKeyString: String |
| 384 |
read FDefaultKeyString write FDefaultKeyString; |
| 385 |
|
| 386 |
property DefaultMouseButtonUse: boolean |
| 387 |
read FDefaultMouseButtonUse write FDefaultMouseButtonUse; |
| 388 |
property DefaultMouseButton: TCastleMouseButton |
| 389 |
read FDefaultMouseButton write FDefaultMouseButton; |
| 390 |
property DefaultMouseButtonCheckModifiers: TModifierKeys |
| 391 |
read FDefaultMouseButtonCheckModifiers write FDefaultMouseButtonCheckModifiers; |
| 392 |
property DefaultMouseButtonModifiers: TModifierKeys |
| 393 |
read FDefaultMouseButtonModifiers write FDefaultMouseButtonModifiers; |
| 394 |
|
| 395 |
property DefaultMouseButton2Use: boolean |
| 396 |
read FDefaultMouseButton2Use write FDefaultMouseButton2Use; |
| 397 |
property DefaultMouseButton2: TCastleMouseButton |
| 398 |
read FDefaultMouseButton2 write FDefaultMouseButton2; |
| 399 |
property DefaultMouseButton2CheckModifiers: TModifierKeys |
| 400 |
read FDefaultMouseButton2CheckModifiers write FDefaultMouseButton2CheckModifiers; |
| 401 |
property DefaultMouseButton2Modifiers: TModifierKeys |
| 402 |
read FDefaultMouseButton2Modifiers write FDefaultMouseButton2Modifiers; |
| 403 |
|
| 404 |
property DefaultMouseWheel: TMouseWheelDirection |
| 405 |
read FDefaultMouseWheel write FDefaultMouseWheel; |
| 406 |
{ @groupEnd } |
| 407 |
|
| 408 |
{ } |
| 409 |
{ TODO: Maybe introduce a way to limit (TKey, or all shortcuts?) |
| 410 |
to activate only when specific modifier is pressed. |
| 411 |
|
| 412 |
Right now both TCastleWalkNavigation and TCastleExamineNavigation check modifiers |
| 413 |
and have not configurable behavior: |
| 414 |
|
| 415 |
- TCastleWalkNavigation allows inputs only when modifiers = []. |
| 416 |
Except Input_Right/LeftRot and Input_Up/DownRotate that have special |
| 417 |
meaning when Ctrl is pressed (see TCastleWalkNavigation.AllowSlowerRotations). |
| 418 |
- TCastleExamineNavigation allows Inputs_Move only when modifiers = [mkCtrl]. |
| 419 |
Other TCastleExamineNavigation are allowed only when modifiers = []. } |
| 420 |
end; |
| 421 |
|
| 422 |
{ Group of TInputShortcut, to easily manage (search, load, save...) |
| 423 |
the inputs. } |
| 424 |
TInputShortcutList = class({$ifdef FPC}specialize{$endif} TObjectList<TInputShortcut>) |
| 425 |
public |
| 426 |
{ Find shortcut by name, returns @nil if not found. } |
| 427 |
function FindName(const Name: string): TInputShortcut; |
| 428 |
|
| 429 |
{ Seeks for a shortcut that has matching key or mouse button or mouse wheel. |
| 430 |
@nil if not found. } |
| 431 |
function SeekMatchingShortcut(const Event: TInputPressRelease): TInputShortcut; |
| 432 |
procedure RestoreDefaults; |
| 433 |
function SeekConflict(out ConflictDescription: string): boolean; |
| 434 |
|
| 435 |
{ Load customized input shortcuts from a config file, |
| 436 |
for example from @link(UserConfig). |
| 437 |
|
| 438 |
This should be used to load inputs previously saved with @link(SaveToConfig). |
| 439 |
Provide the same value of ConfigPath as you used with @link(SaveToConfig). } |
| 440 |
procedure LoadFromConfig(const Config: TCastleConfig; ConfigPath: String = ''); |
| 441 |
|
| 442 |
{ Save customized input shortcuts to a config file, |
| 443 |
for example to a @link(UserConfig). |
| 444 |
|
| 445 |
This will create an XML element <input> under an indicated ConfigPath |
| 446 |
in the config file. Be sure to save each unique TInputShortcutList |
| 447 |
with a different ConfigPath, to not collide in the config file. |
| 448 |
|
| 449 |
Note that we only save to config file the values when they differ |
| 450 |
from default. E.g. TInputShortcut.Key1 will be explicitly saved |
| 451 |
only if it is different than TInputShortcut.DefaultKey1. |
| 452 |
Otherwise the information about this Key1 for this TInputShortcut |
| 453 |
will be removed from the config file (to force using default next time |
| 454 |
this config is loaded). } |
| 455 |
procedure SaveToConfig(const Config: TCastleConfig; ConfigPath: String = ''); |
| 456 |
end; |
| 457 |
|
| 458 |
{ List of all global inputs. |
| 459 |
All TInputShortcut instances with group other than igLocal |
| 460 |
will be automatically added here. } |
| 461 |
function InputsAll: TInputShortcutList; |
| 462 |
|
| 463 |
function InputsGroup(const Group: TInputGroupNotLocal): TInputShortcutList; |
| 464 |
|
| 465 |
implementation |
| 466 |
|
| 467 |
uses SysUtils, Generics.Defaults, |
| 468 |
CastleStringUtils; |
| 469 |
|
| 470 |
{ TInputShortcut ------------------------------------------------------------- } |
| 471 |
|
| 472 |
constructor TInputShortcut.Create(const AOwner: TComponent; |
| 473 |
const ACaption: string; |
| 474 |
const AName: string; |
| 475 |
const AGroup: TInputGroup); |
| 476 |
begin |
| 477 |
inherited Create(AOwner); |
| 478 |
FCaption := ACaption; |
| 479 |
Name := AName; |
| 480 |
FGroup := AGroup; |
| 481 |
|
| 482 |
if Group <> igLocal then |
| 483 |
begin |
| 484 |
Index := InputsAll.Count; |
| 485 |
InputsAll.Add(Self); |
| 486 |
InputsGroup(Group).Add(Self); |
| 487 |
end; |
| 488 |
end; |
| 489 |
|
| 490 |
constructor TInputShortcut.Create(AOwner: TComponent); |
| 491 |
begin |
| 492 |
Create(AOwner, '', '', igLocal); |
| 493 |
end; |
| 494 |
|
| 495 |
function TInputShortcut.GetCharacter: Char; |
| 496 |
begin |
| 497 |
if Length(KeyString) = 1 then |
| 498 |
Result := KeyString[1] |
| 499 |
else |
| 500 |
Result := #0; |
| 501 |
end; |
| 502 |
|
| 503 |
procedure TInputShortcut.SetCharacter(const AValue: Char); |
| 504 |
begin |
| 505 |
if AValue = #0 then |
| 506 |
KeyString := '' |
| 507 |
else |
| 508 |
KeyString := AValue; |
| 509 |
end; |
| 510 |
|
| 511 |
procedure TInputShortcut.MakeDefault; |
| 512 |
begin |
| 513 |
AssignFromDefault(Self); |
| 514 |
end; |
| 515 |
|
| 516 |
procedure TInputShortcut.AssignFromDefault(Source: TInputShortcut); |
| 517 |
begin |
| 518 |
FKey1 := Source.DefaultKey1; |
| 519 |
FKey2 := Source.DefaultKey2; |
| 520 |
FKeyString := Source.DefaultKeyString; |
| 521 |
FMouseButtonUse := Source.DefaultMouseButtonUse; |
| 522 |
FMouseButton := Source.DefaultMouseButton; |
| 523 |
FMouseButtonCheckModifiers := Source.DefaultMouseButtonCheckModifiers; |
| 524 |
FMouseButtonModifiers := Source.DefaultMouseButtonModifiers; |
| 525 |
FMouseButton2Use := Source.DefaultMouseButton2Use; |
| 526 |
FMouseButton2 := Source.DefaultMouseButton2; |
| 527 |
FMouseButton2CheckModifiers := Source.DefaultMouseButton2CheckModifiers; |
| 528 |
FMouseButton2Modifiers := Source.DefaultMouseButton2Modifiers; |
| 529 |
FMouseWheel := Source.DefaultMouseWheel; |
| 530 |
|
| 531 |
{ we don't set here properties, but directly set FXxx fields, |
| 532 |
so that we can call Changed only once. } |
| 533 |
Changed; |
| 534 |
end; |
| 535 |
|
| 536 |
procedure TInputShortcut.Assign(const AKey1: TKey; |
| 537 |
const AKey2: TKey; |
| 538 |
AKeyString: String; |
| 539 |
const AMouseButtonUse: boolean; |
| 540 |
const AMouseButton: TCastleMouseButton; |
| 541 |
const AMouseWheel: TMouseWheelDirection); |
| 542 |
begin |
| 543 |
// only for backward compatibility (when this parameter was Char) convert #0 to '' |
| 544 |
if AKeyString = #0 then |
| 545 |
AKeyString := ''; |
| 546 |
|
| 547 |
FDefaultKey1 := AKey1; |
| 548 |
FDefaultKey2 := AKey2; |
| 549 |
FDefaultKeyString := AKeyString; |
| 550 |
FDefaultMouseButtonUse := AMouseButtonUse; |
| 551 |
FDefaultMouseButton := AMouseButton; |
| 552 |
FDefaultMouseButtonCheckModifiers := []; // not set by parameters, just reset |
| 553 |
FDefaultMouseButtonModifiers := []; // not set by parameters, just reset |
| 554 |
FDefaultMouseButton2Use := false; // not set by parameters, just reset |
| 555 |
FDefaultMouseButton2 := buttonLeft; // not set by parameters, just reset |
| 556 |
FDefaultMouseButton2CheckModifiers := []; // not set by parameters, just reset |
| 557 |
FDefaultMouseButton2Modifiers := []; // not set by parameters, just reset |
| 558 |
FDefaultMouseWheel := AMouseWheel; |
| 559 |
MakeDefault; |
| 560 |
end; |
| 561 |
|
| 562 |
procedure TInputShortcut.AssignCurrent(const AKey1: TKey; |
| 563 |
const AKey2: TKey; |
| 564 |
AKeyString: String; |
| 565 |
const AMouseButtonUse: boolean; |
| 566 |
const AMouseButton: TCastleMouseButton; |
| 567 |
const AMouseWheel: TMouseWheelDirection); |
| 568 |
begin |
| 569 |
// only for backward compatibility (when this parameter was Char) convert #0 to '' |
| 570 |
if AKeyString = #0 then |
| 571 |
AKeyString := ''; |
| 572 |
|
| 573 |
FKey1 := AKey1; |
| 574 |
FKey2 := AKey2; |
| 575 |
FKeyString := AKeyString; |
| 576 |
FMouseButtonUse := AMouseButtonUse; |
| 577 |
FMouseButton := AMouseButton; |
| 578 |
FMouseButtonCheckModifiers := []; // not set by parameters, just reset |
| 579 |
FMouseButtonModifiers := []; // not set by parameters, just reset |
| 580 |
FMouseButton2Use := false; // not set by parameters, just reset |
| 581 |
FMouseButton2 := buttonLeft; // not set by parameters, just reset |
| 582 |
FMouseButton2CheckModifiers := []; // not set by parameters, just reset |
| 583 |
FMouseButton2Modifiers := []; // not set by parameters, just reset |
| 584 |
FMouseWheel := AMouseWheel; |
| 585 |
Changed; |
| 586 |
end; |
| 587 |
|
| 588 |
procedure TInputShortcut.Assign(Source: TInputShortcut; CopyDefaults: boolean); |
| 589 |
begin |
| 590 |
if CopyDefaults then |
| 591 |
begin |
| 592 |
DefaultKey1 := Source.DefaultKey1; |
| 593 |
DefaultKey2 := Source.DefaultKey2; |
| 594 |
DefaultKeyString := Source.DefaultKeyString; |
| 595 |
DefaultMouseButtonUse := Source.DefaultMouseButtonUse; |
| 596 |
DefaultMouseButton := Source.DefaultMouseButton; |
| 597 |
DefaultMouseButtonCheckModifiers := Source.DefaultMouseButtonCheckModifiers; |
| 598 |
DefaultMouseButtonModifiers := Source.DefaultMouseButtonModifiers; |
| 599 |
DefaultMouseButton2Use := Source.DefaultMouseButton2Use; |
| 600 |
DefaultMouseButton2 := Source.DefaultMouseButton2; |
| 601 |
DefaultMouseButton2CheckModifiers := Source.DefaultMouseButton2CheckModifiers; |
| 602 |
DefaultMouseButton2Modifiers := Source.DefaultMouseButton2Modifiers; |
| 603 |
DefaultMouseWheel := Source.DefaultMouseWheel; |
| 604 |
end; |
| 605 |
|
| 606 |
FKey1 := Source.Key1; |
| 607 |
FKey2 := Source.Key2; |
| 608 |
FKeyString := Source.KeyString; |
| 609 |
FMouseButtonUse := Source.MouseButtonUse; |
| 610 |
FMouseButton := Source.MouseButton; |
| 611 |
FMouseButtonCheckModifiers := Source.MouseButtonCheckModifiers; |
| 612 |
FMouseButtonModifiers := Source.MouseButtonModifiers; |
| 613 |
FMouseButton2Use := Source.MouseButton2Use; |
| 614 |
FMouseButton2 := Source.MouseButton2; |
| 615 |
FMouseButton2CheckModifiers := Source.MouseButton2CheckModifiers; |
| 616 |
FMouseButton2Modifiers := Source.MouseButton2Modifiers; |
| 617 |
FMouseWheel := Source.MouseWheel; |
| 618 |
|
| 619 |
{ we don't set here properties, but directly set FXxx fields, |
| 620 |
so that we can call Changed only once. } |
| 621 |
Changed; |
| 622 |
end; |
| 623 |
|
| 624 |
procedure TInputShortcut.MakeClear(const ClearAlsoDefaultState: boolean); |
| 625 |
begin |
| 626 |
FKey1 := keyNone; |
| 627 |
FKey2 := keyNone; |
| 628 |
FKeyString := ''; |
| 629 |
FMouseButtonUse := false; |
| 630 |
FMouseButton := buttonLeft; |
| 631 |
FMouseButtonCheckModifiers := []; |
| 632 |
FMouseButtonModifiers := []; |
| 633 |
FMouseButton2Use := false; |
| 634 |
FMouseButton2 := buttonLeft; |
| 635 |
FMouseButton2CheckModifiers := []; |
| 636 |
FMouseButton2Modifiers := []; |
| 637 |
FMouseWheel := mwNone; |
| 638 |
|
| 639 |
if ClearAlsoDefaultState then |
| 640 |
begin |
| 641 |
FDefaultKey1 := keyNone; |
| 642 |
FDefaultKey2 := keyNone; |
| 643 |
FDefaultKeyString := ''; |
| 644 |
FDefaultMouseButtonUse := false; |
| 645 |
FDefaultMouseButton := buttonLeft; |
| 646 |
FDefaultMouseButtonCheckModifiers := []; |
| 647 |
FDefaultMouseButtonModifiers := []; |
| 648 |
FDefaultMouseButton2Use := false; |
| 649 |
FDefaultMouseButton2 := buttonLeft; |
| 650 |
FDefaultMouseButton2CheckModifiers := []; |
| 651 |
FDefaultMouseButton2Modifiers := []; |
| 652 |
FDefaultMouseWheel := mwNone; |
| 653 |
end; |
| 654 |
|
| 655 |
{ we don't set here properties, but directly set FXxx fields, |
| 656 |
so that we can call Changed only once. } |
| 657 |
Changed; |
| 658 |
end; |
| 659 |
|
| 660 |
function TInputShortcut.IsPressed( |
| 661 |
const Pressed: TKeysPressed; |
| 662 |
const MousePressed: TCastleMouseButtons): boolean; |
| 663 |
begin |
| 664 |
Result := |
| 665 |
( (Pressed <> nil) and (Pressed.Keys[Key1] or |
| 666 |
Pressed.Keys[Key2] or |
| 667 |
Pressed.Strings[KeyString]) ) or |
| 668 |
( MouseButtonUse and |
| 669 |
(MouseButton in MousePressed) and |
| 670 |
(ModifiersDown(Pressed) * MouseButtonCheckModifiers = MouseButtonModifiers) |
| 671 |
) or |
| 672 |
( MouseButton2Use and |
| 673 |
(MouseButton2 in MousePressed) and |
| 674 |
(ModifiersDown(Pressed) * MouseButton2CheckModifiers = MouseButton2Modifiers) |
| 675 |
); |
| 676 |
end; |
| 677 |
|
| 678 |
{$ifndef CASTLE_STRICT_CLI} |
| 679 |
function TInputShortcut.IsPressed(const Container: TCastleContainer): boolean; |
| 680 |
begin |
| 681 |
Result := IsPressed(Container.Pressed, Container.MousePressed); |
| 682 |
end; |
| 683 |
{$endif} |
| 684 |
|
| 685 |
function TInputShortcut.IsKey(const Key: TKey; AKeyString: String): boolean; |
| 686 |
begin |
| 687 |
// only for backward compatibility (when this parameter was Char) convert #0 to '' |
| 688 |
if AKeyString = #0 then |
| 689 |
AKeyString := ''; |
| 690 |
|
| 691 |
Result := |
| 692 |
( (Key <> keyNone) and ( (Key = Key1) or (Key = Key2) ) ) or |
| 693 |
( (KeyString <> '') and (KeyString = AKeyString) ); |
| 694 |
end; |
| 695 |
|
| 696 |
function TInputShortcut.IsMouseButton(const AMouseButton: TCastleMouseButton; |
| 697 |
const ModifiersDown: TModifierKeys): boolean; |
| 698 |
begin |
| 699 |
Result := |
| 700 |
( MouseButtonUse and |
| 701 |
(AMouseButton = MouseButton) and |
| 702 |
(ModifiersDown * MouseButtonCheckModifiers = MouseButtonModifiers) |
| 703 |
) or |
| 704 |
( MouseButton2Use and |
| 705 |
(AMouseButton = MouseButton2) and |
| 706 |
(ModifiersDown * MouseButton2CheckModifiers = MouseButton2Modifiers) |
| 707 |
); |
| 708 |
end; |
| 709 |
|
| 710 |
function TInputShortcut.IsMouseButton(const AMouseButton: TCastleMouseButton): boolean; |
| 711 |
begin |
| 712 |
Result := IsMouseButton(AMouseButton, []); |
| 713 |
end; |
| 714 |
|
| 715 |
function TInputShortcut.IsMouseWheel(const AMouseWheel: TMouseWheelDirection): boolean; |
| 716 |
begin |
| 717 |
Result := (AMouseWheel <> mwNone) and (AMouseWheel = MouseWheel); |
| 718 |
end; |
| 719 |
|
| 720 |
function TInputShortcut.IsEvent(const AKey: TKey; AKeyString: String; |
| 721 |
const AMousePress: boolean; const AMouseButton: TCastleMouseButton; |
| 722 |
const AMouseWheel: TMouseWheelDirection; |
| 723 |
const ModifiersDown: TModifierKeys): boolean; |
| 724 |
begin |
| 725 |
// only for backward compatibility (when this parameter was Char) convert #0 to '' |
| 726 |
if AKeyString = #0 then |
| 727 |
AKeyString := ''; |
| 728 |
|
| 729 |
if AMousePress then |
| 730 |
Result := IsMouseButton(AMouseButton, ModifiersDown) |
| 731 |
else |
| 732 |
if AMouseWheel <> mwNone then |
| 733 |
Result := IsMouseWheel(AMouseWheel) |
| 734 |
else |
| 735 |
Result := IsKey(AKey, AKeyString); |
| 736 |
end; |
| 737 |
|
| 738 |
function TInputShortcut.IsEvent(const Event: TInputPressRelease): boolean; |
| 739 |
begin |
| 740 |
case Event.EventType of |
| 741 |
itKey : Result := IsKey(Event.Key, Event.KeyString); |
| 742 |
itMouseButton: Result := IsMouseButton(Event.MouseButton, Event.ModifiersDown); |
| 743 |
itMouseWheel : Result := IsMouseWheel(Event.MouseWheel); |
| 744 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 745 |
else raise EInternalError.Create('TInputShortcut.IsEvent: Event.EventType?'); |
| 746 |
{$endif} |
| 747 |
end; |
| 748 |
end; |
| 749 |
|
| 750 |
function TInputShortcut.Description(const NoneString: string): string; |
| 751 |
begin |
| 752 |
Result := ''; |
| 753 |
|
| 754 |
{ It's important for this description to be really compact (as it's |
| 755 |
used in situations like menu items (see "The Castle")), that's why |
| 756 |
I mess with checking various cases and trying to make shorter string |
| 757 |
for this. } |
| 758 |
|
| 759 |
if (Key1 <> keyNone) or (Key2 <> keyNone) then |
| 760 |
begin |
| 761 |
if (Key1 <> keyNone) and (Key2 <> keyNone) then |
| 762 |
Result := Format('key "%s" or "%s"', [KeyToStr(Key1), KeyToStr(Key2)]) else |
| 763 |
if Key1 <> keyNone then |
| 764 |
Result := Result + (Format('key "%s"', [KeyToStr(Key1)])) else |
| 765 |
Result := Result + Format('key "%s"', [KeyToStr(Key2)]); |
| 766 |
end; |
| 767 |
|
| 768 |
if KeyString <> '' then |
| 769 |
begin |
| 770 |
if Result <> '' then Result := Result + ' or '; |
| 771 |
Result := Result + Format('char "%s"', [KeyStringToNiceStr(KeyString)]); |
| 772 |
end; |
| 773 |
|
| 774 |
if MouseButtonUse then |
| 775 |
begin |
| 776 |
if Result <> '' then Result := Result + ' or '; |
| 777 |
Result := Result + Format('mouse "%s"', [MouseButtonStr[MouseButton]]); |
| 778 |
if MouseButtonCheckModifiers <> [] then |
| 779 |
Result := Result + '+' + ModifierKeysToNiceStr(MouseButtonModifiers); |
| 780 |
end; |
| 781 |
|
| 782 |
if MouseButton2Use then |
| 783 |
begin |
| 784 |
if Result <> '' then Result := Result + ' or '; |
| 785 |
Result := Result + Format('mouse "%s"', [MouseButtonStr[MouseButton2]]); |
| 786 |
if MouseButton2CheckModifiers <> [] then |
| 787 |
Result := Result + '+' + ModifierKeysToNiceStr(MouseButton2Modifiers); |
| 788 |
end; |
| 789 |
|
| 790 |
if MouseWheel <> mwNone then |
| 791 |
begin |
| 792 |
if Result <> '' then Result := Result + ' or '; |
| 793 |
Result := Result + Format('wheel "%s"', [MouseWheelDirectionStr[MouseWheel]]); |
| 794 |
end; |
| 795 |
|
| 796 |
if Result = '' then |
| 797 |
Result := NoneString; |
| 798 |
end; |
| 799 |
|
| 800 |
function TInputShortcut.Description: string; |
| 801 |
begin |
| 802 |
Result := Description(Format('"%s" key', [Caption])); |
| 803 |
end; |
| 804 |
|
| 805 |
procedure TInputShortcut.Changed; |
| 806 |
begin |
| 807 |
end; |
| 808 |
|
| 809 |
procedure TInputShortcut.SetKey1(const Value: TKey); |
| 810 |
begin |
| 811 |
FKey1 := Value; |
| 812 |
Changed; |
| 813 |
end; |
| 814 |
|
| 815 |
procedure TInputShortcut.SetKey2(const Value: TKey); |
| 816 |
begin |
| 817 |
FKey2 := Value; |
| 818 |
Changed; |
| 819 |
end; |
| 820 |
|
| 821 |
procedure TInputShortcut.SetKeyString(const Value: String); |
| 822 |
begin |
| 823 |
FKeyString := Value; |
| 824 |
Changed; |
| 825 |
end; |
| 826 |
|
| 827 |
procedure TInputShortcut.SetMouseButtonUse(const Value: boolean); |
| 828 |
begin |
| 829 |
FMouseButtonUse := Value; |
| 830 |
Changed; |
| 831 |
end; |
| 832 |
|
| 833 |
procedure TInputShortcut.SetMouseButton(const Value: TCastleMouseButton); |
| 834 |
begin |
| 835 |
FMouseButton := Value; |
| 836 |
Changed; |
| 837 |
end; |
| 838 |
|
| 839 |
procedure TInputShortcut.SetMouseButtonCheckModifiers(const Value: TModifierKeys); |
| 840 |
begin |
| 841 |
FMouseButtonCheckModifiers := Value; |
| 842 |
Changed; |
| 843 |
end; |
| 844 |
|
| 845 |
procedure TInputShortcut.SetMouseButtonModifiers(const Value: TModifierKeys); |
| 846 |
begin |
| 847 |
FMouseButtonModifiers := Value; |
| 848 |
Changed; |
| 849 |
end; |
| 850 |
|
| 851 |
procedure TInputShortcut.SetMouseButton2Use(const Value: boolean); |
| 852 |
begin |
| 853 |
FMouseButton2Use := Value; |
| 854 |
Changed; |
| 855 |
end; |
| 856 |
|
| 857 |
procedure TInputShortcut.SetMouseButton2(const Value: TCastleMouseButton); |
| 858 |
begin |
| 859 |
FMouseButton2 := Value; |
| 860 |
Changed; |
| 861 |
end; |
| 862 |
|
| 863 |
procedure TInputShortcut.SetMouseButton2CheckModifiers(const Value: TModifierKeys); |
| 864 |
begin |
| 865 |
FMouseButton2CheckModifiers := Value; |
| 866 |
Changed; |
| 867 |
end; |
| 868 |
|
| 869 |
procedure TInputShortcut.SetMouseButton2Modifiers(const Value: TModifierKeys); |
| 870 |
begin |
| 871 |
FMouseButton2Modifiers := Value; |
| 872 |
Changed; |
| 873 |
end; |
| 874 |
|
| 875 |
procedure TInputShortcut.SetMouseWheel(const Value: TMouseWheelDirection); |
| 876 |
begin |
| 877 |
FMouseWheel := Value; |
| 878 |
Changed; |
| 879 |
end; |
| 880 |
|
| 881 |
function TInputShortcut.Modifiers: TModifierKeys; |
| 882 |
var |
| 883 |
MK: TModifierKey; |
| 884 |
begin |
| 885 |
Result := []; |
| 886 |
for MK := Low(MK) to High(MK) do |
| 887 |
begin |
| 888 |
if (Key1 = ModifierKeyToKey[MK]) or (Key2 = ModifierKeyToKey[MK]) then |
| 889 |
Include(Result, MK); |
| 890 |
end; |
| 891 |
end; |
| 892 |
|
| 893 |
procedure TInputShortcut.Add(const NewEvent: TInputPressRelease); |
| 894 |
begin |
| 895 |
case NewEvent.EventType of |
| 896 |
itMouseButton: |
| 897 |
begin |
| 898 |
// TODO: add to MouseButton2Xxx, when MouseButtonUse already used |
| 899 |
MouseButtonUse := true; |
| 900 |
MouseButton := NewEvent.MouseButton; |
| 901 |
MouseButtonCheckModifiers := []; // reset |
| 902 |
MouseButtonModifiers := []; // reset |
| 903 |
end; |
| 904 |
itMouseWheel: |
| 905 |
MouseWheel := NewEvent.MouseWheel; |
| 906 |
itKey: |
| 907 |
if Key1 = keyNone then |
| 908 |
Key1 := NewEvent.Key else |
| 909 |
if Key2 = keyNone then |
| 910 |
Key2 := NewEvent.Key else |
| 911 |
begin |
| 912 |
{ We move the previous Key1 to Key2, and set Key1 to new key. |
| 913 |
This looks nice for user when this is displayed as the menu argument. } |
| 914 |
Key2 := Key1; |
| 915 |
Key1 := NewEvent.Key; |
| 916 |
end; |
| 917 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 918 |
else raise EInternalError.Create('TInputShortcut.Add: NewEvent.EventType?'); |
| 919 |
{$endif} |
| 920 |
end; |
| 921 |
end; |
| 922 |
|
| 923 |
procedure TInputShortcut.SaveToConfig(const Config: TCastleConfig; ConfigPath: String); |
| 924 |
begin |
| 925 |
// add slash at the end of ConfigPath, if necessary |
| 926 |
if (ConfigPath <> '') and (ConfigPath[Length(ConfigPath)] <> '/') then |
| 927 |
ConfigPath := ConfigPath + '/'; |
| 928 |
|
| 929 |
Config.SetDeleteKey(ConfigPath + Name + '/key1', |
| 930 |
Key1, DefaultKey1); |
| 931 |
Config.SetDeleteKey(ConfigPath + Name + '/key2', |
| 932 |
Key2, DefaultKey2); |
| 933 |
|
| 934 |
Config.SetDeleteValue(ConfigPath + Name + '/mouse_button_use', |
| 935 |
MouseButtonUse, DefaultMouseButtonUse); |
| 936 |
Config.SetDeleteValue(ConfigPath + Name + '/mouse_button', |
| 937 |
Ord(MouseButton), Ord(DefaultMouseButton)); |
| 938 |
// TODO |
| 939 |
// Config.SetDeleteValue(ConfigPath + Name + '/mouse_button_check_modifiers', |
| 940 |
// ModifiersToString(MouseButtonCheckModifiers), ModifiersToString(DefaultMouseButtonCheckModifiers)); |
| 941 |
// Config.SetDeleteValue(ConfigPath + Name + '/mouse_button_modifiers', |
| 942 |
// ModifiersToString(MouseButtonModifiers), ModifiersToString(DefaultMouseButtonModifiers)); |
| 943 |
|
| 944 |
Config.SetDeleteValue(ConfigPath + Name + '/mouse_button2_use', |
| 945 |
MouseButton2Use, DefaultMouseButton2Use); |
| 946 |
Config.SetDeleteValue(ConfigPath + Name + '/mouse_button2', |
| 947 |
Ord(MouseButton2), Ord(DefaultMouseButton2)); |
| 948 |
// TODO |
| 949 |
// Config.SetDeleteValue(ConfigPath + Name + '/mouse_button2_check_modifiers', |
| 950 |
// ModifiersToString(MouseButton2CheckModifiers), ModifiersToString(DefaultMouseButton2CheckModifiers)); |
| 951 |
// Config.SetDeleteValue(ConfigPath + Name + '/mouse_button2_modifiers', |
| 952 |
// ModifiersToString(MouseButton2Modifiers), ModifiersToString(DefaultMouseButton2Modifiers)); |
| 953 |
|
| 954 |
Config.SetDeleteValue(ConfigPath + Name + '/mouse_wheel', |
| 955 |
Ord(MouseWheel), Ord(DefaultMouseWheel)); |
| 956 |
end; |
| 957 |
|
| 958 |
procedure TInputShortcut.LoadFromConfig(const Config: TCastleConfig; ConfigPath: String); |
| 959 |
begin |
| 960 |
// add slash at the end of ConfigPath, if necessary |
| 961 |
if (ConfigPath <> '') and (ConfigPath[Length(ConfigPath)] <> '/') then |
| 962 |
ConfigPath := ConfigPath + '/'; |
| 963 |
|
| 964 |
Key1 := Config.GetKey( |
| 965 |
ConfigPath + Name + '/key1', DefaultKey1); |
| 966 |
Key2 := Config.GetKey( |
| 967 |
ConfigPath + Name + '/key2', DefaultKey2); |
| 968 |
|
| 969 |
MouseButtonUse := Config.GetValue( |
| 970 |
ConfigPath + Name + '/mouse_button_use', DefaultMouseButtonUse); |
| 971 |
MouseButton := TCastleMouseButton(Config.GetValue( |
| 972 |
ConfigPath + Name + '/mouse_button', Ord(DefaultMouseButton))); |
| 973 |
// TODO |
| 974 |
// MouseButtonCheckModifiers := StringToModifiers(Config.GetValue( |
| 975 |
// ConfigPath + Name + '/mouse_button_check_modifiers', ModifiersToString(DefaultMouseButtonCheckModifiers))); |
| 976 |
// MouseButtonModifiers := StringToModifiers(Config.GetValue( |
| 977 |
// ConfigPath + Name + '/mouse_button_modifiers', ModifiersToString(DefaultMouseButtonModifiers))); |
| 978 |
|
| 979 |
MouseButton2Use := Config.GetValue( |
| 980 |
ConfigPath + Name + '/mouse_button2_use', DefaultMouseButton2Use); |
| 981 |
MouseButton2 := TCastleMouseButton(Config.GetValue( |
| 982 |
ConfigPath + Name + '/mouse_button2', Ord(DefaultMouseButton2))); |
| 983 |
// TODO |
| 984 |
// MouseButton2CheckModifiers := StringToModifiers(Config.GetValue( |
| 985 |
// ConfigPath + Name + '/mouse_button2_check_modifiers', ModifiersToString(DefaultMouseButton2CheckModifiers))); |
| 986 |
// MouseButton2Modifiers := StringToModifiers(Config.GetValue( |
| 987 |
// ConfigPath + Name + '/mouse_button2_modifiers', ModifiersToString(DefaultMouseButton2Modifiers))); |
| 988 |
|
| 989 |
MouseWheel := TMouseWheelDirection(Config.GetValue( |
| 990 |
ConfigPath + Name + '/mouse_wheel', Ord(DefaultMouseWheel))); |
| 991 |
end; |
| 992 |
|
| 993 |
{ TInputShortcutList ----------------------------------------------------- } |
| 994 |
|
| 995 |
function TInputShortcutList.SeekMatchingShortcut( |
| 996 |
const Event: TInputPressRelease): TInputShortcut; |
| 997 |
var |
| 998 |
I: Integer; |
| 999 |
begin |
| 1000 |
for I := 0 to Count - 1 do |
| 1001 |
begin |
| 1002 |
Result := Items[I]; |
| 1003 |
if Result.IsEvent(Event) then |
| 1004 |
Exit; |
| 1005 |
end; |
| 1006 |
Result := nil; |
| 1007 |
end; |
| 1008 |
|
| 1009 |
procedure TInputShortcutList.RestoreDefaults; |
| 1010 |
var |
| 1011 |
I: TInputShortcut; |
| 1012 |
begin |
| 1013 |
for I in Self do |
| 1014 |
I.MakeDefault; |
| 1015 |
end; |
| 1016 |
|
| 1017 |
procedure TInputShortcutList.SaveToConfig(const Config: TCastleConfig; ConfigPath: String = ''); |
| 1018 |
var |
| 1019 |
I: TInputShortcut; |
| 1020 |
begin |
| 1021 |
// add slash at the end of ConfigPath, if necessary |
| 1022 |
if (ConfigPath <> '') and (ConfigPath[Length(ConfigPath)] <> '/') then |
| 1023 |
ConfigPath := ConfigPath + '/'; |
| 1024 |
|
| 1025 |
ConfigPath := ConfigPath + 'inputs/'; |
| 1026 |
|
| 1027 |
for I in Self do |
| 1028 |
I.SaveToConfig(Config, ConfigPath); |
| 1029 |
end; |
| 1030 |
|
| 1031 |
function SortInputShortcut({$ifdef GENERICS_CONSTREF}constref{$else}const{$endif} A, B: TInputShortcut): Integer; |
| 1032 |
begin |
| 1033 |
Result := A.GroupOrder - B.GroupOrder; |
| 1034 |
{ since TFPSList.Sort is not stable, we use Index to keep order predictable |
| 1035 |
when GroupOrder is equal } |
| 1036 |
if Result = 0 then |
| 1037 |
Result := A.Index - B.Index; |
| 1038 |
end; |
| 1039 |
|
| 1040 |
procedure TInputShortcutList.LoadFromConfig(const Config: TCastleConfig; ConfigPath: String); |
| 1041 |
type |
| 1042 |
TInputShortcutComparer = {$ifdef FPC}specialize{$endif} TComparer<TInputShortcut>; |
| 1043 |
var |
| 1044 |
I: TInputShortcut; |
| 1045 |
ConflictDescription: string; |
| 1046 |
G: TInputGroupNotLocal; |
| 1047 |
begin |
| 1048 |
{ we assume that all inputs are added now, so we do some finalizing operations |
| 1049 |
now, like checking defaults for conflicts and sorting by GroupOrder. } |
| 1050 |
if SeekConflict(ConflictDescription) then |
| 1051 |
raise EInternalError.Create( |
| 1052 |
'Default key/mouse shortcuts layout has conflicts: ' + ConflictDescription); |
| 1053 |
|
| 1054 |
for G := Low(TInputGroupNotLocal) to High(TInputGroupNotLocal) do |
| 1055 |
InputsGroup(G).Sort(TInputShortcutComparer.Construct({$ifdef FPC}@{$endif}SortInputShortcut)); |
| 1056 |
|
| 1057 |
// add slash at the end of ConfigPath, if necessary |
| 1058 |
if (ConfigPath <> '') and (ConfigPath[Length(ConfigPath)] <> '/') then |
| 1059 |
ConfigPath := ConfigPath + '/'; |
| 1060 |
|
| 1061 |
ConfigPath := ConfigPath + 'inputs/'; |
| 1062 |
|
| 1063 |
for I in Self do |
| 1064 |
I.LoadFromConfig(Config, ConfigPath); |
| 1065 |
|
| 1066 |
if SeekConflict(ConflictDescription) then |
| 1067 |
begin |
| 1068 |
WarningWrite( |
| 1069 |
'Your key/mouse shortcuts layout has conflicts. This can happen if you ' + |
| 1070 |
'just upgraded the game to newer version, and the newer version has new ' + |
| 1071 |
'key/mouse shortcuts or has different default key/mouse shortcuts than previous ' + |
| 1072 |
'version. It can also happen if you manually edited the configuration ' + |
| 1073 |
'file. I will reset your key/mouse shortcuts to default now.' +nl+ |
| 1074 |
nl+ |
| 1075 |
'Detailed conflict description: ' + ConflictDescription); |
| 1076 |
RestoreDefaults; |
| 1077 |
end; |
| 1078 |
end; |
| 1079 |
|
| 1080 |
function TInputShortcutList.SeekConflict( |
| 1081 |
out ConflictDescription: string): boolean; |
| 1082 |
var |
| 1083 |
I, J: Integer; |
| 1084 |
begin |
| 1085 |
for I := 0 to Count - 1 do |
| 1086 |
for J := I + 1 to Count - 1 do |
| 1087 |
begin |
| 1088 |
if Items[J].IsKey(Items[I].Key1, '') or |
| 1089 |
Items[J].IsKey(Items[I].Key2, '') or |
| 1090 |
(Items[I].MouseButtonUse and Items[J].IsMouseButton(Items[I].MouseButton , Items[I].MouseButtonModifiers )) or |
| 1091 |
(Items[I].MouseButton2Use and Items[J].IsMouseButton(Items[I].MouseButton2, Items[I].MouseButton2Modifiers)) then |
| 1092 |
begin |
| 1093 |
ConflictDescription := Format('"%s" conflicts with "%s"', |
| 1094 |
[Items[I].Caption, Items[J].Caption]); |
| 1095 |
Exit(true); |
| 1096 |
end; |
| 1097 |
end; |
| 1098 |
Result := false; |
| 1099 |
end; |
| 1100 |
|
| 1101 |
function TInputShortcutList.FindName(const Name: string): TInputShortcut; |
| 1102 |
var |
| 1103 |
I: Integer; |
| 1104 |
begin |
| 1105 |
for I := 0 to Count - 1 do |
| 1106 |
if Items[I].Name = Name then |
| 1107 |
Exit(Items[I]); |
| 1108 |
Result := nil; |
| 1109 |
end; |
| 1110 |
|
| 1111 |
{ initialization / finalization ---------------------------------------------- } |
| 1112 |
|
| 1113 |
var |
| 1114 |
UnitInitialized, UnitFinalization: Boolean; |
| 1115 |
FInputsAll: TInputShortcutList; |
| 1116 |
FInputsGroup: array [TInputGroupNotLocal] of TInputShortcutList; |
| 1117 |
|
| 1118 |
procedure DoInitialization; |
| 1119 |
var |
| 1120 |
G: TInputGroupNotLocal; |
| 1121 |
begin |
| 1122 |
if (not UnitInitialized) and (not UnitFinalization) then |
| 1123 |
begin |
| 1124 |
FInputsAll := TInputShortcutList.Create(true); |
| 1125 |
for G := Low(TInputGroupNotLocal) to High(TInputGroupNotLocal) do |
| 1126 |
FInputsGroup[G] := TInputShortcutList.Create(false); |
| 1127 |
UnitInitialized := true; |
| 1128 |
end; |
| 1129 |
end; |
| 1130 |
|
| 1131 |
function InputsAll: TInputShortcutList; |
| 1132 |
begin |
| 1133 |
DoInitialization; |
| 1134 |
Result := FInputsAll; |
| 1135 |
end; |
| 1136 |
|
| 1137 |
function InputsGroup(const Group: TInputGroupNotLocal): TInputShortcutList; |
| 1138 |
begin |
| 1139 |
DoInitialization; |
| 1140 |
Result := FInputsGroup[Group]; |
| 1141 |
end; |
| 1142 |
|
| 1143 |
procedure DoFinalization; |
| 1144 |
var |
| 1145 |
G: TInputGroupNotLocal; |
| 1146 |
begin |
| 1147 |
UnitFinalization := true; |
| 1148 |
for G := Low(TInputGroupNotLocal) to High(TInputGroupNotLocal) do |
| 1149 |
FreeAndNil(FInputsGroup[G]); |
| 1150 |
FreeAndNil(FInputsAll); |
| 1151 |
end; |
| 1152 |
|
| 1153 |
initialization |
| 1154 |
DoInitialization; |
| 1155 |
finalization |
| 1156 |
DoFinalization; |
| 1157 |
end. |