diff --git a/kubo_bar.c b/kubo_bar.c index 5cee631..da6347a 100644 --- a/kubo_bar.c +++ b/kubo_bar.c @@ -18,9 +18,12 @@ #include "kubo_bar.h" +const char *snap_text_on = "Snap enabled"; +const char *snap_text_off = "Snap disabled"; + static inline int push_text(int x, int y, const char *text, Color bg_color); -void kubo_bar_render(struct kubo_context *context) { +void kubo_bar_render(struct kubo_context *context, bool snap_enabled) { const int bar_y = GetScreenHeight() - KUBO_BAR_HEIGHT; int bar_text_x = 10; @@ -30,9 +33,18 @@ void kubo_bar_render(struct kubo_context *context) { bar_text_x += 2 * KUBO_BAR_PADDING; - if (context->state.id == KUBO_CONTEXT_COMMAND) { + const char *snap_text = snap_enabled ? snap_text_on : snap_text_off; + + switch (context->state.id) { + case KUBO_CONTEXT_COMMAND: kubo_command_bar_render(bar_text_x, bar_y + (KUBO_BAR_PADDING / 2), KUBO_BAR_HEIGHT - KUBO_BAR_PADDING); + break; + case KUBO_CONTEXT_WALL_NEW: + bar_text_x += push_text(bar_text_x, bar_y, snap_text, BLACK); + break; + default: + break; } } diff --git a/kubo_bar.h b/kubo_bar.h index 13836cd..db3615d 100644 --- a/kubo_bar.h +++ b/kubo_bar.h @@ -27,6 +27,6 @@ #include "kubo_context.h" #include "kubo_command_bar.h" -void kubo_bar_render(struct kubo_context *context); +void kubo_bar_render(struct kubo_context *context, bool snap_enabled); #endif diff --git a/kubo_input.c b/kubo_input.c index 811b951..ee95190 100644 --- a/kubo_input.c +++ b/kubo_input.c @@ -18,6 +18,8 @@ #include "kubo_input.h" +extern bool kubo_mouse_snap; + static void key_input(struct kubo_context *context, Camera2D *camera); static void char_input(struct kubo_context *context); @@ -90,6 +92,10 @@ static void key_input(struct kubo_context *context, Camera2D *camera) { kubo_context_delete_wall(context); break; + case KEY_T: + kubo_mouse_snap = !kubo_mouse_snap; + break; + case KEY_SPACE: kubo_camera_reset(camera); break; diff --git a/kubo_wall.c b/kubo_wall.c index 5bb89c3..dd106d1 100644 --- a/kubo_wall.c +++ b/kubo_wall.c @@ -40,7 +40,8 @@ Vector2 kubo_wall_get_vertex(struct kubo_wall *wall, size_t index) { return kubo_vector2_arr_get(&wall->vertices, index); } -void kubo_wall_render(struct kubo_wall *wall, bool select, Camera2D *camera) { +void kubo_wall_render(struct kubo_wall *wall, bool select, Camera2D *camera, Vector2 mouse) { + (void)camera; Color wall_color = (select && wall->state == KUBO_WALL_SELECTED) ? BLUE : BLACK; @@ -54,7 +55,7 @@ void kubo_wall_render(struct kubo_wall *wall, bool select, Camera2D *camera) { DrawSplineLinear(points, wall->vertices.count, 10.f, wall_color); if (wall->state == KUBO_WALL_DRAWING) { - Vector2 mouse = GetScreenToWorld2D(GetMousePosition(), *camera); + // Vector2 mouse = GetScreenToWorld2D(mouse, *camera); Vector2 mouse_points[] = {points[wall->vertices.count - 1], mouse}; DrawSplineLinear(mouse_points, 2, 10.f, wall_color); diff --git a/kubo_wall.h b/kubo_wall.h index 5a18268..3cec98c 100644 --- a/kubo_wall.h +++ b/kubo_wall.h @@ -47,6 +47,6 @@ void kubo_wall_cleanup(struct kubo_wall *wall); bool kubo_wall_add_vertex(struct kubo_wall *wall, Vector2 vec); Vector2 kubo_wall_get_vertex(struct kubo_wall *wall, size_t index); -void kubo_wall_render(struct kubo_wall *wall, bool select, Camera2D *camera); +void kubo_wall_render(struct kubo_wall *wall, bool select, Camera2D *camera, Vector2 mouse); #endif diff --git a/kubo_window.c b/kubo_window.c index 903292f..a5bc02b 100644 --- a/kubo_window.c +++ b/kubo_window.c @@ -17,16 +17,20 @@ */ #include "kubo_window.h" +#include "raylib.h" Camera2D camera = {0}; +bool kubo_mouse_snap = false; -static void window_render(struct kubo_context *context); -static void window_left_mouse(struct kubo_context *context); +static void window_render(struct kubo_context *context, Vector2 mouse_pos); +static void window_left_mouse(struct kubo_context *context, Vector2 mouse_pos); static void window_right_mouse(struct kubo_context *context); -static void new_wall_click(struct kubo_context *context); +static void new_wall_click(struct kubo_context *context, Vector2 mouse_pos); static void new_wall_end(struct kubo_context *context); +static Vector2 mouse_grid_snap(Vector2 mouse_pos); + void kubo_window_init() { SetConfigFlags(FLAG_WINDOW_RESIZABLE); InitWindow(KUBO_WINDOW_WIDTH, KUBO_WINDOW_HEIGHT, "Kubo"); @@ -44,7 +48,11 @@ bool kubo_window_should_close(struct kubo_context *context) { } void kubo_window_tick(struct kubo_context *context) { - window_render(context); + Vector2 mouse_pos = GetScreenToWorld2D(GetMousePosition(), camera); + if (kubo_mouse_snap) { + mouse_pos = mouse_grid_snap(mouse_pos); + } + window_render(context, mouse_pos); kubo_camera_zoom(&camera); if ((context->state.id == KUBO_CONTEXT_NORMAL && @@ -54,7 +62,7 @@ void kubo_window_tick(struct kubo_context *context) { } if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { - window_left_mouse(context); + window_left_mouse(context, mouse_pos); } if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) { window_right_mouse(context); @@ -62,7 +70,7 @@ void kubo_window_tick(struct kubo_context *context) { kubo_input_handle(context, &camera); } -static void window_render(struct kubo_context *context) { +static void window_render(struct kubo_context *context, Vector2 mouse_pos) { BeginDrawing(); BeginMode2D(camera); ClearBackground(WHITE); @@ -72,20 +80,36 @@ static void window_render(struct kubo_context *context) { assert(wall != NULL); kubo_wall_render(wall, context->state.id == KUBO_CONTEXT_WALL_SELECT, - &camera); + &camera, mouse_pos); } + rlPushMatrix(); + rlTranslatef(0, 25 * 50, 0); + rlRotatef(90, 1, 0, 0); + DrawGrid(100, KUBO_GRID_SIZE); + rlPopMatrix(); + EndMode2D(); - kubo_bar_render(context); + Vector2 mouse_cam = GetWorldToScreen2D(mouse_pos, camera); + + switch (context->state.id) { + case KUBO_CONTEXT_WALL_NEW: + DrawCircle(mouse_cam.x, mouse_cam.y, 10, BLUE); + break; + default: + break; + } + + kubo_bar_render(context, kubo_mouse_snap); EndDrawing(); } -static void window_left_mouse(struct kubo_context *context) { +static void window_left_mouse(struct kubo_context *context, Vector2 mouse_pos) { switch (context->state.id) { case KUBO_CONTEXT_WALL_NEW: - new_wall_click(context); + new_wall_click(context, mouse_pos); break; case KUBO_CONTEXT_WALL_SELECT: break; @@ -106,15 +130,14 @@ static void window_right_mouse(struct kubo_context *context) { } } -static void new_wall_click(struct kubo_context *context) { +static void new_wall_click(struct kubo_context *context, Vector2 mouse_pos) { struct kubo_wall *current_wall = kubo_context_get_pending_wall(context); if (!current_wall) { return; } - Vector2 new_pos = GetScreenToWorld2D(GetMousePosition(), camera); - kubo_wall_add_vertex(current_wall, new_pos); + kubo_wall_add_vertex(current_wall, mouse_pos); current_wall->state = KUBO_WALL_DRAWING; } @@ -127,3 +150,11 @@ static void new_wall_end(struct kubo_context *context) { current_wall->state = KUBO_WALL_DONE; } + +static Vector2 mouse_grid_snap(Vector2 mouse_pos) { + Vector2 snap_pos = { + .x = round(mouse_pos.x / KUBO_GRID_SIZE) * KUBO_GRID_SIZE, + .y = round(mouse_pos.y / KUBO_GRID_SIZE) * KUBO_GRID_SIZE}; + + return snap_pos; +} diff --git a/kubo_window.h b/kubo_window.h index 2f9867b..e16834c 100644 --- a/kubo_window.h +++ b/kubo_window.h @@ -20,6 +20,7 @@ #define KUBO_WINDOW_H #include +#include #include "kubo_bar.h" #include "kubo_camera.h" @@ -31,6 +32,8 @@ #define KUBO_TARGET_FPS 165 +#define KUBO_GRID_SIZE 50 + void kubo_window_init(); void kubo_window_cleanup();