summaryrefslogtreecommitdiffstats
path: root/src/tk/text.c
blob: 95bd03b68e48140a32957167984e1a7ecc96865e (plain)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * 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 <SDL_ttf.h>
#include <errno.h>
#include <string.h>
#include "../tk.h"
#include "../util.h"
#include "widget.h"

struct mftk_text {
	struct mftk_widget  parent;
	char                min_char;
	char                max_char;
	size_t              len;
	size_t              cur;
	int                 y;
	int                 w;
	int                 h;
	char               *val;
	TTF_Font           *font;
	int                 line_skip;
	int                 ascent;
	SDL_Color          *color;
	SDL_Texture        *texture;
};

static void
_mftk_text_layout(struct mftk_widget *w __attribute__((__unused__)))
{
	/* Size set in constructor */
}

static int
_mftk_text_key_event(struct mftk_widget *w, SDL_Event *e)
{
	struct mftk_text *t = (struct mftk_text *) w;

	switch (e->type) {
		case SDL_KEYDOWN:
			/* TODO */
			t->texture = NULL;
			break;
		default:
			break;
	}

	return 0;
}

static int
_mftk_text_mouse_event(struct mftk_widget *w, SDL_Event *e,
		int x __attribute__((__unused__)),
		int y __attribute__((__unused__)))
{
	switch (e->type) {
		case SDL_MOUSEBUTTONUP:
			if (e->button.button == SDL_BUTTON_LEFT) {
				mftk_window_focus(w->window, w);
			}
			break;
		default:
			break;
	}

	return 0;
}

static int
_mftk_text_render_val(struct mftk_text *t, SDL_Renderer *renderer)
{
	SDL_Surface *surface;
	int          max_y;
	const char  *c;
	int          glyph_max_y;

	surface = TTF_RenderUTF8_Blended(t->font, t->val, *t->color);
	if (surface == NULL) {
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
				"Couldn't create surface: %s",
				TTF_GetError());
		return -1;
	}

	t->texture = SDL_CreateTextureFromSurface(renderer, surface);
	if (t->texture == NULL) {
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
				"Couldn't create texture: %s",
				SDL_GetError());
		SDL_FreeSurface(surface);
		return -1;
	}

	max_y = 0;
	for (c = t->val; *c != '\0'; ++c) {
		if (TTF_GlyphMetrics(t->font, *c, NULL, NULL, NULL,
					&glyph_max_y, NULL) < 0) {
			SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
					"Couldn't get glyph metrics: %s",
					TTF_GetError());
			continue;
		}
		if (glyph_max_y > max_y) {
			max_y = glyph_max_y;
		}
	}
	t->y = (t->line_skip - t->ascent) / 2 + (t->ascent - max_y);
	t->w = surface->w;
	t->h = surface->h;

	SDL_FreeSurface(surface);

	return 0;
}

static int
_mftk_text_render(struct mftk_widget *w, SDL_Renderer *renderer, int x, int y)
{
	struct mftk_text *t = (struct mftk_text *) w;
	SDL_Rect          rect;
	char             *val;
	int               cur_x;

	if (t->texture == NULL && _mftk_text_render_val(t, renderer) < 0) {
		return -1;
	}

	rect.x =        x;
	rect.y = t->y + y;
	rect.w = t->w;
	rect.h = t->h;

	val = mf_strdup(t->val);
	if (val == NULL) {
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
				"Couldn't render widget: %s",
				strerror(errno));
		return -1;
	}
	val[t->cur] = '\0';
	TTF_SizeUTF8(t->font, val, &cur_x, NULL);

	if (SDL_RenderCopy(renderer, t->texture, NULL, &rect) < 0 ||
			SDL_SetRenderDrawColor(renderer,
				t->color->r, t->color->g,
				t->color->b, t->color->a) < 0 ||
			SDL_RenderDrawLine(renderer,
				x, y + w->h, x + w->w, y + w->h) < 0) {
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
				"Couldn't render widget: %s",
				SDL_GetError());
		free(val);
		return -1;
	}
	if (w->focused == SDL_TRUE && SDL_RenderDrawLine(renderer,
				x + cur_x, y + t->y, x + cur_x, y + t->h) < 0) {
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
				"Couldn't render widget: %s",
				SDL_GetError());
		free(val);
		return -1;
	}

	free(val);
	return 0;
}

static void
_mftk_text_destroy(struct mftk_widget *w)
{
	struct mftk_text *t = (struct mftk_text *) w;

	free(t->val);
	SDL_DestroyTexture(t->texture);
}

struct mftk_widget *
mftk_text_new(char min_char, char max_char, size_t len, const char *val,
		TTF_Font *font, SDL_Color *color)
{
	struct mftk_widget *w;
	struct mftk_text   *t;
	int                 advance;
	char                ch;

	mftk_widget_init_focusable(w, t, text);

	t->min_char  = min_char;
	t->max_char  = max_char;
	t->len       = len;
	t->cur       = strlen(val);
	t->val       = mf_strdup(val);
	t->font      = font;
	t->line_skip = TTF_FontLineSkip(font);
	t->ascent    = TTF_FontAscent  (font);
	t->color     = color;
	t->texture   = NULL;

	w->w = 0;
	for (ch = min_char; ch <= max_char; ++ch) {
		if (TTF_GlyphMetrics(font, ch, NULL, NULL, NULL, NULL, &advance)
				< 0) {
			SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
					"Couldn't get glyph metrics: %s",
					TTF_GetError());
			continue;
		}
		advance *= len;
		if (advance > w->w) {
			w->w = advance;
		}
	}
	w->h = t->line_skip + 1;

	return w;
}