diff examples/example-action.c @ 139:8b035f7f978a

core: add action_stack_completed function Helps creating modal actions.
author David Demelier <markand@malikania.fr>
date Tue, 13 Oct 2020 15:15:40 +0200
parents 30b68089ae70
children 453651d76f7c
line wrap: on
line diff
--- a/examples/example-action.c	Tue Oct 13 14:38:43 2020 +0200
+++ b/examples/example-action.c	Tue Oct 13 15:15:40 2020 +0200
@@ -1,5 +1,5 @@
 /*
- * example-action.c -- example on how to use automatic drawables
+ * example-action.c -- example on how to use automatic actions
  *
  * Copyright (c) 2020 David Demelier <markand@malikania.fr>
  *
@@ -40,7 +40,11 @@
 #define W 1280
 #define H 720
 
-static struct action_stack stack;
+/* This is a stack of "parallel" events. */
+static struct action_stack events;
+
+/* This is a stack of modal events. */
+static struct action_stack modal;
 
 /*
  * Event object for the chest to click on.
@@ -163,13 +167,7 @@
 	const int index = guide.msgs[2].msg.index + 3;
 
 	message_action(&guide.msgs[index].msg, &guide.msgs[index].act);
-	action_stack_add(&stack, &guide.msgs[index].act);
-}
-
-static bool
-guide_running(void)
-{
-	return guide.script.actionsz > 0;
+	action_stack_add(&modal, &guide.msgs[index].act);
 }
 
 static void
@@ -190,7 +188,7 @@
 	script_append(&guide.script, &guide.response);
 
 	script_action(&guide.script, &guide.script_act);
-	action_stack_add(&stack, &guide.script_act);
+	action_stack_add(&modal, &guide.script_act);
 }
 
 static void
@@ -198,7 +196,7 @@
 {
 	(void)act;
 
-	if (guide_running())
+	if (!action_stack_completed(&modal))
 		return;
 
 	switch (ev->type) {
@@ -241,7 +239,7 @@
 {
 	(void)act;
 
-	if (chest.opened)
+	if (chest.opened || !action_stack_completed(&modal))
 		return;
 
 	switch (ev->type) {
@@ -250,7 +248,7 @@
 		    ev->click.x, ev->click.y)) {
 			chest.opened = true;
 			message_action(&chest.msg, &chest.msg_act);
-			action_stack_add(&stack, &chest.msg_act);
+			action_stack_add(&modal, &chest.msg_act);
 		}
 	default:
 		break;
@@ -299,8 +297,8 @@
 	struct clock clock = {0};
 
 	clock_start(&clock);
-	action_stack_add(&stack, &chest.event);
-	action_stack_add(&stack, &guide.event);
+	action_stack_add(&events, &chest.event);
+	action_stack_add(&events, &guide.event);
 
 	for (;;) {
 		unsigned int elapsed = clock_elapsed(&clock);
@@ -312,15 +310,18 @@
 			case EVENT_QUIT:
 				return;
 			default:
-				action_stack_handle(&stack, &ev);
+				action_stack_handle(&events, &ev);
+				action_stack_handle(&modal, &ev);
 				break;
 			}
 		}
 
 		painter_set_color(0xffffffff);
 		painter_clear();
-		action_stack_update(&stack, elapsed);
-		action_stack_draw(&stack);
+		action_stack_update(&events, elapsed);
+		action_stack_update(&modal, elapsed);
+		action_stack_draw(&events);
+		action_stack_draw(&modal);
 		painter_present();
 
 		if ((elapsed = clock_elapsed(&clock)) < 20)