2 * Copyright (c) 2016, 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
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.
31 #define TICKS_PER_FRAME (1000 / 60)
33 #define TRIG_PRECALC_NUM 4
35 /* What clicking does */
36 static enum ui_action
{
51 /* The window we'll be using */
52 static SDL_Window
*sdl_win
;
54 /* The renderer we'll be using */
55 static SDL_Renderer
*sdl_renderer
;
57 /* How to limit the framerate */
58 static Uint32 frame_start_ticks
;
60 /* The informative texture */
61 static SDL_Texture
*selected_info
;
63 /* Buffer for event queue */
64 static struct ui_event
*eq_buf
;
66 /* Current max length of event queue */
69 /* Current head of queue */
70 static size_t eq_head
;
72 /* Current tail of queue */
73 static size_t eq_tail
;
75 /* The quiver we'll be using */
76 static struct quiver
*q
;
78 /* Width of drawing area in pixels */
79 static int da_pix_width
;
81 /* Height of drawing area in pixels */
82 static int da_pix_height
;
84 /* The background color */
85 static SDL_Color color_bg
= { .r
= 0xe2, .g
= 0xe2, .b
= 0xe2, .a
= 0xff };
87 /* The normal vertex color */
88 static SDL_Color color_v
= { .r
= 0x82, .g
= 0x82, .b
= 0xb2, .a
= 0xff };
90 /* The vertex color for preview vertices */
91 static SDL_Color color_v_preview
= { .r
= 0x82, .g
= 0x82, .b
= 0xb2, .a
=
94 /* The normal vertex outline color */
95 static SDL_Color color_outline
= { .r
= 0x12, .g
= 0x12, .b
= 0x12, .a
= 0x80 };
97 /* The vertex outline color for preview vertices */
98 static SDL_Color color_outline_preview
= { .r
= 0x12, .g
= 0x12, .b
= 0x12, .a
=
101 /* The selected vertex outline color */
102 static SDL_Color color_outline_sel
= { .r
= 0x42, .g
= 0x42, .b
= 0xe2, .a
=
106 static SDL_Color color_font
= { .r
= 0x12, .g
= 0x12, .b
= 0x12, .a
= 0x80 };
108 /* The normal edge color */
109 static SDL_Color color_e_normal
= { .r
= 0x12, .g
= 0x12, .b
= 0x12, .a
=
112 /* The half edge color */
113 static SDL_Color color_e_half
= { .r
= 0x12, .g
= 0x12, .b
= 0x12, .a
= 0x60 };
115 /* The abnormal edge color */
116 static SDL_Color color_e_abnormal
= { .r
= 0xd0, .g
= 0x12, .b
= 0x12, .a
=
119 /* The selected edge color */
120 static SDL_Color color_e_sel
= { .r
= 0xb2, .g
= 0xb2, .b
= 0xe2, .a
= 0x40 };
122 /* The font for node names, instructions, etc */
123 static TTF_Font
*normal_font
;
125 /* The base font size */
126 static uint_fast8_t base_font_size
= 12;
128 /* How much space (pixels) between lower left of screen and bottom text */
129 static unsigned int text_border_padding
= 24;
131 /* Strings to display at bottom of screen */
132 static const char *bottom_string
[] = {
134 [UIA_NONE
] = "[m] Mutate\n[v] Create new vertex\n"
135 "[d] Delete vertex/edge\n[e] Add edge\n"
136 "[h] Add half edge\n[f] Increase vertex fatness\n"
137 "[g] Decrease vertex fatness\n[q] Quit", [UIA_NEW_VERTEX
] =
138 "[Mouse1] Create new vertex\n[ESC] Cancel", /* */
139 [UIA_DELETE
] = "[Mouse1] Delete vertex/edge\n[ESC] Cancel", /* */
140 [UIA_NEW_EDGE_1
] = "[Mouse1] Select start\n[ESC] Cancel", /* */
141 [UIA_NEW_EDGE_2
] = "[Mouse1] Select end\n[ESC] Cancel", /* */
142 [UIA_NEW_H_EDGE_1
] = "[Mouse1] Select start\n[ESC] Cancel", /* */
143 [UIA_NEW_H_EDGE_2
] = "[Mouse1] Select end\n[ESC] Cancel", /* */
144 [UIA_INC_FATNESS
] = "[Mouse1] Increase fatness\n[ESC] Cancel", /* */
145 [UIA_DEC_FATNESS
] = "[Mouse1] Decrease fatness\n[ESC] Cancel", /* */
146 [UIA_MUTATE
] = "[Mouse1] Mutate at vertex\n[ESC] Cancel", /* */
147 [UIA_ASK_QUIT
] = "Quit?\n\n[y] Confirm\n[n] Cancel", /* */
151 /* The texture containing what to show on the bottom */
152 static SDL_Texture
*bottom_text
[UIA_LEN
];
154 /* The textures containing the names of all vertices - indexed just like q */
155 static SDL_Texture
**vertex_names
;
157 /* The number of elements of vertex_names */
158 static size_t vertex_names_len
;
160 /* Drawing offset x */
163 /* Drawing offset x */
166 /* How wide (in pixels) a fatness 1 node should be */
167 static unsigned int base_node_radius
= 8;
169 /* How much (in pixels) a node should widen for each fatness level */
170 static unsigned int node_radius_per_fatness
= 7;
172 /* How wide (in pixels) the outline should be */
173 static int outline_width
= 2;
175 /* How wide (in pixels) the arrowheads should be */
176 static int arrow_length
= 7;
178 /* How narrow the arrowheads should be */
179 static double arrow_angle
= 6.5 * M_PI
/ 8.0;
181 /* How close to an arrow (in pixels) for it to be selected */
182 static int edge_margin
= 5;
184 /* If we're interacting with a vertex, which one */
185 static size_t selected_vertex
= (size_t) -1;
187 /* If we're interacting with an edge, the i */
188 static size_t selected_edge_i
= (size_t) -1;
190 /* If we're interacting with an edge, the j */
191 static size_t selected_edge_j
= (size_t) -1;
193 /* If we're adding an edge, the last vertex we clicked on */
194 static size_t last_clicked_vertex
= (size_t) -1;
196 /* x-coordinate of last mouse position */
197 static int last_mouse_x
= -1;
199 /* y-coordinate of last mouse position */
200 static int last_mouse_y
= -1;
203 static double precalc_sins
[TRIG_PRECALC_NUM
];
206 static double precalc_coss
[TRIG_PRECALC_NUM
];
208 /* Precalculate sines and cosines */
209 static void precalc_trig(void)
211 precalc_coss
[0] = 0.0;
212 precalc_sins
[0] = 1.0;
214 for (size_t j
= 1; j
< TRIG_PRECALC_NUM
- 1; ++j
) {
215 double theta
= (M_PI
* j
) / (2 * (TRIG_PRECALC_NUM
- 1));
217 precalc_sins
[j
] = sin(theta
);
218 precalc_coss
[j
] = cos(theta
);
221 precalc_coss
[TRIG_PRECALC_NUM
- 1] = 0.0;
222 precalc_sins
[TRIG_PRECALC_NUM
- 1] = 1.0;
226 static int gcd(uint_fast8_t x
, uint_fast8_t y
)
244 /* Allocate and print a rational in the way a user expects */
245 static int pretty_fraction(struct rational
*r
, char **out
)
250 size_t len
= snprintf(0, 0, "%d", (int) r
->p
);
252 if (!(*out
= malloc(len
+ 1))) {
258 sprintf(*out
, "%d", (int) r
->p
);
262 size_t len
= snprintf(0, 0, "%d/%u", (int) r
->p
, (unsigned int) r
->q
);
264 if (!(*out
= malloc(len
+ 1))) {
270 sprintf(*out
, "%d/%u", (int) r
->p
, (unsigned int) r
->q
);
276 /* Render text to texture */
277 static int render_text(const char *text
, SDL_Texture
**out
)
284 SDL_DestroyTexture(*out
);
288 SDL_Surface
*surf
= 0;
290 if (!(surf
= TTF_RenderUTF8_Blended_Wrapped(normal_font
, text
,
292 fprintf(stderr
, L("TTF_RenderUTF8_Shaded(): %s\n"),
298 if (!(*out
= SDL_CreateTextureFromSurface(sdl_renderer
, surf
))) {
299 fprintf(stderr
, L("SDL_CreateTextureFromSurface(): %s\n"),
306 SDL_FreeSurface(surf
);
312 static int load_fonts(void)
317 TTF_CloseFont(normal_font
);
321 if (!(normal_font
= TTF_OpenFont(FONT_PATH
, base_font_size
))) {
323 fprintf(stderr
, L("TTF_OpenFont(): %s\n"), TTF_GetError());
327 for (size_t j
= 0; j
< UIA_LEN
; ++j
) {
328 if ((ret
= render_text(bottom_string
[j
], &bottom_text
[j
]))) {
337 TTF_CloseFont(normal_font
);
346 /* Convert `internal coordinates' to pixel coordinates */
347 static void internal_to_pixel_xy(int in_x
, int in_y
, int *out_x
, int *out_y
)
349 *out_x
= in_x
+ offset_x
;
350 *out_y
= in_y
+ offset_y
;
353 /* Convert pixel coordinates to `internal coordinates' */
354 static void pixel_to_internal_xy(int in_x
, int in_y
, int *out_x
, int *out_y
)
356 *out_x
= in_x
- offset_x
;
357 *out_y
= in_y
- offset_y
;
360 /* Set selected_vertex and selected_edge_{i,j} */
361 static int recalculate_selected_items(int mx
, int my
)
370 pixel_to_internal_xy(mx
, my
, &x
, &y
);
371 size_t last_vertex
= selected_vertex
;
372 size_t last_edge_i
= selected_edge_i
;
373 size_t last_edge_j
= selected_edge_j
;
375 selected_vertex
= (size_t) -1;
376 selected_edge_i
= (size_t) -1;
377 selected_edge_j
= (size_t) -1;
379 for (size_t j
= q
->v_num
; j
> 0; --j
) {
380 struct vertex
*v
= &(q
->v
[j
- 1]);
381 int r
= base_node_radius
+ v
->fatness
* node_radius_per_fatness
;
387 selected_vertex
= j
- 1;
392 for (size_t j
= 1; j
< q
->v_num
; ++j
) {
393 struct vertex
*v1
= &(q
->v
[j
]);
395 for (size_t i
= 0; i
< j
; ++i
) {
396 if (!q
->e
[i
* q
->v_len
+ j
].p
&&
397 !q
->e
[j
* q
->v_len
+ i
].p
) {
401 struct vertex
*v2
= &(q
->v
[i
]);
403 if ((x
- edge_margin
> v1
->x
&&
404 x
- edge_margin
> v2
->x
) ||
405 (x
+ edge_margin
< v1
->x
&&
406 x
+ edge_margin
< v2
->x
) ||
407 (y
- edge_margin
> v1
->y
&&
408 y
- edge_margin
> v2
->y
) ||
409 (y
+ edge_margin
< v1
->y
&&
410 y
+ edge_margin
< v2
->y
)) {
414 if (v1
->x
== v2
->x
) {
415 if (x
+ edge_margin
> v1
->x
&&
416 x
- edge_margin
< v1
->x
) {
421 } else if (v1
->y
== v2
->y
) {
422 if (y
+ edge_margin
> v1
->y
&&
423 y
- edge_margin
< v1
->y
) {
429 double m1
= ((double) (v2
->y
- v1
->y
)) /
430 ((double) (v2
->x
- v1
->x
));
431 double m2
= -1.0 / m1
;
432 double xint
= ((double) y
- (double) v1
->y
+
433 m1
* v1
->x
- m2
* x
) / (m1
- m2
);
434 double yint
= m1
* xint
- m1
* v1
->x
+
437 if ((x
- xint
) * (x
- xint
) + (y
- yint
) * (y
-
439 < edge_margin
* edge_margin
) {
450 if (selected_vertex
!= last_vertex
&&
451 selected_vertex
!= (size_t) -1) {
452 struct vertex
*v
= &(q
->v
[selected_vertex
]);
453 size_t len
= snprintf(0, 0,
454 "Name: %s\nFatness: %d\nPosition: (%d,%d)",
456 (int) v
->fatness
, v
->x
, v
->y
);
458 if (!(s
= malloc(len
+ 1))) {
464 sprintf(s
, "Name: %s\nFatness: %d\nPosition: (%d,%d)",
465 v
->name
, (int) v
->fatness
, v
->x
, v
->y
);
467 if ((ret
= render_text(s
, &selected_info
))) {
470 } else if ((selected_edge_i
!= last_edge_i
||
471 selected_edge_j
!= last_edge_j
) &&
472 selected_edge_i
!= (size_t) -1 &&
473 selected_edge_j
!= (size_t) -1) {
474 struct vertex
*i
= &(q
->v
[selected_edge_i
]);
475 struct vertex
*j
= &(q
->v
[selected_edge_j
]);
476 struct rational
*eij
= &(q
->e
[selected_edge_i
* q
->v_len
+
478 struct rational
*eji
= &(q
->e
[selected_edge_j
* q
->v_len
+
481 if ((ret
= pretty_fraction(eij
, &sij
))) {
485 if ((ret
= pretty_fraction(eji
, &sji
))) {
489 size_t len
= snprintf(0, 0,
490 "%s \u2192 %s: %s\n%s \u2192 %s: %s",
491 i
->name
, j
->name
, sij
,
492 j
->name
, i
->name
, sji
);
494 if (!(s
= malloc(len
+ 1))) {
500 sprintf(s
, "%s \u2192 %s: %s\n%s \u2192 %s: %s", i
->name
,
501 j
->name
, sij
, j
->name
, i
->name
, sji
);
503 if ((ret
= render_text(s
, &selected_info
))) {
516 /* Render vertex names as textures */
517 static int render_vertex_names(void)
520 for (size_t j
= 0; j
< vertex_names_len
; ++j
) {
521 SDL_DestroyTexture(vertex_names
[j
]);
526 if (!(vertex_names
= realloc(vertex_names
, q
->v_num
*
527 sizeof(*vertex_names
)))) {
530 perror(L("realloc()"));
531 vertex_names_len
= 0;
536 vertex_names_len
= q
->v_num
;
538 for (size_t j
= 0; j
< vertex_names_len
; ++j
) {
542 for (size_t j
= 0; j
< vertex_names_len
; ++j
) {
545 if ((ret
= render_text(q
->v
[j
].name
, &(vertex_names
[j
])))) {
553 /* Get information about the window */
554 static void react_to_window_resized(void)
556 int old_pix_width
= da_pix_width
;
557 int old_pix_height
= da_pix_height
;
559 SDL_GetWindowSize(sdl_win
, &da_pix_width
, &da_pix_height
);
561 if (old_pix_width
== da_pix_width
&&
562 old_pix_height
== da_pix_height
) {
566 offset_x
+= (da_pix_width
- old_pix_width
) / 2;
567 offset_y
+= (da_pix_height
- old_pix_height
) / 2;
571 static void eq_pop(struct ui_event
*out
)
573 if (eq_head
== eq_tail
) {
574 *out
= (struct ui_event
) { 0 };
579 memcpy(out
, eq_buf
+ eq_head
, sizeof *out
);
580 eq_buf
[eq_head
] = (struct ui_event
) { 0 };
581 eq_head
= (eq_head
+ 1) % eq_len
;
584 /* Push into queue */
585 static int eq_push(struct ui_event
*in
)
587 if (((eq_tail
+ 1) % eq_len
) == eq_head
) {
590 if ((eq_len
* sizeof *in
) >= ((size_t) -1) / 2) {
592 "eq_push: Impossibly large buffer\n"));
597 if (!(newmem
= realloc(eq_buf
, (eq_len
* 2) *
601 perror(L("realloc"));
606 eq_buf
= (struct ui_event
*) newmem
;
610 memcpy(eq_buf
+ eq_tail
, in
, sizeof *in
);
611 eq_tail
= (eq_tail
+ 1) % eq_len
;
617 int ui_init(struct quiver
*i_q
)
623 if (SDL_Init(SDL_INIT_VIDEO
) < 0) {
624 fprintf(stderr
, L("SDL_Init(): %s\n"), SDL_GetError());
629 sdl_win
= SDL_CreateWindow("Cluster Algebra Visualizer",
630 SDL_WINDOWPOS_UNDEFINED
,
631 SDL_WINDOWPOS_UNDEFINED
, 1000, 1000,
632 SDL_WINDOW_SHOWN
| SDL_WINDOW_RESIZABLE
);
635 fprintf(stderr
, L("SDL_CreateWindow(): %s\n"), SDL_GetError());
640 sdl_renderer
= SDL_CreateRenderer(sdl_win
, -1,
641 SDL_RENDERER_ACCELERATED
);
644 fprintf(stderr
, L("SDL_CreateRenderer(): %s\n"),
650 if (TTF_Init() < 0) {
651 fprintf(stderr
, L("TTF_Init(): %s\n"), TTF_GetError());
656 if ((ret
= load_fonts())) {
660 if ((ret
= render_vertex_names())) {
664 if ((ret
= SDL_SetRenderDrawColor(sdl_renderer
, color_bg
.r
, color_bg
.g
,
665 color_bg
.b
, color_bg
.a
))) {
666 fprintf(stderr
, L("SDL_SetRenderDrawColor(): %s\n"),
671 if ((ret
= SDL_RenderClear(sdl_renderer
))) {
672 fprintf(stderr
, L("SDL_RenderClear(): %s\n"), SDL_GetError());
676 SDL_RenderPresent(sdl_renderer
);
677 react_to_window_resized();
679 /* Set up queue for returning data */
680 if (!(eq_buf
= calloc(2, sizeof *eq_buf
))) {
690 /* Sines and cosines for drawing circles */
697 /* Deal with the fact that the quiver was changed */
698 int ui_respond_quiver_change(void)
700 return render_vertex_names();
704 int ui_teardown(void)
707 for (size_t j
= 0; j
< vertex_names_len
; ++j
) {
708 SDL_DestroyTexture(vertex_names
[j
]);
713 for (size_t j
= 0; j
< UIA_LEN
; ++j
) {
714 SDL_DestroyTexture(bottom_text
[j
]);
719 SDL_DestroyTexture(selected_info
);
724 TTF_CloseFont(normal_font
);
729 SDL_DestroyWindow(sdl_win
);
737 /* Record that a frame has been started */
738 int ui_start_frame(void)
748 frame_start_ticks
= SDL_GetTicks();
750 /* Draw the damn thing */
751 SDL_SetRenderDrawColor(sdl_renderer
, color_bg
.r
, color_bg
.g
, color_bg
.b
,
753 SDL_RenderClear(sdl_renderer
);
756 for (size_t j
= 0; j
< q
->v_num
; ++j
) {
757 for (size_t i
= 0; i
< j
; ++i
) {
758 /* First, determine if we're looking at a half-edge or a full-edge */
759 int d
= gcd(q
->v
[i
].fatness
, q
->v
[j
].fatness
);
760 struct rational
*eij
= &(q
->e
[i
* q
->v_len
+ j
]);
761 struct rational
*eji
= &(q
->e
[j
* q
->v_len
+ i
]);
773 internal_to_pixel_xy(q
->v
[i
].x
, q
->v
[i
].y
, &cx
, &cy
);
774 internal_to_pixel_xy(q
->v
[j
].x
, q
->v
[j
].y
, &cx2
, &cy2
);
776 if (selected_edge_i
== i
&&
777 selected_edge_j
== j
) {
778 if ((ret
= SDL_SetRenderDrawColor(sdl_renderer
,
785 "SDL_RenderDrawColor(): %s\n"),
790 for (int id
= -edge_margin
; id
< edge_margin
;
792 for (int jd
= -edge_margin
; jd
<
794 if ((ret
= SDL_RenderDrawLine(
800 "SDL_RenderDrawLine(): %s\n"),
808 /* This is the (eij)/dj = -(eji)/di condition */
809 if (eij
->p
* q
->v
[i
].fatness
* eji
->q
!= -eji
->p
*
810 q
->v
[j
].fatness
* eij
->q
) {
811 ret
= SDL_SetRenderDrawColor(sdl_renderer
,
816 } else if (abs(eij
->p
) * d
== q
->v
[j
].fatness
*
818 ret
= SDL_SetRenderDrawColor(sdl_renderer
,
823 } else if (2 * abs(eij
->p
) * d
== q
->v
[j
].fatness
*
825 ret
= SDL_SetRenderDrawColor(sdl_renderer
,
831 ret
= SDL_SetRenderDrawColor(sdl_renderer
,
840 "SDL_SetRenderDrawColor(): %s\n"),
845 if ((ret
= SDL_RenderDrawLine(sdl_renderer
, cx
, cy
, cx2
,
847 fprintf(stderr
, L("SDL_RenderDrawLine(): %s\n"),
853 theta
= (cy2
> cy
) ? M_PI
/ 2.0 : -M_PI
/ 2.0;
855 theta
= atan2f(cy2
- cy
, cx2
- cx
);
864 cx2
= cx
+ arrow_length
* cos(theta
+ arrow_angle
);
865 cy2
= cy
+ arrow_length
* sin(theta
+ arrow_angle
);
867 if ((ret
= SDL_RenderDrawLine(sdl_renderer
, cx
, cy
, cx2
,
869 fprintf(stderr
, L("SDL_RenderDrawLine(): %s\n"),
874 if ((ret
= SDL_RenderDrawLine(sdl_renderer
, cx
, cy
, cx
+
875 arrow_length
* cos(theta
-
878 arrow_length
* sin(theta
-
881 fprintf(stderr
, L("SDL_RenderDrawLine(): %s\n"),
888 /* Draw each vertex as a box */
889 for (size_t j
= 0; j
< q
->v_num
; ++j
) {
890 struct vertex
*v
= &(q
->v
[j
]);
894 internal_to_pixel_xy(v
->x
, v
->y
, &cx
, &cy
);
897 SDL_SetRenderDrawBlendMode(sdl_renderer
, SDL_BLENDMODE_NONE
);
898 SDL_SetRenderDrawColor(sdl_renderer
, color_v
.r
, color_v
.g
,
899 color_v
.b
, color_v
.a
);
900 rho
= base_node_radius
+ node_radius_per_fatness
* v
->fatness
;
905 SDL_RenderFillRect(sdl_renderer
, &r
);
908 SDL_SetRenderDrawBlendMode(sdl_renderer
, SDL_BLENDMODE_BLEND
);
910 if (j
== selected_vertex
) {
911 SDL_SetRenderDrawColor(sdl_renderer
,
915 color_outline_sel
.a
);
917 SDL_SetRenderDrawColor(sdl_renderer
, color_outline
.r
,
918 color_outline
.g
, color_outline
.b
,
924 r
.w
= 2 * rho
- outline_width
;
926 SDL_RenderFillRect(sdl_renderer
, &r
);
927 r
.x
= cx
+ rho
- outline_width
;
930 r
.h
= 2 * rho
- outline_width
;
931 SDL_RenderFillRect(sdl_renderer
, &r
);
932 r
.x
= cx
- rho
+ outline_width
;
933 r
.y
= cy
+ rho
- outline_width
;
934 r
.w
= 2 * rho
- outline_width
;
936 SDL_RenderFillRect(sdl_renderer
, &r
);
938 r
.y
= cy
- rho
+ outline_width
;
940 r
.h
= 2 * rho
- outline_width
;
941 SDL_RenderFillRect(sdl_renderer
, &r
);
944 if (j
>= vertex_names_len
) {
946 "render_vertex_names() was not called, somehow\n"));
951 if (SDL_QueryTexture(vertex_names
[j
], &dummy_format
,
952 &dummy_access
, &tex_w
, &tex_h
)) {
953 fprintf(stderr
, L("SDL_QueryTexture(): %s\n"),
959 r
.x
= cx
- tex_w
/ 2;
960 r
.y
= cy
- tex_h
/ 2;
963 SDL_RenderCopy(sdl_renderer
, vertex_names
[j
], 0, &r
);
966 /* If adding a new vertex, draw preview */
967 if (ui_action
== UIA_NEW_VERTEX
&&
968 last_mouse_x
!= -1 &&
969 last_mouse_y
!= -1) {
971 SDL_SetRenderDrawBlendMode(sdl_renderer
, SDL_BLENDMODE_NONE
);
972 SDL_SetRenderDrawColor(sdl_renderer
, color_v_preview
.r
,
973 color_v_preview
.g
, color_v_preview
.b
,
975 rho
= base_node_radius
;
976 r
.x
= last_mouse_x
- rho
;
977 r
.y
= last_mouse_y
- rho
;
980 SDL_RenderFillRect(sdl_renderer
, &r
);
983 SDL_SetRenderDrawBlendMode(sdl_renderer
, SDL_BLENDMODE_BLEND
);
984 SDL_SetRenderDrawColor(sdl_renderer
, color_outline_preview
.r
,
985 color_outline_preview
.g
,
986 color_outline_preview
.b
,
987 color_outline_preview
.a
);
988 r
.x
= last_mouse_x
- rho
;
989 r
.y
= last_mouse_y
- rho
;
990 r
.w
= 2 * rho
- outline_width
;
992 SDL_RenderFillRect(sdl_renderer
, &r
);
993 r
.x
= last_mouse_x
+ rho
- outline_width
;
994 r
.y
= last_mouse_y
- rho
;
996 r
.h
= 2 * rho
- outline_width
;
997 SDL_RenderFillRect(sdl_renderer
, &r
);
998 r
.x
= last_mouse_x
- rho
+ outline_width
;
999 r
.y
= last_mouse_y
+ rho
- outline_width
;
1000 r
.w
= 2 * rho
- outline_width
;
1001 r
.h
= outline_width
;
1002 SDL_RenderFillRect(sdl_renderer
, &r
);
1003 r
.x
= last_mouse_x
- rho
;
1004 r
.y
= last_mouse_y
- rho
+ outline_width
;
1005 r
.w
= outline_width
;
1006 r
.h
= 2 * rho
- outline_width
;
1007 SDL_RenderFillRect(sdl_renderer
, &r
);
1010 /* If adding a new edge, draw possible */
1011 if ((ui_action
== UIA_NEW_EDGE_2
||
1012 ui_action
== UIA_NEW_H_EDGE_2
) &&
1014 /* last_clicked_vertex != (size_t) -1 && */
1015 last_mouse_x
!= -1 &&
1016 last_mouse_y
!= -1) {
1020 ret
= SDL_SetRenderDrawColor(sdl_renderer
, color_e_normal
.r
,
1021 color_e_normal
.g
, color_e_normal
.b
,
1023 internal_to_pixel_xy(q
->v
[last_clicked_vertex
].x
,
1024 q
->v
[last_clicked_vertex
].y
, &cx
, &cy
);
1026 if ((ret
= SDL_RenderDrawLine(sdl_renderer
, cx
, cy
,
1027 last_mouse_x
, last_mouse_y
))) {
1028 fprintf(stderr
, L("SDL_RenderDrawLine(): %s\n"),
1035 if (SDL_QueryTexture(bottom_text
[ui_action
], &dummy_format
,
1036 &dummy_access
, &tex_w
, &tex_h
)) {
1037 fprintf(stderr
, L("SDL_QueryTexture(): %s\n"), SDL_GetError());
1042 r
.x
= text_border_padding
;
1043 r
.y
= da_pix_height
- tex_h
- text_border_padding
;
1046 SDL_RenderCopy(sdl_renderer
, bottom_text
[ui_action
], 0, &r
);
1048 /* If something is selected */
1049 if (selected_info
&&
1050 (selected_vertex
!= (size_t) -1 ||
1051 (selected_edge_i
!= (size_t) -1 &&
1052 selected_edge_j
!= (size_t) -1))) {
1053 if (SDL_QueryTexture(selected_info
, &dummy_format
,
1054 &dummy_access
, &tex_w
, &tex_h
)) {
1055 fprintf(stderr
, L("SDL_QueryTexture(): %s\n"),
1061 r
.x
= text_border_padding
;
1062 r
.y
= text_border_padding
;
1065 SDL_RenderCopy(sdl_renderer
, selected_info
, 0, &r
);
1073 /* Draw a frame, possibly sleeping for framelimit */
1074 int ui_finish_frame(void)
1077 struct ui_event ui_e
= { 0 };
1078 SDL_Event sdl_e
= { 0 };
1081 SDL_RenderPresent(sdl_renderer
);
1083 /* Handle user input */
1084 while (SDL_PollEvent(&sdl_e
) != 0) {
1087 switch (sdl_e
.type
) {
1089 ui_e
= (struct ui_event
) { .type
= ET_QUIT
};
1090 ret
= eq_push(&ui_e
);
1093 k
= sdl_e
.key
.keysym
.sym
;
1095 switch (ui_action
) {
1099 ui_action
= UIA_ASK_QUIT
;
1100 } else if (k
== SDLK_m
) {
1101 ui_action
= UIA_MUTATE
;
1102 } else if (k
== SDLK_v
) {
1103 ui_action
= UIA_NEW_VERTEX
;
1104 } else if (k
== SDLK_d
) {
1105 ui_action
= UIA_DELETE
;
1106 } else if (k
== SDLK_e
) {
1107 ui_action
= UIA_NEW_EDGE_1
;
1108 } else if (k
== SDLK_h
) {
1109 ui_action
= UIA_NEW_H_EDGE_1
;
1110 } else if (k
== SDLK_f
) {
1111 ui_action
= UIA_INC_FATNESS
;
1112 } else if (k
== SDLK_g
) {
1113 ui_action
= UIA_DEC_FATNESS
;
1121 ui_action
= UIA_NONE
;
1122 } else if (k
== SDLK_y
) {
1123 ui_e
= (struct ui_event
) { .type
=
1125 ret
= eq_push(&ui_e
);
1126 ui_action
= UIA_NONE
;
1134 ui_action
= UIA_NONE
;
1141 case SDL_WINDOWEVENT
:
1143 if (sdl_e
.window
.event
== SDL_WINDOWEVENT_RESIZED
||
1144 sdl_e
.window
.event
== SDL_WINDOWEVENT_MAXIMIZED
||
1145 sdl_e
.window
.event
== SDL_WINDOWEVENT_RESTORED
) {
1146 react_to_window_resized();
1147 } else if (sdl_e
.window
.event
==
1148 SDL_WINDOWEVENT_LEAVE
) {
1149 /* This tells the dragging code to not respond */
1155 case SDL_MOUSEMOTION
:
1157 if (sdl_e
.motion
.state
& SDL_BUTTON_LMASK
) {
1158 int x
= sdl_e
.motion
.x
;
1159 int y
= sdl_e
.motion
.y
;
1161 if (last_mouse_x
>= 0 &&
1162 last_mouse_y
>= 0) {
1163 if (selected_vertex
!= (size_t) -1) {
1164 q
->v
[selected_vertex
].x
+= (x
-
1166 q
->v
[selected_vertex
].y
+= (y
-
1169 offset_x
+= (x
- last_mouse_x
);
1170 offset_y
+= (y
- last_mouse_y
);
1174 recalculate_selected_items(sdl_e
.motion
.x
,
1178 last_mouse_x
= sdl_e
.motion
.x
;
1179 last_mouse_y
= sdl_e
.motion
.y
;
1181 case SDL_MOUSEBUTTONUP
:
1183 if ((sdl_e
.button
.state
& SDL_BUTTON_LMASK
) &&
1184 ui_action
!= UIA_NEW_VERTEX
) {
1189 recalculate_selected_items(sdl_e
.button
.x
,
1192 case SDL_MOUSEBUTTONDOWN
:
1194 if (!(sdl_e
.button
.state
& SDL_BUTTON_LMASK
)) {
1198 if (ui_action
!= UIA_NEW_VERTEX
) {
1203 switch (ui_action
) {
1206 if (selected_vertex
== (size_t) -1) {
1210 ui_e
= (struct ui_event
) {
1212 .type
= ET_MUTATE
, .idx_1
=
1215 ret
= eq_push(&ui_e
);
1216 ui_action
= UIA_NONE
;
1218 case UIA_NEW_VERTEX
:
1220 if (selected_vertex
!= (size_t) -1 ||
1221 selected_edge_i
!= (size_t) -1 ||
1222 selected_edge_j
!= (size_t) -1) {
1226 int cx
= sdl_e
.button
.x
- offset_x
;
1227 int cy
= sdl_e
.button
.y
- offset_y
;
1229 ui_e
= (struct ui_event
) {
1231 .type
= ET_NEW_VERTEX
, .int_1
= cx
,
1234 ret
= eq_push(&ui_e
);
1235 ui_action
= UIA_NONE
;
1237 case UIA_NEW_EDGE_1
:
1238 case UIA_NEW_H_EDGE_1
:
1240 if (selected_vertex
== (size_t) -1) {
1241 ui_action
= UIA_NONE
;
1245 last_clicked_vertex
= selected_vertex
;
1246 ui_action
= (ui_action
== UIA_NEW_EDGE_1
?
1247 UIA_NEW_EDGE_2
: UIA_NEW_H_EDGE_2
);
1249 case UIA_NEW_EDGE_2
:
1250 case UIA_NEW_H_EDGE_2
:
1252 if (selected_vertex
== (size_t) -1 ||
1253 selected_vertex
== last_clicked_vertex
) {
1254 ui_action
= UIA_NONE
;
1258 ui_e
= (struct ui_event
) {
1260 .type
= ET_NEW_EDGE
, .idx_1
=
1261 last_clicked_vertex
, .idx_2
=
1262 selected_vertex
, .a
= 1, .b
=
1263 (ui_action
== UIA_NEW_EDGE_2
?
1266 ret
= eq_push(&ui_e
);
1267 ui_action
= UIA_NONE
;
1271 if (selected_vertex
!= (size_t) -1) {
1272 ui_e
= (struct ui_event
) {
1274 .type
= ET_DELETE_VERTEX
,
1275 .idx_1
= selected_vertex
1277 ret
= eq_push(&ui_e
);
1278 } else if (selected_edge_i
!= (size_t) -1 &&
1279 selected_edge_j
!= (size_t) -1) {
1280 ui_e
= (struct ui_event
) {
1282 .type
= ET_DELETE_EDGE
, .idx_1
=
1287 ret
= eq_push(&ui_e
);
1290 ui_action
= UIA_NONE
;
1292 case UIA_INC_FATNESS
:
1293 case UIA_DEC_FATNESS
:
1295 if (selected_vertex
== (size_t) -1) {
1299 ui_e
= (struct ui_event
) {
1301 .type
= ET_CHANGE_FATNESS
, .idx_1
=
1302 selected_vertex
, .int_1
=
1307 ret
= eq_push(&ui_e
);
1308 ui_action
= UIA_NONE
;
1325 now
= SDL_GetTicks();
1327 if (frame_start_ticks
< now
) {
1328 Uint32 elapsed_time
= now
- frame_start_ticks
;
1330 if (elapsed_time
< TICKS_PER_FRAME
) {
1331 SDL_Delay(TICKS_PER_FRAME
- elapsed_time
);
1340 /* Return an event to the main loop */
1341 int ui_get_event(struct ui_event
*e
, uint_fast8_t *more
)
1344 *more
= eq_head
!= eq_tail
;