]> git.lizzy.rs Git - nothing.git/blobdiff - src/game/level/level_editor/player_layer.c
Delete LineStream from the existance
[nothing.git] / src / game / level / level_editor / player_layer.c
index f794becbb0dae2aa333ba38c63b3197044f73f47..0e7bd8d8e635a9920e7e66ae07c37ba7e84eca3c 100644 (file)
@@ -8,37 +8,60 @@
 #include "system/nth_alloc.h"
 #include "system/log.h"
 #include "undo_history.h"
+#include "system/memory.h"
 
+typedef struct {
+    PlayerLayer *layer;
+    Vec2f position;
+    Color color;
+} PlayerUndoContext;
 
-PlayerLayer create_player_layer(Vec position, Color color)
+static
+PlayerUndoContext player_layer_create_undo_context(PlayerLayer *player_layer)
 {
-    return (PlayerLayer) {
-        .position = position,
-        .color_picker = create_color_picker_from_rgba(color),
+    PlayerUndoContext context = {
+        .layer = player_layer,
+        .position = player_layer->position,
+        .color = player_layer->prev_color
     };
+
+    return context;
 }
 
-PlayerLayer create_player_layer_from_line_stream(LineStream *line_stream)
+static
+void player_layer_undo(void *context, size_t context_size)
 {
-    trace_assert(line_stream);
+    trace_assert(context);
+    trace_assert(sizeof(PlayerUndoContext) == context_size);
 
-    const char *line = line_stream_next(line_stream);
-    trace_assert(line);
+    PlayerUndoContext *undo_context = context;
+    PlayerLayer *player_layer = undo_context->layer;
 
-    char colorstr[7] = "000000";
-    Point position = vec(0.0f, 0.0f);
+    player_layer->position = undo_context->position;
+    player_layer->color_picker = create_color_picker_from_rgba(undo_context->color);
+    player_layer->prev_color = undo_context->color;
+}
 
-    const int bound =
-        sscanf(line, "%f%f%6s", &position.x, &position.y, colorstr);
+PlayerLayer create_player_layer(Vec2f position, Color color)
+{
+    return (PlayerLayer) {
+        .position = position,
+        .color_picker = create_color_picker_from_rgba(color),
+        .prev_color = color
+    };
+}
 
-#define BOUND_EXPECTED 3
-    if (bound != BOUND_EXPECTED) {
-        log_fail("Could not read Player Layer properly. Parsed tokens: %d. Expected: %d\n",
-                 bound, BOUND_EXPECTED);
-    }
-#undef BOUND_EXPECTED
+PlayerLayer chop_player_layer(Memory *memory, String *input)
+{
+    trace_assert(memory);
+    trace_assert(input);
 
-    return create_player_layer(position, hexstr(colorstr));
+    String line = chop_by_delim(input, '\n');
+    float x = strtof(string_to_cstr(memory, chop_word(&line)), NULL);
+    float y = strtof(string_to_cstr(memory, chop_word(&line)), NULL);
+    Color color = hexs(chop_word(&line));
+
+    return create_player_layer(vec(x, y), color);
 }
 
 LayerPtr player_layer_as_layer(PlayerLayer *player_layer)
@@ -51,7 +74,7 @@ LayerPtr player_layer_as_layer(PlayerLayer *player_layer)
 }
 
 int player_layer_render(const PlayerLayer *player_layer,
-                        Camera *camera,
+                        const Camera *camera,
                         int active)
 {
     trace_assert(player_layer);
@@ -70,24 +93,13 @@ int player_layer_render(const PlayerLayer *player_layer,
 
     if (active && color_picker_render(
             &player_layer->color_picker,
-            camera,
-            vec(0.0f, 0.0f))) {
+            camera)) {
         return -1;
     }
 
     return 0;
 }
 
-static void player_layer_revert_position(void *layer, Context context)
-{
-    trace_assert(layer);
-
-    PlayerLayer *player_layer = layer;
-    player_layer->position = *((Point*)context.data);
-}
-
-// TODO(#1013): PlayerLayer does not support undo color
-
 int player_layer_event(PlayerLayer *player_layer,
                        const SDL_Event *event,
                        const Camera *camera,
@@ -102,20 +114,33 @@ int player_layer_event(PlayerLayer *player_layer,
     if (color_picker_event(
             &player_layer->color_picker,
             event,
+            camera,
             &selected) < 0) {
         return -1;
     }
 
+    if (selected && !color_picker_drag(&player_layer->color_picker)) {
+        PlayerUndoContext context =
+            player_layer_create_undo_context(player_layer);
+        undo_history_push(
+            undo_history,
+            player_layer_undo,
+            &context,
+            sizeof(context));
+        player_layer->prev_color = color_picker_rgba(&player_layer->color_picker);
+    }
+
     if (!selected &&
         event->type == SDL_MOUSEBUTTONDOWN &&
         event->button.button == SDL_BUTTON_LEFT) {
-        Action action = {
-            .layer = player_layer,
-            .revert = player_layer_revert_position
-        };
-        trace_assert(sizeof(player_layer->position) <= CONTEXT_SIZE);
-        memcpy(action.context.data, &player_layer->position, sizeof(player_layer->position));
-        undo_history_push(undo_history, action);
+
+        PlayerUndoContext context =
+            player_layer_create_undo_context(player_layer);
+
+        undo_history_push(
+            undo_history,
+            player_layer_undo,
+            &context, sizeof(context));
 
         player_layer->position =
             camera_map_screen(camera,