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 { {.keyword = "SHADER", .execute = [](const std::vector<std::string>& args) -> std::string {
if (args.empty()) { if (args.empty()) {
Screen::get()->toggleShaders(); 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 (args[0] == "ON") {
if (Options::video.postfx) { return "Shader already ON"; } if (Options::video.postfx) { return "Shader already ON"; }
@@ -1133,26 +1133,71 @@ void Console::handleEvent(const SDL_Event& event) {
} }
break; break;
case SDL_SCANCODE_TAB: { case SDL_SCANCODE_TAB: {
// Calcular prefijo (input actual en mayúsculas) if (tab_matches_.empty()) {
std::string prefix; // Calcular el input actual en mayúsculas
for (unsigned char c : input_line_) { prefix += static_cast<char>(std::toupper(c)); } std::string upper;
// Regenerar lista si el prefijo ha cambiado o la lista está vacía for (unsigned char c : input_line_) { upper += static_cast<char>(std::toupper(c)); }
if (tab_matches_.empty() || tab_prefix_ != prefix) {
tab_matches_.clear(); const size_t space_pos = upper.rfind(' ');
tab_prefix_ = prefix; if (space_pos == std::string::npos) {
for (const auto& cmd : COMMANDS) { // Modo comando: ciclar keywords que empiecen por el prefijo
if (cmd.hidden) { continue; } for (const auto& cmd : COMMANDS) {
if (prefix.empty() || cmd.keyword.starts_with(prefix)) { if (cmd.hidden) { continue; }
tab_matches_.push_back(std::string(cmd.keyword)); 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; tab_index_ = -1;
} }
if (tab_matches_.empty()) { break; } if (tab_matches_.empty()) { break; }
tab_index_ = (tab_index_ + 1) % static_cast<int>(tab_matches_.size()); tab_index_ = (tab_index_ + 1) % static_cast<int>(tab_matches_.size());
std::string lower = tab_matches_[static_cast<size_t>(tab_index_)]; std::string result = tab_matches_[static_cast<size_t>(tab_index_)];
for (char& c : lower) { c = static_cast<char>(std::tolower(static_cast<unsigned char>(c))); } for (char& c : result) { c = static_cast<char>(std::tolower(static_cast<unsigned char>(c))); }
input_line_ = lower; input_line_ = result;
break; break;
} }
default: default:

View File

@@ -49,7 +49,7 @@ class Console {
// Constantes de consola // Constantes de consola
static constexpr std::string_view CONSOLE_NAME = "JDD Console"; static constexpr std::string_view CONSOLE_NAME = "JDD Console";
static constexpr std::string_view CONSOLE_VERSION = "v2.0"; static constexpr std::string_view CONSOLE_VERSION = "v2.1";
static constexpr int MAX_LINE_CHARS = 32; static constexpr int MAX_LINE_CHARS = 32;
static constexpr int MAX_HISTORY_SIZE = 20; static constexpr int MAX_HISTORY_SIZE = 20;
static constexpr float CURSOR_ON_TIME = 0.5F; static constexpr float CURSOR_ON_TIME = 0.5F;
@@ -101,5 +101,4 @@ class Console {
// Estado de autocompletado (TAB) // Estado de autocompletado (TAB)
std::vector<std::string> tab_matches_; // Comandos que coinciden con el prefijo actual std::vector<std::string> tab_matches_; // Comandos que coinciden con el prefijo actual
int tab_index_{-1}; // Índice actual en tab_matches_ int tab_index_{-1}; // Índice actual en tab_matches_
std::string tab_prefix_; // Prefijo usado en la última búsqueda TAB
}; };