From 4328a335ec6b805982943814fe9faec60f033905 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 13 Jan 2021 14:11:23 +0100 Subject: [PATCH] Added color picker for Bookmarks and highlighting in the hex view --- include/views/view_bookmarks.hpp | 4 ++-- include/views/view_hexeditor.hpp | 5 ++++- plugins/libimhex/include/helpers/utils.hpp | 1 + source/main.cpp | 5 +++-- source/views/view_bookmarks.cpp | 23 ++++++++++++++++++---- source/views/view_hexeditor.cpp | 12 +++++++++-- 6 files changed, 39 insertions(+), 11 deletions(-) diff --git a/include/views/view_bookmarks.hpp b/include/views/view_bookmarks.hpp index 6bebb186c..b49454cb5 100644 --- a/include/views/view_bookmarks.hpp +++ b/include/views/view_bookmarks.hpp @@ -13,14 +13,14 @@ namespace hex { class ViewBookmarks : public View { public: - explicit ViewBookmarks(); + explicit ViewBookmarks(std::list &bookmarks); ~ViewBookmarks() override; void drawContent() override; void drawMenu() override; private: - std::list m_bookmarks; + std::list &m_bookmarks; }; } \ No newline at end of file diff --git a/include/views/view_hexeditor.hpp b/include/views/view_hexeditor.hpp index 71576cbd0..fb9959670 100644 --- a/include/views/view_hexeditor.hpp +++ b/include/views/view_hexeditor.hpp @@ -6,6 +6,7 @@ #include "imgui_memory_editor.h" #include "ImGuiFileBrowser.h" +#include #include #include #include @@ -20,7 +21,7 @@ namespace hex { class ViewHexEditor : public View { public: - ViewHexEditor(std::vector &patternData); + ViewHexEditor(std::vector &patternData, const std::list &bookmarks); ~ViewHexEditor() override; void drawContent() override; @@ -32,6 +33,8 @@ namespace hex { imgui_addons::ImGuiFileBrowser m_fileBrowser; std::vector &m_patternData; + const std::list &m_bookmarks; + std::map m_highlightedBytes; char m_searchStringBuffer[0xFFFF] = { 0 }; diff --git a/plugins/libimhex/include/helpers/utils.hpp b/plugins/libimhex/include/helpers/utils.hpp index 7a9dfc078..455c2cce0 100644 --- a/plugins/libimhex/include/helpers/utils.hpp +++ b/plugins/libimhex/include/helpers/utils.hpp @@ -202,5 +202,6 @@ namespace hex { std::vector name; std::vector comment; + u32 color; }; } \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index b37c6f403..f45d999ab 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -28,9 +28,10 @@ int main(int argc, char **argv) { // Shared Data std::vector patternData; + std::list bookmarks; // Create views - hex::ContentRegistry::Views::add(patternData); + hex::ContentRegistry::Views::add(patternData, bookmarks); hex::ContentRegistry::Views::add(patternData); hex::ContentRegistry::Views::add(patternData); hex::ContentRegistry::Views::add(); @@ -38,7 +39,7 @@ int main(int argc, char **argv) { hex::ContentRegistry::Views::add(); hex::ContentRegistry::Views::add(); hex::ContentRegistry::Views::add(); - hex::ContentRegistry::Views::add(); + hex::ContentRegistry::Views::add(bookmarks); hex::ContentRegistry::Views::add(); hex::ContentRegistry::Views::add(); hex::ContentRegistry::Views::add(); diff --git a/source/views/view_bookmarks.cpp b/source/views/view_bookmarks.cpp index 0cdd130e8..b92674bfb 100644 --- a/source/views/view_bookmarks.cpp +++ b/source/views/view_bookmarks.cpp @@ -7,7 +7,7 @@ namespace hex { - ViewBookmarks::ViewBookmarks() : View("Bookmarks") { + ViewBookmarks::ViewBookmarks(std::list &bookmarks) : View("Bookmarks"), m_bookmarks(bookmarks) { View::subscribeEvent(Events::AddBookmark, [this](const void *userData) { Bookmark bookmark = *reinterpret_cast(userData); bookmark.name.resize(64); @@ -21,6 +21,8 @@ namespace hex { if (bookmark.comment.empty()) std::memset(bookmark.comment.data(), 0x00, 0xF'FFFF); + bookmark.color = ImGui::GetColorU32(ImGuiCol_Header); + this->m_bookmarks.push_back(bookmark); ProjectFile::markDirty(); }); @@ -52,8 +54,15 @@ namespace hex { u32 id = 1; std::list::const_iterator bookmarkToRemove = this->m_bookmarks.end(); for (auto iter = this->m_bookmarks.begin(); iter != this->m_bookmarks.end(); iter++) { - auto &[region, name, comment] = *iter; + auto &[region, name, comment, color] = *iter; + auto headerColor = ImColor(color); + auto hoverColor = ImColor(color); + hoverColor.Value.w *= 1.3F; + + ImGui::PushStyleColor(ImGuiCol_Header, color); + ImGui::PushStyleColor(ImGuiCol_HeaderActive, color); + ImGui::PushStyleColor(ImGuiCol_HeaderHovered, u32(hoverColor)); if (ImGui::CollapsingHeader((std::string(name.data()) + "###" + std::to_string((u64)comment.data())).c_str())) { ImGui::TextUnformatted("Information"); ImGui::Separator(); @@ -90,16 +99,22 @@ namespace hex { ImGui::PushID(id); ImGui::InputText("##nolabel", name.data(), 64); ImGui::PopID(); + ImGui::SameLine(); + ImGui::PushID(id + 1); + ImGui::ColorEdit4("Color", (float*)&headerColor.Value, ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_NoAlpha); + color = headerColor; + ImGui::PopID(); ImGui::NewLine(); ImGui::TextUnformatted("Comment"); ImGui::Separator(); - ImGui::PushID(id + 1); + ImGui::PushID(id + 2); ImGui::InputTextMultiline("##nolabel", comment.data(), 0xF'FFFF); ImGui::PopID(); ImGui::NewLine(); - id += 2; + id += 3; } + ImGui::PopStyleColor(3); } if (bookmarkToRemove != this->m_bookmarks.end()) { diff --git a/source/views/view_hexeditor.cpp b/source/views/view_hexeditor.cpp index 676993e0a..a1c135dbe 100644 --- a/source/views/view_hexeditor.cpp +++ b/source/views/view_hexeditor.cpp @@ -15,8 +15,8 @@ namespace hex { - ViewHexEditor::ViewHexEditor(std::vector &patternData) - : View("Hex Editor"), m_patternData(patternData) { + ViewHexEditor::ViewHexEditor(std::vector &patternData, const std::list &bookmarks) + : View("Hex Editor"), m_patternData(patternData), m_bookmarks(bookmarks) { this->m_memoryEditor.ReadFn = [](const ImU8 *data, size_t off) -> ImU8 { auto provider = SharedData::currentProvider; @@ -43,6 +43,14 @@ namespace hex { ViewHexEditor *_this = (ViewHexEditor *) data; std::optional currColor, prevColor; + + for (const auto &[region, name, comment, color] : _this->m_bookmarks) { + if (off >= region.address && off < (region.address + region.size)) + currColor = (color & 0x00FFFFFF) | 0x40000000; + if ((off - 1) >= region.address && (off - 1) < (region.address + region.size)) + prevColor = (color & 0x00FFFFFF) | 0x40000000;; + } + if (_this->m_highlightedBytes.contains(off)) currColor = _this->m_highlightedBytes[off]; if (_this->m_highlightedBytes.contains(off - 1))