6 #include "game/camera.h"
7 #include "system/line_stream.h"
8 #include "system/log.h"
10 #include "system/nth_alloc.h"
11 #include "system/stacktrace.h"
12 #include "system/str.h"
13 #include "ui/edit_field.h"
14 #include "./point_layer.h"
15 #include "math/extrema.h"
16 #include "math/mat3x3.h"
17 #include "./color_picker.h"
18 #include "undo_history.h"
20 #define POINT_LAYER_ELEMENT_RADIUS 10.0f
21 #define POINT_LAYER_ID_TEXT_SIZE vec(2.0f, 2.0f)
22 #define POINT_LAYER_ID_TEXT_COLOR COLOR_BLACK
24 static int point_clipboard = 0;
25 static Color point_clipboard_color;
27 // TODO(#1140): PointLayer does not support snapping
39 PointLayerState state;
40 Dynarray/*<Vec2f>*/ positions;
41 Dynarray/*<Color>*/ colors;
42 Dynarray/*<char[ID_MAX_SIZE]>*/ ids;
44 ColorPicker color_picker;
48 Edit_field *edit_field;
51 const char *id_name_prefix;
72 PointUndoContext create_point_undo_swap_context(PointLayer *point_layer,
73 size_t index, size_t index2)
75 trace_assert(point_layer);
76 trace_assert(index < point_layer->positions.count);
77 trace_assert(index2 < point_layer->positions.count);
79 PointUndoContext undo_context;
80 undo_context.type = POINT_UNDO_SWAP;
81 undo_context.layer = point_layer;
82 undo_context.index = index;
83 undo_context.index2 = index2;
88 PointUndoContext create_point_undo_context(PointLayer *point_layer,
91 trace_assert(type != POINT_UNDO_SWAP);
93 (void) create_point_undo_swap_context;
95 PointUndoContext undo_context;
98 type == POINT_UNDO_ADD
99 ? point_layer->positions.count - 1
100 : (size_t) point_layer->selection;
102 undo_context.type = type;
103 undo_context.layer = point_layer;
104 dynarray_copy_to(&point_layer->positions, &undo_context.position, index);
105 dynarray_copy_to(&point_layer->colors, &undo_context.color, index);
106 dynarray_copy_to(&point_layer->ids, &undo_context.id, index);
107 undo_context.index = index;
113 void point_layer_undo(void *context, size_t context_size)
115 trace_assert(context);
116 trace_assert(sizeof(PointUndoContext) == context_size);
118 PointUndoContext *undo_context = context;
119 PointLayer *point_layer = undo_context->layer;
121 switch (undo_context->type) {
122 case POINT_UNDO_ADD: {
123 dynarray_pop(&point_layer->positions, NULL);
124 dynarray_pop(&point_layer->colors, NULL);
125 dynarray_pop(&point_layer->ids, NULL);
126 point_layer->selection = -1;
129 case POINT_UNDO_DELETE: {
130 dynarray_insert_before(&point_layer->positions, undo_context->index, &undo_context->position);
131 dynarray_insert_before(&point_layer->colors, undo_context->index, &undo_context->color);
132 dynarray_insert_before(&point_layer->ids, undo_context->index, &undo_context->id);
133 point_layer->selection = -1;
136 case POINT_UNDO_UPDATE: {
137 dynarray_replace_at(&point_layer->positions, undo_context->index, &undo_context->position);
138 dynarray_replace_at(&point_layer->colors, undo_context->index, &undo_context->color);
139 dynarray_replace_at(&point_layer->ids, undo_context->index, &undo_context->id);
142 case POINT_UNDO_SWAP: {
143 dynarray_swap(&point_layer->positions, undo_context->index, undo_context->index2);
144 dynarray_swap(&point_layer->colors, undo_context->index, undo_context->index2);
145 dynarray_swap(&point_layer->ids, undo_context->index, undo_context->index2);
150 #define POINT_UNDO_PUSH(HISTORY, CONTEXT) \
152 PointUndoContext context = (CONTEXT); \
160 LayerPtr point_layer_as_layer(PointLayer *point_layer)
169 PointLayer *create_point_layer(const char *id_name_prefix)
171 Lt *lt = create_lt();
173 PointLayer *point_layer = PUSH_LT(lt, nth_calloc(1, sizeof(PointLayer)), free);
174 if (point_layer == NULL) {
177 point_layer->lt = lt;
179 point_layer->state = POINT_LAYER_IDLE;
181 point_layer->positions = create_dynarray(sizeof(Vec2f));
182 point_layer->colors = create_dynarray(sizeof(Color));
183 point_layer->ids = create_dynarray(sizeof(char) * ID_MAX_SIZE);
185 point_layer->edit_field = PUSH_LT(
188 POINT_LAYER_ID_TEXT_SIZE,
189 POINT_LAYER_ID_TEXT_COLOR),
191 if (point_layer->edit_field == NULL) {
195 point_layer->id_name_prefix = id_name_prefix;
200 PointLayer *create_point_layer_from_line_stream(LineStream *line_stream,
201 const char *id_name_prefix)
203 trace_assert(line_stream);
205 PointLayer *point_layer = create_point_layer(id_name_prefix);
209 line_stream_next(line_stream),
212 log_fail("Could not read amount of points");
213 RETURN_LT(point_layer->lt, NULL);
217 char id[ID_MAX_SIZE];
219 for (size_t i = 0; i < count; ++i) {
221 line_stream_next(line_stream),
222 "%"STRINGIFY(ID_MAX_SIZE)"s%f%f%6s",
223 id, &x, &y, color_name) < 0) {
224 log_fail("Could not read %dth goal\n", i);
225 RETURN_LT(point_layer->lt, NULL);
227 const Color color = hexstr(color_name);
228 const Vec2f point = vec(x, y);
230 dynarray_push(&point_layer->colors, &color);
231 dynarray_push(&point_layer->positions, &point);
232 dynarray_push(&point_layer->ids, id);
235 point_layer->selection = -1;
237 point_layer->color_picker = create_color_picker_from_rgba(COLOR_RED);
242 void destroy_point_layer(PointLayer *point_layer)
244 trace_assert(point_layer);
246 free(point_layer->positions.data);
247 free(point_layer->colors.data);
248 free(point_layer->ids.data);
250 RETURN_LT0(point_layer->lt);
254 Triangle element_shape(Vec2f position, float scale)
256 return triangle_mat3x3_product(
257 equilateral_triangle(),
259 trans_mat_vec(position),
263 int point_layer_render(const PointLayer *point_layer,
264 const Camera *camera,
267 trace_assert(point_layer);
268 trace_assert(camera);
270 const int n = (int)point_layer->positions.count;
271 Vec2f *positions = (Vec2f *)point_layer->positions.data;
272 Color *colors = (Color *)point_layer->colors.data;
273 char *ids = (char *)point_layer->ids.data;
275 for (int i = 0; i < n; ++i) {
276 const Color color = color_scale(
277 point_layer->state == POINT_LAYER_RECOLOR && i == point_layer->selection
278 ? point_layer->inter_color
280 rgba(1.0f, 1.0f, 1.0f, active ? 1.0f : 0.5f));
282 const Vec2f position =
283 point_layer->state == POINT_LAYER_MOVE && i == point_layer->selection
284 ? point_layer->inter_position
288 if (active && i == point_layer->selection) {
289 if (camera_fill_triangle(
293 POINT_LAYER_ELEMENT_RADIUS + 5.0f),
294 color_invert(color)) < 0) {
298 if (point_layer->state != POINT_LAYER_EDIT_ID &&
301 ids + ID_MAX_SIZE * i,
302 POINT_LAYER_ID_TEXT_SIZE,
303 POINT_LAYER_ID_TEXT_COLOR,
309 if (camera_fill_triangle(
313 POINT_LAYER_ELEMENT_RADIUS),
319 if (point_layer->state == POINT_LAYER_EDIT_ID) {
320 if (edit_field_render_world(
321 point_layer->edit_field,
323 positions[point_layer->selection]) < 0) {
328 if (active && color_picker_render(&point_layer->color_picker, camera) < 0) {
337 int point_layer_element_at(const PointLayer *point_layer,
340 trace_assert(point_layer);
342 int n = (int) point_layer->positions.count;
343 Vec2f *positions = (Vec2f *)point_layer->positions.data;
345 for (int i = n - 1; i >= 0; --i) {
346 if (vec_length(vec_sub(positions[i], position)) < POINT_LAYER_ELEMENT_RADIUS) {
355 int point_layer_add_element(PointLayer *point_layer,
358 UndoHistory *undo_history)
360 trace_assert(point_layer);
361 trace_assert(undo_history);
363 char id[ID_MAX_SIZE];
364 snprintf(id, ID_MAX_SIZE, "%s_%d",
365 point_layer->id_name_prefix,
366 point_layer->id_name_counter++);
368 dynarray_push(&point_layer->positions, &position);
369 dynarray_push(&point_layer->colors, &color);
370 dynarray_push(&point_layer->ids, id);
374 create_point_undo_context(point_layer, POINT_UNDO_ADD));
380 void point_layer_swap_elements(PointLayer *point_layer,
382 UndoHistory *undo_history)
384 trace_assert(point_layer);
385 trace_assert(undo_history);
386 trace_assert(a < point_layer->positions.count);
387 trace_assert(b < point_layer->positions.count);
389 dynarray_swap(&point_layer->positions, a, b);
390 dynarray_swap(&point_layer->colors, a, b);
391 dynarray_swap(&point_layer->ids, a, b);
395 create_point_undo_swap_context(point_layer, a, b));
399 void point_layer_delete_nth_element(PointLayer *point_layer,
401 UndoHistory *undo_history)
403 trace_assert(point_layer);
407 create_point_undo_context(
411 dynarray_delete_at(&point_layer->positions, i);
412 dynarray_delete_at(&point_layer->colors, i);
413 dynarray_delete_at(&point_layer->ids, i);
417 int point_layer_idle_event(PointLayer *point_layer,
418 const SDL_Event *event,
419 const Camera *camera,
420 UndoHistory *undo_history)
422 trace_assert(point_layer);
424 trace_assert(camera);
427 if (color_picker_event(
428 &point_layer->color_picker,
436 if (point_layer->selection >= 0) {
437 point_layer->inter_color = color_picker_rgba(&point_layer->color_picker);
438 point_layer->state = POINT_LAYER_RECOLOR;
443 switch (event->type) {
444 case SDL_MOUSEBUTTONDOWN: {
445 switch (event->button.button) {
446 case SDL_BUTTON_LEFT: {
447 const Vec2f position = camera_map_screen(camera, event->button.x, event->button.y);
449 point_layer->selection = point_layer_element_at(
450 point_layer, position);
452 if (point_layer->selection < 0) {
453 point_layer_add_element(
456 color_picker_rgba(&point_layer->color_picker),
459 Color *colors = (Color*)point_layer->colors.data;
460 Vec2f *positions = (Vec2f*)point_layer->positions.data;
462 point_layer->state = POINT_LAYER_MOVE;
463 point_layer->color_picker =
464 create_color_picker_from_rgba(colors[point_layer->selection]);
465 point_layer->inter_position = positions[point_layer->selection];
472 switch (event->key.keysym.sym) {
474 if ((event->key.keysym.mod & KMOD_SHIFT)
475 && (point_layer->selection >= 0)
476 && ((size_t)(point_layer->selection + 1) < point_layer->positions.count)) {
477 point_layer_swap_elements(
479 (size_t) point_layer->selection,
480 (size_t) point_layer->selection + 1,
482 point_layer->selection++;
487 if ((event->key.keysym.mod & KMOD_SHIFT)
488 && (point_layer->selection > 0)
489 && ((size_t) point_layer->selection < point_layer->positions.count)) {
490 point_layer_swap_elements(
492 (size_t) point_layer->selection,
493 (size_t) point_layer->selection - 1,
495 point_layer->selection--;
500 if (0 <= point_layer->selection && point_layer->selection < (int) point_layer->positions.count) {
501 point_layer_delete_nth_element(
503 (size_t)point_layer->selection,
505 point_layer->selection = -1;
510 if (point_layer->selection >= 0) {
511 char *ids = (char*)point_layer->ids.data;
512 point_layer->state = POINT_LAYER_EDIT_ID;
514 point_layer->edit_field,
515 ids + ID_MAX_SIZE * point_layer->selection);
516 SDL_StartTextInput();
521 if ((event->key.keysym.mod & KMOD_LCTRL) && point_layer->selection >= 0) {
523 dynarray_copy_to(&point_layer->colors, &point_clipboard_color, (size_t)point_layer->selection);
528 if ((event->key.keysym.mod & KMOD_LCTRL) && point_clipboard) {
530 SDL_GetMouseState(&x, &y);
531 Vec2f position = camera_map_screen(camera, x, y);
533 point_layer_add_element(
536 point_clipboard_color,
548 int point_layer_edit_id_event(PointLayer *point_layer,
549 const SDL_Event *event,
550 const Camera *camera,
551 UndoHistory *undo_history)
553 trace_assert(point_layer);
555 trace_assert(camera);
557 switch (event->type) {
559 switch(event->key.keysym.sym) {
563 create_point_undo_context(
567 char *id = dynarray_pointer_at(&point_layer->ids, (size_t) point_layer->selection);
568 const char *text = edit_field_as_text(point_layer->edit_field);
569 size_t n = min_size_t(strlen(text), ID_MAX_SIZE - 1);
571 memset(id + n, 0, ID_MAX_SIZE - n);
573 point_layer->state = POINT_LAYER_IDLE;
579 point_layer->state = POINT_LAYER_IDLE;
587 return edit_field_event(point_layer->edit_field, event);
591 int point_layer_move_event(PointLayer *point_layer,
592 const SDL_Event *event,
593 const Camera *camera,
594 UndoHistory *undo_history)
596 trace_assert(point_layer);
598 trace_assert(camera);
599 trace_assert(point_layer->selection >= 0);
601 Vec2f *positions = (Vec2f*)point_layer->positions.data;
603 switch (event->type) {
604 case SDL_MOUSEBUTTONUP: {
605 switch (event->button.button) {
606 case SDL_BUTTON_LEFT: {
607 point_layer->state = POINT_LAYER_IDLE;
609 const float distance = vec_length(
610 vec_sub(point_layer->inter_position,
611 positions[point_layer->selection]));
613 if (distance > 1e-6) {
616 create_point_undo_context(
621 &point_layer->positions,
622 (size_t) point_layer->selection,
623 &point_layer->inter_position);
629 case SDL_MOUSEMOTION: {
630 const Uint8 *state = SDL_GetKeyboardState(NULL);
631 const Vec2f mouse_pos = camera_map_screen(camera, event->motion.x, event->motion.y);
632 const Vec2f point_pos = positions[point_layer->selection];
634 if (!(state[SDL_SCANCODE_LCTRL] || state[SDL_SCANCODE_RCTRL])) {
635 point_layer->inter_position = mouse_pos;
637 const float dx = fabsf(point_pos.x - mouse_pos.x);
638 const float dy = fabsf(point_pos.y - mouse_pos.y);
641 point_layer->inter_position = vec(mouse_pos.x, point_pos.y);
643 point_layer->inter_position = vec(point_pos.x, mouse_pos.y);
653 int point_layer_recolor_event(PointLayer *point_layer,
654 const SDL_Event *event,
655 const Camera *camera,
656 UndoHistory *undo_history)
658 trace_assert(point_layer);
660 trace_assert(camera);
661 trace_assert(undo_history);
662 trace_assert(point_layer->selection >= 0);
665 if (color_picker_event(
666 &point_layer->color_picker,
674 point_layer->inter_color = color_picker_rgba(&point_layer->color_picker);
676 if (!color_picker_drag(&point_layer->color_picker)) {
679 create_point_undo_context(
684 &point_layer->colors,
685 (size_t) point_layer->selection,
686 &point_layer->inter_color);
688 point_layer->state = POINT_LAYER_IDLE;
696 int point_layer_event(PointLayer *point_layer,
697 const SDL_Event *event,
698 const Camera *camera,
699 UndoHistory *undo_history)
701 trace_assert(point_layer);
703 trace_assert(camera);
704 trace_assert(undo_history);
706 switch (point_layer->state) {
707 case POINT_LAYER_IDLE:
708 return point_layer_idle_event(point_layer, event, camera, undo_history);
710 case POINT_LAYER_EDIT_ID:
711 return point_layer_edit_id_event(point_layer, event, camera, undo_history);
713 case POINT_LAYER_MOVE:
714 return point_layer_move_event(point_layer, event, camera, undo_history);
716 case POINT_LAYER_RECOLOR:
717 return point_layer_recolor_event(point_layer, event, camera, undo_history);
723 size_t point_layer_count(const PointLayer *point_layer)
725 trace_assert(point_layer);
726 return point_layer->positions.count;
729 const Vec2f *point_layer_positions(const PointLayer *point_layer)
731 trace_assert(point_layer);
732 return (const Vec2f *)point_layer->positions.data;
735 const Color *point_layer_colors(const PointLayer *point_layer)
737 trace_assert(point_layer);
738 return (const Color *)point_layer->colors.data;
741 const char *point_layer_ids(const PointLayer *point_layer)
743 trace_assert(point_layer);
744 return (const char *)point_layer->ids.data;
747 int point_layer_dump_stream(const PointLayer *point_layer,
750 trace_assert(point_layer);
751 trace_assert(filedump);
753 size_t n = point_layer->ids.count;
754 char *ids = (char *) point_layer->ids.data;
755 Vec2f *positions = (Vec2f *) point_layer->positions.data;
756 Color *colors = (Color *) point_layer->colors.data;
758 fprintf(filedump, "%zd\n", n);
759 for (size_t i = 0; i < n; ++i) {
760 fprintf(filedump, "%s %f %f ",
761 ids + ID_MAX_SIZE * i,
762 positions[i].x, positions[i].y);
763 color_hex_to_stream(colors[i], filedump);
764 fprintf(filedump, "\n");