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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
/*
* Copyright (C) 2021 P. J. McDermott
*
* This file is part of Maze Fight
*
* Maze Fight is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Maze Fight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Maze Fight. If not, see <http://www.gnu.org/licenses/>.
*/
#include <SDL.h>
#include "../tk.h"
#include "style.h"
#include "widget.h"
struct mftk_button {
struct mftk_widget parent;
struct mftk_widget *label;
int padding;
int (*action)(void *);
void *user_data;
};
static void
_mftk_button_layout(struct mftk_widget *w)
{
struct mftk_button *b = (struct mftk_button *) w;
mftk_widget_layout(b->label);
w->w = b->label->w + b->padding * 2;
w->h = b->label->h + b->padding * 2;
}
static void
_mftk_button_focus(struct mftk_widget *w __attribute__((__unused__)))
{
/* Nothing to do */
}
static void
_mftk_button_defocus(struct mftk_widget *w __attribute__((__unused__)))
{
/* Nothing to do */
}
static int
_mftk_button_key_event(struct mftk_widget *w, SDL_Event *e)
{
struct mftk_button *b = (struct mftk_button *) w;
switch (e->type) {
case SDL_KEYDOWN:
switch (e->key.keysym.sym) {
case SDLK_SPACE:
case SDLK_RETURN:
case SDLK_KP_ENTER:
if (b->action == NULL) {
return 0;
}
return b->action(b->user_data);
default:
break;
}
default:
break;
}
return 0;
}
static int
_mftk_button_mouse_event(struct mftk_widget *w, SDL_Event *e,
int x __attribute__((__unused__)),
int y __attribute__((__unused__)))
{
struct mftk_button *b = (struct mftk_button *) w;
switch (e->type) {
case SDL_MOUSEBUTTONUP:
if (e->button.button == SDL_BUTTON_LEFT) {
mftk_window_focus(w->window, w);
if (b->action == NULL) {
return 0;
}
return b->action(b->user_data);
}
break;
default:
break;
}
return 0;
}
static int
_mftk_button_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y)
{
struct mftk_button *b = (struct mftk_button *) w;
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = w->w;
rect.h = w->h;
if (SDL_SetRenderDrawColor(renderer,
MFTK_COLOR_BUTN_R, MFTK_COLOR_BUTN_G,
MFTK_COLOR_BUTN_B, MFTK_COLOR_BUTN_A) < 0 ||
SDL_RenderFillRect(renderer, &rect) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Couldn't render widget: %s",
SDL_GetError());
return -1;
}
if (mftk_widget_render(b->label, renderer,
x + b->padding, y + b->padding) < 0) {
return -1;
}
return 0;
}
static void
_mftk_button_destroy(struct mftk_widget *w)
{
struct mftk_button *b = (struct mftk_button *) w;
mftk_widget_destroy(&b->label);
}
struct mftk_widget *
mftk_button_new(TTF_Font *font, const char *text, int padding,
int (*action)(void *), void *user_data, SDL_Renderer *renderer)
{
struct mftk_widget *w;
struct mftk_button *b;
mftk_widget_init_focusable(w, b, button);
b->label = mftk_label_new(font, text, renderer);
if (b->label == NULL) {
free(b);
return NULL;
}
b->padding = padding;
b->action = action;
b->user_data = user_data;
return w;
}
|