grid and mouse snapping
This commit is contained in:
parent
736829a119
commit
61986f41fa
7 changed files with 72 additions and 19 deletions
16
kubo_bar.c
16
kubo_bar.c
|
|
@ -18,9 +18,12 @@
|
||||||
|
|
||||||
#include "kubo_bar.h"
|
#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);
|
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;
|
const int bar_y = GetScreenHeight() - KUBO_BAR_HEIGHT;
|
||||||
int bar_text_x = 10;
|
int bar_text_x = 10;
|
||||||
|
|
||||||
|
|
@ -30,9 +33,18 @@ void kubo_bar_render(struct kubo_context *context) {
|
||||||
|
|
||||||
bar_text_x += 2 * KUBO_BAR_PADDING;
|
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_command_bar_render(bar_text_x, bar_y + (KUBO_BAR_PADDING / 2),
|
||||||
KUBO_BAR_HEIGHT - KUBO_BAR_PADDING);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,6 @@
|
||||||
#include "kubo_context.h"
|
#include "kubo_context.h"
|
||||||
#include "kubo_command_bar.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
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,8 @@
|
||||||
|
|
||||||
#include "kubo_input.h"
|
#include "kubo_input.h"
|
||||||
|
|
||||||
|
extern bool kubo_mouse_snap;
|
||||||
|
|
||||||
static void key_input(struct kubo_context *context, Camera2D *camera);
|
static void key_input(struct kubo_context *context, Camera2D *camera);
|
||||||
static void char_input(struct kubo_context *context);
|
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);
|
kubo_context_delete_wall(context);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case KEY_T:
|
||||||
|
kubo_mouse_snap = !kubo_mouse_snap;
|
||||||
|
break;
|
||||||
|
|
||||||
case KEY_SPACE:
|
case KEY_SPACE:
|
||||||
kubo_camera_reset(camera);
|
kubo_camera_reset(camera);
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,8 @@ Vector2 kubo_wall_get_vertex(struct kubo_wall *wall, size_t index) {
|
||||||
return kubo_vector2_arr_get(&wall->vertices, 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 =
|
Color wall_color =
|
||||||
(select && wall->state == KUBO_WALL_SELECTED) ? BLUE : BLACK;
|
(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);
|
DrawSplineLinear(points, wall->vertices.count, 10.f, wall_color);
|
||||||
|
|
||||||
if (wall->state == KUBO_WALL_DRAWING) {
|
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};
|
Vector2 mouse_points[] = {points[wall->vertices.count - 1], mouse};
|
||||||
|
|
||||||
DrawSplineLinear(mouse_points, 2, 10.f, wall_color);
|
DrawSplineLinear(mouse_points, 2, 10.f, wall_color);
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,6 @@ void kubo_wall_cleanup(struct kubo_wall *wall);
|
||||||
bool kubo_wall_add_vertex(struct kubo_wall *wall, Vector2 vec);
|
bool kubo_wall_add_vertex(struct kubo_wall *wall, Vector2 vec);
|
||||||
Vector2 kubo_wall_get_vertex(struct kubo_wall *wall, size_t index);
|
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
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -17,16 +17,20 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "kubo_window.h"
|
#include "kubo_window.h"
|
||||||
|
#include "raylib.h"
|
||||||
|
|
||||||
Camera2D camera = {0};
|
Camera2D camera = {0};
|
||||||
|
bool kubo_mouse_snap = false;
|
||||||
|
|
||||||
static void window_render(struct kubo_context *context);
|
static void window_render(struct kubo_context *context, Vector2 mouse_pos);
|
||||||
static void window_left_mouse(struct kubo_context *context);
|
static void window_left_mouse(struct kubo_context *context, Vector2 mouse_pos);
|
||||||
static void window_right_mouse(struct kubo_context *context);
|
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 void new_wall_end(struct kubo_context *context);
|
||||||
|
|
||||||
|
static Vector2 mouse_grid_snap(Vector2 mouse_pos);
|
||||||
|
|
||||||
void kubo_window_init() {
|
void kubo_window_init() {
|
||||||
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
|
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
|
||||||
InitWindow(KUBO_WINDOW_WIDTH, KUBO_WINDOW_HEIGHT, "Kubo");
|
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) {
|
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);
|
kubo_camera_zoom(&camera);
|
||||||
if ((context->state.id == KUBO_CONTEXT_NORMAL &&
|
if ((context->state.id == KUBO_CONTEXT_NORMAL &&
|
||||||
|
|
@ -54,7 +62,7 @@ void kubo_window_tick(struct kubo_context *context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
|
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
|
||||||
window_left_mouse(context);
|
window_left_mouse(context, mouse_pos);
|
||||||
}
|
}
|
||||||
if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) {
|
if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) {
|
||||||
window_right_mouse(context);
|
window_right_mouse(context);
|
||||||
|
|
@ -62,7 +70,7 @@ void kubo_window_tick(struct kubo_context *context) {
|
||||||
kubo_input_handle(context, &camera);
|
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();
|
BeginDrawing();
|
||||||
BeginMode2D(camera);
|
BeginMode2D(camera);
|
||||||
ClearBackground(WHITE);
|
ClearBackground(WHITE);
|
||||||
|
|
@ -72,20 +80,36 @@ static void window_render(struct kubo_context *context) {
|
||||||
assert(wall != NULL);
|
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,
|
||||||
&camera);
|
&camera, mouse_pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rlPushMatrix();
|
||||||
|
rlTranslatef(0, 25 * 50, 0);
|
||||||
|
rlRotatef(90, 1, 0, 0);
|
||||||
|
DrawGrid(100, KUBO_GRID_SIZE);
|
||||||
|
rlPopMatrix();
|
||||||
|
|
||||||
EndMode2D();
|
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();
|
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) {
|
switch (context->state.id) {
|
||||||
case KUBO_CONTEXT_WALL_NEW:
|
case KUBO_CONTEXT_WALL_NEW:
|
||||||
new_wall_click(context);
|
new_wall_click(context, mouse_pos);
|
||||||
break;
|
break;
|
||||||
case KUBO_CONTEXT_WALL_SELECT:
|
case KUBO_CONTEXT_WALL_SELECT:
|
||||||
break;
|
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);
|
struct kubo_wall *current_wall = kubo_context_get_pending_wall(context);
|
||||||
|
|
||||||
if (!current_wall) {
|
if (!current_wall) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector2 new_pos = GetScreenToWorld2D(GetMousePosition(), camera);
|
kubo_wall_add_vertex(current_wall, mouse_pos);
|
||||||
kubo_wall_add_vertex(current_wall, new_pos);
|
|
||||||
current_wall->state = KUBO_WALL_DRAWING;
|
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;
|
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;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@
|
||||||
#define KUBO_WINDOW_H
|
#define KUBO_WINDOW_H
|
||||||
|
|
||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
|
#include <rlgl.h>
|
||||||
|
|
||||||
#include "kubo_bar.h"
|
#include "kubo_bar.h"
|
||||||
#include "kubo_camera.h"
|
#include "kubo_camera.h"
|
||||||
|
|
@ -31,6 +32,8 @@
|
||||||
|
|
||||||
#define KUBO_TARGET_FPS 165
|
#define KUBO_TARGET_FPS 165
|
||||||
|
|
||||||
|
#define KUBO_GRID_SIZE 50
|
||||||
|
|
||||||
void kubo_window_init();
|
void kubo_window_init();
|
||||||
void kubo_window_cleanup();
|
void kubo_window_cleanup();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue