ui/ux: Implement toolbar button functions

This commit is contained in:
WerWolv 2021-08-21 13:53:50 +02:00
parent 37d2d58d2f
commit 8ad0239bca
6 changed files with 97 additions and 36 deletions

View file

@ -22,27 +22,63 @@ 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);
auto provider = SharedData::currentProvider;
// Undo
ImGui::Disabled([&provider] {
if (ImGui::ToolBarButton(ICON_VS_DISCARD, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarBlue), buttonSize))
provider->undo();
}, provider == nullptr || !provider->canUndo());
// Redo
ImGui::Disabled([&provider] {
if (ImGui::ToolBarButton(ICON_VS_REDO, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarBlue), buttonSize))
provider->redo();
}, provider == nullptr || !provider->canRedo());
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);
// Create new file
if (ImGui::ToolBarButton(ICON_VS_FILE, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarGray), buttonSize))
EventManager::post<RequestOpenWindow>("Create File");
// Open file
if (ImGui::ToolBarButton(ICON_VS_FOLDER_OPENED, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarBrown), buttonSize))
EventManager::post<RequestOpenWindow>("Open File");
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);
// Save file
ImGui::Disabled([&provider] {
if (ImGui::ToolBarButton(ICON_VS_SAVE, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarBlue), buttonSize))
provider->save();
}, provider == nullptr || !provider->isWritable() || !provider->isSavable());
// Save file as
ImGui::Disabled([&provider] {
if (ImGui::ToolBarButton(ICON_VS_SAVE_AS, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarBlue), buttonSize))
View::openFileBrowser("hex.view.hexeditor.save_as"_lang, View::DialogMode::Save, { }, [&provider](auto path) {
provider->saveAs(path);
});
}, provider == nullptr || !provider->isSavable());
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);
// Create bookmark
ImGui::Disabled([] {
if (ImGui::ToolBarButton(ICON_VS_BOOKMARK, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarGreen), buttonSize)) {
Region region = { 0 };
EventManager::post<QuerySelection>(region);
ImHexApi::Bookmarks::add(region.address, region.size, { }, { });
}
}, provider == nullptr || !provider->isReadable());
ImGui::ToolBarButton(ICON_VS_BOOKMARK, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarGreen), buttonSize);
});
}

View file

@ -21,7 +21,7 @@ namespace hex {
class EventId {
public:
constexpr EventId(const char *func = __builtin_FUNCTION(), u32 line = __builtin_LINE()) {
explicit constexpr EventId(const char *func = __builtin_FUNCTION(), u32 line = __builtin_LINE()) {
this->m_hash = line ^ 123456789;
for (auto c : std::string_view(func)) {
this->m_hash = (this->m_hash >> 5) | (this->m_hash << 27);
@ -43,7 +43,7 @@ namespace hex {
struct Event : public EventBase {
using Callback = std::function<void(Params...)>;
explicit Event(Callback func) noexcept : m_func(func) {}
explicit Event(Callback func) noexcept : m_func(std::move(func)) {}
void operator()(Params... params) const noexcept {
this->m_func(params...);
@ -83,10 +83,10 @@ namespace hex {
}
template<typename E>
static void post(auto ... args) noexcept {
static void post(auto&& ... args) noexcept {
for (const auto &[id, event] : s_events) {
if (id == E::id)
(*reinterpret_cast<E *>(event))(args...);
(*reinterpret_cast<E *>(event))(std::forward<decltype(args)>(args)...);
}
}
@ -113,6 +113,8 @@ namespace hex {
EVENT_DEF(RequestAddBookmark, ImHexApi::Bookmarks::Entry);
EVENT_DEF(RequestAppendPatternLanguageCode, std::string);
EVENT_DEF(RequestChangeWindowTitle, std::string);
EVENT_DEF(RequestCloseImHex);
EVENT_DEF(RequestCloseImHex, bool);
EVENT_DEF(QuerySelection, Region&);
}

View file

@ -23,6 +23,7 @@ namespace hex::prv {
virtual bool isReadable() = 0;
virtual bool isWritable() = 0;
virtual bool isResizable() = 0;
virtual bool isSavable() = 0;
virtual void read(u64 offset, void *buffer, size_t size, bool overlays = true);
virtual void readRelative(u64 offset, void *buffer, size_t size, bool overlays = true);
@ -31,6 +32,9 @@ namespace hex::prv {
virtual void resize(ssize_t newSize);
virtual void save();
virtual void saveAs(const std::string &path);
virtual void readRaw(u64 offset, void *buffer, size_t size) = 0;
virtual void writeRaw(u64 offset, const void *buffer, size_t size) = 0;
virtual size_t getActualSize() = 0;

View file

@ -34,6 +34,9 @@ namespace hex::prv {
this->write(offset + this->getBaseAddress(), buffer, size);
}
void Provider::save() { }
void Provider::saveAs(const std::string &path) { }
void Provider::resize(ssize_t newSize) { }
void Provider::applyOverlays(u64 offset, void *buffer, size_t size) {

View file

@ -41,6 +41,10 @@ namespace hex::prv {
return true;
}
bool FileProvider::isSavable() {
return !this->getPatches().empty();
}
void FileProvider::read(u64 offset, void *buffer, size_t size, bool overlays) {
@ -82,6 +86,32 @@ namespace hex::prv {
std::memcpy(reinterpret_cast<u8*>(this->m_mappedFile) + PageSize * this->m_currPage + offset, buffer, size);
}
void FileProvider::save() {
this->applyPatches();
}
void FileProvider::saveAs(const std::string &path) {
FILE *file = fopen(path.c_str(), "wb");
if (file != nullptr) {
std::vector<u8> buffer(0xFF'FFFF, 0x00);
size_t bufferSize = buffer.size();
fseek(file, 0, SEEK_SET);
auto provider = SharedData::currentProvider;
for (u64 offset = 0; offset < provider->getActualSize(); offset += bufferSize) {
if (bufferSize > provider->getActualSize() - offset)
bufferSize = provider->getActualSize() - offset;
provider->readRelative(offset, buffer.data(), bufferSize);
fwrite(buffer.data(), 1, bufferSize, file);
}
fclose(file);
}
}
void FileProvider::resize(ssize_t newSize) {
this->close();

View file

@ -204,6 +204,10 @@ namespace hex {
if (alpha.is_number())
this->m_highlightAlpha = alpha;
});
EventManager::subscribe<QuerySelection>(this, [this](auto &region) {
region = Region { this->m_memoryEditor.DataPreviewAddr, (this->m_memoryEditor.DataPreviewAddrEnd - this->m_memoryEditor.DataPreviewAddr) + 1 };
});
}
ViewHexEditor::~ViewHexEditor() {
@ -263,31 +267,13 @@ namespace hex {
static void save() {
auto provider = SharedData::currentProvider;
for (const auto &[address, value] : provider->getPatches())
provider->writeRaw(address, &value, sizeof(u8));
provider->save();
}
static void saveAs() {
View::openFileBrowser("hex.view.hexeditor.save_as"_lang, View::DialogMode::Save, { }, [](auto path) {
FILE *file = fopen(path.c_str(), "wb");
if (file != nullptr) {
std::vector<u8> buffer(0xFF'FFFF, 0x00);
size_t bufferSize = buffer.size();
fseek(file, 0, SEEK_SET);
auto provider = SharedData::currentProvider;
for (u64 offset = 0; offset < provider->getActualSize(); offset += bufferSize) {
if (bufferSize > provider->getActualSize() - offset)
bufferSize = provider->getActualSize() - offset;
provider->readRelative(offset, buffer.data(), bufferSize);
fwrite(buffer.data(), 1, bufferSize, file);
}
fclose(file);
}
auto provider = SharedData::currentProvider;
provider->saveAs(path);
});
}