diff options
Diffstat (limited to 'src/gui/gui.c')
-rw-r--r-- | src/gui/gui.c | 142 |
1 files changed, 139 insertions, 3 deletions
diff --git a/src/gui/gui.c b/src/gui/gui.c index 2a3d018..3982f68 100644 --- a/src/gui/gui.c +++ b/src/gui/gui.c | |||
@@ -1,4 +1,140 @@ | |||
1 | // | 1 | #include <gui/window.h> |
2 | // Created by adam on 01/01/18. | 2 | #include <gui/gui.h> |
3 | // | 3 | #include <common/mem.h> |
4 | #include <MLV/MLV_all.h> | ||
5 | #include <gui/button.h> | ||
4 | 6 | ||
7 | |||
8 | GUI *gui_init(const char *fpath1, const char *fpath2) { | ||
9 | GUI *gui = malloc_or_die(sizeof(GUI)); | ||
10 | |||
11 | gui->window = malloc_or_die(sizeof(Window)); | ||
12 | window_init(gui->window, 500, 500, "Morphing"); | ||
13 | window_create(gui->window); | ||
14 | gui->canvasSrc = canvas_create_from_image(fpath1); | ||
15 | gui->canvasTrg = canvas_create_from_image(fpath2); | ||
16 | if (gui->canvasSrc->mlv == NULL || gui->canvasTrg->mlv == NULL) { | ||
17 | canvas_destroy(gui->canvasSrc); | ||
18 | canvas_destroy(gui->canvasTrg); | ||
19 | /*window_free(gui->window);*/ | ||
20 | fprintf(stderr, "Impossible to create an image from the path given. Verify the two paths.\n"); | ||
21 | exit(-4); | ||
22 | } | ||
23 | int canvasSrcWidth; | ||
24 | int canvasTrgWidth; | ||
25 | int canvasSrcHeight; | ||
26 | int canvasTrgHeight; | ||
27 | |||
28 | MLV_get_image_size(gui->canvasSrc->mlv, &canvasSrcWidth, &canvasSrcHeight); | ||
29 | MLV_get_image_size(gui->canvasTrg->mlv, &canvasTrgWidth, &canvasTrgHeight); | ||
30 | |||
31 | if (canvasSrcWidth != canvasTrgWidth || canvasSrcHeight != canvasTrgHeight) { | ||
32 | canvas_destroy(gui->canvasSrc); | ||
33 | canvas_destroy(gui->canvasTrg); | ||
34 | window_free(gui->window); | ||
35 | fprintf(stderr, "The two pictures do not have the same size\n"); | ||
36 | exit(-5); | ||
37 | } | ||
38 | |||
39 | gui->window->width = 2 * canvasSrcWidth + 10; | ||
40 | gui->window->height = canvasSrcHeight + 200; | ||
41 | |||
42 | MLV_change_window_size((unsigned int) gui->window->width, (unsigned int) gui->window->height); | ||
43 | |||
44 | gui->button1 = malloc_or_die(sizeof(Button)); | ||
45 | gui->button2 = malloc_or_die(sizeof(Button)); | ||
46 | gui->button3 = malloc_or_die(sizeof(Button)); | ||
47 | gui->button4 = malloc_or_die(sizeof(Button)); | ||
48 | gui->button5 = malloc_or_die(sizeof(Button)); | ||
49 | gui->button6 = malloc_or_die(sizeof(Button)); | ||
50 | gui->button7 = malloc_or_die(sizeof(Button)); | ||
51 | |||
52 | gui->pictureFrame1 = malloc_or_die(sizeof(PictureFrame)); | ||
53 | gui->pictureFrame2 = malloc_or_die(sizeof(PictureFrame)); | ||
54 | |||
55 | gui->morphing = morphing_create(canvasSrcWidth, canvasSrcHeight); | ||
56 | |||
57 | |||
58 | sprintf(labelFrame, "%03d frames", frame); | ||
59 | button_init(gui->button1, "Add constraint point", 10, 0, 0, button_click_add_constraint); | ||
60 | button_init(gui->button2, "Show/Hide", 10, 0, 0, button_click_show_hide); | ||
61 | button_init(gui->button3, "Start rendering", 10, 0, 0, button_click_rendering); | ||
62 | button_init(gui->button4, "<<<", 10, 0, 0, button_click_less_frame); | ||
63 | button_init(gui->button5, labelFrame, 10, 0, 0, button_click_none); | ||
64 | button_init(gui->button6, ">>>", 10, 0, 0, button_click_more_frame); | ||
65 | button_init(gui->button7, "Exit", 10, 0, 0, button_click_exit); | ||
66 | |||
67 | pictureframe_init(gui->pictureFrame1, canvasSrcWidth, canvasSrcHeight, 0, 0, pictureframe_origin_split, gui->morphing, | ||
68 | gui->canvasSrc, | ||
69 | pictureframe_click_handler_origin); | ||
70 | pictureframe_init(gui->pictureFrame2, canvasSrcWidth, canvasSrcHeight, 0, 0, pictureframe_target_split, gui->morphing, | ||
71 | gui->canvasTrg, | ||
72 | pictureframe_click_handler_target); | ||
73 | |||
74 | window_add_pictureframe(gui->window, gui->pictureFrame1); | ||
75 | window_add_pictureframe(gui->window, gui->pictureFrame2); | ||
76 | |||
77 | window_add_button(gui->window, gui->button1); | ||
78 | window_add_button(gui->window, gui->button2); | ||
79 | window_add_button(gui->window, gui->button3); | ||
80 | window_add_button(gui->window, gui->button4); | ||
81 | window_add_button(gui->window, gui->button5); | ||
82 | window_add_button(gui->window, gui->button6); | ||
83 | window_add_button(gui->window, gui->button7); | ||
84 | |||
85 | |||
86 | window_print_buttons(gui->window); | ||
87 | window_print_pictureframes(gui->window); | ||
88 | return gui; | ||
89 | } | ||
90 | |||
91 | void gui_handle_event(GUI *gui) { | ||
92 | window_click_keyboard_handler(gui->window, &gui->keyboardButton, &gui->keyboardModifier, &gui->unicode, &gui->mouse_x, | ||
93 | &gui->mouse_y); | ||
94 | switch (mode) { | ||
95 | case PRINTING: | ||
96 | window_print_pictureframes(gui->window); | ||
97 | mode = WAITING_BUTTON_SHOW; | ||
98 | break; | ||
99 | case WAITING_BUTTON_SHOW: | ||
100 | window_print_pictureframes(gui->window); | ||
101 | break; | ||
102 | case WAITING_BUTTON_HIDE: | ||
103 | window_print_pictureframes(gui->window); | ||
104 | break; | ||
105 | case PRINTING_BUTTONS: | ||
106 | button_init(gui->button5, labelFrame, 10, gui->button5->component.x_pos, gui->button5->component.y_pos, | ||
107 | button_click_none); | ||
108 | window_print_buttons(gui->window); | ||
109 | mode = WAITING_BUTTON_SHOW; | ||
110 | break; | ||
111 | case RENDERING: | ||
112 | window_rendering(gui->window, gui->pictureFrame1, gui->canvasSrc, gui->canvasTrg, gui->morphing); | ||
113 | break; | ||
114 | case INSERT_TARGET: | ||
115 | if (gui->keyboardButton == MLV_KEYBOARD_ESCAPE) { | ||
116 | window_print_pictureframes(gui->window); | ||
117 | mode = WAITING_BUTTON_SHOW; | ||
118 | } | ||
119 | break; | ||
120 | default: | ||
121 | break; | ||
122 | } | ||
123 | } | ||
124 | |||
125 | void gui_free(GUI *gui) { | ||
126 | window_free(gui->window); | ||
127 | canvas_destroy(gui->canvasSrc); | ||
128 | canvas_destroy(gui->canvasTrg); | ||
129 | morphing_destroy(gui->morphing); | ||
130 | |||
131 | free(gui->button1); | ||
132 | free(gui->button2); | ||
133 | free(gui->button3); | ||
134 | free(gui->button4); | ||
135 | free(gui->button5); | ||
136 | free(gui->button6); | ||
137 | free(gui->button7); | ||
138 | free(gui->pictureFrame1); | ||
139 | free(gui->pictureFrame2); | ||
140 | } \ No newline at end of file | ||