Store result of snprintf as size_t to placate -Wall
[clav.git] / ui-sdl.c
blob6c31011731a906ba82bc5ec30d8e5a7feb48bf9e
1 /*
2 * Copyright (c) 2017, S. Gilles <sgilles@math.umd.edu>
4 * Permission to use, copy, modify, and/or distribute this software
5 * for any purpose with or without fee is hereby granted, provided
6 * that the above copyright notice and this permission notice appear
7 * in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
13 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
14 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <errno.h>
19 #include <math.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
24 #include <SDL.h>
25 #include <SDL_ttf.h>
27 #include "file-selection.h"
28 #include "macros.h"
29 #include "quiver.h"
30 #include "ui.h"
32 #define TICKS_PER_FRAME (1000 / 60)
34 #ifndef M_PI
35 #define M_PI (3.14159265358979323846)
36 #endif
38 /* What clicking does */
39 static enum ui_action {
40 UIA_NONE = 0,
41 UIA_ASK_QUIT,
42 UIA_DEC_FATNESS,
43 UIA_DELETE,
44 UIA_ENTER_LOAD,
45 UIA_ENTER_RENAME,
46 UIA_ENTER_SAVE,
47 UIA_INC_FATNESS,
48 UIA_MUTATE,
49 UIA_NEW_EDGE_1,
50 UIA_NEW_EDGE_2,
51 UIA_NEW_H_EDGE_1,
52 UIA_NEW_H_EDGE_2,
53 UIA_NEW_VERTEX,
54 UIA_RENAME,
55 UIA_LEN
56 } ui_action;
58 /* The window we'll be using */
59 static SDL_Window *sdl_win;
61 /* The renderer we'll be using */
62 static SDL_Renderer *sdl_renderer;
64 /* How to limit the framerate */
65 static Uint32 frame_start_ticks;
67 /* Whether the scene needs to be drawn again */
68 static uint_fast8_t redraw;
70 /* Whether a vertex was just dragged (necessitating info redraw) */
71 static uint_fast8_t dragged_selected_vertex;
73 /* The informative texture */
74 static SDL_Texture *selected_info;
76 /* Buffer for event queue */
77 static struct ui_event *eq_buf;
79 /* Current max length of event queue */
80 static size_t eq_len;
82 /* Current head of queue */
83 static size_t eq_head;
85 /* Current tail of queue */
86 static size_t eq_tail;
88 /* The quiver we'll be using */
89 static struct quiver *q;
91 /* Width of drawing area in pixels */
92 static int da_pix_width;
94 /* Height of drawing area in pixels */
95 static int da_pix_height;
97 /* The background color */
98 static SDL_Color color_bg = { .r = 0xe2, .g = 0xe2, .b = 0xe2, .a = 0xff };
100 /* The normal vertex color */
101 static SDL_Color color_v = { .r = 0x82, .g = 0x82, .b = 0xb2, .a = 0xff };
103 /* The vertex color for preview vertices */
104 static SDL_Color color_v_preview = { .r = 0x82, .g = 0x82, .b = 0xb2, .a =
105 0x40 };
107 /* The normal vertex outline color */
108 static SDL_Color color_outline = { .r = 0x12, .g = 0x12, .b = 0x12, .a = 0x80 };
110 /* The vertex outline color for preview vertices */
111 static SDL_Color color_outline_preview = { .r = 0x12, .g = 0x12, .b = 0x12, .a =
112 0x20 };
114 /* The selected vertex outline color */
115 static SDL_Color color_outline_sel = { .r = 0x42, .g = 0x42, .b = 0xe2, .a =
116 0x80 };
118 /* The font color */
119 static SDL_Color color_font = { .r = 0x12, .g = 0x12, .b = 0x12, .a = 0x80 };
121 /* The normal edge color */
122 static SDL_Color color_e_normal = { .r = 0x12, .g = 0x12, .b = 0x12, .a =
123 0xff };
125 /* The half edge color */
126 static SDL_Color color_e_half = { .r = 0x12, .g = 0x12, .b = 0x12, .a = 0x60 };
128 /* The abnormal edge color */
129 static SDL_Color color_e_abnormal = { .r = 0xd0, .g = 0x12, .b = 0x12, .a =
130 0xf0 };
132 /* The selected edge color */
133 static SDL_Color color_e_sel = { .r = 0xb2, .g = 0xb2, .b = 0xe2, .a = 0x40 };
135 /* The font for node names, instructions, etc */
136 static TTF_Font *normal_font;
138 /* The base font size */
139 static uint_fast8_t base_font_size = 12;
141 /* How much space (pixels) between lower left of screen and bottom text */
142 static unsigned int text_border_padding = 24;
144 /* Strings to display at bottom of screen */
145 static const char *bottom_string[] = {
146 /* */
147 [UIA_NONE] = "[m] Mutate\n[v] Create new vertex\n"
148 "[d] Delete vertex/edge\n[e] Add edge\n"
149 "[h] Add half edge\n[f] Increase vertex fatness\n"
150 "[g] Decrease vertex fatness\n[r] Rename vertex\n"
151 "[s] Save quiver\n[l] Load quiver\n[q] Quit", /* */
152 [UIA_ASK_QUIT] = "Quit?\n\n[y] Confirm\n[n] Cancel", /* */
153 [UIA_DEC_FATNESS] = "[Mouse1] Decrease fatness\n[ESC] Cancel", /* */
154 [UIA_DELETE] = "[Mouse1] Delete vertex/edge\n[ESC] Cancel", /* */
155 [UIA_ENTER_LOAD] = "[Enter] Load from\n[ESC] Cancel", /* */
156 [UIA_ENTER_RENAME] = "[Enter] Rename\n[ESC] Cancel", /* */
157 [UIA_ENTER_SAVE] = "[Enter] Save to\n[ESC] Cancel", /* */
158 [UIA_INC_FATNESS] = "[Mouse1] Increase fatness\n[ESC] Cancel", /* */
159 [UIA_MUTATE] = "[Mouse1] Mutate at vertex\n[ESC] Cancel", /* */
160 [UIA_NEW_EDGE_1] = "[Mouse1] Select start\n[ESC] Cancel", /* */
161 [UIA_NEW_EDGE_2] = "[Mouse1] Select end\n[ESC] Cancel", /* */
162 [UIA_NEW_H_EDGE_1] = "[Mouse1] Select start\n[ESC] Cancel", /* */
163 [UIA_NEW_H_EDGE_2] = "[Mouse1] Select end\n[ESC] Cancel", /* */
164 [UIA_NEW_VERTEX] = "[Mouse1] Create new vertex\n[ESC] Cancel", /* */
165 [UIA_RENAME] = "[Mouse1] Rename vertex\n[ESC] Cancel", /* */
166 [UIA_LEN] = "" /* */
169 /* The texture containing what to show on the bottom */
170 static SDL_Texture *bottom_text[UIA_LEN];
172 /* The textures containing the names of all vertices - indexed just like q */
173 static SDL_Texture **vertex_names;
175 /* The number of elements of vertex_names */
176 static size_t vertex_names_len;
178 /* Drawing offset x */
179 static int offset_x;
181 /* Drawing offset x */
182 static int offset_y;
184 /* How wide (in pixels) a fatness 1 node should be */
185 static unsigned int base_node_radius = 8;
187 /* How much (in pixels) a node should widen for each fatness level */
188 static unsigned int node_radius_per_fatness = 7;
190 /* How wide (in pixels) the outline should be */
191 static int outline_width = 2;
193 /* How wide (in pixels) the arrowheads should be */
194 static int arrow_length = 7;
196 /* How narrow the arrowheads should be */
197 static double arrow_angle = 6.5 * M_PI / 8.0;
199 /* How close to an arrow (in pixels) for it to be selected */
200 static int edge_margin = 5;
202 /* If we're interacting with a vertex, which one */
203 static size_t selected_vertex = (size_t) -1;
205 /* If we're interacting with an edge, the i */
206 static size_t selected_edge_i = (size_t) -1;
208 /* If we're interacting with an edge, the j */
209 static size_t selected_edge_j = (size_t) -1;
211 /* If we're adding an edge, the last vertex we clicked on */
212 static size_t last_clicked_vertex = (size_t) -1;
214 /* x-coordinate of last mouse position */
215 static int last_mouse_x = -1;
217 /* y-coordinate of last mouse position */
218 static int last_mouse_y = -1;
220 /* Window title */
221 static char window_title[2048];
223 /* What name we last loaded/saved */
224 static char current_filename[2048] = { 'U', 'n', 't', 'i', 't', 'l', 'e', 'd',
225 0 };
227 /* Whether we've edited since last loading/saving */
228 static uint_fast8_t current_file_edited = 0;
230 /* Maximum length of an input string we'll accept */
231 static size_t max_input_size = 1 << 10;
233 /* Current position in input */
234 static size_t input_idx;
236 /* Current length of string held in input */
237 static size_t input_len;
239 /* If a string is being typed in, what it is (in UTF-8 as per SDL2) */
240 static char *input;
242 /* Input, with `Filename:' or `New name:' prepended (for rendering) */
243 static char *input_with_prefix;
245 /* Texture for what user is currently entering */
246 static SDL_Texture *input_texture;
248 /* Whether we need to re-render the input */
249 static uint_fast8_t rerender_input_string;
251 /* To prevent awkward repeats in SDL events, rate-limit `s' and `l' */
252 static uint_fast8_t save_load_delay;
254 /* If something needs to be freed next frame, but not before */
255 static void *free_this;
257 /* GCD */
258 static int gcd(uint_fast8_t x, uint_fast8_t y)
260 uint_fast8_t r = 0;
262 if (!x &&
263 !y) {
264 return 1;
267 while (y) {
268 r = x % y;
269 x = y;
270 y = r;
273 return x;
276 /* Allocate and print a rational in the way a user expects */
277 static int pretty_fraction(struct rational *r, char **out)
279 int ret = 0;
281 if (r->q == 1) {
282 size_t len = snprintf(0, 0, "%d", (int) r->p);
284 if (!(*out = malloc(len + 1))) {
285 ret = errno;
286 perror(L("malloc"));
287 goto done;
290 sprintf(*out, "%d", (int) r->p);
291 goto done;
294 size_t len = snprintf(0, 0, "%d/%u", (int) r->p, (unsigned int) r->q);
296 if (!(*out = malloc(len + 1))) {
297 ret = errno;
298 perror(L("malloc"));
299 goto done;
302 sprintf(*out, "%d/%u", (int) r->p, (unsigned int) r->q);
303 done:
305 return ret;
308 /* Render text to texture */
309 static int render_text(const char *text, SDL_Texture **out)
311 if (!out) {
312 return 0;
315 if (*out) {
316 SDL_DestroyTexture(*out);
319 int ret = 0;
320 SDL_Surface *surf = 0;
322 if (!(surf = TTF_RenderUTF8_Blended_Wrapped(normal_font, text,
323 color_font, 800))) {
324 fprintf(stderr, L("TTF_RenderUTF8_Shaded(): %s\n"),
325 TTF_GetError());
326 ret = ENOMEDIUM;
327 goto done;
330 if (!(*out = SDL_CreateTextureFromSurface(sdl_renderer, surf))) {
331 fprintf(stderr, L("SDL_CreateTextureFromSurface(): %s\n"),
332 SDL_GetError());
333 ret = ENOMEDIUM;
334 goto done;
337 done:
338 SDL_FreeSurface(surf);
340 return ret;
343 /* Load fonts */
344 static int load_fonts(void)
346 int ret = 0;
348 if (normal_font) {
349 TTF_CloseFont(normal_font);
350 normal_font = 0;
353 if (!(normal_font = TTF_OpenFont(FONT_PATH, base_font_size))) {
354 ret = ENOMEDIUM;
355 fprintf(stderr, L("TTF_OpenFont(): %s\n"), TTF_GetError());
356 goto done;
359 for (size_t j = 0; j < UIA_LEN; ++j) {
360 if ((ret = render_text(bottom_string[j], &bottom_text[j]))) {
361 goto done;
365 done:
367 if (ret) {
368 if (normal_font) {
369 TTF_CloseFont(normal_font);
372 normal_font = 0;
375 return ret;
378 /* Insert str into input at given position */
379 static void text_input(const char *str)
381 size_t l = strlen(str);
383 if (l + input_len + 1 >= max_input_size) {
384 return;
387 for (size_t k = input_len; k > input_idx; --k) {
388 input[k + l] = input[k];
391 /* Special-case for input_idx: 0 prohibits `>=' above */
392 input[input_idx + l] = input[input_idx];
394 for (size_t k = 0; k < l; ++k) {
395 input[input_idx + k] = str[k];
398 input_idx += l;
399 input_len += l;
400 rerender_input_string = 1;
403 /* Move input cursor left */
404 static void text_input_left(void)
406 if (input_idx > 0) {
407 input_idx--;
410 while (input_idx > 0) {
411 unsigned char c = input[input_idx];
413 if ((c & 0xc0) == 0x80) {
414 input_idx--;
415 } else {
416 break;
421 /* Move input cursor right */
422 static void text_input_right(void)
424 size_t adv = 1;
425 unsigned char c = input[input_idx];
427 if ((c & 0x80) == 0x0) {
428 adv = 1;
429 } else if ((c & 0xe0) == 0xc0) {
430 adv = 2;
431 } else if ((c & 0xf0) == 0xe0) {
432 adv = 3;
433 } else if ((c & 0xf8) == 0xf0) {
434 adv = 4;
437 if (input_idx + adv <= input_len) {
438 input_idx += adv;
442 /* Backspace */
443 static void text_input_bs(void)
445 if (!input_idx) {
446 return;
449 size_t bs = 1;
451 while (input_idx > bs) {
452 unsigned char c = input[input_idx - bs];
454 if ((c & 0xc0) == 0x80) {
455 bs++;
456 } else {
457 break;
461 for (size_t k = input_idx - bs; k <= input_len - bs; k++) {
462 input[k] = input[k + bs];
465 input_len -= bs;
466 input_idx -= bs;
467 rerender_input_string = 1;
470 /* Set the title to something like `clav-sdl - foobar.txt*' */
471 static void update_window_title(void)
473 size_t cutoff = sizeof (window_title) / sizeof (window_title[0]);
474 size_t r = snprintf(window_title, cutoff, "clav-sdl - %s%s",
475 current_filename, (current_file_edited ? "*" : ""));
477 if (r >= cutoff) {
478 window_title[cutoff - 1] = '\0';
481 SDL_SetWindowTitle(sdl_win, window_title);
484 /* Make sure the window title displays as edited */
485 static void mark_as_edited(void)
487 if (current_file_edited) {
488 return;
491 current_file_edited = 1;
492 update_window_title();
495 /* Convert `internal coordinates' to pixel coordinates */
496 static void internal_to_pixel_xy(int in_x, int in_y, int *out_x, int *out_y)
498 *out_x = in_x + offset_x;
499 *out_y = in_y + offset_y;
502 /* Convert pixel coordinates to `internal coordinates' */
503 static void pixel_to_internal_xy(int in_x, int in_y, int *out_x, int *out_y)
505 *out_x = in_x - offset_x;
506 *out_y = in_y - offset_y;
509 /* Set selected_vertex and selected_edge_{i,j} */
510 static int recalculate_selected_items(int mx, int my)
512 int x = 0;
513 int y = 0;
514 int ret = 0;
515 char *s = 0;
516 char *sij = 0;
517 char *sji = 0;
519 pixel_to_internal_xy(mx, my, &x, &y);
520 size_t last_vertex = selected_vertex;
521 size_t last_edge_i = selected_edge_i;
522 size_t last_edge_j = selected_edge_j;
524 selected_vertex = (size_t) -1;
525 selected_edge_i = (size_t) -1;
526 selected_edge_j = (size_t) -1;
528 if (last_vertex != (size_t) -1) {
529 struct vertex *v = &(q->v[last_vertex]);
530 int r = base_node_radius + v->fatness * node_radius_per_fatness;
532 if (x > v->x - r &&
533 x < v->x + r &&
534 y > v->y - r &&
535 y < v->y + r) {
536 selected_vertex = last_vertex;
537 goto compute_str;
541 for (size_t j = q->v_num; j > 0; --j) {
542 struct vertex *v = &(q->v[j - 1]);
543 int r = base_node_radius + v->fatness * node_radius_per_fatness;
545 if (x > v->x - r &&
546 x < v->x + r &&
547 y > v->y - r &&
548 y < v->y + r) {
549 selected_vertex = j - 1;
550 goto compute_str;
554 for (size_t j = 1; j < q->v_num; ++j) {
555 struct vertex *v1 = &(q->v[j]);
557 for (size_t i = 0; i < j; ++i) {
558 if (!q->e[i * q->v_len + j].p &&
559 !q->e[j * q->v_len + i].p) {
560 continue;
563 struct vertex *v2 = &(q->v[i]);
565 if ((x - edge_margin > v1->x &&
566 x - edge_margin > v2->x) ||
567 (x + edge_margin < v1->x &&
568 x + edge_margin < v2->x) ||
569 (y - edge_margin > v1->y &&
570 y - edge_margin > v2->y) ||
571 (y + edge_margin < v1->y &&
572 y + edge_margin < v2->y)) {
573 continue;
576 if (v1->x == v2->x) {
577 if (x + edge_margin > v1->x &&
578 x - edge_margin < v1->x) {
579 selected_edge_i = i;
580 selected_edge_j = j;
581 goto compute_str;
583 } else if (v1->y == v2->y) {
584 if (y + edge_margin > v1->y &&
585 y - edge_margin < v1->y) {
586 selected_edge_i = i;
587 selected_edge_j = j;
588 goto compute_str;
590 } else {
591 double m1 = ((double) (v2->y - v1->y)) /
592 ((double) (v2->x - v1->x));
593 double m2 = -1.0 / m1;
594 double xint = ((double) y - (double) v1->y +
595 m1 * v1->x - m2 * x) / (m1 - m2);
596 double yint = m1 * xint - m1 * v1->x +
597 (double) v1->y;
599 if ((x - xint) * (x - xint) + (y - yint) * (y -
600 yint)
601 < edge_margin * edge_margin) {
602 selected_edge_i = i;
603 selected_edge_j = j;
604 goto compute_str;
610 compute_str:
612 if (selected_vertex != (size_t) -1 &&
613 (selected_vertex != last_vertex ||
614 dragged_selected_vertex)) {
615 dragged_selected_vertex = 0;
616 struct vertex *v = &(q->v[selected_vertex]);
617 size_t len = snprintf(0, 0,
618 "Name: %s\nFatness: %d\nPosition: (%d,%d)",
619 v->name,
620 (int) v->fatness, v->x, v->y);
622 if (!(s = malloc(len + 1))) {
623 ret = errno;
624 perror(L("malloc"));
625 goto done;
628 sprintf(s, "Name: %s\nFatness: %d\nPosition: (%d,%d)",
629 v->name, (int) v->fatness, v->x, v->y);
631 if ((ret = render_text(s, &selected_info))) {
632 goto done;
634 } else if ((selected_edge_i != last_edge_i ||
635 selected_edge_j != last_edge_j) &&
636 selected_edge_i != (size_t) -1 &&
637 selected_edge_j != (size_t) -1) {
638 struct vertex *i = &(q->v[selected_edge_i]);
639 struct vertex *j = &(q->v[selected_edge_j]);
640 struct rational *eij = &(q->e[selected_edge_i * q->v_len +
641 selected_edge_j]);
642 struct rational *eji = &(q->e[selected_edge_j * q->v_len +
643 selected_edge_i]);
645 if ((ret = pretty_fraction(eij, &sij))) {
646 goto done;
649 if ((ret = pretty_fraction(eji, &sji))) {
650 goto done;
653 size_t len = snprintf(0, 0,
654 "%s \u2192 %s: %s\n%s \u2192 %s: %s",
655 i->name, j->name, sij,
656 j->name, i->name, sji);
658 if (!(s = malloc(len + 1))) {
659 ret = errno;
660 perror(L("malloc"));
661 goto done;
664 sprintf(s, "%s \u2192 %s: %s\n%s \u2192 %s: %s", i->name,
665 j->name, sij, j->name, i->name, sji);
667 if ((ret = render_text(s, &selected_info))) {
668 goto done;
672 done:
673 free(s);
674 free(sij);
675 free(sji);
677 return ret;
680 /* Render vertex names as textures */
681 static int render_vertex_names(void)
683 if (vertex_names) {
684 for (size_t j = 0; j < vertex_names_len; ++j) {
685 SDL_DestroyTexture(vertex_names[j]);
686 vertex_names[j] = 0;
690 if (!q->v_num) {
691 return 0;
694 if (!(vertex_names = realloc(vertex_names, q->v_num *
695 sizeof(*vertex_names)))) {
696 int sv_err = errno;
698 perror(L("realloc()"));
699 vertex_names_len = 0;
701 return sv_err;
704 vertex_names_len = q->v_num;
706 for (size_t j = 0; j < vertex_names_len; ++j) {
707 vertex_names[j] = 0;
710 for (size_t j = 0; j < vertex_names_len; ++j) {
711 int ret = 0;
713 if ((ret = render_text(q->v[j].name, &(vertex_names[j])))) {
714 return ret;
718 return 0;
721 /* Get information about the window */
722 static void react_to_window_resized(void)
724 int old_pix_width = da_pix_width;
725 int old_pix_height = da_pix_height;
727 SDL_GetWindowSize(sdl_win, &da_pix_width, &da_pix_height);
729 if (old_pix_width == da_pix_width &&
730 old_pix_height == da_pix_height) {
731 return;
734 offset_x += (da_pix_width - old_pix_width) / 2;
735 offset_y += (da_pix_height - old_pix_height) / 2;
738 /* Pop from queue */
739 static void eq_pop(struct ui_event *out)
741 if (eq_head == eq_tail) {
742 *out = (struct ui_event) { 0 };
744 return;
747 memcpy(out, eq_buf + eq_head, sizeof *out);
748 eq_buf[eq_head] = (struct ui_event) { 0 };
749 eq_head = (eq_head + 1) % eq_len;
752 /* Push into queue */
753 static int eq_push(struct ui_event *in)
755 if (((eq_tail + 1) % eq_len) == eq_head) {
756 void *newmem;
758 if ((eq_len * sizeof *in) >= ((size_t) -1) / 2) {
759 fprintf(stderr, L(
760 "eq_push: Impossibly large buffer\n"));
762 return ENOMEM;
765 if (!(newmem = realloc(eq_buf, (eq_len * 2) *
766 sizeof *eq_buf))) {
767 int sv_err = errno;
769 perror(L("realloc"));
771 return sv_err;
774 eq_buf = (struct ui_event *) newmem;
775 eq_len *= 2;
778 memcpy(eq_buf + eq_tail, in, sizeof *in);
779 eq_tail = (eq_tail + 1) % eq_len;
781 return 0;
784 /* Initialize SDL */
785 int ui_init(struct quiver *i_q)
787 int ret;
789 q = i_q;
790 size_t padding = strlen("Filename: ");
792 if (!(input_with_prefix = malloc(max_input_size + padding))) {
793 ret = errno;
794 perror(L("malloc"));
796 return ret;
799 strcpy(input_with_prefix, "Filename: ");
800 input = input_with_prefix + padding;
801 input_idx = 0;
802 input_len = 0;
804 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
805 fprintf(stderr, L("SDL_Init(): %s\n"), SDL_GetError());
807 return ENOMEDIUM;
810 sdl_win = SDL_CreateWindow("clav-sdl", SDL_WINDOWPOS_UNDEFINED,
811 SDL_WINDOWPOS_UNDEFINED, 1000, 1000,
812 SDL_WINDOW_SHOWN |
813 SDL_WINDOW_RESIZABLE);
815 if (!sdl_win) {
816 fprintf(stderr, L("SDL_CreateWindow(): %s\n"), SDL_GetError());
818 return ENOMEDIUM;
821 sdl_renderer = SDL_CreateRenderer(sdl_win, -1,
822 SDL_RENDERER_ACCELERATED);
824 if (!sdl_renderer) {
825 fprintf(stderr, L("SDL_CreateRenderer(): %s\n"),
826 SDL_GetError());
828 return ENOMEDIUM;
831 if (TTF_Init() < 0) {
832 fprintf(stderr, L("TTF_Init(): %s\n"), TTF_GetError());
834 return ENOMEDIUM;
837 if ((ret = load_fonts())) {
838 goto done;
841 if ((ret = render_vertex_names())) {
842 goto done;
845 if ((ret = SDL_SetRenderDrawColor(sdl_renderer, color_bg.r, color_bg.g,
846 color_bg.b, color_bg.a))) {
847 fprintf(stderr, L("SDL_SetRenderDrawColor(): %s\n"),
848 SDL_GetError());
849 goto done;
852 if ((ret = SDL_RenderClear(sdl_renderer))) {
853 fprintf(stderr, L("SDL_RenderClear(): %s\n"), SDL_GetError());
854 goto done;
857 update_window_title();
858 SDL_RenderPresent(sdl_renderer);
859 react_to_window_resized();
861 /* Set up queue for returning data */
862 if (!(eq_buf = calloc(2, sizeof *eq_buf))) {
863 ret = errno;
864 perror(L("malloc"));
865 goto done;
868 eq_len = 2;
869 eq_head = 0;
870 eq_tail = 0;
871 done:
873 return ret;
876 /* Deal with the fact that the quiver was changed */
877 int ui_respond_quiver_change(void)
879 return render_vertex_names();
882 /* Acknowledge a successful load */
883 int ui_respond_successful_load(const char *filename)
885 const char *tail = (const char *) strrchr(filename, '/');
887 if (!tail) {
888 tail = filename;
889 } else {
890 tail++;
893 size_t cutoff = sizeof(current_filename) / sizeof(current_filename[0]);
894 size_t r = snprintf(current_filename, cutoff, "%s", tail);
896 if (r >= cutoff) {
897 current_filename[cutoff - 1] = '\0';
900 current_file_edited = 0;
901 update_window_title();
903 return 0;
906 /* Acknowledge a successful save */
907 int ui_respond_successful_save(const char *filename)
909 return ui_respond_successful_load(filename);
912 /* Tear down SDL */
913 int ui_teardown(void)
915 if (vertex_names) {
916 for (size_t j = 0; j < vertex_names_len; ++j) {
917 SDL_DestroyTexture(vertex_names[j]);
918 vertex_names[j] = 0;
921 free(vertex_names);
922 vertex_names = 0;
925 for (size_t j = 0; j < UIA_LEN; ++j) {
926 SDL_DestroyTexture(bottom_text[j]);
927 bottom_text[j] = 0;
930 if (selected_info) {
931 SDL_DestroyTexture(selected_info);
932 selected_info = 0;
935 if (input_texture) {
936 SDL_DestroyTexture(input_texture);
937 input_texture = 0;
940 if (normal_font) {
941 TTF_CloseFont(normal_font);
942 normal_font = 0;
945 if (sdl_win) {
946 SDL_DestroyWindow(sdl_win);
947 sdl_win = 0;
950 free(eq_buf);
951 eq_buf = 0;
952 free(input_with_prefix);
953 input_with_prefix = 0;
954 input = 0;
955 SDL_Quit();
957 return 0;
960 /* Record that a frame has been started */
961 int ui_start_frame(void)
963 int ret = 0;
964 int rho = 0;
965 SDL_Rect r = { 0 };
966 Uint32 dummy_format;
967 int dummy_access;
968 int tex_w;
969 int tex_h;
971 frame_start_ticks = SDL_GetTicks();
973 if (!redraw) {
974 goto done;
977 redraw = 0;
979 /* Draw the damn thing */
980 SDL_SetRenderDrawColor(sdl_renderer, color_bg.r, color_bg.g, color_bg.b,
981 color_bg.a);
982 SDL_RenderClear(sdl_renderer);
984 /* Special case for if text input is going on */
985 if (rerender_input_string) {
986 rerender_input_string = 0;
988 if ((ret = render_text(input_with_prefix, &input_texture))) {
989 goto done;
993 /* Draw each edge */
994 for (size_t j = 0; j < q->v_num; ++j) {
995 for (size_t i = 0; i < j; ++i) {
996 /* First, determine if we're looking at a half-edge or a full-edge */
997 int d = gcd(q->v[i].fatness, q->v[j].fatness);
998 struct rational *eij = &(q->e[i * q->v_len + j]);
999 struct rational *eji = &(q->e[j * q->v_len + i]);
1000 int cx = 0;
1001 int cy = 0;
1002 int cx2 = 0;
1003 int cy2 = 0;
1004 double theta = 0.0;
1006 if (!eij->p &&
1007 !eji->p) {
1008 continue;
1011 internal_to_pixel_xy(q->v[i].x, q->v[i].y, &cx, &cy);
1012 internal_to_pixel_xy(q->v[j].x, q->v[j].y, &cx2, &cy2);
1014 if (selected_edge_i == i &&
1015 selected_edge_j == j) {
1016 if ((ret = SDL_SetRenderDrawColor(sdl_renderer,
1017 color_e_sel.r,
1018 color_e_sel.g,
1019 color_e_sel.b,
1020 color_e_sel.a)))
1022 fprintf(stderr, L(
1023 "SDL_RenderDrawColor(): %s\n"),
1024 SDL_GetError());
1025 goto done;
1028 for (int id = -edge_margin; id < edge_margin;
1029 ++id) {
1030 for (int jd = -edge_margin; jd <
1031 edge_margin; ++jd) {
1032 if ((ret = SDL_RenderDrawLine(
1033 sdl_renderer, cx +
1034 id, cy + jd,
1035 cx2 + id, cy2 +
1036 jd))) {
1037 fprintf(stderr, L(
1038 "SDL_RenderDrawLine(): %s\n"),
1039 SDL_GetError());
1040 goto done;
1046 /* This is the (eij)/dj = -(eji)/di condition */
1047 if (eij->p * q->v[i].fatness * eji->q != -eji->p *
1048 q->v[j].fatness * eij->q) {
1049 ret = SDL_SetRenderDrawColor(sdl_renderer,
1050 color_e_abnormal.r,
1051 color_e_abnormal.g,
1052 color_e_abnormal.b,
1053 color_e_abnormal.a);
1054 } else if (abs(eij->p) * d == q->v[j].fatness *
1055 eij->q) {
1056 ret = SDL_SetRenderDrawColor(sdl_renderer,
1057 color_e_normal.r,
1058 color_e_normal.g,
1059 color_e_normal.b,
1060 color_e_normal.a);
1061 } else if (2 * abs(eij->p) * d == q->v[j].fatness *
1062 eij->q) {
1063 ret = SDL_SetRenderDrawColor(sdl_renderer,
1064 color_e_half.r,
1065 color_e_half.g,
1066 color_e_half.b,
1067 color_e_half.a);
1068 } else {
1069 ret = SDL_SetRenderDrawColor(sdl_renderer,
1070 color_e_abnormal.r,
1071 color_e_abnormal.g,
1072 color_e_abnormal.b,
1073 color_e_abnormal.a);
1076 if (ret) {
1077 fprintf(stderr, L(
1078 "SDL_SetRenderDrawColor(): %s\n"),
1079 SDL_GetError());
1080 goto done;
1083 if ((ret = SDL_RenderDrawLine(sdl_renderer, cx, cy, cx2,
1084 cy2))) {
1085 fprintf(stderr, L("SDL_RenderDrawLine(): %s\n"),
1086 SDL_GetError());
1087 goto done;
1090 if (cx == cx2) {
1091 theta = (cy2 > cy) ? M_PI / 2.0 : -M_PI / 2.0;
1092 } else {
1093 theta = atan2f(cy2 - cy, cx2 - cx);
1096 if ((eij->p < 0)) {
1097 theta += M_PI;
1100 cx = (cx + cx2) / 2;
1101 cy = (cy + cy2) / 2;
1102 cx2 = cx + arrow_length * cos(theta + arrow_angle);
1103 cy2 = cy + arrow_length * sin(theta + arrow_angle);
1105 if ((ret = SDL_RenderDrawLine(sdl_renderer, cx, cy, cx2,
1106 cy2))) {
1107 fprintf(stderr, L("SDL_RenderDrawLine(): %s\n"),
1108 SDL_GetError());
1109 goto done;
1112 if ((ret = SDL_RenderDrawLine(sdl_renderer, cx, cy, cx +
1113 arrow_length * cos(theta -
1114 arrow_angle),
1115 cy +
1116 arrow_length * sin(theta -
1117 arrow_angle))))
1119 fprintf(stderr, L("SDL_RenderDrawLine(): %s\n"),
1120 SDL_GetError());
1121 goto done;
1126 /* Draw each vertex as a box */
1127 for (size_t j = 0; j < q->v_num; ++j) {
1128 struct vertex *v = &(q->v[j]);
1129 int cx = 0;
1130 int cy = 0;
1132 internal_to_pixel_xy(v->x, v->y, &cx, &cy);
1134 /* Central square */
1135 SDL_SetRenderDrawBlendMode(sdl_renderer, SDL_BLENDMODE_NONE);
1136 SDL_SetRenderDrawColor(sdl_renderer, color_v.r, color_v.g,
1137 color_v.b, color_v.a);
1138 rho = base_node_radius + node_radius_per_fatness * v->fatness;
1139 r.x = cx - rho;
1140 r.y = cy - rho;
1141 r.w = 2 * rho;
1142 r.h = 2 * rho;
1143 SDL_RenderFillRect(sdl_renderer, &r);
1145 /* Outline */
1146 SDL_SetRenderDrawBlendMode(sdl_renderer, SDL_BLENDMODE_BLEND);
1148 if (j == selected_vertex ||
1149 j == last_clicked_vertex) {
1150 SDL_SetRenderDrawColor(sdl_renderer,
1151 color_outline_sel.r,
1152 color_outline_sel.g,
1153 color_outline_sel.b,
1154 color_outline_sel.a);
1155 } else {
1156 SDL_SetRenderDrawColor(sdl_renderer, color_outline.r,
1157 color_outline.g, color_outline.b,
1158 color_outline.a);
1161 r.x = cx - rho;
1162 r.y = cy - rho;
1163 r.w = 2 * rho - outline_width;
1164 r.h = outline_width;
1165 SDL_RenderFillRect(sdl_renderer, &r);
1166 r.x = cx + rho - outline_width;
1167 r.y = cy - rho;
1168 r.w = outline_width;
1169 r.h = 2 * rho - outline_width;
1170 SDL_RenderFillRect(sdl_renderer, &r);
1171 r.x = cx - rho + outline_width;
1172 r.y = cy + rho - outline_width;
1173 r.w = 2 * rho - outline_width;
1174 r.h = outline_width;
1175 SDL_RenderFillRect(sdl_renderer, &r);
1176 r.x = cx - rho;
1177 r.y = cy - rho + outline_width;
1178 r.w = outline_width;
1179 r.h = 2 * rho - outline_width;
1180 SDL_RenderFillRect(sdl_renderer, &r);
1182 /* Text */
1183 if (j >= vertex_names_len) {
1184 fprintf(stderr, L(
1185 "render_vertex_names() was not called, somehow\n"));
1186 ret = EINVAL;
1187 goto done;
1190 if (SDL_QueryTexture(vertex_names[j], &dummy_format,
1191 &dummy_access, &tex_w, &tex_h)) {
1192 fprintf(stderr, L("SDL_QueryTexture(): %s\n"),
1193 SDL_GetError());
1194 ret = ENOMEDIUM;
1195 goto done;
1198 r.x = cx - tex_w / 2;
1199 r.y = cy - tex_h / 2;
1200 r.w = tex_w;
1201 r.h = tex_h;
1202 SDL_RenderCopy(sdl_renderer, vertex_names[j], 0, &r);
1205 /* If adding a new vertex, draw preview */
1206 if (ui_action == UIA_NEW_VERTEX &&
1207 last_mouse_x != -1 &&
1208 last_mouse_y != -1) {
1209 /* Central square */
1210 SDL_SetRenderDrawBlendMode(sdl_renderer, SDL_BLENDMODE_NONE);
1211 SDL_SetRenderDrawColor(sdl_renderer, color_v_preview.r,
1212 color_v_preview.g, color_v_preview.b,
1213 color_v_preview.a);
1214 rho = base_node_radius;
1215 r.x = last_mouse_x - rho;
1216 r.y = last_mouse_y - rho;
1217 r.w = 2 * rho;
1218 r.h = 2 * rho;
1219 SDL_RenderFillRect(sdl_renderer, &r);
1221 /* Outline */
1222 SDL_SetRenderDrawBlendMode(sdl_renderer, SDL_BLENDMODE_BLEND);
1223 SDL_SetRenderDrawColor(sdl_renderer, color_outline_preview.r,
1224 color_outline_preview.g,
1225 color_outline_preview.b,
1226 color_outline_preview.a);
1227 r.x = last_mouse_x - rho;
1228 r.y = last_mouse_y - rho;
1229 r.w = 2 * rho - outline_width;
1230 r.h = outline_width;
1231 SDL_RenderFillRect(sdl_renderer, &r);
1232 r.x = last_mouse_x + rho - outline_width;
1233 r.y = last_mouse_y - rho;
1234 r.w = outline_width;
1235 r.h = 2 * rho - outline_width;
1236 SDL_RenderFillRect(sdl_renderer, &r);
1237 r.x = last_mouse_x - rho + outline_width;
1238 r.y = last_mouse_y + rho - outline_width;
1239 r.w = 2 * rho - outline_width;
1240 r.h = outline_width;
1241 SDL_RenderFillRect(sdl_renderer, &r);
1242 r.x = last_mouse_x - rho;
1243 r.y = last_mouse_y - rho + outline_width;
1244 r.w = outline_width;
1245 r.h = 2 * rho - outline_width;
1246 SDL_RenderFillRect(sdl_renderer, &r);
1249 /* If adding a new edge, draw possible */
1250 if ((ui_action == UIA_NEW_EDGE_2 ||
1251 ui_action == UIA_NEW_H_EDGE_2) &&
1253 /* last_clicked_vertex != (size_t) -1 && */
1254 last_mouse_x != -1 &&
1255 last_mouse_y != -1) {
1256 int cx = 0;
1257 int cy = 0;
1259 if ((ret = SDL_SetRenderDrawColor(sdl_renderer,
1260 color_e_normal.r,
1261 color_e_normal.g,
1262 color_e_normal.b,
1263 color_e_normal.a))) {
1264 fprintf(stderr, L("SDL_RenderDrawLine(): %s\n"),
1265 SDL_GetError());
1266 goto done;
1269 internal_to_pixel_xy(q->v[last_clicked_vertex].x,
1270 q->v[last_clicked_vertex].y, &cx, &cy);
1272 if ((ret = SDL_RenderDrawLine(sdl_renderer, cx, cy,
1273 last_mouse_x, last_mouse_y))) {
1274 fprintf(stderr, L("SDL_RenderDrawLine(): %s\n"),
1275 SDL_GetError());
1276 goto done;
1280 /* Bottom text */
1281 if (SDL_QueryTexture(bottom_text[ui_action], &dummy_format,
1282 &dummy_access, &tex_w, &tex_h)) {
1283 fprintf(stderr, L("SDL_QueryTexture(): %s\n"), SDL_GetError());
1284 ret = ENOMEDIUM;
1285 goto done;
1288 r.x = text_border_padding;
1289 r.y = da_pix_height - tex_h - text_border_padding;
1290 r.w = tex_w;
1291 r.h = tex_h;
1292 SDL_RenderCopy(sdl_renderer, bottom_text[ui_action], 0, &r);
1294 /* If something is selected */
1295 if (selected_info &&
1296 (selected_vertex != (size_t) -1 ||
1297 (selected_edge_i != (size_t) -1 &&
1298 selected_edge_j != (size_t) -1))) {
1299 if (SDL_QueryTexture(selected_info, &dummy_format,
1300 &dummy_access, &tex_w, &tex_h)) {
1301 fprintf(stderr, L("SDL_QueryTexture(): %s\n"),
1302 SDL_GetError());
1303 ret = ENOMEDIUM;
1304 goto done;
1307 r.x = text_border_padding;
1308 r.y = text_border_padding;
1309 r.w = tex_w;
1310 r.h = tex_h;
1311 SDL_RenderCopy(sdl_renderer, selected_info, 0, &r);
1314 /* If user is entering text */
1315 if (ui_action == UIA_ENTER_SAVE ||
1316 ui_action == UIA_ENTER_LOAD ||
1317 ui_action == UIA_ENTER_RENAME) {
1318 if (SDL_QueryTexture(bottom_text[ui_action], &dummy_format,
1319 &dummy_access, &tex_w, &tex_h)) {
1320 fprintf(stderr, L("SDL_QueryTexture(): %s\n"),
1321 SDL_GetError());
1322 ret = ENOMEDIUM;
1323 goto done;
1326 r.x = text_border_padding;
1327 r.y = da_pix_height - tex_h - text_border_padding;
1329 if (SDL_QueryTexture(input_texture, &dummy_format,
1330 &dummy_access, &tex_w, &tex_h)) {
1331 fprintf(stderr, L("SDL_QueryTexture(): %s\n"),
1332 SDL_GetError());
1333 ret = ENOMEDIUM;
1334 goto done;
1337 r.y -= (tex_h + text_border_padding);
1338 r.w = tex_w;
1339 r.h = tex_h;
1340 SDL_RenderCopy(sdl_renderer, input_texture, 0, &r);
1342 /* Now draw a cursor */
1343 char store_idxchar = input[input_idx];
1345 input[input_idx] = '\0';
1347 if ((ret = TTF_SizeUTF8(normal_font, input_with_prefix, &tex_w,
1348 &tex_h))) {
1349 fprintf(stderr, L("TTF_SizeText(): %s\n"),
1350 TTF_GetError());
1351 goto done;
1354 input[input_idx] = store_idxchar;
1356 if ((ret = SDL_SetRenderDrawColor(sdl_renderer, color_font.r,
1357 color_font.g, color_font.b,
1358 color_font.a))) {
1359 fprintf(stderr, L("SDL_SetRenderDrawColor(): %s\n"),
1360 SDL_GetError());
1361 goto done;
1364 if ((ret = SDL_RenderDrawLine(sdl_renderer, r.x + tex_w, r.y,
1365 r.x + tex_w, r.y + tex_h))) {
1366 fprintf(stderr, L("SDL_RenderDrawLine(): %s\n"),
1367 SDL_GetError());
1368 goto done;
1372 done:
1374 return ret;
1377 /* Draw a frame, possibly sleeping for framelimit */
1378 int ui_finish_frame(void)
1380 int ret = 0;
1381 struct ui_event ui_e = { 0 };
1382 SDL_Event sdl_e = { 0 };
1383 Uint32 now = 0;
1384 uint_fast8_t save_requested = 0;
1385 uint_fast8_t load_requested = 0;
1387 if (free_this) {
1388 free(free_this);
1389 free_this = 0;
1392 if (save_load_delay) {
1393 save_load_delay--;
1396 SDL_RenderPresent(sdl_renderer);
1398 /* Handle user input */
1399 while (SDL_PollEvent(&sdl_e) != 0) {
1400 redraw = 1;
1401 SDL_Keycode k = 0;
1403 switch (sdl_e.type) {
1404 case SDL_QUIT:
1405 ui_e = (struct ui_event) { .type = ET_QUIT };
1406 ret = eq_push(&ui_e);
1407 break;
1408 case SDL_TEXTINPUT:
1409 text_input(sdl_e.text.text);
1410 break;
1411 case SDL_KEYUP:
1413 if (sdl_e.key.repeat) {
1414 break;
1417 k = sdl_e.key.keysym.sym;
1419 switch (ui_action) {
1420 case UIA_ENTER_SAVE:
1421 case UIA_ENTER_LOAD:
1422 case UIA_ENTER_RENAME:
1424 if (k == SDLK_ESCAPE) {
1425 SDL_StopTextInput();
1426 ui_action = UIA_NONE;
1427 last_clicked_vertex = (size_t) -1;
1428 } else if (k == SDLK_RETURN ||
1429 k == SDLK_RETURN2) {
1430 SDL_StopTextInput();
1431 ui_e = (struct ui_event) { .str =
1432 input };
1434 if (ui_action == UIA_ENTER_SAVE) {
1435 ui_e.type = ET_SAVE;
1436 } else if (ui_action ==
1437 UIA_ENTER_LOAD) {
1438 ui_e.type = ET_LOAD;
1439 } else if (ui_action ==
1440 UIA_ENTER_RENAME) {
1441 ui_e.type = ET_RENAME;
1442 ui_e.idx_1 =
1443 last_clicked_vertex;
1446 ret = eq_push(&ui_e);
1447 ui_action = UIA_NONE;
1448 last_clicked_vertex = (size_t) -1;
1449 } else if (k == SDLK_LEFT) {
1450 text_input_left();
1451 } else if (k == SDLK_RIGHT) {
1452 text_input_right();
1453 } else if (k == SDLK_BACKSPACE) {
1454 text_input_bs();
1457 break;
1458 case UIA_NONE:
1460 if (k == SDLK_q) {
1461 ui_action = UIA_ASK_QUIT;
1462 } else if (k == SDLK_d) {
1463 ui_action = UIA_DELETE;
1464 } else if (k == SDLK_e) {
1465 ui_action = UIA_NEW_EDGE_1;
1466 } else if (k == SDLK_f) {
1467 ui_action = UIA_INC_FATNESS;
1468 } else if (k == SDLK_g) {
1469 ui_action = UIA_DEC_FATNESS;
1470 } else if (k == SDLK_h) {
1471 ui_action = UIA_NEW_H_EDGE_1;
1472 } else if (k == SDLK_m) {
1473 ui_action = UIA_MUTATE;
1474 } else if (k == SDLK_r) {
1475 ui_action = UIA_RENAME;
1476 } else if (k == SDLK_v) {
1477 ui_action = UIA_NEW_VERTEX;
1478 } else if (k == SDLK_l &&
1479 !save_load_delay) {
1480 /* Don't load - SDL_KEYUP repeats */
1481 load_requested = 1;
1482 } else if (k == SDLK_s &&
1483 !save_load_delay) {
1484 save_requested = 1;
1487 break;
1488 case UIA_ASK_QUIT:
1490 if (k == SDLK_n ||
1491 k == SDLK_ESCAPE) {
1492 ui_action = UIA_NONE;
1493 } else if (k == SDLK_y) {
1494 ui_e = (struct ui_event) { .type =
1495 ET_QUIT };
1496 ret = eq_push(&ui_e);
1497 ui_action = UIA_NONE;
1500 break;
1501 default:
1503 if (k == SDLK_q ||
1504 k == SDLK_ESCAPE) {
1505 ui_action = UIA_NONE;
1508 break;
1511 break;
1512 case SDL_WINDOWEVENT:
1514 if (sdl_e.window.event == SDL_WINDOWEVENT_RESIZED ||
1515 sdl_e.window.event == SDL_WINDOWEVENT_MAXIMIZED ||
1516 sdl_e.window.event == SDL_WINDOWEVENT_RESTORED) {
1517 react_to_window_resized();
1518 } else if (sdl_e.window.event ==
1519 SDL_WINDOWEVENT_LEAVE) {
1520 /* This tells the dragging code to not respond */
1521 last_mouse_x = -1;
1522 last_mouse_y = -1;
1525 break;
1526 case SDL_MOUSEMOTION:
1528 if (sdl_e.motion.state & SDL_BUTTON_LMASK) {
1529 int x = sdl_e.motion.x;
1530 int y = sdl_e.motion.y;
1532 if (last_mouse_x >= 0 &&
1533 last_mouse_y >= 0) {
1534 if (selected_vertex != (size_t) -1) {
1535 q->v[selected_vertex].x += (x -
1536 last_mouse_x);
1537 q->v[selected_vertex].y += (y -
1538 last_mouse_y);
1539 dragged_selected_vertex = 1;
1540 recalculate_selected_items(
1541 sdl_e.motion.x,
1542 sdl_e.motion.y);
1543 mark_as_edited();
1544 } else {
1545 offset_x += (x - last_mouse_x);
1546 offset_y += (y - last_mouse_y);
1549 } else {
1550 recalculate_selected_items(sdl_e.motion.x,
1551 sdl_e.motion.y);
1554 last_mouse_x = sdl_e.motion.x;
1555 last_mouse_y = sdl_e.motion.y;
1556 break;
1557 case SDL_MOUSEBUTTONUP:
1559 if ((sdl_e.button.state & SDL_BUTTON_LMASK) &&
1560 ui_action != UIA_NEW_VERTEX) {
1561 last_mouse_x = -1;
1562 last_mouse_y = -1;
1565 recalculate_selected_items(sdl_e.button.x,
1566 sdl_e.button.y);
1567 break;
1568 case SDL_MOUSEBUTTONDOWN:
1570 if (!(sdl_e.button.state & SDL_BUTTON_LMASK)) {
1571 break;
1574 if (ui_action != UIA_NEW_VERTEX) {
1575 last_mouse_x = -1;
1576 last_mouse_y = -1;
1579 switch (ui_action) {
1580 case UIA_MUTATE:
1582 if (selected_vertex == (size_t) -1) {
1583 break;
1586 ui_e = (struct ui_event) {
1587 /* */
1588 .type = ET_MUTATE, .idx_1 =
1589 selected_vertex
1591 ret = eq_push(&ui_e);
1592 ui_action = UIA_NONE;
1593 break;
1594 case UIA_NEW_VERTEX:
1596 if (selected_vertex != (size_t) -1 ||
1597 selected_edge_i != (size_t) -1 ||
1598 selected_edge_j != (size_t) -1) {
1599 break;
1602 int cx = sdl_e.button.x - offset_x;
1603 int cy = sdl_e.button.y - offset_y;
1605 ui_e = (struct ui_event) {
1606 /* */
1607 .type = ET_NEW_VERTEX, .int_1 = cx,
1608 .int_2 = cy
1610 ret = eq_push(&ui_e);
1611 ui_action = UIA_NONE;
1612 break;
1613 case UIA_NEW_EDGE_1:
1614 case UIA_NEW_H_EDGE_1:
1615 case UIA_RENAME:
1617 if (selected_vertex == (size_t) -1) {
1618 ui_action = UIA_NONE;
1619 break;
1622 last_clicked_vertex = selected_vertex;
1624 if (ui_action == UIA_NEW_EDGE_1) {
1625 ui_action = UIA_NEW_EDGE_2;
1626 } else if (ui_action == UIA_NEW_H_EDGE_1) {
1627 ui_action = UIA_NEW_H_EDGE_2;
1628 } else if (ui_action == UIA_RENAME) {
1629 ui_action = UIA_ENTER_RENAME;
1630 input_len = 0;
1631 input_idx = 0;
1632 input[0] = '\0';
1633 SDL_StartTextInput();
1635 /* Intentionally not copying null terminator */
1636 strncpy(input_with_prefix, "New name: ",
1637 10);
1638 rerender_input_string = 1;
1641 break;
1642 case UIA_NEW_EDGE_2:
1643 case UIA_NEW_H_EDGE_2:
1645 if (selected_vertex == (size_t) -1 ||
1646 selected_vertex == last_clicked_vertex) {
1647 ui_action = UIA_NONE;
1648 last_clicked_vertex = (size_t) -1;
1649 break;
1652 ui_e = (struct ui_event) {
1653 /* */
1654 .type = ET_NEW_EDGE, .idx_1 =
1655 last_clicked_vertex, .idx_2 =
1656 selected_vertex, .a = 1, .b =
1657 (ui_action == UIA_NEW_EDGE_2 ?
1658 1 : 2)
1660 ret = eq_push(&ui_e);
1661 ui_action = UIA_NONE;
1662 last_clicked_vertex = (size_t) -1;
1663 break;
1664 case UIA_DELETE:
1666 if (selected_vertex != (size_t) -1) {
1667 ui_e = (struct ui_event) {
1668 /* */
1669 .type = ET_DELETE_VERTEX,
1670 .idx_1 = selected_vertex
1672 ret = eq_push(&ui_e);
1673 } else if (selected_edge_i != (size_t) -1 &&
1674 selected_edge_j != (size_t) -1) {
1675 ui_e = (struct ui_event) {
1676 /* */
1677 .type = ET_DELETE_EDGE, .idx_1 =
1678 selected_edge_i,
1679 .idx_2 =
1680 selected_edge_j
1682 ret = eq_push(&ui_e);
1685 ui_action = UIA_NONE;
1686 break;
1687 case UIA_INC_FATNESS:
1688 case UIA_DEC_FATNESS:
1690 if (selected_vertex == (size_t) -1) {
1691 break;
1694 ui_e = (struct ui_event) {
1695 /* */
1696 .type = ET_CHANGE_FATNESS, .idx_1 =
1697 selected_vertex, .int_1 =
1698 (ui_action ==
1699 UIA_INC_FATNESS
1700 ? 1 : -1)
1702 ret = eq_push(&ui_e);
1703 ui_action = UIA_NONE;
1704 break;
1705 case UIA_NONE:
1706 case UIA_ASK_QUIT:
1707 case UIA_ENTER_SAVE:
1708 case UIA_ENTER_LOAD:
1709 case UIA_ENTER_RENAME:
1710 case UIA_LEN:
1711 break;
1714 break;
1717 if (ret) {
1718 goto done;
1722 if (load_requested ||
1723 save_requested) {
1724 save_load_delay = 30;
1725 char *f = 0;
1726 int r = 0;
1728 if (load_requested) {
1729 r = choose_load_file(&f);
1730 } else {
1731 r = choose_save_file(&f);
1734 if (!r) {
1735 if (f) {
1736 ui_e = (struct ui_event) { .str = f };
1737 ui_e.type = load_requested ? ET_LOAD : ET_SAVE;
1739 /* f is freed on next ui_finish_frame */
1740 free_this = f;
1741 ret = eq_push(&ui_e);
1743 } else {
1744 ui_action = load_requested ? UIA_ENTER_LOAD :
1745 UIA_ENTER_SAVE;
1746 input_idx = 0;
1747 input_len = 0;
1748 input[0] = '\0';
1749 SDL_StartTextInput();
1751 /* Intentionally not copying null terminator */
1752 strncpy(input_with_prefix, "Filename: ", 10);
1753 rerender_input_string = 1;
1757 /* framelimit */
1758 now = SDL_GetTicks();
1760 if (frame_start_ticks < now) {
1761 Uint32 elapsed_time = now - frame_start_ticks;
1763 if (elapsed_time < TICKS_PER_FRAME) {
1764 SDL_Delay(TICKS_PER_FRAME - elapsed_time);
1768 done:
1770 return ret;
1773 /* Return an event to the main loop */
1774 int ui_get_event(struct ui_event *e, uint_fast8_t *more)
1776 eq_pop(e);
1777 *more = eq_head != eq_tail;
1779 if (e) {
1780 switch (e->type) {
1781 case ET_NONE:
1782 case ET_LOAD:
1783 case ET_SAVE:
1784 case ET_QUIT:
1785 break;
1786 case ET_CHANGE_FATNESS:
1787 case ET_DELETE_EDGE:
1788 case ET_DELETE_VERTEX:
1789 case ET_MUTATE:
1790 case ET_NEW_EDGE:
1791 case ET_NEW_VERTEX:
1792 case ET_RENAME:
1793 mark_as_edited();
1794 break;
1798 return 0;