| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "gui.h" |
| 21 |
#include "guiConst.h" |
| 22 |
#include "commandPalette.h" |
| 23 |
#include "misc/cpp/imgui_stdlib.h" |
| 24 |
#include <fmt/printf.h> |
| 25 |
#include <algorithm> |
| 26 |
#include <ctype.h> |
| 27 |
#include "../ta-log.h" |
| 28 |
#include "util.h" |
| 29 |
|
| 30 |
struct MatchScore { |
| 31 |
size_t charsBeforeNeedle=0; |
| 32 |
size_t charsWithinNeedle=0; |
| 33 |
static bool IsFirstPreferable(const MatchScore& a, const MatchScore& b) { |
| 34 |
int aBetter; |
| 35 |
aBetter=b.charsWithinNeedle-a.charsWithinNeedle; |
| 36 |
if (aBetter!=0) return aBetter>0; |
| 37 |
aBetter=b.charsBeforeNeedle-a.charsBeforeNeedle; |
| 38 |
if (aBetter!=0) return aBetter>0; |
| 39 |
return false; |
| 40 |
} |
| 41 |
}; |
| 42 |
|
| 43 |
struct MatchResult { |
| 44 |
MatchScore score; |
| 45 |
std::vector<int> highlightChars; |
| 46 |
}; |
| 47 |
|
| 48 |
static bool charMatch(const char* a, const char* b) { |
| 49 |
// stub for future utf8 support, possibly with matching for related chars? |
| 50 |
return std::tolower(*a)==std::tolower(*b); |
| 51 |
} |
| 52 |
|
| 53 |
// #define MATCH_GREEDY |
| 54 |
// #define RUN_MATCH_TEST |
| 55 |
|
| 56 |
static bool matchFuzzy(const char* haystack, int haystackLen, const char* needle, int needleLen, MatchResult* result) { |
| 57 |
if (needleLen==0) { |
| 58 |
result->score.charsBeforeNeedle=0; |
| 59 |
result->score.charsWithinNeedle=0; |
| 60 |
result->highlightChars.clear(); |
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
std::vector<MatchResult> matchPool(needleLen+1); |
| 65 |
std::vector<MatchResult*> unusedMatches(needleLen+1); |
| 66 |
std::vector<MatchResult*> matchesByLen(needleLen+1); |
| 67 |
for (int i=0; i<needleLen+1; i++) { |
| 68 |
unusedMatches[i]=&matchPool[i]; |
| 69 |
matchesByLen[i]=NULL; |
| 70 |
} |
| 71 |
|
| 72 |
for (int hIdx=0; hIdx<haystackLen; hIdx++) { |
| 73 |
// try to continue our in-flight valid matches |
| 74 |
for (int matchLen=needleLen-1; matchLen>=0; matchLen--) { |
| 75 |
MatchResult*& m=matchesByLen[matchLen]; |
| 76 |
|
| 77 |
// ignore null matches except for 0 |
| 78 |
if (matchLen>0 && !m) continue; |
| 79 |
|
| 80 |
#ifdef MATCH_GREEDY |
| 81 |
// in greedy mode, don't start any new matches once we've already started matching. |
| 82 |
// this will still return the correct bool result, but its score could be much poorer |
| 83 |
// than the optimal match. consider the case: |
| 84 |
// |
| 85 |
// find "gl" in "google" |
| 86 |
// |
| 87 |
// greedy will see the match "g...l.", which has charsWithinNeedle of 3, while the |
| 88 |
// fully algorithm will find the tighter match "...gl.", which has |
| 89 |
// charsWithinNeedle of 0 |
| 90 |
|
| 91 |
if (matchLen==0 && unusedMatches.size() < matchPool.size()) { |
| 92 |
continue; |
| 93 |
} |
| 94 |
#endif |
| 95 |
|
| 96 |
// check match! |
| 97 |
if (charMatch(haystack+hIdx, needle+matchLen)) { |
| 98 |
|
| 99 |
// pull a fresh match from the pool if necessary |
| 100 |
if (matchLen==0) { |
| 101 |
m=unusedMatches.back(); |
| 102 |
unusedMatches.pop_back(); |
| 103 |
m->score.charsBeforeNeedle=hIdx; |
| 104 |
m->score.charsWithinNeedle=0; |
| 105 |
m->highlightChars.clear(); |
| 106 |
} |
| 107 |
|
| 108 |
m->highlightChars.push_back(hIdx); |
| 109 |
|
| 110 |
// advance, replacing the previous match of an equal len, which can only have been |
| 111 |
// worse because it existed before us, so we can prune it out |
| 112 |
if (matchesByLen[matchLen+1]) { |
| 113 |
unusedMatches.push_back(matchesByLen[matchLen+1]); |
| 114 |
} |
| 115 |
|
| 116 |
matchesByLen[matchLen+1]=m; |
| 117 |
m=NULL; |
| 118 |
|
| 119 |
} else { |
| 120 |
// tally up charsWithinNeedle |
| 121 |
if (matchLen>0) { |
| 122 |
matchesByLen[matchLen]->score.charsWithinNeedle++; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
if (matchesByLen[needleLen]) { |
| 129 |
if (result) *result=*matchesByLen[needleLen]; |
| 130 |
return true; |
| 131 |
} |
| 132 |
|
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
#ifdef RUN_MATCH_TEST |
| 137 |
static void matchFuzzyTest() { |
| 138 |
String hay="a__i_a_i__o"; |
| 139 |
String needle="aio"; |
| 140 |
MatchResult match; |
| 141 |
matchFuzzy(hay.c_str(), hay.length(), needle.c_str(), needle.length(), &match); |
| 142 |
logI( "match.score.charsWithinNeedle: %d", match.score.charsWithinNeedle ); |
| 143 |
} |
| 144 |
#endif |
| 145 |
|
| 146 |
void FurnaceGUI::drawPalette() { |
| 147 |
bool accepted=false; |
| 148 |
|
| 149 |
if (paletteFirstFrame && !mobileUI) |
| 150 |
ImGui::SetKeyboardFocusHere(); |
| 151 |
|
| 152 |
int width=ImGui::GetContentRegionAvail().x; |
| 153 |
ImGui::SetNextItemWidth(width); |
| 154 |
|
| 155 |
const char* hint=_("Search..."); |
| 156 |
switch (curPaletteType) { |
| 157 |
case CMDPAL_TYPE_RECENT: |
| 158 |
hint=_("Search recent files..."); |
| 159 |
break; |
| 160 |
case CMDPAL_TYPE_INSTRUMENTS: |
| 161 |
hint=_("Search instruments..."); |
| 162 |
break; |
| 163 |
case CMDPAL_TYPE_SAMPLES: |
| 164 |
hint=_("Search samples..."); |
| 165 |
break; |
| 166 |
case CMDPAL_TYPE_INSTRUMENT_CHANGE: |
| 167 |
hint=_("Search instruments (to change to)..."); |
| 168 |
break; |
| 169 |
case CMDPAL_TYPE_ADD_CHIP: |
| 170 |
hint=_("Search chip (to add)..."); |
| 171 |
break; |
| 172 |
} |
| 173 |
|
| 174 |
#ifdef RUN_MATCH_TEST |
| 175 |
matchFuzzyTest(); |
| 176 |
#endif |
| 177 |
|
| 178 |
if (ImGui::InputTextWithHint("##CommandPaletteSearch",hint,&paletteQuery) || paletteFirstFrame) { |
| 179 |
paletteSearchResults.clear(); |
| 180 |
std::vector<MatchScore> matchScores; |
| 181 |
|
| 182 |
auto Evaluate=[&](int i, const char* name, int nameLen) { |
| 183 |
MatchResult result; |
| 184 |
if (matchFuzzy(name, nameLen, paletteQuery.c_str(), paletteQuery.length(), &result)) { |
| 185 |
paletteSearchResults.emplace_back(); |
| 186 |
paletteSearchResults.back().id=i; |
| 187 |
paletteSearchResults.back().highlightChars=std::move(result.highlightChars); |
| 188 |
matchScores.push_back(result.score); |
| 189 |
} |
| 190 |
}; |
| 191 |
|
| 192 |
switch (curPaletteType) { |
| 193 |
case CMDPAL_TYPE_MAIN: |
| 194 |
for (int i=0; i<GUI_ACTION_MAX; i++) { |
| 195 |
if (guiActions[i].isNotABind()) continue; |
| 196 |
Evaluate(i,guiActions[i].friendlyName,strlen(guiActions[i].friendlyName)); |
| 197 |
} |
| 198 |
break; |
| 199 |
|
| 200 |
case CMDPAL_TYPE_RECENT: |
| 201 |
for (int i=0; i<(int)recentFile.size(); i++) { |
| 202 |
Evaluate(i,recentFile[i].c_str(),recentFile[i].length()); |
| 203 |
} |
| 204 |
break; |
| 205 |
|
| 206 |
case CMDPAL_TYPE_INSTRUMENTS: |
| 207 |
case CMDPAL_TYPE_INSTRUMENT_CHANGE: { |
| 208 |
const char* noneStr=_("- None -"); |
| 209 |
Evaluate(0,noneStr,strlen(noneStr)); |
| 210 |
for (int i=0; i<e->song.insLen; i++) { |
| 211 |
String s=fmt::sprintf("%02X: %s", i, e->song.ins[i]->name.c_str()); |
| 212 |
Evaluate(i+1,s.c_str(),s.length()); // because over here ins=0 is 'None' |
| 213 |
} |
| 214 |
break; |
| 215 |
} |
| 216 |
|
| 217 |
case CMDPAL_TYPE_SAMPLES: |
| 218 |
for (int i=0; i<e->song.sampleLen; i++) { |
| 219 |
Evaluate(i,e->song.sample[i]->name.c_str(),e->song.sample[i]->name.length()); |
| 220 |
} |
| 221 |
break; |
| 222 |
|
| 223 |
case CMDPAL_TYPE_ADD_CHIP: |
| 224 |
for (int i=0; availableSystems[i]; i++) { |
| 225 |
int ds=availableSystems[i]; |
| 226 |
const char* sysname=getSystemName((DivSystem)ds); |
| 227 |
Evaluate(ds,sysname,strlen(sysname)); |
| 228 |
} |
| 229 |
break; |
| 230 |
|
| 231 |
default: |
| 232 |
logE(_("invalid command palette type")); |
| 233 |
ImGui::CloseCurrentPopup(); |
| 234 |
break; |
| 235 |
}; |
| 236 |
|
| 237 |
// sort indices by match quality |
| 238 |
std::vector<int> sortingIndices(paletteSearchResults.size()); |
| 239 |
for (size_t i=0; i<sortingIndices.size(); ++i) sortingIndices[i]=(int)i; |
| 240 |
std::sort(sortingIndices.begin(), sortingIndices.end(), [&](size_t a, size_t b) { |
| 241 |
return MatchScore::IsFirstPreferable(matchScores[a], matchScores[b]); |
| 242 |
}); |
| 243 |
|
| 244 |
// update paletteSearchResults from sorted indices (taking care not to stomp while we iterate |
| 245 |
std::vector<PaletteSearchResult> paletteSearchResultsCopy=paletteSearchResults; |
| 246 |
for (size_t i=0; i<sortingIndices.size(); ++i) paletteSearchResults[i]=paletteSearchResultsCopy[sortingIndices[i]]; |
| 247 |
} |
| 248 |
|
| 249 |
ImVec2 avail=ImGui::GetContentRegionAvail(); |
| 250 |
avail.y-=ImGui::GetFrameHeightWithSpacing(); |
| 251 |
|
| 252 |
if (ImGui::BeginChild("CommandPaletteList",avail,0,0)) { |
| 253 |
bool navigated=false; |
| 254 |
if (ImGui::IsKeyPressed(ImGuiKey_UpArrow) && curPaletteChoice>0) { |
| 255 |
curPaletteChoice-=1; |
| 256 |
navigated=true; |
| 257 |
} |
| 258 |
if (ImGui::IsKeyPressed(ImGuiKey_DownArrow)) { |
| 259 |
curPaletteChoice+=1; |
| 260 |
navigated=true; |
| 261 |
} |
| 262 |
|
| 263 |
if (paletteSearchResults.size()>0 && curPaletteChoice<0) { |
| 264 |
curPaletteChoice=0; |
| 265 |
navigated=true; |
| 266 |
} |
| 267 |
if (curPaletteChoice>=(int)paletteSearchResults.size()) { |
| 268 |
curPaletteChoice=paletteSearchResults.size()-1; |
| 269 |
navigated=true; |
| 270 |
} |
| 271 |
|
| 272 |
int columnCount=curPaletteType==CMDPAL_TYPE_MAIN ? 2 : 1; |
| 273 |
if (ImGui::BeginTable("##commandPaletteTable",columnCount,ImGuiTableFlags_SizingStretchProp)) { |
| 274 |
// ImGui::TableSetupColumn("##action",ImGuiTableColumnFlags_WidthStretch); |
| 275 |
// ImGui::TableSetupColumn("##shortcut"); |
| 276 |
for (int i=0; i<(int)paletteSearchResults.size(); i++) { |
| 277 |
bool current=(i==curPaletteChoice); |
| 278 |
int id=paletteSearchResults[i].id; |
| 279 |
|
| 280 |
String s="???"; |
| 281 |
switch (curPaletteType) { |
| 282 |
case CMDPAL_TYPE_MAIN: |
| 283 |
s=guiActions[id].friendlyName; |
| 284 |
break; |
| 285 |
case CMDPAL_TYPE_RECENT: |
| 286 |
s=recentFile[id].c_str(); |
| 287 |
break; |
| 288 |
case CMDPAL_TYPE_INSTRUMENTS: |
| 289 |
case CMDPAL_TYPE_INSTRUMENT_CHANGE: |
| 290 |
if (id==0) { |
| 291 |
s=_("- None -"); |
| 292 |
} else { |
| 293 |
s=fmt::sprintf("%02X: %s", id-1, e->song.ins[id-1]->name.c_str()); |
| 294 |
} |
| 295 |
break; |
| 296 |
case CMDPAL_TYPE_SAMPLES: |
| 297 |
s=e->song.sample[id]->name.c_str(); |
| 298 |
break; |
| 299 |
case CMDPAL_TYPE_ADD_CHIP: |
| 300 |
s=getSystemName((DivSystem)id); |
| 301 |
break; |
| 302 |
default: |
| 303 |
logE(_("invalid command palette type")); |
| 304 |
break; |
| 305 |
}; |
| 306 |
|
| 307 |
ImGui::TableNextRow(); |
| 308 |
ImGui::TableNextColumn(); |
| 309 |
|
| 310 |
ImGui::PushID(s.c_str()); |
| 311 |
bool selectable=ImGui::Selectable("##paletteSearchItem",current,ImGuiSelectableFlags_SpanAllColumns|ImGuiSelectableFlags_AllowOverlap); |
| 312 |
const char* str=s.c_str(); |
| 313 |
size_t chCursor=0; |
| 314 |
const std::vector<int>& highlights=paletteSearchResults[i].highlightChars; |
| 315 |
for (size_t ch=0; ch<highlights.size(); ch++) { |
| 316 |
ImGui::SameLine(0.0f,0.0f); |
| 317 |
ImGui::Text("%.*s", (int)(highlights[ch]-chCursor), str+chCursor); |
| 318 |
ImGui::SameLine(0.0f,0.0f); |
| 319 |
ImGui::TextColored(uiColors[GUI_COLOR_ACCENT_PRIMARY], "%.1s", str+highlights[ch]); |
| 320 |
chCursor=highlights[ch]+1; |
| 321 |
} |
| 322 |
ImGui::SameLine(0.0f,0.0f); |
| 323 |
ImGui::Text("%.*s", (int)(s.length()-chCursor), str+chCursor); |
| 324 |
|
| 325 |
if (curPaletteType==CMDPAL_TYPE_MAIN) { |
| 326 |
ImGui::TableNextColumn(); |
| 327 |
ImGui::TextColored(uiColors[GUI_COLOR_TEXT_DISABLED], "%s", getMultiKeysName(actionKeys[paletteSearchResults[i].id].data(),actionKeys[paletteSearchResults[i].id].size(),true).c_str()); |
| 328 |
} |
| 329 |
|
| 330 |
if (selectable) { |
| 331 |
curPaletteChoice=i; |
| 332 |
accepted=true; |
| 333 |
} |
| 334 |
|
| 335 |
ImGui::PopID(); |
| 336 |
|
| 337 |
if ((navigated || paletteFirstFrame) && current) ImGui::SetScrollHereY(); |
| 338 |
} |
| 339 |
ImGui::EndTable(); |
| 340 |
} |
| 341 |
} |
| 342 |
ImGui::EndChild(); |
| 343 |
|
| 344 |
if (!accepted) { |
| 345 |
if (curPaletteChoice>=(int)paletteSearchResults.size()) { |
| 346 |
curPaletteChoice=paletteSearchResults.size()-1; |
| 347 |
} |
| 348 |
accepted=ImGui::IsKeyPressed(ImGuiKey_Enter); |
| 349 |
} |
| 350 |
|
| 351 |
if (ImGui::Button(_("Cancel")) || ImGui::IsKeyPressed(ImGuiKey_Escape)) { |
| 352 |
ImGui::CloseCurrentPopup(); |
| 353 |
} |
| 354 |
|
| 355 |
// do not move this to after the resetPalette() calls! |
| 356 |
// 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. |
| 357 |
paletteFirstFrame=false; |
| 358 |
|
| 359 |
if (accepted) { |
| 360 |
if (paletteSearchResults.size()>0) { |
| 361 |
int i=paletteSearchResults[curPaletteChoice].id; |
| 362 |
switch (curPaletteType) { |
| 363 |
case CMDPAL_TYPE_MAIN: |
| 364 |
doAction(i); |
| 365 |
break; |
| 366 |
case CMDPAL_TYPE_RECENT: |
| 367 |
openRecentFile(recentFile[i]); |
| 368 |
break; |
| 369 |
case CMDPAL_TYPE_INSTRUMENTS: |
| 370 |
curIns=i-1; |
| 371 |
break; |
| 372 |
case CMDPAL_TYPE_SAMPLES: |
| 373 |
curSample=i; |
| 374 |
break; |
| 375 |
case CMDPAL_TYPE_INSTRUMENT_CHANGE: |
| 376 |
doChangeIns(i-1); |
| 377 |
break; |
| 378 |
case CMDPAL_TYPE_ADD_CHIP: |
| 379 |
if (i!=DIV_SYSTEM_NULL) { |
| 380 |
if (!e->addSystem((DivSystem)i)) { |
| 381 |
showError("cannot add chip! ("+e->getLastError()+")"); |
| 382 |
} else { |
| 383 |
MARK_MODIFIED; |
| 384 |
} |
| 385 |
ImGui::CloseCurrentPopup(); |
| 386 |
if (e->song.autoSystem) { |
| 387 |
autoDetectSystem(); |
| 388 |
} |
| 389 |
updateWindowTitle(); |
| 390 |
} |
| 391 |
break; |
| 392 |
default: |
| 393 |
logE(_("invalid command palette type")); |
| 394 |
break; |
| 395 |
}; |
| 396 |
} |
| 397 |
ImGui::CloseCurrentPopup(); |
| 398 |
} |
| 399 |
} |