Skip to content

Commit de25ce7

Browse files
committed
feat: Add support for custom inspector edit widgets
1 parent 21e61bf commit de25ce7

File tree

4 files changed

+146
-101
lines changed

4 files changed

+146
-101
lines changed

lib/libimhex/include/hex/api/content_registry/data_inspector.hpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <optional>
99
#include <string>
1010
#include <vector>
11+
#include <bit>
1112

1213
EXPORT_MODULE namespace hex {
1314

@@ -22,8 +23,10 @@ EXPORT_MODULE namespace hex {
2223

2324
namespace impl {
2425

26+
struct DoNotUseThisByItselfTag {};
27+
2528
using DisplayFunction = std::function<std::string()>;
26-
using EditingFunction = std::function<std::vector<u8>(std::string, std::endian)>;
29+
using EditingFunction = std::function<std::optional<std::vector<u8>>(std::string&, std::endian, DoNotUseThisByItselfTag)>;
2730
using GeneratorFunction = std::function<DisplayFunction(const std::vector<u8> &, std::endian, NumberDisplayStyle)>;
2831

2932
struct Entry {
@@ -38,6 +41,35 @@ EXPORT_MODULE namespace hex {
3841

3942
}
4043

44+
namespace EditWidget {
45+
46+
class Widget {
47+
public:
48+
using Function = std::function<std::vector<u8>(const std::string&, std::endian)>;
49+
50+
explicit Widget(const Function &function) : m_function(function) {}
51+
52+
virtual ~Widget() = default;
53+
virtual std::optional<std::vector<u8>> draw(std::string &value, std::endian endian) = 0;
54+
std::optional<std::vector<u8>> operator()(std::string &value, std::endian endian, impl::DoNotUseThisByItselfTag) {
55+
return draw(value, endian);
56+
}
57+
58+
std::vector<u8> getBytes(const std::string &value, std::endian endian) const {
59+
return m_function(value, endian);
60+
}
61+
62+
private:
63+
Function m_function;
64+
};
65+
66+
struct TextInput : Widget {
67+
explicit TextInput(const Function &function) : Widget(function) {}
68+
std::optional<std::vector<u8>> draw(std::string &value, std::endian endian) override;
69+
};
70+
71+
}
72+
4173
/**
4274
* @brief Adds a new entry to the data inspector
4375
* @param unlocalizedName The unlocalized name of the entry

lib/libimhex/source/api/content_registry.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,18 @@ namespace hex {
870870

871871
}
872872

873+
namespace EditWidget {
874+
std::optional<std::vector<u8>> TextInput::draw(std::string &value, std::endian endian) {
875+
if (ImGui::InputText("##InspectorLineEditing", value,
876+
ImGuiInputTextFlags_EnterReturnsTrue |
877+
ImGuiInputTextFlags_AutoSelectAll)) {
878+
return getBytes(value, endian);
879+
}
880+
881+
return std::nullopt;
882+
}
883+
}
884+
873885
void add(const UnlocalizedString &unlocalizedName, size_t requiredSize, impl::GeneratorFunction displayGeneratorFunction, std::optional<impl::EditingFunction> editingFunction) {
874886
log::debug("Registered new data inspector format: {}", unlocalizedName.get());
875887

0 commit comments

Comments
 (0)