ocornut / imgui

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
MIT License
57.81k stars 9.91k forks source link

DragScalar and SliderScalar with inputs_step #1815

Open Wolftronics-SBC opened 6 years ago

Wolftronics-SBC commented 6 years ago

ImGui :: DragScalar and ImGui :: SliderScalar with drag_step and slider_step? did not you want to do that?

as ImGui :: InputScalar

ImGui :: Checkbox ("Show step buttons", & inputs_step); ImGui :: InputScalar ("input s32", ImGuiDataType_S32, & s32_v, inputs_step? & S32_one: NULL, NULL, "% d");

ocornut commented 6 years ago

We have a API explosion problem, for now I don't think I would add, but I admit it is tempting to sneak them in the DragScalar() SliderScalar() functions since they are fresh.

Later I expect to refactor the internal code to be combineable at a lower level.

thewoz commented 1 year ago

Hi sorry to reopen this question. But I would also like to have an integer slider that accepts a step such that it gives only negative numbers.

Nahor commented 7 months ago
  1. It's easy enough to simulate steps when the value changes:

    static float foobar = 0;
    
    // Slider with a step of 3.0
    if (ImGui::SliderFloat("Foobar", &foobar , 0.0f, 15.0f, "%.0f")) {
        foobar = std::round(foobar / 3.0f) * 3.0f;
    }

    however:

    • when dragging with the mouse, the slider handle does not snap to position. The handle follows the mouse rather than the input value.
    • the text of the slider also follows the value at the mouse location (but that can be worked around by passing a preformatted string instead)
    • does not work as-is with keyboard arrows and gamepad, because any change made to the value will be rounded back to the old one blocking changes (using a non-rounded temporary variable while editing can help work around that).
  2. If possible support non-constant steps. In one of my use case, I need the values to be divisors of another bigger number (e.g. divisors of 1440 (number of minutes in a day), where values 7, 11, 13, 14, 17, 19, ... are not valid). Maybe a callback function, which could return the preferred value (nearest for mouse drag, next/previous for arrows/gamepad)

Jonathhhan commented 6 months ago

Here my hack for a step slider:

        ImGui::Dummy(ImVec2(0, 10));

        static int width = 512;
        int widthSliderStep = 128;
        static int widthSliderValue = width / widthSliderStep;
        if (ImGui::SliderInt("Width", &widthSliderValue, 1, 8, std::to_string(width).c_str(), ImGuiSliderFlags_NoInput)) {
            width = widthSliderValue * widthSliderStep;
        }

        ImGui::Dummy(ImVec2(0, 10));

        static float height = 15;
        float heightSliderStep = 1.5;
        static int heightSliderValue = height / heightSliderStep;
        std::ostringstream ssHeight;
        ssHeight << std::fixed << std::setprecision(1) << height;
        if (ImGui::SliderInt("Height", &heightSliderValue, 1, 20, ssHeight.str().c_str(), ImGuiSliderFlags_NoInput)) {
            height = heightSliderValue * heightSliderStep;
        }

        ImGui::Dummy(ImVec2(0, 10));