From 7189f28c00c527d4e5e23424a9f1a486be755799 Mon Sep 17 00:00:00 2001 From: Luka Jankovic Date: Sat, 26 Jul 2025 23:03:27 +0200 Subject: [PATCH] panning --- README.md | 3 ++- kubo_context.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++---- kubo_context.h | 11 +++++++-- kubo_input.c | 15 +++++++++--- kubo_wall.c | 25 +++++++++++++------ kubo_wall.h | 7 +++--- kubo_window.c | 5 +++- scene.yaml | 40 ++++++++++++++++++++++-------- 8 files changed, 139 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 997f3e6..ea2e990 100644 --- a/README.md +++ b/README.md @@ -25,10 +25,11 @@ $ ./kubo * wall rendering * scene file management * basic command framework +* pan (teseting needed) ### In progress -* pan +* fix scene load active wall bug ### TODO diff --git a/kubo_context.c b/kubo_context.c index 42ce2ed..4e0d87e 100644 --- a/kubo_context.c +++ b/kubo_context.c @@ -25,6 +25,8 @@ static const struct kubo_context_state_data kubo_context_states[] = { {.id = KUBO_CONTEXT_WALL_SELECT, .label = "Wall Select", .color = BLUE}}; static inline void wall_select_refresh(struct kubo_context *context); +static void select_next_wall(struct kubo_context *context); +static void select_prev_wall(struct kubo_context *context); void kubo_context_init(struct kubo_context *context) { if (!context) { @@ -36,6 +38,9 @@ void kubo_context_init(struct kubo_context *context) { context->exit_pending = false; context->state = kubo_context_states[KUBO_CONTEXT_NORMAL]; + + context->offset_x = 0; + context->offset_y = 0; } void kubo_context_cleanup(struct kubo_context *context) { @@ -95,12 +100,63 @@ void kubo_context_delete_wall(struct kubo_context *context) { } kubo_wall_arr_del(&context->walls, context->wall_select_index); - kubo_context_select_next_wall(context); + select_next_wall(context); } -void kubo_context_select_next_wall(struct kubo_context *context) { - if (context->state.id != KUBO_CONTEXT_WALL_SELECT || - !context->walls.count) { +void kubo_context_input_up(struct kubo_context *context) { + switch (context->state.id) { + case KUBO_CONTEXT_NORMAL: + context->offset_y -= KUBO_CONTEXT_OFFSET_JMP; + break; + case KUBO_CONTEXT_WALL_SELECT: + select_next_wall(context); + break; + default: + break; + } +} + +void kubo_context_input_down(struct kubo_context *context) { + switch (context->state.id) { + case KUBO_CONTEXT_NORMAL: + context->offset_y += KUBO_CONTEXT_OFFSET_JMP; + break; + case KUBO_CONTEXT_WALL_SELECT: + select_prev_wall(context); + break; + default: + break; + } +} + +void kubo_context_input_left(struct kubo_context *context) { + switch (context->state.id) { + case KUBO_CONTEXT_NORMAL: + context->offset_x -= KUBO_CONTEXT_OFFSET_JMP; + break; + case KUBO_CONTEXT_WALL_SELECT: + select_prev_wall(context); + break; + default: + break; + } +} + +void kubo_context_input_right(struct kubo_context *context) { + switch (context->state.id) { + case KUBO_CONTEXT_NORMAL: + context->offset_x += KUBO_CONTEXT_OFFSET_JMP; + break; + case KUBO_CONTEXT_WALL_SELECT: + select_next_wall(context); + break; + default: + break; + } +} + +static void select_next_wall(struct kubo_context *context) { + if (!context->walls.count) { return; } @@ -110,7 +166,7 @@ void kubo_context_select_next_wall(struct kubo_context *context) { wall_select_refresh(context); } -void kubo_context_select_prev_wall(struct kubo_context *context) { +static void select_prev_wall(struct kubo_context *context) { if (context->state.id != KUBO_CONTEXT_WALL_SELECT || !context->walls.count) { return; diff --git a/kubo_context.h b/kubo_context.h index ffc6318..cd1cf5f 100644 --- a/kubo_context.h +++ b/kubo_context.h @@ -27,6 +27,8 @@ #include "kubo_dynarray.h" #include "kubo_wall.h" +#define KUBO_CONTEXT_OFFSET_JMP 100 + KUBO_DYNARRAY_REGISTER(kubo_wall_arr, struct kubo_wall *) enum kubo_context_state { @@ -49,6 +51,9 @@ struct kubo_context { struct kubo_wall_arr walls; struct kubo_context_state_data state; + int offset_x; + int offset_y; + // KUBO_CONTEXT_WALL_SELECT size_t wall_select_index; }; @@ -64,7 +69,9 @@ void kubo_context_accept_cmd(struct kubo_context *context); struct kubo_wall *kubo_context_get_pending_wall(struct kubo_context *context); void kubo_context_delete_wall(struct kubo_context *context); -void kubo_context_select_next_wall(struct kubo_context *context); -void kubo_context_select_prev_wall(struct kubo_context *context); +void kubo_context_input_up(struct kubo_context *context); +void kubo_context_input_down(struct kubo_context *context); +void kubo_context_input_left(struct kubo_context *context); +void kubo_context_input_right(struct kubo_context *context); #endif diff --git a/kubo_input.c b/kubo_input.c index 5387429..41b443b 100644 --- a/kubo_input.c +++ b/kubo_input.c @@ -51,21 +51,28 @@ static void key_input(struct kubo_context *context) { break; case KEY_RIGHT: - case KEY_UP: case KEY_L: + kubo_context_input_right(context); + break; + + case KEY_UP: case KEY_K: - kubo_context_select_next_wall(context); + kubo_context_input_up(context); break; case KEY_LEFT: + case KEY_H: + kubo_context_input_left(context); + break; + case KEY_DOWN: case KEY_J: - case KEY_H: - kubo_context_select_prev_wall(context); + kubo_context_input_down(context); break; case KEY_X: kubo_context_delete_wall(context); + break; default: break; diff --git a/kubo_wall.c b/kubo_wall.c index 4539641..53e9d1d 100644 --- a/kubo_wall.c +++ b/kubo_wall.c @@ -40,18 +40,29 @@ 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) { - +void kubo_wall_render(struct kubo_wall *wall, bool select, int offset_x, + int offset_y) { Color wall_color = (select && wall->state == KUBO_WALL_SELECTED) ? BLUE : BLACK; - DrawSplineLinear(wall->vertices.data, wall->vertices.count, 10.f, - wall_color); + Vector2 *points = malloc(sizeof(Vector2) * wall->vertices.count); + + for (size_t i = 0; i < wall->vertices.count; i++) { + + points[i] = kubo_vector2_arr_get(&wall->vertices, i); + + points[i].x += offset_x; + points[i].y += offset_y; + } + + DrawSplineLinear(points, wall->vertices.count, 10.f, wall_color); if (wall->state == KUBO_WALL_DRAWING) { Vector2 mouse = GetMousePosition(); - Vector2 points[] = {wall->vertices.data[wall->vertices.count - 1], - mouse}; - DrawSplineLinear(points, 2, 10.f, wall_color); + Vector2 mouse_points[] = {points[wall->vertices.count - 1], mouse}; + + DrawSplineLinear(mouse_points, 2, 10.f, wall_color); } + + free(points); } diff --git a/kubo_wall.h b/kubo_wall.h index d9001ae..2373428 100644 --- a/kubo_wall.h +++ b/kubo_wall.h @@ -19,8 +19,9 @@ #ifndef KUBO_WALL_H #define KUBO_WALL_H -#include #include +#include +#include #include "kubo_dynarray.h" @@ -46,7 +47,7 @@ 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); +void kubo_wall_render(struct kubo_wall *wall, bool select, int offset_x, + int offset_y); #endif - diff --git a/kubo_window.c b/kubo_window.c index 25b2f60..4489ec2 100644 --- a/kubo_window.c +++ b/kubo_window.c @@ -58,7 +58,8 @@ static void window_render(struct kubo_context *context) { struct kubo_wall *wall = kubo_wall_arr_get(&context->walls, i); assert(wall != NULL); - kubo_wall_render(wall, context->state.id == KUBO_CONTEXT_WALL_SELECT); + kubo_wall_render(wall, context->state.id == KUBO_CONTEXT_WALL_SELECT, + context->offset_x, context->offset_y); } kubo_bar_render(context); @@ -99,6 +100,8 @@ static void new_wall_click(struct kubo_context *context) { int bar_limit = GetScreenHeight() - KUBO_BAR_HEIGHT; Vector2 new_pos = GetMousePosition(); + new_pos.x -= context->offset_x; + new_pos.y -= context->offset_y; if (new_pos.y < bar_limit) { kubo_wall_add_vertex(current_wall, new_pos); current_wall->state = KUBO_WALL_DRAWING; diff --git a/scene.yaml b/scene.yaml index 2a426ec..e386daa 100644 --- a/scene.yaml +++ b/scene.yaml @@ -1,11 +1,31 @@ walls: - - vertices: - - [100, 100] - - [100, 200] - - vertices: - - [100, 300] - - [100, 500] - - [200, 500] - - vertices: - - [300, 300] - - [500, 500] +- vertices: + - - 100 + - 100 + - - 100 + - 200 +- vertices: + - - 100 + - 300 + - - 100 + - 500 + - - 200 + - 500 +- vertices: + - - 300 + - 300 + - - 500 + - 500 + - - 594 + - 420 +- vertices: + - - 589 + - 276 + - - 710 + - 366 + - - 616 + - 453 + - - 729 + - 493 + - - 644 + - 594