changeset 605:a8094fad01a1

ui: rework styles
author David Demelier <markand@malikania.fr>
date Fri, 11 Aug 2023 19:22:00 +0200
parents dd80b59fffaf
children 35e58271d820
files examples/example-label/example-label.c examples/example-ui/example-ui.c libmlk-ui/mlk/ui/frame.c libmlk-ui/mlk/ui/label.c libmlk-ui/mlk/ui/style.c libmlk-ui/mlk/ui/style.h
diffstat 6 files changed, 89 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/examples/example-label/example-label.c	Thu Aug 10 20:08:00 2023 +0200
+++ b/examples/example-label/example-label.c	Fri Aug 11 19:22:00 2023 +0200
@@ -136,7 +136,7 @@
 	(void)self;
 
 	mlk_glower_update(&glower, ticks);
-	label->style->text_color = glower.color;
+	label->style->normal.color.text = glower.color;
 }
 
 static void
@@ -157,7 +157,7 @@
 	mlk_glower_init(&glower);
 
 	/* Change default style. */
-	mlk_style.text_color = 0x005162ff;
+	mlk_style.normal.color.text = 0x005162ff;
 
 	/* Copy default label delegate and style and adapt. */
 	style_glow = mlk_style;
--- a/examples/example-ui/example-ui.c	Thu Aug 10 20:08:00 2023 +0200
+++ b/examples/example-ui/example-ui.c	Fri Aug 11 19:22:00 2023 +0200
@@ -35,6 +35,7 @@
 #include <mlk/ui/frame.h>
 #include <mlk/ui/label.h>
 #include <mlk/ui/notify.h>
+#include <mlk/ui/style.h>
 #include <mlk/ui/ui.h>
 
 #include <mlk/example/example.h>
@@ -107,14 +108,18 @@
 			.x = FRAME_ORIGIN_X,
 			.y = FRAME_ORIGIN_Y,
 			.w = FRAME_WIDTH,
-			.h = FRAME_HEIGHT
+			.h = FRAME_HEIGHT,
+			.delegate = &mlk_frame_delegate,
+			.style = &mlk_style
 		}
 	},
 	.header = {
 		.label = {
 			.text = "Preferences",
 			.x = FRAME_ORIGIN_X,
-			.y = FRAME_ORIGIN_Y
+			.y = FRAME_ORIGIN_Y,
+			.delegate = &mlk_label_delegate,
+			.style = &mlk_style
 		}
 	},
 	.autosave = {
@@ -123,7 +128,9 @@
 			.h = ELEMENT_HEIGHT
 		},
 		.label = {
-			.text = "Auto save game"
+			.text = "Auto save game",
+			.delegate = &mlk_label_delegate,
+			.style = &mlk_style
 		}
 	},
 	.buttons = {
--- a/libmlk-ui/mlk/ui/frame.c	Thu Aug 10 20:08:00 2023 +0200
+++ b/libmlk-ui/mlk/ui/frame.c	Fri Aug 11 19:22:00 2023 +0200
@@ -28,14 +28,16 @@
 {
 	(void)self;
 
-	mlk_painter_set_color(frame->style->border_color);
+	const struct mlk_style_attr *attr = &frame->style->normal;
+
+	mlk_painter_set_color(attr->color.border);
 	mlk_painter_draw_rectangle(frame->x, frame->y, frame->w, frame->h);
-	mlk_painter_set_color(frame->style->bg_color);
+	mlk_painter_set_color(attr->color.bg);
 	mlk_painter_draw_rectangle(
-		frame->x + frame->style->border_size,
-		frame->y + frame->style->border_size,
-		frame->w - (frame->style->border_size * 2),
-		frame->h - (frame->style->border_size * 2)
+		frame->x + attr->geo.border,
+		frame->y + attr->geo.border,
+		frame->w - (attr->geo.border * 2),
+		frame->h - (attr->geo.border * 2)
 	);
 }
 
--- a/libmlk-ui/mlk/ui/label.c	Thu Aug 10 20:08:00 2023 +0200
+++ b/libmlk-ui/mlk/ui/label.c	Fri Aug 11 19:22:00 2023 +0200
@@ -33,7 +33,7 @@
 {
 	(void)self;
 
-	return mlk_font_query(label->style->text_font, label->text, w, h);
+	return mlk_font_query(label->style->normal.font, label->text, w, h);
 }
 
 static void
@@ -41,10 +41,12 @@
 {
 	(void)self;
 
+	const struct mlk_style_attr *attr = &label->style->normal;
+
 	mlk_ui_draw_text(
 		MLK_ALIGN_NONE,
-		label->style->text_font,
-		label->style->text_color,
+		attr->font,
+		attr->color.text,
 		label->text,
 		label->x,
 		label->y,
--- a/libmlk-ui/mlk/ui/style.c	Thu Aug 10 20:08:00 2023 +0200
+++ b/libmlk-ui/mlk/ui/style.c	Fri Aug 11 19:22:00 2023 +0200
@@ -4,22 +4,49 @@
 #include "style.h"
 #include "ui.h"
 
+/* https://lospec.com/palette-list/duel */
+
+#define NORMAL_COLOR_BG         (0xf5f7faff)
+#define NORMAL_COLOR_FG         (0x000000ff)
+#define NORMAL_COLOR_BORDER     (0xcdd2daff)
+#define NORMAL_COLOR_TEXT       (0x222323ff)
+#define NORMAL_GEO_BORDER       (2)
+#define NORMAL_GEO_PADDING      (10)
+#define NORMAL_FONT             (MLK_UI_FONT_INTERFACE)
+
+#define SELECTED_COLOR_BG       (0x328ca7ff)
+#define SELECTED_COLOR_FG       (0x000000ff)
+#define SELECTED_COLOR_BORDER   (0x006f89ff)
+#define SELECTED_COLOR_TEXT     (0xf5f7faff)
+#define SELECTED_GEO_BORDER     (2)
+#define SELECTED_GEO_PADDING    (10)
+#define SELECTED_FONT           (MLK_UI_FONT_INTERFACE)
+
 struct mlk_style mlk_style = {
-	.padding        = MLK_STYLE_PADDING,
-	.bg_color       = MLK_STYLE_BG_COLOR,
-	.fg_color       = MLK_STYLE_FG_COLOR,
-	.border_color   = MLK_STYLE_BORDER_COLOR,
-	.border_size    = MLK_STYLE_BORDER_SIZE,
-	.text_color     = MLK_STYLE_TEXT_COLOR,
-	.selected_color = MLK_STYLE_SELECTED_COLOR,
-	.animation      = MLK_STYLE_ANIMATION,
-	.text_font      = &mlk_ui_fonts[MLK_UI_FONT_INTERFACE]
+	.normal = {
+		.color = {
+			.bg      = NORMAL_COLOR_BG,
+			.fg      = NORMAL_COLOR_FG,
+			.border  = NORMAL_COLOR_BORDER,
+			.text    = NORMAL_COLOR_TEXT
+		},
+		.geo = {
+			.border  = NORMAL_GEO_BORDER,
+			.padding = NORMAL_GEO_PADDING
+		},
+		.font = &mlk_ui_fonts[NORMAL_FONT]
+	},
+	.selected = {
+		.color = {
+			.bg      = SELECTED_COLOR_BG,
+			.fg      = SELECTED_COLOR_FG,
+			.border  = SELECTED_COLOR_BORDER,
+			.text    = SELECTED_COLOR_TEXT
+		},
+		.geo = {
+			.border  = SELECTED_GEO_BORDER,
+			.padding = SELECTED_GEO_PADDING
+		},
+		.font = &mlk_ui_fonts[SELECTED_FONT]
+	}
 };
-
-void
-mlk_style_init(struct mlk_style *style)
-{
-	assert(style);
-
-	memset(style, 0, sizeof (*style));
-}
--- a/libmlk-ui/mlk/ui/style.h	Thu Aug 10 20:08:00 2023 +0200
+++ b/libmlk-ui/mlk/ui/style.h	Fri Aug 11 19:22:00 2023 +0200
@@ -1,34 +1,31 @@
 #ifndef MLK_UI_STYLE_H
 #define MLK_UI_STYLE_H
 
-/* https://lospec.com/palette-list/duel */
-
-#define MLK_STYLE_PADDING               (10)
-#define MLK_STYLE_BG_COLOR              (0xf5f7faff)
-#define MLK_STYLE_FG_COLOR              (0x000000ff)
-#define MLK_STYLE_BORDER_COLOR          (0xcdd2daff)
-#define MLK_STYLE_BORDER_SIZE           (2)
-#define MLK_STYLE_TEXT_COLOR            (0x222323ff)
-#define MLK_STYLE_SELECTED_COLOR        (0x55b67dff)
-#define MLK_STYLE_ANIMATION             (500)
-
 struct mlk_font;
 
+struct mlk_style_color {
+	unsigned long bg;
+	unsigned long fg;
+	unsigned long border;
+	unsigned long text;
+};
+
+struct mlk_style_geo {
+	unsigned short border;
+	unsigned short padding;
+};
+
+struct mlk_style_attr {
+	struct mlk_style_color color;
+	struct mlk_style_geo geo;
+	struct mlk_font *font;
+};
+
 struct mlk_style {
-	unsigned int padding;
-	unsigned long bg_color;
-	unsigned long fg_color;
-	unsigned long border_color;
-	unsigned int border_size;
-	unsigned long text_color;
-	unsigned long selected_color;
-	unsigned int animation;
-	struct mlk_font *text_font;
+	struct mlk_style_attr normal;
+	struct mlk_style_attr selected;
 };
 
 extern struct mlk_style mlk_style;
 
-void
-mlk_style_init(struct mlk_style *style);
-
 #endif /* MLK_UI_STYLE_H */