Skip to content

Commit

Permalink
Add a color picker UI element
Browse files Browse the repository at this point in the history
Replace sliders in the color_write_enable sample with a color picker
element.
  • Loading branch information
Krzysztof-Dmitruk-Mobica committed Jul 14, 2023
1 parent d586556 commit c9cbc16
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
22 changes: 22 additions & 0 deletions framework/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1245,4 +1245,26 @@ void Drawer::text(const char *formatstr, ...)
va_end(args);
}

bool Drawer::color_picker(const char *caption, const char channel_count, float *color, uint16_t width, ImGuiColorEditFlags flags)
{
assert((channel_count == 3 || channel_count == 4) && "Channel count value must be 3 or 4");
bool res;
if (width)
ImGui::PushItemWidth(width);
switch (channel_count)
{
case 3:
res = ImGui::ColorPicker3(caption, color, flags);
break;
case 4:
res = ImGui::ColorPicker4(caption, color, flags);
break;
}
if (res)
{
dirty = true;
};
return res;
}

} // namespace vkb
10 changes: 10 additions & 0 deletions framework/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ class Drawer
*/
void text(const char *formatstr, ...);

/**
* @brief Adds a color picker to the gui
* @param caption The text to display
* @param channel_count Number of channels. Must be 3 or 4.
* @param color Color channel array on which the picker works. It contains values ranging from 0 to 1.
* @param width Element width.
* @param flags Flags to modify the appearance and behavior of the element.
*/
bool color_picker(const char *caption, const char channel_count, float *color, uint16_t width = 0, ImGuiColorEditFlags flags = 0);

private:
bool dirty{false};
};
Expand Down
20 changes: 8 additions & 12 deletions samples/extensions/color_write_enable/color_write_enable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,9 @@ void ColorWriteEnable::build_command_buffers()
// Clear color values.
std::array<VkClearValue, 4> clear_values = {};
clear_values[0].color = {0.0f, 0.0f, 0.0f, 0.0f};
clear_values[1].color = {background_r_value, 0.0f, 0.0f, 0.0f};
clear_values[2].color = {0.0f, background_g_value, 0.0f, 0.0f};
clear_values[3].color = {0.0f, 0.0f, background_b_value, 0.0f};
clear_values[1].color = {background_color[0], 0.0f, 0.0f, 0.0f};
clear_values[2].color = {0.0f, background_color[1], 0.0f, 0.0f};
clear_values[3].color = {0.0f, 0.0f, background_color[2], 0.0f};

// Begin the render pass.
VkRenderPassBeginInfo render_pass_begin_info = vkb::initializers::render_pass_begin_info();
Expand Down Expand Up @@ -539,15 +539,11 @@ void ColorWriteEnable::on_update_ui_overlay(vkb::Drawer &drawer)
{
if (drawer.header("Background color"))
{
if (drawer.slider_float("Red", &background_r_value, 0.0f, 1.0f))
{
build_command_buffers();
}
if (drawer.slider_float("Green", &background_g_value, 0.0f, 1.0f))
{
build_command_buffers();
}
if (drawer.slider_float("Blue", &background_b_value, 0.0f, 1.0f))
if (drawer.color_picker("", 3, background_color.data(), 200,
ImGuiColorEditFlags_NoSidePreview |
ImGuiColorEditFlags_NoSmallPreview |
ImGuiColorEditFlags_Float |
ImGuiColorEditFlags_RGB))
{
build_command_buffers();
}
Expand Down
11 changes: 5 additions & 6 deletions samples/extensions/color_write_enable/color_write_enable.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,11 @@ class ColorWriteEnable : public ApiVulkanSample
void setup_descriptor_set_layout();
void setup_descriptor_set();

bool r_bit_enabled = true;
bool g_bit_enabled = true;
bool b_bit_enabled = true;
float background_r_value = 0.5f;
float background_g_value = 0.5f;
float background_b_value = 0.5f;
bool r_bit_enabled = true;
bool g_bit_enabled = true;
bool b_bit_enabled = true;

std::array<float, 3> background_color = {0.5f, 0.5f, 0.5f};
};

std::unique_ptr<vkb::Application> create_color_write_enable();

0 comments on commit c9cbc16

Please sign in to comment.