@@ -16,6 +16,7 @@ class TextAlignment(IntEnum):
1616 RIGHT = 2
1717
1818
19+ ICON_PADDING = 15
1920DEFAULT_BUTTON_FONT_SIZE = 60
2021BUTTON_ENABLED_TEXT_COLOR = rl .Color (228 , 228 , 228 , 255 )
2122BUTTON_DISABLED_TEXT_COLOR = rl .Color (228 , 228 , 228 , 51 )
@@ -46,6 +47,7 @@ def gui_button(
4647 border_radius : int = 10 , # Corner rounding in pixels
4748 text_alignment : TextAlignment = TextAlignment .CENTER ,
4849 text_padding : int = 20 , # Padding for left/right alignment
50+ icon = None ,
4951) -> int :
5052 result = 0
5153
@@ -68,20 +70,42 @@ def gui_button(
6870 rl .draw_rectangle_rounded (rect , roundness , 20 , rl .BLACK )
6971 rl .draw_rectangle_rounded_lines_ex (rect , roundness , 20 , 2 , rl .WHITE )
7072
73+ # Handle icon and text positioning
7174 font = gui_app .font (font_weight )
7275 text_size = rl .measure_text_ex (font , text , font_size , 0 )
7376 text_pos = rl .Vector2 (0 , rect .y + (rect .height - text_size .y ) // 2 ) # Vertical centering
7477
75- # Horizontal alignment
76- if text_alignment == TextAlignment .LEFT :
77- text_pos .x = rect .x + text_padding
78- elif text_alignment == TextAlignment .CENTER :
79- text_pos .x = rect .x + (rect .width - text_size .x ) // 2
80- elif text_alignment == TextAlignment .RIGHT :
81- text_pos .x = rect .x + rect .width - text_size .x - text_padding
82-
83- # Draw the button text
84- text_color = BUTTON_ENABLED_TEXT_COLOR if is_enabled else BUTTON_DISABLED_TEXT_COLOR
85- rl .draw_text_ex (font , text , text_pos , font_size , 0 , text_color )
78+ # Draw icon if provided
79+ if icon :
80+ icon_y = rect .y + (rect .height - icon .height ) / 2
81+ if text :
82+ if text_alignment == TextAlignment .LEFT :
83+ icon_x = rect .x + text_padding
84+ text_pos .x = icon_x + icon .width + ICON_PADDING
85+ elif text_alignment == TextAlignment .CENTER :
86+ total_width = icon .width + ICON_PADDING + text_size .x
87+ icon_x = rect .x + (rect .width - total_width ) / 2
88+ text_pos .x = icon_x + icon .width + ICON_PADDING
89+ else : # RIGHT
90+ text_pos .x = rect .x + rect .width - text_size .x - text_padding
91+ icon_x = text_pos .x - ICON_PADDING - icon .width
92+ else :
93+ # Center icon when no text
94+ icon_x = rect .x + (rect .width - icon .width ) / 2
95+
96+ rl .draw_texture_v (icon , rl .Vector2 (icon_x , icon_y ), rl .WHITE if is_enabled else rl .Color (255 , 255 , 255 , 100 ))
97+ else :
98+ # No icon, position text normally
99+ if text_alignment == TextAlignment .LEFT :
100+ text_pos .x = rect .x + text_padding
101+ elif text_alignment == TextAlignment .CENTER :
102+ text_pos .x = rect .x + (rect .width - text_size .x ) // 2
103+ elif text_alignment == TextAlignment .RIGHT :
104+ text_pos .x = rect .x + rect .width - text_size .x - text_padding
105+
106+ # Draw the button text if any
107+ if text :
108+ text_color = BUTTON_ENABLED_TEXT_COLOR if is_enabled else BUTTON_DISABLED_TEXT_COLOR
109+ rl .draw_text_ex (font , text , text_pos , font_size , 0 , text_color )
86110
87111 return result
0 commit comments