Вопрос Help me with Checkbox[imgui]

Пользователь
Статус
Оффлайн
Регистрация
21 Авг 2020
Сообщения
115
Реакции[?]
45
Поинты[?]
0
I just want to reverse the checkbox with text. Where is "Hud" i want this checkbox and after this text
Like as in 2 picture



C++:
struct
{
    std::map<ImGuiID, float> slider_size;
    std::map<ImGuiID, float> slider_old_value;
    std::map<ImGuiID, float> slider_active_value;
    std::map<ImGuiID, ImVec4> slider_hover_color;

    std::map<ImGuiID, ImVec4> begin_combo_hover_color;
    std::map<ImGuiID, float> begin_combo_size_active;

    std::map<ImGuiID, float> checkbox_pos_active;
    std::map<ImGuiID, float> checkbox_size_active;

    std::map<ImGuiID, ImVec4> tab_button_hover_color;

    std::map<ImGuiID, ImVec4> selectable_hover_color;
    std::map<ImGuiID, ImVec4> custom_button_hover_color;
    std::map<ImGuiID, ImVec4> custom_button_rect_hover_color;
} m_Animations;
bool ImGui::Checkbox(const char* label, bool* v)
{
    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 = 12;
    const ImVec2 pos = window->DC.CursorPos;
    const ImRect total_bb(pos + ImVec2(window->Size.x - 64, 0), pos + ImVec2(window->Size.x - 24, square_sz + 4));

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

    bool hovered, held;
    bool pressed = ButtonBehavior(total_bb, id, &hovered, &held);
    if (pressed)
    {
        *v = !(*v);
        MarkItemEdited(id);
    }

    const ImRect check_bb(pos + ImVec2(window->Size.x - 50, 0), pos + ImVec2(window->Size.x - 24, square_sz));
    RenderNavHighlight(total_bb, id);

    window->DrawList->AddRectFilled(check_bb.Min, check_bb.Max, ImColor(36, 36, 36, (int)(g.Style.Alpha * 255)), 10);

    //static float pos_active = 3.f;
    //static float size_active = 6.f;

    //static std::map<ImGuiID, float> pos_active;
    //static std::map<ImGuiID, float> size_active;

    auto it_pos_active = m_Animations.checkbox_pos_active.find(id);
    auto it_size_active = m_Animations.checkbox_size_active.find(id);

    if (it_pos_active == m_Animations.checkbox_pos_active.end())
    {
     m_Animations.checkbox_pos_active.insert({ id, 3.f });
        it_pos_active = m_Animations.checkbox_pos_active.find(id);
    }
    if (it_size_active == m_Animations.checkbox_size_active.end())
    {
       m_Animations.checkbox_size_active.insert({ id, 6.f });
        it_size_active =m_Animations.checkbox_size_active.find(id);
    }

    if (*v)
    {
        if (it_pos_active->second < 22)
            it_pos_active->second += 1.5f;

        if (it_size_active->second < 8)
            it_size_active->second += 0.1f;

        window->DrawList->AddRectFilled(check_bb.Min, ImVec2(check_bb.Min.x + it_pos_active->second, check_bb.Max.y), ImColor(40, 40, 40, (int)(g.Style.Alpha * 255)), 10);
        window->DrawList->AddRect(check_bb.Min, check_bb.Max, ImColor(60 / 255.f, 60 / 255.f, 60 / 255.f, g.Style.Alpha), 10);
        window->DrawList->AddCircleFilled(check_bb.Min + ImVec2(it_pos_active->second, 6), it_size_active->second, ImColor(96, 124, 255, (int)(g.Style.Alpha * 255)), 16);
    }
    else
    {
        if (it_pos_active->second > 3)
            it_pos_active->second -= 1.5f;

        if (it_size_active->second > 6)
            it_size_active->second -= 0.1f;

        window->DrawList->AddRectFilled(check_bb.Min, ImVec2(check_bb.Min.x + it_pos_active->second, check_bb.Max.y), ImColor(40, 40, 40, (int)(g.Style.Alpha * 255)), 10);
        window->DrawList->AddRect(check_bb.Min, check_bb.Max, ImColor(60 / 255.f, 60 / 255.f, 60 / 255.f, g.Style.Alpha), 10);
        window->DrawList->AddCircleFilled(check_bb.Min + ImVec2(it_pos_active->second, 6), it_size_active->second, ImColor(76, 76, 76, (int)(g.Style.Alpha * 255)), 16);
    }


    if (g.LogEnabled)
        LogRenderedText(&total_bb.Min, *v ? "[x]" : "[ ]");
    if (label_size.x > 0.0f)
        RenderText(ImVec2(pos.x, total_bb.Min.y - 5), label);



    IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0));
    return pressed;
}
 

Вложения

  • 1.2 KB Просмотры: 94
  • 1 KB Просмотры: 84
Участник
Статус
Оффлайн
Регистрация
15 Янв 2021
Сообщения
492
Реакции[?]
289
Поинты[?]
79K
Maybe you need Toggle Button ? It's simpliest to modify:

C++:
void ToggleButton(const char* str_id, bool* v)
{
    ImVec2 p = ImGui::GetCursorScreenPos();
    ImDrawList* draw_list = ImGui::GetWindowDrawList();

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

    if (ImGui::InvisibleButton(str_id, ImVec2(width, height)))
        *v = !*v;
    ImU32 col_bg;
    if (ImGui::IsItemHovered())
        col_bg = *v ? IM_COL32(145+20, 211, 68+20, 255) : IM_COL32(218-20, 218-20, 218-20, 255);
    else
        col_bg = *v ? IM_COL32(145, 211, 68, 255) : IM_COL32(218, 218, 218, 255);

    draw_list->AddRectFilled(p, ImVec2(p.x + width, p.y + height), col_bg, height * 0.5f);
    draw_list->AddCircleFilled(ImVec2(*v ? (p.x + width - radius) : (p.x + radius), p.y + radius), radius - 1.5f, IM_COL32(255, 255, 255, 255));
}
 
Пользователь
Статус
Оффлайн
Регистрация
21 Авг 2020
Сообщения
115
Реакции[?]
45
Поинты[?]
0
if (label_size.x > 0.0f)
RenderText(ImVec2(pos.x + YOUR NUMBER , total_bb.Min.y - 5), label);
is not this
Maybe you need Toggle Button ? It's simpliest to modify:

C++:
void ToggleButton(const char* str_id, bool* v)
{
    ImVec2 p = ImGui::GetCursorScreenPos();
    ImDrawList* draw_list = ImGui::GetWindowDrawList();

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

    if (ImGui::InvisibleButton(str_id, ImVec2(width, height)))
        *v = !*v;
    ImU32 col_bg;
    if (ImGui::IsItemHovered())
        col_bg = *v ? IM_COL32(145+20, 211, 68+20, 255) : IM_COL32(218-20, 218-20, 218-20, 255);
    else
        col_bg = *v ? IM_COL32(145, 211, 68, 255) : IM_COL32(218, 218, 218, 255);

    draw_list->AddRectFilled(p, ImVec2(p.x + width, p.y + height), col_bg, height * 0.5f);
    draw_list->AddCircleFilled(ImVec2(*v ? (p.x + width - radius) : (p.x + radius), p.y + radius), radius - 1.5f, IM_COL32(255, 255, 255, 255));
}
no
 
Последнее редактирование:
Начинающий
Статус
Оффлайн
Регистрация
13 Мар 2022
Сообщения
89
Реакции[?]
22
Поинты[?]
4K
try this

C++:
struct
{
    std::map<ImGuiID, float> slider_size;
    std::map<ImGuiID, float> slider_old_value;
    std::map<ImGuiID, float> slider_active_value;
    std::map<ImGuiID, ImVec4> slider_hover_color;

    std::map<ImGuiID, ImVec4> begin_combo_hover_color;
    std::map<ImGuiID, float> begin_combo_size_active;

    std::map<ImGuiID, float> checkbox_pos_active;
    std::map<ImGuiID, float> checkbox_size_active;

    std::map<ImGuiID, ImVec4> tab_button_hover_color;

    std::map<ImGuiID, ImVec4> selectable_hover_color;
    std::map<ImGuiID, ImVec4> custom_button_hover_color;
    std::map<ImGuiID, ImVec4> custom_button_rect_hover_color;
} m_Animations;
bool ImGui::Checkbox(const char* label, bool* v)
{
    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 = 12;
    const ImVec2 pos = window->DC.CursorPos;
    const ImRect total_bb(pos + ImVec2(window->Size.x - 744, 0), pos + ImVec2(window->Size.x - 704, square_sz + 4)); // 14

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

    bool hovered, held;
    bool pressed = ButtonBehavior(total_bb, id, &hovered, &held);
    if (pressed)
    {
        *v = !(*v);
        MarkItemEdited(id);
    }

    const ImRect check_bb(pos + ImVec2(window->Size.x - 730, 0), pos + ImVec2(window->Size.x - 704, square_sz));
    RenderNavHighlight(total_bb, id);

    window->DrawList->AddRectFilled(check_bb.Min, check_bb.Max, ImColor(36, 36, 36, (int)(g.Style.Alpha * 255)), 10);

    //static float pos_active = 3.f;
    //static float size_active = 6.f;

    //static std::map<ImGuiID, float> pos_active;
    //static std::map<ImGuiID, float> size_active;

    auto it_pos_active = m_Animations.checkbox_pos_active.find(id);
    auto it_size_active = m_Animations.checkbox_size_active.find(id);

    if (it_pos_active == m_Animations.checkbox_pos_active.end())
    {
     m_Animations.checkbox_pos_active.insert({ id, 3.f });
        it_pos_active = m_Animations.checkbox_pos_active.find(id);
    }
    if (it_size_active == m_Animations.checkbox_size_active.end())
    {
       m_Animations.checkbox_size_active.insert({ id, 6.f });
        it_size_active =m_Animations.checkbox_size_active.find(id);
    }

    if (*v)
    {
        if (it_pos_active->second < 22)
            it_pos_active->second += 1.5f;

        if (it_size_active->second < 8)
            it_size_active->second += 0.1f;

        window->DrawList->AddRectFilled(check_bb.Min, ImVec2(check_bb.Min.x + it_pos_active->second, check_bb.Max.y), ImColor(96, 124, 255, (int)(g.Style.Alpha * 255)), 10);
        window->DrawList->AddRect(check_bb.Min, check_bb.Max, ImColor(60 / 255.f, 60 / 255.f, 60 / 255.f, g.Style.Alpha), 10);
        window->DrawList->AddCircleFilled(check_bb.Min + ImVec2(it_pos_active->second, 6), it_size_active->second, ImColor(40, 40, 40, (int)(g.Style.Alpha * 255)), 16);
    }
    else
    {
        if (it_pos_active->second > 3)
            it_pos_active->second -= 1.5f;

        if (it_size_active->second > 6)
            it_size_active->second -= 0.1f;

        window->DrawList->AddRectFilled(check_bb.Min, ImVec2(check_bb.Min.x + it_pos_active->second, check_bb.Max.y), ImColor(40, 40, 40, (int)(g.Style.Alpha * 255)), 10);
        window->DrawList->AddRect(check_bb.Min, check_bb.Max, ImColor(60 / 255.f, 60 / 255.f, 60 / 255.f, g.Style.Alpha), 10);
        window->DrawList->AddCircleFilled(check_bb.Min + ImVec2(it_pos_active->second, 6), it_size_active->second, ImColor(76, 76, 76, (int)(g.Style.Alpha * 255)), 16);
    }


    if (g.LogEnabled)
        LogRenderedText(&total_bb.Min, *v ? "[x]" : "[ ]");
    if (label_size.x > 0.0f)
        RenderText(ImVec2(pos.x + 15, total_bb.Min.y - 5), label);



    IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0));
    return pressed;
}
 
Пользователь
Статус
Оффлайн
Регистрация
21 Авг 2020
Сообщения
115
Реакции[?]
45
Поинты[?]
0
try this

C++:
struct
{
    std::map<ImGuiID, float> slider_size;
    std::map<ImGuiID, float> slider_old_value;
    std::map<ImGuiID, float> slider_active_value;
    std::map<ImGuiID, ImVec4> slider_hover_color;

    std::map<ImGuiID, ImVec4> begin_combo_hover_color;
    std::map<ImGuiID, float> begin_combo_size_active;

    std::map<ImGuiID, float> checkbox_pos_active;
    std::map<ImGuiID, float> checkbox_size_active;

    std::map<ImGuiID, ImVec4> tab_button_hover_color;

    std::map<ImGuiID, ImVec4> selectable_hover_color;
    std::map<ImGuiID, ImVec4> custom_button_hover_color;
    std::map<ImGuiID, ImVec4> custom_button_rect_hover_color;
} m_Animations;
bool ImGui::Checkbox(const char* label, bool* v)
{
    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 = 12;
    const ImVec2 pos = window->DC.CursorPos;
    const ImRect total_bb(pos + ImVec2(window->Size.x - 744, 0), pos + ImVec2(window->Size.x - 704, square_sz + 4)); // 14

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

    bool hovered, held;
    bool pressed = ButtonBehavior(total_bb, id, &hovered, &held);
    if (pressed)
    {
        *v = !(*v);
        MarkItemEdited(id);
    }

    const ImRect check_bb(pos + ImVec2(window->Size.x - 730, 0), pos + ImVec2(window->Size.x - 704, square_sz));
    RenderNavHighlight(total_bb, id);

    window->DrawList->AddRectFilled(check_bb.Min, check_bb.Max, ImColor(36, 36, 36, (int)(g.Style.Alpha * 255)), 10);

    //static float pos_active = 3.f;
    //static float size_active = 6.f;

    //static std::map<ImGuiID, float> pos_active;
    //static std::map<ImGuiID, float> size_active;

    auto it_pos_active = m_Animations.checkbox_pos_active.find(id);
    auto it_size_active = m_Animations.checkbox_size_active.find(id);

    if (it_pos_active == m_Animations.checkbox_pos_active.end())
    {
     m_Animations.checkbox_pos_active.insert({ id, 3.f });
        it_pos_active = m_Animations.checkbox_pos_active.find(id);
    }
    if (it_size_active == m_Animations.checkbox_size_active.end())
    {
       m_Animations.checkbox_size_active.insert({ id, 6.f });
        it_size_active =m_Animations.checkbox_size_active.find(id);
    }

    if (*v)
    {
        if (it_pos_active->second < 22)
            it_pos_active->second += 1.5f;

        if (it_size_active->second < 8)
            it_size_active->second += 0.1f;

        window->DrawList->AddRectFilled(check_bb.Min, ImVec2(check_bb.Min.x + it_pos_active->second, check_bb.Max.y), ImColor(96, 124, 255, (int)(g.Style.Alpha * 255)), 10);
        window->DrawList->AddRect(check_bb.Min, check_bb.Max, ImColor(60 / 255.f, 60 / 255.f, 60 / 255.f, g.Style.Alpha), 10);
        window->DrawList->AddCircleFilled(check_bb.Min + ImVec2(it_pos_active->second, 6), it_size_active->second, ImColor(40, 40, 40, (int)(g.Style.Alpha * 255)), 16);
    }
    else
    {
        if (it_pos_active->second > 3)
            it_pos_active->second -= 1.5f;

        if (it_size_active->second > 6)
            it_size_active->second -= 0.1f;

        window->DrawList->AddRectFilled(check_bb.Min, ImVec2(check_bb.Min.x + it_pos_active->second, check_bb.Max.y), ImColor(40, 40, 40, (int)(g.Style.Alpha * 255)), 10);
        window->DrawList->AddRect(check_bb.Min, check_bb.Max, ImColor(60 / 255.f, 60 / 255.f, 60 / 255.f, g.Style.Alpha), 10);
        window->DrawList->AddCircleFilled(check_bb.Min + ImVec2(it_pos_active->second, 6), it_size_active->second, ImColor(76, 76, 76, (int)(g.Style.Alpha * 255)), 16);
    }


    if (g.LogEnabled)
        LogRenderedText(&total_bb.Min, *v ? "[x]" : "[ ]");
    if (label_size.x > 0.0f)
        RenderText(ImVec2(pos.x + 15, total_bb.Min.y - 5), label);



    IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0));
    return pressed;
}
not working
 
I Want to Die in New Orleans
Участник
Статус
Оффлайн
Регистрация
10 Окт 2020
Сообщения
507
Реакции[?]
491
Поинты[?]
80K
I just want to reverse the checkbox with text. Where is "Hud" i want this checkbox and after this text
Like as in 2 picture



C++:
struct
{
    std::map<ImGuiID, float> slider_size;
    std::map<ImGuiID, float> slider_old_value;
    std::map<ImGuiID, float> slider_active_value;
    std::map<ImGuiID, ImVec4> slider_hover_color;

    std::map<ImGuiID, ImVec4> begin_combo_hover_color;
    std::map<ImGuiID, float> begin_combo_size_active;

    std::map<ImGuiID, float> checkbox_pos_active;
    std::map<ImGuiID, float> checkbox_size_active;

    std::map<ImGuiID, ImVec4> tab_button_hover_color;

    std::map<ImGuiID, ImVec4> selectable_hover_color;
    std::map<ImGuiID, ImVec4> custom_button_hover_color;
    std::map<ImGuiID, ImVec4> custom_button_rect_hover_color;
} m_Animations;
bool ImGui::Checkbox(const char* label, bool* v)
{
    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 = 12;
    const ImVec2 pos = window->DC.CursorPos;
    const ImRect total_bb(pos + ImVec2(window->Size.x - 64, 0), pos + ImVec2(window->Size.x - 24, square_sz + 4));

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

    bool hovered, held;
    bool pressed = ButtonBehavior(total_bb, id, &hovered, &held);
    if (pressed)
    {
        *v = !(*v);
        MarkItemEdited(id);
    }

    const ImRect check_bb(pos + ImVec2(window->Size.x - 50, 0), pos + ImVec2(window->Size.x - 24, square_sz));
    RenderNavHighlight(total_bb, id);

    window->DrawList->AddRectFilled(check_bb.Min, check_bb.Max, ImColor(36, 36, 36, (int)(g.Style.Alpha * 255)), 10);

    //static float pos_active = 3.f;
    //static float size_active = 6.f;

    //static std::map<ImGuiID, float> pos_active;
    //static std::map<ImGuiID, float> size_active;

    auto it_pos_active = m_Animations.checkbox_pos_active.find(id);
    auto it_size_active = m_Animations.checkbox_size_active.find(id);

    if (it_pos_active == m_Animations.checkbox_pos_active.end())
    {
     m_Animations.checkbox_pos_active.insert({ id, 3.f });
        it_pos_active = m_Animations.checkbox_pos_active.find(id);
    }
    if (it_size_active == m_Animations.checkbox_size_active.end())
    {
       m_Animations.checkbox_size_active.insert({ id, 6.f });
        it_size_active =m_Animations.checkbox_size_active.find(id);
    }

    if (*v)
    {
        if (it_pos_active->second < 22)
            it_pos_active->second += 1.5f;

        if (it_size_active->second < 8)
            it_size_active->second += 0.1f;

        window->DrawList->AddRectFilled(check_bb.Min, ImVec2(check_bb.Min.x + it_pos_active->second, check_bb.Max.y), ImColor(40, 40, 40, (int)(g.Style.Alpha * 255)), 10);
        window->DrawList->AddRect(check_bb.Min, check_bb.Max, ImColor(60 / 255.f, 60 / 255.f, 60 / 255.f, g.Style.Alpha), 10);
        window->DrawList->AddCircleFilled(check_bb.Min + ImVec2(it_pos_active->second, 6), it_size_active->second, ImColor(96, 124, 255, (int)(g.Style.Alpha * 255)), 16);
    }
    else
    {
        if (it_pos_active->second > 3)
            it_pos_active->second -= 1.5f;

        if (it_size_active->second > 6)
            it_size_active->second -= 0.1f;

        window->DrawList->AddRectFilled(check_bb.Min, ImVec2(check_bb.Min.x + it_pos_active->second, check_bb.Max.y), ImColor(40, 40, 40, (int)(g.Style.Alpha * 255)), 10);
        window->DrawList->AddRect(check_bb.Min, check_bb.Max, ImColor(60 / 255.f, 60 / 255.f, 60 / 255.f, g.Style.Alpha), 10);
        window->DrawList->AddCircleFilled(check_bb.Min + ImVec2(it_pos_active->second, 6), it_size_active->second, ImColor(76, 76, 76, (int)(g.Style.Alpha * 255)), 16);
    }


    if (g.LogEnabled)
        LogRenderedText(&total_bb.Min, *v ? "[x]" : "[ ]");
    if (label_size.x > 0.0f)
        RenderText(ImVec2(pos.x, total_bb.Min.y - 5), label);



    IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags | ImGuiItemStatusFlags_Checkable | (*v ? ImGuiItemStatusFlags_Checked : 0));
    return pressed;
}

should work
if not, describe the problem and the question in more detail
 
Похожие темы
Сверху Снизу