#include "dynarray.h"
#include "game/camera.h"
#include "system/log.h"
-#include "system/lt.h"
#include "system/nth_alloc.h"
#include "system/stacktrace.h"
#include "system/str.h"
// TODO(#1140): PointLayer does not support snapping
-typedef enum {
- POINT_LAYER_IDLE = 0,
- POINT_LAYER_EDIT_ID,
- POINT_LAYER_MOVE,
- POINT_LAYER_RECOLOR
-} PointLayerState;
-
-struct PointLayer
-{
- Lt *lt;
- PointLayerState state;
- Dynarray/*<Vec2f>*/ positions;
- Dynarray/*<Color>*/ colors;
- Dynarray/*<char[ID_MAX_SIZE]>*/ ids;
- int selection;
- ColorPicker color_picker;
-
- Vec2f inter_position;
- Color inter_color;
- Edit_field *edit_field;
-
- int id_name_counter;
- const char *id_name_prefix;
-};
-
typedef enum {
POINT_UNDO_ADD,
POINT_UNDO_DELETE,
return layer;
}
-PointLayer *create_point_layer(const char *id_name_prefix)
+PointLayer create_point_layer(const char *id_name_prefix)
{
- Lt *lt = create_lt();
-
- PointLayer *point_layer = PUSH_LT(lt, nth_calloc(1, sizeof(PointLayer)), free);
- if (point_layer == NULL) {
- RETURN_LT(lt, NULL);
- }
- point_layer->lt = lt;
-
- point_layer->state = POINT_LAYER_IDLE;
-
- point_layer->positions = create_dynarray(sizeof(Vec2f));
- point_layer->colors = create_dynarray(sizeof(Color));
- point_layer->ids = create_dynarray(sizeof(char) * ID_MAX_SIZE);
-
- point_layer->edit_field = PUSH_LT(
- lt,
- create_edit_field(
- POINT_LAYER_ID_TEXT_SIZE,
- POINT_LAYER_ID_TEXT_COLOR),
- destroy_edit_field);
- if (point_layer->edit_field == NULL) {
- RETURN_LT(lt, NULL);
- }
+ PointLayer result = {0};
+ result.state = POINT_LAYER_IDLE;
+ result.positions = create_dynarray(sizeof(Vec2f));
+ result.colors = create_dynarray(sizeof(Color));
+ result.ids = create_dynarray(sizeof(char) * ID_MAX_SIZE);
+ result.edit_field.font_size = POINT_LAYER_ID_TEXT_SIZE;
+ result.edit_field.font_color = POINT_LAYER_ID_TEXT_COLOR;
+ result.id_name_prefix = id_name_prefix;
+ return result;
+}
- point_layer->id_name_prefix = id_name_prefix;
+PointLayer *create_point_layer_from_memory(Memory *memory,
+ const char *id_name_prefix)
+{
+ trace_assert(memory);
+ trace_assert(id_name_prefix);
- return point_layer;
+ PointLayer *result = memory_alloc(memory, sizeof(PointLayer));
+ memset(result, 0, sizeof(PointLayer));
+ result->state = POINT_LAYER_IDLE;
+ result->positions = create_dynarray_from_memory(memory, sizeof(Vec2f));
+ result->colors = create_dynarray_from_memory(memory, sizeof(Color));
+ result->ids = create_dynarray_from_memory(memory, sizeof(char) * ID_MAX_SIZE);
+ result->edit_field.font_size = POINT_LAYER_ID_TEXT_SIZE;
+ result->edit_field.font_color = POINT_LAYER_ID_TEXT_COLOR;
+ result->id_name_prefix = id_name_prefix;
+ return result;
}
-PointLayer *chop_point_layer(Memory *memory,
- String *input,
- const char *id_name_prefix)
+void point_layer_load(PointLayer *point_layer,
+ Memory *memory,
+ String *input)
{
+ trace_assert(point_layer);
trace_assert(memory);
trace_assert(input);
- trace_assert(id_name_prefix);
-
- PointLayer *point_layer = create_point_layer(id_name_prefix);
int n = atoi(string_to_cstr(memory, trim(chop_by_delim(input, '\n'))));
char id[ENTITY_MAX_ID_SIZE];
Color color = hexs(trim(chop_word(&line)));
memset(id, 0, ENTITY_MAX_ID_SIZE);
- memcpy(
- id,
- string_id.data,
- min_size_t(ENTITY_MAX_ID_SIZE - 1, string_id.count));
+ memcpy(id, string_id.data, min_size_t(ENTITY_MAX_ID_SIZE - 1, string_id.count));
dynarray_push(&point_layer->positions, &point);
dynarray_push(&point_layer->colors, &color);
dynarray_push(&point_layer->ids, id);
}
-
- point_layer->selection = -1;
- point_layer->color_picker = create_color_picker_from_rgba(COLOR_RED);
-
- return point_layer;
-}
-
-void destroy_point_layer(PointLayer *point_layer)
-{
- trace_assert(point_layer);
-
- free(point_layer->positions.data);
- free(point_layer->colors.data);
- free(point_layer->ids.data);
-
- RETURN_LT0(point_layer->lt);
}
static inline
if (point_layer->state == POINT_LAYER_EDIT_ID) {
if (edit_field_render_world(
- point_layer->edit_field,
+ &point_layer->edit_field,
camera,
positions[point_layer->selection]) < 0) {
return -1;
char *ids = (char*)point_layer->ids.data;
point_layer->state = POINT_LAYER_EDIT_ID;
edit_field_replace(
- point_layer->edit_field,
+ &point_layer->edit_field,
ids + ID_MAX_SIZE * point_layer->selection);
SDL_StartTextInput();
}
POINT_UNDO_UPDATE));
char *id = dynarray_pointer_at(&point_layer->ids, (size_t) point_layer->selection);
- const char *text = edit_field_as_text(point_layer->edit_field);
+ const char *text = edit_field_as_text(&point_layer->edit_field);
size_t n = min_size_t(strlen(text), ID_MAX_SIZE - 1);
memcpy(id, text, n);
memset(id + n, 0, ID_MAX_SIZE - n);
} break;
}
- return edit_field_event(point_layer->edit_field, event);
+ return edit_field_event(&point_layer->edit_field, event);
}
static