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

Text inputs of Drag widgets are not clamped to min and max bounds #3209

Closed
BluTree opened this issue May 7, 2020 · 4 comments
Closed

Text inputs of Drag widgets are not clamped to min and max bounds #3209

BluTree opened this issue May 7, 2020 · 4 comments

Comments

@BluTree
Copy link

BluTree commented May 7, 2020

Version/Branch of Dear ImGui:

Version: 1.76 WIP
Branch: docking

Back-end/Renderer/Compiler/OS

Back-ends: imgui_impl_glfw.cpp + imgui_impl_opengl3.cpp
Operating System: W10

My Issue/Question:

When editing a drag widget with the input text, the value is not clamped between the min and max value passed in parameter. The only way to do this is do redrag a little the widget to update the value.

Screenshots/Video

issue

Standalone, minimal, complete and verifiable example:

ImGui::Begin("Example Bug");
static float value { 1.f };

ImGui::DragFloat("Density", &value, 0.01f, 0.1f, 1.f);
ImGui::End();
@ocornut
Copy link
Owner

ocornut commented May 7, 2020

See #1829, #946, #413

You can create yourself a DragFloatClamped two-line function that checks the return value and does the clamping.

I think we can add internal flags to do this also but it's not easy presently to expose them to the public API.

@ocornut
Copy link
Owner

ocornut commented May 7, 2020

(Copy of comment posted in #1829:

So any code that must perform a special action every time the value is changed needs to have an extra variable to keep track of the "true" value. This is incredibly annoying.

You can easily wrap that behavior:

    bool SliderFloatClamp(const char* label, float* v, float v_min, float v_max)
    {
        float v_backup = *v;
        if (!SliderFloat(label, v, v_min, v_max))
            return false;
        if (*v < v_min) *v = v_min;
        else if (*v > v_max) *v = v_max;
        return v_backup != *v;
    }

If you display a color square to "verify" that the return value is correct when out of bound (doesn't return true every frame):

bool ret = ImGui::SliderFloatClamp("float", &f, 0.0f, 1.0f);
ImGui::ColorButton("result", ret ? ImVec4(0, 1, 0, 1) : ImVec4(1, 0, 0, 1));

I've also made internal changes to TempInputScalar() to make it honor clamping BUT neither stock version of Slider and Drag are using this feature:

// Our current specs do NOT clamp when using CTRL+Click manual input, but we should eventually add a flag for that..
if (temp_input_is_active || temp_input_start)
     return TempInputScalar(frame_bb, id, label, data_type, p_data, format);// , p_min, p_max);

And TempInputScalar() now has the following comment:

// Note that Drag/Slider functions are currently NOT forwarding the min/max values clamping values!
// This is intended: this way we allow CTRL+Click manual input to set a value out of bounds, for maximum flexibility.
// However this may not be ideal for all uses, as some user code may break on out of bound values.
// In the future we should add flags to Slider/Drag to specify how to enforce min/max values with CTRL+Click.
// See GitHub issues #1829 and #3209
// In the meanwhile, you can easily "wrap" those functions to enforce clamping, using wrapper functions, e.g.
//   bool SliderFloatClamp(const char* label, float* v, float v_min, float v_max)
//   {
//      float v_backup = *v;
//      if (!SliderFloat(label, v, v_min, v_max))
//         return false;
//      *v = ImClamp(*v, v_min, v_max);
//      return v_backup != *v;
//   }

The only thing missing would be to find a nice way to expose new flags to the Slider/Drag api, being considerate of other desirable features that those function wants (#701), so then we can expose clamping behavior in the function signature. I think the power parameter may be getting in the way of some signature and #1316 suggest we could potentially remove it (and remove with flags such as ImGuiSliderFlags_Logarithmic etc.

ocornut added a commit that referenced this issue Aug 17, 2020
…ampOnInput flags to force clamping value when using CTRL+Click to type in a value manually. (#1829, #3209)
@ocornut
Copy link
Owner

ocornut commented Aug 17, 2020

This is now supported with ImGuiDragFlags_ClampOnInput and ImGuiSliderFlags_ClampOnInput flags. See #3361 for details.

@ocornut ocornut closed this as completed Aug 17, 2020
ocornut added a commit that referenced this issue Aug 18, 2020
…, #1823, #1316, #642, #1829, #3209)

Technically API breaking (but ImGuiDragFlags were pushed on master 16 hours ago)
@ocornut
Copy link
Owner

ocornut commented Sep 25, 2020

FYI renamed ImGuiDragFlags_ClampOnInput to ImGuiSliderFlags_AlwaysClamp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants