diff --git a/framework/gui.cpp b/framework/gui.cpp index 03fef56bf..dc5dc3c2e 100644 --- a/framework/gui.cpp +++ b/framework/gui.cpp @@ -1245,46 +1245,28 @@ void Drawer::text(const char *formatstr, ...) va_end(args); } -bool Drawer::color_picker(const char *caption, std::array &color, float width, ImGuiColorEditFlags flags) +template <> +bool Drawer::color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags) { - bool res; - ImGui::PushItemWidth(width); - res = ImGui::ColorPicker3(caption, color.data(), flags); - ImGui::PopItemWidth(); - if (res) - dirty = true; - return res; + return ImGui::ColorEdit3(caption, colors, flags); } -bool Drawer::color_picker(const char *caption, std::array &color, float width, ImGuiColorEditFlags flags) +template <> +bool Drawer::color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags) { - bool res; - ImGui::PushItemWidth(width); - res = ImGui::ColorPicker4(caption, color.data(), flags); - ImGui::PopItemWidth(); - if (res) - dirty = true; - return res; + return ImGui::ColorEdit4(caption, colors, flags); } -bool Drawer::color_edit(const char *caption, std::array &color, float width, ImGuiColorEditFlags flags) +template <> +bool Drawer::color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags) { - bool res; - ImGui::PushItemWidth(width); - res = ImGui::ColorEdit3(caption, color.data(), flags); - ImGui::PopItemWidth(); - if (res) - dirty = true; - return res; + return ImGui::ColorPicker3(caption, colors, flags); } -bool Drawer::color_edit(const char *caption, std::array &color, float width, ImGuiColorEditFlags flags) +template <> +bool Drawer::color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags) { - bool res; - res = ImGui::ColorEdit4(caption, color.data(), flags); - if (res) - dirty = true; - return res; + return ImGui::ColorPicker4(caption, colors, flags); } } // namespace vkb diff --git a/framework/gui.h b/framework/gui.h index fd9c26ded..628288713 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,17 +208,49 @@ 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 + inline bool color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags) + { + assert(false); + return false; + } + bool dirty{false}; }; +template <> +bool Drawer::color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags); + +template <> +bool Drawer::color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags); + +template <> +bool Drawer::color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags); + +template <> +bool Drawer::color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags); + class VulkanSample; /** diff --git a/framework/hpp_gui.cpp b/framework/hpp_gui.cpp index 0afe26353..3126fcade 100644 --- a/framework/hpp_gui.cpp +++ b/framework/hpp_gui.cpp @@ -1173,46 +1173,28 @@ void HPPDrawer::text(const char *formatstr, ...) va_end(args); } -bool HPPDrawer::color_picker(const char *caption, std::array &color, float width, ImGuiColorEditFlags flags) +template <> +bool HPPDrawer::color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags) { - bool res; - ImGui::PushItemWidth(width); - res = ImGui::ColorPicker3(caption, color.data(), flags); - ImGui::PopItemWidth(); - if (res) - dirty = true; - return res; + return ImGui::ColorEdit3(caption, colors, flags); } -bool HPPDrawer::color_picker(const char *caption, std::array &color, float width, ImGuiColorEditFlags flags) +template <> +bool HPPDrawer::color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags) { - bool res; - ImGui::PushItemWidth(width); - res = ImGui::ColorPicker4(caption, color.data(), flags); - ImGui::PopItemWidth(); - if (res) - dirty = true; - return res; + return ImGui::ColorEdit4(caption, colors, flags); } -bool HPPDrawer::color_edit(const char *caption, std::array &color, float width, ImGuiColorEditFlags flags) +template <> +bool HPPDrawer::color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags) { - bool res; - ImGui::PushItemWidth(width); - res = ImGui::ColorEdit3(caption, color.data(), flags); - ImGui::PopItemWidth(); - if (res) - dirty = true; - return res; + return ImGui::ColorPicker3(caption, colors, flags); } -bool HPPDrawer::color_edit(const char *caption, std::array &color, float width, ImGuiColorEditFlags flags) +template <> +bool HPPDrawer::color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags) { - bool res; - res = ImGui::ColorEdit4(caption, color.data(), flags); - if (res) - dirty = true; - return res; + return ImGui::ColorPicker4(caption, colors, flags); } } // namespace vkb diff --git a/framework/hpp_gui.h b/framework/hpp_gui.h index f3584a046..585cd7bc6 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; /** @@ -161,46 +167,51 @@ class HPPDrawer */ void text(const char *formatstr, ...); - /** - * @brief Adds a color picker to the gui - * @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_picker(const char *caption, std::array &color, float width = 0.0f, ImGuiColorEditFlags flags = 0); - - /** - * @brief Adds a color picker to the gui - * @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_picker(const char *caption, std::array &color, float width = 0.0f, ImGuiColorEditFlags flags = 0); - /** * @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); - - /** - * @brief Adds a color edit to the gui - * @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 + inline bool color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags) + { + assert(false); + return false; + } + bool dirty = false; }; +template <> +bool HPPDrawer::color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags); + +template <> +bool HPPDrawer::color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags); + +template <> +bool HPPDrawer::color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags); + +template <> +bool HPPDrawer::color_op_impl(const char *caption, float *colors, ImGuiColorEditFlags flags); + class HPPVulkanSample; /** diff --git a/samples/extensions/color_write_enable/color_write_enable.cpp b/samples/extensions/color_write_enable/color_write_enable.cpp index 02fa8a6fb..de71c5d35 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(); }