]> git.lizzy.rs Git - nothing.git/blob - src/game/level/level_editor/proto_rect.c
Add TODO(#886)
[nothing.git] / src / game / level / level_editor / proto_rect.c
1 #include <stdbool.h>
2
3 #include <SDL.h>
4
5 #include "system/stacktrace.h"
6 #include "system/log.h"
7 #include "game/camera.h"
8
9 #include "rect_layer.h"
10 #include "proto_rect.h"
11
12 #define PROTO_AREA_THRESHOLD 10.0
13
14 int proto_rect_render(const ProtoRect *proto_rect,
15                       Camera *camera)
16 {
17     trace_assert(proto_rect);
18     trace_assert(proto_rect->color_current);
19     trace_assert(camera);
20
21     if (proto_rect->active) {
22         if (camera_fill_rect(
23                 camera,
24                 rect_from_points(
25                     proto_rect->begin,
26                     proto_rect->end),
27                 *proto_rect->color_current) < 0) {
28             return -1;
29         }
30     }
31
32     return 0;
33 }
34
35 int proto_rect_event(ProtoRect *proto_rect,
36                      const SDL_Event *event,
37                      const Camera *camera)
38 {
39     trace_assert(proto_rect);
40     trace_assert(proto_rect->color_current);
41     trace_assert(proto_rect->layer_current);
42     trace_assert(event);
43     trace_assert(camera);
44
45     if (proto_rect->active) {
46         // Active
47         switch (event->type) {
48         case SDL_MOUSEBUTTONUP: {
49             switch (event->button.button) {
50             case SDL_BUTTON_LEFT: {
51                 const Rect real_rect =
52                     rect_from_points(
53                         proto_rect->begin,
54                         proto_rect->end);
55                 const float area = real_rect.w * real_rect.h;
56
57                 if (area >= PROTO_AREA_THRESHOLD) {
58                     rect_layer_add_rect(proto_rect->layer_current, real_rect, *proto_rect->color_current);
59                 } else {
60                     log_info("The area is too small %f. Such small box won't be created.\n", area);
61                 }
62                 proto_rect->active = false;
63             } break;
64             }
65         } break;
66
67         case SDL_MOUSEMOTION: {
68             proto_rect->end = camera_map_screen(
69                 camera,
70                 event->motion.x,
71                 event->motion.y);
72         } break;
73         }
74     } else {
75         // Inactive
76         switch (event->type) {
77         case SDL_MOUSEBUTTONDOWN: {
78             switch (event->button.button) {
79             case SDL_BUTTON_LEFT: {
80                 proto_rect->active = true;
81                 proto_rect->begin = camera_map_screen(
82                     camera,
83                     event->button.x,
84                     event->button.y);
85                 proto_rect->end = proto_rect->begin;
86             } break;
87             }
88         } break;
89         }
90     }
91
92     return 0;
93 }
94
95 int proto_rect_mouse_button(ProtoRect *proto_rect,
96                             const SDL_MouseButtonEvent *event,
97                             const Camera *camera)
98 {
99     trace_assert(proto_rect);
100     trace_assert(proto_rect->color_current);
101     trace_assert(proto_rect->layer_current);
102     trace_assert(event);
103     trace_assert(camera);
104
105     if (proto_rect->active) {
106         // Active
107         switch (event->type) {
108         case SDL_MOUSEBUTTONUP: {
109             switch (event->button) {
110             case SDL_BUTTON_LEFT: {
111                 const Rect real_rect =
112                     rect_from_points(
113                         proto_rect->begin,
114                         proto_rect->end);
115                 const float area = real_rect.w * real_rect.h;
116
117                 if (area >= PROTO_AREA_THRESHOLD) {
118                     rect_layer_add_rect(proto_rect->layer_current, real_rect, *proto_rect->color_current);
119                 } else {
120                     log_info("The area is too small %f. Such small box won't be created.\n", area);
121                 }
122                 proto_rect->active = false;
123             } break;
124
125             case SDL_BUTTON_RIGHT: {
126                 proto_rect->active = false;
127             } break;
128             }
129         } break;
130         }
131     } else {
132         // Inactive
133         switch (event->type) {
134         case SDL_MOUSEBUTTONDOWN: {
135             switch (event->button) {
136             case SDL_BUTTON_LEFT: {
137                 proto_rect->active = true;
138                 proto_rect->begin = camera_map_screen(
139                     camera,
140                     event->x,
141                     event->y);
142                 proto_rect->end = proto_rect->begin;
143             } break;
144             }
145         } break;
146         }
147     }
148
149     return 0;
150 }
151
152
153 int proto_rect_mouse_motion(ProtoRect *proto_rect,
154                             const SDL_MouseMotionEvent *event,
155                             const Camera *camera)
156 {
157     trace_assert(proto_rect);
158     trace_assert(event);
159
160     if (proto_rect->active) {
161         proto_rect->end = camera_map_screen(
162             camera,
163             event->x,
164             event->y);
165     }
166
167     return 0;
168 }