Исходник ImGui KeyButton

Статус
В этой теме нельзя размещать новые ответы.

rraggerr

проверенный какой-то
Автор темы
1,626
847
Обратите внимание, пользователь заблокирован на форуме. Не рекомендуется проводить сделки.
C++:
void ImGui::RenderTextClipped(const ImVec2& pos_min, const ImVec2& pos_max, const char* text, const char* text_end, const ImVec2* text_size_if_known, ImGuiAlign align, const ImVec2* clip_min, const ImVec2* clip_max)
{
    // Hide anything after a '##' string
    const char* text_display_end = FindRenderedTextEnd(text, text_end);
    const int text_len = (int)(text_display_end - text);
    if (text_len == 0)
        return;

    ImGuiContext& g = *GImGui;
    ImGuiWindow* window = GetCurrentWindow();

    // Perform CPU side clipping for single clipped element to avoid using scissor state
    ImVec2 pos = pos_min;
    const ImVec2 text_size = text_size_if_known ? *text_size_if_known : CalcTextSize(text, text_display_end, false, 0.0f);

    if (!clip_max) clip_max = &pos_max;
    bool need_clipping = (pos.x + text_size.x >= clip_max->x) || (pos.y + text_size.y >= clip_max->y);
    if (!clip_min) clip_min = &pos_min; else need_clipping |= (pos.x < clip_min->x) || (pos.y < clip_min->y);

    // Align
    if (align & ImGuiAlign_Center) pos.x = ImMax(pos.x, (pos.x + pos_max.x - text_size.x) * 0.5f);
    else if (align & ImGuiAlign_Right) pos.x = ImMax(pos.x, pos_max.x - text_size.x);
    if (align & ImGuiAlign_VCenter) pos.y = ImMax(pos.y, (pos.y + pos_max.y - text_size.y) * 0.5f);

    // Render
    if (need_clipping)
    {
        ImVec4 fine_clip_rect(clip_min->x, clip_min->y, clip_max->x, clip_max->y);
        window->DrawList->AddText(g.Font, g.FontSize, pos, GetColorU32(ImGuiCol_Text), text, text_display_end, 0.0f, &fine_clip_rect);
    }
    else
    {
        window->DrawList->AddText(g.Font, g.FontSize, pos, GetColorU32(ImGuiCol_Text), text, text_display_end, 0.0f, NULL);
    }
    if (g.LogEnabled)
        LogRenderedText(pos, text, text_display_end);
}
C++:
enum ImGuiAlign_
{
    ImGuiAlign_Left     = 1 << 0,
    ImGuiAlign_Center   = 1 << 1,
    ImGuiAlign_Right    = 1 << 2,
    ImGuiAlign_Top      = 1 << 3,
    ImGuiAlign_VCenter  = 1 << 4,
    ImGuiAlign_Default  = ImGuiAlign_Left | ImGuiAlign_Top
};
C++:
bool ImGui::KeyButton( const char* label, int* key, const ImVec2& size_arg )
{
    ImGuiWindow* window = GetCurrentWindow();

    if( window->SkipItems )
        return false;

    ImGuiContext& g = *GImGui;
    const ImGuiIO& io = g.IO;
    const ImGuiStyle& style = g.Style;

    const ImGuiID id = window->GetID( label );

    ImVec2 label_size = CalcTextSize( label, NULL, true );
    ImVec2 size = CalcItemSize( size_arg, CalcItemWidth(), label_size.y + style.FramePadding.y * 2.0f );

    const ImRect frame_bb( window->DC.CursorPos, window->DC.CursorPos + size );
    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 ) )
        return false;

    bool value_changed = false;

    char* active_text = nullptr;

    bool hovered = false, held = false;

    if( g.ActiveId == id )
    {
        if( !g.ActiveIdIsJustActivated )
        {
            for( int i = 0; i < 124; i++ )
            {
                if( g.IO.KeysDown[ i ] )
                {
                    SetActiveID( 0 );
                    *key = i;
                    value_changed = true;
                    break;
                }
            }
        }

        active_text = "...";
    }
    else
    {
        hovered = IsHovered( frame_bb, id );

        if( hovered )
        {
            SetHoveredID( id );

            if( g.IO.MouseDown[ 0 ] )
            {
                held = true;
                FocusWindow( window );
            }
            else if( g.IO.MouseReleased[ 0 ] )
            {
                SetActiveID( id );
            }
        }

        active_text = ( char* )GetNameFromCode( *key ).c_str();
    }

    const ImU32 col = GetColorU32( ( hovered && held ) ? ImGuiCol_FrameBgActive : hovered ? ImGuiCol_FrameBgHovered : ImGuiCol_FrameBg );
    RenderFrame( frame_bb.Min, frame_bb.Max, col, true, style.FrameRounding ); // main frame

    if( active_text ) // active text drawing
    {
        label_size = CalcTextSize( active_text, NULL, true );
        RenderTextClipped( frame_bb.Min, frame_bb.Max, active_text, NULL, &label_size, ImGuiAlign_Center | ImGuiAlign_VCenter );
    }

    RenderText( ImVec2( frame_bb.Max.x + style.ItemInnerSpacing.x, frame_bb.Min.y + style.FramePadding.y ), label ); // render item lable

    return value_changed;
}
Использование :

C++:
int Key = 0;    //  Первоначальная кнопка
ImGui::KeyButton(XorStr("Bind Key"), &Settings::Main->Key, ImVec2(50,50));
Получаем поле, при нажатии на которое идет получение нажатий клавиш(любых)
43fd8f90db64.png


Если уже есть в MoonGui то извинити я низняль
 

rraggerr

проверенный какой-то
Автор темы
1,626
847
Обратите внимание, пользователь заблокирован на форуме. Не рекомендуется проводить сделки.
Статус
В этой теме нельзя размещать новые ответы.