Mercurial > molko
annotate libcore/core/message.c @ 146:7d7ea7a9cf50
core: add theme_shallow function
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 14 Oct 2020 18:20:58 +0200 |
parents | 7f1af54bb35a |
children |
rev | line source |
---|---|
24
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
1 /* |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
2 * message.c -- message dialog |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
3 * |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr> |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
5 * |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
6 * Permission to use, copy, modify, and/or distribute this software for any |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
7 * purpose with or without fee is hereby granted, provided that the above |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
8 * copyright notice and this permission notice appear in all copies. |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
9 * |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
17 */ |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
18 |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
19 #include <assert.h> |
67
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
20 #include <stdlib.h> |
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
21 #include <string.h> |
24
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
22 |
67
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
23 #include "action.h" |
64
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
24 #include "event.h" |
24
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
25 #include "font.h" |
99
4ac71ac10c9f
core: start adding some UI elements
David Demelier <markand@malikania.fr>
parents:
97
diff
changeset
|
26 #include "frame.h" |
4ac71ac10c9f
core: start adding some UI elements
David Demelier <markand@malikania.fr>
parents:
97
diff
changeset
|
27 #include "label.h" |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
28 #include "maths.h" |
28
783841af4033
core: implement painter API, closes #2451 @1h
David Demelier <markand@malikania.fr>
parents:
24
diff
changeset
|
29 #include "message.h" |
783841af4033
core: implement painter API, closes #2451 @1h
David Demelier <markand@malikania.fr>
parents:
24
diff
changeset
|
30 #include "painter.h" |
100
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
31 #include "panic.h" |
28
783841af4033
core: implement painter API, closes #2451 @1h
David Demelier <markand@malikania.fr>
parents:
24
diff
changeset
|
32 #include "sprite.h" |
24
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
33 #include "texture.h" |
99
4ac71ac10c9f
core: start adding some UI elements
David Demelier <markand@malikania.fr>
parents:
97
diff
changeset
|
34 #include "theme.h" |
141
4eeeccf2b732
core: add trace/vtrace functions, closes #2493
David Demelier <markand@malikania.fr>
parents:
140
diff
changeset
|
35 #include "trace.h" |
67
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
36 #include "util.h" |
24
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
37 |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
38 #define THEME(msg) (msg->theme ? msg->theme : theme_default()) |
99
4ac71ac10c9f
core: start adding some UI elements
David Demelier <markand@malikania.fr>
parents:
97
diff
changeset
|
39 |
67
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
40 static void |
136
30b68089ae70
core: rework actions and a bit of drawables, closes #2492
David Demelier <markand@malikania.fr>
parents:
135
diff
changeset
|
41 handle(struct action *action, const union event *ev) |
67
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
42 { |
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
43 assert(action); |
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
44 assert(ev); |
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
45 |
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
46 message_handle(action->data, ev); |
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
47 } |
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
48 |
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
49 static bool |
136
30b68089ae70
core: rework actions and a bit of drawables, closes #2492
David Demelier <markand@malikania.fr>
parents:
135
diff
changeset
|
50 update(struct action *action, unsigned int ticks) |
67
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
51 { |
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
52 assert(action); |
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
53 |
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
54 return message_update(action->data, ticks); |
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
55 } |
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
56 |
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
57 static void |
136
30b68089ae70
core: rework actions and a bit of drawables, closes #2492
David Demelier <markand@malikania.fr>
parents:
135
diff
changeset
|
58 draw(struct action *action) |
67
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
59 { |
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
60 assert(action); |
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
61 |
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
62 message_draw(action->data); |
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
63 } |
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
64 |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
65 static void |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
66 draw_frame(const struct message *msg) |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
67 { |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
68 assert(msg); |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
69 |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
70 struct frame frame = { |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
71 .w = msg->w, |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
72 .h = msg->h, |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
73 .theme = msg->theme |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
74 }; |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
75 |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
76 frame_draw(&frame); |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
77 } |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
78 |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
79 static void |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
80 draw_lines(const struct message *msg) |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
81 { |
145
7f1af54bb35a
core: rework label alignment, closes #2494 @1h
David Demelier <markand@malikania.fr>
parents:
141
diff
changeset
|
82 struct theme theme; |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
83 unsigned int lineh; |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
84 |
145
7f1af54bb35a
core: rework label alignment, closes #2494 @1h
David Demelier <markand@malikania.fr>
parents:
141
diff
changeset
|
85 /* Shallow copy theme to modify colors. */ |
146
7d7ea7a9cf50
core: add theme_shallow function
David Demelier <markand@malikania.fr>
parents:
145
diff
changeset
|
86 theme_shallow(&theme, msg->theme); |
145
7f1af54bb35a
core: rework label alignment, closes #2494 @1h
David Demelier <markand@malikania.fr>
parents:
141
diff
changeset
|
87 |
146
7d7ea7a9cf50
core: add theme_shallow function
David Demelier <markand@malikania.fr>
parents:
145
diff
changeset
|
88 /* Compute text size for list alignment. */ |
7d7ea7a9cf50
core: add theme_shallow function
David Demelier <markand@malikania.fr>
parents:
145
diff
changeset
|
89 lineh = font_height(theme.fonts[THEME_FONT_INTERFACE]); |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
90 |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
91 for (int i = 0; i < 6; ++i) { |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
92 if (!msg->text[i]) |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
93 continue; |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
94 |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
95 struct label label = { |
145
7f1af54bb35a
core: rework label alignment, closes #2494 @1h
David Demelier <markand@malikania.fr>
parents:
141
diff
changeset
|
96 .y = i * lineh, |
7f1af54bb35a
core: rework label alignment, closes #2494 @1h
David Demelier <markand@malikania.fr>
parents:
141
diff
changeset
|
97 .w = msg->w, |
7f1af54bb35a
core: rework label alignment, closes #2494 @1h
David Demelier <markand@malikania.fr>
parents:
141
diff
changeset
|
98 .h = msg->h, |
7f1af54bb35a
core: rework label alignment, closes #2494 @1h
David Demelier <markand@malikania.fr>
parents:
141
diff
changeset
|
99 .theme = &theme, |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
100 .text = msg->text[i], |
145
7f1af54bb35a
core: rework label alignment, closes #2494 @1h
David Demelier <markand@malikania.fr>
parents:
141
diff
changeset
|
101 .align = LABEL_ALIGN_TOP_LEFT, |
7f1af54bb35a
core: rework label alignment, closes #2494 @1h
David Demelier <markand@malikania.fr>
parents:
141
diff
changeset
|
102 .flags = LABEL_FLAGS_SHADOW |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
103 }; |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
104 |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
105 /* |
145
7f1af54bb35a
core: rework label alignment, closes #2494 @1h
David Demelier <markand@malikania.fr>
parents:
141
diff
changeset
|
106 * The function label_draw will use THEME_COLOR_NORMAL to draw |
7f1af54bb35a
core: rework label alignment, closes #2494 @1h
David Demelier <markand@malikania.fr>
parents:
141
diff
changeset
|
107 * text and THEME_COLOR_SHADOW so if we have selected a line |
7f1af54bb35a
core: rework label alignment, closes #2494 @1h
David Demelier <markand@malikania.fr>
parents:
141
diff
changeset
|
108 * we need to cheat the normal color. |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
109 */ |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
110 if (msg->flags & MESSAGE_FLAGS_QUESTION && msg->index == (unsigned int)i) |
145
7f1af54bb35a
core: rework label alignment, closes #2494 @1h
David Demelier <markand@malikania.fr>
parents:
141
diff
changeset
|
111 theme.colors[THEME_COLOR_NORMAL] = THEME(msg)->colors[THEME_COLOR_SELECTED]; |
7f1af54bb35a
core: rework label alignment, closes #2494 @1h
David Demelier <markand@malikania.fr>
parents:
141
diff
changeset
|
112 else |
7f1af54bb35a
core: rework label alignment, closes #2494 @1h
David Demelier <markand@malikania.fr>
parents:
141
diff
changeset
|
113 theme.colors[THEME_COLOR_NORMAL] = THEME(msg)->colors[THEME_COLOR_NORMAL]; |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
114 |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
115 label_draw(&label); |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
116 } |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
117 } |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
118 |
24
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
119 void |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
120 message_start(struct message *msg) |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
121 { |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
122 assert(msg); |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
123 |
141
4eeeccf2b732
core: add trace/vtrace functions, closes #2493
David Demelier <markand@malikania.fr>
parents:
140
diff
changeset
|
124 if (msg->flags & (MESSAGE_FLAGS_FADEIN|MESSAGE_FLAGS_FADEOUT)) |
4eeeccf2b732
core: add trace/vtrace functions, closes #2493
David Demelier <markand@malikania.fr>
parents:
140
diff
changeset
|
125 assert(msg->delay > 0); |
4eeeccf2b732
core: add trace/vtrace functions, closes #2493
David Demelier <markand@malikania.fr>
parents:
140
diff
changeset
|
126 |
24
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
127 msg->elapsed = 0; |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
128 msg->scale = msg->flags & MESSAGE_FLAGS_FADEIN ? 0.0 : 1.0; |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
129 msg->state = msg->flags & MESSAGE_FLAGS_FADEIN |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
130 ? MESSAGE_STATE_OPENING |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
131 : MESSAGE_STATE_SHOWING; |
141
4eeeccf2b732
core: add trace/vtrace functions, closes #2493
David Demelier <markand@malikania.fr>
parents:
140
diff
changeset
|
132 |
4eeeccf2b732
core: add trace/vtrace functions, closes #2493
David Demelier <markand@malikania.fr>
parents:
140
diff
changeset
|
133 if (msg->flags & MESSAGE_FLAGS_AUTOMATIC && msg->timeout == 0) |
4eeeccf2b732
core: add trace/vtrace functions, closes #2493
David Demelier <markand@malikania.fr>
parents:
140
diff
changeset
|
134 trace("message is automatic but has zero timeout"); |
64
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
135 } |
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
136 |
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
137 void |
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
138 message_handle(struct message *msg, const union event *ev) |
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
139 { |
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
140 assert(msg); |
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
141 assert(ev); |
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
142 |
100
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
143 /* Skip if the message animation hasn't complete. */ |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
144 if (msg->state != MESSAGE_STATE_SHOWING) |
100
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
145 return; |
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
146 |
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
147 /* Only keyboard event are valid. */ |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
148 if (ev->type != EVENT_KEYDOWN || msg->state == MESSAGE_STATE_NONE) |
64
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
149 return; |
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
150 |
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
151 switch (ev->key.key) { |
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
152 case KEY_UP: |
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
153 if (msg->index > 0) |
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
154 msg->index--; |
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
155 break; |
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
156 case KEY_DOWN: |
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
157 if (msg->index < 5 && msg->text[msg->index + 1]) |
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
158 msg->index++; |
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
159 break; |
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
160 case KEY_ENTER: |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
161 msg->state = msg->flags & MESSAGE_FLAGS_FADEOUT |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
162 ? MESSAGE_STATE_HIDING |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
163 : MESSAGE_STATE_NONE; |
64
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
164 msg->elapsed = 0; |
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
165 break; |
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
166 default: |
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
167 break; |
da9b7462ab92
core: implement question, closes #2464 @2h
David Demelier <markand@malikania.fr>
parents:
62
diff
changeset
|
168 } |
24
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
169 } |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
170 |
60
7266c750b649
core: simplify message
David Demelier <markand@malikania.fr>
parents:
59
diff
changeset
|
171 bool |
46
b815621df3e3
core: remove all fixed width integers, closes #2460
David Demelier <markand@malikania.fr>
parents:
28
diff
changeset
|
172 message_update(struct message *msg, unsigned int ticks) |
24
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
173 { |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
174 assert(msg); |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
175 |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
176 msg->elapsed += ticks; |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
177 |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
178 switch (msg->state) { |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
179 case MESSAGE_STATE_OPENING: |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
180 msg->scale = (double)msg->elapsed / (double)msg->delay; |
100
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
181 |
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
182 if (msg->scale > 1) |
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
183 msg->scale = 1; |
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
184 |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
185 if (msg->elapsed >= msg->delay) { |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
186 msg->state = MESSAGE_STATE_SHOWING; |
62 | 187 msg->elapsed = 0; |
188 } | |
189 | |
24
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
190 break; |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
191 case MESSAGE_STATE_SHOWING: |
24
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
192 /* Do automatically switch state if requested by the user. */ |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
193 if (msg->flags & MESSAGE_FLAGS_AUTOMATIC && msg->elapsed >= msg->timeout) { |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
194 msg->state = msg->flags & MESSAGE_FLAGS_FADEOUT |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
195 ? MESSAGE_STATE_HIDING |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
196 : MESSAGE_STATE_NONE; |
62 | 197 msg->elapsed = 0; |
198 } | |
199 | |
200 break; | |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
201 case MESSAGE_STATE_HIDING: |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
202 msg->scale = 1 - (double)msg->elapsed / (double)msg->delay; |
100
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
203 |
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
204 if (msg->scale < 0) |
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
205 msg->scale = 0; |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
206 if (msg->elapsed >= msg->delay) { |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
207 msg->state = MESSAGE_STATE_NONE; |
62 | 208 msg->elapsed = 0; |
209 } | |
24
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
210 |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
211 break; |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
212 default: |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
213 break; |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
214 } |
60
7266c750b649
core: simplify message
David Demelier <markand@malikania.fr>
parents:
59
diff
changeset
|
215 |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
216 return msg->state == MESSAGE_STATE_NONE; |
99
4ac71ac10c9f
core: start adding some UI elements
David Demelier <markand@malikania.fr>
parents:
97
diff
changeset
|
217 } |
4ac71ac10c9f
core: start adding some UI elements
David Demelier <markand@malikania.fr>
parents:
97
diff
changeset
|
218 |
24
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
219 void |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
220 message_draw(struct message *msg) |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
221 { |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
222 assert(msg); |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
223 |
100
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
224 struct texture tex; |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
225 int x, y; |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
226 unsigned int w, h; |
100
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
227 |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
228 if (!texture_new(&tex, msg->w, msg->h)) |
100
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
229 panic(); |
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
230 |
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
231 PAINTER_BEGIN(&tex); |
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
232 draw_frame(msg); |
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
233 draw_lines(msg); |
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
234 PAINTER_END(); |
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
235 |
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
236 /* Compute scaling. */ |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
237 w = msg->w * msg->scale; |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
238 h = msg->h * msg->scale; |
100
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
239 |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
240 /* Centerize within its drawing area. */ |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
241 maths_centerize(&x, &y, w, h, msg->x, msg->y, msg->w, msg->h); |
100
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
242 |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
243 /* Draw and clear. */ |
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
244 texture_scale(&tex, 0, 0, msg->w, msg->h, x, y, w, h, 0); |
100
ef9135c34505
message: improve rendering and add example
David Demelier <markand@malikania.fr>
parents:
99
diff
changeset
|
245 texture_finish(&tex); |
24
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
246 } |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
247 |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
248 void |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
249 message_hide(struct message *msg) |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
250 { |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
251 assert(msg); |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
252 |
140
453651d76f7c
core: upgrade message in terms of customization
David Demelier <markand@malikania.fr>
parents:
136
diff
changeset
|
253 msg->state = MESSAGE_STATE_HIDING; |
24
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
254 msg->elapsed = 0; |
4a06503641eb
core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
255 } |
62 | 256 |
257 void | |
136
30b68089ae70
core: rework actions and a bit of drawables, closes #2492
David Demelier <markand@malikania.fr>
parents:
135
diff
changeset
|
258 message_action(struct message *msg, struct action *action) |
67
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
259 { |
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
260 assert(msg); |
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
261 assert(action); |
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
262 |
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
263 memset(action, 0, sizeof (struct action)); |
136
30b68089ae70
core: rework actions and a bit of drawables, closes #2492
David Demelier <markand@malikania.fr>
parents:
135
diff
changeset
|
264 action->data = msg; |
30b68089ae70
core: rework actions and a bit of drawables, closes #2492
David Demelier <markand@malikania.fr>
parents:
135
diff
changeset
|
265 action->handle = handle; |
30b68089ae70
core: rework actions and a bit of drawables, closes #2492
David Demelier <markand@malikania.fr>
parents:
135
diff
changeset
|
266 action->update = update; |
30b68089ae70
core: rework actions and a bit of drawables, closes #2492
David Demelier <markand@malikania.fr>
parents:
135
diff
changeset
|
267 action->draw = draw; |
30b68089ae70
core: rework actions and a bit of drawables, closes #2492
David Demelier <markand@malikania.fr>
parents:
135
diff
changeset
|
268 |
30b68089ae70
core: rework actions and a bit of drawables, closes #2492
David Demelier <markand@malikania.fr>
parents:
135
diff
changeset
|
269 message_start(msg); |
67
7187c0d9b9c0
core: implement message action, closes #2467
David Demelier <markand@malikania.fr>
parents:
65
diff
changeset
|
270 } |