I've looked a bit into it tonight, so it is far from finished. Though I have some participial lookup code implemented which let you activate the Pause-button though three different keys.
What I want to discuss here is the principals of how this feature is going to be implemented.
Processing of input today
After having looking around how keyboard events are processed I've found out that all windows in OpenTTD have their own event processing function which process key-events. Often they use a switch case block like this:
Code: Select all
case WE_KEYPRESS: {
switch (e->we.keypress.keycode) {
case WKC_F1: case WKC_PAUSE: ToolbarPauseClick(w); break;
case WKC_F2: ShowGameOptions(); break;
case WKC_F3: MenuClickSaveLoad(0); break;
// ...
default: return;
}
e->we.keypress.cont = false;
} break;
Lookup table
The idea I have is that there is a lookup table, an array, which uses pre-defined actions as index.Like this (a bit simplified):
Code: Select all
if (_key_lookup_table[AUTO_RAIL] == event.keycode)
ActivateAutoRails();
Code: Select all
_key_lookup_table[AUTO_RAIL] = 'A';
For the moment I plan to not just store one keycode but a few to allow multiple key settings per action. (the pause-button can be activated by both F1 and Pause-button today)
Also I think this structure is a good place to store a short descriptive text such as "Auto rails" which can be used in the configuration GUI.
Because of the added complexity I'll use either a macro or an inline function for checking if an action should be activated in the event processing routines. If we have 3 possible keys per action that make 3 tests per action, so a macro or an inline is needed for code readability and flexibility (to be able to change 3 to 2 or 5 or whatever desired).
Code: Select all
// Macro:
IF_KEY_ACTION(e, KA_TOGGLE_PAUSE)
ToolbarPauseClick(w);
// Inline function:
if (IsKeyAction(e->we.keypress.keycode, KA_TOGGLE_PAUSE))
ToolbarPauseClick(w);
Side by side
An important thing during the development is that the selected approach can work side by side with the current system. Only a few actions can be changed to use the lookup-table during the time the infrastructure is built up. This is good since declaring actions and action strings for every key shortcut in OpenTTD is a BIG task. But if we first have a working infrastructure it will at least not be wasted time.
Any (constructive) feedback?
A patch will be posted later when I have something with not too messy variable names.
EDIT:
Suggestions on variable names / names of terms are more than welcome! Such as what to call the lookup-table, is there a better term than key-actions? etc...