1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
#include <glib.h>
#include "button.h"
#include "log.h"
Button *button_simple_create(PangoAlignment align, Color color, Color line_color)
{
Button *btn = g_malloc0(sizeof(ButtonSimple));
btn->simple = true;
btn->align = align;
btn->color = color;
btn->line_color = line_color;
CAST(btn, ButtonSimple)->action = NULL;
return btn;
}
void button_simple_set_text(Button *btn, char *text, Color text_color)
{
g_assert(btn->simple);
ButtonSimple *sbtn = CAST(btn, ButtonSimple);
if (sbtn->text != NULL) g_free(sbtn->text);
sbtn->text = text;
sbtn->text_color = text_color;
}
const char *button_simple_get_text(Button *btn)
{
g_assert(btn->simple);
return CAST(btn, ButtonSimple)->text;
}
void button_simple_set_action(Button *btn, ButtonAction action, gpointer data)
{
g_assert(btn->simple);
CAST(btn, ButtonSimple)->action = action;
CAST(btn, ButtonSimple)->action_data = data;
}
ButtonAction button_simple_get_action(Button *btn)
{
g_assert(btn->simple);
return CAST(btn, ButtonSimple)->action;
}
Button *button_group_create(PangoAlignment align, Color color, Color line_color)
{
Button *btn = g_malloc0(sizeof(ButtonGroup));
btn->simple = false;
btn->align = align;
btn->color = color;
btn->line_color = line_color;
return btn;
}
void button_group_append(Button *btn, Button *child)
{
g_assert(!btn->simple);
CAST(btn, ButtonGroup)->children = g_list_append(CAST(btn, ButtonGroup)->children, child);
}
void button_set_padding(Button *btn, int padding)
{
g_assert(padding >= 0);
btn->padding = padding;
}
bool button_set_animation(Button *btn, Animation *anim)
{
if (btn->anim) return false;
btn->anim = anim;
return true;
}
// Wrapper for animation sources
static gboolean anim_handler(gpointer data)
{
Button *btn = data;
if (btn->anim->handler(btn->anim))
return G_SOURCE_CONTINUE;
g_free(btn->anim);
btn->anim = NULL;
return G_SOURCE_REMOVE;
}
void button_start_animation(Button *btn)
{
// Maybe error out
if (btn->anim == NULL) {
log_warning("Button animation was not set");
return;
}
const int fps = 60;
if (btn->anim->handler(btn->anim))
g_timeout_add(1000 / 60, anim_handler, btn);
}
void button_destroy(Button *btn)
{
if (btn->simple) g_free(CAST(btn, ButtonSimple)->text);
animation_destroy(btn->anim);
g_free(btn);
}
|