Skip to content

Commit

Permalink
Refactor color picker and edit to use templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof-Dmitruk-Mobica committed Jul 15, 2023
1 parent 0601652 commit 8fdcbc8
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 94 deletions.
42 changes: 12 additions & 30 deletions framework/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1245,46 +1245,28 @@ void Drawer::text(const char *formatstr, ...)
va_end(args);
}

bool Drawer::color_picker(const char *caption, std::array<float, 3> &color, float width, ImGuiColorEditFlags flags)
template <>
bool Drawer::color_op_impl<Drawer::ColorOp::Edit, 3>(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<float, 4> &color, float width, ImGuiColorEditFlags flags)
template <>
bool Drawer::color_op_impl<Drawer::ColorOp::Edit, 4>(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<float, 3> &color, float width, ImGuiColorEditFlags flags)
template <>
bool Drawer::color_op_impl<Drawer::ColorOp::Pick, 3>(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<float, 4> &color, float width, ImGuiColorEditFlags flags)
template <>
bool Drawer::color_op_impl<Drawer::ColorOp::Pick, 4>(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
40 changes: 39 additions & 1 deletion framework/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ struct Font
class Drawer
{
public:
enum class ColorOp
{
Edit,
Pick
};

Drawer() = default;

/**
Expand Down Expand Up @@ -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<float, 4> &color, float width = 0.0f, ImGuiColorEditFlags flags = 0);
template <ColorOp OP, size_t N>
bool color_op(const std::string &caption, std::array<float, N> &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<OP, N>(caption.c_str(), color.data(), flags);
ImGui::PopItemWidth();
if (res)
dirty = true;
return res;
}

private:
template <ColorOp OP, size_t N>
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<Drawer::ColorOp::Edit, 3>(const char *caption, float *colors, ImGuiColorEditFlags flags);

template <>
bool Drawer::color_op_impl<Drawer::ColorOp::Edit, 4>(const char *caption, float *colors, ImGuiColorEditFlags flags);

template <>
bool Drawer::color_op_impl<Drawer::ColorOp::Pick, 3>(const char *caption, float *colors, ImGuiColorEditFlags flags);

template <>
bool Drawer::color_op_impl<Drawer::ColorOp::Pick, 4>(const char *caption, float *colors, ImGuiColorEditFlags flags);

class VulkanSample;

/**
Expand Down
42 changes: 12 additions & 30 deletions framework/hpp_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1173,46 +1173,28 @@ void HPPDrawer::text(const char *formatstr, ...)
va_end(args);
}

bool HPPDrawer::color_picker(const char *caption, std::array<float, 3> &color, float width, ImGuiColorEditFlags flags)
template <>
bool HPPDrawer::color_op_impl<HPPDrawer::ColorOp::Edit, 3>(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<float, 4> &color, float width, ImGuiColorEditFlags flags)
template <>
bool HPPDrawer::color_op_impl<HPPDrawer::ColorOp::Edit, 4>(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<float, 3> &color, float width, ImGuiColorEditFlags flags)
template <>
bool HPPDrawer::color_op_impl<HPPDrawer::ColorOp::Pick, 3>(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<float, 4> &color, float width, ImGuiColorEditFlags flags)
template <>
bool HPPDrawer::color_op_impl<HPPDrawer::ColorOp::Pick, 4>(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
67 changes: 39 additions & 28 deletions framework/hpp_gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ struct HPPFont
class HPPDrawer
{
public:
enum class ColorOp
{
Edit,
Pick
};

HPPDrawer() = default;

/**
Expand Down Expand Up @@ -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<float, 3> &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<float, 4> &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<float, 3> &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<float, 4> &color, float width = 0.0f, ImGuiColorEditFlags flags = 0);
template <ColorOp OP, size_t N>
bool color_op(const std::string &caption, std::array<float, N> &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<OP, N>(caption.c_str(), color.data(), flags);
ImGui::PopItemWidth();
if (res)
dirty = true;
return res;
}

private:
template <ColorOp OP, size_t N>
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<HPPDrawer::ColorOp::Edit, 3>(const char *caption, float *colors, ImGuiColorEditFlags flags);

template <>
bool HPPDrawer::color_op_impl<HPPDrawer::ColorOp::Edit, 4>(const char *caption, float *colors, ImGuiColorEditFlags flags);

template <>
bool HPPDrawer::color_op_impl<HPPDrawer::ColorOp::Pick, 3>(const char *caption, float *colors, ImGuiColorEditFlags flags);

template <>
bool HPPDrawer::color_op_impl<HPPDrawer::ColorOp::Pick, 4>(const char *caption, float *colors, ImGuiColorEditFlags flags);

class HPPVulkanSample;

/**
Expand Down
10 changes: 5 additions & 5 deletions samples/extensions/color_write_enable/color_write_enable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<vkb::Drawer::ColorOp::Pick>("", background_color, 0,
ImGuiColorEditFlags_NoSidePreview |
ImGuiColorEditFlags_NoSmallPreview |
ImGuiColorEditFlags_Float |
ImGuiColorEditFlags_RGB))
{
build_command_buffers();
}
Expand Down

0 comments on commit 8fdcbc8

Please sign in to comment.