furnace/src/gui/commandPalette.cpp

205 lines
5.6 KiB
C++
Raw Normal View History

2023-06-15 01:04:45 -04:00
/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2023 tildearrow and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "gui.h"
#include "guiConst.h"
#include "commandPalette.h"
2023-06-15 01:04:45 -04:00
#include "misc/cpp/imgui_stdlib.h"
#include <fmt/printf.h>
#include <algorithm>
#include <cctype>
#include "../ta-log.h"
2023-06-18 22:21:16 -04:00
static inline bool matchFuzzy(const char* haystack,const char* needle) {
2023-06-15 01:04:45 -04:00
size_t h_i=0; // haystack idx
size_t n_i=0; // needle idx
while (needle[n_i]!='\0') {
for (; std::tolower(haystack[h_i])!=std::tolower(needle[n_i]); h_i++) {
if (haystack[h_i]=='\0')
return false;
}
n_i+=1;
}
return true;
}
void FurnaceGUI::drawPalette() {
bool accepted=false;
if (paletteFirstFrame)
ImGui::SetKeyboardFocusHere();
int width=ImGui::GetContentRegionAvail().x;
ImGui::SetNextItemWidth(width);
2023-06-15 01:04:45 -04:00
2023-07-31 15:12:29 -04:00
const char* hint="Search...";
switch (curPaletteType) {
case CMDPAL_TYPE_RECENT:
hint="Search recent files...";
break;
case CMDPAL_TYPE_INSTRUMENTS:
hint="Search instruments...";
break;
case CMDPAL_TYPE_SAMPLES:
hint="Search samples...";
break;
}
if (ImGui::InputTextWithHint("##CommandPaletteSearch",hint,&paletteQuery) || paletteFirstFrame) {
2023-06-15 01:04:45 -04:00
paletteSearchResults.clear();
2023-06-18 22:21:16 -04:00
switch (curPaletteType) {
case CMDPAL_TYPE_MAIN:
for (int i=0; i<GUI_ACTION_MAX; i++) {
if (guiActions[i].defaultBind==-1) continue;
if (matchFuzzy(guiActions[i].friendlyName,paletteQuery.c_str())) {
paletteSearchResults.push_back(i);
}
2023-06-15 01:04:45 -04:00
}
2023-06-18 22:21:16 -04:00
break;
case CMDPAL_TYPE_RECENT:
for (int i=0; i<(int)recentFile.size(); i++) {
if (matchFuzzy(recentFile.at(i).c_str(),paletteQuery.c_str())) {
2023-06-18 22:21:16 -04:00
paletteSearchResults.push_back(i);
}
}
break;
case CMDPAL_TYPE_INSTRUMENTS:
if (matchFuzzy("- None -",paletteQuery.c_str())) {
paletteSearchResults.push_back(0);
}
for (int i=0; i<e->song.insLen; i++) {
if (matchFuzzy(e->song.ins[i]->name.c_str(),paletteQuery.c_str())) {
paletteSearchResults.push_back(i+1); // because over here ins=0 is 'None'
}
}
break;
case CMDPAL_TYPE_SAMPLES:
for (int i=0; i<e->song.sampleLen; i++) {
logD("ins #%x: %s", i, e->song.sample[i]->name.c_str());
if (matchFuzzy(e->song.sample[i]->name.c_str(),paletteQuery.c_str())) {
paletteSearchResults.push_back(i);
}
}
break;
2023-06-18 22:21:16 -04:00
default:
logE("invalid command palette type");
ImGui::CloseCurrentPopup();
2023-06-18 22:21:16 -04:00
break;
};
2023-06-15 01:04:45 -04:00
}
ImVec2 avail=ImGui::GetContentRegionAvail();
avail.y-=ImGui::GetFrameHeightWithSpacing();
if (ImGui::BeginChild("CommandPaletteList",avail,false,0)) {
2023-06-15 01:04:45 -04:00
bool navigated=false;
if (ImGui::IsKeyPressed(ImGuiKey_UpArrow) && curPaletteChoice>0) {
curPaletteChoice-=1;
navigated=true;
}
if (ImGui::IsKeyPressed(ImGuiKey_DownArrow)) {
curPaletteChoice+=1;
navigated=true;
}
2023-06-18 22:21:16 -04:00
for (int i=0; i<(int)paletteSearchResults.size(); i++) {
2023-06-15 01:04:45 -04:00
bool current=(i==curPaletteChoice);
int id=paletteSearchResults[i];
2023-07-31 15:12:29 -04:00
const char* s="???";
2023-06-18 22:21:16 -04:00
switch (curPaletteType) {
case CMDPAL_TYPE_MAIN:
s=guiActions[id].friendlyName;
2023-06-18 22:21:16 -04:00
break;
case CMDPAL_TYPE_RECENT:
s=recentFile.at(id).c_str();
2023-06-18 22:21:16 -04:00
break;
case CMDPAL_TYPE_INSTRUMENTS:
if (id==0) {
s="- None -";
} else {
s=e->song.ins[id-1]->name.c_str();
}
break;
case CMDPAL_TYPE_SAMPLES:
s=e->song.sample[id]->name.c_str();
break;
2023-06-18 22:21:16 -04:00
default:
logE("invalid command palette type");
2023-06-18 22:21:16 -04:00
break;
};
if (ImGui::Selectable(s,current)) {
2023-06-15 01:04:45 -04:00
curPaletteChoice=i;
accepted=true;
}
if ((navigated || paletteFirstFrame) && current) ImGui::SetScrollHereY();
2023-06-15 01:04:45 -04:00
}
}
ImGui::EndChild();
if (!accepted) {
2023-06-18 22:21:16 -04:00
if (curPaletteChoice>=(int)paletteSearchResults.size()) {
2023-06-15 01:04:45 -04:00
curPaletteChoice=paletteSearchResults.size()-1;
}
accepted=ImGui::IsKeyPressed(ImGuiKey_Enter);
2023-06-15 01:04:45 -04:00
}
if (ImGui::Button("Cancel") || ImGui::IsKeyPressed(ImGuiKey_Escape)) {
ImGui::CloseCurrentPopup();
}
// do not move this to after the resetPalette() calls!
// if they are called before and we're jumping from one palette to the next, the paletteFirstFrame won't be true at the start and the setup will not happen.
paletteFirstFrame=false;
2023-06-15 01:04:45 -04:00
if (accepted) {
2023-07-31 15:12:29 -04:00
if (paletteSearchResults.size()>0) {
int i=paletteSearchResults[curPaletteChoice];
switch (curPaletteType) {
case CMDPAL_TYPE_MAIN:
doAction(i);
break;
2023-06-18 22:21:16 -04:00
case CMDPAL_TYPE_RECENT:
openRecentFile(recentFile.at(i));
break;
2023-06-18 22:21:16 -04:00
case CMDPAL_TYPE_INSTRUMENTS:
curIns=i-1;
break;
case CMDPAL_TYPE_SAMPLES:
curSample=i;
break;
default:
logE("invalid command palette type");
break;
};
}
2023-07-31 15:12:29 -04:00
ImGui::CloseCurrentPopup();
2023-06-18 22:21:16 -04:00
}
2023-06-15 01:04:45 -04:00
}