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 "./color_picker.h"
18 #define POINT_LAYER_ELEMENT_RADIUS 10.0f
21 POINT_LAYER_NORMAL_STATE = 0,
22 POINT_LAYER_ID_EDITING_STATE
28 PointLayerState state;
29 Dynarray/*<Point>*/ *points;
30 Dynarray/*<Color>*/ *colors;
31 Dynarray/*<char[ID_MAX_SIZE]>*/ *ids;
32 Edit_field *edit_field;
34 ColorPicker color_picker;
37 LayerPtr point_layer_as_layer(PointLayer *point_layer)
46 PointLayer *create_point_layer(void)
50 PointLayer *point_layer = PUSH_LT(lt, nth_calloc(1, sizeof(PointLayer)), free);
51 if (point_layer == NULL) {
56 point_layer->state = POINT_LAYER_NORMAL_STATE;
58 point_layer->points = PUSH_LT(lt, create_dynarray(sizeof(Point)), destroy_dynarray);
59 if (point_layer->points == NULL) {
63 point_layer->colors = PUSH_LT(lt, create_dynarray(sizeof(Color)), destroy_dynarray);
64 if (point_layer->colors == NULL) {
68 point_layer->ids = PUSH_LT(lt, create_dynarray(sizeof(char) * ID_MAX_SIZE), destroy_dynarray);
69 if (point_layer->ids == NULL) {
73 point_layer->edit_field = PUSH_LT(
77 rgba(0.0f, 0.0f, 0.0f, 1.0f)),
79 if (point_layer->edit_field == NULL) {
86 PointLayer *create_point_layer_from_line_stream(LineStream *line_stream)
88 trace_assert(line_stream);
90 PointLayer *point_layer = create_point_layer();
94 line_stream_next(line_stream),
97 log_fail("Could not read amount of points");
98 RETURN_LT(point_layer->lt, NULL);
102 char id[ID_MAX_SIZE];
104 for (size_t i = 0; i < count; ++i) {
106 line_stream_next(line_stream),
107 "%"STRINGIFY(ID_MAX_SIZE)"s%f%f%6s",
108 id, &x, &y, color_name) < 0) {
109 log_fail("Could not read %dth goal\n", i);
110 RETURN_LT(point_layer->lt, NULL);
112 const Color color = hexstr(color_name);
113 const Point point = vec(x, y);
115 dynarray_push(point_layer->colors, &color);
116 dynarray_push(point_layer->points, &point);
117 dynarray_push(point_layer->ids, id);
120 point_layer->selected = -1;
122 point_layer->color_picker = create_color_picker_from_rgba(rgba(1.0f, 0.0f, 0.0f, 1.0f));
127 void destroy_point_layer(PointLayer *point_layer)
129 trace_assert(point_layer);
130 RETURN_LT0(point_layer->lt);
133 int point_layer_render(const PointLayer *point_layer,
137 trace_assert(point_layer);
138 trace_assert(camera);
140 const int n = (int) dynarray_count(point_layer->points);
141 Point *points = dynarray_data(point_layer->points);
142 Color *colors = dynarray_data(point_layer->colors);
144 for (int i = 0; i < n; ++i) {
145 const Triangle t = triangle_mat3x3_product(
146 equilateral_triangle(),
148 trans_mat(points[i].x, points[i].y),
149 scale_mat(POINT_LAYER_ELEMENT_RADIUS)));
151 const Color color = color_scale(
153 rgba(1.0f, 1.0f, 1.0f, active ? 1.0f : 0.5f));
155 if (i == point_layer->selected) {
156 const Triangle t0 = triangle_mat3x3_product(
157 equilateral_triangle(),
159 trans_mat(points[i].x, points[i].y),
162 if (camera_fill_triangle(camera, t0, color_invert(color)) < 0) {
167 if (camera_fill_triangle(camera, t, color) < 0) {
171 /* TODO(#854): The ids of PointLayer are not displayed constantly */
174 if (point_layer->state == POINT_LAYER_ID_EDITING_STATE) {
175 /* TODO(#855): PointLayer edit field is not scaled on zoom */
176 if (edit_field_render(
177 point_layer->edit_field,
179 camera_point(camera, points[point_layer->selected])) < 0) {
184 if (active && color_picker_render(&point_layer->color_picker, camera) < 0) {
193 static int point_layer_mouse_button(PointLayer *point_layer,
194 const SDL_MouseButtonEvent *event,
195 const Camera *camera)
197 trace_assert(point_layer);
200 if (point_layer->state == POINT_LAYER_NORMAL_STATE &&
201 event->type == SDL_MOUSEBUTTONDOWN &&
202 event->button == SDL_BUTTON_LEFT) {
203 const int n = (int) dynarray_count(point_layer->points);
204 const Point *points = dynarray_data(point_layer->points);
205 const Point point = camera_map_screen(camera, event->x, event->y);
206 const Color color = color_picker_rgba(&point_layer->color_picker);
208 for (int i = 0; i < n; ++i) {
209 if (vec_length(vec_sub(points[i], point)) < POINT_LAYER_ELEMENT_RADIUS) {
210 point_layer->selected = i;
215 char id[ID_MAX_SIZE];
217 for (size_t i = 0; i < ID_MAX_SIZE - 1; ++i) {
218 id[i] = (char) ('a' + rand() % ('z' - 'a' + 1));
220 id[ID_MAX_SIZE - 1] = '\0';
222 dynarray_push(point_layer->points, &point);
223 dynarray_push(point_layer->colors, &color);
224 dynarray_push(point_layer->ids, id);
231 int point_layer_keyboard(PointLayer *point_layer,
232 const SDL_KeyboardEvent *key)
234 trace_assert(point_layer);
237 switch(point_layer->state) {
238 case POINT_LAYER_NORMAL_STATE: {
239 if (key->type == SDL_KEYDOWN) {
240 switch (key->keysym.sym) {
242 if (0 <= point_layer->selected && point_layer->selected < (int) dynarray_count(point_layer->points)) {
243 dynarray_delete_at(point_layer->points, (size_t) point_layer->selected);
244 dynarray_delete_at(point_layer->colors, (size_t) point_layer->selected);
245 dynarray_delete_at(point_layer->ids, (size_t) point_layer->selected);
247 point_layer->selected = -1;
251 if (point_layer->selected >= 0) {
252 char *ids = dynarray_data(point_layer->ids);
253 point_layer->state = POINT_LAYER_ID_EDITING_STATE;
255 point_layer->edit_field,
256 ids + ID_MAX_SIZE * point_layer->selected);
257 SDL_StartTextInput();
266 case POINT_LAYER_ID_EDITING_STATE: {
267 if (edit_field_keyboard(point_layer->edit_field, key) < 0) {
271 if (key->type == SDL_KEYDOWN) {
272 switch(key->keysym.sym) {
274 char *ids = dynarray_data(point_layer->ids);
275 const char *text = edit_field_as_text(point_layer->edit_field);
276 size_t n = max_size_t(strlen(text), ID_MAX_SIZE - 1);
277 memcpy(ids + point_layer->selected * ID_MAX_SIZE, text, n);
278 *(ids + point_layer->selected * ID_MAX_SIZE + n) = '\0';
279 point_layer->state = POINT_LAYER_NORMAL_STATE;
284 point_layer->state = POINT_LAYER_NORMAL_STATE;
297 int point_layer_text_input(PointLayer *point_layer,
298 const SDL_TextInputEvent *text_input)
300 trace_assert(point_layer);
301 trace_assert(text_input);
303 if (point_layer->state == POINT_LAYER_ID_EDITING_STATE) {
304 /* TODO(#856): Special development keybindings interfere with id editing field */
305 return edit_field_text_input(point_layer->edit_field, text_input);
311 int point_layer_event(PointLayer *point_layer,
312 const SDL_Event *event,
313 const Camera *camera)
315 trace_assert(point_layer);
317 trace_assert(camera);
320 if (color_picker_event(
321 &point_layer->color_picker,
331 switch(event->type) {
332 case SDL_MOUSEBUTTONDOWN:
333 case SDL_MOUSEBUTTONUP:
334 return point_layer_mouse_button(
341 return point_layer_keyboard(
346 return point_layer_text_input(
354 size_t point_layer_count(const PointLayer *point_layer)
356 trace_assert(point_layer);
357 return dynarray_count(point_layer->points);
360 const Point *point_layer_points(const PointLayer *point_layer)
362 trace_assert(point_layer);
363 return dynarray_data(point_layer->points);
366 const Color *point_layer_colors(const PointLayer *point_layer)
368 trace_assert(point_layer);
369 return dynarray_data(point_layer->colors);
372 const char *point_layer_ids(const PointLayer *point_layer)
374 trace_assert(point_layer);
375 return dynarray_data(point_layer->ids);
378 int point_layer_dump_stream(const PointLayer *point_layer,
381 trace_assert(point_layer);
382 trace_assert(filedump);
384 size_t n = dynarray_count(point_layer->ids);
385 char *ids = dynarray_data(point_layer->ids);
386 Point *points = dynarray_data(point_layer->points);
387 Color *colors = dynarray_data(point_layer->colors);
389 fprintf(filedump, "%zd\n", n);
390 for (size_t i = 0; i < n; ++i) {
391 fprintf(filedump, "%s %f %f ",
392 ids + ID_MAX_SIZE * i,
393 points[i].x, points[i].y);
394 color_hex_to_stream(colors[i], filedump);
395 fprintf(filedump, "\n");