#include <errno.h>
#include "game/camera.h"
-#include "system/lt.h"
#include "system/stacktrace.h"
#include "system/nth_alloc.h"
#include "system/log.h"
#include "color.h"
#include "rect_layer.h"
#include "dynarray.h"
-#include "color_picker.h"
#include "system/str.h"
-#include "ui/edit_field.h"
#include "undo_history.h"
#include "game/level/action.h"
#include "game.h"
#define RECT_LAYER_SELECTION_THICCNESS 15.0f
#define RECT_LAYER_ID_LABEL_SIZE vec(3.0f, 3.0f)
#define CREATE_AREA_THRESHOLD 10.0
-#define RECT_LAYER_GRID_ROWS 3
-#define RECT_LAYER_GRID_COLUMNS 4
static int rect_clipboard = 0;
static Rect rect_clipboard_rect;
CURSOR_STYLE_RESIZE_DIAG1 // [12]
};
-typedef enum {
- RECT_LAYER_IDLE = 0,
- RECT_LAYER_CREATE,
- RECT_LAYER_RESIZE,
- RECT_LAYER_MOVE,
- RECT_LAYER_ID_RENAME,
- RECT_LAYER_RECOLOR
-} RectLayerState;
-
-struct RectLayer {
- Lt *lt;
- RectLayerState state;
- int resize_mask;
- Dynarray ids;
- Dynarray rects;
- Dynarray colors;
- Dynarray actions;
- ColorPicker color_picker;
- Vec2f create_begin;
- Vec2f create_end;
- int selection;
- Vec2f move_anchor; // The mouse offset from the left-top
- // corner of the rect during moving it
- Edit_field id_edit_field;
- Color inter_color;
- Rect inter_rect;
- int id_name_counter;
- const char *id_name_prefix;
- Cursor *cursor;
-
- int snapping_enabled;
-};
-
typedef enum {
RECT_UNDO_ADD,
RECT_UNDO_DELETE,
return layer;
}
-RectLayer *create_rect_layer(const char *id_name_prefix, Cursor *cursor)
+RectLayer *create_rect_layer_from_memory(Memory *memory,
+ const char *id_name_prefix,
+ Cursor *cursor)
{
+ trace_assert(memory);
+ trace_assert(id_name_prefix);
trace_assert(cursor);
- Lt *lt = create_lt();
-
- RectLayer *layer = PUSH_LT(lt, nth_calloc(1, sizeof(RectLayer)), free);
- if (layer == NULL) {
- RETURN_LT(lt, NULL);
- }
- layer->lt = lt;
-
- layer->ids = create_dynarray(sizeof(char) * ENTITY_MAX_ID_SIZE);
- layer->rects = create_dynarray(sizeof(Rect));
- layer->colors = create_dynarray(sizeof(Color));
- layer->actions = create_dynarray(sizeof(Action));
-
- layer->id_edit_field.font_size = RECT_LAYER_ID_LABEL_SIZE;
- layer->id_edit_field.font_color = COLOR_BLACK;
+ RectLayer *rect_layer = memory_alloc(memory, sizeof(RectLayer));
- layer->color_picker = create_color_picker_from_rgba(rgba(1.0f, 0.0f, 0.0f, 1.0f));
- layer->selection = -1;
- layer->id_name_prefix = id_name_prefix;
- layer->cursor = cursor;
+ rect_layer->ids = create_dynarray_from_memory(memory, sizeof(char) * ENTITY_MAX_ID_SIZE);
+ rect_layer->rects = create_dynarray_from_memory(memory, sizeof(Rect));
+ rect_layer->colors = create_dynarray_from_memory(memory, sizeof(Color));
+ rect_layer->actions = create_dynarray_from_memory(memory, sizeof(Action));
+ rect_layer->id_edit_field.font_size = RECT_LAYER_ID_LABEL_SIZE;
+ rect_layer->id_edit_field.font_color = COLOR_BLACK;
+ rect_layer->color_picker = create_color_picker_from_rgba(rgba(1.0f, 0.0f, 0.0f, 1.0f));
+ rect_layer->selection = -1;
+ rect_layer->id_name_prefix = id_name_prefix;
+ rect_layer->cursor = cursor;
- return layer;
+ return rect_layer;
}
-RectLayer *chop_rect_layer(Memory *memory,
- String *input,
- const char *id_name_prefix,
- Cursor *cursor)
+void rect_layer_load(RectLayer *layer, Memory *memory, String *input)
{
+ trace_assert(layer);
trace_assert(memory);
trace_assert(input);
- RectLayer *layer = create_rect_layer(id_name_prefix, cursor);
- trace_assert(layer);
-
int n = atoi(string_to_cstr(memory, trim(chop_by_delim(input, '\n'))));
char id[ENTITY_MAX_ID_SIZE];
for (int i = 0; i < n; ++i) {
dynarray_push(&layer->actions, &action);
}
-
- return layer;
-}
-
-void destroy_rect_layer(RectLayer *layer)
-{
- trace_assert(layer);
-
- free(layer->ids.data);
- free(layer->rects.data);
- free(layer->colors.data);
- free(layer->actions.data);
-
- RETURN_LT0(layer->lt);
}
int rect_layer_render(const RectLayer *layer, const Camera *camera, int active)