1 /*  DreamChess
2 **
3 **  DreamChess is the legal property of its developers, whose names are too
4 **  numerous to list here. Please refer to the AUTHORS.txt file distributed
5 **  with this source distribution.
6 **
7 **  This program is free software: you can redistribute it and/or modify
8 **  it under the terms of the GNU General Public License as published by
9 **  the Free Software Foundation, either version 3 of the License, or
10 **  (at your option) any later version.
11 **
12 **  This program is distributed in the hope that it will be useful,
13 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 **  GNU General Public License for more details.
16 **
17 **  You should have received a copy of the GNU General Public License
18 **  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include <gamegui/label.h>
25 
26 static gg_colour_t col_grey = {0.5f, 0.5f, 0.5f, 1.0f};
27 
28 static gg_colour_t col_trans = {0.0f, 0.0f, 0.0f, 0.0f};
29 
30 static gg_colour_t col_texthighlight = {0.55f, 0.65f, 0.95f, 1.0f};
31 
32 static gg_colour_t col_text = {1.0f, 1.0f, 1.0f, 1.0f};
33 
gg_label_get_class_id(void)34 gg_class_id gg_label_get_class_id(void) {
35 	GG_CHILD(gg_align_get_class_id())
36 }
37 
38 /** Implements widget::render for text widgets. */
gg_label_render(gg_widget_t * widget,int x,int y,int focus)39 void gg_label_render(gg_widget_t *widget, int x, int y, int focus) {
40 	gg_label_t *label = GG_LABEL(widget);
41 
42 	if (label->bgcolour.a != 0.0f)
43 		gg_system_draw_filled_rect(x, y, label->width_a, label->height_a, &label->bgcolour);
44 
45 	x += label->xalign * (label->width_a - label->width);
46 	y += (1.0f - label->yalign) * (label->height_a - label->height);
47 
48 	/* TODO Fix temporary hack */
49 	if (!widget->enabled)
50 		gg_system_draw_string(label->label, x, y, &col_grey, 0, 0);
51 	else if (focus != GG_FOCUS_NONE)
52 		gg_system_draw_string(label->label, x, y, &col_texthighlight, label->bouncy, 0);
53 	else
54 		gg_system_draw_string(label->label, x, y, &label->colour, 0, 0);
55 }
56 
gg_label_set_bouncy(gg_label_t * label,int bouncy)57 void gg_label_set_bouncy(gg_label_t *label, int bouncy) {
58 	label->bouncy = bouncy;
59 }
60 
gg_label_set_colour(gg_label_t * label,gg_colour_t * colour,gg_colour_t * bgcolour)61 void gg_label_set_colour(gg_label_t *label, gg_colour_t *colour, gg_colour_t *bgcolour) {
62 	if (colour)
63 		label->colour = *colour;
64 
65 	if (bgcolour)
66 		label->bgcolour = *bgcolour;
67 }
68 
69 /** @brief Destroys a text widget.
70  *
71  *  @param widget The text widget.
72  */
gg_label_destroy(gg_widget_t * widget)73 void gg_label_destroy(gg_widget_t *widget) {
74 	gg_label_t *label = GG_LABEL(widget);
75 
76 	if (label->label)
77 		free(label->label);
78 
79 	gg_widget_destroy(widget);
80 }
81 
gg_label_init(gg_label_t * label,char * text)82 void gg_label_init(gg_label_t *label, char *text) {
83 	gg_align_init((gg_align_t *)label);
84 
85 	label->render = gg_label_render;
86 	label->destroy = gg_label_destroy;
87 	label->id = gg_label_get_class_id();
88 	label->label = strdup(text);
89 	label->bouncy = 0;
90 	label->enabled = 1;
91 	label->colour = col_text;
92 	label->bgcolour = col_trans;
93 	gg_system_get_string_size(text, &label->width, &label->height);
94 	label->height += GG_BOUNCE_AMP;
95 }
96 
97 /** @brief Creates a text widget.
98  *
99  *  A text widget consists of a single label. This widget has no input
100  *  functionality.
101  *
102  *  @param string The text for the widget.
103  *  @return The created text widget.
104  */
gg_label_create(char * string)105 gg_widget_t *gg_label_create(char *string) {
106 	gg_label_t *label = malloc(sizeof(gg_label_t));
107 
108 	gg_label_init(label, string);
109 
110 	return GG_WIDGET(label);
111 }
112