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

Fix for ImGui::SliderInt makes a value 0, when min_range==max_range #3774

Closed
wants to merge 1 commit into from

Conversation

erwincoumans
Copy link
Contributor

@erwincoumans erwincoumans commented Jan 31, 2021

Great project, thanks and kudos for all the tremendous effort!!!

Searched the FAQ and possibly similar issues, such as this old issue and more recent issue.

I maybe missing something, but an ImGui::SliderInt makes a value 0, while dragging, if min_range==max_range even if min_range=42 (for example).

Simple reproduction case:

static int test=42;
int min_range = 42;
int max_range = 42;
if(ImGui::SliderInt("Test",&test,min_range,max_range,0,ImGuiSliderFlags_ClampOnInput))
{
    printf("test=%d\n",test);
}

It prints test=0 while dragging the slider, would like it to stay in range 42,42

Patch makes it work, but perhaps breaks other widgets?

// Convert a parametric position on a slider into a value v in the output space (the logical opposite of ScaleRatioFromValueT)
template<typename TYPE, typename SIGNEDTYPE, typename FLOATTYPE>
TYPE ImGui::ScaleValueFromRatioT(ImGuiDataType data_type, float t, TYPE v_min, TYPE v_max, bool is_logarithmic, float logarithmic_zero_epsilon, float zero_deadzone_halfsize)
{
    if (v_min == v_max)
        return (TYPE)v_min;

instead of

// Convert a parametric position on a slider into a value v in the output space (the logical opposite of ScaleRatioFromValueT)
template<typename TYPE, typename SIGNEDTYPE, typename FLOATTYPE>
TYPE ImGui::ScaleValueFromRatioT(ImGuiDataType data_type, float t, TYPE v_min, TYPE v_max, bool is_logarithmic, float logarithmic_zero_epsilon, float zero_deadzone_halfsize)
{
    if (v_min == v_max)
        return (TYPE)0.0f;

…hile dragging, if min_range==max_range.

static int test=2;
int min_range = 2
int max_range = 2
if(ImGui::SliderInt("Test",&test,min_range,max_range,0,ImGuiSliderFlags_ClampOnInput))
{
    printf("test=%d\n",test);
}

test becomes 0 while dragging the slider, would like it to stay in range 2,2

~~~~~~~~~~~
Patch makes it work, but perhaps breaks other widgets?

// Convert a parametric position on a slider into a value v in the output space (the logical opposite of ScaleRatioFromValueT)
template<typename TYPE, typename SIGNEDTYPE, typename FLOATTYPE>
TYPE ImGui::ScaleValueFromRatioT(ImGuiDataType data_type, float t, TYPE v_min, TYPE v_max, bool is_logarithmic, float logarithmic_zero_epsilon, float zero_deadzone_halfsize)
{
    if (v_min == v_max)
        return (TYPE)v_min;

// Convert a parametric position on a slider into a value v in the output space (the logical opposite of ScaleRatioFromValueT)
template<typename TYPE, typename SIGNEDTYPE, typename FLOATTYPE>
TYPE ImGui::ScaleValueFromRatioT(ImGuiDataType data_type, float t, TYPE v_min, TYPE v_max, bool is_logarithmic, float logarithmic_zero_epsilon, float zero_deadzone_halfsize)
{
    if (v_min == v_max)
        return (TYPE)0.0f;
~~~~~~~~~~~
@ocornut
Copy link
Owner

ocornut commented Feb 1, 2021

Merged the fix: 4dec436
Thank you Erwin!

Here's a repro that include DragFloat() having same issue with the Logarithmic flag:

{
    static int test = 42;
    int min_range = 42;
    int max_range = 42;
    if (ImGui::SliderInt("Test", &test, min_range, max_range, 0, ImGuiSliderFlags_ClampOnInput))
    {
    }
}
{
    static float test = 42;
    float min_range = 42;
    float max_range = 42;
    if (ImGui::DragFloat("TestDrag", &test, 0.2f, min_range, max_range, 0, ImGuiSliderFlags_ClampOnInput | ImGuiSliderFlags_Logarithmic))
    {
    }
}

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

Successfully merging this pull request may close these issues.

2 participants