1
0
mirror of synced 2024-11-28 09:30:51 +01:00

ui: Added toolbar

This commit is contained in:
WerWolv 2021-08-21 00:52:11 +02:00
parent 0f45bef980
commit 37d2d58d2f
13 changed files with 133 additions and 79 deletions

View File

@ -28,7 +28,7 @@ namespace hex {
friend void ImHexSettingsHandler_ApplyAll(ImGuiContext *ctx, ImGuiSettingsHandler *handler);
friend void ImHexSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandler *handler, ImGuiTextBuffer *buf);
bool setFont(const std::filesystem::path &font_path);
void setFont(const std::filesystem::path &font_path);
private:
void setupNativeWindow();

View File

@ -12,7 +12,7 @@ add_library(${PROJECT_NAME} SHARED
source/content/settings_entries.cpp
source/content/tools_entries.cpp
source/content/data_processor_nodes.cpp
source/content/footer_items.cpp
source/content/ui_items.cpp
source/math_evaluator.cpp

View File

@ -1,5 +1,9 @@
#include <hex/plugin.hpp>
#include <codicons_font.h>
#include <imgui.h>
#include <imgui_internal.h>
namespace hex::plugin::builtin {
void addFooterItems() {
@ -15,4 +19,32 @@ namespace hex::plugin::builtin {
}
void addToolbarItems() {
ContentRegistry::Interface::addToolbarItem([] {
const static auto buttonSize = ImVec2(ImGui::GetCurrentWindow()->MenuBarHeight(), ImGui::GetCurrentWindow()->MenuBarHeight());
ImGui::ToolBarButton(ICON_VS_DISCARD, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarBlue), buttonSize);
ImGui::ToolBarButton(ICON_VS_REDO, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarBlue), buttonSize);
ImGui::SeparatorEx(ImGuiSeparatorFlags_Vertical);
ImGui::ToolBarButton(ICON_VS_FILE, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarGray), buttonSize);
ImGui::ToolBarButton(ICON_VS_FOLDER_OPENED, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarBrown), buttonSize);
ImGui::SeparatorEx(ImGuiSeparatorFlags_Vertical);
ImGui::ToolBarButton(ICON_VS_SAVE, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarBlue), buttonSize);
ImGui::ToolBarButton(ICON_VS_SAVE_AS, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarBlue), buttonSize);
ImGui::SeparatorEx(ImGuiSeparatorFlags_Vertical);
ImGui::ToolBarButton(ICON_VS_COPY, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarGray), buttonSize);
ImGui::ToolBarButton(ICON_VS_OUTPUT, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarGray), buttonSize);
ImGui::SeparatorEx(ImGuiSeparatorFlags_Vertical);
ImGui::ToolBarButton(ICON_VS_BOOKMARK, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarGreen), buttonSize);
});
}
}

View File

@ -10,6 +10,7 @@ namespace hex::plugin::builtin {
void registerDataProcessorNodes();
void addFooterItems();
void addToolbarItems();
void registerLanguageEnUS();
void registerLanguageDeDE();
@ -29,6 +30,7 @@ IMHEX_PLUGIN_SETUP("Built-in", "WerWolv", "Default ImHex functionality") {
registerDataProcessorNodes();
addFooterItems();
addToolbarItems();
registerLanguageEnUS();
registerLanguageDeDE();

View File

@ -191,9 +191,11 @@ namespace hex {
static void addWelcomeScreenEntry(const DrawCallback &function);
static void addFooterItem(const DrawCallback &function);
static void addToolbarItem(const DrawCallback &function);
static std::vector<DrawCallback>& getWelcomeScreenEntries();
static std::vector<DrawCallback>& getFooterItems();
static std::vector<DrawCallback>& getToolbarItems();
};
};

View File

@ -72,6 +72,7 @@ namespace hex {
static std::vector<ContentRegistry::Interface::DrawCallback> welcomeScreenEntries;
static std::vector<ContentRegistry::Interface::DrawCallback> footerItems;
static std::vector<ContentRegistry::Interface::DrawCallback> toolbarItems;
static std::vector<ContentRegistry::DataProcessorNode::Entry> dataProcessorNodes;
static u32 dataProcessorNodeIdCounter;

View File

@ -2,6 +2,7 @@
#include <glad/glad.h>
#include <imgui.h>
#include <imgui_internal.h>
#include <hex.hpp>
#include <hex/api/imhex_api.hpp>
@ -20,5 +21,6 @@
[[gnu::visibility("default")]] const char* getPluginName() { return name; } \
[[gnu::visibility("default")]] const char* getPluginAuthor() { return author; } \
[[gnu::visibility("default")]] const char* getPluginDescription() { return description; } \
[[gnu::visibility("default")]] void setImGuiContext(ImGuiContext *ctx) { ImGui::SetCurrentContext(ctx); GImGui = ctx; } \
} \
void hex::plugin::namespaceName::internal::initializePlugin()

View File

@ -233,6 +233,10 @@ namespace hex {
getFooterItems().push_back(function);
}
void ContentRegistry::Interface::addToolbarItem(const ContentRegistry::Interface::DrawCallback &function){
getToolbarItems().push_back(function);
}
std::vector<ContentRegistry::Interface::DrawCallback>& ContentRegistry::Interface::getWelcomeScreenEntries() {
return SharedData::welcomeScreenEntries;
@ -240,4 +244,7 @@ namespace hex {
std::vector<ContentRegistry::Interface::DrawCallback>& ContentRegistry::Interface::getFooterItems() {
return SharedData::footerItems;
}
std::vector<ContentRegistry::Interface::DrawCallback>& ContentRegistry::Interface::getToolbarItems() {
return SharedData::toolbarItems;
}
}

View File

@ -22,6 +22,7 @@ namespace hex {
std::vector<ContentRegistry::Interface::DrawCallback> SharedData::welcomeScreenEntries;
std::vector<ContentRegistry::Interface::DrawCallback> SharedData::footerItems;
std::vector<ContentRegistry::Interface::DrawCallback> SharedData::toolbarItems;
std::vector<ContentRegistry::DataProcessorNode::Entry> SharedData::dataProcessorNodes;
u32 SharedData::dataProcessorNodeIdCounter = 1;

View File

@ -11,6 +11,7 @@ namespace hex {
constexpr auto GetPluginNameSymbol = "_ZN3hex6plugin{0}{1}8internal13getPluginNameEv";
constexpr auto GetPluginAuthorSymbol = "_ZN3hex6plugin{0}{1}8internal15getPluginAuthorEv";
constexpr auto GetPluginDescriptionSymbol = "_ZN3hex6plugin{0}{1}8internal20getPluginDescriptionEv";
constexpr auto SetImGuiContextSymbol = "_ZN3hex6plugin{0}{1}8internal15setImGuiContextEP12ImGuiContext";
Plugin::Plugin(std::string_view path) {
this->m_handle = dlopen(path.data(), RTLD_LAZY);
@ -26,6 +27,7 @@ namespace hex {
this->m_getPluginNameFunction = getPluginFunction<GetPluginNameFunc>(pluginName, GetPluginNameSymbol);
this->m_getPluginAuthorFunction = getPluginFunction<GetPluginAuthorFunc>(pluginName, GetPluginAuthorSymbol);
this->m_getPluginDescriptionFunction = getPluginFunction<GetPluginDescriptionFunc>(pluginName, GetPluginDescriptionSymbol);
this->m_setImGuiContextFunction = getPluginFunction<SetImGuiContextFunc>(pluginName, SetImGuiContextSymbol);
}
Plugin::Plugin(Plugin &&other) noexcept {
@ -34,12 +36,14 @@ namespace hex {
this->m_getPluginNameFunction = other.m_getPluginNameFunction;
this->m_getPluginAuthorFunction = other.m_getPluginAuthorFunction;
this->m_getPluginDescriptionFunction = other.m_getPluginDescriptionFunction;
this->m_setImGuiContextFunction = other.m_setImGuiContextFunction;
other.m_handle = nullptr;
other.m_initializePluginFunction = nullptr;
other.m_getPluginNameFunction = nullptr;
other.m_getPluginAuthorFunction = nullptr;
other.m_getPluginDescriptionFunction = nullptr;
other.m_setImGuiContextFunction = nullptr;
}
Plugin::~Plugin() {
@ -73,6 +77,11 @@ namespace hex {
return "";
}
void Plugin::setImGuiContext(ImGuiContext *ctx) const {
if (this->m_setImGuiContextFunction != nullptr)
this->m_setImGuiContextFunction(ctx);
}
bool PluginManager::load(std::string_view pluginFolder) {
if (!std::filesystem::exists(pluginFolder))
return false;

View File

@ -90,7 +90,7 @@ namespace hex::init {
{
std::lock_guard guard(this->m_progressMutex);
auto drawList = ImGui::GetOverlayDrawList();
auto drawList = ImGui::GetForegroundDrawList();
drawList->AddImage(splashTexture, ImVec2(0, 0), splashTexture.size() * this->m_globalScale);

View File

@ -110,7 +110,7 @@
case RegionBottom | RegionRight:
return HTBOTTOMRIGHT;
case RegionClient:
if ((cursor.y < (window.top + titleBarHeight)) && !ImGui::IsAnyItemHovered())
if ((cursor.y < (window.top + titleBarHeight * 2)) && !ImGui::IsAnyItemHovered())
return HTCAPTION;
else break;
}
@ -137,7 +137,7 @@
}
void Window::drawTitleBar() {
auto buttonSize = ImVec2(titleBarHeight * 1.5F, titleBarHeight);
auto buttonSize = ImVec2(titleBarHeight * 1.5F, titleBarHeight - 1);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));
ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetColorU32(ImGuiCol_MenuBarBg));
@ -146,22 +146,22 @@
ImGui::SetCursorPosX(ImGui::GetWindowWidth() - buttonSize.x * 6);
#if defined(DEBUG)
if (ImGui::Button(ICON_VS_DEBUG, buttonSize))
if (ImGui::TitleBarButton(ICON_VS_DEBUG, buttonSize))
hex::openWebpage("https://imhex.werwolv.net/debug");
ImGui::InfoTooltip("hex.menu.debug_build"_lang);
#endif
if (ImGui::Button(ICON_VS_SMILEY, buttonSize))
if (ImGui::TitleBarButton(ICON_VS_SMILEY, buttonSize))
hex::openWebpage("mailto://hey@werwolv.net");
ImGui::InfoTooltip("hex.menu.feedback"_lang);
ImGui::SetCursorPosX(ImGui::GetWindowWidth() - buttonSize.x * 3);
if (ImGui::Button(ICON_VS_CHROME_MINIMIZE, buttonSize))
if (ImGui::TitleBarButton(ICON_VS_CHROME_MINIMIZE, buttonSize))
glfwIconifyWindow(this->m_window);
if (glfwGetWindowAttrib(this->m_window, GLFW_MAXIMIZED)) {
if (ImGui::Button(ICON_VS_CHROME_RESTORE, buttonSize))
if (ImGui::TitleBarButton(ICON_VS_CHROME_RESTORE, buttonSize))
glfwRestoreWindow(this->m_window);
} else {
if (ImGui::Button(ICON_VS_CHROME_MAXIMIZE, buttonSize))
if (ImGui::TitleBarButton(ICON_VS_CHROME_MAXIMIZE, buttonSize))
glfwMaximizeWindow(this->m_window);
}
@ -169,7 +169,7 @@
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, 0xFF2311E8);
if (ImGui::Button(ICON_VS_CHROME_CLOSE, buttonSize)) {
if (ImGui::TitleBarButton(ICON_VS_CHROME_CLOSE, buttonSize)) {
EventManager::post<RequestCloseImHex>();
EventManager::post<EventWindowClosing>(this->m_window);
}

View File

@ -107,6 +107,9 @@ namespace hex {
}
ImGui::GetStyle().Colors[ImGuiCol_DockingEmptyBg] = ImGui::GetStyle().Colors[ImGuiCol_WindowBg];
ImGui::GetStyle().Colors[ImGuiCol_TitleBg] = ImGui::GetStyle().Colors[ImGuiCol_MenuBarBg];
ImGui::GetStyle().Colors[ImGuiCol_TitleBgActive] = ImGui::GetStyle().Colors[ImGuiCol_MenuBarBg];
ImGui::GetStyle().Colors[ImGuiCol_TitleBgCollapsed] = ImGui::GetStyle().Colors[ImGuiCol_MenuBarBg];
if (!this->m_bannerTexture.valid()) {
log::fatal("Failed to load banner texture!");
@ -214,7 +217,9 @@ namespace hex {
auto signalHandler = [](int signalNumber) {
EventManager::post<EventAbnormalTermination>(signalNumber);
std::abort();
std::signal(signalNumber, SIG_DFL);
std::raise(signalNumber);
};
std::signal(SIGTERM, signalHandler);
@ -255,15 +260,9 @@ namespace hex {
}
}
bool Window::setFont(const std::filesystem::path &path) {
if (!std::filesystem::exists(path))
return false;
void Window::setFont(const std::filesystem::path &path) {
auto &io = ImGui::GetIO();
// If we have a custom font, then rescaling is unnecessary and will make it blurry
io.FontGlobalScale = 1.0f;
// Load font data & build atlas
std::uint8_t *px;
int w, h;
@ -271,15 +270,6 @@ namespace hex {
ImVector<ImWchar> ranges;
ImFontGlyphRangesBuilder glyphRangesBuilder;
glyphRangesBuilder.AddRanges(io.Fonts->GetGlyphRangesDefault());
glyphRangesBuilder.AddRanges(io.Fonts->GetGlyphRangesJapanese());
glyphRangesBuilder.AddRanges(io.Fonts->GetGlyphRangesChineseFull());
glyphRangesBuilder.AddRanges(io.Fonts->GetGlyphRangesCyrillic());
glyphRangesBuilder.AddRanges(io.Fonts->GetGlyphRangesKorean());
glyphRangesBuilder.AddRanges(io.Fonts->GetGlyphRangesThai());
glyphRangesBuilder.AddRanges(io.Fonts->GetGlyphRangesVietnamese());
glyphRangesBuilder.BuildRanges(&ranges);
ImWchar fontAwesomeRange[] = {
ICON_MIN_FA, ICON_MAX_FA,
0
@ -291,16 +281,42 @@ namespace hex {
};
ImFontConfig cfg;
cfg.OversampleH = cfg.OversampleV = 1, cfg.PixelSnapH = true;
cfg.SizePixels = 13.0f * this->m_fontScale;
io.Fonts->AddFontFromFileTTF(path.string().c_str(), std::floor(14.0f * this->m_fontScale), &cfg, ranges.Data); // Needs conversion to char for Windows
if (!std::filesystem::exists(path)) {
// Load default font
io.Fonts->Clear();
cfg.OversampleH = cfg.OversampleV = 1, cfg.PixelSnapH = true;
cfg.SizePixels = 13.0f * this->m_fontScale;
io.Fonts->AddFontDefault(&cfg);
} else {
// Load custom font
// If we have a custom font, then rescaling is unnecessary and will make it blurry
io.FontGlobalScale = 1.0f;
glyphRangesBuilder.AddRanges(io.Fonts->GetGlyphRangesDefault());
glyphRangesBuilder.AddRanges(io.Fonts->GetGlyphRangesJapanese());
glyphRangesBuilder.AddRanges(io.Fonts->GetGlyphRangesChineseFull());
glyphRangesBuilder.AddRanges(io.Fonts->GetGlyphRangesCyrillic());
glyphRangesBuilder.AddRanges(io.Fonts->GetGlyphRangesKorean());
glyphRangesBuilder.AddRanges(io.Fonts->GetGlyphRangesThai());
glyphRangesBuilder.AddRanges(io.Fonts->GetGlyphRangesVietnamese());
glyphRangesBuilder.BuildRanges(&ranges);
cfg.OversampleH = cfg.OversampleV = 1, cfg.PixelSnapH = true;
cfg.SizePixels = 13.0f * this->m_fontScale;
io.Fonts->AddFontFromFileTTF(path.string().c_str(), std::floor(14.0f * this->m_fontScale), &cfg, ranges.Data); // Needs conversion to char for Windows
}
cfg.MergeMode = true;
io.Fonts->AddFontFromMemoryCompressedTTF(font_awesome_compressed_data, font_awesome_compressed_size, 13.0f * this->m_fontScale, &cfg, fontAwesomeRange);
io.Fonts->AddFontFromMemoryCompressedTTF(codicons_compressed_data, codicons_compressed_size, 13.0f * this->m_fontScale, &cfg, codiconsRange);
ImGuiFreeType::BuildFontAtlas(io.Fonts, ImGuiFreeType::Monochrome);
ImGuiFreeType::BuildFontAtlas(io.Fonts);
io.Fonts->GetTexDataAsRGBA32(&px, &w, &h);
// Create new font atlas
@ -311,8 +327,6 @@ namespace hex {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA8, GL_UNSIGNED_INT, px);
io.Fonts->SetTexID(reinterpret_cast<ImTextureID>(tex));
return true;
}
void Window::frameBegin() {
@ -322,11 +336,12 @@ namespace hex {
ImGui::NewFrame();
ImGuiViewport* viewport = ImGui::GetMainViewport();
ImGui::SetNextWindowPos(viewport->GetWorkPos());
ImGui::SetNextWindowSize(viewport->GetWorkSize());
ImGui::SetNextWindowPos(viewport->WorkPos);
ImGui::SetNextWindowSize(viewport->WorkSize);
ImGui::SetNextWindowViewport(viewport->ID);
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
ImGuiWindowFlags windowFlags = ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoDocking
| ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse
@ -337,11 +352,11 @@ namespace hex {
ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
if (ImGui::Begin("DockSpace", nullptr, windowFlags)) {
ImGui::PopStyleVar(2);
ImGui::DockSpace(ImGui::GetID("MainDock"), ImVec2(0.0f, ImGui::GetContentRegionAvail().y - ImGui::GetTextLineHeightWithSpacing() - 1));
ImGui::PopStyleVar();
ImGui::DockSpace(ImGui::GetID("MainDock"), ImVec2(0.0f, ImGui::GetContentRegionAvail().y - ImGui::GetTextLineHeightWithSpacing() - ImGui::GetStyle().FramePadding.y * 2 - 1));
ImGui::Separator();
ImGui::SetCursorPosX(8);
for (const auto &callback : ContentRegistry::Interface::getFooterItems()) {
auto prevIdx = ImGui::GetWindowDrawList()->_VtxCurrentIdx;
callback();
@ -355,7 +370,8 @@ namespace hex {
}
}
if (ImGui::BeginMenuBar()) {
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
if (ImGui::BeginMainMenuBar()) {
auto menuBarHeight = ImGui::GetCurrentWindow()->MenuBarHeight();
ImGui::SetCursorPosX(5);
@ -386,7 +402,19 @@ namespace hex {
this->drawTitleBar();
ImGui::EndMenuBar();
ImGui::EndMainMenuBar();
}
ImGui::PopStyleVar();
// Draw toolbar
if (ImGui::BeginMenuBar()) {
for (const auto &callback : ContentRegistry::Interface::getToolbarItems()) {
callback();
ImGui::SameLine();
}
ImGui::EndMenuBar();
}
if (SharedData::currentProvider == nullptr) {
@ -410,6 +438,7 @@ namespace hex {
}
ImGui::End();
ImGui::PopStyleVar(2);
// Popup for when no plugins were loaded. Intentionally left untranslated because localization isn't available
@ -788,6 +817,9 @@ namespace hex {
ImGuiIO& io = ImGui::GetIO();
ImGuiStyle& style = ImGui::GetStyle();
style.Alpha = 1.0F;
style.WindowRounding = 0.0F;
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable | ImGuiConfigFlags_NavEnableKeyboard;
#if !defined(OS_LINUX)
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
@ -838,44 +870,7 @@ namespace hex {
break;
}
if (this->setFont(fontFile)) {
}
else {
io.Fonts->Clear();
ImFontConfig cfg;
cfg.OversampleH = cfg.OversampleV = 1, cfg.PixelSnapH = true;
cfg.SizePixels = 13.0f * this->m_fontScale;
io.Fonts->AddFontDefault(&cfg);
cfg.MergeMode = true;
ImWchar fontAwesomeRange[] = {
ICON_MIN_FA, ICON_MAX_FA,
0
};
ImWchar codiconsRange[] = {
ICON_MIN_VS, ICON_MAX_VS,
0
};
std::uint8_t *px;
int w, h;
io.Fonts->AddFontFromMemoryCompressedTTF(font_awesome_compressed_data, font_awesome_compressed_size, 11.0f * this->m_fontScale, &cfg, fontAwesomeRange);
io.Fonts->AddFontFromMemoryCompressedTTF(codicons_compressed_data, codicons_compressed_size, 11.0f * this->m_fontScale, &cfg, codiconsRange);
io.Fonts->GetTexDataAsRGBA32(&px, &w, &h);
// Create new font atlas
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA8, GL_UNSIGNED_INT, px);
io.Fonts->SetTexID(reinterpret_cast<ImTextureID>(tex));
}
this->setFont(fontFile);
style.WindowMenuButtonPosition = ImGuiDir_None;
@ -903,6 +898,9 @@ namespace hex {
ImGui_ImplGlfw_InitForOpenGL(this->m_window, true);
ImGui_ImplOpenGL3_Init("#version 150");
for (const auto &plugin : PluginManager::getPlugins())
plugin.setImGuiContext(ImGui::GetCurrentContext());
}
void Window::deinitGLFW() {