1
0
mirror of synced 2025-02-20 04:01:01 +01:00

Added registry for command palette commands

This commit is contained in:
WerWolv 2021-01-11 23:02:55 +01:00
parent 81652e3650
commit 90e0aa83d8
4 changed files with 94 additions and 48 deletions

View File

@ -39,6 +39,25 @@ namespace hex {
static auto get(std::string_view name);
};
struct CommandPaletteCommands {
CommandPaletteCommands() = delete;
enum class Type : u32 {
SymbolCommand,
KeywordCommand
};
struct Entry {
Type type;
std::string command;
std::string description;
std::function<std::string(std::string)> callback;
};
static void add(Type type, std::string_view command, std::string_view description, const std::function<std::string(std::string)> &callback);
static std::vector<Entry> getEntries();
};
};
}

View File

@ -58,34 +58,35 @@ namespace hex {
static nlohmann::json settingsJsonStorage;
static std::map<std::string, Events> customEventsStorage;
static u32 customEventsLastIdStorage = u32(Events::Events_BuiltinEnd) + 1;
static std::vector<ContentRegistry::CommandPaletteCommands::Entry> commandPaletteCommandsStorage;
this->imguiContext = ImGui::GetCurrentContext();
this->eventHandlers = &eventHandlersStorage;
this->deferredCalls = &deferredCallsStorage;
this->currentProvider = &currentProviderStorage;
this->settingsEntries = &settingsEntriesStorage;
this->sharedVariables = &sharedVariablesStorage;
this->settingsJson = &settingsJsonStorage;
this->customEvents = &customEventsStorage;
this->customEventsLastId = &customEventsLastIdStorage;
this->windowPos = &windowPosStorage;
this->windowSize = &windowSizeStorage;
this->imguiContext = ImGui::GetCurrentContext();
this->eventHandlers = &eventHandlersStorage;
this->deferredCalls = &deferredCallsStorage;
this->currentProvider = &currentProviderStorage;
this->settingsEntries = &settingsEntriesStorage;
this->sharedVariables = &sharedVariablesStorage;
this->windowPos = &windowPosStorage;
this->windowSize = &windowSizeStorage;
this->settingsJson = &settingsJsonStorage;
this->customEvents = &customEventsStorage;
this->customEventsLastId = &customEventsLastIdStorage;
this->commandPaletteCommands = &commandPaletteCommandsStorage;
}
void initializeData(const SharedData &other) {
this->imguiContext = other.imguiContext;
this->eventHandlers = other.eventHandlers;
this->deferredCalls = other.deferredCalls;
this->currentProvider = other.currentProvider;
this->settingsEntries = other.settingsEntries;
this->sharedVariables = other.sharedVariables;
this->settingsJson = other.settingsJson;
this->customEvents = other.customEvents;
this->customEventsLastId = other.customEventsLastId;
this->windowPos = other.windowPos;
this->windowSize = other.windowSize;
this->imguiContext = other.imguiContext;
this->eventHandlers = other.eventHandlers;
this->deferredCalls = other.deferredCalls;
this->currentProvider = other.currentProvider;
this->settingsEntries = other.settingsEntries;
this->sharedVariables = other.sharedVariables;
this->windowPos = other.windowPos;
this->windowSize = other.windowSize;
this->settingsJson = other.settingsJson;
this->customEvents = other.customEvents;
this->customEventsLastId = other.customEventsLastId;
this->commandPaletteCommands = other.commandPaletteCommands;
}
public:
@ -97,6 +98,7 @@ namespace hex {
nlohmann::json *settingsJson;
std::map<std::string, Events> *customEvents;
u32 *customEventsLastId;
std::vector<ContentRegistry::CommandPaletteCommands::Entry> *commandPaletteCommands;
ImVec2 *windowPos;
ImVec2 *windowSize;

View File

@ -62,4 +62,15 @@ namespace hex {
return customEvents[name.data()];
}
/* Command Palette Commands */
void ContentRegistry::CommandPaletteCommands::add(ContentRegistry::CommandPaletteCommands::Type type, std::string_view command, std::string_view description, const std::function<std::string(std::string)> &callback) {
SharedData::get().commandPaletteCommands->push_back(ContentRegistry::CommandPaletteCommands::Entry{ type, command.data(), description.data(), callback });
}
std::vector<ContentRegistry::CommandPaletteCommands::Entry> ContentRegistry::CommandPaletteCommands::getEntries() {
return *SharedData::get().commandPaletteCommands;
}
}

View File

@ -10,6 +10,27 @@ namespace hex {
this->getWindowOpenState() = true;
this->m_commandBuffer.resize(1024, 0x00);
ContentRegistry::CommandPaletteCommands::add(
ContentRegistry::CommandPaletteCommands::Type::SymbolCommand,
"#", "Calculator",
[](std::string input) {
MathEvaluator evaluator;
evaluator.registerStandardVariables();
evaluator.registerStandardFunctions();
std::optional<long double> result;
try {
result = evaluator.evaluate(input);
} catch (std::runtime_error &e) {}
if (result.has_value())
return hex::format("#%s = %Lf", input.data(), result.value());
else
return hex::format("#%s = ???", input.data());
});
this->m_lastResults = this->getCommandResults("");
}
@ -77,7 +98,7 @@ namespace hex {
PerfectMatch
};
std::vector<std::string> ViewCommandPalette::getCommandResults(std::string_view command) {
std::vector<std::string> ViewCommandPalette::getCommandResults(std::string_view input) {
constexpr auto matchCommand = [](std::string_view currCommand, std::string_view commandToMatch) -> std::pair<MatchType, std::string_view> {
if (currCommand.empty()) {
return { MatchType::InfoMatch, "" };
@ -98,31 +119,24 @@ namespace hex {
std::vector<std::string> results;
if (auto [match, value] = matchCommand(command, "#"); match != MatchType::NoMatch) {
if (match != MatchType::PerfectMatch)
results.emplace_back("# (Calculator)");
else {
MathEvaluator evaluator;
evaluator.registerStandardVariables();
evaluator.registerStandardFunctions();
for (const auto &[type, command, description, callback] : ContentRegistry::CommandPaletteCommands::getEntries()) {
auto result = evaluator.evaluate(std::string(value));
if (type == ContentRegistry::CommandPaletteCommands::Type::SymbolCommand) {
if (auto [match, value] = matchCommand(input, command); match != MatchType::NoMatch) {
if (match != MatchType::PerfectMatch)
results.emplace_back(command + " (" + description + ")");
else
results.emplace_back(callback(input.substr(command.length()).data()));
}
} else if (type == ContentRegistry::CommandPaletteCommands::Type::KeywordCommand) {
if (auto [match, value] = matchCommand(input, command + " "); match != MatchType::NoMatch) {
if (match != MatchType::PerfectMatch)
results.emplace_back(command + " (" + description + ")");
else
results.emplace_back(callback(input.substr(command.length() + 1).data()));
}
}
if (result.has_value())
results.emplace_back(hex::format("#%s = %Lf", value.data(), result.value()));
else
results.emplace_back(hex::format("#%s = ???", value.data()));
}
}
if (auto [match, value] = matchCommand(command, "/find "); match != MatchType::NoMatch) {
if (match != MatchType::PerfectMatch)
results.emplace_back("/find (Find Command)");
else {
results.emplace_back(hex::format("Command: Find \"%s\"", value.data()));
}
}
if (auto [match, value] = matchCommand(command, ">"); match != MatchType::NoMatch) {
results.emplace_back("> (Command)");
}
return results;