From e797ac3a57360b12a7d1971811e352c3ad653172 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 28 Sep 2022 18:30:41 +0200 Subject: [PATCH] feat: Added Float16 data visualizer Closes #749 --- .../source/content/data_visualizers.cpp | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/plugins/builtin/source/content/data_visualizers.cpp b/plugins/builtin/source/content/data_visualizers.cpp index 28089b3c5..51d342eae 100644 --- a/plugins/builtin/source/content/data_visualizers.cpp +++ b/plugins/builtin/source/content/data_visualizers.cpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace hex::plugin::builtin { @@ -160,7 +161,9 @@ namespace hex::plugin::builtin { } }; - template + enum class Float16 : u16 {}; + + template class DataVisualizerFloatingPoint : public hex::ContentRegistry::HexEditor::DataVisualizer { public: DataVisualizerFloatingPoint() : DataVisualizer(ByteCount, CharCount) { } @@ -206,6 +209,42 @@ namespace hex::plugin::builtin { } }; + template<> + class DataVisualizerFloatingPoint : public hex::ContentRegistry::HexEditor::DataVisualizer { + public: + DataVisualizerFloatingPoint() : DataVisualizer(ByteCount, CharCount) { } + + void draw(u64 address, const u8 *data, size_t size, bool upperCase) override { + hex::unused(address); + + if (size == ByteCount) + ImGui::Text(getFormatString(upperCase), hex::float16ToFloat32(*reinterpret_cast(data))); + else + ImGui::TextFormatted("{: {}s}", CharCount); + } + + bool drawEditing(u64 address, u8 *data, size_t size, bool upperCase, bool startedEditing) override { + hex::unused(startedEditing); + + this->draw(address, data, size, upperCase); + return false; + } + + private: + constexpr static inline auto ByteCount = sizeof(Float16); + constexpr static inline auto CharCount = 14; + + const static inline auto FormatStringUpperCase = hex::format("%{}G", CharCount); + const static inline auto FormatStringLowerCase = hex::format("%{}g", CharCount); + + const char *getFormatString(bool upperCase) { + if (upperCase) + return FormatStringUpperCase.c_str(); + else + return FormatStringLowerCase.c_str(); + } + }; + class DataVisualizerRGBA8 : public hex::ContentRegistry::HexEditor::DataVisualizer { public: DataVisualizerRGBA8() : DataVisualizer(4, 2) { } @@ -262,6 +301,7 @@ namespace hex::plugin::builtin { ContentRegistry::HexEditor::addDataVisualizer>("hex.builtin.visualizer.decimal.signed.32bit"); ContentRegistry::HexEditor::addDataVisualizer>("hex.builtin.visualizer.decimal.signed.64bit"); + ContentRegistry::HexEditor::addDataVisualizer>("hex.builtin.visualizer.floating_point.16bit"); ContentRegistry::HexEditor::addDataVisualizer>("hex.builtin.visualizer.floating_point.32bit"); ContentRegistry::HexEditor::addDataVisualizer>("hex.builtin.visualizer.floating_point.64bit");