Skip to content

Commit

Permalink
Merge cursor_t struct into gesture3d struct
Browse files Browse the repository at this point in the history
The difference between the two was not very clear.  Might change this
again in the future, until I find a clean way.
  • Loading branch information
guillaumechereau committed Jul 1, 2024
1 parent 2e9a28c commit 74cb083
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 132 deletions.
12 changes: 6 additions & 6 deletions src/box_edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,19 @@ static void highlight_face(const float box[4][4], int face)
render_rect_fill(&goxel.rend, plane, color);
}

static int on_hover(gesture3d_t *gest, const cursor_t *curs, void *user)
static int on_hover(gesture3d_t *gest, void *user)
{
data_t *data = (void*)user;

goxel_set_help_text("Drag to move face");
data->flags |= FLAG_SNAPED;
data->snap_face = get_face(curs->normal);
data->snap_face = get_face(gest->normal);
highlight_face(data->box, data->snap_face);
render_gizmo(data->box, data->snap_face);
return 0;
}

static int on_drag(gesture3d_t *gest, const cursor_t *curs, void *user)
static int on_drag(gesture3d_t *gest, void *user)
{
data_t *data = (void*)user;
float face_plane[4][4], v[3], pos[3], n[3], d[3], ofs[3], box[4][4];
Expand All @@ -114,17 +114,17 @@ static int on_drag(gesture3d_t *gest, const cursor_t *curs, void *user)
if (gest->state == GESTURE3D_STATE_BEGIN) {
data->flags |= FLAG_FIRST;
mat4_copy(data->box, data->start_box);
data->snap_face = get_face(curs->normal);
data->snap_face = get_face(gest->normal);
mat4_mul(data->box, FACES_MATS[data->snap_face], face_plane);
vec3_normalize(face_plane[0], v);
gest->snap_mask = SNAP_SHAPE_PLANE;
plane_from_vectors(gest->snap_shape, curs->pos, curs->normal, v);
plane_from_vectors(gest->snap_shape, gest->pos, gest->normal, v);
return 0;
}

mat4_mul(data->start_box, FACES_MATS[data->snap_face], face_plane);
vec3_normalize(face_plane[2], n);
vec3_sub(curs->pos, gest->snap_shape[3], v);
vec3_sub(gest->pos, gest->snap_shape[3], v);
vec3_project(v, n, v);
vec3_add(gest->snap_shape[3], v, pos);
pos[0] = round(pos[0]);
Expand Down
28 changes: 14 additions & 14 deletions src/gesture3d.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

#include "goxel.h"

int gesture3d(gesture3d_t *gest, cursor_t *curs, void *user)
int gesture3d(gesture3d_t *gest, void *user)
{
bool pressed = curs->flags & GESTURE3D_FLAG_PRESSED;
bool pressed = gest->flags & GESTURE3D_FLAG_PRESSED;
int r, ret = 0;
const int btns_mask = GESTURE3D_FLAG_CTRL;

Expand All @@ -30,9 +30,9 @@ int gesture3d(gesture3d_t *gest, cursor_t *curs, void *user)
if (gest->type == GESTURE3D_TYPE_DRAG) {
switch (gest->state) {
case GESTURE3D_STATE_POSSIBLE:
if ((gest->buttons & btns_mask) != (curs->flags & btns_mask))
if ((gest->buttons & btns_mask) != (gest->flags & btns_mask))
break;
if (curs->snaped && pressed) {
if (gest->snaped && pressed) {
gest->state = GESTURE3D_STATE_BEGIN;
}
break;
Expand All @@ -52,14 +52,14 @@ int gesture3d(gesture3d_t *gest, cursor_t *curs, void *user)
if (gest->type == GESTURE3D_TYPE_CLICK) {
switch (gest->state) {
case GESTURE3D_STATE_POSSIBLE:
if (curs->snaped && !pressed) {
if (gest->snaped && !pressed) {
gest->state = GESTURE3D_STATE_RECOGNISED;
}
break;
case GESTURE3D_STATE_RECOGNISED:
if ((gest->buttons & btns_mask) != (curs->flags & btns_mask))
if ((gest->buttons & btns_mask) != (gest->flags & btns_mask))
break;
if (curs->snaped && pressed) {
if (gest->snaped && pressed) {
gest->state = GESTURE3D_STATE_TRIGGERED;
}
break;
Expand All @@ -72,26 +72,26 @@ int gesture3d(gesture3d_t *gest, cursor_t *curs, void *user)
if (!DEFINED(GOXEL_MOBILE) && gest->type == GESTURE3D_TYPE_HOVER) {
switch (gest->state) {
case GESTURE3D_STATE_POSSIBLE:
if ((gest->buttons & btns_mask) != (curs->flags & btns_mask))
if ((gest->buttons & btns_mask) != (gest->flags & btns_mask))
break;
if (curs->snaped && !pressed &&
!(curs->flags & GESTURE3D_FLAG_OUT)) {
if (gest->snaped && !pressed &&
!(gest->flags & GESTURE3D_FLAG_OUT)) {
gest->state = GESTURE3D_STATE_BEGIN;
}
break;
case GESTURE3D_STATE_BEGIN:
case GESTURE3D_STATE_UPDATE:
gest->state = GESTURE3D_STATE_UPDATE;
if ((gest->buttons & btns_mask) != (curs->flags & btns_mask)) {
if ((gest->buttons & btns_mask) != (gest->flags & btns_mask)) {
gest->state = GESTURE3D_STATE_END;
}
if (pressed) {
gest->state = GESTURE3D_STATE_END;
}
if (!curs->snaped) {
if (!gest->snaped) {
gest->state = GESTURE3D_STATE_END;
}
if (curs->flags & GESTURE3D_FLAG_OUT) {
if (gest->flags & GESTURE3D_FLAG_OUT) {
gest->state = GESTURE3D_STATE_END;
}
break;
Expand All @@ -106,7 +106,7 @@ int gesture3d(gesture3d_t *gest, cursor_t *curs, void *user)
gest->state == GESTURE3D_STATE_END ||
gest->state == GESTURE3D_STATE_TRIGGERED)
{
r = gest->callback(gest, curs, user);
r = gest->callback(gest, user);
if (r == GESTURE3D_STATE_FAILED) {
gest->state = GESTURE3D_STATE_FAILED;
ret = 0;
Expand Down
25 changes: 10 additions & 15 deletions src/gesture3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,32 @@ enum {
GESTURE3D_STATE_FAILED,
};

// Represent a 3d cursor.
// The program keeps track of two cursors, that are then used by the tools.
enum {
// The state flags of the cursor.
GESTURE3D_FLAG_PRESSED = 1 << 0,
GESTURE3D_FLAG_SHIFT = 1 << 1,
GESTURE3D_FLAG_CTRL = 1 << 2,
GESTURE3D_FLAG_OUT = 1 << 3, // Outside of sensing area.
};

typedef struct cursor {
float pos[3];
float normal[3];
int snaped;
int flags;
} cursor_t;

// #### 3d gestures
struct gesture3d
{
int type;
int state;
int buttons; // CURSOR_SHIFT | CURSOR_CTRL
int buttons; // GESTURE3D_FLAG_SHIFT | GESTURE3D_FLAG_CTRL
int snap_mask;
float snap_offset;
float snap_shape[4][4]; // Used for plane snapping.
int (*callback)(gesture3d_t *gest, const cursor_t *curs,
void *user);
int (*callback)(gesture3d_t *gest, void *user);
void *user;

// Automatically updated attributes.
int state;
float pos[3];
float normal[3];
int snaped;
int flags;
};

int gesture3d(gesture3d_t *gest, cursor_t *curs, void *user);
int gesture3d(gesture3d_t *gest, void *user);

#endif // GESTURE3D_H
52 changes: 26 additions & 26 deletions src/goxel.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,6 @@ bool goxel_gesture3d(const gesture3d_t *gesture)
bool ret;
gesture3d_t *gest;
typeof(goxel.gesture3ds[0]) *slot = NULL;
cursor_t *curs;

// Search if we already have a different active gesture.
for (i = 0; i < goxel.gesture3ds_count; i++) {
Expand Down Expand Up @@ -562,12 +561,17 @@ bool goxel_gesture3d(const gesture3d_t *gesture)
// Update the gesture data until it has started. That way we can
// modify the gesture inside its callback function.
if (slot->gesture.state == 0) {
slot->gesture = *gesture;
slot->gesture.type = gesture->type;
slot->gesture.buttons = gesture->buttons;
slot->gesture.snap_mask = gesture->snap_mask;
slot->gesture.snap_offset = gesture->snap_offset;
mat4_copy(gesture->snap_shape, slot->gesture.snap_shape);
slot->gesture.callback = gesture->callback;
slot->gesture.user = gesture->user;
}

slot->alive = true;
curs = &slot->cursor;
ret = gesture3d(&slot->gesture, curs, gesture->user);
ret = gesture3d(&slot->gesture, gesture->user);
return ret;
}

Expand Down Expand Up @@ -670,35 +674,33 @@ int goxel_iter(const inputs_t *inputs)
return goxel.quit ? 1 : 0;
}

static void set_cursor_hint(cursor_t *curs)
static void set_cursor_hint(const gesture3d_t *gest)
{
if (!curs->snaped) {
if (!gest->snaped) {
goxel_set_hint_text(NULL);
return;
}
goxel_set_hint_text("[%.0f %.0f %.0f]",
curs->pos[0] - 0.5, curs->pos[1] - 0.5, curs->pos[2] - 0.5);
gest->pos[0] - 0.5, gest->pos[1] - 0.5, gest->pos[2] - 0.5);
}

static int on_drag(const gesture_t *gest, void *user)
{
int i;
gesture3d_t *gest3d;
cursor_t *c;

for (i = 0; i < goxel.gesture3ds_count; i++) {
gest3d = &goxel.gesture3ds[i].gesture;
c = &goxel.gesture3ds[i].cursor;
if (gest->state == GESTURE_BEGIN)
c->flags |= GESTURE3D_FLAG_PRESSED;
gest3d->flags |= GESTURE3D_FLAG_PRESSED;
if (gest->state == GESTURE_END)
c->flags &= ~GESTURE3D_FLAG_PRESSED;
gest3d->flags &= ~GESTURE3D_FLAG_PRESSED;

c->snaped = goxel_unproject(
gest3d->snaped = goxel_unproject(
gest->viewport, gest->pos, gest3d->snap_mask,
gest3d->snap_shape, gest3d->snap_offset,
c->pos, c->normal);
set_cursor_hint(c);
gest3d->pos, gest3d->normal);
set_cursor_hint(gest3d);
}
return 0;
}
Expand Down Expand Up @@ -809,18 +811,17 @@ static int on_hover(const gesture_t *gest, void *user)
{
int i;
gesture3d_t *gest3d;
cursor_t *c;

for (i = 0; i < goxel.gesture3ds_count; i++) {
gest3d = &goxel.gesture3ds[i].gesture;
c = &goxel.gesture3ds[i].cursor;
c->snaped = goxel_unproject(
gest3d->snaped = goxel_unproject(
gest->viewport, gest->pos, gest3d->snap_mask,
gest3d->snap_shape, gest3d->snap_offset,
c->pos, c->normal);
set_cursor_hint(c);
c->flags &= ~GESTURE3D_FLAG_PRESSED;
set_flag(&c->flags, GESTURE3D_FLAG_OUT, gest->state == GESTURE_END);
gest3d->pos, gest3d->normal);
set_cursor_hint(gest3d);
gest3d->flags &= ~GESTURE3D_FLAG_PRESSED;
set_flag(&gest3d->flags, GESTURE3D_FLAG_OUT,
gest->state == GESTURE_END);
}
return 0;
}
Expand All @@ -831,19 +832,18 @@ void goxel_mouse_in_view(const float viewport[4], const inputs_t *inputs,
{
float p[3], n[3];
camera_t *camera = get_camera();
gesture3d_t *gest3d;
int i;
cursor_t *curs;


painter_t painter = goxel.painter;
gesture_update(goxel.gestures_count, goxel.gestures,
inputs, viewport, NULL);

for (i = 0; i < goxel.gesture3ds_count; i++) {
curs = &goxel.gesture3ds[i].cursor;
set_flag(&curs->flags, GESTURE3D_FLAG_SHIFT,
gest3d = &goxel.gesture3ds[i].gesture;
set_flag(&gest3d->flags, GESTURE3D_FLAG_SHIFT,
inputs->keys[KEY_LEFT_SHIFT]);
set_flag(&curs->flags, GESTURE3D_FLAG_CTRL,
set_flag(&gest3d->flags, GESTURE3D_FLAG_CTRL,
inputs->keys[KEY_CONTROL]);
}

Expand Down
1 change: 0 additions & 1 deletion src/goxel.h
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,6 @@ typedef struct goxel
// All the 3d gestures we listen to.
struct {
gesture3d_t gesture;
cursor_t cursor;
bool alive;
} gesture3ds[16];
int gesture3ds_count;
Expand Down
13 changes: 6 additions & 7 deletions src/tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,20 @@ const tool_t *tool_get(int id)
return g_tools[id];
}

static int pick_color_gesture(gesture3d_t *gest, const cursor_t *curs,
void *user)
static int pick_color_gesture(gesture3d_t *gest, void *user)
{
const volume_t *volume = goxel_get_layers_volume(goxel.image);
int pi[3] = {floor(curs->pos[0]),
floor(curs->pos[1]),
floor(curs->pos[2])};
int pi[3] = {floor(gest->pos[0]),
floor(gest->pos[1]),
floor(gest->pos[2])};
uint8_t color[4];

goxel_set_help_text("Click on a voxel to pick the color");
if (!curs->snaped) return 0;
if (!gest->snaped) return 0;
volume_get_at(volume, NULL, pi, color);
color[3] = 255;
goxel_set_help_text("pick: %d %d %d", color[0], color[1], color[2]);
if (curs->flags & GESTURE3D_FLAG_PRESSED) {
if (gest->flags & GESTURE3D_FLAG_PRESSED) {
vec4_copy(color, goxel.painter.color);
}
return 0;
Expand Down
Loading

0 comments on commit 74cb083

Please sign in to comment.