From 166c534c62acdf983548b129c2a07c41ef0c08f7 Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 8 Nov 2018 14:44:15 +0100 Subject: [PATCH] Added GuiLabelEx() --- src/raygui.h | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/raygui.h b/src/raygui.h index 0a9e1fa..bc1e9a9 100644 --- a/src/raygui.h +++ b/src/raygui.h @@ -388,6 +388,7 @@ RAYGUIDEF Vector2 GuiScrollPanel(Rectangle bounds, Rectangle content, Vector2 vi // Basic controls set RAYGUIDEF void GuiLabel(Rectangle bounds, const char *text); // Label control, shows text +RAYGUIDEF void GuiLabelEx(Rectangle bounds, const char *text, int alignment, int padding); // Label control extended RAYGUIDEF bool GuiButton(Rectangle bounds, const char *text); // Button control, returns true when clicked RAYGUIDEF bool GuiLabelButton(Rectangle bounds, const char *text); // Label button control, show true when clicked RAYGUIDEF bool GuiImageButton(Rectangle bounds, Texture2D texture); // Image button control, returns true when clicked @@ -1021,7 +1022,7 @@ RAYGUIDEF void GuiGroupBox(Rectangle bounds, const char *text) DrawRectangle(bounds.x, bounds.y, GROUPBOX_LINE_THICK, bounds.height, Fade(GetColor(style[DEFAULT_LINES_COLOR]), guiAlpha)); DrawRectangle(bounds.x, bounds.y + bounds.height - 1, bounds.width, GROUPBOX_LINE_THICK, Fade(GetColor(style[DEFAULT_LINES_COLOR]), guiAlpha)); DrawRectangle(bounds.x + bounds.width - 1, bounds.y, GROUPBOX_LINE_THICK, bounds.height, Fade(GetColor(style[DEFAULT_LINES_COLOR]), guiAlpha)); - if (text[0] == '\0') DrawRectangle(bounds.x, bounds.y, bounds.width, GROUPBOX_LINE_THICK, Fade(GetColor(style[DEFAULT_LINES_COLOR]), guiAlpha)); + if ((text == NULL) || (text[0] == '\0')) DrawRectangle(bounds.x, bounds.y, bounds.width, GROUPBOX_LINE_THICK, Fade(GetColor(style[DEFAULT_LINES_COLOR]), guiAlpha)); else { DrawRectangle(bounds.x, bounds.y, GROUPBOX_TEXT_PADDING, GROUPBOX_LINE_THICK, Fade(GetColor(style[DEFAULT_LINES_COLOR]), guiAlpha)); @@ -1036,7 +1037,7 @@ RAYGUIDEF void GuiGroupBox(Rectangle bounds, const char *text) DrawRectangle(bounds.x, bounds.y, GROUPBOX_LINE_THICK, bounds.height, Fade(GetColor(style[DEFAULT_BORDER_COLOR_DISABLED]), guiAlpha)); DrawRectangle(bounds.x, bounds.y + bounds.height - 1, bounds.width, GROUPBOX_LINE_THICK, Fade(GetColor(style[DEFAULT_BORDER_COLOR_DISABLED]), guiAlpha)); DrawRectangle(bounds.x + bounds.width - 1, bounds.y, GROUPBOX_LINE_THICK, bounds.height, Fade(GetColor(style[DEFAULT_BORDER_COLOR_DISABLED]), guiAlpha)); - if (text[0] == '\0') DrawRectangle(bounds.x, bounds.y, bounds.width, GROUPBOX_LINE_THICK, Fade(GetColor(style[DEFAULT_LINES_COLOR]), guiAlpha)); + if ((text == NULL) || (text[0] == '\0')) DrawRectangle(bounds.x, bounds.y, bounds.width, GROUPBOX_LINE_THICK, Fade(GetColor(style[DEFAULT_LINES_COLOR]), guiAlpha)); else { DrawRectangle(bounds.x, bounds.y, GROUPBOX_TEXT_PADDING, GROUPBOX_LINE_THICK, Fade(GetColor(style[DEFAULT_BORDER_COLOR_DISABLED]), guiAlpha)); @@ -1108,6 +1109,13 @@ RAYGUIDEF Vector2 GuiScrollPanel(Rectangle bounds, Rectangle content, Vector2 vi // Label control RAYGUIDEF void GuiLabel(Rectangle bounds, const char *text) +{ + GuiLabelEx(bounds, text, 0, 0); // Left-alignment, no-padding +} + +// Label control extended +// NOTE: Alignment and padding are just considered on horizontal +RAYGUIDEF void GuiLabelEx(Rectangle bounds, const char *text, int alignment, int padding) { GuiControlState state = guiState; @@ -1116,6 +1124,11 @@ RAYGUIDEF void GuiLabel(Rectangle bounds, const char *text) if (bounds.width < textWidth) bounds.width = textWidth; if (bounds.height < textHeight) bounds.height = textHeight; + + Vector2 textPosition = { bounds.x + padding, bounds.y + bounds.height/2 - textHeight/2 + VALIGN_OFFSET(bounds.height) }; // Left alignment + + if (alignment == 1) textPosition.x = bounds.x + bounds.width/2 - textWidth/2 + padding; // Middle-alignment + else if (alignment == 2) textPosition.x = bounds.x + bounds.width - textWidth + padding; // Right alignment // Update control //-------------------------------------------------------------------- @@ -1128,8 +1141,8 @@ RAYGUIDEF void GuiLabel(Rectangle bounds, const char *text) { case NORMAL: case FOCUSED: - case PRESSED: GuiDrawText(text, bounds.x, bounds.y + bounds.height/2 - textHeight/2 + VALIGN_OFFSET(bounds.height), Fade(GetColor(style[LABEL_TEXT_COLOR_NORMAL]), guiAlpha)); break; - case DISABLED: GuiDrawText(text, bounds.x, bounds.y + bounds.height/2 - textHeight/2 + VALIGN_OFFSET(bounds.height), Fade(GetColor(style[LABEL_TEXT_COLOR_DISABLED]), guiAlpha)); break; + case PRESSED: GuiDrawText(text, textPosition.x, textPosition.y, Fade(GetColor(style[LABEL_TEXT_COLOR_NORMAL]), guiAlpha)); break; + case DISABLED: GuiDrawText(text, textPosition.x, textPosition.y, Fade(GetColor(style[LABEL_TEXT_COLOR_DISABLED]), guiAlpha)); break; default: break; } //--------------------------------------------------------------------