Skip to content

Commit

Permalink
Redo plane tool UI
Browse files Browse the repository at this point in the history
Remove the 'relative' mode.  Instead add a 'custom' rotation checkbox
that allows to set the Euler angles manually.
  • Loading branch information
guillaumechereau committed Jan 21, 2024
1 parent 6400907 commit 4f006bb
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 40 deletions.
47 changes: 42 additions & 5 deletions src/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ void gui_enabled_end(void)
}

// To remove?
static bool gui_quat(const char *label, float q[4])
static bool gui_quat(float q[4])
{
// Hack to prevent weird behavior when we change the euler angles.
// We keep track of the last used euler angles value and reuse them if
Expand All @@ -1368,11 +1368,9 @@ static bool gui_quat(const char *label, float q[4])
vec3_copy(last.eul, eul);
else
quat_to_eul(q, EULER_ORDER_DEFAULT, eul);
gui_group_begin(label);
if (gui_angle("x", &eul[0], 0, 0)) ret = true;
if (gui_angle("y", &eul[1], 0, 0)) ret = true;
if (gui_angle("z", &eul[2], 0, 0)) ret = true;
gui_group_end();

if (ret) {
eul_to_quat(eul, EULER_ORDER_DEFAULT, q);
Expand All @@ -1382,15 +1380,15 @@ static bool gui_quat(const char *label, float q[4])
return ret;
}

bool gui_rotation_mat4(const char *label, float m[4][4])
bool gui_rotation_mat4(float m[4][4])
{
float rot[3][3], quat[4];
bool ret = false;
int i, j;

mat4_to_mat3(m, rot);
mat3_to_quat(rot, quat);
if (gui_quat(label, quat)) {
if (gui_quat(quat)) {
quat_to_mat3(quat, rot);
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
Expand All @@ -1400,6 +1398,45 @@ bool gui_rotation_mat4(const char *label, float m[4][4])
return ret;
}

bool gui_rotation_mat4_axis(float m[4][4])
{
float rot[3][3];
bool ret = false;
bool v;
int i, j;
const struct {
const char *label;
float rot[3][3];
} axis[] = {
{"+X", {{0, 0, -1}, {0, 1, 0}, {1, 0, 0}}},
{"-X", {{0, 0, 1}, {0, 1, 0}, {-1, 0, 0}}},
{"+Y", {{1, 0, 0}, {0, 0, -1}, {0, 1, 0}}},
{"-Y", {{1, 0, 0}, {0, 0, 1}, {-1, 0, 0}}},
{"+Z", {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}},
{"-Z", {{1, 0, 0}, {0, -1, 0}, {0, 0, -1}}},
};

mat4_to_mat3(m, rot);

for (i = 0; i < 3; i++) {
gui_row_begin(2);
for (j = 0; j < 2; j++) {
v = memcmp(rot, axis[i * 2 + j].rot, sizeof(rot)) == 0;
if (gui_selectable(axis[i * 2 + j].label, &v, NULL, -1)) {
memcpy(rot, axis[i * 2 + j].rot, sizeof(rot));
ret = true;
}
}
gui_row_end();
}
if (ret) {
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
m[i][j] = rot[i][j];
}
return ret;
}

void gui_open_popup(const char *title, int flags, void *data,
int (*func)(void *data))
{
Expand Down
3 changes: 2 additions & 1 deletion src/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ bool gui_input_float(const char *label, float *v, float step,
float minv, float maxv, const char *format);
bool gui_angle(const char *id, float *v, int vmin, int vmax);
bool gui_bbox(float box[4][4]);
bool gui_rotation_mat4(const char *label, float m[4][4]);
bool gui_rotation_mat4(float m[4][4]);
bool gui_rotation_mat4_axis(float m[4][4]);
bool gui_action_button(int id, const char *label, float size);
bool gui_selectable(const char *name, bool *v, const char *tooltip, float w);
bool gui_selectable_toggle(const char *name, int *v, int set_v,
Expand Down
50 changes: 16 additions & 34 deletions src/tools/plane.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

typedef struct {
tool_t tool;
int move_mode;
bool custom_rotation;
} tool_plane_t;

static int iter(tool_t *tool, const painter_t *painter,
Expand Down Expand Up @@ -73,7 +73,6 @@ static void cut(bool above)

static int gui(tool_t *tool_)
{
int i;
bool v;
int x, y, z;

Expand All @@ -83,38 +82,21 @@ static int gui(tool_t *tool_)
set_flag(&goxel.snap_mask, SNAP_PLANE, v);
}

gui_combo("Move", &tool->move_mode, (const char*[]) {
"Relative", "Absolute"}, 2);

switch (tool->move_mode) {
case 0:
gui_group_begin(NULL);
i = 0;
if (gui_input_int("Move", &i, 0, 0))
mat4_itranslate(goxel.plane, 0, 0, -i);
i = 0;
if (gui_input_int("Rot X", &i, 0, 0))
mat4_irotate(goxel.plane, i * M_PI / 2, 1, 0, 0);
i = 0;
if (gui_input_int("Rot Y", &i, 0, 0))
mat4_irotate(goxel.plane, i * M_PI / 2, 0, 1, 0);
gui_group_end();
break;

case 1:

x = (int)round(goxel.plane[3][0]);
y = (int)round(goxel.plane[3][1]);
z = (int)round(goxel.plane[3][2]);
gui_group_begin("Origin");
if (gui_input_int("X", &x, 0, 0)) goxel.plane[3][0] = x;
if (gui_input_int("Y", &y, 0, 0)) goxel.plane[3][1] = y;
if (gui_input_int("Z", &z, 0, 0)) goxel.plane[3][2] = z;
gui_group_end();
gui_rotation_mat4("Rotation", goxel.plane);
break;

}
x = (int)round(goxel.plane[3][0]);
y = (int)round(goxel.plane[3][1]);
z = (int)round(goxel.plane[3][2]);
gui_group_begin("Origin");
if (gui_input_int("X", &x, 0, 0)) goxel.plane[3][0] = x;
if (gui_input_int("Y", &y, 0, 0)) goxel.plane[3][1] = y;
if (gui_input_int("Z", &z, 0, 0)) goxel.plane[3][2] = z;
gui_group_end();
gui_group_begin("Rotation");
gui_checkbox("Custom", &tool->custom_rotation, NULL);
if (tool->custom_rotation)
gui_rotation_mat4(goxel.plane);
else
gui_rotation_mat4_axis(goxel.plane);
gui_group_end();

if (gui_button("Cut Above", 1, 0)) {
cut(true);
Expand Down

0 comments on commit 4f006bb

Please sign in to comment.