ui: Added API to add custom layouts, imhex application and api cleanup

This commit is contained in:
WerWolv
2022-01-18 00:10:10 +01:00
parent c4cbcc7232
commit ee8b665472
28 changed files with 310 additions and 143 deletions

View File

@@ -113,13 +113,14 @@ namespace hex {
}
template<hex::derived_from<View> T, typename ... Args>
void add(Args&& ... args) {
return impl::add(new T(std::forward<Args>(args)...));
}
std::vector<View*>& getEntries();
std::map<std::string, View*>& getEntries();
View* getViewByName(const std::string &unlocalizedName);
}
@@ -215,15 +216,39 @@ namespace hex {
/* Interface Registry. Allows adding new items to various interfaces */
namespace Interface {
using DrawCallback = std::function<void()>;
void addWelcomeScreenEntry(const DrawCallback &function);
void addFooterItem(const DrawCallback &function);
void addToolbarItem(const DrawCallback &function);
namespace impl {
std::vector<DrawCallback>& getWelcomeScreenEntries();
std::vector<DrawCallback>& getFooterItems();
std::vector<DrawCallback>& getToolbarItems();
using DrawCallback = std::function<void()>;
using LayoutFunction = std::function<void(u32)>;
struct Layout {
std::string unlocalizedName;
LayoutFunction callback;
};
struct MainMenuItem {
std::string unlocalizedName;
DrawCallback callback;
};
}
u32 getDockSpaceId();
void registerMainMenuItem(const std::string &unlocalizedName, const impl::DrawCallback &function = []{});
void addWelcomeScreenEntry(const impl::DrawCallback &function);
void addFooterItem(const impl::DrawCallback &function);
void addToolbarItem(const impl::DrawCallback &function);
void addLayout(const std::string &unlocalizedName, const impl::LayoutFunction &function);
std::vector<impl::MainMenuItem>& getMainMenuItems();
std::vector<impl::DrawCallback>& getWelcomeScreenEntries();
std::vector<impl::DrawCallback>& getFooterItems();
std::vector<impl::DrawCallback>& getToolbarItems();
std::vector<impl::Layout>& getLayouts();
}
/* Provider Registry. Allows adding new data providers to be created from the UI */

View File

@@ -111,6 +111,8 @@ namespace hex {
EVENT_DEF(EventAbnormalTermination, int);
EVENT_DEF(EventOSThemeChanged);
EVENT_DEF(EventProviderCreated, prv::Provider*);
EVENT_DEF(EventFrameBegin);
EVENT_DEF(EventFrameEnd);
EVENT_DEF(RequestOpenWindow, std::string);
EVENT_DEF(RequestSelectionChange, Region);

View File

@@ -63,7 +63,7 @@ namespace hex {
static nlohmann::json settingsJson;
static std::vector<ContentRegistry::CommandPaletteCommands::Entry> commandPaletteCommands;
static std::map<std::string, ContentRegistry::PatternLanguage::Function> patternLanguageFunctions;
static std::vector<View*> views;
static std::map<std::string, View*> views;
static std::vector<ContentRegistry::Tools::impl::Entry> toolsEntries;
static std::vector<ContentRegistry::DataInspector::impl::Entry> dataInspectorEntries;
static u32 patternPaletteOffset;
@@ -75,9 +75,13 @@ namespace hex {
static std::map<std::string, std::vector<LanguageDefinition>> languageDefinitions;
static std::map<std::string, std::string> loadedLanguageStrings;
static std::vector<ContentRegistry::Interface::DrawCallback> welcomeScreenEntries;
static std::vector<ContentRegistry::Interface::DrawCallback> footerItems;
static std::vector<ContentRegistry::Interface::DrawCallback> toolbarItems;
static ImGuiID dockSpaceId;
static std::vector<ContentRegistry::Interface::impl::MainMenuItem> mainMenuItems;
static std::vector<ContentRegistry::Interface::impl::DrawCallback> welcomeScreenEntries;
static std::vector<ContentRegistry::Interface::impl::DrawCallback> footerItems;
static std::vector<ContentRegistry::Interface::impl::DrawCallback> toolbarItems;
static std::vector<ContentRegistry::Interface::impl::Layout> layouts;
static std::map<Shortcut, std::function<void()>> globalShortcuts;

View File

@@ -53,6 +53,7 @@ namespace hex {
const bool& getWindowOpenState() const;
[[nodiscard]] const std::string& getUnlocalizedName() const;
[[nodiscard]] std::string getName() const;
static void confirmButtons(const std::string &textLeft, const std::string &textRight, const std::function<void()> &leftButtonFn, const std::function<void()> &rightButtonFn);
static void discardNavigationRequests();

View File

@@ -206,13 +206,22 @@ namespace hex {
void ContentRegistry::Views::impl::add(View *view) {
log::info("Registered new view: {}", view->getUnlocalizedName());
getEntries().emplace_back(view);
getEntries().insert({ view->getUnlocalizedName(), view });
}
std::vector<View*>& ContentRegistry::Views::getEntries() {
std::map<std::string, View*>& ContentRegistry::Views::getEntries() {
return SharedData::views;
}
View *ContentRegistry::Views::getViewByName(const std::string &unlocalizedName) {
auto &views = getEntries();
if (views.contains(unlocalizedName))
return views[unlocalizedName];
else
return nullptr;
}
/* Tools */
@@ -281,29 +290,53 @@ namespace hex {
/* Interface */
void ContentRegistry::Interface::addWelcomeScreenEntry(const ContentRegistry::Interface::DrawCallback &function) {
u32 ContentRegistry::Interface::getDockSpaceId() {
return SharedData::dockSpaceId;
}
void ContentRegistry::Interface::registerMainMenuItem(const std::string &unlocalizedName, const impl::DrawCallback &function) {
log::info("Registered new main menu item: {}", unlocalizedName);
getMainMenuItems().push_back({ unlocalizedName, function });
}
void ContentRegistry::Interface::addWelcomeScreenEntry(const ContentRegistry::Interface::impl::DrawCallback &function) {
getWelcomeScreenEntries().push_back(function);
}
void ContentRegistry::Interface::addFooterItem(const ContentRegistry::Interface::DrawCallback &function){
void ContentRegistry::Interface::addFooterItem(const ContentRegistry::Interface::impl::DrawCallback &function){
getFooterItems().push_back(function);
}
void ContentRegistry::Interface::addToolbarItem(const ContentRegistry::Interface::DrawCallback &function){
void ContentRegistry::Interface::addToolbarItem(const ContentRegistry::Interface::impl::DrawCallback &function){
getToolbarItems().push_back(function);
}
void ContentRegistry::Interface::addLayout(const std::string &unlocalizedName, const impl::LayoutFunction &function) {
log::info("Added new layout: {}", unlocalizedName);
std::vector<ContentRegistry::Interface::DrawCallback>& ContentRegistry::Interface::getWelcomeScreenEntries() {
getLayouts().push_back({ unlocalizedName, function });
}
std::vector<ContentRegistry::Interface::impl::MainMenuItem> &ContentRegistry::Interface::getMainMenuItems() {
return SharedData::mainMenuItems;
}
std::vector<ContentRegistry::Interface::impl::DrawCallback>& ContentRegistry::Interface::getWelcomeScreenEntries() {
return SharedData::welcomeScreenEntries;
}
std::vector<ContentRegistry::Interface::DrawCallback>& ContentRegistry::Interface::getFooterItems() {
std::vector<ContentRegistry::Interface::impl::DrawCallback>& ContentRegistry::Interface::getFooterItems() {
return SharedData::footerItems;
}
std::vector<ContentRegistry::Interface::DrawCallback>& ContentRegistry::Interface::getToolbarItems() {
std::vector<ContentRegistry::Interface::impl::DrawCallback>& ContentRegistry::Interface::getToolbarItems() {
return SharedData::toolbarItems;
}
std::vector<ContentRegistry::Interface::impl::Layout>& ContentRegistry::Interface::getLayouts() {
return SharedData::layouts;
}
/* Providers */

View File

@@ -13,7 +13,7 @@ namespace hex {
nlohmann::json SharedData::settingsJson;
std::vector<ContentRegistry::CommandPaletteCommands::Entry> SharedData::commandPaletteCommands;
std::map<std::string, ContentRegistry::PatternLanguage::Function> SharedData::patternLanguageFunctions;
std::vector<View*> SharedData::views;
std::map<std::string, View*> SharedData::views;
std::vector<ContentRegistry::Tools::impl::Entry> SharedData::toolsEntries;
std::vector<ContentRegistry::DataInspector::impl::Entry> SharedData::dataInspectorEntries;
u32 SharedData::patternPaletteOffset;
@@ -25,9 +25,13 @@ namespace hex {
std::map<std::string, std::vector<LanguageDefinition>> SharedData::languageDefinitions;
std::map<std::string, std::string> SharedData::loadedLanguageStrings;
std::vector<ContentRegistry::Interface::DrawCallback> SharedData::welcomeScreenEntries;
std::vector<ContentRegistry::Interface::DrawCallback> SharedData::footerItems;
std::vector<ContentRegistry::Interface::DrawCallback> SharedData::toolbarItems;
ImGuiID SharedData::dockSpaceId;
std::vector<ContentRegistry::Interface::impl::MainMenuItem> SharedData::mainMenuItems;
std::vector<ContentRegistry::Interface::impl::DrawCallback> SharedData::welcomeScreenEntries;
std::vector<ContentRegistry::Interface::impl::DrawCallback> SharedData::footerItems;
std::vector<ContentRegistry::Interface::impl::DrawCallback> SharedData::toolbarItems;
std::vector<ContentRegistry::Interface::impl::Layout> SharedData::layouts;
std::map<Shortcut, std::function<void()>> SharedData::globalShortcuts;

View File

@@ -105,6 +105,10 @@ namespace hex {
return this->m_unlocalizedViewName;
}
std::string View::getName() const {
return View::toWindowName(this->m_unlocalizedViewName);
}
void View::discardNavigationRequests() {
if (ImGui::IsWindowFocused(ImGuiFocusedFlags_ChildWindows))
ImGui::GetIO().ConfigFlags &= ~ImGuiConfigFlags_NavEnableKeyboard;