From a05d3ad97d513c03d671059a5bae18487af23e24 Mon Sep 17 00:00:00 2001 From: Adam NAILI Date: Fri, 22 Dec 2017 18:31:33 +0100 Subject: Updating doc of button, implementing button_is_selected, button_print, button_click_test (debug click handling), button_init --- include/gui/button.h | 49 ++++++++++++++++++++++++++++++++++++++++++--- src/gui/button.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ test/gui/button.c | 4 ++++ 3 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 src/gui/button.c create mode 100644 test/gui/button.c diff --git a/include/gui/button.h b/include/gui/button.h index fda83e9..6f91e37 100644 --- a/include/gui/button.h +++ b/include/gui/button.h @@ -5,17 +5,23 @@ * File: button.h * Buttons handling */ -#include "window.h" + +#include +#include "component.h" /** * Struct: Button * Component that can be triggered by click to execute a specific action. * * Fields: - * component - component that will acted as a button thanks to a rightful initialization. + * component - component that will acted as a button thanks to a rightful initialization + * *label - title on the button + * sizeInterligne - parameter that change padding of the button */ typedef struct { Component component; + char *label; + int sizeInterligne; } Button; /** @@ -25,8 +31,45 @@ typedef struct { * 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 + */ +void button_init(Button *button, const char *text, int sizeInterligne, int x_pos, int y_pos, ClickHandler clickHandler); + +/** + * Function: button_print + * Prints the button. + * + * Parameters: + * *parameterSelf - pointer to the button + */ +void button_print(Component *parameterSelf); + +/** + * Function: button_click_test + * Debug function to test if the click is working on the current button. + * + * 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_init(Button *button, char *text); +void button_click_test(int x, int y, Component *parameterSelf); +/** + * Function: button_is_selected + * Checks if the button is selected or not. + * + * 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 + */ +bool button_is_selected(int x, int y, Button *button); #endif diff --git a/src/gui/button.c b/src/gui/button.c new file mode 100644 index 0000000..fbdc172 --- /dev/null +++ b/src/gui/button.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +#include +#include + + +bool button_is_selected(int x, int y, Button *button) { + assert(x >= 0); + assert(y >= 0); + 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; +} + +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(x >= 0); + assert(y >= 0); + assert(parameterSelf != NULL); + Button *self = (Button *) parameterSelf; + if (button_is_selected(x, y, self)) { + printf("OK\n"); + } +} + +void +button_init(Button *button, const char *text, int sizeInterligne, int x_pos, int y_pos, ClickHandler clickHandler) { + assert(button != NULL); + assert(text != NULL); + assert(sizeInterligne >= 0); + assert(x_pos >= 0); + assert(y_pos >= 0); + button->label = malloc_or_die(sizeof(char) * (strlen(text) + 1)); + strcpy(button->label, text); + button->sizeInterligne = sizeInterligne; + MLV_get_size_of_adapted_text_box(text, sizeInterligne, &button->component.width, &button->component.height); + button->component.x_pos = x_pos; + button->component.y_pos = y_pos; + button->component.print_method = button_print; + button->component.click_handler = clickHandler; +} diff --git a/test/gui/button.c b/test/gui/button.c new file mode 100644 index 0000000..58270ef --- /dev/null +++ b/test/gui/button.c @@ -0,0 +1,4 @@ +// +// Created by adam on 16/12/17. +// + -- cgit v1.2.3