C++ Исходник Onetap v2 slider on imgui with animations

Олдфаг
Статус
Оффлайн
Регистрация
4 Янв 2020
Сообщения
2,985
Реакции[?]
1,256
Поинты[?]
16K
C++:
bool ImGui::SliderScalar(const char* label, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format, ImGuiSliderFlags flags)
{
    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();

    static std::map<ImGuiID, float> alpha_anim;
    auto it_alpha = alpha_anim.find(id);

    if (it_alpha == alpha_anim.end()) //если массив закончился
    {
        alpha_anim.insert({ id, 0 }); //заполняем контейнер, 55 - стартовая прозрачность
        it_alpha = alpha_anim.find(id); //задаем значение
    }

    const ImVec2 label_size = CalcTextSize(label, NULL, true);
    const ImRect frame_bb(window->DC.CursorPos + ImVec2(135 , 3), window->DC.CursorPos - ImVec2(-70, 3) + ImVec2(w, label_size.y + style.FramePadding.y * 2.0f));
    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));

    ItemSize(total_bb, style.FramePadding.y);
    if (!ItemAdd(total_bb, id, &frame_bb))
        return false;

    // Default format string when passing NULL
    if (format == NULL)
        format = DataTypeGetInfo(data_type)->PrintFmt;
    else if (data_type == ImGuiDataType_S32 && strcmp(format, "%d") != 0) // (FIXME-LEGACY: Patch old "%.0f" format string to use "%d", read function more details.)
        format = PatchFormatStringFloatToInt(format);

    // Tabbing or CTRL-clicking on Slider turns it into an input box
    const bool hovered = ItemHoverable(frame_bb, id);
    const bool thovered = ItemHoverable(total_bb, id);
    const bool temp_input_allowed = (flags & ImGuiSliderFlags_NoInput) == 0;
    bool temp_input_is_active = temp_input_allowed && TempInputIsActive(id);
    if (!temp_input_is_active)
    {
        const bool focus_requested = temp_input_allowed && FocusableItemRegister(window, id);
        const bool clicked = (hovered && g.IO.MouseClicked[0]);
        if (focus_requested || clicked || g.NavActivateId == id || g.NavInputId == id)
        {
            SetActiveID(id, window);
            SetFocusID(id, window);
            FocusWindow(window);
            g.ActiveIdUsingNavDirMask |= (1 << ImGuiDir_Left) | (1 << ImGuiDir_Right);
            if (temp_input_allowed && (focus_requested || (clicked && g.IO.KeyCtrl) || g.NavInputId == id))
            {
                temp_input_is_active = true;
                FocusableItemUnregister(window);
            }
        }
    }

    // Draw frame
    const ImU32 frame_col = GetColorU32(g.ActiveId == id ? ImGuiCol_FrameBgActive : g.HoveredId == id ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg);
    RenderNavHighlight(frame_bb, id);
    RenderFrame(frame_bb.Min, frame_bb.Max, ImColor(33, 35, 40), true, g.Style.FrameRounding);

    // Slider behavior
    ImRect grab_bb;
    const bool value_changed = SliderBehavior(frame_bb, id, data_type, p_data, p_min, p_max, format, flags, &grab_bb);
    if (value_changed)
        MarkItemEdited(id);

    // Render grab
    if (grab_bb.Max.x > grab_bb.Min.x)
        window->DrawList->AddRectFilled(frame_bb.Min, ImVec2(grab_bb.Max.x + 2.f, frame_bb.Max.y), ColorConvertFloat4ToU32(ImVec4(titleCol[0], titleCol[1], titleCol[2], 1.f)), 4.f);

    // Display value using user-provided display format so user can add prefix/suffix/decorations to the value.
    char value_buf[64];
    const char* value_buf_end = value_buf + DataTypeFormatString(value_buf, IM_ARRAYSIZE(value_buf), data_type, p_data, format);

    ImVec2 value_size = CalcTextSize(value_buf, value_buf_end, true);

    if (thovered) {
        if (it_alpha->second < 255) //макс. прозрачность
            it_alpha->second += 7; //8 - скорость появления
    }

    else {
        if (it_alpha->second > 0) //прозрачность после анимации
            it_alpha->second -= 7; //8 - скорость исчезновения
    }

    if (value_size.x > 0 && it_alpha->second > 0) {
        auto value_col = GetColorU32(ImGuiCol_FrameBg, it_alpha->second / 255);
        PushStyleVar(ImGuiStyleVar_Alpha, it_alpha->second / 255);
        window->DrawList->AddRectFilled(frame_bb.Max - ImVec2(frame_bb.Max.x - frame_bb.Min.x, 0) / 2 - ImVec2(value_size.x / 2 + 5.f, 0.f) - ImVec2(0, 28), frame_bb.Max - ImVec2(frame_bb.Max.x - frame_bb.Min.x, 0) / 2 + ImVec2(value_size.x / 2 + 5.f, value_size.y + 1) - ImVec2(0, 28), value_col, 4.f, 15);
        PopStyleVar();
        PushStyleColor(ImGuiCol_Text, ColorConvertU32ToFloat4(ImColor(157, 164, 183, (int)it_alpha->second)));
        RenderTextClipped(frame_bb.Min - ImVec2(0, 50), frame_bb.Max + ImVec2(0, 15), value_buf, value_buf_end, NULL, ImVec2(0.5f, 0.5f));
        PopStyleColor();
    }

    PushStyleColor(ImGuiCol_Text, ColorConvertU32ToFloat4(ImColor(157, 164, 183)));
    if (label_size.x > 0.0f)
        RenderText(ImVec2(frame_bb.Max.x - 256, frame_bb.Min.y - 3.2f), label);
    PopStyleColor();

    IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags);
    return value_changed;
}
 
Последнее редактирование:
Сверху Снизу