raysan5 / raygui

A simple and easy-to-use immediate-mode gui library
zlib License
3.36k stars 289 forks source link

GetTextBounds doesn't add padding to textBounds.x #337

Closed danielchasehooper closed 10 months ago

danielchasehooper commented 11 months ago

GetTextBounds doesn't add padding to textBounds.x - so there isn't a way to add padding on the left side of text https://github.com/raysan5/raygui/blob/cfb919177511866dbdb77baeb4b2c77ce4486987/src/raygui.h#L4612-L4619

raysan5 commented 11 months ago

Actually it is considered a bit down in the function because padding depends on text alignment and also the type of control:

// Get text bounds considering control bounds
static Rectangle GetTextBounds(int control, Rectangle bounds)
{
    Rectangle textBounds = bounds;

    textBounds.x = bounds.x + GuiGetStyle(control, BORDER_WIDTH);
    textBounds.y = bounds.y + GuiGetStyle(control, BORDER_WIDTH) + GuiGetStyle(control, TEXT_PADDING);
    textBounds.width = bounds.width - 2*GuiGetStyle(control, BORDER_WIDTH) - 2*GuiGetStyle(control, TEXT_PADDING);
    textBounds.height = bounds.height - 2*GuiGetStyle(control, BORDER_WIDTH) - 2*GuiGetStyle(control, TEXT_PADDING);    // NOTE: Text is processed line per line!

    // Depending on control, TEXT_PADDING and TEXT_ALIGNMENT properties could affect the text-bounds
    switch (control)
    {
        case COMBOBOX:
        case DROPDOWNBOX:
        case LISTVIEW:
            // TODO: Special cases (no label): COMBOBOX, DROPDOWNBOX, LISTVIEW
        case SLIDER:
        case CHECKBOX:
        case VALUEBOX:
        case SPINNER:
            // TODO: More special cases (label on side): SLIDER, CHECKBOX, VALUEBOX, SPINNER
        default:
        {
            // TODO: WARNING: TEXT_ALIGNMENT is already considered in GuiDrawText()
            if (GuiGetStyle(control, TEXT_ALIGNMENT) == TEXT_ALIGN_RIGHT) textBounds.x -= GuiGetStyle(control, TEXT_PADDING);
            else textBounds.x += GuiGetStyle(control, TEXT_PADDING);
        }
        break;
    }

    return textBounds;
}
danielchasehooper commented 11 months ago

hmm. good point. I wasn't able to add padding and when I looked at the code I misattributed the cause by misreading the code. maybe I was setting DEFAULT when it should've been some other control type. Will try to recreate the issue.