From 2f3d8ebc9b5e10e56bed5da316f5ef098dda0997 Mon Sep 17 00:00:00 2001 From: Adam NAILI Date: Wed, 3 Jan 2018 19:40:51 +0100 Subject: Updating documentation, cleaning includes, updating report --- doc/project-report.md | 16 +++++-- include/gui/button.h | 90 ++++++++++++++++++++++++++++----------- include/gui/component.h | 13 ++++++ include/gui/gui.h | 53 +++++++++++++++++++---- include/gui/pictureframe.h | 52 +++++++++++++---------- include/gui/window.h | 62 ++++++++++++++++----------- src/gui/button.c | 44 +++++++++---------- src/gui/gui.c | 97 +++++++++++++++++++++--------------------- src/gui/pictureframe.c | 76 ++++++++++++++++++--------------- src/gui/window.c | 35 ++++++--------- src/main.c | 8 ++-- test/gui/button.c | 2 +- test/gui/group.c | 2 +- test/gui/pictureframe.c | 103 ++++++++++++++++++++++----------------------- test/gui/window.c | 2 +- 15 files changed, 381 insertions(+), 274 deletions(-) diff --git a/doc/project-report.md b/doc/project-report.md index e48c204..3851150 100644 --- a/doc/project-report.md +++ b/doc/project-report.md @@ -130,9 +130,19 @@ RBGa vectors from the two base images: each component is square-rooted back to i ### GUI -TODO: modular component-based architecture, wrapping libMLV low level functions\ - based on delegated click handlers and painting functions\ - computing intermediate morphing between each frame, combined with a times, thus not using MLV_Animation +The Graphical User Interface is designed with a modular component-based architecture. That architecture implies an +Object-Oriented Programming's vision. Thanks to that, the application is very flexible for adding components and +placing them. The components created are groups, buttons, and picture frames that are all based on a common structure +called Component. Groups federate Components of any type and place them by a margin management. + +Thanks to a click handler and a printing function stored inside Components, it is possible to perform the actions on +click or to paint the component without knowing what is stored inside the group. The group is a component that handles +clicks and is able to paint itself by using the click handler and the painter function of the Component contained inside +its list. In other words, it delegates to the Components the action to perform. + +It also wraps some libMLV low level functions to create basic application features that can be used to create coherent +state for the application and components. The rendering process is done by computing intermediate morphing between each +frame combined with a time. By this implementation, the application is not using MLV_Animation. \newpage diff --git a/include/gui/button.h b/include/gui/button.h index 41008c1..b5908cb 100644 --- a/include/gui/button.h +++ b/include/gui/button.h @@ -5,7 +5,6 @@ * Buttons handling */ -#include #include "component.h" /** @@ -24,67 +23,108 @@ typedef struct { } Button; /** - * Function: button_init - * Initializes the button. + * Function: button_create + * Allocates and initializes the button. * * Parameters: - * *button - pointer to the input button * text - label for the button * sizeInterligne - parameter to initialize padding inside the button * x_pos - position of the button on x axis * y_pos - position of the button on y axis * clickHandler - pointer of function that will be loaded inside our button to perform its purpose + * + * Returns: + * A pointer of Button */ -void button_init(Button *button, const char *text, int sizeInterligne, int x_pos, int y_pos, ClickHandler clickHandler); +Button * +button_create(const char *text, int sizeInterligne, int x_pos, int y_pos, ClickHandler clickHandler); /** - * Function: button_print - * Prints the button. + * Function: button_destroy + * Frees the resources for the Button * * Parameters: - * *parameterSelf - pointer to the button + * *button - pointer to the button */ -void button_print(Component *parameterSelf); +void button_destroy(Button *button); /** - * Function: button_click_test - * Debug function to test if the click is working on the current button. + * Function: button_click_add_constraint + * Allows to add a constraint point in order on the first picture, then on the second. * * Parameters: * x - position of the click on x axis * y - position of the click on y axis * *parameterSelf - pointer on the button that is clicked */ -void button_click_test(int x, int y, Component *parameterSelf); - void button_click_add_constraint(int x, int y, Component *parameterSelf); +/** + * Function: button_click_show_hide + * Allows to show and hide the constraint points and triangles. + * + * Parameters: + * x - position of the click on x axis + * y - position of the click on y axis + * *parameterSelf - pointer on the button that is clicked + */ void button_click_show_hide(int x, int y, Component *parameterSelf); +/** + * Function: button_click_exit + * Quit the program. + * + * Parameters: + * x - position of the click on x axis + * y - position of the click on y axis + * *parameterSelf - pointer on the button that is clicked. + */ void button_click_exit(int x, int y, Component *parameterSelf); +/** + * Function: button_click_none + * Allows the button to do nothing on click. + * + * Parameters: + * x - position of the click on x axis + * y - position of the click on y axis + * *parameterSelf - pointer on the button that is clicked + */ void button_click_none(int x, int y, Component *parameterSelf); + +/** + * Function: button_click_more_frame + * Multiplies by two the number of frames to create when rendering. + * + * Parameters: + * x - position of the click on x axis + * y - position of the click on y axis + * *parameterSelf - pointer on the button that is clicked + */ void button_click_more_frame(int x, int y, Component *parameterSelf); +/** + * Function: button_click_less_frame + * Divides by two the number of frames to create when rendering. + * + * Parameters: + * x - position of the click on x axis + * y - position of the click on y axis + * *parameterSelf - pointer on the button that is clicked + */ void button_click_less_frame(int x, int y, Component *parameterSelf); -void button_click_rendering(int x, int y, Component *parameterSelf); - - /** - * Function: button_is_selected - * Checks if the button is selected or not. + * Function: button_click_rendering + * Launches the rendering of the morphing * * Parameters: - * x - position in x for the check - * y - position in y for the check - * *button - pointer to the current button - * - * Returns: - * A bool from stdbool + * x - position of the click on x axis + * y - position of the click on y axis + * *parameterSelf - pointer on the button that is clicked */ +void button_click_rendering(int x, int y, Component *parameterSelf); -bool button_is_selected(int x, int y, Button *button); #endif diff --git a/include/gui/component.h b/include/gui/component.h index 0275d45..9700dfe 100644 --- a/include/gui/component.h +++ b/include/gui/component.h @@ -1,5 +1,18 @@ #ifndef UPEM_C_COMPONENT_H #define UPEM_C_COMPONENT_H + +/** + * Enum: Mode + * + * WAITING_BUTTON_SHOW - Waits a click on buttons to perform actions and paints the constraint points on the GUI + * WAITING_BUTTON_HIDE - Waits a click on buttons to perform actions and does not print the constraint points on the GUI + * INSERT_ORIGIN - Waits a click on the origin pictureframe and lock other components + * INSERT_TARGET - Waits a click on the target pictureframe and lock other components + * PRINTING - Force the application to paint the pictureframes + * EXITING - Exits the programme + * PRINTING_BUTTONS - paints the buttons + * RENDERING - launches the rendering mode + */ typedef enum { WAITING_BUTTON_SHOW, WAITING_BUTTON_HIDE, INSERT_ORIGIN, INSERT_TARGET, PRINTING, EXITING, PRINTING_BUTTONS, RENDERING } Mode; diff --git a/include/gui/gui.h b/include/gui/gui.h index daaf8de..10f59e8 100644 --- a/include/gui/gui.h +++ b/include/gui/gui.h @@ -1,11 +1,32 @@ #ifndef UPEM_MORPHING_GUI #define UPEM_MORPHING_GUI -#include +#include "gui/window.h" + /** * File: gui.h */ + +/** + * Struct: GUI + * Supports all the parts of the GUI + * + * Fields: + * *window - pointer to the Window + * *button1 - pointer to the button 1 + * *button2 - pointer to the button 2 + * *button3 - pointer to the button 3 + * *button4 - pointer to the button 4 + * *button5 - pointer to the button 5 + * *button6 - pointer to the button 6 + * *button7 - pointer to the button 7 + * *pictureFrame1 - pointer to the picture frame 1 + * *pictureFrame2 - pointer to the picture frame 2 + * *canvasSrc - pointer to the canvas 1 + * *canvasTrg - pointer to the canvas 2 + * *morphing - pointer to the current morphing + */ typedef struct { Window *window; Button *button1; @@ -20,20 +41,36 @@ typedef struct { Canvas *canvasSrc; Canvas *canvasTrg; Morphing *morphing; - MLV_Keyboard_button keyboardButton; - MLV_Keyboard_modifier keyboardModifier; - int unicode; - int mouse_x; - int mouse_y; } GUI; +/** + * Function: gui_create + * + * + * Parameters: + * *fpath1 - path to the first picture + * *fpath2 - path to the second picture + * + * Returns: + * A pointer to a GUI structure + */ +GUI *gui_create(const char *fpath1, const char *fpath2); -GUI *gui_init(const char *fpath1, const char *fpath2); /** * Function: gui_handle_event * Builds and opens the graphical user interface. + * + * Parameters: + * *gui - pointer to the current GUI structure */ void gui_handle_event(GUI *gui); -void gui_free(GUI *gui); +/** + * Function: gui_destroy + * Frees the resources allocated for the GUI + * Parameters: + * *gui - pointer to the current GUI structure + */ +void gui_destroy(GUI *gui); + #endif diff --git a/include/gui/pictureframe.h b/include/gui/pictureframe.h index 7b9644e..a070190 100644 --- a/include/gui/pictureframe.h +++ b/include/gui/pictureframe.h @@ -1,14 +1,17 @@ #ifndef UPEM_MORPHING_PITUREFRAME #define UPEM_MORPHING_PITUREFRAME -#include -#include +#include "morpher/morphing.h" +#include "painter/canvas.h" #include "component.h" + /** * File: pictureframe.h */ +/*Needed storage point to share memory between the origin PictureFrame and the target PictureFrame. It ables to cancel the Add constraint functionality*/ CartesianVector savedPoint; + /** * Type: CartesianMappingDivision * Type of functions needed to split CartesianMapping and keep only the CartesianVector needed, related to the type of PictureFrame involved @@ -50,24 +53,35 @@ CartesianVector pictureframe_origin_split(const CartesianMapping *cartesianMappi */ CartesianVector pictureframe_target_split(const CartesianMapping *cartesianMapping); -bool pictureframe_is_selected(int x, int y, PictureFrame *pictureFrame); - /** - * Function: pictureframe_conversion_to_origin - * Returns the relative coordinate on the picture corresponding to the input values + * Function: pictureframe_create + * Allocates and initializes the PictureFrame. * * Parameters: - * x - value on x axis from the origin of the window to convert - * y - value on y axis from the origin of the window to convert - * *pictureFrame - pointer to the reference pictureframe that will give his relative coordinates + * width - width of the current picture + * height - height of the current picture + * x_pos - position of the button on x axis + * y_pos - position of the button on y axis + * cartesianMappingDivision - pointer of function needed to select the right CartesianVector inside the morphing's CartesianMapping + * *morphing - pointer to the morphing + * *canvas - pointer to the canvas + * clickHandler - pointer of function that will be loaded inside our pictureframe + * + * Returns: + * A pointer of PictureFrame */ -CartesianVector pictureframe_conversion_to_pic(int x, int y, PictureFrame *pictureFrame); +PictureFrame *pictureframe_create(int width, int height, int x_pos, int y_pos, + CartesianMappingDivision cartesianMappingDivision, Morphing *morphing, Canvas *canvas, + ClickHandler clickHandler); -CartesianVector pictureframe_conversion_to_origin(int x, int y, PictureFrame *pictureFrame); - -void pictureframe_init(PictureFrame *pictureFrame, int width, int height, int x_pos, int y_pos, - CartesianMappingDivision cartesianMappingDivision, Morphing *morphing, Canvas *canvas, - ClickHandler clickHandler); +/** + * Function: pictureframe_destroy + * Frees the current PictureFrame + * + * Parameters: + * *pictureframe - pointer to the current PictureFrame + */ +void pictureframe_destroy(PictureFrame *pictureFrame); /** * Function: pictureframe_draw_canvas @@ -78,14 +92,6 @@ void pictureframe_init(PictureFrame *pictureFrame, int width, int height, int x_ */ void pictureframe_draw_canvas(PictureFrame *pictureFrame); -/** - * Function: pictureframe_print - * Prints the PictureFrame (The Canvas involved, plus the triangles and points of the Morphing) - * - * Parameters: - * *parameterSelf - pointer that will be casted into a PictureFrame to be printed - */ -void pictureframe_print(Component *parameterSelf); /** * Function: pictureframe_click_handler_origin diff --git a/include/gui/window.h b/include/gui/window.h index 10f0e74..e6b14dc 100644 --- a/include/gui/window.h +++ b/include/gui/window.h @@ -1,20 +1,15 @@ #ifndef UPEM_MORPHING_WINDOW #define UPEM_MORPHING_WINDOW +#include "MLV/MLV_keyboard.h" +#include "group.h" +#include "button.h" +#include "pictureframe.h" /** * File: window.h * Windows and components handling. - * - * See also: - * The famous OS */ -#include -#include "group.h" -#include "component.h" -#include "button.h" -#include "pictureframe.h" - /** * Struct: Window * Supports and handles components. @@ -34,25 +29,26 @@ typedef struct { } Window; /** - * Function: window_init - * Initializes a window. + * Function: window_create + * Allocates and initializes a window. * * Parameters: - * *window - pointer to the input window * width - width of the window to initialize * height - height of the window to initialize * *title - title of the actual window + * Returns: + * A pointer to a Window */ -void window_init(Window *window, int width, int height, char *title); +Window *window_create(int width, int height, char *title); /** - * Function: window_free + * Function: window_destroy * Frees the resources supported by the window and the window itself. * * Parameters: * *window - pointer to the input window */ -void window_free(Window *window); +void window_destroy(Window *window); /** * Function: window_add_button @@ -74,15 +70,6 @@ void window_add_button(Window *window, Button *button); */ void window_add_pictureframe(Window *window, PictureFrame *pictureFrame); -/** - * Function: window_create - * Initializes the resources to create a window. - * - * Parameters: - * *window - pointer to the input window - */ -void window_create(Window *window); - /** * Function: window_print_buttons * Prints all the buttons to the screen @@ -101,9 +88,34 @@ void window_print_buttons(Window *window); */ void window_print_pictureframes(Window *window); +/** + * Function: window_click_keyboard_handler + * Handles click and keyboard. + * + * Parameters: + * *window - pointer to the current window + * *keyboardButton - code of the keyboard key that is pushed or released + * *keyboardModifier - mode of the keyboard when a key is push or released + * *unicode - character coded in unicode of the letter obtained by combining the code and the mode + * *mouse_x - coordinate on the X axis of the mouse + * *mouse_y - coordinate on the Y axis of the mouse + */ void window_click_keyboard_handler(Window *window, MLV_Keyboard_button *keyboardButton, MLV_Keyboard_modifier *keyboardModifier, int *unicode, int *mouse_x, int *mouse_y); -void window_rendering(Window *window,PictureFrame *pictureFrame1,Canvas *canvasSrc, Canvas *canvasTarget, Morphing *morphing); + +/** + * Function: window_rendering + * Launches the rendering on the pictureframe origin. + * + * Parameters: + * *window - pointer to the current window + * *pictureFrame1 - pointer to the origin PictureFrame + * *canvasSrc - pointer to the source Canvas + * *canvasTarget - pointer to the target Canvas + * *morphing - pointer to the Morphing that will makes the transformation + */ +void window_rendering(Window *window, PictureFrame *pictureFrame1, Canvas *canvasSrc, Canvas *canvasTarget, + Morphing *morphing); #endif diff --git a/src/gui/button.c b/src/gui/button.c index a55796d..35ac2ed 100644 --- a/src/gui/button.c +++ b/src/gui/button.c @@ -1,37 +1,28 @@ -#include #include +#include #include -#include #include -#include -#include +#include "gui/button.h" +#include "MLV/MLV_all.h" +#include "common/mem.h" -bool button_is_selected(int x, int y, Button *button) { + +static bool button_is_selected(int x, int y, Button *button) { assert(button != NULL); int x1 = button->component.x_pos; int y1 = button->component.y_pos; int x2 = button->component.x_pos + button->component.width; int y2 = button->component.y_pos + button->component.height; - if (x >= x1 && x <= x2 && y >= y1 && y <= y2) { - return true; - } - return false; + return x >= x1 && x <= x2 && y >= y1 && y <= y2; } -void button_print(Component *parameterSelf) { +static void button_print(Component *parameterSelf) { assert(parameterSelf != NULL); Button *self = (Button *) parameterSelf; MLV_draw_adapted_text_box(self->component.x_pos, self->component.y_pos, self->label, self->sizeInterligne, MLV_COLOR_BLACK, MLV_COLOR_WHITE, MLV_COLOR_DARK_GREY, MLV_TEXT_CENTER); } -void button_click_test(int x, int y, Component *parameterSelf) { - assert(parameterSelf != NULL); - Button *self = (Button *) parameterSelf; - if (button_is_selected(x, y, self) && (mode == WAITING_BUTTON_SHOW || mode == WAITING_BUTTON_HIDE)) { - } -} - void button_click_add_constraint(int x, int y, Component *parameterSelf) { assert(parameterSelf != NULL); Button *self = (Button *) parameterSelf; @@ -66,7 +57,7 @@ void button_click_less_frame(int x, int y, Component *parameterSelf) { if (button_is_selected(x, y, self) && (mode == WAITING_BUTTON_SHOW || mode == WAITING_BUTTON_HIDE)) { if (frame > 2) { frame = frame / 2; - sprintf(labelFrame,"%03d frames", frame); + sprintf(labelFrame, "%03d frames", frame); mode = PRINTING_BUTTONS; } } @@ -78,13 +69,13 @@ void button_click_more_frame(int x, int y, Component *parameterSelf) { if (button_is_selected(x, y, self) && (mode == WAITING_BUTTON_SHOW || mode == WAITING_BUTTON_HIDE)) { if (frame < 256) { frame = frame * 2; - sprintf(labelFrame,"%03d frames", frame); + sprintf(labelFrame, "%03d frames", frame); mode = PRINTING_BUTTONS; } } } -void button_click_rendering(int x,int y, Component *parameterSelf) { +void button_click_rendering(int x, int y, Component *parameterSelf) { assert(parameterSelf != NULL); Button *self = (Button *) parameterSelf; if (button_is_selected(x, y, self) && (mode == WAITING_BUTTON_SHOW || mode == WAITING_BUTTON_HIDE)) { @@ -96,14 +87,12 @@ void button_click_none(int x, int y, Component *parameterSelf) { assert(parameterSelf != NULL); } - -void -button_init(Button *button, const char *text, int sizeInterligne, int x_pos, int y_pos, ClickHandler clickHandler) { - assert(button != NULL); +Button *button_create(const char *text, int sizeInterligne, int x_pos, int y_pos, ClickHandler clickHandler) { assert(text != NULL); assert(sizeInterligne >= 0); assert(x_pos >= 0); assert(y_pos >= 0); + Button *button = malloc_or_die(sizeof(Button)); button->label = malloc_or_die(sizeof(char) * (strlen(text) + 1)); strcpy(button->label, text); button->sizeInterligne = sizeInterligne; @@ -112,4 +101,11 @@ button_init(Button *button, const char *text, int sizeInterligne, int x_pos, int button->component.y_pos = y_pos; button->component.print_method = button_print; button->component.click_handler = clickHandler; + return button; } + +void button_destroy(Button *button){ + assert(button != NULL); + free(button->label); + free(button); +} \ No newline at end of file diff --git a/src/gui/gui.c b/src/gui/gui.c index 3982f68..ced2661 100644 --- a/src/gui/gui.c +++ b/src/gui/gui.c @@ -1,22 +1,18 @@ -#include -#include -#include -#include -#include +#include "gui/window.h" +#include "gui/gui.h" +#include "common/mem.h" +#include "MLV/MLV_all.h" -GUI *gui_init(const char *fpath1, const char *fpath2) { +GUI *gui_create(const char *fpath1, const char *fpath2) { GUI *gui = malloc_or_die(sizeof(GUI)); - - gui->window = malloc_or_die(sizeof(Window)); - window_init(gui->window, 500, 500, "Morphing"); - window_create(gui->window); + gui->window = window_create(500, 500, "Morphing"); gui->canvasSrc = canvas_create_from_image(fpath1); gui->canvasTrg = canvas_create_from_image(fpath2); if (gui->canvasSrc->mlv == NULL || gui->canvasTrg->mlv == NULL) { canvas_destroy(gui->canvasSrc); canvas_destroy(gui->canvasTrg); - /*window_free(gui->window);*/ + window_destroy(gui->window); fprintf(stderr, "Impossible to create an image from the path given. Verify the two paths.\n"); exit(-4); } @@ -31,7 +27,7 @@ GUI *gui_init(const char *fpath1, const char *fpath2) { if (canvasSrcWidth != canvasTrgWidth || canvasSrcHeight != canvasTrgHeight) { canvas_destroy(gui->canvasSrc); canvas_destroy(gui->canvasTrg); - window_free(gui->window); + window_destroy(gui->window); fprintf(stderr, "The two pictures do not have the same size\n"); exit(-5); } @@ -41,14 +37,6 @@ GUI *gui_init(const char *fpath1, const char *fpath2) { MLV_change_window_size((unsigned int) gui->window->width, (unsigned int) gui->window->height); - gui->button1 = malloc_or_die(sizeof(Button)); - gui->button2 = malloc_or_die(sizeof(Button)); - gui->button3 = malloc_or_die(sizeof(Button)); - gui->button4 = malloc_or_die(sizeof(Button)); - gui->button5 = malloc_or_die(sizeof(Button)); - gui->button6 = malloc_or_die(sizeof(Button)); - gui->button7 = malloc_or_die(sizeof(Button)); - gui->pictureFrame1 = malloc_or_die(sizeof(PictureFrame)); gui->pictureFrame2 = malloc_or_die(sizeof(PictureFrame)); @@ -56,20 +44,22 @@ GUI *gui_init(const char *fpath1, const char *fpath2) { sprintf(labelFrame, "%03d frames", frame); - button_init(gui->button1, "Add constraint point", 10, 0, 0, button_click_add_constraint); - button_init(gui->button2, "Show/Hide", 10, 0, 0, button_click_show_hide); - button_init(gui->button3, "Start rendering", 10, 0, 0, button_click_rendering); - button_init(gui->button4, "<<<", 10, 0, 0, button_click_less_frame); - button_init(gui->button5, labelFrame, 10, 0, 0, button_click_none); - button_init(gui->button6, ">>>", 10, 0, 0, button_click_more_frame); - button_init(gui->button7, "Exit", 10, 0, 0, button_click_exit); - - pictureframe_init(gui->pictureFrame1, canvasSrcWidth, canvasSrcHeight, 0, 0, pictureframe_origin_split, gui->morphing, - gui->canvasSrc, - pictureframe_click_handler_origin); - pictureframe_init(gui->pictureFrame2, canvasSrcWidth, canvasSrcHeight, 0, 0, pictureframe_target_split, gui->morphing, - gui->canvasTrg, - pictureframe_click_handler_target); + gui->button1 = button_create("Add constraint point", 10, 0, 0, button_click_add_constraint); + gui->button2 = button_create("Show/Hide", 10, 0, 0, button_click_show_hide); + gui->button3 = button_create("Start rendering", 10, 0, 0, button_click_rendering); + gui->button4 = button_create("<<<", 10, 0, 0, button_click_less_frame); + gui->button5 = button_create(labelFrame, 10, 0, 0, button_click_none); + gui->button6 = button_create(">>>", 10, 0, 0, button_click_more_frame); + gui->button7 = button_create("Exit", 10, 0, 0, button_click_exit); + + gui->pictureFrame1 = pictureframe_create(canvasSrcWidth, canvasSrcHeight, 0, 0, pictureframe_origin_split, + gui->morphing, + gui->canvasSrc, + pictureframe_click_handler_origin); + gui->pictureFrame2 = pictureframe_create(canvasSrcWidth, canvasSrcHeight, 0, 0, pictureframe_target_split, + gui->morphing, + gui->canvasTrg, + pictureframe_click_handler_target); window_add_pictureframe(gui->window, gui->pictureFrame1); window_add_pictureframe(gui->window, gui->pictureFrame2); @@ -89,8 +79,13 @@ GUI *gui_init(const char *fpath1, const char *fpath2) { } void gui_handle_event(GUI *gui) { - window_click_keyboard_handler(gui->window, &gui->keyboardButton, &gui->keyboardModifier, &gui->unicode, &gui->mouse_x, - &gui->mouse_y); + MLV_Keyboard_button keyboardButton; + MLV_Keyboard_modifier keyboardModifier; + int unicode; + int mouse_x; + int mouse_y; + window_click_keyboard_handler(gui->window, &keyboardButton, &keyboardModifier, &unicode, &mouse_x, + &mouse_y); switch (mode) { case PRINTING: window_print_pictureframes(gui->window); @@ -103,8 +98,9 @@ void gui_handle_event(GUI *gui) { window_print_pictureframes(gui->window); break; case PRINTING_BUTTONS: - button_init(gui->button5, labelFrame, 10, gui->button5->component.x_pos, gui->button5->component.y_pos, - button_click_none); + free(gui->button5->label); + gui->button5->label = malloc_or_die(sizeof(char) * (strlen(labelFrame) + 1)); + strcpy(gui->button5->label, labelFrame); window_print_buttons(gui->window); mode = WAITING_BUTTON_SHOW; break; @@ -112,7 +108,7 @@ void gui_handle_event(GUI *gui) { window_rendering(gui->window, gui->pictureFrame1, gui->canvasSrc, gui->canvasTrg, gui->morphing); break; case INSERT_TARGET: - if (gui->keyboardButton == MLV_KEYBOARD_ESCAPE) { + if (keyboardButton == MLV_KEYBOARD_ESCAPE) { window_print_pictureframes(gui->window); mode = WAITING_BUTTON_SHOW; } @@ -122,19 +118,20 @@ void gui_handle_event(GUI *gui) { } } -void gui_free(GUI *gui) { - window_free(gui->window); +void gui_destroy(GUI *gui) { + window_destroy(gui->window); canvas_destroy(gui->canvasSrc); canvas_destroy(gui->canvasTrg); morphing_destroy(gui->morphing); - free(gui->button1); - free(gui->button2); - free(gui->button3); - free(gui->button4); - free(gui->button5); - free(gui->button6); - free(gui->button7); - free(gui->pictureFrame1); - free(gui->pictureFrame2); + button_destroy(gui->button1); + button_destroy(gui->button2); + button_destroy(gui->button3); + button_destroy(gui->button4); + button_destroy(gui->button5); + button_destroy(gui->button6); + button_destroy(gui->button7); + + pictureframe_destroy(gui->pictureFrame1); + pictureframe_destroy(gui->pictureFrame2); } \ No newline at end of file diff --git a/src/gui/pictureframe.c b/src/gui/pictureframe.c index a6a94bf..53a0ed3 100644 --- a/src/gui/pictureframe.c +++ b/src/gui/pictureframe.c @@ -1,20 +1,35 @@ #include -#include -#include +#include "common/mem.h" +#include "gui/pictureframe.h" +#include "MLV/MLV_all.h" -CartesianVector pictureframe_origin_split(const CartesianMapping *cartesianMapping) { - return cartesianMapping->origin; +static bool pictureframe_is_selected(int x, int y, PictureFrame *pictureFrame) { + assert(pictureFrame != NULL); + int x1 = pictureFrame->component.x_pos; + int y1 = pictureFrame->component.y_pos; + int x2 = pictureFrame->component.x_pos + pictureFrame->component.width; + int y2 = pictureFrame->component.y_pos + pictureFrame->component.height; + if (x >= x1 && x <= x2 && y >= y1 && y <= y2) { + return true; + } + return false; } -CartesianVector pictureframe_target_split(const CartesianMapping *cartesianMapping) { - return cartesianMapping->target; +static CartesianVector pictureframe_conversion_to_pic(int x, int y, PictureFrame *pictureFrame) { + CartesianVector vector; + vector.x = x - pictureFrame->component.x_pos; + vector.y = y - pictureFrame->component.y_pos; + return vector; } -void pictureframe_draw_canvas(PictureFrame *pictureFrame){ - MLV_draw_image(pictureFrame->canvas->mlv, pictureFrame->component.x_pos, pictureFrame->component.y_pos); +static CartesianVector pictureframe_conversion_to_origin(int x, int y, PictureFrame *pictureFrame) { + CartesianVector vector; + vector.x = x + pictureFrame->component.x_pos; + vector.y = y + pictureFrame->component.y_pos; + return vector; } -void pictureframe_print(Component *parameterSelf) { +static void pictureframe_print(Component *parameterSelf) { PictureFrame *self = (PictureFrame *) parameterSelf; pictureframe_draw_canvas(self); if (mode != WAITING_BUTTON_HIDE && mode != RENDERING) { @@ -46,30 +61,12 @@ void pictureframe_print(Component *parameterSelf) { } } -bool pictureframe_is_selected(int x, int y, PictureFrame *pictureFrame) { - assert(pictureFrame != NULL); - int x1 = pictureFrame->component.x_pos; - int y1 = pictureFrame->component.y_pos; - int x2 = pictureFrame->component.x_pos + pictureFrame->component.width; - int y2 = pictureFrame->component.y_pos + pictureFrame->component.height; - if (x >= x1 && x <= x2 && y >= y1 && y <= y2) { - return true; - } - return false; -} - -CartesianVector pictureframe_conversion_to_pic(int x, int y, PictureFrame *pictureFrame) { - CartesianVector vector; - vector.x = x - pictureFrame->component.x_pos; - vector.y = y - pictureFrame->component.y_pos; - return vector; +CartesianVector pictureframe_origin_split(const CartesianMapping *cartesianMapping) { + return cartesianMapping->origin; } -CartesianVector pictureframe_conversion_to_origin(int x, int y, PictureFrame *pictureFrame) { - CartesianVector vector; - vector.x = x + pictureFrame->component.x_pos; - vector.y = y + pictureFrame->component.y_pos; - return vector; +CartesianVector pictureframe_target_split(const CartesianMapping *cartesianMapping) { + return cartesianMapping->target; } void pictureframe_click_handler_origin(int x_pos, int y_pos, Component *parameterSelf) { @@ -92,10 +89,13 @@ void pictureframe_click_handler_target(int x_pos, int y_pos, Component *paramete } } -void pictureframe_init(PictureFrame *pictureFrame, int width, int height, int x_pos, int y_pos, - CartesianMappingDivision cartesianMappingDivision, Morphing *morphing, Canvas *canvas, - ClickHandler clickHandler) { - assert(pictureFrame != NULL); +void pictureframe_draw_canvas(PictureFrame *pictureFrame) { + MLV_draw_image(pictureFrame->canvas->mlv, pictureFrame->component.x_pos, pictureFrame->component.y_pos); +} + +PictureFrame *pictureframe_create(int width, int height, int x_pos, int y_pos, + CartesianMappingDivision cartesianMappingDivision, Morphing *morphing, Canvas *canvas, + ClickHandler clickHandler) { assert(width > 0); assert(height > 0); assert(x_pos >= 0); @@ -103,6 +103,7 @@ void pictureframe_init(PictureFrame *pictureFrame, int width, int height, int x_ assert(cartesianMappingDivision != NULL); assert(morphing != NULL); assert(canvas != NULL); + PictureFrame *pictureFrame = malloc_or_die(sizeof(PictureFrame)); pictureFrame->component.width = width; pictureFrame->component.height = height; pictureFrame->component.x_pos = x_pos; @@ -112,4 +113,9 @@ void pictureframe_init(PictureFrame *pictureFrame, int width, int height, int x_ pictureFrame->morphing = morphing; pictureFrame->canvas = canvas; pictureFrame->cartesianMappingDivision = cartesianMappingDivision; + return pictureFrame; +} + +void pictureframe_destroy(PictureFrame *pictureFrame){ + free(pictureFrame); } \ No newline at end of file diff --git a/src/gui/window.c b/src/gui/window.c index 32aeb26..1859f58 100644 --- a/src/gui/window.c +++ b/src/gui/window.c @@ -1,22 +1,16 @@ #include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include "gui/window.h" +#include "MLV/MLV_all.h" +#include "painter/rasterizer.h" #include "common/mem.h" -#include "string.h" -#include "assert.h" -#include "MLV/MLV_window.h" -void window_init(Window *window, int width, int height, char *title) { - assert(window != NULL); + +Window *window_create(int width, int height, char *title) { assert(width > 0); assert(height > 0); assert(title != NULL); + Window *window = malloc_or_die(sizeof(Window)); window->width = width; window->height = height; window->title = malloc_or_die(sizeof(char) * (strlen(title) + 1)); @@ -25,13 +19,16 @@ void window_init(Window *window, int width, int height, char *title) { group_init(window->group_buttons, window->width, 100, 0, window->height - 100, 5); window->group_pictureframe = malloc_or_die(sizeof(Group)); group_init(window->group_pictureframe, window->width, window->height - 100, 0, 0, 5); + MLV_create_window(window->title, window->title, (unsigned int) window->width, (unsigned int) window->height); + return window; } -void window_free(Window *window) { +void window_destroy(Window *window) { assert(window != NULL); free(window->title); group_free(window->group_buttons); group_free(window->group_pictureframe); + free(window); } void window_add_button(Window *window, Button *button) { @@ -46,11 +43,6 @@ void window_add_pictureframe(Window *window, PictureFrame *pictureFrame) { group_add_component(window->group_pictureframe, &(pictureFrame->component)); } -void window_create(Window *window) { - assert(window != NULL); - MLV_create_window(window->title, window->title, (unsigned int) window->width, (unsigned int) window->height); -} - void window_print_buttons(Window *window) { assert(window != NULL); window->group_buttons->component.print_method(&(window->group_buttons->component)); @@ -74,12 +66,13 @@ void window_wait_keyboard_or_mouse(MLV_Keyboard_button *keyboardButton, MLV_Keyb void window_click_keyboard_handler(Window *window, MLV_Keyboard_button *keyboardButton, MLV_Keyboard_modifier *keyboardModifier, int *unicode, int *mouse_x, int *mouse_y) { - window_wait_keyboard_or_mouse(keyboardButton,keyboardModifier,unicode,mouse_x,mouse_y); + window_wait_keyboard_or_mouse(keyboardButton, keyboardModifier, unicode, mouse_x, mouse_y); group_click_handler(*mouse_x, *mouse_y, &(window->group_buttons->component)); group_click_handler(*mouse_x, *mouse_y, &(window->group_pictureframe->component)); } -void window_rendering(Window *window,PictureFrame *pictureFrame1,Canvas *canvasSrc, Canvas *canvasTarget, Morphing *morphing){ +void window_rendering(Window *window, PictureFrame *pictureFrame1, Canvas *canvasSrc, Canvas *canvasTarget, + Morphing *morphing) { int i; window_print_pictureframes(window); for (i = 1; i <= frame; ++i) { diff --git a/src/main.c b/src/main.c index a4ed06c..86f071d 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,7 @@ #include #include -#include -#include +#include "gui/gui.h" +#include "MLV/MLV_path.h" int main(int argc, char **argv) { if (argc < 3) { @@ -16,10 +16,10 @@ int main(int argc, char **argv) { fprintf(stderr, "One path is incorrect\n"); exit(-3); }; - GUI *gui = gui_init(argv[1], argv[2]); + GUI *gui = gui_create(argv[1], argv[2]); while (mode != EXITING) { gui_handle_event(gui); } - gui_free(gui); + gui_destroy(gui); return 0; } diff --git a/test/gui/button.c b/test/gui/button.c index df3e04d..a436c75 100644 --- a/test/gui/button.c +++ b/test/gui/button.c @@ -21,7 +21,7 @@ static void test_button() { } }*/ /*MLV_wait_seconds(10); - window_free(&window);*/ + window_destroy(&window);*/ } int main() { diff --git a/test/gui/group.c b/test/gui/group.c index 7dbf80c..b645d26 100644 --- a/test/gui/group.c +++ b/test/gui/group.c @@ -30,7 +30,7 @@ static void test_group() { } } MLV_wait_seconds(15); - window_free(&window);*/ + window_destroy(&window);*/ } int main() { diff --git a/test/gui/pictureframe.c b/test/gui/pictureframe.c index 13926b4..f1d90df 100644 --- a/test/gui/pictureframe.c +++ b/test/gui/pictureframe.c @@ -1,92 +1,78 @@ #include "gui/component.h" -#include -#include -#include -#include -#include -#include "MLV/MLV_all.h" +#include "gui/window.h" +#include "common/mem.h" extern Mode mode; static void test_pictureframe() { - Window window; - window_init(&window, 1005, 512, "Coucou"); - window_create(&window); + Window *window = window_create(1005, 512, "Coucou"); - Button button1; - Button button2; - Button button3; - Button button4; - Button button5; - Button button6; - Button button7; + sprintf(labelFrame, "%03d frames", frame); + Button *button1 = button_create("Add constraint point", 10, 0, 0, button_click_add_constraint); + Button *button2 = button_create("Show/Hide", 10, 0, 0, button_click_show_hide); + Button *button3 = button_create("Start rendering", 10, 0, 0, button_click_rendering); + Button *button4 = button_create("<<<", 10, 0, 0, button_click_less_frame); + Button *button5 = button_create(labelFrame, 10, 0, 0, button_click_none); + Button *button6 = button_create(">>>", 10, 0, 0, button_click_more_frame); + Button *button7 = button_create("Exit", 10, 0, 0, button_click_exit); - PictureFrame pictureFrame1; - PictureFrame pictureFrame2; Morphing *morphing = morphing_create(500, 250); Canvas *canvasSrc = canvas_create_from_image("/home/adam/Images/goku.png"); Canvas *canvasTarget = canvas_create_from_image("/home/adam/Images/marty.jpg"); - sprintf(labelFrame, "%03d frames", frame); - button_init(&button1, "Add constraint point", 10, 0, 0, button_click_add_constraint); - button_init(&button2, "Show/Hide", 10, 0, 0, button_click_show_hide); - button_init(&button3, "Start rendering", 10, 0, 0, button_click_rendering); - button_init(&button4, "<<<", 10, 0, 0, button_click_less_frame); - button_init(&button5, labelFrame, 10, 0, 0, button_click_none); - button_init(&button6, ">>>", 10, 0, 0, button_click_more_frame); - button_init(&button7, "Exit", 10, 0, 0, button_click_exit); - + PictureFrame *pictureFrame1 = pictureframe_create(500, 250, 0, 0, pictureframe_origin_split, morphing, canvasSrc, + pictureframe_click_handler_origin); + PictureFrame *pictureFrame2 = pictureframe_create(500, 250, 0, 0, pictureframe_target_split, morphing, canvasTarget, + pictureframe_click_handler_target); - pictureframe_init(&pictureFrame1, 500, 250, 0, 0, pictureframe_origin_split, morphing, canvasSrc, - pictureframe_click_handler_origin); - pictureframe_init(&pictureFrame2, 500, 250, 0, 0, pictureframe_target_split, morphing, canvasTarget, - pictureframe_click_handler_target); + window_add_pictureframe(window, pictureFrame1); + window_add_pictureframe(window, pictureFrame2); - window_add_pictureframe(&window, &pictureFrame1); - window_add_pictureframe(&window, &pictureFrame2); + window_add_button(window, button1); + window_add_button(window, button2); + window_add_button(window, button3); + window_add_button(window, button4); + window_add_button(window, button5); + window_add_button(window, button6); + window_add_button(window, button7); - window_add_button(&window, &button1); - window_add_button(&window, &button2); - window_add_button(&window, &button3); - window_add_button(&window, &button4); - window_add_button(&window, &button5); - window_add_button(&window, &button6); - window_add_button(&window, &button7); - - window_print_buttons(&window); - window_print_pictureframes(&window); + window_print_buttons(window); + window_print_pictureframes(window); MLV_Keyboard_button keyboardButton; MLV_Keyboard_modifier keyboardModifier; int unicode; int mouse_x; int mouse_y; + while (mode != EXITING) { - window_click_keyboard_handler(&window, &keyboardButton, &keyboardModifier, &unicode, &mouse_x, &mouse_y); + window_click_keyboard_handler(window, &keyboardButton, &keyboardModifier, &unicode, &mouse_x, &mouse_y); switch (mode) { case PRINTING: - window_print_pictureframes(&window); + window_print_pictureframes(window); mode = WAITING_BUTTON_SHOW; break; case WAITING_BUTTON_SHOW: - window_print_pictureframes(&window); + window_print_pictureframes(window); break; case WAITING_BUTTON_HIDE: - window_print_pictureframes(&window); + window_print_pictureframes(window); break; case PRINTING_BUTTONS: - button_init(&button5, labelFrame, 10, button5.component.x_pos, button5.component.y_pos, button_click_none); - window_print_buttons(&window); + free(button5->label); + button5->label = malloc_or_die(sizeof(char) * (strlen(labelFrame) + 1)); + strcpy(button5->label, labelFrame); + window_print_buttons(window); mode = WAITING_BUTTON_SHOW; break; case RENDERING: - window_rendering(&window,&pictureFrame1,canvasSrc,canvasTarget,morphing); + window_rendering(window, pictureFrame1, canvasSrc, canvasTarget, morphing); break; case INSERT_TARGET: if (keyboardButton == MLV_KEYBOARD_ESCAPE) { - window_print_pictureframes(&window); + window_print_pictureframes(window); mode = WAITING_BUTTON_SHOW; } break; @@ -94,12 +80,23 @@ static void test_pictureframe() { break; } } + + button_destroy(button1); + button_destroy(button2); + button_destroy(button3); + button_destroy(button4); + button_destroy(button5); + button_destroy(button6); + button_destroy(button7); + + pictureframe_destroy(pictureFrame1); + pictureframe_destroy(pictureFrame2); + canvas_destroy(canvasSrc); canvas_destroy(canvasTarget); - morphing_destroy(morphing); - window_free(&window); + window_destroy(window); } int main() { diff --git a/test/gui/window.c b/test/gui/window.c index b19300e..3156989 100644 --- a/test/gui/window.c +++ b/test/gui/window.c @@ -6,7 +6,7 @@ static void test_window() { window_init(&window, 1000, 512, "Coucou"); window_create(&window); MLV_wait_seconds(150); - window_free(&window);*/ + window_destroy(&window);*/ } int main() { -- cgit v1.2.3