Improve the keymapper

The keymapper, available as one of the options in the starting menu when the EXE is run without a scenery specified, has undergone a few changes:
- Key descriptions are now parsed the way Starter does and are displayed in the window. They are also no longer consumed when saving to a file.
- Unbound keys are now also displayed as well. Not all of them though; some entries won't exist if they are not specified in the eu07_input-keyboard.ini file.
- You can now unbind a key by pressing F10.

The Parser has got a new boolean option: `skipComments`, true by default to leave current behavior unchanged. When set to false, comments will be parsed as ordinary tokens.
This commit is contained in:
jakubg1
2024-03-03 03:00:47 +01:00
parent 9259708d4c
commit 84980a4484
7 changed files with 222 additions and 183 deletions

View File

@@ -21,8 +21,15 @@ bool ui::keymapper_panel::key(int key)
key |= keyboard_input::keymodifier::shift;
if (Global.ctrlState)
key |= keyboard_input::keymodifier::control;
// F10 unbinds the key.
if (it->second.compare("f10") == 0)
key = 0;
keyboard.bindings().insert_or_assign(bind_active, key);
// Replace the binding with a new key.
auto it2 = keyboard.bindings().find(bind_active);
if (it2 != keyboard.bindings().end()) {
it2->second = std::tuple<int, std::string>(key, std::get<std::string>(it2->second));
}
bind_active = user_command::none;
keyboard.bind();
@@ -45,18 +52,26 @@ void ui::keymapper_panel::render()
if (ImGui::Button(STR_C("Save")))
keyboard.dump_bindings();
for (const std::pair<user_command, int> &binding : keyboard.bindings()) {
for (const std::pair<user_command, std::tuple<int, std::string>> &binding : keyboard.bindings()) {
// Binding ID
ImGui::AlignTextToFramePadding();
ImGui::Text(simulation::Commands_descriptions[static_cast<std::size_t>(binding.first)].name.c_str());
// Binding description
std::string description = std::get<std::string>(binding.second);
ImGui::SameLine(260);
ImGui::Text((description.size() > 0 ? description : "(No description)").c_str());
// Binding key button
int keycode = std::get<int>(binding.second);
std::string label;
if (binding.second & keyboard_input::keymodifier::control)
if (keycode & keyboard_input::keymodifier::control)
label += "ctrl + ";
if (binding.second & keyboard_input::keymodifier::shift)
if (keycode & keyboard_input::keymodifier::shift)
label += "shift + ";
auto it = keyboard.keytonamemap.find(binding.second & 0xFFFF);
auto it = keyboard.keytonamemap.find(keycode & 0xFFFF);
if (it != keyboard.keytonamemap.end())
label += it->second;
@@ -64,7 +79,7 @@ void ui::keymapper_panel::render()
bool styles_pushed = false;
if (bind_active == binding.first) {
label = "?";
label = "Press key (F10 to unbind)";
ImVec4 active_col = ImGui::GetStyleColorVec4(ImGuiCol_ButtonActive);
ImGui::PushStyleColor(ImGuiCol_Button, active_col);
styles_pushed = true;