summaryrefslogtreecommitdiff
path: root/src/gui/button.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui/button.c')
-rw-r--r--src/gui/button.c133
1 files changed, 133 insertions, 0 deletions
diff --git a/src/gui/button.c b/src/gui/button.c
new file mode 100644
index 0000000..03addf8
--- /dev/null
+++ b/src/gui/button.c
@@ -0,0 +1,133 @@
1#include <gui/button.h>
2#include <stdlib.h>
3#include <assert.h>
4#include <common/mem.h>
5#include <string.h>
6#include <MLV/MLV_all.h>
7#include <gui/component.h>
8
9bool button_is_selected(int x, int y, Button *button) {
10 assert(x >= 0);
11 assert(y >= 0);
12 assert(button != NULL);
13 int x1 = button->component.x_pos;
14 int y1 = button->component.y_pos;
15 int x2 = button->component.x_pos + button->component.width;
16 int y2 = button->component.y_pos + button->component.height;
17 if (x >= x1 && x <= x2 && y >= y1 && y <= y2) {
18 return true;
19 }
20 return false;
21}
22
23void button_print(Component *parameterSelf) {
24 assert(parameterSelf != NULL);
25 Button *self = (Button *) parameterSelf;
26 MLV_draw_adapted_text_box(self->component.x_pos, self->component.y_pos, self->label, self->sizeInterligne,
27 MLV_COLOR_BLACK, MLV_COLOR_WHITE, MLV_COLOR_DARK_GREY, MLV_TEXT_CENTER);
28}
29
30void button_click_test(int x, int y, Component *parameterSelf) {
31 assert(x >= 0);
32 assert(y >= 0);
33 assert(parameterSelf != NULL);
34 Button *self = (Button *) parameterSelf;
35 if (button_is_selected(x, y, self) && (mode == WAITING_BUTTON_SHOW || mode == WAITING_BUTTON_HIDE)) {
36 }
37}
38
39void button_click_add_constraint(int x, int y, Component *parameterSelf) {
40 assert(x >= 0);
41 assert(y >= 0);
42 assert(parameterSelf != NULL);
43 Button *self = (Button *) parameterSelf;
44 if (button_is_selected(x, y, self) && (mode == WAITING_BUTTON_SHOW || mode == WAITING_BUTTON_HIDE)) {
45 mode = INSERT_ORIGIN;
46 }
47}
48
49void button_click_show_hide(int x, int y, Component *parameterSelf) {
50 assert(x >= 0);
51 assert(y >= 0);
52 assert(parameterSelf != NULL);
53 Button *self = (Button *) parameterSelf;
54 if (button_is_selected(x, y, self)) {
55 if (mode == WAITING_BUTTON_SHOW) {
56 mode = WAITING_BUTTON_HIDE;
57 } else if (mode == WAITING_BUTTON_HIDE) {
58 mode = WAITING_BUTTON_SHOW;
59 }
60 }
61}
62
63void button_click_exit(int x, int y, Component *parameterSelf) {
64 assert(x >= 0);
65 assert(y >= 0);
66 assert(parameterSelf != NULL);
67 Button *self = (Button *) parameterSelf;
68 if (button_is_selected(x, y, self) && (mode == WAITING_BUTTON_SHOW || mode == WAITING_BUTTON_HIDE)) {
69 mode = EXITING;
70 }
71}
72
73void button_click_less_frame(int x, int y, Component *parameterSelf) {
74 assert(x >= 0);
75 assert(y >= 0);
76 assert(parameterSelf != NULL);
77 Button *self = (Button *) parameterSelf;
78 if (button_is_selected(x, y, self) && (mode == WAITING_BUTTON_SHOW || mode == WAITING_BUTTON_HIDE)) {
79 if (frame > 2) {
80 frame = frame / 2;
81 sprintf(labelFrame,"%03d frames", frame);
82 mode = PRINTING_BUTTONS;
83 }
84 }
85}
86
87void button_click_more_frame(int x, int y, Component *parameterSelf) {
88 assert(x >= 0);
89 assert(y >= 0);
90 assert(parameterSelf != NULL);
91 Button *self = (Button *) parameterSelf;
92 if (button_is_selected(x, y, self) && (mode == WAITING_BUTTON_SHOW || mode == WAITING_BUTTON_HIDE)) {
93 if (frame < 256) {
94 frame = frame * 2;
95 sprintf(labelFrame,"%03d frames", frame);
96 mode = PRINTING_BUTTONS;
97 }
98 }
99}
100
101void button_click_rendering(int x,int y, Component *parameterSelf) {
102 assert(x >= 0);
103 assert(y >= 0);
104 assert(parameterSelf != NULL);
105 Button *self = (Button *) parameterSelf;
106 if (button_is_selected(x, y, self) && (mode == WAITING_BUTTON_SHOW || mode == WAITING_BUTTON_HIDE)) {
107 mode = RENDERING;
108 }
109}
110
111void button_click_none(int x, int y, Component *parameterSelf) {
112 assert(x >= 0);
113 assert(y >= 0);
114 assert(parameterSelf != NULL);
115}
116
117
118void
119button_init(Button *button, const char *text, int sizeInterligne, int x_pos, int y_pos, ClickHandler clickHandler) {
120 assert(button != NULL);
121 assert(text != NULL);
122 assert(sizeInterligne >= 0);
123 assert(x_pos >= 0);
124 assert(y_pos >= 0);
125 button->label = malloc_or_die(sizeof(char) * (strlen(text) + 1));
126 strcpy(button->label, text);
127 button->sizeInterligne = sizeInterligne;
128 MLV_get_size_of_adapted_text_box(text, sizeInterligne, &button->component.width, &button->component.height);
129 button->component.x_pos = x_pos;
130 button->component.y_pos = y_pos;
131 button->component.print_method = button_print;
132 button->component.click_handler = clickHandler;
133}