ImGui Slider Custom

Начинающий
Статус
Оффлайн
Регистрация
25 Мар 2019
Сообщения
7
Реакции[?]
2
Поинты[?]
0
https://yougame.biz/threads/44255/
Знатоки, объясните пожалуйста, как вставить данный код, на примере imgui-master
Original UC (не рек)

I just modified the ImGUI Sliders a bit and I thought they look cool so I'll release them.

In your imgui.cpp you need to replace the function "SliderBehavior" and your desired Slider-Function to be compatible with the new SliderBehavior.

Code of the new SliderBehavior:

Код:
bool ImGui::SliderBehavior(const ImRect& frame_bb, ImGuiID id, float* v, float v_min, float v_max, float power, int decimal_precision, ImGuiSliderFlags flags, ImVec4 color, ImVec2 valuesize, const char* label, char* value)
{
    ImGuiContext& g = *GImGui;
    ImGuiWindow* window = GetCurrentWindow();
    const ImGuiStyle& style = g.Style;
 
    window->DrawList->AddLine(ImVec2(frame_bb.Min.x, frame_bb.Min.y + ((frame_bb.Max.y - frame_bb.Min.y)/2)), ImVec2(frame_bb.Max.x, frame_bb.Min.y + ((frame_bb.Max.y - frame_bb.Min.y) / 2)), GetColorU32(ImVec4(0.21f, 0.20f, 0.21f, 1.00f)));
 
 
    const bool is_non_linear = (power < 1.0f - 0.00001f) || (power > 1.0f + 0.00001f);
    const bool is_horizontal = (flags & ImGuiSliderFlags_Vertical) == 0;
 
    const float grab_padding = 2.0f;
    const float slider_sz = is_horizontal ? (frame_bb.GetWidth() - grab_padding * 2.0f) : (frame_bb.GetHeight() - grab_padding * 2.0f);
    float grab_sz;
    if (decimal_precision > 0)
        grab_sz = ImMin(style.GrabMinSize, slider_sz);
    else
        grab_sz = ImMin(ImMax(1.0f * (slider_sz / ((v_min < v_max ? v_max - v_min : v_min - v_max) + 1.0f)), style.GrabMinSize), slider_sz);
    const float slider_usable_sz = slider_sz - grab_sz;
    const float slider_usable_pos_min = (is_horizontal ? frame_bb.Min.x : frame_bb.Min.y) + grab_padding + grab_sz * 0.5f;
    const float slider_usable_pos_max = (is_horizontal ? frame_bb.Max.x : frame_bb.Max.y) - grab_padding - grab_sz * 0.5f;
 
    float linear_zero_pos = 0.0f;
    if (v_min * v_max < 0.0f)
    {
        const float linear_dist_min_to_0 = powf(fabsf(0.0f - v_min), 1.0f / power);
        const float linear_dist_max_to_0 = powf(fabsf(v_max - 0.0f), 1.0f / power);
        linear_zero_pos = linear_dist_min_to_0 / (linear_dist_min_to_0 + linear_dist_max_to_0);
    }
    else
    {
        linear_zero_pos = v_min < 0.0f ? 1.0f : 0.0f;
    }
 
    bool isDown = false;
    bool value_changed = false;
    if (g.ActiveId == id)
    {
        if (g.IO.MouseDown[0])
        {
            isDown = true;
            const float mouse_abs_pos = is_horizontal ? g.IO.MousePos.x : g.IO.MousePos.y;
            float clicked_t = (slider_usable_sz > 0.0f) ? ImClamp((mouse_abs_pos - slider_usable_pos_min) / slider_usable_sz, 0.0f, 1.0f) : 0.0f;
            if (!is_horizontal)
                clicked_t = 1.0f - clicked_t;
 
            float new_value;
            if (is_non_linear)
            {
                    if (clicked_t < linear_zero_pos)
                {
                    float a = 1.0f - (clicked_t / linear_zero_pos);
                    a = powf(a, power);
                    new_value = ImLerp(ImMin(v_max, 0.0f), v_min, a);
                }
                else
                {
                    float a;
                    if (fabsf(linear_zero_pos - 1.0f) > 1.e-6f)
                        a = (clicked_t - linear_zero_pos) / (1.0f - linear_zero_pos);
                    else
                        a = clicked_t;
                    a = powf(a, power);
                    new_value = ImLerp(ImMax(v_min, 0.0f), v_max, a);
                }
            }
            else
            {
                new_value = ImLerp(v_min, v_max, clicked_t);
            }
 
            new_value = RoundScalar(new_value, decimal_precision);
            if (*v != new_value)
            {
                *v = new_value;
                value_changed = true;
            }
        }
        else
        {
            ClearActiveID();
        }
    }
 
    float grab_t = SliderBehaviorCalcRatioFromValue(*v, v_min, v_max, power, linear_zero_pos);
 
    if (!is_horizontal)
        grab_t = 1.0f - grab_t;
    const float grab_pos = ImLerp(slider_usable_pos_min, slider_usable_pos_max, grab_t);
    ImRect grab_bb;
    if (is_horizontal)
        grab_bb = ImRect(ImVec2(grab_pos - grab_sz * 0.5f, frame_bb.Min.y + grab_padding), ImVec2(grab_pos + grab_sz * 0.5f, frame_bb.Max.y - grab_padding));
    else
        grab_bb = ImRect(ImVec2(frame_bb.Min.x + grab_padding, grab_pos - grab_sz * 0.5f), ImVec2(frame_bb.Max.x - grab_padding, grab_pos + grab_sz * 0.5f));
    window->DrawList->AddLine(ImVec2(frame_bb.Min.x, frame_bb.Min.y + ((frame_bb.Max.y - frame_bb.Min.y) / 2)), ImVec2(grab_bb.Min.x, frame_bb.Min.y + ((frame_bb.Max.y - frame_bb.Min.y) / 2)), GetColorU32(ImVec4(0.76f, 0.10f, 0.24f, 1.00f)));
    if (isDown)
        window->DrawList->AddCircleFilled(ImVec2(grab_bb.Min.x + 6, grab_bb.Min.y + ((grab_bb.Max.y - grab_bb.Min.y) / 2)), 13, GetColorU32(g.ActiveId == id ? ImVec4(0.76f, 0.10f, 0.24f, 1.00f)/*Active*/ : ImVec4(0.76f, 0.10f, 0.24f, 1.00f), 0.2f));
    window->DrawList->AddCircleFilled(ImVec2(grab_bb.Min.x + 6, grab_bb.Min.y + ((grab_bb.Max.y - grab_bb.Min.y) / 2)), 5, GetColorU32(g.ActiveId == id ? ImVec4(0.76f, 0.10f, 0.24f, 1.00f)/*Active*/ : ImVec4(0.76f, 0.10f, 0.24f, 1.00f), 0.2f));
    window->DrawList->AddRectFilled(ImVec2(grab_bb.Min.x, frame_bb.Min.y + 2), ImVec2(grab_bb.Max.x + valuesize.x, frame_bb.Min.y + 14), GetColorU32(ImVec4(0.21f, 0.20f, 0.21f, 1.00f)), 3);
    window->DrawList->AddText(ImVec2(grab_bb.Min.x + 5, frame_bb.Min.y + 0), ImColor(255, 255, 255), value, label);
 
    
    return value_changed;
}
And an Example Slider:
Код:
bool ImGui::SliderFloat(const char* label, float* v, float v_min, float v_max, const char* display_format, float power, ImVec2 size, ImVec4 color)
{
    ImGuiWindow* window = GetCurrentWindow();
    if (window->SkipItems)
        return false;
 
    ImGuiContext& g = *GImGui;
    const ImGuiStyle& style = g.Style;
    const ImGuiID id = window->GetID(label);
    const float w = CalcItemWidth();
 
    const ImVec2 label_size = CalcTextSize(label, NULL, true)*2.7;
    const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y*0.5f));
    const ImRect total_bb(frame_bb.Min, frame_bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f));
 
    if (!ItemAdd(total_bb, &id))
    {
        ItemSize(total_bb, style.FramePadding.y);
        return false;
    }
 
    const bool hovered = IsHovered(frame_bb, id);
    if (hovered)
        SetHoveredID(id);
 
    if (!display_format)
        display_format = "%.3f";
    int decimal_precision = ParseFormatPrecision(display_format, 3);
 
    bool start_text_input = false;
    const bool tab_focus_requested = FocusableItemRegister(window, g.ActiveId == id);
    if (tab_focus_requested || (hovered && g.IO.MouseClicked[0]))
    {
        SetActiveID(id, window);
        FocusWindow(window);
 
        if (tab_focus_requested || g.IO.KeyCtrl)
        {
            start_text_input = true;
            g.ScalarAsInputTextId = 0;
        }
    }
    if (start_text_input || (g.ActiveId == id && g.ScalarAsInputTextId == id))
        return InputScalarAsWidgetReplacement(frame_bb, label, ImGuiDataType_Float, v, id, decimal_precision);
 
    ItemSize(total_bb, style.FramePadding.y);
 
    char value_buf[64];
    const char* value_buf_end = value_buf + ImFormatString(value_buf, IM_ARRAYSIZE(value_buf), display_format, *v);
    const bool value_changed = SliderBehavior(frame_bb, id, v, v_min, v_max, power, decimal_precision, 0, color, CalcTextSize(value_buf, NULL, true), value_buf_end, value_buf);
 
    if (label_size.x > 0.0f)
        RenderText(ImVec2(frame_bb.Min.x + style.ItemInnerSpacing.x, frame_bb.Min.y + 25), label);
 
    return value_changed;
}
Screenshots:
 
Сверху Снизу