changeset 67:7187c0d9b9c0

core: implement message action, closes #2467
author David Demelier <markand@malikania.fr>
date Mon, 27 Jan 2020 13:04:19 +0100
parents 9435a53adab4
children 767b90552ede
files src/core/message.c src/core/message.h
diffstat 2 files changed, 66 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/message.c	Mon Jan 27 13:03:39 2020 +0100
+++ b/src/core/message.c	Mon Jan 27 13:04:19 2020 +0100
@@ -17,18 +17,56 @@
  */
 
 #include <assert.h>
+#include <stdlib.h>
+#include <string.h>
 
+#include "action.h"
 #include "event.h"
 #include "font.h"
 #include "message.h"
 #include "painter.h"
 #include "sprite.h"
 #include "texture.h"
+#include "util.h"
 #include "window.h"
 
 #define MESSAGE_SPEED   200     /* Time delay for animations */
 #define MESSAGE_TIMEOUT 5000    /* Time for auto-closing */
 
+static void
+action_handle(struct action *action, const union event *ev)
+{
+	assert(action);
+	assert(ev);
+
+	message_handle(action->data, ev);
+}
+
+static bool
+action_update(struct action *action, unsigned int ticks)
+{
+	assert(action);
+
+	return message_update(action->data, ticks);
+}
+
+static void
+action_draw(struct action *action)
+{
+	assert(action);
+
+	message_draw(action->data);
+}
+
+static void
+action_finish(struct action *action)
+{
+	assert(action);
+
+	message_finish(action->data);
+	free(action->data);
+}
+
 static unsigned int
 average(const struct message *msg)
 {
@@ -220,9 +258,23 @@
 }
 
 void
-message_close(struct message *msg)
+message_finish(struct message *msg)
 {
 	assert(msg);
 
 	clear(msg);
 }
+
+void
+message_action(const struct message *msg, struct action *action)
+{
+	assert(msg);
+	assert(action);
+
+	memset(action, 0, sizeof (struct action));
+	action->data = ememdup(msg, sizeof (struct message));
+	action->handle = action_handle;
+	action->update = action_update;
+	action->draw = action_draw;
+	action->finish = action_finish;
+}
--- a/src/core/message.h	Mon Jan 27 13:03:39 2020 +0100
+++ b/src/core/message.h	Mon Jan 27 13:04:19 2020 +0100
@@ -26,8 +26,9 @@
 
 #include <stdbool.h>
 
+struct action;
+struct font;
 struct texture;
-struct font;
 
 union event;
 
@@ -138,4 +139,15 @@
 void
 message_finish(struct message *msg);
 
+/**
+ * Convert message into an action.
+ *
+ * \pre msg != NULL
+ * \pre action != NULL
+ * \param msg the message to copy from
+ * \param action the action to fill
+ */
+void
+message_action(const struct message *msg, struct action *action);
+
 #endif /* !MOLKO_MESSAGE_H */