changeset 100:ef9135c34505

message: improve rendering and add example
author David Demelier <markand@malikania.fr>
date Tue, 31 Mar 2020 10:57:14 +0200
parents 4ac71ac10c9f
children 0bc32f70d67c
files Makefile examples/example-message.c src/core/message.c src/core/message.h
diffstat 4 files changed, 50 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Mon Mar 30 23:08:23 2020 +0200
+++ b/Makefile	Tue Mar 31 10:57:14 2020 +0200
@@ -159,7 +159,7 @@
 doxygen:
 	doxygen doxygen/Doxyfile
 
-everything: ${PROG} ${TOOLS_PRGS} ${TESTS_PRGS}
+everything: ${PROG} ${EXAMPLES} ${TOOLS_PRGS} ${TESTS_PRGS}
 
 install:
 	mkdir -p ${DESTDIR}${BINDIR}
@@ -178,4 +178,4 @@
 	rm -f ${EXAMPLES_PRGS} ${EXAMPLES_OBJS} ${EXAMPLES_DEPS}
 	rm -f ${TOOLS_PRGS} ${TOOLS_DEPS}
 
-.PHONY: all clean doxygen everything tests tools
+.PHONY: all clean doxygen everything examples tests tools
--- a/examples/example-message.c	Mon Mar 30 23:08:23 2020 +0200
+++ b/examples/example-message.c	Tue Mar 31 10:57:14 2020 +0200
@@ -22,6 +22,7 @@
 #include <painter.h>
 #include <panic.h>
 #include <sys.h>
+#include <util.h>
 #include <theme.h>
 #include <window.h>
 
@@ -51,6 +52,7 @@
 	struct clock clock = {0};
 
 	message_start(msg);
+	clock_start(&clock);
 
 	while (msg->state) {
 		union event ev;
@@ -74,6 +76,9 @@
 		painter_clear();
 		message_draw(msg);
 		painter_present();
+
+		if ((elapsed = clock_elapsed(&clock)) < 20)
+			delay(20 - elapsed);
 	}
 }
 
--- a/src/core/message.c	Mon Mar 30 23:08:23 2020 +0200
+++ b/src/core/message.c	Tue Mar 31 10:57:14 2020 +0200
@@ -27,13 +27,14 @@
 #include "label.h"
 #include "message.h"
 #include "painter.h"
+#include "panic.h"
 #include "sprite.h"
 #include "texture.h"
 #include "theme.h"
 #include "util.h"
 #include "window.h"
 
-#define MESSAGE_SPEED   200     /* Time delay for animations */
+#define MESSAGE_SPEED   100     /* Time delay for animations */
 #define MESSAGE_TIMEOUT 5000    /* Time for auto-closing */
 
 #define WIDTH   (window.w * 0.75)
@@ -80,6 +81,7 @@
 	assert(msg);
 
 	msg->elapsed = 0;
+	msg->scale = 0.0;
 	msg->state = msg->flags & MESSAGE_QUICK ? MESSAGE_SHOWING : MESSAGE_OPENING;
 }
 
@@ -89,6 +91,11 @@
 	assert(msg);
 	assert(ev);
 
+	/* Skip if the message animation hasn't complete. */
+	if (msg->state != MESSAGE_SHOWING)
+		return;
+
+	/* Only keyboard event are valid. */
 	if (ev->type != EVENT_KEYDOWN || msg->state == MESSAGE_NONE)
 		return;
 
@@ -119,6 +126,11 @@
 
 	switch (msg->state) {
 	case MESSAGE_OPENING:
+		msg->scale = (double)msg->elapsed / (double)MESSAGE_SPEED;
+
+		if (msg->scale > 1)
+			msg->scale = 1;
+
 		if (msg->elapsed >= MESSAGE_SPEED) {
 			msg->state = MESSAGE_SHOWING;
 			msg->elapsed = 0;
@@ -134,6 +146,10 @@
 
 		break;
 	case MESSAGE_HIDING:
+		msg->scale = 1 - (double)msg->elapsed / (double)MESSAGE_SPEED;
+
+		if (msg->scale < 0)
+			msg->scale = 0;
 		if (msg->elapsed >= MESSAGE_SPEED) {
 			msg->state = MESSAGE_NONE;
 			msg->elapsed = 0;
@@ -150,9 +166,9 @@
 static void
 draw_frame(const struct message *msg)
 {
+	assert(msg);
+
 	struct frame frame = {
-		.x = (window.w / 2) - (WIDTH / 2),
-		.y = HEIGHT,
 		.w = WIDTH,
 		.h = HEIGHT
 	};
@@ -174,8 +190,8 @@
 			continue;
 
 		struct label label = {
-			.x = (window.w / 2) - (WIDTH / 2),
-			.y = HEIGHT + (i * lineh),
+			.x = 10,
+			.y = 10 + (i * lineh),
 			.theme = msg->theme,
 			.text = msg->text[i]
 		};
@@ -198,17 +214,27 @@
 {
 	assert(msg);
 
-	switch (msg->state) {
-	case MESSAGE_OPENING:
-	case MESSAGE_HIDING:
-		break;
-	case MESSAGE_SHOWING:
-		draw_frame(msg);
-		draw_lines(msg);
-		break;
-	default:
-		break;
-	}
+	struct texture tex;
+	int x, y, w, h;
+
+	if (!texture_new(&tex, WIDTH, HEIGHT))
+		panic();
+
+	PAINTER_BEGIN(&tex);
+	draw_frame(msg);
+	draw_lines(msg);
+	PAINTER_END();
+
+	/* Compute scaling. */
+	w = WIDTH * msg->scale;
+	h = HEIGHT * msg->scale;
+
+	/* Compute position. */
+	x = (window.w / 2) - (w / 2);
+	y = HEIGHT;
+
+	texture_scale(&tex, 0, 0, WIDTH, HEIGHT, x, y, w, h, 0.0);
+	texture_finish(&tex);
 }
 
 void
--- a/src/core/message.h	Mon Mar 30 23:08:23 2020 +0200
+++ b/src/core/message.h	Tue Mar 31 10:57:14 2020 +0200
@@ -107,6 +107,7 @@
 	enum message_state state;       /*!< (RO) Current state */
 	struct theme *theme;            /*!< (RW, ref, optional) Theme to use. */
 	unsigned int elapsed;           /*!< (RO) Time elapsed. */
+	double scale;                   /*!< (RO) Current scale [0-1]. */
 };
 
 /**