]> git.lizzy.rs Git - nothing.git/blob - src/game/level/level_editor/player_layer.c
Delete LineStream from the existance
[nothing.git] / src / game / level / level_editor / player_layer.c
1 #include <stdio.h>
2
3 #include <SDL.h>
4
5 #include "game/camera.h"
6 #include "system/stacktrace.h"
7 #include "player_layer.h"
8 #include "system/nth_alloc.h"
9 #include "system/log.h"
10 #include "undo_history.h"
11 #include "system/memory.h"
12
13 typedef struct {
14     PlayerLayer *layer;
15     Vec2f position;
16     Color color;
17 } PlayerUndoContext;
18
19 static
20 PlayerUndoContext player_layer_create_undo_context(PlayerLayer *player_layer)
21 {
22     PlayerUndoContext context = {
23         .layer = player_layer,
24         .position = player_layer->position,
25         .color = player_layer->prev_color
26     };
27
28     return context;
29 }
30
31 static
32 void player_layer_undo(void *context, size_t context_size)
33 {
34     trace_assert(context);
35     trace_assert(sizeof(PlayerUndoContext) == context_size);
36
37     PlayerUndoContext *undo_context = context;
38     PlayerLayer *player_layer = undo_context->layer;
39
40     player_layer->position = undo_context->position;
41     player_layer->color_picker = create_color_picker_from_rgba(undo_context->color);
42     player_layer->prev_color = undo_context->color;
43 }
44
45 PlayerLayer create_player_layer(Vec2f position, Color color)
46 {
47     return (PlayerLayer) {
48         .position = position,
49         .color_picker = create_color_picker_from_rgba(color),
50         .prev_color = color
51     };
52 }
53
54 PlayerLayer chop_player_layer(Memory *memory, String *input)
55 {
56     trace_assert(memory);
57     trace_assert(input);
58
59     String line = chop_by_delim(input, '\n');
60     float x = strtof(string_to_cstr(memory, chop_word(&line)), NULL);
61     float y = strtof(string_to_cstr(memory, chop_word(&line)), NULL);
62     Color color = hexs(chop_word(&line));
63
64     return create_player_layer(vec(x, y), color);
65 }
66
67 LayerPtr player_layer_as_layer(PlayerLayer *player_layer)
68 {
69     LayerPtr layer = {
70         .type = LAYER_PLAYER,
71         .ptr = player_layer
72     };
73     return layer;
74 }
75
76 int player_layer_render(const PlayerLayer *player_layer,
77                         const Camera *camera,
78                         int active)
79 {
80     trace_assert(player_layer);
81     trace_assert(camera);
82
83     if (camera_fill_rect(
84             camera,
85             rect_from_vecs(
86                 player_layer->position,
87                 vec(25.0f, 25.0f)),
88             color_scale(
89                 color_picker_rgba(&player_layer->color_picker),
90                 rgba(1.0f, 1.0f, 1.0f, active ? 1.0f : 0.5f))) < 0) {
91         return -1;
92     }
93
94     if (active && color_picker_render(
95             &player_layer->color_picker,
96             camera)) {
97         return -1;
98     }
99
100     return 0;
101 }
102
103 int player_layer_event(PlayerLayer *player_layer,
104                        const SDL_Event *event,
105                        const Camera *camera,
106                        UndoHistory *undo_history)
107 {
108     trace_assert(player_layer);
109     trace_assert(event);
110     trace_assert(camera);
111     trace_assert(undo_history);
112
113     int selected = 0;
114     if (color_picker_event(
115             &player_layer->color_picker,
116             event,
117             camera,
118             &selected) < 0) {
119         return -1;
120     }
121
122     if (selected && !color_picker_drag(&player_layer->color_picker)) {
123         PlayerUndoContext context =
124             player_layer_create_undo_context(player_layer);
125         undo_history_push(
126             undo_history,
127             player_layer_undo,
128             &context,
129             sizeof(context));
130         player_layer->prev_color = color_picker_rgba(&player_layer->color_picker);
131     }
132
133     if (!selected &&
134         event->type == SDL_MOUSEBUTTONDOWN &&
135         event->button.button == SDL_BUTTON_LEFT) {
136
137         PlayerUndoContext context =
138             player_layer_create_undo_context(player_layer);
139
140         undo_history_push(
141             undo_history,
142             player_layer_undo,
143             &context, sizeof(context));
144
145         player_layer->position =
146             camera_map_screen(camera,
147                               event->button.x,
148                               event->button.y);
149     }
150
151     return 0;
152 }
153
154 int player_layer_dump_stream(const PlayerLayer *player_layer,
155                              FILE *filedump)
156 {
157     trace_assert(player_layer);
158     trace_assert(filedump);
159
160     fprintf(filedump, "%f %f ", player_layer->position.x, player_layer->position.y);
161     color_hex_to_stream(color_picker_rgba(&player_layer->color_picker), filedump);
162     fprintf(filedump, "\n");
163
164     return 0;
165 }