From 41da8b54ed619ea869ca286cd8ec02e105e21c19 Mon Sep 17 00:00:00 2001 From: Adam NAILI Date: Sun, 31 Dec 2017 15:00:23 +0100 Subject: Implementing all kinds of buttons. Rendering to fix --- include/gui/button.h | 14 +++++++++ include/gui/component.h | 4 ++- include/gui/textview.h | 2 ++ src/gui/button.c | 75 +++++++++++++++++++++++++++++++++++++++++--- src/gui/component.c | 6 +++- src/gui/pictureframe.c | 64 ++++++++++++++++++++------------------ test/gui/pictureframe.c | 83 ++++++++++++++++++++++++++++++++++++++++--------- 7 files changed, 197 insertions(+), 51 deletions(-) diff --git a/include/gui/button.h b/include/gui/button.h index 26d7970..41008c1 100644 --- a/include/gui/button.h +++ b/include/gui/button.h @@ -58,6 +58,20 @@ void button_print(Component *parameterSelf); void button_click_test(int x, int y, Component *parameterSelf); void button_click_add_constraint(int x, int y, Component *parameterSelf); + +void button_click_show_hide(int x, int y, Component *parameterSelf); + +void button_click_exit(int x, int y, Component *parameterSelf); + +void button_click_none(int x, int y, Component *parameterSelf); + +void button_click_more_frame(int x, int y, Component *parameterSelf); + +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. diff --git a/include/gui/component.h b/include/gui/component.h index 0e8f437..0275d45 100644 --- a/include/gui/component.h +++ b/include/gui/component.h @@ -1,10 +1,12 @@ #ifndef UPEM_C_COMPONENT_H #define UPEM_C_COMPONENT_H typedef enum { - WAITING_BUTTON, INSERT_ORIGIN, INSERT_TARGET,PRINTING + WAITING_BUTTON_SHOW, WAITING_BUTTON_HIDE, INSERT_ORIGIN, INSERT_TARGET, PRINTING, EXITING, PRINTING_BUTTONS, RENDERING } Mode; extern Mode mode; +extern int frame; +extern char labelFrame[20]; /** * File: component.h * Windows and components handling. diff --git a/include/gui/textview.h b/include/gui/textview.h index 7a07eb3..7414336 100644 --- a/include/gui/textview.h +++ b/include/gui/textview.h @@ -1,6 +1,8 @@ #ifndef UPEM_MORPHING_TEXTVIEW #define UPEM_MORPHING_TEXTVIEW +#include "component.h" + /** * File: textview.h */ diff --git a/src/gui/button.c b/src/gui/button.c index a1fa1cf..03addf8 100644 --- a/src/gui/button.c +++ b/src/gui/button.c @@ -32,21 +32,88 @@ void button_click_test(int x, int y, Component *parameterSelf) { assert(y >= 0); assert(parameterSelf != NULL); Button *self = (Button *) parameterSelf; - if (button_is_selected(x, y, self) && mode == WAITING_BUTTON) { - printf("OK\n"); + 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){ +void button_click_add_constraint(int x, int y, Component *parameterSelf) { assert(x >= 0); assert(y >= 0); assert(parameterSelf != NULL); Button *self = (Button *) parameterSelf; - if (button_is_selected(x, y, self) && mode == WAITING_BUTTON) { + if (button_is_selected(x, y, self) && (mode == WAITING_BUTTON_SHOW || mode == WAITING_BUTTON_HIDE)) { mode = INSERT_ORIGIN; } } +void button_click_show_hide(int x, int y, Component *parameterSelf) { + assert(x >= 0); + assert(y >= 0); + assert(parameterSelf != NULL); + Button *self = (Button *) parameterSelf; + if (button_is_selected(x, y, self)) { + if (mode == WAITING_BUTTON_SHOW) { + mode = WAITING_BUTTON_HIDE; + } else if (mode == WAITING_BUTTON_HIDE) { + mode = WAITING_BUTTON_SHOW; + } + } +} + +void button_click_exit(int x, int y, Component *parameterSelf) { + assert(x >= 0); + assert(y >= 0); + assert(parameterSelf != NULL); + Button *self = (Button *) parameterSelf; + if (button_is_selected(x, y, self) && (mode == WAITING_BUTTON_SHOW || mode == WAITING_BUTTON_HIDE)) { + mode = EXITING; + } +} + +void button_click_less_frame(int x, int y, Component *parameterSelf) { + assert(x >= 0); + assert(y >= 0); + assert(parameterSelf != NULL); + Button *self = (Button *) 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); + mode = PRINTING_BUTTONS; + } + } +} + +void button_click_more_frame(int x, int y, Component *parameterSelf) { + assert(x >= 0); + assert(y >= 0); + assert(parameterSelf != NULL); + Button *self = (Button *) 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); + mode = PRINTING_BUTTONS; + } + } +} + +void button_click_rendering(int x,int y, Component *parameterSelf) { + assert(x >= 0); + assert(y >= 0); + assert(parameterSelf != NULL); + Button *self = (Button *) parameterSelf; + if (button_is_selected(x, y, self) && (mode == WAITING_BUTTON_SHOW || mode == WAITING_BUTTON_HIDE)) { + mode = RENDERING; + } +} + +void button_click_none(int x, int y, Component *parameterSelf) { + assert(x >= 0); + assert(y >= 0); + assert(parameterSelf != NULL); +} + void button_init(Button *button, const char *text, int sizeInterligne, int x_pos, int y_pos, ClickHandler clickHandler) { diff --git a/src/gui/component.c b/src/gui/component.c index 07fb336..3eb31c5 100644 --- a/src/gui/component.c +++ b/src/gui/component.c @@ -1,3 +1,7 @@ #include -Mode mode = WAITING_BUTTON; +Mode mode = WAITING_BUTTON_SHOW; + +int frame = 2; + +char labelFrame[20]; diff --git a/src/gui/pictureframe.c b/src/gui/pictureframe.c index e4081a7..4126f59 100644 --- a/src/gui/pictureframe.c +++ b/src/gui/pictureframe.c @@ -1,7 +1,6 @@ #include #include #include -#include CartesianVector pictureframe_origin_split(const CartesianMapping *cartesianMapping) { return cartesianMapping->origin; @@ -11,33 +10,39 @@ CartesianVector pictureframe_target_split(const CartesianMapping *cartesianMappi return cartesianMapping->target; } +void pictureframe_draw_canvas(PictureFrame *pictureFrame){ + MLV_draw_image(pictureFrame->canvas->mlv, pictureFrame->component.x_pos, pictureFrame->component.y_pos); +} + void pictureframe_print(Component *parameterSelf) { PictureFrame *self = (PictureFrame *) parameterSelf; - MLV_draw_image(self->canvas->mlv, self->component.x_pos, self->component.y_pos); - TriangleMap *p = self->morphing->first; - CartesianVector p1; - CartesianVector p2; - CartesianVector p3; - CartesianVector pointToPrint1; - CartesianVector pointToPrint2; - CartesianVector pointToPrint3; - while(p != NULL){ - p1 = self->cartesianMappingDivision(&(p->vertices[0])); - p2 = self->cartesianMappingDivision(&(p->vertices[1])); - p3 = self->cartesianMappingDivision(&(p->vertices[2])); + pictureframe_draw_canvas(self); + if (mode != WAITING_BUTTON_HIDE) { + TriangleMap *p = self->morphing->first; + CartesianVector p1; + CartesianVector p2; + CartesianVector p3; + CartesianVector pointToPrint1; + CartesianVector pointToPrint2; + CartesianVector pointToPrint3; + while (p != NULL) { + p1 = self->cartesianMappingDivision(&(p->vertices[0])); + p2 = self->cartesianMappingDivision(&(p->vertices[1])); + p3 = self->cartesianMappingDivision(&(p->vertices[2])); - pointToPrint1 = pictureframe_conversion_to_picture(p1.x,p1.y,self); - pointToPrint2 = pictureframe_conversion_to_picture(p2.x,p2.y,self); - pointToPrint3 = pictureframe_conversion_to_picture(p3.x,p3.y,self); + pointToPrint1 = pictureframe_conversion_to_picture(p1.x, p1.y, self); + pointToPrint2 = pictureframe_conversion_to_picture(p2.x, p2.y, self); + pointToPrint3 = pictureframe_conversion_to_picture(p3.x, p3.y, self); - MLV_draw_filled_circle(pointToPrint1.x,pointToPrint1.y,2,MLV_COLOR_RED); - MLV_draw_filled_circle(pointToPrint2.x,pointToPrint2.y,2,MLV_COLOR_RED); - MLV_draw_filled_circle(pointToPrint3.x,pointToPrint3.y,2,MLV_COLOR_RED); + MLV_draw_filled_circle(pointToPrint1.x, pointToPrint1.y, 2, MLV_COLOR_RED); + MLV_draw_filled_circle(pointToPrint2.x, pointToPrint2.y, 2, MLV_COLOR_RED); + MLV_draw_filled_circle(pointToPrint3.x, pointToPrint3.y, 2, MLV_COLOR_RED); - MLV_draw_line(pointToPrint1.x,pointToPrint1.y,pointToPrint2.x,pointToPrint2.y,MLV_COLOR_RED); - MLV_draw_line(pointToPrint1.x,pointToPrint1.y,pointToPrint3.x,pointToPrint3.y,MLV_COLOR_RED); - MLV_draw_line(pointToPrint3.x,pointToPrint3.y,pointToPrint2.x,pointToPrint2.y,MLV_COLOR_RED); - p = p->next; + MLV_draw_line(pointToPrint1.x, pointToPrint1.y, pointToPrint2.x, pointToPrint2.y, MLV_COLOR_RED); + MLV_draw_line(pointToPrint1.x, pointToPrint1.y, pointToPrint3.x, pointToPrint3.y, MLV_COLOR_RED); + MLV_draw_line(pointToPrint3.x, pointToPrint3.y, pointToPrint2.x, pointToPrint2.y, MLV_COLOR_RED); + p = p->next; + } } } @@ -72,8 +77,8 @@ CartesianVector pictureframe_conversion_to_picture(int x, int y, PictureFrame *p void pictureframe_click_handler_origin(int x_pos, int y_pos, Component *parameterSelf) { PictureFrame *self = (PictureFrame *) parameterSelf; if (pictureframe_is_selected(x_pos, y_pos, self) && mode == INSERT_ORIGIN) { - CartesianVector vector = pictureframe_conversion_to_origin(x_pos,y_pos,self); - MLV_draw_filled_circle(x_pos,y_pos,2,MLV_COLOR_BLUE); + CartesianVector vector = pictureframe_conversion_to_origin(x_pos, y_pos, self); + MLV_draw_filled_circle(x_pos, y_pos, 2, MLV_COLOR_BLUE); savedPoint = vector; MLV_actualise_window(); mode = INSERT_TARGET; @@ -83,16 +88,15 @@ void pictureframe_click_handler_origin(int x_pos, int y_pos, Component *paramete void pictureframe_click_handler_target(int x_pos, int y_pos, Component *parameterSelf) { PictureFrame *self = (PictureFrame *) parameterSelf; if (pictureframe_is_selected(x_pos, y_pos, self) && mode == INSERT_TARGET) { - CartesianVector vector = pictureframe_conversion_to_origin(x_pos,y_pos,self); - printf("(%d,%d) | (%d,%d)\n",savedPoint.x,savedPoint.y,vector.x,vector.y); - morphing_add_constraint(self->morphing,savedPoint,vector); - printf("OK\n"); + CartesianVector vector = pictureframe_conversion_to_origin(x_pos, y_pos, self); + morphing_add_constraint(self->morphing, savedPoint, vector); mode = PRINTING; } } void pictureframe_init(PictureFrame *pictureFrame, int width, int height, int x_pos, int y_pos, - CartesianMappingDivision cartesianMappingDivision, Morphing *morphing, Canvas *canvas,ClickHandler clickHandler) { + CartesianMappingDivision cartesianMappingDivision, Morphing *morphing, Canvas *canvas, + ClickHandler clickHandler) { assert(pictureFrame != NULL); assert(width > 0); assert(height > 0); diff --git a/test/gui/pictureframe.c b/test/gui/pictureframe.c index 07e1236..9aa5639 100644 --- a/test/gui/pictureframe.c +++ b/test/gui/pictureframe.c @@ -1,6 +1,9 @@ #include "gui/component.h" #include #include +#include +#include +#include #include "MLV/MLV_all.h" extern Mode mode; @@ -11,42 +14,92 @@ static void test_pictureframe() { window_create(&window); Button button1; + Button button2; + Button button3; + Button button4; + Button button5; + Button button6; + Button button7; PictureFrame pictureFrame1; PictureFrame pictureFrame2; Morphing *morphing = morphing_create(500, 250); - Canvas *canvas1 = canvas_create_from_image("/home/adam/Images/goku.png"); - Canvas *canvas2 = canvas_create_from_image("/home/adam/Images/marty.jpg"); + Canvas *canvasSrc = canvas_create_from_image("/home/adam/Images/goku.png"); + Canvas *canvasTarget = canvas_create_from_image("/home/adam/Images/marty.jpg"); + int i; + + 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_init(&pictureFrame1, 500, 250, 0, 0, pictureframe_origin_split, morphing, canvas1, + 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, canvas2, + 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_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); int mouse_x; int mouse_y; - while (1) { - if (MLV_get_mouse_button_state(MLV_BUTTON_LEFT) == MLV_PRESSED) { - MLV_get_mouse_position(&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)); - if(mode == PRINTING){ - window_print_pictureframes(&window); - mode = WAITING_BUTTON; - } + while (mode != EXITING) { + MLV_wait_mouse(&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)); + + switch (mode) { + case PRINTING: + window_print_pictureframes(&window); + mode = WAITING_BUTTON_SHOW; + break; + case WAITING_BUTTON_SHOW: + window_print_pictureframes(&window); + break; + case WAITING_BUTTON_HIDE: + 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); + mode = WAITING_BUTTON_SHOW; + break; + case RENDERING: + for (i = 0; i < frame ; ++i) { + pictureFrame1.canvas = rasterize(canvasSrc,canvasTarget,morphing,(TimeVector)(TIME_UNIT/frame)); + pictureframe_draw_canvas(&pictureFrame1); + MLV_actualise_window(); + printf("Rendering\n"); + } + break; + default: + break; } } - /*MLV_wait_seconds(15); - window_free(&window);*/ + canvas_destroy(canvasSrc); + canvas_destroy(canvasTarget); + + morphing_destroy(morphing); + + window_free(&window); } int main() { -- cgit v1.2.3