autocompletar amb armadura de lagarto

This commit is contained in:
2026-03-30 22:50:56 +02:00
parent 8f191f02fa
commit 8dcf473f31
2 changed files with 61 additions and 17 deletions

View File

@@ -202,7 +202,7 @@ static const std::vector<ConsoleCommand> COMMANDS = {
{.keyword = "SHADER", .execute = [](const std::vector<std::string>& args) -> std::string {
if (args.empty()) {
Screen::get()->toggleShaders();
return std::string("Shaders ") + (Options::video.postfx ? "ON" : "OFF");
return std::string("Shader ") + (Options::video.postfx ? "ON" : "OFF");
}
if (args[0] == "ON") {
if (Options::video.postfx) { return "Shader already ON"; }
@@ -1133,26 +1133,71 @@ void Console::handleEvent(const SDL_Event& event) {
}
break;
case SDL_SCANCODE_TAB: {
// Calcular prefijo (input actual en mayúsculas)
std::string prefix;
for (unsigned char c : input_line_) { prefix += static_cast<char>(std::toupper(c)); }
// Regenerar lista si el prefijo ha cambiado o la lista está vacía
if (tab_matches_.empty() || tab_prefix_ != prefix) {
tab_matches_.clear();
tab_prefix_ = prefix;
for (const auto& cmd : COMMANDS) {
if (cmd.hidden) { continue; }
if (prefix.empty() || cmd.keyword.starts_with(prefix)) {
tab_matches_.push_back(std::string(cmd.keyword));
if (tab_matches_.empty()) {
// Calcular el input actual en mayúsculas
std::string upper;
for (unsigned char c : input_line_) { upper += static_cast<char>(std::toupper(c)); }
const size_t space_pos = upper.rfind(' ');
if (space_pos == std::string::npos) {
// Modo comando: ciclar keywords que empiecen por el prefijo
for (const auto& cmd : COMMANDS) {
if (cmd.hidden) { continue; }
if (upper.empty() || cmd.keyword.starts_with(upper)) {
tab_matches_.emplace_back(cmd.keyword);
}
}
} else {
// Modo sub-argumento: completar primer arg del comando base
using SubArgs = std::vector<std::string_view>;
using Entry = std::pair<std::string_view, SubArgs>;
static const std::vector<Entry> SUB_ARGS = {
{"SS", {"ON", "OFF", "SIZE", "UPSCALE", "DOWNSCALE"}},
{"SHADER", {"ON", "OFF", "NEXT", "POSTFX", "CRTPI"}},
{"BORDER", {"ON", "OFF"}},
{"FULLSCREEN", {"ON", "OFF"}},
{"ZOOM", {"UP", "DOWN"}},
{"INTSCALE", {"ON", "OFF"}},
{"VSYNC", {"ON", "OFF"}},
{"DRIVER", {"LIST", "AUTO", "NONE"}},
{"PALETTE", {"NEXT", "PREV"}},
{"AUDIO", {"ON", "OFF", "VOL"}},
{"MUSIC", {"ON", "OFF", "VOL"}},
{"SOUND", {"ON", "OFF", "VOL"}},
#ifdef _DEBUG
{"SHOW", {"INFO", "NOTIFICATION", "CHEEVO"}},
{"SET", {"PLAYER", "INITIAL", "ITEMS"}},
{"DEBUG", {"ON", "OFF"}},
{"ROOM", {"NEXT", "PREV"}},
{"SCENE", {"LOGO", "LOADING", "TITLE", "CREDITS", "GAME", "ENDING", "ENDING2", "RESTART"}},
#else
{"SHOW", {"INFO"}},
{"SET", {"PLAYER"}},
#endif
{"HIDE", {"INFO"}},
{"KIOSK", {"ON"}},
{"CHEAT", {"INFINITE", "INVINCIBILITY", "OPEN", "CLOSE"}},
};
const std::string base_cmd = upper.substr(0, space_pos);
const std::string sub_prefix = upper.substr(space_pos + 1);
for (const auto& [keyword, args] : SUB_ARGS) {
if (keyword == base_cmd) {
for (const auto& arg : args) {
if (sub_prefix.empty() || std::string_view{arg}.starts_with(sub_prefix)) {
tab_matches_.emplace_back(std::string(base_cmd) + " " + std::string(arg));
}
}
break;
}
}
}
tab_index_ = -1;
}
if (tab_matches_.empty()) { break; }
tab_index_ = (tab_index_ + 1) % static_cast<int>(tab_matches_.size());
std::string lower = tab_matches_[static_cast<size_t>(tab_index_)];
for (char& c : lower) { c = static_cast<char>(std::tolower(static_cast<unsigned char>(c))); }
input_line_ = lower;
std::string result = tab_matches_[static_cast<size_t>(tab_index_)];
for (char& c : result) { c = static_cast<char>(std::tolower(static_cast<unsigned char>(c))); }
input_line_ = result;
break;
}
default: