Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add colour choosing functionality to combos via flags on BeginCombo() and Selectable() #4168

Closed
wants to merge 1 commit into from

Conversation

Treeki
Copy link

@Treeki Treeki commented May 24, 2021

My application has a material editor that allows you to pick from a small fixed set of colours, and I wanted to use a combo box. I thought it would be nice to display preview swatches next to them. I can do this by adding rectangles to the DrawList, but there's no elegant way to adjust the position of the combo preview text without modifying BeginCombo().

This PR adds the following functionality:

  • New flag: ImGuiComboFlags_ColorPreview
  • New flag: ImGuiSelectableFlags_ColorPreview
  • col_preview parameter (of type ImVec4) added to BeginCombo() and Selectable()
    • Displays a swatch to the left of the text when the corresponding flag is enabled
    • RenderColorRectWithAlphaCheckerboard is used to match the previews shown by ColorEdit, etc. for colours with alpha channels
  • The demo is updated to demonstrate this

imgui_combo_color_preview

There are some questions I wasn't sure about:

  • Adding an extra parameter to these two functions just for one use case makes the API surface a bit more cluttered - is there a better way to do this? (Pushing to a stack or calling a new SetNextXXX function...?)
  • Should the colour be supplied as an ImVec4 or an ImU32?
  • The swatches may need to use one of the rounding settings from the style

I don't know if this is the ideal implementation, but hopefully it's a good starting point.

@ocornut
Copy link
Owner

ocornut commented May 28, 2021

Hello,

Thank you for the PR.

As is I don't think the proposed solution is adequate, however I understand this is a desirable outcome we want to allow. This has been also discussed in #1658 (comment)

  1. As for the Selectable(): none of the changes are required. You can submit a selected with no label, then draw whichever content you like over it.

  2. As for drawing a complex "preview" in the closed combo, two solutions:

  • we make BeginCombo() provide the geometry for that preview box, to let you draw something after calling BeginCombo(). EDIT In reality this is likely to require us to have aabb in ImDrawCmd to merge most.
  • we work toward spitting BeginCombo() in smaller chunks so it become easier to copy it.

I think both are somehow viable paths.

ocornut added a commit that referenced this pull request May 28, 2021
@ocornut
Copy link
Owner

ocornut commented Jun 15, 2021

I have pushed an experimental API (declared in imgui_internal.h)

if (ImGui::BeginCombo("combo custom", "", flags | ImGuiComboFlags_CustomPreview))
{
    // ...
    ImGui::EndCombo();
}
if (ImGui::BeginComboPreview())
{
    ImGui::ColorButton("##color", ImVec4(1.0f, 0.5f, 0.5f, 1.0f), ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoDragDrop, color_square_size);
    ImGui::TextUnformatted(items[item_current_idx]);
    ImGui::EndComboPreview();
}

image

The idea is that BeginComboPreview() will set cursor and clip rect, and EndComboPreview() restores things.
(similar to BeginGroup and unfinished features such as PushWorkRect)
Whereas most custom solutions (including this PR) would leave the preview unclipped.

If it is works well this may eventually be promoted to a public api (imgui.h)

Thanks for the nudge :)

@ocornut ocornut closed this Jun 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants