From b95f9885a4cb53555189ff73da1ee399d68c359d Mon Sep 17 00:00:00 2001 From: tildearrow Date: Thu, 6 Feb 2025 00:33:34 -0500 Subject: [PATCH] GUI: don't sort note keys in settings until Apply or OK are clicked issue #2345 --- src/gui/gui.h | 12 +++++++ src/gui/settings.cpp | 74 +++++++++++++++++++++++++++----------------- 2 files changed, 57 insertions(+), 29 deletions(-) diff --git a/src/gui/gui.h b/src/gui/gui.h index 69aeb12d8..a4bd2405a 100644 --- a/src/gui/gui.h +++ b/src/gui/gui.h @@ -1602,6 +1602,15 @@ struct PendingDrawOsc { lineSize(0.0f) {} }; +struct MappedInput { + int scan; + int val; + MappedInput(): + scan(SDL_SCANCODE_UNKNOWN), val(0) {} + MappedInput(int s, int v): + scan(s), val(v) {} +}; + struct FurnaceCV; class FurnaceGUI { @@ -2450,6 +2459,7 @@ class FurnaceGUI { // SDL_Scancode,int std::map noteKeys; + std::vector noteKeysRaw; // SDL_Keycode,int std::map valueKeys; @@ -3037,6 +3047,8 @@ class FurnaceGUI { bool parseSysEx(unsigned char* data, size_t len); void applyUISettings(bool updateFonts=true); + void decompileNoteKeys(); + void compileNoteKeys(); void initSystemPresets(); void initRandomDemoSong(); diff --git a/src/gui/settings.cpp b/src/gui/settings.cpp index 6e85f10b7..2bfd1de9a 100644 --- a/src/gui/settings.cpp +++ b/src/gui/settings.cpp @@ -535,15 +535,6 @@ void FurnaceGUI::promptKey(int which, int bindIdx) { } } -struct MappedInput { - int scan; - int val; - MappedInput(): - scan(SDL_SCANCODE_UNKNOWN), val(0) {} - MappedInput(int s, int v): - scan(s), val(v) {} -}; - void FurnaceGUI::drawSettings() { if (nextWindow==GUI_WINDOW_SETTINGS) { settingsOpen=true; @@ -2288,18 +2279,7 @@ void FurnaceGUI::drawSettings() { ImGui::TreePop(); } if (ImGui::TreeNode(_("Note input"))) { - std::vector sorted; if (ImGui::BeginTable("keysNoteInput",4)) { - for (std::map::value_type& i: noteKeys) { - std::vector::iterator j; - for (j=sorted.begin(); j!=sorted.end(); j++) { - if (j->val>i.second) { - break; - } - } - sorted.insert(j,MappedInput(i.first,i.second)); - } - static char id[4096]; ImGui::TableNextRow(ImGuiTableRowFlags_Headers); @@ -2312,7 +2292,8 @@ void FurnaceGUI::drawSettings() { ImGui::TableNextColumn(); ImGui::Text(_("Remove")); - for (MappedInput& i: sorted) { + for (size_t _i=0; _i96) i.val=96; - noteKeys[i.scan]=i.val; settingsChanged=true; } } ImGui::TableNextColumn(); snprintf(id,4095,ICON_FA_TIMES "##SNRemove_%d",i.scan); if (ImGui::Button(id)) { - noteKeys.erase(i.scan); + noteKeysRaw.erase(noteKeysRaw.begin()+_i); + _i--; settingsChanged=true; } } @@ -2364,8 +2345,19 @@ void FurnaceGUI::drawSettings() { if (sName[0]==0) continue; snprintf(id,4095,"%s##SNNewKey_%d",sName,i); if (ImGui::Selectable(id)) { - noteKeys[(SDL_Scancode)i]=0; - settingsChanged=true; + bool alreadyThere=false; + for (MappedInput& j: noteKeysRaw) { + if (j.scan==i) { + alreadyThere=true; + break; + } + } + if (alreadyThere) { + showError(_("that key is bound already!")); + } else { + noteKeysRaw.push_back(MappedInput(i,0)); + settingsChanged=true; + } } } ImGui::EndCombo(); @@ -4947,6 +4939,7 @@ void FurnaceGUI::readConfig(DivConfig& conf, FurnaceGUISettingGroups groups) { } decodeKeyMap(noteKeys,conf.getString("noteKeys",DEFAULT_NOTE_KEYS)); + decompileNoteKeys(); } if (groups&GUI_SETTINGS_BEHAVIOR) { @@ -5542,6 +5535,7 @@ void FurnaceGUI::writeConfig(DivConfig& conf, FurnaceGUISettingGroups groups) { conf.set(String("keybind_GUI_ACTION_")+String(guiActions[i].name),actionKeys[i]); } + compileNoteKeys(); conf.set("noteKeys",encodeKeyMap(noteKeys)); } @@ -6118,6 +6112,7 @@ void FurnaceGUI::resetKeybinds() { actionKeys[i]=guiActions[i].defaultBind; } decodeKeyMap(noteKeys,DEFAULT_NOTE_KEYS); + decompileNoteKeys(); parseKeybinds(); } @@ -6296,6 +6291,27 @@ void setupLabel(const char* lStr, char* label, int len) { } } +void FurnaceGUI::decompileNoteKeys() { + noteKeysRaw.clear(); + for (std::map::value_type& i: noteKeys) { + std::vector::iterator j; + for (j=noteKeysRaw.begin(); j!=noteKeysRaw.end(); j++) { + if (j->val>i.second) { + break; + } + } + noteKeysRaw.insert(j,MappedInput(i.first,i.second)); + } +} + +void FurnaceGUI::compileNoteKeys() { + noteKeys.clear(); + for (MappedInput& i: noteKeysRaw) { + noteKeys[i.scan]=i.val; + } + decompileNoteKeys(); +} + void FurnaceGUI::applyUISettings(bool updateFonts) { ImGuiStyle sty; if (settings.guiColorsBase) {