summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam NAILI2017-12-22 18:31:33 +0100
committerAdam NAILI2017-12-22 18:31:33 +0100
commita05d3ad97d513c03d671059a5bae18487af23e24 (patch)
treee5ff2cf856974f1d535937ab7ca96772a56a323c
parenta42fdf7d5c9712297f1bbe3dd0951baa8ee32447 (diff)
downloadmorpher-a05d3ad97d513c03d671059a5bae18487af23e24.tar.gz
Updating doc of button, implementing button_is_selected, button_print, button_click_test (debug click handling), button_init
-rw-r--r--include/gui/button.h49
-rw-r--r--src/gui/button.c56
-rw-r--r--test/gui/button.c4
3 files changed, 106 insertions, 3 deletions
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 @@
5 * File: button.h 5 * File: button.h
6 * Buttons handling 6 * Buttons handling
7 */ 7 */
8#include "window.h" 8
9#include <stdbool.h>
10#include "component.h"
9 11
10/** 12/**
11 * Struct: Button 13 * Struct: Button
12 * Component that can be triggered by click to execute a specific action. 14 * Component that can be triggered by click to execute a specific action.
13 * 15 *
14 * Fields: 16 * Fields:
15 * component - component that will acted as a button thanks to a rightful initialization. 17 * component - component that will acted as a button thanks to a rightful initialization
18 * *label - title on the button
19 * sizeInterligne - parameter that change padding of the button
16 */ 20 */
17typedef struct { 21typedef struct {
18 Component component; 22 Component component;
23 char *label;
24 int sizeInterligne;
19} Button; 25} Button;
20 26
21/** 27/**
@@ -25,8 +31,45 @@ typedef struct {
25 * Parameters: 31 * Parameters:
26 * *button - pointer to the input button 32 * *button - pointer to the input button
27 * text - label for the button 33 * text - label for the button
34 * sizeInterligne - parameter to initialize padding inside the button
35 * x_pos - position of the button on x axis
36 * y_pos - position of the button on y axis
37 * clickHandler - pointer of function that will be loaded inside our button to perform its purpose
38 */
39void button_init(Button *button, const char *text, int sizeInterligne, int x_pos, int y_pos, ClickHandler clickHandler);
40
41/**
42 * Function: button_print
43 * Prints the button.
44 *
45 * Parameters:
46 * *parameterSelf - pointer to the button
47 */
48void button_print(Component *parameterSelf);
49
50/**
51 * Function: button_click_test
52 * Debug function to test if the click is working on the current button.
53 *
54 * Parameters:
55 * x - position of the click on x axis
56 * y - position of the click on y axis
57 * *parameterSelf - pointer on the button that is clicked
28 */ 58 */
29void button_init(Button *button, char *text); 59void button_click_test(int x, int y, Component *parameterSelf);
30 60
61/**
62 * Function: button_is_selected
63 * Checks if the button is selected or not.
64 *
65 * Parameters:
66 * x - position in x for the check
67 * y - position in y for the check
68 * *button - pointer to the current button
69 *
70 * Returns:
71 * A bool from stdbool
72 */
73bool button_is_selected(int x, int y, Button *button);
31 74
32#endif 75#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 @@
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
9
10bool button_is_selected(int x, int y, Button *button) {
11 assert(x >= 0);
12 assert(y >= 0);
13 assert(button != NULL);
14 int x1 = button->component.x_pos;
15 int y1 = button->component.y_pos;
16 int x2 = button->component.x_pos + button->component.width;
17 int y2 = button->component.y_pos + button->component.height;
18 if (x >= x1 && x <= x2 && y >= y1 && y <= y2) {
19 return true;
20 }
21 return false;
22}
23
24void button_print(Component *parameterSelf) {
25 assert(parameterSelf != NULL);
26 Button *self = (Button *) parameterSelf;
27 MLV_draw_adapted_text_box(self->component.x_pos, self->component.y_pos, self->label, self->sizeInterligne,
28 MLV_COLOR_BLACK, MLV_COLOR_WHITE, MLV_COLOR_DARK_GREY, MLV_TEXT_CENTER);
29}
30
31void button_click_test(int x, int y, Component *parameterSelf) {
32 assert(x >= 0);
33 assert(y >= 0);
34 assert(parameterSelf != NULL);
35 Button *self = (Button *) parameterSelf;
36 if (button_is_selected(x, y, self)) {
37 printf("OK\n");
38 }
39}
40
41void
42button_init(Button *button, const char *text, int sizeInterligne, int x_pos, int y_pos, ClickHandler clickHandler) {
43 assert(button != NULL);
44 assert(text != NULL);
45 assert(sizeInterligne >= 0);
46 assert(x_pos >= 0);
47 assert(y_pos >= 0);
48 button->label = malloc_or_die(sizeof(char) * (strlen(text) + 1));
49 strcpy(button->label, text);
50 button->sizeInterligne = sizeInterligne;
51 MLV_get_size_of_adapted_text_box(text, sizeInterligne, &button->component.width, &button->component.height);
52 button->component.x_pos = x_pos;
53 button->component.y_pos = y_pos;
54 button->component.print_method = button_print;
55 button->component.click_handler = clickHandler;
56}
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 @@
1//
2// Created by adam on 16/12/17.
3//
4