Intermediate Checkboxes #2644

Closed
opened 2019-06-29 22:53:42 +00:00 by Folling · 3 comments
Folling commented 2019-06-29 22:53:42 +00:00 (Migrated from github.com)

Version: ImGui.NET 1.70
Branch: master

Operating System: Arch Linux

I was looking into adding Checkboxes that support an intermediate mode, but didn't find them. It's a feature that would be handy to have.
An intermediate Checkbox allows for a third state, which could generally be seen as a "i'm not sure", instead of a yes or no. Most GUI libraries support them, and they've plenty applications, especially for input forms. Occasionally they're also used to delineate when something is neither true or false. E.g. "Selected all" in a list, but only some items are selected:

image

https://docs.oracle.com/javafx/2/ui_controls/checkbox.htm
This document for JavaFX demonstrates the intermediate model fairly well.

Any chance this could be implemented in Dear ImGui?

Version: ImGui.NET 1.70 Branch: master Operating System: Arch Linux I was looking into adding Checkboxes that support an intermediate mode, but didn't find them. It's a feature that would be handy to have. An intermediate Checkbox allows for a third state, which could generally be seen as a "i'm not sure", instead of a yes or no. Most GUI libraries support them, and they've plenty applications, especially for input forms. Occasionally they're also used to delineate when something is neither true or false. E.g. "Selected all" in a list, but only some items are selected: ![image](https://user-images.githubusercontent.com/23152870/60390193-71d2e600-9ad1-11e9-9bb6-66bd01e6d7e7.png) https://docs.oracle.com/javafx/2/ui_controls/checkbox.htm This document for JavaFX demonstrates the intermediate model fairly well. Any chance this could be implemented in Dear ImGui?
ocornut commented 2019-06-30 09:45:00 +00:00 (Migrated from github.com)

I would like to design this as a more general feature (e.g. any widgets representing a "mixed" selection, or potentially even one single component of a multi-component widget), so the problem is that I'd be hesitant to design something too specific to Checkbox.

I don't mind adding something in imgui_internal.h to support it for Checkbox at a first step, with the understanding that the API might break if we change how it works later on?

I would like to design this as a more general feature (e.g. any widgets representing a "mixed" selection, or potentially even one single component of a multi-component widget), so the problem is that I'd be hesitant to design something too specific to Checkbox. I don't mind adding something in `imgui_internal.h` to support it for Checkbox at a first step, with the understanding that the API might break if we change how it works later on?
ocornut commented 2019-06-30 10:06:14 +00:00 (Migrated from github.com)

Pushed something to imgui_internal.h
My draft to support the general feature involved a ImGuiItemFlags_MixedValue flag so I went this way for the checkbox.

You'll need to add your own wrapper function (in any of your own .cpp file), e.g.

namespace ImGui
{
    bool CheckBoxTristate(const char* label, int* v_tristate)
    {
        bool ret;
        if (*v_tristate == -1)
        {
            ImGui::PushItemFlag(ImGuiItemFlags_MixedValue, true);
            bool b = false;
            ret = ImGui::Checkbox(label, &b);
            if (ret)
                *v_tristate = 1;
            ImGui::PopItemFlag();
        }
        else
        {
            bool b = (*v_tristate != 0);
            ret = ImGui::Checkbox(label, &b);
            if (ret)
                *v_tristate = (int)b;
        }
        return ret;
    }
};

Usage

        static int tristate = -1;
        ImGui::CheckBoxTristate("Tristate", &tristate);
        if (ImGui::SmallButton("reset to -1"))
            tristate = -1;

tristate_checkbox

Pushed something to `imgui_internal.h` My draft to support the general feature involved a `ImGuiItemFlags_MixedValue` flag so I went this way for the checkbox. You'll need to add your own wrapper function (in any of your own .cpp file), e.g. ``` namespace ImGui { bool CheckBoxTristate(const char* label, int* v_tristate) { bool ret; if (*v_tristate == -1) { ImGui::PushItemFlag(ImGuiItemFlags_MixedValue, true); bool b = false; ret = ImGui::Checkbox(label, &b); if (ret) *v_tristate = 1; ImGui::PopItemFlag(); } else { bool b = (*v_tristate != 0); ret = ImGui::Checkbox(label, &b); if (ret) *v_tristate = (int)b; } return ret; } }; ``` Usage ``` static int tristate = -1; ImGui::CheckBoxTristate("Tristate", &tristate); if (ImGui::SmallButton("reset to -1")) tristate = -1; ``` ![tristate_checkbox](https://user-images.githubusercontent.com/8225057/60395144-526aa600-9b2f-11e9-8ca0-097a24033a10.gif)
Folling commented 2019-06-30 11:48:06 +00:00 (Migrated from github.com)

Well, that was fast! I like the generalisation of that concept, and for now this workaround is more than enough, thanks for the quick reply!

Well, that was fast! I like the generalisation of that concept, and for now this workaround is more than enough, thanks for the quick reply!
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ocornut/imgui#2644
No description provided.