grid and mouse snapping
This commit is contained in:
parent
736829a119
commit
61986f41fa
7 changed files with 72 additions and 19 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue