Исходник Нормальный чекбокс с анимацией

Эксперт
Статус
Оффлайн
Регистрация
30 Дек 2019
Сообщения
1,970
Реакции[?]
958
Поинты[?]
19K
maj0r я умею только критиковать?
1. Аля невервин

Код:
ImVec2 p = ImGui::GetCursorScreenPos();
    ImDrawList* draw_list = ImGui::GetWindowDrawList();

    float height = ImGui::GetFrameHeight();
    float width = height * 1.55f;
    float radius = height * 0.30f;

    ImGuiContext& g = *GImGui;
    const ImGuiStyle& style = g.Style;
    ImGuiWindow* window = ImGui::GetCurrentWindow();
    const ImGuiID id = window->GetID(label);
    const ImVec2 label_size = ImGui::CalcTextSize(label, NULL, true);

    const float square_sz = GetFrameHeight();
    const ImVec2 pos = window->DC.CursorPos;
    const ImRect total_bb(pos + ImVec2(0, 8), pos + ImVec2(0, 8) + ImVec2(square_sz + (label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f), label_size.y + style.FramePadding.y * 2.0f));
    if (!ItemAdd(total_bb, id))
        return false;

    ImGui::InvisibleButton(label, ImVec2(width, height));
    if (ImGui::IsItemClicked())
        *v = !*v;

    static std::map<ImGuiID, float> filled_animation;
    auto it_filled = filled_animation.find(id);
    if (it_filled == filled_animation.end())
    {
        filled_animation.insert({ id, 0.f });
        it_filled = filled_animation.find(id);
    }
    it_filled->second = ImClamp(it_filled->second + (2.35f * ImGui::GetIO().DeltaTime * ((*v) ? 1.f : -1.f)), 0.4f, 1.2f);
  
    ImU32 col_bg = ImGui::GetColorU32(ImLerp(ImVec4(0 / 255.f, 0 / 255.f, 12 / 255.f, 1.0f), ImVec4(7 / 255.f, 9 / 255.f, 41 / 255.f, 1.0f), it_filled->second - 0.4));
    ImU32 col_cm = ImGui::GetColorU32(ImLerp(ImVec4(78 / 255.f, 88 / 255.f, 98 / 255.f, 1.0f), ImVec4(0 / 255.f, 160 / 255.f, 238 / 255.f, 1.0f), it_filled->second - 0.4));

    draw_list->AddRectFilled(p + ImVec2(8, 6), ImVec2(p.x + width + 2, p.y + 18), col_bg, 6);
    draw_list->AddCircleFilled(ImVec2(p.x + radius + it_filled->second * (width - radius * 2), p.y + radius + 6), 6, col_cm);

    if (label_size.x > 0.0f)
    {
        RenderText(ImVec2((p.x + width) + 12, (p.y + height) - 15), label);
    }
2. Аля какая-то залупа

Код:
auto aslpha = float(ImGui::GetStyle().Alpha);
    ImGuiWindow* window = GetCurrentWindow();
    if (window->SkipItems)
        return false;

    ImGuiContext& g = *GImGui;
    const ImGuiStyle& style = g.Style;
    const ImGuiID id = window->GetID(label);
    const ImVec2 label_size = CalcTextSize(label, NULL, true);

    const float square_sz = GetFrameHeight();
    const ImVec2 pos = window->DC.CursorPos + ImVec2(5, 0);
    const ImRect total_bb(pos, pos + ImVec2(GetWindowSize().x - 5, 20));
    static int alpha = 140;
    float deltatime = 1.5f * ImGui::GetIO().DeltaTime;
    static std::map<ImGuiID, float> hover_animation;
    bool hovered, held;
    bool pressed = ButtonBehavior(total_bb, id, &hovered, &held);
    auto it_hover = hover_animation.find(id);
    if (it_hover == hover_animation.end())
    {
        hover_animation.insert({ id, 0.f });
        it_hover = hover_animation.find(id);
    }
    it_hover->second = ImClamp(it_hover->second + (0.2f * ImGui::GetIO().DeltaTime * (hovered ? 1.f : -5.f)), 0.0f, 0.12f);
    it_hover->second *= aslpha;
    static std::map<ImGuiID, float> filled_animation;
    auto it_filled = filled_animation.find(id);
    if (it_filled == filled_animation.end())
    {
        filled_animation.insert({ id, 0.f });
        it_filled = filled_animation.find(id);
    }
    it_filled->second = ImClamp(it_filled->second + (2.35f * ImGui::GetIO().DeltaTime * ((*v) ? 1.f : -2.f)), it_hover->second, 1.f);
    it_filled->second *= aslpha;
    ItemSize(total_bb, style.FramePadding.y);
    if (!ItemAdd(total_bb, id))
    {
        IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0));
        return false;
    }

    if (pressed)
    {
        *v = !(*v);
        MarkItemEdited(id);
    }
    float t = *v ? 1.0f : 0.0f;
    float ANIM_SPEED = 0.50f; // Bigger = Slower
    if (g.LastActiveId == g.CurrentWindow->GetID(label)) {
        float t_anim = ImSaturate(g.LastActiveIdTimer / ANIM_SPEED);
        t = *v ? (t_anim) : 0;
    }
    const ImRect check_bb(pos, pos + ImVec2(square_sz, square_sz));
    RenderNavHighlight(total_bb, id);
    ImVec2 label_pos = ImVec2(check_bb.Max.x + style.ItemInnerSpacing.x, check_bb.Min.y + style.FramePadding.y);
    ImU32 check_col = GetColorU32(ImGuiCol_CheckMark);
    bool mixed_value = (window->DC.ItemFlags & ImGuiItemFlags_MixedValue) != 0;
    const float pad = ImMax(1.0f, IM_FLOOR(square_sz / 6.0f));
    window->DrawList->AddRect(ImVec2(check_bb.Min.x + pad - 1, check_bb.Min.y + pad - 1), ImVec2(check_bb.Max.x - pad + 1, check_bb.Max.y - pad + 1), ImColor(60, 60, 60, int(alpha * aslpha)), 2);
    if (it_filled->second)
    {
        window->DrawList->AddRectFilled(ImVec2(check_bb.Min.x + pad, check_bb.Min.y + pad), ImVec2(check_bb.Max.x - pad, check_bb.Max.y - pad), ImColor(130, 132, 170, int((it_filled->second * 205) * aslpha)), 2);
        ImGui::RenderCheckMark(window->DrawList, ImVec2(check_bb.Min.x + 4 + (4 - (4 * it_filled->second)), check_bb.Min.y + 4 + (4 - (4 * it_filled->second))), ImColor(0, 0, 0, int((it_filled->second * 205) * aslpha)), it_filled->second * 10);
    }

  
    if (g.LogEnabled)
        LogRenderedText(&label_pos, mixed_value ? "[~]" : *v ? "[x]" : "[ ]");
    if (label_size.x > 0.0f)
        window->DrawList->AddText(check_bb.Min + ImVec2(pad, pad) + ImVec2(18, 0), *v ? ImColor(250, 250, 250, int(220 * aslpha)) : ImColor(170, 170, 170, int(205 * aslpha)), label);

    IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0));
    return pressed;
 
Забаненный
Статус
Оффлайн
Регистрация
6 Дек 2019
Сообщения
643
Реакции[?]
322
Поинты[?]
0
Обратите внимание, пользователь заблокирован на форуме. Не рекомендуется проводить сделки.
maj0r я умею только критиковать?
1. Аля невервин

Код:
ImVec2 p = ImGui::GetCursorScreenPos();
    ImDrawList* draw_list = ImGui::GetWindowDrawList();

    float height = ImGui::GetFrameHeight();
    float width = height * 1.55f;
    float radius = height * 0.30f;

    ImGuiContext& g = *GImGui;
    const ImGuiStyle& style = g.Style;
    ImGuiWindow* window = ImGui::GetCurrentWindow();
    const ImGuiID id = window->GetID(label);
    const ImVec2 label_size = ImGui::CalcTextSize(label, NULL, true);

    const float square_sz = GetFrameHeight();
    const ImVec2 pos = window->DC.CursorPos;
    const ImRect total_bb(pos + ImVec2(0, 8), pos + ImVec2(0, 8) + ImVec2(square_sz + (label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f), label_size.y + style.FramePadding.y * 2.0f));
    if (!ItemAdd(total_bb, id))
        return false;

    ImGui::InvisibleButton(label, ImVec2(width, height));
    if (ImGui::IsItemClicked())
        *v = !*v;

    static std::map<ImGuiID, float> filled_animation;
    auto it_filled = filled_animation.find(id);
    if (it_filled == filled_animation.end())
    {
        filled_animation.insert({ id, 0.f });
        it_filled = filled_animation.find(id);
    }
    it_filled->second = ImClamp(it_filled->second + (2.35f * ImGui::GetIO().DeltaTime * ((*v) ? 1.f : -1.f)), 0.4f, 1.2f);
 
    ImU32 col_bg = ImGui::GetColorU32(ImLerp(ImVec4(0 / 255.f, 0 / 255.f, 12 / 255.f, 1.0f), ImVec4(7 / 255.f, 9 / 255.f, 41 / 255.f, 1.0f), it_filled->second - 0.4));
    ImU32 col_cm = ImGui::GetColorU32(ImLerp(ImVec4(78 / 255.f, 88 / 255.f, 98 / 255.f, 1.0f), ImVec4(0 / 255.f, 160 / 255.f, 238 / 255.f, 1.0f), it_filled->second - 0.4));

    draw_list->AddRectFilled(p + ImVec2(8, 6), ImVec2(p.x + width + 2, p.y + 18), col_bg, 6);
    draw_list->AddCircleFilled(ImVec2(p.x + radius + it_filled->second * (width - radius * 2), p.y + radius + 6), 6, col_cm);

    if (label_size.x > 0.0f)
    {
        RenderText(ImVec2((p.x + width) + 12, (p.y + height) - 15), label);
    }
2. Аля какая-то залупа

Код:
auto aslpha = float(ImGui::GetStyle().Alpha);
    ImGuiWindow* window = GetCurrentWindow();
    if (window->SkipItems)
        return false;

    ImGuiContext& g = *GImGui;
    const ImGuiStyle& style = g.Style;
    const ImGuiID id = window->GetID(label);
    const ImVec2 label_size = CalcTextSize(label, NULL, true);

    const float square_sz = GetFrameHeight();
    const ImVec2 pos = window->DC.CursorPos + ImVec2(5, 0);
    const ImRect total_bb(pos, pos + ImVec2(GetWindowSize().x - 5, 20));
    static int alpha = 140;
    float deltatime = 1.5f * ImGui::GetIO().DeltaTime;
    static std::map<ImGuiID, float> hover_animation;
    bool hovered, held;
    bool pressed = ButtonBehavior(total_bb, id, &hovered, &held);
    auto it_hover = hover_animation.find(id);
    if (it_hover == hover_animation.end())
    {
        hover_animation.insert({ id, 0.f });
        it_hover = hover_animation.find(id);
    }
    it_hover->second = ImClamp(it_hover->second + (0.2f * ImGui::GetIO().DeltaTime * (hovered ? 1.f : -5.f)), 0.0f, 0.12f);
    it_hover->second *= aslpha;
    static std::map<ImGuiID, float> filled_animation;
    auto it_filled = filled_animation.find(id);
    if (it_filled == filled_animation.end())
    {
        filled_animation.insert({ id, 0.f });
        it_filled = filled_animation.find(id);
    }
    it_filled->second = ImClamp(it_filled->second + (2.35f * ImGui::GetIO().DeltaTime * ((*v) ? 1.f : -2.f)), it_hover->second, 1.f);
    it_filled->second *= aslpha;
    ItemSize(total_bb, style.FramePadding.y);
    if (!ItemAdd(total_bb, id))
    {
        IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0));
        return false;
    }

    if (pressed)
    {
        *v = !(*v);
        MarkItemEdited(id);
    }
    float t = *v ? 1.0f : 0.0f;
    float ANIM_SPEED = 0.50f; // Bigger = Slower
    if (g.LastActiveId == g.CurrentWindow->GetID(label)) {
        float t_anim = ImSaturate(g.LastActiveIdTimer / ANIM_SPEED);
        t = *v ? (t_anim) : 0;
    }
    const ImRect check_bb(pos, pos + ImVec2(square_sz, square_sz));
    RenderNavHighlight(total_bb, id);
    ImVec2 label_pos = ImVec2(check_bb.Max.x + style.ItemInnerSpacing.x, check_bb.Min.y + style.FramePadding.y);
    ImU32 check_col = GetColorU32(ImGuiCol_CheckMark);
    bool mixed_value = (window->DC.ItemFlags & ImGuiItemFlags_MixedValue) != 0;
    const float pad = ImMax(1.0f, IM_FLOOR(square_sz / 6.0f));
    window->DrawList->AddRect(ImVec2(check_bb.Min.x + pad - 1, check_bb.Min.y + pad - 1), ImVec2(check_bb.Max.x - pad + 1, check_bb.Max.y - pad + 1), ImColor(60, 60, 60, int(alpha * aslpha)), 2);
    if (it_filled->second)
    {
        window->DrawList->AddRectFilled(ImVec2(check_bb.Min.x + pad, check_bb.Min.y + pad), ImVec2(check_bb.Max.x - pad, check_bb.Max.y - pad), ImColor(130, 132, 170, int((it_filled->second * 205) * aslpha)), 2);
        ImGui::RenderCheckMark(window->DrawList, ImVec2(check_bb.Min.x + 4 + (4 - (4 * it_filled->second)), check_bb.Min.y + 4 + (4 - (4 * it_filled->second))), ImColor(0, 0, 0, int((it_filled->second * 205) * aslpha)), it_filled->second * 10);
    }

 
    if (g.LogEnabled)
        LogRenderedText(&label_pos, mixed_value ? "[~]" : *v ? "[x]" : "[ ]");
    if (label_size.x > 0.0f)
        window->DrawList->AddText(check_bb.Min + ImVec2(pad, pad) + ImVec2(18, 0), *v ? ImColor(250, 250, 250, int(220 * aslpha)) : ImColor(170, 170, 170, int(205 * aslpha)), label);

    IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0));
    return pressed;
второй это вырвиглаз
 
Эксперт
Статус
Оффлайн
Регистрация
13 Сен 2020
Сообщения
1,415
Реакции[?]
713
Поинты[?]
10K
maj0r я умею только критиковать?
1. Аля невервин

Код:
ImVec2 p = ImGui::GetCursorScreenPos();
    ImDrawList* draw_list = ImGui::GetWindowDrawList();

    float height = ImGui::GetFrameHeight();
    float width = height * 1.55f;
    float radius = height * 0.30f;

    ImGuiContext& g = *GImGui;
    const ImGuiStyle& style = g.Style;
    ImGuiWindow* window = ImGui::GetCurrentWindow();
    const ImGuiID id = window->GetID(label);
    const ImVec2 label_size = ImGui::CalcTextSize(label, NULL, true);

    const float square_sz = GetFrameHeight();
    const ImVec2 pos = window->DC.CursorPos;
    const ImRect total_bb(pos + ImVec2(0, 8), pos + ImVec2(0, 8) + ImVec2(square_sz + (label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f), label_size.y + style.FramePadding.y * 2.0f));
    if (!ItemAdd(total_bb, id))
        return false;

    ImGui::InvisibleButton(label, ImVec2(width, height));
    if (ImGui::IsItemClicked())
        *v = !*v;

    static std::map<ImGuiID, float> filled_animation;
    auto it_filled = filled_animation.find(id);
    if (it_filled == filled_animation.end())
    {
        filled_animation.insert({ id, 0.f });
        it_filled = filled_animation.find(id);
    }
    it_filled->second = ImClamp(it_filled->second + (2.35f * ImGui::GetIO().DeltaTime * ((*v) ? 1.f : -1.f)), 0.4f, 1.2f);
 
    ImU32 col_bg = ImGui::GetColorU32(ImLerp(ImVec4(0 / 255.f, 0 / 255.f, 12 / 255.f, 1.0f), ImVec4(7 / 255.f, 9 / 255.f, 41 / 255.f, 1.0f), it_filled->second - 0.4));
    ImU32 col_cm = ImGui::GetColorU32(ImLerp(ImVec4(78 / 255.f, 88 / 255.f, 98 / 255.f, 1.0f), ImVec4(0 / 255.f, 160 / 255.f, 238 / 255.f, 1.0f), it_filled->second - 0.4));

    draw_list->AddRectFilled(p + ImVec2(8, 6), ImVec2(p.x + width + 2, p.y + 18), col_bg, 6);
    draw_list->AddCircleFilled(ImVec2(p.x + radius + it_filled->second * (width - radius * 2), p.y + radius + 6), 6, col_cm);

    if (label_size.x > 0.0f)
    {
        RenderText(ImVec2((p.x + width) + 12, (p.y + height) - 15), label);
    }
2. Аля какая-то залупа

Код:
auto aslpha = float(ImGui::GetStyle().Alpha);
    ImGuiWindow* window = GetCurrentWindow();
    if (window->SkipItems)
        return false;

    ImGuiContext& g = *GImGui;
    const ImGuiStyle& style = g.Style;
    const ImGuiID id = window->GetID(label);
    const ImVec2 label_size = CalcTextSize(label, NULL, true);

    const float square_sz = GetFrameHeight();
    const ImVec2 pos = window->DC.CursorPos + ImVec2(5, 0);
    const ImRect total_bb(pos, pos + ImVec2(GetWindowSize().x - 5, 20));
    static int alpha = 140;
    float deltatime = 1.5f * ImGui::GetIO().DeltaTime;
    static std::map<ImGuiID, float> hover_animation;
    bool hovered, held;
    bool pressed = ButtonBehavior(total_bb, id, &hovered, &held);
    auto it_hover = hover_animation.find(id);
    if (it_hover == hover_animation.end())
    {
        hover_animation.insert({ id, 0.f });
        it_hover = hover_animation.find(id);
    }
    it_hover->second = ImClamp(it_hover->second + (0.2f * ImGui::GetIO().DeltaTime * (hovered ? 1.f : -5.f)), 0.0f, 0.12f);
    it_hover->second *= aslpha;
    static std::map<ImGuiID, float> filled_animation;
    auto it_filled = filled_animation.find(id);
    if (it_filled == filled_animation.end())
    {
        filled_animation.insert({ id, 0.f });
        it_filled = filled_animation.find(id);
    }
    it_filled->second = ImClamp(it_filled->second + (2.35f * ImGui::GetIO().DeltaTime * ((*v) ? 1.f : -2.f)), it_hover->second, 1.f);
    it_filled->second *= aslpha;
    ItemSize(total_bb, style.FramePadding.y);
    if (!ItemAdd(total_bb, id))
    {
        IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0));
        return false;
    }

    if (pressed)
    {
        *v = !(*v);
        MarkItemEdited(id);
    }
    float t = *v ? 1.0f : 0.0f;
    float ANIM_SPEED = 0.50f; // Bigger = Slower
    if (g.LastActiveId == g.CurrentWindow->GetID(label)) {
        float t_anim = ImSaturate(g.LastActiveIdTimer / ANIM_SPEED);
        t = *v ? (t_anim) : 0;
    }
    const ImRect check_bb(pos, pos + ImVec2(square_sz, square_sz));
    RenderNavHighlight(total_bb, id);
    ImVec2 label_pos = ImVec2(check_bb.Max.x + style.ItemInnerSpacing.x, check_bb.Min.y + style.FramePadding.y);
    ImU32 check_col = GetColorU32(ImGuiCol_CheckMark);
    bool mixed_value = (window->DC.ItemFlags & ImGuiItemFlags_MixedValue) != 0;
    const float pad = ImMax(1.0f, IM_FLOOR(square_sz / 6.0f));
    window->DrawList->AddRect(ImVec2(check_bb.Min.x + pad - 1, check_bb.Min.y + pad - 1), ImVec2(check_bb.Max.x - pad + 1, check_bb.Max.y - pad + 1), ImColor(60, 60, 60, int(alpha * aslpha)), 2);
    if (it_filled->second)
    {
        window->DrawList->AddRectFilled(ImVec2(check_bb.Min.x + pad, check_bb.Min.y + pad), ImVec2(check_bb.Max.x - pad, check_bb.Max.y - pad), ImColor(130, 132, 170, int((it_filled->second * 205) * aslpha)), 2);
        ImGui::RenderCheckMark(window->DrawList, ImVec2(check_bb.Min.x + 4 + (4 - (4 * it_filled->second)), check_bb.Min.y + 4 + (4 - (4 * it_filled->second))), ImColor(0, 0, 0, int((it_filled->second * 205) * aslpha)), it_filled->second * 10);
    }

 
    if (g.LogEnabled)
        LogRenderedText(&label_pos, mixed_value ? "[~]" : *v ? "[x]" : "[ ]");
    if (label_size.x > 0.0f)
        window->DrawList->AddText(check_bb.Min + ImVec2(pad, pad) + ImVec2(18, 0), *v ? ImColor(250, 250, 250, int(220 * aslpha)) : ImColor(170, 170, 170, int(205 * aslpha)), label);

    IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0));
    return pressed;
гифки бы хоть прикрепил
 
..................................................
Участник
Статус
Оффлайн
Регистрация
13 Авг 2020
Сообщения
990
Реакции[?]
249
Поинты[?]
25K
даа давай, а хули кружок не по середине. Но всё равно красиво
1626535642272.png
Вот второй вариант
1626535801852.png
 
Эксперт
Статус
Оффлайн
Регистрация
30 Дек 2019
Сообщения
1,970
Реакции[?]
958
Поинты[?]
19K
Участник
Статус
Оффлайн
Регистрация
30 Авг 2020
Сообщения
660
Реакции[?]
396
Поинты[?]
16K
Сверху Снизу