Fix up trig constants
[clav.git] / ui-sdl.c
bloba9c82c27653145c4c7b2ee3e63cbcd63981cf361
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 /* Maximum length of an input string we'll accept */
221 static size_t max_input_size = 1 << 10;
223 /* Current position in input */
224 static size_t input_idx;
226 /* Current length of string held in input */
227 static size_t input_len;
229 /* If a string is being typed in, what it is (in UTF-8 as per SDL2) */
230 static char *input;
232 /* Input, with `Filename:' or `New name:' prepended (for rendering) */
233 static char *input_with_prefix;
235 /* Texture for what user is currently entering */
236 static SDL_Texture *input_texture;
238 /* Whether we need to re-render the input */
239 static uint_fast8_t rerender_input_string;
241 /* To prevent awkward repeats in SDL events, rate-limit `s' and `l' */
242 static uint_fast8_t save_load_delay;
244 /* If something needs to be freed next frame, but not before */
245 static void *free_this;
247 /* GCD */
248 static int gcd(uint_fast8_t x, uint_fast8_t y)
250 uint_fast8_t r = 0;
252 if (!x &&
253 !y) {
254 return 1;
257 while (y) {
258 r = x % y;
259 x = y;
260 y = r;
263 return x;
266 /* Allocate and print a rational in the way a user expects */
267 static int pretty_fraction(struct rational *r, char **out)
269 int ret = 0;
271 if (r->q == 1) {
272 size_t len = snprintf(0, 0, "%d", (int) r->p);
274 if (!(*out = malloc(len + 1))) {
275 ret = errno;
276 perror(L("malloc"));
277 goto done;
280 sprintf(*out, "%d", (int) r->p);
281 goto done;
284 size_t len = snprintf(0, 0, "%d/%u", (int) r->p, (unsigned int) r->q);
286 if (!(*out = malloc(len + 1))) {
287 ret = errno;
288 perror(L("malloc"));
289 goto done;
292 sprintf(*out, "%d/%u", (int) r->p, (unsigned int) r->q);
293 done:
295 return ret;
298 /* Render text to texture */
299 static int render_text(const char *text, SDL_Texture **out)
301 if (!out) {
302 return 0;
305 if (*out) {
306 SDL_DestroyTexture(*out);
309 int ret = 0;
310 SDL_Surface *surf = 0;
312 if (!(surf = TTF_RenderUTF8_Blended_Wrapped(normal_font, text,
313 color_font, 800))) {
314 fprintf(stderr, L("TTF_RenderUTF8_Shaded(): %s\n"),
315 TTF_GetError());
316 ret = ENOMEDIUM;
317 goto done;
320 if (!(*out = SDL_CreateTextureFromSurface(sdl_renderer, surf))) {
321 fprintf(stderr, L("SDL_CreateTextureFromSurface(): %s\n"),
322 SDL_GetError());
323 ret = ENOMEDIUM;
324 goto done;
327 done:
328 SDL_FreeSurface(surf);
330 return ret;
333 /* Load fonts */
334 static int load_fonts(void)
336 int ret = 0;
338 if (normal_font) {
339 TTF_CloseFont(normal_font);
340 normal_font = 0;
343 if (!(normal_font = TTF_OpenFont(FONT_PATH, base_font_size))) {
344 ret = ENOMEDIUM;
345 fprintf(stderr, L("TTF_OpenFont(): %s\n"), TTF_GetError());
346 goto done;
349 for (size_t j = 0; j < UIA_LEN; ++j) {
350 if ((ret = render_text(bottom_string[j], &bottom_text[j]))) {
351 goto done;
355 done:
357 if (ret) {
358 if (normal_font) {
359 TTF_CloseFont(normal_font);
362 normal_font = 0;
365 return ret;
368 /* Insert str into input at given position */
369 static void text_input(const char *str)
371 size_t l = strlen(str);
373 if (l + input_len + 1 >= max_input_size) {
374 return;
377 for (size_t k = input_len; k > input_idx; --k) {
378 input[k + l] = input[k];
381 /* Special-case for input_idx: 0 prohibits `>=' above */
382 input[input_idx + l] = input[input_idx];
384 for (size_t k = 0; k < l; ++k) {
385 input[input_idx + k] = str[k];
388 input_idx += l;
389 input_len += l;
390 rerender_input_string = 1;
393 /* Move input cursor left */
394 static void text_input_left(void)
396 if (input_idx > 0) {
397 input_idx--;
400 while (input_idx > 0) {
401 unsigned char c = input[input_idx];
403 if ((c & 0xc0) == 0x80) {
404 input_idx--;
405 } else {
406 break;
411 /* Move input cursor right */
412 static void text_input_right(void)
414 size_t adv = 1;
415 unsigned char c = input[input_idx];
417 if ((c & 0x80) == 0x0) {
418 adv = 1;
419 } else if ((c & 0xe0) == 0xc0) {
420 adv = 2;
421 } else if ((c & 0xf0) == 0xe0) {
422 adv = 3;
423 } else if ((c & 0xf8) == 0xf0) {
424 adv = 4;
427 if (input_idx + adv <= input_len) {
428 input_idx += adv;
432 /* Backspace */
433 static void text_input_bs(void)
435 if (!input_idx) {
436 return;
439 size_t bs = 1;
441 while (input_idx > bs) {
442 unsigned char c = input[input_idx - bs];
444 if ((c & 0xc0) == 0x80) {
445 bs++;
446 } else {
447 break;
451 for (size_t k = input_idx - bs; k <= input_len - bs; k++) {
452 input[k] = input[k + bs];
455 input_len -= bs;
456 input_idx -= bs;
457 rerender_input_string = 1;
460 /* Convert `internal coordinates' to pixel coordinates */
461 static void internal_to_pixel_xy(int in_x, int in_y, int *out_x, int *out_y)
463 *out_x = in_x + offset_x;
464 *out_y = in_y + offset_y;
467 /* Convert pixel coordinates to `internal coordinates' */
468 static void pixel_to_internal_xy(int in_x, int in_y, int *out_x, int *out_y)
470 *out_x = in_x - offset_x;
471 *out_y = in_y - offset_y;
474 /* Set selected_vertex and selected_edge_{i,j} */
475 static int recalculate_selected_items(int mx, int my)
477 int x = 0;
478 int y = 0;
479 int ret = 0;
480 char *s = 0;
481 char *sij = 0;
482 char *sji = 0;
484 pixel_to_internal_xy(mx, my, &x, &y);
485 size_t last_vertex = selected_vertex;
486 size_t last_edge_i = selected_edge_i;
487 size_t last_edge_j = selected_edge_j;
489 selected_vertex = (size_t) -1;
490 selected_edge_i = (size_t) -1;
491 selected_edge_j = (size_t) -1;
493 for (size_t j = q->v_num; j > 0; --j) {
494 struct vertex *v = &(q->v[j - 1]);
495 int r = base_node_radius + v->fatness * node_radius_per_fatness;
497 if (x > v->x - r &&
498 x < v->x + r &&
499 y > v->y - r &&
500 y < v->y + r) {
501 selected_vertex = j - 1;
502 goto compute_str;
506 for (size_t j = 1; j < q->v_num; ++j) {
507 struct vertex *v1 = &(q->v[j]);
509 for (size_t i = 0; i < j; ++i) {
510 if (!q->e[i * q->v_len + j].p &&
511 !q->e[j * q->v_len + i].p) {
512 continue;
515 struct vertex *v2 = &(q->v[i]);
517 if ((x - edge_margin > v1->x &&
518 x - edge_margin > v2->x) ||
519 (x + edge_margin < v1->x &&
520 x + edge_margin < v2->x) ||
521 (y - edge_margin > v1->y &&
522 y - edge_margin > v2->y) ||
523 (y + edge_margin < v1->y &&
524 y + edge_margin < v2->y)) {
525 continue;
528 if (v1->x == v2->x) {
529 if (x + edge_margin > v1->x &&
530 x - edge_margin < v1->x) {
531 selected_edge_i = i;
532 selected_edge_j = j;
533 goto compute_str;
535 } else if (v1->y == v2->y) {
536 if (y + edge_margin > v1->y &&
537 y - edge_margin < v1->y) {
538 selected_edge_i = i;
539 selected_edge_j = j;
540 goto compute_str;
542 } else {
543 double m1 = ((double) (v2->y - v1->y)) /
544 ((double) (v2->x - v1->x));
545 double m2 = -1.0 / m1;
546 double xint = ((double) y - (double) v1->y +
547 m1 * v1->x - m2 * x) / (m1 - m2);
548 double yint = m1 * xint - m1 * v1->x +
549 (double) v1->y;
551 if ((x - xint) * (x - xint) + (y - yint) * (y -
552 yint)
553 < edge_margin * edge_margin) {
554 selected_edge_i = i;
555 selected_edge_j = j;
556 goto compute_str;
562 compute_str:
564 if (selected_vertex != (size_t) -1 &&
565 (selected_vertex != last_vertex ||
566 dragged_selected_vertex)) {
567 dragged_selected_vertex = 0;
568 struct vertex *v = &(q->v[selected_vertex]);
569 size_t len = snprintf(0, 0,
570 "Name: %s\nFatness: %d\nPosition: (%d,%d)",
571 v->name,
572 (int) v->fatness, v->x, v->y);
574 if (!(s = malloc(len + 1))) {
575 ret = errno;
576 perror(L("malloc"));
577 goto done;
580 sprintf(s, "Name: %s\nFatness: %d\nPosition: (%d,%d)",
581 v->name, (int) v->fatness, v->x, v->y);
583 if ((ret = render_text(s, &selected_info))) {
584 goto done;
586 } else if ((selected_edge_i != last_edge_i ||
587 selected_edge_j != last_edge_j) &&
588 selected_edge_i != (size_t) -1 &&
589 selected_edge_j != (size_t) -1) {
590 struct vertex *i = &(q->v[selected_edge_i]);
591 struct vertex *j = &(q->v[selected_edge_j]);
592 struct rational *eij = &(q->e[selected_edge_i * q->v_len +
593 selected_edge_j]);
594 struct rational *eji = &(q->e[selected_edge_j * q->v_len +
595 selected_edge_i]);
597 if ((ret = pretty_fraction(eij, &sij))) {
598 goto done;
601 if ((ret = pretty_fraction(eji, &sji))) {
602 goto done;
605 size_t len = snprintf(0, 0,
606 "%s \u2192 %s: %s\n%s \u2192 %s: %s",
607 i->name, j->name, sij,
608 j->name, i->name, sji);
610 if (!(s = malloc(len + 1))) {
611 ret = errno;
612 perror(L("malloc"));
613 goto done;
616 sprintf(s, "%s \u2192 %s: %s\n%s \u2192 %s: %s", i->name,
617 j->name, sij, j->name, i->name, sji);
619 if ((ret = render_text(s, &selected_info))) {
620 goto done;
624 done:
625 free(s);
626 free(sij);
627 free(sji);
629 return ret;
632 /* Render vertex names as textures */
633 static int render_vertex_names(void)
635 if (vertex_names) {
636 for (size_t j = 0; j < vertex_names_len; ++j) {
637 SDL_DestroyTexture(vertex_names[j]);
638 vertex_names[j] = 0;
642 if (!q->v_num) {
643 return 0;
646 if (!(vertex_names = realloc(vertex_names, q->v_num *
647 sizeof(*vertex_names)))) {
648 int sv_err = errno;
650 perror(L("realloc()"));
651 vertex_names_len = 0;
653 return sv_err;
656 vertex_names_len = q->v_num;
658 for (size_t j = 0; j < vertex_names_len; ++j) {
659 vertex_names[j] = 0;
662 for (size_t j = 0; j < vertex_names_len; ++j) {
663 int ret = 0;
665 if ((ret = render_text(q->v[j].name, &(vertex_names[j])))) {
666 return ret;
670 return 0;
673 /* Get information about the window */
674 static void react_to_window_resized(void)
676 int old_pix_width = da_pix_width;
677 int old_pix_height = da_pix_height;
679 SDL_GetWindowSize(sdl_win, &da_pix_width, &da_pix_height);
681 if (old_pix_width == da_pix_width &&
682 old_pix_height == da_pix_height) {
683 return;
686 offset_x += (da_pix_width - old_pix_width) / 2;
687 offset_y += (da_pix_height - old_pix_height) / 2;
690 /* Pop from queue */
691 static void eq_pop(struct ui_event *out)
693 if (eq_head == eq_tail) {
694 *out = (struct ui_event) { 0 };
696 return;
699 memcpy(out, eq_buf + eq_head, sizeof *out);
700 eq_buf[eq_head] = (struct ui_event) { 0 };
701 eq_head = (eq_head + 1) % eq_len;
704 /* Push into queue */
705 static int eq_push(struct ui_event *in)
707 if (((eq_tail + 1) % eq_len) == eq_head) {
708 void *newmem;
710 if ((eq_len * sizeof *in) >= ((size_t) -1) / 2) {
711 fprintf(stderr, L(
712 "eq_push: Impossibly large buffer\n"));
714 return ENOMEM;
717 if (!(newmem = realloc(eq_buf, (eq_len * 2) *
718 sizeof *eq_buf))) {
719 int sv_err = errno;
721 perror(L("realloc"));
723 return sv_err;
726 eq_buf = (struct ui_event *) newmem;
727 eq_len *= 2;
730 memcpy(eq_buf + eq_tail, in, sizeof *in);
731 eq_tail = (eq_tail + 1) % eq_len;
733 return 0;
736 /* Initialize SDL */
737 int ui_init(struct quiver *i_q)
739 int ret;
741 q = i_q;
742 size_t padding = strlen("Filename: ");
744 if (!(input_with_prefix = malloc(max_input_size + padding))) {
745 ret = errno;
746 perror(L("malloc"));
748 return ret;
751 strcpy(input_with_prefix, "Filename: ");
752 input = input_with_prefix + padding;
753 input_idx = 0;
754 input_len = 0;
756 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
757 fprintf(stderr, L("SDL_Init(): %s\n"), SDL_GetError());
759 return ENOMEDIUM;
762 sdl_win = SDL_CreateWindow("Cluster Algebra Visualizer",
763 SDL_WINDOWPOS_UNDEFINED,
764 SDL_WINDOWPOS_UNDEFINED, 1000, 1000,
765 SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
767 if (!sdl_win) {
768 fprintf(stderr, L("SDL_CreateWindow(): %s\n"), SDL_GetError());
770 return ENOMEDIUM;
773 sdl_renderer = SDL_CreateRenderer(sdl_win, -1,
774 SDL_RENDERER_ACCELERATED);
776 if (!sdl_renderer) {
777 fprintf(stderr, L("SDL_CreateRenderer(): %s\n"),
778 SDL_GetError());
780 return ENOMEDIUM;
783 if (TTF_Init() < 0) {
784 fprintf(stderr, L("TTF_Init(): %s\n"), TTF_GetError());
786 return ENOMEDIUM;
789 if ((ret = load_fonts())) {
790 goto done;
793 if ((ret = render_vertex_names())) {
794 goto done;
797 if ((ret = SDL_SetRenderDrawColor(sdl_renderer, color_bg.r, color_bg.g,
798 color_bg.b, color_bg.a))) {
799 fprintf(stderr, L("SDL_SetRenderDrawColor(): %s\n"),
800 SDL_GetError());
801 goto done;
804 if ((ret = SDL_RenderClear(sdl_renderer))) {
805 fprintf(stderr, L("SDL_RenderClear(): %s\n"), SDL_GetError());
806 goto done;
809 SDL_RenderPresent(sdl_renderer);
810 react_to_window_resized();
812 /* Set up queue for returning data */
813 if (!(eq_buf = calloc(2, sizeof *eq_buf))) {
814 ret = errno;
815 perror(L("malloc"));
816 goto done;
819 eq_len = 2;
820 eq_head = 0;
821 eq_tail = 0;
822 done:
824 return ret;
827 /* Deal with the fact that the quiver was changed */
828 int ui_respond_quiver_change(void)
830 return render_vertex_names();
833 /* Tear down SDL */
834 int ui_teardown(void)
836 if (vertex_names) {
837 for (size_t j = 0; j < vertex_names_len; ++j) {
838 SDL_DestroyTexture(vertex_names[j]);
839 vertex_names[j] = 0;
842 free(vertex_names);
843 vertex_names = 0;
846 for (size_t j = 0; j < UIA_LEN; ++j) {
847 SDL_DestroyTexture(bottom_text[j]);
848 bottom_text[j] = 0;
851 if (selected_info) {
852 SDL_DestroyTexture(selected_info);
853 selected_info = 0;
856 if (input_texture) {
857 SDL_DestroyTexture(input_texture);
858 input_texture = 0;
861 if (normal_font) {
862 TTF_CloseFont(normal_font);
863 normal_font = 0;
866 if (sdl_win) {
867 SDL_DestroyWindow(sdl_win);
868 sdl_win = 0;
871 free(eq_buf);
872 eq_buf = 0;
873 free(input_with_prefix);
874 input_with_prefix = 0;
875 input = 0;
876 SDL_Quit();
878 return 0;
881 /* Record that a frame has been started */
882 int ui_start_frame(void)
884 int ret = 0;
885 int rho = 0;
886 SDL_Rect r = { 0 };
887 Uint32 dummy_format;
888 int dummy_access;
889 int tex_w;
890 int tex_h;
892 frame_start_ticks = SDL_GetTicks();
894 if (!redraw) {
895 goto done;
898 redraw = 0;
900 /* Draw the damn thing */
901 SDL_SetRenderDrawColor(sdl_renderer, color_bg.r, color_bg.g, color_bg.b,
902 color_bg.a);
903 SDL_RenderClear(sdl_renderer);
905 /* Special case for if text input is going on */
906 if (rerender_input_string) {
907 rerender_input_string = 0;
909 if ((ret = render_text(input_with_prefix, &input_texture))) {
910 goto done;
914 /* Draw each edge */
915 for (size_t j = 0; j < q->v_num; ++j) {
916 for (size_t i = 0; i < j; ++i) {
917 /* First, determine if we're looking at a half-edge or a full-edge */
918 int d = gcd(q->v[i].fatness, q->v[j].fatness);
919 struct rational *eij = &(q->e[i * q->v_len + j]);
920 struct rational *eji = &(q->e[j * q->v_len + i]);
921 int cx = 0;
922 int cy = 0;
923 int cx2 = 0;
924 int cy2 = 0;
925 double theta = 0.0;
927 if (!eij->p &&
928 !eji->p) {
929 continue;
932 internal_to_pixel_xy(q->v[i].x, q->v[i].y, &cx, &cy);
933 internal_to_pixel_xy(q->v[j].x, q->v[j].y, &cx2, &cy2);
935 if (selected_edge_i == i &&
936 selected_edge_j == j) {
937 if ((ret = SDL_SetRenderDrawColor(sdl_renderer,
938 color_e_sel.r,
939 color_e_sel.g,
940 color_e_sel.b,
941 color_e_sel.a)))
943 fprintf(stderr, L(
944 "SDL_RenderDrawColor(): %s\n"),
945 SDL_GetError());
946 goto done;
949 for (int id = -edge_margin; id < edge_margin;
950 ++id) {
951 for (int jd = -edge_margin; jd <
952 edge_margin; ++jd) {
953 if ((ret = SDL_RenderDrawLine(
954 sdl_renderer, cx +
955 id, cy + jd,
956 cx2 + id, cy2 +
957 jd))) {
958 fprintf(stderr, L(
959 "SDL_RenderDrawLine(): %s\n"),
960 SDL_GetError());
961 goto done;
967 /* This is the (eij)/dj = -(eji)/di condition */
968 if (eij->p * q->v[i].fatness * eji->q != -eji->p *
969 q->v[j].fatness * eij->q) {
970 ret = SDL_SetRenderDrawColor(sdl_renderer,
971 color_e_abnormal.r,
972 color_e_abnormal.g,
973 color_e_abnormal.b,
974 color_e_abnormal.a);
975 } else if (abs(eij->p) * d == q->v[j].fatness *
976 eij->q) {
977 ret = SDL_SetRenderDrawColor(sdl_renderer,
978 color_e_normal.r,
979 color_e_normal.g,
980 color_e_normal.b,
981 color_e_normal.a);
982 } else if (2 * abs(eij->p) * d == q->v[j].fatness *
983 eij->q) {
984 ret = SDL_SetRenderDrawColor(sdl_renderer,
985 color_e_half.r,
986 color_e_half.g,
987 color_e_half.b,
988 color_e_half.a);
989 } else {
990 ret = SDL_SetRenderDrawColor(sdl_renderer,
991 color_e_abnormal.r,
992 color_e_abnormal.g,
993 color_e_abnormal.b,
994 color_e_abnormal.a);
997 if (ret) {
998 fprintf(stderr, L(
999 "SDL_SetRenderDrawColor(): %s\n"),
1000 SDL_GetError());
1001 goto done;
1004 if ((ret = SDL_RenderDrawLine(sdl_renderer, cx, cy, cx2,
1005 cy2))) {
1006 fprintf(stderr, L("SDL_RenderDrawLine(): %s\n"),
1007 SDL_GetError());
1008 goto done;
1011 if (cx == cx2) {
1012 theta = (cy2 > cy) ? M_PI / 2.0 : -M_PI / 2.0;
1013 } else {
1014 theta = atan2f(cy2 - cy, cx2 - cx);
1017 if ((eij->p < 0)) {
1018 theta += M_PI;
1021 cx = (cx + cx2) / 2;
1022 cy = (cy + cy2) / 2;
1023 cx2 = cx + arrow_length * cos(theta + arrow_angle);
1024 cy2 = cy + arrow_length * sin(theta + arrow_angle);
1026 if ((ret = SDL_RenderDrawLine(sdl_renderer, cx, cy, cx2,
1027 cy2))) {
1028 fprintf(stderr, L("SDL_RenderDrawLine(): %s\n"),
1029 SDL_GetError());
1030 goto done;
1033 if ((ret = SDL_RenderDrawLine(sdl_renderer, cx, cy, cx +
1034 arrow_length * cos(theta -
1035 arrow_angle),
1036 cy +
1037 arrow_length * sin(theta -
1038 arrow_angle))))
1040 fprintf(stderr, L("SDL_RenderDrawLine(): %s\n"),
1041 SDL_GetError());
1042 goto done;
1047 /* Draw each vertex as a box */
1048 for (size_t j = 0; j < q->v_num; ++j) {
1049 struct vertex *v = &(q->v[j]);
1050 int cx = 0;
1051 int cy = 0;
1053 internal_to_pixel_xy(v->x, v->y, &cx, &cy);
1055 /* Central square */
1056 SDL_SetRenderDrawBlendMode(sdl_renderer, SDL_BLENDMODE_NONE);
1057 SDL_SetRenderDrawColor(sdl_renderer, color_v.r, color_v.g,
1058 color_v.b, color_v.a);
1059 rho = base_node_radius + node_radius_per_fatness * v->fatness;
1060 r.x = cx - rho;
1061 r.y = cy - rho;
1062 r.w = 2 * rho;
1063 r.h = 2 * rho;
1064 SDL_RenderFillRect(sdl_renderer, &r);
1066 /* Outline */
1067 SDL_SetRenderDrawBlendMode(sdl_renderer, SDL_BLENDMODE_BLEND);
1069 if (j == selected_vertex ||
1070 j == last_clicked_vertex) {
1071 SDL_SetRenderDrawColor(sdl_renderer,
1072 color_outline_sel.r,
1073 color_outline_sel.g,
1074 color_outline_sel.b,
1075 color_outline_sel.a);
1076 } else {
1077 SDL_SetRenderDrawColor(sdl_renderer, color_outline.r,
1078 color_outline.g, color_outline.b,
1079 color_outline.a);
1082 r.x = cx - rho;
1083 r.y = cy - rho;
1084 r.w = 2 * rho - outline_width;
1085 r.h = outline_width;
1086 SDL_RenderFillRect(sdl_renderer, &r);
1087 r.x = cx + rho - outline_width;
1088 r.y = cy - rho;
1089 r.w = outline_width;
1090 r.h = 2 * rho - outline_width;
1091 SDL_RenderFillRect(sdl_renderer, &r);
1092 r.x = cx - rho + outline_width;
1093 r.y = cy + rho - outline_width;
1094 r.w = 2 * rho - outline_width;
1095 r.h = outline_width;
1096 SDL_RenderFillRect(sdl_renderer, &r);
1097 r.x = cx - rho;
1098 r.y = cy - rho + outline_width;
1099 r.w = outline_width;
1100 r.h = 2 * rho - outline_width;
1101 SDL_RenderFillRect(sdl_renderer, &r);
1103 /* Text */
1104 if (j >= vertex_names_len) {
1105 fprintf(stderr, L(
1106 "render_vertex_names() was not called, somehow\n"));
1107 ret = EINVAL;
1108 goto done;
1111 if (SDL_QueryTexture(vertex_names[j], &dummy_format,
1112 &dummy_access, &tex_w, &tex_h)) {
1113 fprintf(stderr, L("SDL_QueryTexture(): %s\n"),
1114 SDL_GetError());
1115 ret = ENOMEDIUM;
1116 goto done;
1119 r.x = cx - tex_w / 2;
1120 r.y = cy - tex_h / 2;
1121 r.w = tex_w;
1122 r.h = tex_h;
1123 SDL_RenderCopy(sdl_renderer, vertex_names[j], 0, &r);
1126 /* If adding a new vertex, draw preview */
1127 if (ui_action == UIA_NEW_VERTEX &&
1128 last_mouse_x != -1 &&
1129 last_mouse_y != -1) {
1130 /* Central square */
1131 SDL_SetRenderDrawBlendMode(sdl_renderer, SDL_BLENDMODE_NONE);
1132 SDL_SetRenderDrawColor(sdl_renderer, color_v_preview.r,
1133 color_v_preview.g, color_v_preview.b,
1134 color_v_preview.a);
1135 rho = base_node_radius;
1136 r.x = last_mouse_x - rho;
1137 r.y = last_mouse_y - rho;
1138 r.w = 2 * rho;
1139 r.h = 2 * rho;
1140 SDL_RenderFillRect(sdl_renderer, &r);
1142 /* Outline */
1143 SDL_SetRenderDrawBlendMode(sdl_renderer, SDL_BLENDMODE_BLEND);
1144 SDL_SetRenderDrawColor(sdl_renderer, color_outline_preview.r,
1145 color_outline_preview.g,
1146 color_outline_preview.b,
1147 color_outline_preview.a);
1148 r.x = last_mouse_x - rho;
1149 r.y = last_mouse_y - rho;
1150 r.w = 2 * rho - outline_width;
1151 r.h = outline_width;
1152 SDL_RenderFillRect(sdl_renderer, &r);
1153 r.x = last_mouse_x + rho - outline_width;
1154 r.y = last_mouse_y - rho;
1155 r.w = outline_width;
1156 r.h = 2 * rho - outline_width;
1157 SDL_RenderFillRect(sdl_renderer, &r);
1158 r.x = last_mouse_x - rho + outline_width;
1159 r.y = last_mouse_y + rho - outline_width;
1160 r.w = 2 * rho - outline_width;
1161 r.h = outline_width;
1162 SDL_RenderFillRect(sdl_renderer, &r);
1163 r.x = last_mouse_x - rho;
1164 r.y = last_mouse_y - rho + outline_width;
1165 r.w = outline_width;
1166 r.h = 2 * rho - outline_width;
1167 SDL_RenderFillRect(sdl_renderer, &r);
1170 /* If adding a new edge, draw possible */
1171 if ((ui_action == UIA_NEW_EDGE_2 ||
1172 ui_action == UIA_NEW_H_EDGE_2) &&
1174 /* last_clicked_vertex != (size_t) -1 && */
1175 last_mouse_x != -1 &&
1176 last_mouse_y != -1) {
1177 int cx = 0;
1178 int cy = 0;
1180 if ((ret = SDL_SetRenderDrawColor(sdl_renderer,
1181 color_e_normal.r,
1182 color_e_normal.g,
1183 color_e_normal.b,
1184 color_e_normal.a))) {
1185 fprintf(stderr, L("SDL_RenderDrawLine(): %s\n"),
1186 SDL_GetError());
1187 goto done;
1190 internal_to_pixel_xy(q->v[last_clicked_vertex].x,
1191 q->v[last_clicked_vertex].y, &cx, &cy);
1193 if ((ret = SDL_RenderDrawLine(sdl_renderer, cx, cy,
1194 last_mouse_x, last_mouse_y))) {
1195 fprintf(stderr, L("SDL_RenderDrawLine(): %s\n"),
1196 SDL_GetError());
1197 goto done;
1201 /* Bottom text */
1202 if (SDL_QueryTexture(bottom_text[ui_action], &dummy_format,
1203 &dummy_access, &tex_w, &tex_h)) {
1204 fprintf(stderr, L("SDL_QueryTexture(): %s\n"), SDL_GetError());
1205 ret = ENOMEDIUM;
1206 goto done;
1209 r.x = text_border_padding;
1210 r.y = da_pix_height - tex_h - text_border_padding;
1211 r.w = tex_w;
1212 r.h = tex_h;
1213 SDL_RenderCopy(sdl_renderer, bottom_text[ui_action], 0, &r);
1215 /* If something is selected */
1216 if (selected_info &&
1217 (selected_vertex != (size_t) -1 ||
1218 (selected_edge_i != (size_t) -1 &&
1219 selected_edge_j != (size_t) -1))) {
1220 if (SDL_QueryTexture(selected_info, &dummy_format,
1221 &dummy_access, &tex_w, &tex_h)) {
1222 fprintf(stderr, L("SDL_QueryTexture(): %s\n"),
1223 SDL_GetError());
1224 ret = ENOMEDIUM;
1225 goto done;
1228 r.x = text_border_padding;
1229 r.y = text_border_padding;
1230 r.w = tex_w;
1231 r.h = tex_h;
1232 SDL_RenderCopy(sdl_renderer, selected_info, 0, &r);
1235 /* If user is entering text */
1236 if (ui_action == UIA_ENTER_SAVE ||
1237 ui_action == UIA_ENTER_LOAD ||
1238 ui_action == UIA_ENTER_RENAME) {
1239 if (SDL_QueryTexture(bottom_text[ui_action], &dummy_format,
1240 &dummy_access, &tex_w, &tex_h)) {
1241 fprintf(stderr, L("SDL_QueryTexture(): %s\n"),
1242 SDL_GetError());
1243 ret = ENOMEDIUM;
1244 goto done;
1247 r.x = text_border_padding;
1248 r.y = da_pix_height - tex_h - text_border_padding;
1250 if (SDL_QueryTexture(input_texture, &dummy_format,
1251 &dummy_access, &tex_w, &tex_h)) {
1252 fprintf(stderr, L("SDL_QueryTexture(): %s\n"),
1253 SDL_GetError());
1254 ret = ENOMEDIUM;
1255 goto done;
1258 r.y -= (tex_h + text_border_padding);
1259 r.w = tex_w;
1260 r.h = tex_h;
1261 SDL_RenderCopy(sdl_renderer, input_texture, 0, &r);
1263 /* Now draw a cursor */
1264 char store_idxchar = input[input_idx];
1266 input[input_idx] = '\0';
1268 if ((ret = TTF_SizeUTF8(normal_font, input_with_prefix, &tex_w,
1269 &tex_h))) {
1270 fprintf(stderr, L("TTF_SizeText(): %s\n"),
1271 TTF_GetError());
1272 goto done;
1275 input[input_idx] = store_idxchar;
1277 if ((ret = SDL_SetRenderDrawColor(sdl_renderer, color_font.r,
1278 color_font.g, color_font.b,
1279 color_font.a))) {
1280 fprintf(stderr, L("SDL_SetRenderDrawColor(): %s\n"),
1281 SDL_GetError());
1282 goto done;
1285 if ((ret = SDL_RenderDrawLine(sdl_renderer, r.x + tex_w, r.y,
1286 r.x + tex_w, r.y + tex_h))) {
1287 fprintf(stderr, L("SDL_RenderDrawLine(): %s\n"),
1288 SDL_GetError());
1289 goto done;
1293 done:
1295 return ret;
1298 /* Draw a frame, possibly sleeping for framelimit */
1299 int ui_finish_frame(void)
1301 int ret = 0;
1302 struct ui_event ui_e = { 0 };
1303 SDL_Event sdl_e = { 0 };
1304 Uint32 now = 0;
1305 uint_fast8_t save_requested = 0;
1306 uint_fast8_t load_requested = 0;
1308 if (free_this) {
1309 free(free_this);
1310 free_this = 0;
1313 if (save_load_delay) {
1314 save_load_delay--;
1317 SDL_RenderPresent(sdl_renderer);
1319 /* Handle user input */
1320 while (SDL_PollEvent(&sdl_e) != 0) {
1321 redraw = 1;
1322 SDL_Keycode k = 0;
1324 switch (sdl_e.type) {
1325 case SDL_QUIT:
1326 ui_e = (struct ui_event) { .type = ET_QUIT };
1327 ret = eq_push(&ui_e);
1328 break;
1329 case SDL_TEXTINPUT:
1330 text_input(sdl_e.text.text);
1331 break;
1332 case SDL_KEYUP:
1334 if (sdl_e.key.repeat) {
1335 break;
1338 k = sdl_e.key.keysym.sym;
1340 switch (ui_action) {
1341 case UIA_ENTER_SAVE:
1342 case UIA_ENTER_LOAD:
1343 case UIA_ENTER_RENAME:
1345 if (k == SDLK_ESCAPE) {
1346 SDL_StopTextInput();
1347 ui_action = UIA_NONE;
1348 last_clicked_vertex = (size_t) -1;
1349 } else if (k == SDLK_RETURN ||
1350 k == SDLK_RETURN2) {
1351 SDL_StopTextInput();
1352 ui_e = (struct ui_event) { .str =
1353 input };
1355 if (ui_action == UIA_ENTER_SAVE) {
1356 ui_e.type = ET_SAVE;
1357 } else if (ui_action ==
1358 UIA_ENTER_LOAD) {
1359 ui_e.type = ET_LOAD;
1360 } else if (ui_action ==
1361 UIA_ENTER_RENAME) {
1362 ui_e.type = ET_RENAME;
1363 ui_e.idx_1 =
1364 last_clicked_vertex;
1367 ret = eq_push(&ui_e);
1368 ui_action = UIA_NONE;
1369 last_clicked_vertex = (size_t) -1;
1370 } else if (k == SDLK_LEFT) {
1371 text_input_left();
1372 } else if (k == SDLK_RIGHT) {
1373 text_input_right();
1374 } else if (k == SDLK_BACKSPACE) {
1375 text_input_bs();
1378 break;
1379 case UIA_NONE:
1381 if (k == SDLK_q) {
1382 ui_action = UIA_ASK_QUIT;
1383 } else if (k == SDLK_d) {
1384 ui_action = UIA_DELETE;
1385 } else if (k == SDLK_e) {
1386 ui_action = UIA_NEW_EDGE_1;
1387 } else if (k == SDLK_f) {
1388 ui_action = UIA_INC_FATNESS;
1389 } else if (k == SDLK_g) {
1390 ui_action = UIA_DEC_FATNESS;
1391 } else if (k == SDLK_h) {
1392 ui_action = UIA_NEW_H_EDGE_1;
1393 } else if (k == SDLK_m) {
1394 ui_action = UIA_MUTATE;
1395 } else if (k == SDLK_r) {
1396 ui_action = UIA_RENAME;
1397 } else if (k == SDLK_v) {
1398 ui_action = UIA_NEW_VERTEX;
1399 } else if (k == SDLK_l &&
1400 !save_load_delay) {
1401 /* Don't load - SDL_KEYUP repeats */
1402 load_requested = 1;
1403 } else if (k == SDLK_s &&
1404 !save_load_delay) {
1405 save_requested = 1;
1408 break;
1409 case UIA_ASK_QUIT:
1411 if (k == SDLK_n ||
1412 k == SDLK_ESCAPE) {
1413 ui_action = UIA_NONE;
1414 } else if (k == SDLK_y) {
1415 ui_e = (struct ui_event) { .type =
1416 ET_QUIT };
1417 ret = eq_push(&ui_e);
1418 ui_action = UIA_NONE;
1421 break;
1422 default:
1424 if (k == SDLK_q ||
1425 k == SDLK_ESCAPE) {
1426 ui_action = UIA_NONE;
1429 break;
1432 break;
1433 case SDL_WINDOWEVENT:
1435 if (sdl_e.window.event == SDL_WINDOWEVENT_RESIZED ||
1436 sdl_e.window.event == SDL_WINDOWEVENT_MAXIMIZED ||
1437 sdl_e.window.event == SDL_WINDOWEVENT_RESTORED) {
1438 react_to_window_resized();
1439 } else if (sdl_e.window.event ==
1440 SDL_WINDOWEVENT_LEAVE) {
1441 /* This tells the dragging code to not respond */
1442 last_mouse_x = -1;
1443 last_mouse_y = -1;
1446 break;
1447 case SDL_MOUSEMOTION:
1449 if (sdl_e.motion.state & SDL_BUTTON_LMASK) {
1450 int x = sdl_e.motion.x;
1451 int y = sdl_e.motion.y;
1453 if (last_mouse_x >= 0 &&
1454 last_mouse_y >= 0) {
1455 if (selected_vertex != (size_t) -1) {
1456 q->v[selected_vertex].x += (x -
1457 last_mouse_x);
1458 q->v[selected_vertex].y += (y -
1459 last_mouse_y);
1460 dragged_selected_vertex = 1;
1461 recalculate_selected_items(
1462 sdl_e.motion.x,
1463 sdl_e.motion.y);
1464 } else {
1465 offset_x += (x - last_mouse_x);
1466 offset_y += (y - last_mouse_y);
1469 } else {
1470 recalculate_selected_items(sdl_e.motion.x,
1471 sdl_e.motion.y);
1474 last_mouse_x = sdl_e.motion.x;
1475 last_mouse_y = sdl_e.motion.y;
1476 break;
1477 case SDL_MOUSEBUTTONUP:
1479 if ((sdl_e.button.state & SDL_BUTTON_LMASK) &&
1480 ui_action != UIA_NEW_VERTEX) {
1481 last_mouse_x = -1;
1482 last_mouse_y = -1;
1485 recalculate_selected_items(sdl_e.button.x,
1486 sdl_e.button.y);
1487 break;
1488 case SDL_MOUSEBUTTONDOWN:
1490 if (!(sdl_e.button.state & SDL_BUTTON_LMASK)) {
1491 break;
1494 if (ui_action != UIA_NEW_VERTEX) {
1495 last_mouse_x = -1;
1496 last_mouse_y = -1;
1499 switch (ui_action) {
1500 case UIA_MUTATE:
1502 if (selected_vertex == (size_t) -1) {
1503 break;
1506 ui_e = (struct ui_event) {
1507 /* */
1508 .type = ET_MUTATE, .idx_1 =
1509 selected_vertex
1511 ret = eq_push(&ui_e);
1512 ui_action = UIA_NONE;
1513 break;
1514 case UIA_NEW_VERTEX:
1516 if (selected_vertex != (size_t) -1 ||
1517 selected_edge_i != (size_t) -1 ||
1518 selected_edge_j != (size_t) -1) {
1519 break;
1522 int cx = sdl_e.button.x - offset_x;
1523 int cy = sdl_e.button.y - offset_y;
1525 ui_e = (struct ui_event) {
1526 /* */
1527 .type = ET_NEW_VERTEX, .int_1 = cx,
1528 .int_2 = cy
1530 ret = eq_push(&ui_e);
1531 ui_action = UIA_NONE;
1532 break;
1533 case UIA_NEW_EDGE_1:
1534 case UIA_NEW_H_EDGE_1:
1535 case UIA_RENAME:
1537 if (selected_vertex == (size_t) -1) {
1538 ui_action = UIA_NONE;
1539 break;
1542 last_clicked_vertex = selected_vertex;
1544 if (ui_action == UIA_NEW_EDGE_1) {
1545 ui_action = UIA_NEW_EDGE_2;
1546 } else if (ui_action == UIA_NEW_H_EDGE_1) {
1547 ui_action = UIA_NEW_H_EDGE_2;
1548 } else if (ui_action == UIA_RENAME) {
1549 ui_action = UIA_ENTER_RENAME;
1550 input_len = 0;
1551 input_idx = 0;
1552 input[0] = '\0';
1553 SDL_StartTextInput();
1555 /* Intentionally not copying null terminator */
1556 strncpy(input_with_prefix, "New name: ",
1557 10);
1558 rerender_input_string = 1;
1561 break;
1562 case UIA_NEW_EDGE_2:
1563 case UIA_NEW_H_EDGE_2:
1565 if (selected_vertex == (size_t) -1 ||
1566 selected_vertex == last_clicked_vertex) {
1567 ui_action = UIA_NONE;
1568 last_clicked_vertex = (size_t) -1;
1569 break;
1572 ui_e = (struct ui_event) {
1573 /* */
1574 .type = ET_NEW_EDGE, .idx_1 =
1575 last_clicked_vertex, .idx_2 =
1576 selected_vertex, .a = 1, .b =
1577 (ui_action == UIA_NEW_EDGE_2 ?
1578 1 : 2)
1580 ret = eq_push(&ui_e);
1581 ui_action = UIA_NONE;
1582 last_clicked_vertex = (size_t) -1;
1583 break;
1584 case UIA_DELETE:
1586 if (selected_vertex != (size_t) -1) {
1587 ui_e = (struct ui_event) {
1588 /* */
1589 .type = ET_DELETE_VERTEX,
1590 .idx_1 = selected_vertex
1592 ret = eq_push(&ui_e);
1593 } else if (selected_edge_i != (size_t) -1 &&
1594 selected_edge_j != (size_t) -1) {
1595 ui_e = (struct ui_event) {
1596 /* */
1597 .type = ET_DELETE_EDGE, .idx_1 =
1598 selected_edge_i,
1599 .idx_2 =
1600 selected_edge_j
1602 ret = eq_push(&ui_e);
1605 ui_action = UIA_NONE;
1606 break;
1607 case UIA_INC_FATNESS:
1608 case UIA_DEC_FATNESS:
1610 if (selected_vertex == (size_t) -1) {
1611 break;
1614 ui_e = (struct ui_event) {
1615 /* */
1616 .type = ET_CHANGE_FATNESS, .idx_1 =
1617 selected_vertex, .int_1 =
1618 (ui_action ==
1619 UIA_INC_FATNESS
1620 ? 1 : -1)
1622 ret = eq_push(&ui_e);
1623 ui_action = UIA_NONE;
1624 break;
1625 case UIA_NONE:
1626 case UIA_ASK_QUIT:
1627 case UIA_ENTER_SAVE:
1628 case UIA_ENTER_LOAD:
1629 case UIA_ENTER_RENAME:
1630 case UIA_LEN:
1631 break;
1634 break;
1637 if (ret) {
1638 goto done;
1642 if (load_requested ||
1643 save_requested) {
1644 save_load_delay = 30;
1645 char *f = 0;
1646 int r = 0;
1648 if (load_requested) {
1649 r = choose_load_file(&f);
1650 } else {
1651 r = choose_save_file(&f);
1654 if (!r) {
1655 if (f) {
1656 ui_e = (struct ui_event) { .str = f };
1657 ui_e.type = load_requested ? ET_LOAD : ET_SAVE;
1659 /* f is freed on next ui_finish_frame */
1660 free_this = f;
1661 ret = eq_push(&ui_e);
1663 } else {
1664 ui_action = load_requested ? UIA_ENTER_LOAD :
1665 UIA_ENTER_SAVE;
1666 input_idx = 0;
1667 input_len = 0;
1668 input[0] = '\0';
1669 SDL_StartTextInput();
1671 /* Intentionally not copying null terminator */
1672 strncpy(input_with_prefix, "Filename: ", 10);
1673 rerender_input_string = 1;
1677 /* framelimit */
1678 now = SDL_GetTicks();
1680 if (frame_start_ticks < now) {
1681 Uint32 elapsed_time = now - frame_start_ticks;
1683 if (elapsed_time < TICKS_PER_FRAME) {
1684 SDL_Delay(TICKS_PER_FRAME - elapsed_time);
1688 done:
1690 return ret;
1693 /* Return an event to the main loop */
1694 int ui_get_event(struct ui_event *e, uint_fast8_t *more)
1696 eq_pop(e);
1697 *more = eq_head != eq_tail;
1699 return 0;