1 #include "game/camera.h"
3 #include "system/stacktrace.h"
4 #include "system/nth_alloc.h"
5 #include "system/log.h"
8 #include "rect_layer.h"
10 #include "system/line_stream.h"
11 #include "color_picker.h"
12 #include "system/str.h"
13 #include "ui/edit_field.h"
14 #include "undo_history.h"
16 #define RECT_LAYER_SELECTION_THICCNESS 10.0f
17 #define RECT_LAYER_ID_LABEL_SIZE vec(3.0f, 3.0f)
18 #define CREATE_AREA_THRESHOLD 10.0
20 // TODO: RectLayer does not support copy-pasting
25 // TODO(#955): Rectangles in Level Editor have only one resize anchor to work with
38 ColorPicker color_picker;
43 Edit_field *id_edit_field;
44 // TODO(#1043): RectLayer should use intermediate values instead of previous ones
60 char id[RECT_LAYER_ID_MAX_SIZE];
65 UndoContext create_undo_context(RectLayer *rect_layer, UndoType type)
67 trace_assert(rect_layer);
69 size_t index = type == UNDO_ADD ? dynarray_count(rect_layer->rects) - 1 : (size_t) rect_layer->selection;
71 UndoContext undo_context;
72 undo_context.type = type;
73 undo_context.layer = rect_layer;
74 dynarray_copy_to(rect_layer->rects, &undo_context.rect, index);
75 dynarray_copy_to(rect_layer->colors, &undo_context.color, index);
76 dynarray_copy_to(rect_layer->ids, undo_context.id, index);
77 undo_context.index = index;
83 void rect_layer_undo(void *context, size_t context_size)
85 trace_assert(context);
86 trace_assert(sizeof(UndoContext) == context_size);
88 UndoContext *undo_context = context;
89 RectLayer *rect_layer = undo_context->layer;
91 switch (undo_context->type) {
93 dynarray_delete_at(rect_layer->rects, undo_context->index);
94 dynarray_delete_at(rect_layer->colors, undo_context->index);
95 dynarray_delete_at(rect_layer->ids, undo_context->index);
96 rect_layer->selection = -1;
100 dynarray_insert_before(rect_layer->rects, undo_context->index, &undo_context->rect);
101 dynarray_insert_before(rect_layer->colors, undo_context->index, &undo_context->color);
102 dynarray_insert_before(rect_layer->ids, undo_context->index, &undo_context->id);
103 rect_layer->selection = -1;
107 dynarray_replace_at(rect_layer->rects, undo_context->index, &undo_context->rect);
108 dynarray_replace_at(rect_layer->colors, undo_context->index, &undo_context->color);
109 dynarray_replace_at(rect_layer->ids, undo_context->index, &undo_context->id);
114 #define UNDO_PUSH(LAYER, HISTORY, UNDO_TYPE) \
116 UndoContext context = create_undo_context(LAYER, UNDO_TYPE); \
125 static int rect_layer_add_rect(RectLayer *layer,
128 UndoHistory *undo_history)
132 if (dynarray_push(layer->rects, &rect) < 0) {
136 if (dynarray_push(layer->colors, &color) < 0) {
140 char id[RECT_LAYER_ID_MAX_SIZE];
141 for (size_t i = 0; i < RECT_LAYER_ID_MAX_SIZE - 1; ++i) {
142 id[i] = (char) ('a' + rand() % ('z' - 'a' + 1));
144 id[RECT_LAYER_ID_MAX_SIZE - 1] = '\0';
146 if (dynarray_push(layer->ids, id)) {
150 UNDO_PUSH(layer, undo_history, UNDO_ADD);
155 // TODO(#956): rect_layer_rect_at doesn't return rectangles according to some z-order
156 static int rect_layer_rect_at(RectLayer *layer, Vec position)
160 const size_t n = dynarray_count(layer->rects);
161 Rect *rects = dynarray_data(layer->rects);
163 for (size_t i = 0; i < n; ++i) {
164 if (rect_contains_point(rects[i], position)) {
172 static Rect rect_layer_resize_anchor(const Camera *camera, Rect boundary_rect)
174 const Rect overlay_rect =
176 camera_rect(camera, boundary_rect),
177 RECT_LAYER_SELECTION_THICCNESS * 0.5f);
180 overlay_rect.x + overlay_rect.w,
181 overlay_rect.y + overlay_rect.h,
182 RECT_LAYER_SELECTION_THICCNESS * 2.0f,
183 RECT_LAYER_SELECTION_THICCNESS * 2.0f);
186 static int rect_layer_delete_rect_at(RectLayer *layer,
188 UndoHistory *undo_history)
192 UNDO_PUSH(layer, undo_history, UNDO_DELETE);
194 dynarray_delete_at(layer->rects, i);
195 dynarray_delete_at(layer->colors, i);
196 dynarray_delete_at(layer->ids, i);
201 static int rect_layer_event_idle(RectLayer *layer,
202 const SDL_Event *event,
203 const Camera *camera,
204 UndoHistory *undo_history)
208 trace_assert(camera);
210 int color_changed = 0;
211 if (color_picker_event(&layer->color_picker, event, camera, &color_changed) < 0) {
216 if (layer->selection >= 0) {
217 dynarray_copy_to(layer->colors, &layer->inter_color, (size_t)layer->selection);
218 layer->state = RECT_LAYER_RECOLOR;
223 switch (event->type) {
224 case SDL_MOUSEBUTTONDOWN: {
225 switch (event->button.button) {
226 case SDL_BUTTON_LEFT: {
227 Point position = camera_map_screen(
231 int rect_at_position =
232 rect_layer_rect_at(layer, position);
234 Rect *rects = dynarray_data(layer->rects);
235 Color *colors = dynarray_data(layer->colors);
237 if (rect_at_position >= 0) {
238 layer->selection = rect_at_position;
239 layer->state = RECT_LAYER_MOVE;
244 rects[layer->selection].x,
245 rects[layer->selection].y));
246 layer->color_picker =
247 create_color_picker_from_rgba(colors[rect_at_position]);
249 dynarray_copy_to(layer->rects, &layer->inter_rect, (size_t) rect_at_position);
250 } else if (layer->selection >= 0 && rect_contains_point(
251 rect_layer_resize_anchor(
253 rects[layer->selection]),
255 (float) event->button.x,
256 (float) event->button.y))) {
257 layer->state = RECT_LAYER_RESIZE;
258 dynarray_copy_to(layer->rects, &layer->inter_rect, (size_t) layer->selection);
260 layer->selection = rect_at_position;
262 if (layer->selection < 0) {
263 layer->state = RECT_LAYER_CREATE;
264 layer->create_begin = position;
265 layer->create_end = position;
273 switch (event->key.keysym.sym) {
275 if (layer->selection >= 0) {
276 rect_layer_delete_rect_at(layer, (size_t) layer->selection, undo_history);
277 layer->selection = -1;
282 if (layer->selection >= 0) {
283 const char *ids = dynarray_data(layer->ids);
284 Color *colors = dynarray_data(layer->colors);
287 layer->id_edit_field,
288 RECT_LAYER_ID_LABEL_SIZE,
289 color_invert(colors[layer->selection]));
291 layer->state = RECT_LAYER_ID_RENAME;
293 layer->id_edit_field,
294 ids + layer->selection * RECT_LAYER_ID_MAX_SIZE);
295 SDL_StartTextInput();
305 static int rect_layer_event_create(RectLayer *layer,
306 const SDL_Event *event,
307 const Camera *camera,
308 UndoHistory *undo_history)
312 trace_assert(camera);
314 switch (event->type) {
315 case SDL_MOUSEBUTTONUP: {
316 switch (event->button.button) {
317 case SDL_BUTTON_LEFT: {
318 const Rect real_rect =
322 const float area = real_rect.w * real_rect.h;
324 if (area >= CREATE_AREA_THRESHOLD) {
328 color_picker_rgba(&layer->color_picker),
331 log_info("The area is too small %f. Such small box won't be created.\n", area);
333 layer->state = RECT_LAYER_IDLE;
338 case SDL_MOUSEMOTION: {
339 layer->create_end = camera_map_screen(
348 static int rect_layer_event_resize(RectLayer *layer,
349 const SDL_Event *event,
350 const Camera *camera,
351 UndoHistory *undo_history)
355 trace_assert(camera);
356 trace_assert(layer->selection >= 0);
358 switch (event->type) {
359 case SDL_MOUSEMOTION: {
360 layer->inter_rect = rect_from_points(
361 vec(layer->inter_rect.x, layer->inter_rect.y),
367 vec(RECT_LAYER_SELECTION_THICCNESS * -0.5f,
368 RECT_LAYER_SELECTION_THICCNESS * -0.5f)));
371 case SDL_MOUSEBUTTONUP: {
372 layer->state = RECT_LAYER_IDLE;
373 UNDO_PUSH(layer, undo_history, UNDO_UPDATE);
374 dynarray_replace_at(layer->rects, (size_t) layer->selection, &layer->inter_rect);
381 static int rect_layer_event_move(RectLayer *layer,
382 const SDL_Event *event,
383 const Camera *camera,
384 UndoHistory *undo_history)
388 trace_assert(camera);
389 trace_assert(layer->selection >= 0);
391 switch (event->type) {
392 case SDL_MOUSEMOTION: {
393 Point position = vec_sub(
400 trace_assert(layer->selection >= 0);
402 layer->inter_rect.x = position.x;
403 layer->inter_rect.y = position.y;
406 case SDL_MOUSEBUTTONUP: {
407 layer->state = RECT_LAYER_IDLE;
408 UNDO_PUSH(layer, undo_history, UNDO_UPDATE);
409 dynarray_replace_at(layer->rects, (size_t) layer->selection, &layer->inter_rect);
415 static int rect_layer_event_id_rename(RectLayer *layer,
416 const SDL_Event *event,
417 const Camera *camera,
418 UndoHistory *undo_history)
422 trace_assert(camera);
423 trace_assert(layer->selection >= 0);
425 switch (event->type) {
427 switch (event->key.keysym.sym) {
429 UNDO_PUSH(layer, undo_history, UNDO_UPDATE);
431 char *id = dynarray_pointer_at(layer->ids, (size_t)layer->selection);
432 memset(id, 0, RECT_LAYER_ID_MAX_SIZE);
433 memcpy(id, edit_field_as_text(layer->id_edit_field), RECT_LAYER_ID_MAX_SIZE - 1);
434 layer->state = RECT_LAYER_IDLE;
439 layer->state = RECT_LAYER_IDLE;
446 return edit_field_event(layer->id_edit_field, event);
449 LayerPtr rect_layer_as_layer(RectLayer *rect_layer)
458 RectLayer *create_rect_layer(void)
460 Lt *lt = create_lt();
462 RectLayer *layer = PUSH_LT(lt, nth_calloc(1, sizeof(RectLayer)), free);
468 layer->ids = PUSH_LT(
470 create_dynarray(sizeof(char) * RECT_LAYER_ID_MAX_SIZE),
472 if (layer->ids == NULL) {
476 layer->rects = PUSH_LT(
478 create_dynarray(sizeof(Rect)),
480 if (layer->rects == NULL) {
484 layer->colors = PUSH_LT(
486 create_dynarray(sizeof(Color)),
488 if (layer->colors == NULL) {
492 layer->id_edit_field = PUSH_LT(
495 RECT_LAYER_ID_LABEL_SIZE,
498 if (layer->id_edit_field == NULL) {
502 layer->color_picker = create_color_picker_from_rgba(rgba(1.0f, 0.0f, 0.0f, 1.0f));
503 layer->selection = -1;
508 RectLayer *create_rect_layer_from_line_stream(LineStream *line_stream)
510 trace_assert(line_stream);
512 RectLayer *layer = create_rect_layer();
517 const char *line = line_stream_next(line_stream);
519 RETURN_LT(layer->lt, NULL);
523 if (sscanf(line, "%zu", &count) < 0) {
524 RETURN_LT(layer->lt, NULL);
527 for (size_t i = 0; i < count; ++i) {
528 line = line_stream_next(line_stream);
530 RETURN_LT(layer->lt, NULL);
535 char id[RECT_LAYER_ID_MAX_SIZE];
538 "%"STRINGIFY(RECT_LAYER_ID_MAX_SIZE)"s%f%f%f%f%6s\n",
543 RETURN_LT(layer->lt, NULL);
546 Color color = hexstr(hex);
548 dynarray_push(layer->rects, &rect);
549 dynarray_push(layer->ids, id);
550 dynarray_push(layer->colors, &color);
556 void destroy_rect_layer(RectLayer *layer)
559 RETURN_LT0(layer->lt);
562 int rect_layer_render(const RectLayer *layer, Camera *camera, int active)
565 trace_assert(camera);
567 const size_t n = dynarray_count(layer->rects);
568 Rect *rects = dynarray_data(layer->rects);
569 Color *colors = dynarray_data(layer->colors);
570 const char *ids = dynarray_data(layer->ids);
573 for (size_t i = 0; i < n; ++i) {
574 Rect rect = rects[i];
575 Color color = colors[i];
577 if (layer->selection == (int) i) {
578 if (layer->state == RECT_LAYER_RESIZE || layer->state == RECT_LAYER_MOVE) {
579 rect = layer->inter_rect;
582 if (layer->state == RECT_LAYER_RECOLOR) {
583 color = layer->inter_color;
587 if (camera_fill_rect(
592 rgba(1.0f, 1.0f, 1.0f, active ? 1.0f : 0.5f))) < 0) {
597 if (active && (size_t) layer->selection == i) {
598 const Rect overlay_rect =
600 camera_rect(camera, rect),
601 RECT_LAYER_SELECTION_THICCNESS * 0.5f);
602 const Color overlay_color = color_invert(color);
605 if (camera_fill_rect(
610 rgba(1.0f, 1.0f, 1.0f, active ? 1.0f : 0.5f))) < 0) {
614 if (camera_draw_thicc_rect_screen(
618 RECT_LAYER_SELECTION_THICCNESS) < 0) {
623 if (layer->state == RECT_LAYER_ID_RENAME) {
624 // ID renaming Edit Field
625 if (edit_field_render_world(
626 layer->id_edit_field,
628 rect_position(rect)) < 0) {
633 if (camera_render_text(
635 ids + layer->selection * RECT_LAYER_ID_MAX_SIZE,
636 RECT_LAYER_ID_LABEL_SIZE,
638 rect_position(rect)) < 0) {
644 if (camera_fill_rect_screen(
646 rect_layer_resize_anchor(camera, rect),
647 overlay_color) < 0) {
654 const Color color = color_picker_rgba(&layer->color_picker);
655 if (layer->state == RECT_LAYER_CREATE) {
656 if (camera_fill_rect(camera, rect_from_points(layer->create_begin, layer->create_end), color) < 0) {
661 if (active && color_picker_render(&layer->color_picker, camera) < 0) {
669 int rect_layer_event_recolor(RectLayer *layer,
670 const SDL_Event *event,
671 const Camera *camera,
672 UndoHistory *undo_history)
676 trace_assert(camera);
677 trace_assert(undo_history);
678 trace_assert(layer->selection >= 0);
680 int color_changed = 0;
681 if (color_picker_event(&layer->color_picker, event, camera, &color_changed) < 0) {
686 layer->inter_color = color_picker_rgba(&layer->color_picker);
688 if (!color_picker_drag(&layer->color_picker)) {
689 UNDO_PUSH(layer, undo_history, UNDO_UPDATE);
690 dynarray_replace_at(layer->colors, (size_t) layer->selection, &layer->inter_color);
691 layer->state = RECT_LAYER_IDLE;
698 int rect_layer_event(RectLayer *layer,
699 const SDL_Event *event,
700 const Camera *camera,
701 UndoHistory *undo_history)
705 trace_assert(undo_history);
707 switch (layer->state) {
708 case RECT_LAYER_IDLE:
709 return rect_layer_event_idle(layer, event, camera, undo_history);
711 case RECT_LAYER_CREATE:
712 return rect_layer_event_create(layer, event, camera, undo_history);
714 case RECT_LAYER_RESIZE:
715 return rect_layer_event_resize(layer, event, camera, undo_history);
717 case RECT_LAYER_MOVE:
718 return rect_layer_event_move(layer, event, camera, undo_history);
720 case RECT_LAYER_ID_RENAME:
721 return rect_layer_event_id_rename(layer, event, camera, undo_history);
723 case RECT_LAYER_RECOLOR:
724 return rect_layer_event_recolor(layer, event, camera, undo_history);
730 size_t rect_layer_count(const RectLayer *layer)
732 return dynarray_count(layer->rects);
735 const Rect *rect_layer_rects(const RectLayer *layer)
737 return dynarray_data(layer->rects);
740 const Color *rect_layer_colors(const RectLayer *layer)
742 return dynarray_data(layer->colors);
745 const char *rect_layer_ids(const RectLayer *layer)
747 return dynarray_data(layer->ids);
750 int rect_layer_dump_stream(const RectLayer *layer, FILE *filedump)
753 trace_assert(filedump);
755 size_t n = dynarray_count(layer->ids);
756 char *ids = dynarray_data(layer->ids);
757 Rect *rects = dynarray_data(layer->rects);
758 Color *colors = dynarray_data(layer->colors);
760 fprintf(filedump, "%zd\n", n);
761 for (size_t i = 0; i < n; ++i) {
762 fprintf(filedump, "%s %f %f %f %f ",
763 ids + RECT_LAYER_ID_MAX_SIZE * i,
764 rects[i].x, rects[i].y, rects[i].w, rects[i].h);
765 color_hex_to_stream(colors[i], filedump);
766 fprintf(filedump, "\n");