From 50ca886e105774c3efdc3069c5118f23b5377b2c Mon Sep 17 00:00:00 2001 From: Krzysztof-Dmitruk-Mobica Date: Fri, 14 Jul 2023 09:12:04 +0200 Subject: [PATCH] Refactor color picker and edit to use templates --- framework/gui.cpp | 42 --------------- framework/gui.h | 51 ++++++++++++++++++- framework/hpp_gui.cpp | 42 --------------- framework/hpp_gui.h | 51 ++++++++++++++++++- .../color_write_enable/color_write_enable.cpp | 10 ++-- 5 files changed, 105 insertions(+), 91 deletions(-) diff --git a/framework/gui.cpp b/framework/gui.cpp index 03fef56bfa..40cf4b7974 100644 --- a/framework/gui.cpp +++ b/framework/gui.cpp @@ -1245,46 +1245,4 @@ void Drawer::text(const char *formatstr, ...) va_end(args); } -bool Drawer::color_picker(const char *caption, std::array &color, float width, ImGuiColorEditFlags flags) -{ - bool res; - ImGui::PushItemWidth(width); - res = ImGui::ColorPicker3(caption, color.data(), flags); - ImGui::PopItemWidth(); - if (res) - dirty = true; - return res; -} - -bool Drawer::color_picker(const char *caption, std::array &color, float width, ImGuiColorEditFlags flags) -{ - bool res; - ImGui::PushItemWidth(width); - res = ImGui::ColorPicker4(caption, color.data(), flags); - ImGui::PopItemWidth(); - if (res) - dirty = true; - return res; -} - -bool Drawer::color_edit(const char *caption, std::array &color, float width, ImGuiColorEditFlags flags) -{ - bool res; - ImGui::PushItemWidth(width); - res = ImGui::ColorEdit3(caption, color.data(), flags); - ImGui::PopItemWidth(); - if (res) - dirty = true; - return res; -} - -bool Drawer::color_edit(const char *caption, std::array &color, float width, ImGuiColorEditFlags flags) -{ - bool res; - res = ImGui::ColorEdit4(caption, color.data(), flags); - if (res) - dirty = true; - return res; -} - } // namespace vkb diff --git a/framework/gui.h b/framework/gui.h index fd9c26ded6..6790d9a8da 100644 --- a/framework/gui.h +++ b/framework/gui.h @@ -81,6 +81,12 @@ struct Font class Drawer { public: + enum class ColorOp + { + Edit, + Pick + }; + Drawer() = default; /** @@ -202,14 +208,57 @@ class Drawer /** * @brief Adds a color edit to the gui + * @tparam OP Mode of the color element. + * @tparam N Color channel count. Must be 3 or 4. * @param caption The text to display * @param color Color channel array on which the picker works. It contains values ranging from 0 to 1. * @param width Element width. Zero is a special value for the default element width. * @param flags Flags to modify the appearance and behavior of the element. */ - bool color_edit(const char *caption, std::array &color, float width = 0.0f, ImGuiColorEditFlags flags = 0); + template + bool color_op(const std::string &caption, std::array &color, float width = 0.0f, ImGuiColorEditFlags flags = 0) + { + static_assert((N == 3) || (N == 4), "The channel count must be 3 or 4."); + + ImGui::PushItemWidth(width); + bool res = color_op_impl(caption.c_str(), color.data(), flags); + ImGui::PopItemWidth(); + if (res) + dirty = true; + return res; + } private: + template + bool color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags) + { + assert(false); + } + + template <> + bool color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags) + { + return ImGui::ColorEdit3(caption, colors, flags); + } + + template <> + bool color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags) + { + return ImGui::ColorEdit4(caption, colors, flags); + } + + template <> + bool color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags) + { + return ImGui::ColorPicker3(caption, colors, flags); + } + + template <> + bool color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags) + { + return ImGui::ColorPicker4(caption, colors, flags); + } + bool dirty{false}; }; diff --git a/framework/hpp_gui.cpp b/framework/hpp_gui.cpp index 0afe263534..208dc87e8f 100644 --- a/framework/hpp_gui.cpp +++ b/framework/hpp_gui.cpp @@ -1173,46 +1173,4 @@ void HPPDrawer::text(const char *formatstr, ...) va_end(args); } -bool HPPDrawer::color_picker(const char *caption, std::array &color, float width, ImGuiColorEditFlags flags) -{ - bool res; - ImGui::PushItemWidth(width); - res = ImGui::ColorPicker3(caption, color.data(), flags); - ImGui::PopItemWidth(); - if (res) - dirty = true; - return res; -} - -bool HPPDrawer::color_picker(const char *caption, std::array &color, float width, ImGuiColorEditFlags flags) -{ - bool res; - ImGui::PushItemWidth(width); - res = ImGui::ColorPicker4(caption, color.data(), flags); - ImGui::PopItemWidth(); - if (res) - dirty = true; - return res; -} - -bool HPPDrawer::color_edit(const char *caption, std::array &color, float width, ImGuiColorEditFlags flags) -{ - bool res; - ImGui::PushItemWidth(width); - res = ImGui::ColorEdit3(caption, color.data(), flags); - ImGui::PopItemWidth(); - if (res) - dirty = true; - return res; -} - -bool HPPDrawer::color_edit(const char *caption, std::array &color, float width, ImGuiColorEditFlags flags) -{ - bool res; - res = ImGui::ColorEdit4(caption, color.data(), flags); - if (res) - dirty = true; - return res; -} - } // namespace vkb diff --git a/framework/hpp_gui.h b/framework/hpp_gui.h index f3584a0461..83a2963748 100644 --- a/framework/hpp_gui.h +++ b/framework/hpp_gui.h @@ -69,6 +69,12 @@ struct HPPFont class HPPDrawer { public: + enum class ColorOp + { + Edit, + Pick + }; + HPPDrawer() = default; /** @@ -190,14 +196,57 @@ class HPPDrawer /** * @brief Adds a color edit to the gui + * @tparam OP Mode of the color element. + * @tparam N Color channel count. Must be 3 or 4. * @param caption The text to display * @param color Color channel array on which the picker works. It contains values ranging from 0 to 1. * @param width Element width. Zero is a special value for the default element width. * @param flags Flags to modify the appearance and behavior of the element. */ - bool color_edit(const char *caption, std::array &color, float width = 0.0f, ImGuiColorEditFlags flags = 0); + template + bool color_op(const std::string &caption, std::array &color, float width = 0.0f, ImGuiColorEditFlags flags = 0) + { + static_assert((N == 3) || (N == 4), "The channel count must be 3 or 4."); + + ImGui::PushItemWidth(width); + bool res = color_op_impl(caption.c_str(), color.data(), flags); + ImGui::PopItemWidth(); + if (res) + dirty = true; + return res; + } private: + template + bool color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags) + { + assert(false); + } + + template <> + bool color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags) + { + return ImGui::ColorEdit3(caption, colors, flags); + } + + template <> + bool color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags) + { + return ImGui::ColorEdit4(caption, colors, flags); + } + + template <> + bool color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags) + { + return ImGui::ColorPicker3(caption, colors, flags); + } + + template <> + bool color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags) + { + return ImGui::ColorPicker4(caption, colors, flags); + } + bool dirty = false; }; diff --git a/samples/extensions/color_write_enable/color_write_enable.cpp b/samples/extensions/color_write_enable/color_write_enable.cpp index 02fa8a6fbf..de71c5d359 100644 --- a/samples/extensions/color_write_enable/color_write_enable.cpp +++ b/samples/extensions/color_write_enable/color_write_enable.cpp @@ -539,11 +539,11 @@ void ColorWriteEnable::on_update_ui_overlay(vkb::Drawer &drawer) { if (drawer.header("Background color")) { - if (drawer.color_picker("", background_color, 300, - ImGuiColorEditFlags_NoSidePreview | - ImGuiColorEditFlags_NoSmallPreview | - ImGuiColorEditFlags_Float | - ImGuiColorEditFlags_RGB)) + if (drawer.color_op("", background_color, 0, + ImGuiColorEditFlags_NoSidePreview | + ImGuiColorEditFlags_NoSmallPreview | + ImGuiColorEditFlags_Float | + ImGuiColorEditFlags_RGB)) { build_command_buffers(); }