changeset 520:7e7c6786d21e

ui: button has now pressed style attributes
author David Demelier <markand@malikania.fr>
date Sat, 04 Mar 2023 15:10:35 +0100
parents 8b603a7e048a
children 338a4436e255
files examples/example-ui/button-glower.c examples/example-ui/example-ui.c libmlk-ui/mlk/ui/button.c libmlk-ui/mlk/ui/button.h
diffstat 4 files changed, 40 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/examples/example-ui/button-glower.c	Sat Mar 04 14:53:18 2023 +0100
+++ b/examples/example-ui/button-glower.c	Sat Mar 04 15:10:35 2023 +0100
@@ -23,12 +23,14 @@
 static void
 update(struct mlk_button_delegate *delegate, struct mlk_button *button, unsigned int ticks)
 {
-	(void)button;
-
 	struct button_glower *glower = delegate->data;
 
-	mlk_glower_update(&glower->glower, ticks);
-	glower->style.bg_color = glower->glower.color;
+	/* Don't update if pressed. */
+	if (!button->pressed) {
+		mlk_glower_update(&glower->glower, ticks);
+		glower->style.bg_color = glower->glower.color;
+		glower->style.pressed_bg_color = glower->glower.color;
+	}
 }
 
 void
@@ -37,6 +39,7 @@
 	assert(glower);
 
 	glower->style.bg_color = glower->glower.start;
+	glower->style.pressed_bg_color = glower->glower.start;
 	glower->delegate.data = glower;
 	glower->delegate.update = update;
 
--- a/examples/example-ui/example-ui.c	Sat Mar 04 14:53:18 2023 +0100
+++ b/examples/example-ui/example-ui.c	Sat Mar 04 15:10:35 2023 +0100
@@ -135,7 +135,9 @@
 		},
 		.quit_style = {
 			.bg_color = 0x24aed6ff,
-			.text_color = 0xffffffff
+			.pressed_bg_color = 0x328ca7ff,
+			.text_color = 0xffffffff,
+			.pressed_text_color = 0xffffffff
 		},
 		.quit = {
 			.text = "Quit",
@@ -150,7 +152,9 @@
 			},
 			.style = {
 				.text_color = 0xffffffff,
+				.pressed_text_color = 0xffffffff,
 				.border_color = BUTTON_STYLE_GLOW_COLOR_1,
+				.pressed_border_color = BUTTON_STYLE_GLOW_COLOR_1,
 				.border_size = 2
 			}
 		},
--- a/libmlk-ui/mlk/ui/button.c	Sat Mar 04 14:53:18 2023 +0100
+++ b/libmlk-ui/mlk/ui/button.c	Sat Mar 04 15:10:35 2023 +0100
@@ -57,10 +57,19 @@
 	(void)delegate;
 
 	const struct mlk_button_style *style = MLK__STYLE(button, mlk_button_style);
+	unsigned long long border_color, bg_color;
 
-	mlk_painter_set_color(style->border_color);
+	if (button->pressed) {
+		bg_color = style->pressed_bg_color;
+		border_color = style->pressed_border_color;
+	} else {
+		bg_color = style->bg_color;
+		border_color = style->border_color;
+	}
+
+	mlk_painter_set_color(border_color);
 	mlk_painter_draw_rectangle(button->x, button->y, button->w, button->h);
-	mlk_painter_set_color(style->bg_color);
+	mlk_painter_set_color(bg_color);
 	mlk_painter_draw_rectangle(
 		button->x + style->border_size,
 		button->y + style->border_size,
@@ -75,11 +84,17 @@
 	(void)delegate;
 
 	const struct mlk_button_style *style = MLK__STYLE(button, mlk_button_style);
+	unsigned long long text_color;
+
+	if (button->pressed)
+		text_color = style->pressed_text_color;
+	else
+		text_color = style->text_color;
 
 	mlk_ui_draw_text(
 		MLK_ALIGN_CENTER,
 		style_font(button),
-		style->text_color,
+		text_color,
 		button->text,
 		button->x,
 		button->y,
@@ -89,10 +104,13 @@
 }
 
 struct mlk_button_style mlk_button_style = {
-	.bg_color       = MLK_UI_COLOR_BG,
-	.text_color     = MLK_UI_COLOR_TEXT,
-	.border_color   = MLK_UI_COLOR_BORDER,
-	.border_size    = 1
+	.bg_color               = MLK_UI_COLOR_BG,
+	.pressed_bg_color       = 0xcdd2daff,
+	.border_color           = MLK_UI_COLOR_BORDER,
+	.pressed_border_color   = 0xa6aebaff,
+	.border_size            = 1,
+	.text_color             = MLK_UI_COLOR_TEXT,
+	.pressed_text_color     = MLK_UI_COLOR_TEXT
 };
 
 struct mlk_button_delegate mlk_button_delegate = {
--- a/libmlk-ui/mlk/ui/button.h	Sat Mar 04 14:53:18 2023 +0100
+++ b/libmlk-ui/mlk/ui/button.h	Sat Mar 04 15:10:35 2023 +0100
@@ -26,9 +26,12 @@
 
 struct mlk_button_style {
 	unsigned long bg_color;
+	unsigned long pressed_bg_color;
 	unsigned long border_color;
+	unsigned long pressed_border_color;
 	unsigned long border_size;
 	unsigned long text_color;
+	unsigned long pressed_text_color;
 	struct mlk_font *text_font;
 };