diff options
Diffstat (limited to 'source/blender/editors/gizmo_library')
20 files changed, 6304 insertions, 0 deletions
diff --git a/source/blender/editors/gizmo_library/CMakeLists.txt b/source/blender/editors/gizmo_library/CMakeLists.txt new file mode 100644 index 00000000000..0e7b2a8be0f --- /dev/null +++ b/source/blender/editors/gizmo_library/CMakeLists.txt @@ -0,0 +1,63 @@ +# ***** BEGIN GPL LICENSE BLOCK ***** +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# ***** END GPL LICENSE BLOCK ***** + +set(INC + ../include + ../../blenkernel + ../../blenlib + ../../blentranslation + ../../bmesh + ../../depsgraph + ../../gpu + ../../makesdna + ../../makesrna + ../../windowmanager + ../../../../intern/guardedalloc + ../../../../intern/eigen + ../../../../intern/glew-mx +) + +set(INC_SYS + ${GLEW_INCLUDE_PATH} +) + +set(SRC + gizmo_draw_utils.c + gizmo_geometry.h + gizmo_library_intern.h + gizmo_library_presets.c + gizmo_library_utils.c + geometry/geom_arrow_gizmo.c + geometry/geom_cube_gizmo.c + geometry/geom_dial_gizmo.c + gizmo_group_types/value2d_gizmo_group.c + gizmo_types/arrow2d_gizmo.c + gizmo_types/arrow3d_gizmo.c + gizmo_types/blank3d_gizmo.c + gizmo_types/button2d_gizmo.c + gizmo_types/cage2d_gizmo.c + gizmo_types/cage3d_gizmo.c + gizmo_types/dial3d_gizmo.c + gizmo_types/move3d_gizmo.c + gizmo_types/primitive3d_gizmo.c + gizmo_types/value2d_gizmo.c +) + +add_definitions(${GL_DEFINITIONS}) + +blender_add_lib(bf_editor_gizmo_library "${SRC}" "${INC}" "${INC_SYS}") diff --git a/source/blender/editors/gizmo_library/geometry/geom_arrow_gizmo.c b/source/blender/editors/gizmo_library/geometry/geom_arrow_gizmo.c new file mode 100644 index 00000000000..55b33b0d8ad --- /dev/null +++ b/source/blender/editors/gizmo_library/geometry/geom_arrow_gizmo.c @@ -0,0 +1,141 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2016 Blender Foundation. + * All rights reserved. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file geom_arrow_gizmo.c + * \ingroup edgizmolib + */ + +#include "../gizmo_geometry.h" + +static float verts[][3] = { + {-0.000000, 0.012320, 0.000000}, + {-0.000000, 0.012320, 0.974306}, + {0.008711, 0.008711, 0.000000}, + {0.008711, 0.008711, 0.974306}, + {0.012320, -0.000000, 0.000000}, + {0.012320, -0.000000, 0.974306}, + {0.008711, -0.008711, 0.000000}, + {0.008711, -0.008711, 0.974306}, + {-0.000000, -0.012320, 0.000000}, + {-0.000000, -0.012320, 0.974306}, + {-0.008711, -0.008711, 0.000000}, + {-0.008711, -0.008711, 0.974306}, + {-0.012320, 0.000000, 0.000000}, + {-0.012320, 0.000000, 0.974306}, + {-0.008711, 0.008711, 0.000000}, + {-0.008711, 0.008711, 0.974306}, + {0.000000, 0.072555, 0.974306}, + {0.051304, 0.051304, 0.974306}, + {0.072555, -0.000000, 0.974306}, + {0.051304, -0.051304, 0.974306}, + {-0.000000, -0.072555, 0.974306}, + {-0.051304, -0.051304, 0.974306}, + {-0.072555, 0.000000, 0.974306}, + {-0.051304, 0.051304, 0.974306}, + {0.000000, -0.000000, 1.268098}, +}; + +static float normals[][3] = { + {0.000000, 0.776360, -0.630238}, + {0.000000, 0.594348, -0.804163}, + {0.548967, 0.548967, -0.630238}, + {0.420270, 0.420270, -0.804163}, + {0.776360, 0.000000, -0.630238}, + {0.594378, 0.000000, -0.804163}, + {0.548967, -0.548967, -0.630238}, + {0.420270, -0.420270, -0.804163}, + {0.000000, -0.776360, -0.630238}, + {0.000000, -0.594378, -0.804163}, + {-0.548967, -0.548967, -0.630238}, + {-0.420270, -0.420270, -0.804163}, + {-0.776360, 0.000000, -0.630238}, + {-0.594378, 0.000000, -0.804163}, + {-0.548967, 0.548967, -0.630238}, + {-0.420270, 0.420270, -0.804163}, + {0.000000, 0.843226, -0.537492}, + {0.596271, 0.596271, -0.537492}, + {0.843226, 0.000000, -0.537492}, + {0.596271, -0.596271, -0.537492}, + {0.000000, -0.843226, -0.537492}, + {-0.596271, -0.596271, -0.537492}, + {-0.843226, 0.000000, -0.537492}, + {-0.596271, 0.596271, -0.537492}, + {0.000000, 0.000000, 1.000000}, +}; + +static unsigned short indices[] = { + 1, 3, 2, + 3, 5, 4, + 5, 7, 6, + 7, 9, 8, + 9, 11, 10, + 11, 13, 12, + 5, 18, 19, + 15, 1, 0, + 13, 15, 14, + 6, 10, 14, + 11, 21, 22, + 7, 19, 20, + 13, 22, 23, + 3, 17, 18, + 9, 20, 21, + 15, 23, 16, + 1, 16, 17, + 23, 22, 24, + 21, 20, 24, + 19, 18, 24, + 17, 16, 24, + 16, 23, 24, + 22, 21, 24, + 20, 19, 24, + 18, 17, 24, + 0, 1, 2, + 2, 3, 4, + 4, 5, 6, + 6, 7, 8, + 8, 9, 10, + 10, 11, 12, + 7, 5, 19, + 14, 15, 0, + 12, 13, 14, + 14, 0, 2, + 2, 4, 6, + 6, 8, 10, + 10, 12, 14, + 14, 2, 6, + 13, 11, 22, + 9, 7, 20, + 15, 13, 23, + 5, 3, 18, + 11, 9, 21, + 1, 15, 16, + 3, 1, 17, +}; + +GizmoGeomInfo wm_gizmo_geom_data_arrow = { + .nverts = 25, + .ntris = 46, + .verts = verts, + .normals = normals, + .indices = indices, +}; diff --git a/source/blender/editors/gizmo_library/geometry/geom_cube_gizmo.c b/source/blender/editors/gizmo_library/geometry/geom_cube_gizmo.c new file mode 100644 index 00000000000..410e2a5ad6e --- /dev/null +++ b/source/blender/editors/gizmo_library/geometry/geom_cube_gizmo.c @@ -0,0 +1,75 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2016 Blender Foundation. + * All rights reserved. + * + * Contributor(s): none yet. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file geom_cube_gizmo.c + * \ingroup edgizmolib + */ + +#include "../gizmo_geometry.h" + +static const float verts[][3] = { + {1.000000, 1.000000, -1.000000}, + {1.000000, -1.000000, -1.000000}, + {-1.000000, -1.000000, -1.000000}, + {-1.000000, 1.000000, -1.000000}, + {1.000000, 1.000000, 1.000000}, + {0.999999, -1.000001, 1.000000}, + {-1.000000, -1.000000, 1.000000}, + {-1.000000, 1.000000, 1.000000}, +}; + +static const float normals[][3] = { + {0.577349, 0.577349, -0.577349}, + {0.577349, -0.577349, -0.577349}, + {-0.577349, -0.577349, -0.577349}, + {-0.577349, 0.577349, -0.577349}, + {0.577349, 0.577349, 0.577349}, + {0.577349, -0.577349, 0.577349}, + {-0.577349, -0.577349, 0.577349}, + {-0.577349, 0.577349, 0.577349}, +}; + +static const unsigned short indices[] = { + 1, 2, 3, + 7, 6, 5, + 4, 5, 1, + 5, 6, 2, + 2, 6, 7, + 0, 3, 7, + 0, 1, 3, + 4, 7, 5, + 0, 4, 1, + 1, 5, 2, + 3, 2, 7, + 4, 0, 7, +}; + +GizmoGeomInfo wm_gizmo_geom_data_cube = { + .nverts = 8, + .ntris = 12, + .verts = verts, + .normals = normals, + .indices = indices, +}; diff --git a/source/blender/editors/gizmo_library/geometry/geom_dial_gizmo.c b/source/blender/editors/gizmo_library/geometry/geom_dial_gizmo.c new file mode 100644 index 00000000000..22cf0785c64 --- /dev/null +++ b/source/blender/editors/gizmo_library/geometry/geom_dial_gizmo.c @@ -0,0 +1,813 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2016 Blender Foundation. + * All rights reserved. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file geom_dial_gizmo.c + * \ingroup edgizmolib + */ + +#include "../gizmo_geometry.h" + +static const float verts[][3] = { + {1.034000, 0.000000, 0.000000}, + {1.017000, 0.000000, 0.029445}, + {0.983000, 0.000000, 0.029445}, + {0.966000, 0.000000, 0.000000}, + {0.983000, 0.000000, -0.029445}, + {1.017000, 0.000000, -0.029445}, + {1.014132, 0.201723, 0.000000}, + {0.997459, 0.198407, 0.029445}, + {0.964112, 0.191774, 0.029445}, + {0.947439, 0.188457, 0.000000}, + {0.964112, 0.191774, -0.029445}, + {0.997459, 0.198407, -0.029445}, + {0.955292, 0.395695, 0.000000}, + {0.939586, 0.389189, 0.029445}, + {0.908174, 0.376178, 0.029445}, + {0.892468, 0.369672, 0.000000}, + {0.908174, 0.376178, -0.029445}, + {0.939586, 0.389189, -0.029445}, + {0.859740, 0.574460, 0.000000}, + {0.845605, 0.565015, 0.029445}, + {0.817335, 0.546126, 0.029445}, + {0.803200, 0.536681, 0.000000}, + {0.817335, 0.546126, -0.029445}, + {0.845605, 0.565015, -0.029445}, + {0.731148, 0.731148, 0.000000}, + {0.719128, 0.719128, 0.029445}, + {0.695086, 0.695086, 0.029445}, + {0.683065, 0.683065, 0.000000}, + {0.695086, 0.695086, -0.029445}, + {0.719128, 0.719128, -0.029445}, + {0.574460, 0.859740, 0.000000}, + {0.565015, 0.845605, 0.029445}, + {0.546125, 0.817335, 0.029445}, + {0.536681, 0.803200, 0.000000}, + {0.546125, 0.817335, -0.029445}, + {0.565015, 0.845605, -0.029445}, + {0.395695, 0.955291, 0.000000}, + {0.389189, 0.939585, 0.029445}, + {0.376178, 0.908173, 0.029445}, + {0.369672, 0.892467, 0.000000}, + {0.376178, 0.908173, -0.029445}, + {0.389189, 0.939585, -0.029445}, + {0.201724, 1.014132, 0.000000}, + {0.198407, 0.997459, 0.029445}, + {0.191774, 0.964112, 0.029445}, + {0.188457, 0.947439, 0.000000}, + {0.191774, 0.964112, -0.029445}, + {0.198407, 0.997459, -0.029445}, + {0.000000, 1.034000, 0.000000}, + {0.000000, 1.017000, 0.029445}, + {0.000000, 0.983000, 0.029445}, + {0.000000, 0.966000, 0.000000}, + {0.000000, 0.983000, -0.029445}, + {0.000000, 1.017000, -0.029445}, + {-0.201723, 1.014132, 0.000000}, + {-0.198407, 0.997459, 0.029445}, + {-0.191774, 0.964112, 0.029445}, + {-0.188457, 0.947439, 0.000000}, + {-0.191774, 0.964112, -0.029445}, + {-0.198407, 0.997459, -0.029445}, + {-0.395695, 0.955291, 0.000000}, + {-0.389189, 0.939585, 0.029445}, + {-0.376178, 0.908174, 0.029445}, + {-0.369672, 0.892468, 0.000000}, + {-0.376178, 0.908174, -0.029445}, + {-0.389189, 0.939585, -0.029445}, + {-0.574459, 0.859740, 0.000000}, + {-0.565015, 0.845605, 0.029445}, + {-0.546125, 0.817335, 0.029445}, + {-0.536681, 0.803200, 0.000000}, + {-0.546125, 0.817335, -0.029445}, + {-0.565015, 0.845605, -0.029445}, + {-0.731149, 0.731148, 0.000000}, + {-0.719128, 0.719127, 0.029445}, + {-0.695086, 0.695086, 0.029445}, + {-0.683065, 0.683065, 0.000000}, + {-0.695086, 0.695086, -0.029445}, + {-0.719128, 0.719127, -0.029445}, + {-0.859740, 0.574460, 0.000000}, + {-0.845604, 0.565015, 0.029445}, + {-0.817335, 0.546126, 0.029445}, + {-0.803200, 0.536681, 0.000000}, + {-0.817335, 0.546126, -0.029445}, + {-0.845604, 0.565015, -0.029445}, + {-0.955291, 0.395695, 0.000000}, + {-0.939585, 0.389189, 0.029445}, + {-0.908173, 0.376178, 0.029445}, + {-0.892468, 0.369672, 0.000000}, + {-0.908173, 0.376178, -0.029445}, + {-0.939585, 0.389189, -0.029445}, + {-1.014132, 0.201723, 0.000000}, + {-0.997459, 0.198407, 0.029445}, + {-0.964112, 0.191774, 0.029445}, + {-0.947439, 0.188457, 0.000000}, + {-0.964112, 0.191774, -0.029445}, + {-0.997459, 0.198407, -0.029445}, + {-1.034000, 0.000000, 0.000000}, + {-1.017000, 0.000000, 0.029445}, + {-0.983000, 0.000000, 0.029445}, + {-0.966000, 0.000000, 0.000000}, + {-0.983000, 0.000000, -0.029445}, + {-1.017000, 0.000000, -0.029445}, + {-1.014132, -0.201723, 0.000000}, + {-0.997459, -0.198407, 0.029445}, + {-0.964112, -0.191774, 0.029445}, + {-0.947439, -0.188457, 0.000000}, + {-0.964112, -0.191774, -0.029445}, + {-0.997459, -0.198407, -0.029445}, + {-0.955292, -0.395694, 0.000000}, + {-0.939586, -0.389189, 0.029445}, + {-0.908174, -0.376177, 0.029445}, + {-0.892468, -0.369672, 0.000000}, + {-0.908174, -0.376177, -0.029445}, + {-0.939586, -0.389189, -0.029445}, + {-0.859740, -0.574460, 0.000000}, + {-0.845604, -0.565015, 0.029445}, + {-0.817335, -0.546126, 0.029445}, + {-0.803200, -0.536681, 0.000000}, + {-0.817335, -0.546126, -0.029445}, + {-0.845604, -0.565015, -0.029445}, + {-0.731149, -0.731148, 0.000000}, + {-0.719128, -0.719127, 0.029445}, + {-0.695086, -0.695086, 0.029445}, + {-0.683065, -0.683065, 0.000000}, + {-0.695086, -0.695086, -0.029445}, + {-0.719128, -0.719127, -0.029445}, + {-0.574460, -0.859739, 0.000000}, + {-0.565015, -0.845604, 0.029445}, + {-0.546126, -0.817334, 0.029445}, + {-0.536681, -0.803199, 0.000000}, + {-0.546126, -0.817334, -0.029445}, + {-0.565015, -0.845604, -0.029445}, + {-0.395695, -0.955291, 0.000000}, + {-0.389189, -0.939585, 0.029445}, + {-0.376178, -0.908174, 0.029445}, + {-0.369672, -0.892468, 0.000000}, + {-0.376178, -0.908174, -0.029445}, + {-0.389189, -0.939585, -0.029445}, + {-0.201724, -1.014132, 0.000000}, + {-0.198407, -0.997459, 0.029445}, + {-0.191774, -0.964112, 0.029445}, + {-0.188458, -0.947438, 0.000000}, + {-0.191774, -0.964112, -0.029445}, + {-0.198407, -0.997459, -0.029445}, + {0.000000, -1.034000, 0.000000}, + {0.000000, -1.017000, 0.029445}, + {0.000000, -0.983000, 0.029445}, + {0.000000, -0.966000, 0.000000}, + {0.000000, -0.983000, -0.029445}, + {0.000000, -1.017000, -0.029445}, + {0.201723, -1.014132, 0.000000}, + {0.198407, -0.997459, 0.029445}, + {0.191773, -0.964112, 0.029445}, + {0.188457, -0.947439, 0.000000}, + {0.191773, -0.964112, -0.029445}, + {0.198407, -0.997459, -0.029445}, + {0.395695, -0.955291, 0.000000}, + {0.389189, -0.939585, 0.029445}, + {0.376178, -0.908173, 0.029445}, + {0.369672, -0.892467, 0.000000}, + {0.376178, -0.908173, -0.029445}, + {0.389189, -0.939585, -0.029445}, + {0.574460, -0.859740, 0.000000}, + {0.565015, -0.845605, 0.029445}, + {0.546125, -0.817335, 0.029445}, + {0.536681, -0.803200, 0.000000}, + {0.546125, -0.817335, -0.029445}, + {0.565015, -0.845605, -0.029445}, + {0.731148, -0.731149, 0.000000}, + {0.719127, -0.719128, 0.029445}, + {0.695086, -0.695086, 0.029445}, + {0.683065, -0.683066, 0.000000}, + {0.695086, -0.695086, -0.029445}, + {0.719127, -0.719128, -0.029445}, + {0.859740, -0.574460, 0.000000}, + {0.845605, -0.565015, 0.029445}, + {0.817335, -0.546126, 0.029445}, + {0.803200, -0.536681, 0.000000}, + {0.817335, -0.546126, -0.029445}, + {0.845605, -0.565015, -0.029445}, + {0.955291, -0.395695, 0.000000}, + {0.939585, -0.389189, 0.029445}, + {0.908173, -0.376178, 0.029445}, + {0.892467, -0.369673, 0.000000}, + {0.908173, -0.376178, -0.029445}, + {0.939585, -0.389189, -0.029445}, + {1.014132, -0.201723, 0.000000}, + {0.997459, -0.198407, 0.029445}, + {0.964112, -0.191774, 0.029445}, + {0.947439, -0.188457, 0.000000}, + {0.964112, -0.191774, -0.029445}, + {0.997459, -0.198407, -0.029445}, +}; + +static const float normals[][3] = { + {1.000000, 0.000000, 0.000000}, + {0.522691, 0.000000, 0.852504}, + {-0.475845, 0.000000, 0.879513}, + {-1.000000, 0.000000, 0.000000}, + {-0.475845, 0.000000, -0.879513}, + {0.522691, 0.000000, -0.852504}, + {0.980773, 0.195074, 0.000000}, + {0.512650, 0.101962, 0.852504}, + {-0.466689, -0.092807, 0.879513}, + {-0.980773, -0.195074, 0.000000}, + {-0.466689, -0.092807, -0.879513}, + {0.512650, 0.101962, -0.852504}, + {0.923856, 0.382672, 0.000000}, + {0.482894, 0.200018, 0.852504}, + {-0.439619, -0.182073, 0.879513}, + {-0.923856, -0.382672, 0.000000}, + {-0.439619, -0.182073, -0.879513}, + {0.482894, 0.200018, -0.852504}, + {0.831446, 0.555559, 0.000000}, + {0.434614, 0.290384, 0.852504}, + {-0.395642, -0.264351, 0.879513}, + {-0.831446, -0.555559, 0.000000}, + {-0.395642, -0.264351, -0.879513}, + {0.434614, 0.290384, -0.852504}, + {0.707083, 0.707083, 0.000000}, + {0.369610, 0.369610, 0.852504}, + {-0.336467, -0.336467, 0.879513}, + {-0.707083, -0.707083, 0.000000}, + {-0.336467, -0.336467, -0.879513}, + {0.369610, 0.369610, -0.852504}, + {0.555559, 0.831446, 0.000000}, + {0.290384, 0.434614, 0.852504}, + {-0.264351, -0.395642, 0.879513}, + {-0.555559, -0.831446, 0.000000}, + {-0.264351, -0.395642, -0.879513}, + {0.290384, 0.434614, -0.852504}, + {0.382672, 0.923856, 0.000000}, + {0.200018, 0.482894, 0.852504}, + {-0.182073, -0.439619, 0.879513}, + {-0.382672, -0.923856, 0.000000}, + {-0.182073, -0.439619, -0.879513}, + {0.200018, 0.482894, -0.852504}, + {0.195074, 0.980773, 0.000000}, + {0.101962, 0.512650, 0.852504}, + {-0.092807, -0.466689, 0.879513}, + {-0.195074, -0.980773, 0.000000}, + {-0.092807, -0.466689, -0.879513}, + {0.101962, 0.512650, -0.852504}, + {0.000000, 1.000000, 0.000000}, + {0.000000, 0.522691, 0.852504}, + {0.000000, -0.475845, 0.879513}, + {0.000000, -1.000000, 0.000000}, + {0.000000, -0.475845, -0.879513}, + {0.000000, 0.522691, -0.852504}, + {-0.195074, 0.980773, 0.000000}, + {-0.101962, 0.512650, 0.852504}, + {0.092807, -0.466689, 0.879513}, + {0.195074, -0.980773, 0.000000}, + {0.092807, -0.466689, -0.879513}, + {-0.101962, 0.512650, -0.852504}, + {-0.382672, 0.923856, 0.000000}, + {-0.200018, 0.482894, 0.852504}, + {0.182073, -0.439619, 0.879513}, + {0.382672, -0.923856, 0.000000}, + {0.182073, -0.439619, -0.879513}, + {-0.200018, 0.482894, -0.852504}, + {-0.555559, 0.831446, 0.000000}, + {-0.290384, 0.434614, 0.852504}, + {0.264351, -0.395642, 0.879513}, + {0.555559, -0.831446, 0.000000}, + {0.264351, -0.395642, -0.879513}, + {-0.290384, 0.434614, -0.852504}, + {-0.707083, 0.707083, 0.000000}, + {-0.369610, 0.369610, 0.852504}, + {0.336467, -0.336467, 0.879513}, + {0.707083, -0.707083, 0.000000}, + {0.336467, -0.336467, -0.879513}, + {-0.369610, 0.369610, -0.852504}, + {-0.831446, 0.555559, 0.000000}, + {-0.434614, 0.290384, 0.852504}, + {0.395642, -0.264351, 0.879513}, + {0.831446, -0.555559, 0.000000}, + {0.395642, -0.264351, -0.879513}, + {-0.434614, 0.290384, -0.852504}, + {-0.923856, 0.382672, 0.000000}, + {-0.482894, 0.200018, 0.852504}, + {0.439619, -0.182073, 0.879513}, + {0.923856, -0.382672, 0.000000}, + {0.439619, -0.182073, -0.879513}, + {-0.482894, 0.200018, -0.852504}, + {-0.980773, 0.195074, 0.000000}, + {-0.512650, 0.101962, 0.852504}, + {0.466689, -0.092807, 0.879513}, + {0.980773, -0.195074, 0.000000}, + {0.466689, -0.092807, -0.879513}, + {-0.512650, 0.101962, -0.852504}, + {-1.000000, 0.000000, 0.000000}, + {-0.522691, 0.000000, 0.852504}, + {0.475845, 0.000000, 0.879513}, + {1.000000, 0.000000, 0.000000}, + {0.475845, 0.000000, -0.879513}, + {-0.522691, 0.000000, -0.852504}, + {-0.980773, -0.195074, 0.000000}, + {-0.512650, -0.101962, 0.852504}, + {0.466689, 0.092807, 0.879513}, + {0.980773, 0.195074, 0.000000}, + {0.466689, 0.092807, -0.879513}, + {-0.512650, -0.101962, -0.852504}, + {-0.923856, -0.382672, 0.000000}, + {-0.482894, -0.200018, 0.852504}, + {0.439619, 0.182073, 0.879513}, + {0.923856, 0.382672, 0.000000}, + {0.439619, 0.182073, -0.879513}, + {-0.482894, -0.200018, -0.852504}, + {-0.831446, -0.555559, 0.000000}, + {-0.434614, -0.290384, 0.852504}, + {0.395642, 0.264351, 0.879513}, + {0.831446, 0.555559, 0.000000}, + {0.395642, 0.264351, -0.879513}, + {-0.434614, -0.290384, -0.852504}, + {-0.707083, -0.707083, 0.000000}, + {-0.369610, -0.369610, 0.852504}, + {0.336467, 0.336467, 0.879513}, + {0.707083, 0.707083, 0.000000}, + {0.336467, 0.336467, -0.879513}, + {-0.369610, -0.369610, -0.852504}, + {-0.555559, -0.831446, 0.000000}, + {-0.290384, -0.434614, 0.852504}, + {0.264351, 0.395642, 0.879513}, + {0.555559, 0.831446, 0.000000}, + {0.264351, 0.395642, -0.879513}, + {-0.290384, -0.434614, -0.852504}, + {-0.382672, -0.923856, 0.000000}, + {-0.200018, -0.482894, 0.852504}, + {0.182073, 0.439619, 0.879513}, + {0.382672, 0.923856, 0.000000}, + {0.182073, 0.439619, -0.879513}, + {-0.200018, -0.482894, -0.852504}, + {-0.195074, -0.980773, 0.000000}, + {-0.101962, -0.512650, 0.852504}, + {0.092807, 0.466689, 0.879513}, + {0.195074, 0.980773, 0.000000}, + {0.092807, 0.466689, -0.879513}, + {-0.101962, -0.512650, -0.852504}, + {0.000000, -1.000000, 0.000000}, + {0.000000, -0.522691, 0.852504}, + {0.000000, 0.475845, 0.879513}, + {0.000000, 1.000000, 0.000000}, + {0.000000, 0.475845, -0.879513}, + {0.000000, -0.522691, -0.852504}, + {0.195074, -0.980773, 0.000000}, + {0.101962, -0.512650, 0.852504}, + {-0.092807, 0.466689, 0.879513}, + {-0.195074, 0.980773, 0.000000}, + {-0.092807, 0.466689, -0.879513}, + {0.101962, -0.512650, -0.852504}, + {0.382672, -0.923856, 0.000000}, + {0.200018, -0.482894, 0.852504}, + {-0.182073, 0.439619, 0.879513}, + {-0.382672, 0.923856, 0.000000}, + {-0.182073, 0.439619, -0.879513}, + {0.200018, -0.482894, -0.852504}, + {0.555559, -0.831446, 0.000000}, + {0.290384, -0.434614, 0.852504}, + {-0.264351, 0.395642, 0.879513}, + {-0.555559, 0.831446, 0.000000}, + {-0.264351, 0.395642, -0.879513}, + {0.290384, -0.434614, -0.852504}, + {0.707083, -0.707083, 0.000000}, + {0.369610, -0.369610, 0.852504}, + {-0.336467, 0.336467, 0.879513}, + {-0.707083, 0.707083, 0.000000}, + {-0.336467, 0.336467, -0.879513}, + {0.369610, -0.369610, -0.852504}, + {0.831446, -0.555559, 0.000000}, + {0.434614, -0.290384, 0.852504}, + {-0.395642, 0.264351, 0.879513}, + {-0.831446, 0.555559, 0.000000}, + {-0.395642, 0.264351, -0.879513}, + {0.434614, -0.290384, -0.852504}, + {0.923856, -0.382672, 0.000000}, + {0.482894, -0.200018, 0.852504}, + {-0.439619, 0.182073, 0.879513}, + {-0.923856, 0.382672, 0.000000}, + {-0.439619, 0.182073, -0.879513}, + {0.482894, -0.200018, -0.852504}, + {0.980773, -0.195074, 0.000000}, + {0.512650, -0.101962, 0.852504}, + {-0.466689, 0.092807, 0.879513}, + {-0.980773, 0.195074, 0.000000}, + {-0.466689, 0.092807, -0.879513}, + {0.512650, -0.101962, -0.852504}, +}; + +static const unsigned short indices[] = { + 6, 7, 1, + 7, 8, 2, + 8, 9, 3, + 9, 10, 4, + 10, 11, 5, + 5, 11, 6, + 12, 13, 7, + 13, 14, 8, + 14, 15, 9, + 15, 16, 10, + 16, 17, 11, + 11, 17, 12, + 18, 19, 13, + 13, 19, 20, + 20, 21, 15, + 15, 21, 22, + 22, 23, 17, + 17, 23, 18, + 24, 25, 19, + 19, 25, 26, + 26, 27, 21, + 21, 27, 28, + 28, 29, 23, + 23, 29, 24, + 30, 31, 25, + 25, 31, 32, + 26, 32, 33, + 27, 33, 34, + 34, 35, 29, + 29, 35, 30, + 36, 37, 31, + 31, 37, 38, + 38, 39, 33, + 39, 40, 34, + 40, 41, 35, + 35, 41, 36, + 36, 42, 43, + 43, 44, 38, + 44, 45, 39, + 45, 46, 40, + 46, 47, 41, + 47, 42, 36, + 48, 49, 43, + 49, 50, 44, + 50, 51, 45, + 51, 52, 46, + 52, 53, 47, + 47, 53, 48, + 54, 55, 49, + 49, 55, 56, + 50, 56, 57, + 57, 58, 52, + 58, 59, 53, + 53, 59, 54, + 60, 61, 55, + 55, 61, 62, + 56, 62, 63, + 63, 64, 58, + 64, 65, 59, + 59, 65, 60, + 66, 67, 61, + 61, 67, 68, + 68, 69, 63, + 69, 70, 64, + 70, 71, 65, + 71, 66, 60, + 72, 73, 67, + 73, 74, 68, + 68, 74, 75, + 75, 76, 70, + 76, 77, 71, + 71, 77, 72, + 78, 79, 73, + 79, 80, 74, + 74, 80, 81, + 81, 82, 76, + 82, 83, 77, + 83, 78, 72, + 78, 84, 85, + 85, 86, 80, + 80, 86, 87, + 87, 88, 82, + 82, 88, 89, + 89, 84, 78, + 90, 91, 85, + 91, 92, 86, + 86, 92, 93, + 93, 94, 88, + 88, 94, 95, + 95, 90, 84, + 96, 97, 91, + 97, 98, 92, + 98, 99, 93, + 99, 100, 94, + 100, 101, 95, + 101, 96, 90, + 102, 103, 97, + 103, 104, 98, + 104, 105, 99, + 99, 105, 106, + 106, 107, 101, + 101, 107, 102, + 108, 109, 103, + 103, 109, 110, + 110, 111, 105, + 105, 111, 112, + 112, 113, 107, + 107, 113, 108, + 114, 115, 109, + 115, 116, 110, + 116, 117, 111, + 111, 117, 118, + 112, 118, 119, + 113, 119, 114, + 114, 120, 121, + 121, 122, 116, + 122, 123, 117, + 117, 123, 124, + 124, 125, 119, + 125, 120, 114, + 126, 127, 121, + 121, 127, 128, + 128, 129, 123, + 123, 129, 130, + 130, 131, 125, + 125, 131, 126, + 132, 133, 127, + 133, 134, 128, + 128, 134, 135, + 135, 136, 130, + 136, 137, 131, + 131, 137, 132, + 132, 138, 139, + 133, 139, 140, + 134, 140, 141, + 141, 142, 136, + 142, 143, 137, + 143, 138, 132, + 138, 144, 145, + 139, 145, 146, + 146, 147, 141, + 141, 147, 148, + 148, 149, 143, + 149, 144, 138, + 144, 150, 151, + 151, 152, 146, + 146, 152, 153, + 153, 154, 148, + 154, 155, 149, + 155, 150, 144, + 156, 157, 151, + 151, 157, 158, + 158, 159, 153, + 159, 160, 154, + 160, 161, 155, + 155, 161, 156, + 156, 162, 163, + 163, 164, 158, + 158, 164, 165, + 165, 166, 160, + 160, 166, 167, + 167, 162, 156, + 162, 168, 169, + 169, 170, 164, + 164, 170, 171, + 165, 171, 172, + 166, 172, 173, + 173, 168, 162, + 174, 175, 169, + 175, 176, 170, + 170, 176, 177, + 177, 178, 172, + 172, 178, 179, + 173, 179, 174, + 174, 180, 181, + 181, 182, 176, + 176, 182, 183, + 183, 184, 178, + 178, 184, 185, + 179, 185, 180, + 186, 187, 181, + 187, 188, 182, + 188, 189, 183, + 183, 189, 190, + 190, 191, 185, + 191, 186, 180, + 0, 1, 187, + 1, 2, 188, + 2, 3, 189, + 3, 4, 190, + 190, 4, 5, + 191, 5, 0, + 0, 6, 1, + 1, 7, 2, + 2, 8, 3, + 3, 9, 4, + 4, 10, 5, + 0, 5, 6, + 6, 12, 7, + 7, 13, 8, + 8, 14, 9, + 9, 15, 10, + 10, 16, 11, + 6, 11, 12, + 12, 18, 13, + 14, 13, 20, + 14, 20, 15, + 16, 15, 22, + 16, 22, 17, + 12, 17, 18, + 18, 24, 19, + 20, 19, 26, + 20, 26, 21, + 22, 21, 28, + 22, 28, 23, + 18, 23, 24, + 24, 30, 25, + 26, 25, 32, + 27, 26, 33, + 28, 27, 34, + 28, 34, 29, + 24, 29, 30, + 30, 36, 31, + 32, 31, 38, + 32, 38, 33, + 33, 39, 34, + 34, 40, 35, + 30, 35, 36, + 37, 36, 43, + 37, 43, 38, + 38, 44, 39, + 39, 45, 40, + 40, 46, 41, + 41, 47, 36, + 42, 48, 43, + 43, 49, 44, + 44, 50, 45, + 45, 51, 46, + 46, 52, 47, + 42, 47, 48, + 48, 54, 49, + 50, 49, 56, + 51, 50, 57, + 51, 57, 52, + 52, 58, 53, + 48, 53, 54, + 54, 60, 55, + 56, 55, 62, + 57, 56, 63, + 57, 63, 58, + 58, 64, 59, + 54, 59, 60, + 60, 66, 61, + 62, 61, 68, + 62, 68, 63, + 63, 69, 64, + 64, 70, 65, + 65, 71, 60, + 66, 72, 67, + 67, 73, 68, + 69, 68, 75, + 69, 75, 70, + 70, 76, 71, + 66, 71, 72, + 72, 78, 73, + 73, 79, 74, + 75, 74, 81, + 75, 81, 76, + 76, 82, 77, + 77, 83, 72, + 79, 78, 85, + 79, 85, 80, + 81, 80, 87, + 81, 87, 82, + 83, 82, 89, + 83, 89, 78, + 84, 90, 85, + 85, 91, 86, + 87, 86, 93, + 87, 93, 88, + 89, 88, 95, + 89, 95, 84, + 90, 96, 91, + 91, 97, 92, + 92, 98, 93, + 93, 99, 94, + 94, 100, 95, + 95, 101, 90, + 96, 102, 97, + 97, 103, 98, + 98, 104, 99, + 100, 99, 106, + 100, 106, 101, + 96, 101, 102, + 102, 108, 103, + 104, 103, 110, + 104, 110, 105, + 106, 105, 112, + 106, 112, 107, + 102, 107, 108, + 108, 114, 109, + 109, 115, 110, + 110, 116, 111, + 112, 111, 118, + 113, 112, 119, + 108, 113, 114, + 115, 114, 121, + 115, 121, 116, + 116, 122, 117, + 118, 117, 124, + 118, 124, 119, + 119, 125, 114, + 120, 126, 121, + 122, 121, 128, + 122, 128, 123, + 124, 123, 130, + 124, 130, 125, + 120, 125, 126, + 126, 132, 127, + 127, 133, 128, + 129, 128, 135, + 129, 135, 130, + 130, 136, 131, + 126, 131, 132, + 133, 132, 139, + 134, 133, 140, + 135, 134, 141, + 135, 141, 136, + 136, 142, 137, + 137, 143, 132, + 139, 138, 145, + 140, 139, 146, + 140, 146, 141, + 142, 141, 148, + 142, 148, 143, + 143, 149, 138, + 145, 144, 151, + 145, 151, 146, + 147, 146, 153, + 147, 153, 148, + 148, 154, 149, + 149, 155, 144, + 150, 156, 151, + 152, 151, 158, + 152, 158, 153, + 153, 159, 154, + 154, 160, 155, + 150, 155, 156, + 157, 156, 163, + 157, 163, 158, + 159, 158, 165, + 159, 165, 160, + 161, 160, 167, + 161, 167, 156, + 163, 162, 169, + 163, 169, 164, + 165, 164, 171, + 166, 165, 172, + 167, 166, 173, + 167, 173, 162, + 168, 174, 169, + 169, 175, 170, + 171, 170, 177, + 171, 177, 172, + 173, 172, 179, + 168, 173, 174, + 175, 174, 181, + 175, 181, 176, + 177, 176, 183, + 177, 183, 178, + 179, 178, 185, + 174, 179, 180, + 180, 186, 181, + 181, 187, 182, + 182, 188, 183, + 184, 183, 190, + 184, 190, 185, + 185, 191, 180, + 186, 0, 187, + 187, 1, 188, + 188, 2, 189, + 189, 3, 190, + 191, 190, 5, + 186, 191, 0, +}; + +GizmoGeomInfo wm_gizmo_geom_data_dial = { + .nverts = 192, + .ntris = 384, + .verts = verts, + .normals = normals, + .indices = indices, +}; diff --git a/source/blender/editors/gizmo_library/gizmo_draw_utils.c b/source/blender/editors/gizmo_library/gizmo_draw_utils.c new file mode 100644 index 00000000000..9ef53d8f696 --- /dev/null +++ b/source/blender/editors/gizmo_library/gizmo_draw_utils.c @@ -0,0 +1,121 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2014 Blender Foundation. + * All rights reserved. + * + * Contributor(s): Blender Foundation + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file gizmo_draw_utils.c + * \ingroup edgizmolib + */ + +#include "BLI_listbase.h" +#include "BLI_ghash.h" +#include "BLI_math.h" +#include "BLI_string.h" +#include "BLI_string_utils.h" + +#include "BKE_context.h" + +#include "ED_screen.h" +#include "ED_view3d.h" + +#include "GPU_batch.h" +#include "GPU_glew.h" +#include "GPU_immediate.h" +#include "GPU_state.h" + +#include "MEM_guardedalloc.h" + +#include "RNA_access.h" + +#include "WM_api.h" +#include "WM_types.h" + +/* only for own init/exit calls (wm_gizmotype_init/wm_gizmotype_free) */ +#include "wm.h" + +/* own includes */ +#include "gizmo_library_intern.h" + +/** + * Main draw call for GizmoGeomInfo data + */ +void wm_gizmo_geometryinfo_draw(const GizmoGeomInfo *info, const bool UNUSED(select), const float color[4]) +{ + /* TODO store the Batches inside the GizmoGeomInfo and updated it when geom changes + * So we don't need to re-created and discard it every time */ + + GPUVertBuf *vbo; + GPUIndexBuf *el; + GPUBatch *batch; + GPUIndexBufBuilder elb = {0}; + + GPUVertFormat format = {0}; + uint pos_id = GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT); + + /* Elements */ + GPU_indexbuf_init(&elb, GPU_PRIM_TRIS, info->ntris, info->nverts); + for (int i = 0; i < info->ntris; ++i) { + const unsigned short *idx = &info->indices[i * 3]; + GPU_indexbuf_add_tri_verts(&elb, idx[0], idx[1], idx[2]); + } + el = GPU_indexbuf_build(&elb); + + vbo = GPU_vertbuf_create_with_format(&format); + GPU_vertbuf_data_alloc(vbo, info->nverts); + + GPU_vertbuf_attr_fill(vbo, pos_id, info->verts); + + batch = GPU_batch_create_ex(GPU_PRIM_TRIS, vbo, el, GPU_BATCH_OWNS_VBO | GPU_BATCH_OWNS_INDEX); + GPU_batch_program_set_builtin(batch, GPU_SHADER_3D_UNIFORM_COLOR); + + GPU_batch_uniform_4fv(batch, "color", color); + + /* We may want to re-visit this, for now disable + * since it causes issues leaving the GL state modified. */ +#if 0 + glEnable(GL_CULL_FACE); + GPU_depth_test(true); +#endif + + GPU_batch_draw(batch); + +#if 0 + GPU_depth_test(false); + glDisable(GL_CULL_FACE); +#endif + + + GPU_batch_discard(batch); +} + +void wm_gizmo_vec_draw( + const float color[4], const float (*verts)[3], uint vert_count, + uint pos, uint primitive_type) +{ + immUniformColor4fv(color); + immBegin(primitive_type, vert_count); + for (int i = 0; i < vert_count; i++) { + immVertex3fv(pos, verts[i]); + } + immEnd(); +} diff --git a/source/blender/editors/gizmo_library/gizmo_geometry.h b/source/blender/editors/gizmo_library/gizmo_geometry.h new file mode 100644 index 00000000000..53541b0aa01 --- /dev/null +++ b/source/blender/editors/gizmo_library/gizmo_geometry.h @@ -0,0 +1,54 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2016 Blender Foundation. + * All rights reserved. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file gizmo_geometry.h + * \ingroup edgizmolib + * + * \name Gizmo Geometry + * + * \brief Prototypes for arrays defining the gizmo geometry. The actual definitions can be found in files usually + * called geom_xxx_gizmo.c + */ + + +#ifndef __GIZMO_GEOMETRY_H__ +#define __GIZMO_GEOMETRY_H__ + +typedef struct GizmoGeomInfo { + int nverts; + int ntris; + const float (*verts)[3]; + const float (*normals)[3]; + const unsigned short *indices; +} GizmoGeomInfo; + +/* arrow gizmo */ +extern GizmoGeomInfo wm_gizmo_geom_data_arrow; + +/* cube gizmo */ +extern GizmoGeomInfo wm_gizmo_geom_data_cube; + +/* dial gizmo */ +extern GizmoGeomInfo wm_gizmo_geom_data_dial; + +#endif /* __GIZMO_GEOMETRY_H__ */ diff --git a/source/blender/editors/gizmo_library/gizmo_group_types/value2d_gizmo_group.c b/source/blender/editors/gizmo_library/gizmo_group_types/value2d_gizmo_group.c new file mode 100644 index 00000000000..274d35269d1 --- /dev/null +++ b/source/blender/editors/gizmo_library/gizmo_group_types/value2d_gizmo_group.c @@ -0,0 +1,176 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file value2d_gizmo_group.c + * \ingroup edgizmolib + * + * \name 2D Value Gizmo + * + * \brief Gizmo that edits a value for operator redo. + */ + +#include "BLI_utildefines.h" + +#include "BKE_context.h" + +#include "ED_undo.h" +#include "ED_screen.h" +#include "ED_gizmo_library.h" + +#include "UI_resources.h" + +#include "MEM_guardedalloc.h" + +#include "RNA_access.h" + +#include "WM_api.h" +#include "WM_types.h" + +/* -------------------------------------------------------------------- */ +/** \name Value Gizmo + * \{ */ + +struct ValueOpRedoGroup { + wmGizmo *gizmo; + struct { + const bContext *context; /* needed for redo. */ + wmOperator *op; + } state; +}; + +static void gizmo_op_redo_exec(struct ValueOpRedoGroup *igzgroup) +{ + wmOperator *op = igzgroup->state.op; + if (op == WM_operator_last_redo((bContext *)igzgroup->state.context)) { + ED_undo_operator_repeat((bContext *)igzgroup->state.context, op); + } +} + +/* translate callbacks */ +static void gizmo_value_operator_redo_value_get( + const wmGizmo *gz, wmGizmoProperty *gz_prop, + void *value_p) +{ + float *value = value_p; + BLI_assert(gz_prop->type->array_length == 1); + UNUSED_VARS_NDEBUG(gz_prop); + + struct ValueOpRedoGroup *igzgroup = gz->parent_gzgroup->customdata; + wmOperator *op = igzgroup->state.op; + *value = RNA_property_float_get(op->ptr, op->type->prop); +} + +static void gizmo_value_operator_redo_value_set( + const wmGizmo *gz, wmGizmoProperty *gz_prop, + const void *value_p) +{ + const float *value = value_p; + BLI_assert(gz_prop->type->array_length == 1); + UNUSED_VARS_NDEBUG(gz_prop); + + struct ValueOpRedoGroup *igzgroup = gz->parent_gzgroup->customdata; + wmOperator *op = igzgroup->state.op; + RNA_property_float_set(op->ptr, op->type->prop, *value); + gizmo_op_redo_exec(igzgroup); +} + +static void WIDGETGROUP_value_operator_redo_modal_from_setup( + const bContext *C, wmGizmoGroup *gzgroup) +{ + /* Start off dragging. */ + wmWindow *win = CTX_wm_window(C); + wmGizmo *gz = gzgroup->gizmos.first; + wmGizmoMap *gzmap = gzgroup->parent_gzmap; + WM_gizmo_modal_set_from_setup( + gzmap, (bContext *)C, gz, 0, win->eventstate); +} + +static void WIDGETGROUP_value_operator_redo_setup(const bContext *C, wmGizmoGroup *gzgroup) +{ + struct ValueOpRedoGroup *igzgroup = MEM_mallocN(sizeof(struct ValueOpRedoGroup), __func__); + + igzgroup->gizmo = WM_gizmo_new("GIZMO_GT_value_2d", gzgroup, NULL); + wmGizmo *gz = igzgroup->gizmo; + + igzgroup->state.context = C; + igzgroup->state.op = WM_operator_last_redo(C); + + gzgroup->customdata = igzgroup; + + UI_GetThemeColor3fv(TH_GIZMO_PRIMARY, gz->color); + UI_GetThemeColor3fv(TH_GIZMO_HI, gz->color_hi); + + WM_gizmo_target_property_def_func( + gz, "offset", + &(const struct wmGizmoPropertyFnParams) { + .value_get_fn = gizmo_value_operator_redo_value_get, + .value_set_fn = gizmo_value_operator_redo_value_set, + .range_get_fn = NULL, + .user_data = igzgroup, + }); + + /* Become modal as soon as it's started. */ + WIDGETGROUP_value_operator_redo_modal_from_setup(C, gzgroup); +} + +static void WIDGETGROUP_value_operator_redo_refresh(const bContext *UNUSED(C), wmGizmoGroup *gzgroup) +{ + struct ValueOpRedoGroup *igzgroup = gzgroup->customdata; + wmGizmo *gz = igzgroup->gizmo; + wmOperator *op = WM_operator_last_redo((bContext *)igzgroup->state.context); + wmGizmoMap *gzmap = gzgroup->parent_gzmap; + + /* FIXME */ + extern struct wmGizmo *wm_gizmomap_modal_get(struct wmGizmoMap *gzmap); + if ((op != igzgroup->state.op) || + (wm_gizmomap_modal_get(gzmap) != gz)) + { + WM_gizmo_group_type_unlink_delayed_ptr(gzgroup->type); + } +} + +static void WM_GGT_value_operator_redo(wmGizmoGroupType *gzgt) +{ + gzgt->name = "Value Operator Redo"; + gzgt->idname = "WM_GGT_value_operator_redo"; + + /* FIXME, allow multiple. */ + gzgt->flag = WM_GIZMOGROUPTYPE_3D | WM_GIZMOGROUPTYPE_TOOL_INIT; + + gzgt->gzmap_params.spaceid = SPACE_VIEW3D; + gzgt->gzmap_params.regionid = RGN_TYPE_WINDOW; + + + gzgt->setup = WIDGETGROUP_value_operator_redo_setup; + gzgt->refresh = WIDGETGROUP_value_operator_redo_refresh; +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Public API + * \{ */ + +void ED_gizmogrouptypes_value_2d(void) +{ + WM_gizmogrouptype_append(WM_GGT_value_operator_redo); +} + +/** \} */ diff --git a/source/blender/editors/gizmo_library/gizmo_library_intern.h b/source/blender/editors/gizmo_library/gizmo_library_intern.h new file mode 100644 index 00000000000..db88a52f087 --- /dev/null +++ b/source/blender/editors/gizmo_library/gizmo_library_intern.h @@ -0,0 +1,111 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2016 Blender Foundation. + * All rights reserved. + * + * Contributor(s): none yet. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file gizmo_library_intern.h + * \ingroup edgizmolib + */ + +#ifndef __GIZMO_LIBRARY_INTERN_H__ +#define __GIZMO_LIBRARY_INTERN_H__ + +/* distance around which gizmos respond to input (and get highlighted) */ +#define GIZMO_HOTSPOT 14.0f + +/** + * Data for common interactions. Used in gizmo_library_utils.c functions. + */ +typedef struct GizmoCommonData { + int flag; + + float range_fac; /* factor for arrow min/max distance */ + float offset; + + /* property range for constrained gizmos */ + float range; + /* min/max value for constrained gizmos */ + float min, max; +} GizmoCommonData; + +typedef struct GizmoInteraction { + float init_value; /* initial property value */ + float init_mval[2]; + float init_offset; + float init_matrix_final[4][4]; + float init_matrix_basis[4][4]; + + /* offset of last handling step */ + float prev_offset; + /* Total offset added by precision tweaking. + * Needed to allow toggling precision on/off without causing jumps */ + float precision_offset; +} GizmoInteraction; + +/* GizmoCommonData->flag */ +enum { + GIZMO_CUSTOM_RANGE_SET = (1 << 0), +}; + + +float gizmo_offset_from_value( + GizmoCommonData *data, const float value, + const bool constrained, const bool inverted); +float gizmo_value_from_offset( + GizmoCommonData *data, GizmoInteraction *inter, const float offset, + const bool constrained, const bool inverted, const bool use_precision); + +void gizmo_property_data_update( + struct wmGizmo *gz, GizmoCommonData *data, wmGizmoProperty *gz_prop, + const bool constrained, const bool inverted); + +void gizmo_property_value_reset( + bContext *C, const struct wmGizmo *gz, GizmoInteraction *inter, wmGizmoProperty *gz_prop); + + +/* -------------------------------------------------------------------- */ + +void gizmo_color_get( + const struct wmGizmo *gz, const bool highlight, + float r_color[4]); + +bool gizmo_window_project_2d( + bContext *C, const struct wmGizmo *gz, const float mval[2], int axis, bool use_offset, + float r_co[2]); + +bool gizmo_window_project_3d( + bContext *C, const struct wmGizmo *gz, const float mval[2], bool use_offset, + float r_co[3]); + +/* -------------------------------------------------------------------- */ +/* Gizmo drawing */ + +#include "gizmo_geometry.h" + +void wm_gizmo_geometryinfo_draw(const struct GizmoGeomInfo *info, const bool select, const float color[4]); +void wm_gizmo_vec_draw( + const float color[4], const float (*verts)[3], uint vert_count, + uint pos, uint primitive_type); + + +#endif /* __GIZMO_LIBRARY_INTERN_H__ */ diff --git a/source/blender/editors/gizmo_library/gizmo_library_presets.c b/source/blender/editors/gizmo_library/gizmo_library_presets.c new file mode 100644 index 00000000000..bd047ba1da4 --- /dev/null +++ b/source/blender/editors/gizmo_library/gizmo_library_presets.c @@ -0,0 +1,150 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file blender/editors/gizmo_library/gizmo_library_presets.c + * \ingroup edgizmolib + * + * \name Gizmo Lib Presets + * + * \brief Preset shapes that can be drawn from any gizmo type. + */ + +#include "MEM_guardedalloc.h" + +#include "BLI_math.h" + +#include "DNA_view3d_types.h" +#include "DNA_object_types.h" + +#include "BKE_context.h" + +#include "BIF_gl.h" + +#include "GPU_draw.h" +#include "GPU_immediate.h" +#include "GPU_immediate_util.h" +#include "GPU_matrix.h" +#include "GPU_select.h" + +#include "DEG_depsgraph.h" + +#include "RNA_access.h" + +#include "WM_types.h" +#include "WM_api.h" + +#include "ED_view3d.h" +#include "ED_screen.h" + +/* own includes */ +#include "ED_gizmo_library.h" /* own include */ +#include "gizmo_library_intern.h" /* own include */ + +/* TODO, this is to be used by RNA. might move to ED_gizmo_library */ + +/** + * Given a single axis, orient the matrix to a different direction. + */ +static void single_axis_convert( + int src_axis, float src_mat[4][4], + int dst_axis, float dst_mat[4][4]) +{ + copy_m4_m4(dst_mat, src_mat); + if (src_axis == dst_axis) { + return; + } + + float rotmat[3][3]; + mat3_from_axis_conversion_single(src_axis, dst_axis, rotmat); + transpose_m3(rotmat); + mul_m4_m4m3(dst_mat, src_mat, rotmat); +} + +/** + * Use for all geometry. + */ +static void ed_gizmo_draw_preset_geometry( + const struct wmGizmo *gz, float mat[4][4], int select_id, + const GizmoGeomInfo *info) +{ + const bool is_select = (select_id != -1); + const bool is_highlight = is_select && (gz->state & WM_GIZMO_STATE_HIGHLIGHT) != 0; + + float color[4]; + gizmo_color_get(gz, is_highlight, color); + + if (is_select) { + GPU_select_load_id(select_id); + } + + GPU_matrix_push(); + GPU_matrix_mul(mat); + wm_gizmo_geometryinfo_draw(info, is_select, color); + GPU_matrix_pop(); + + if (is_select) { + GPU_select_load_id(-1); + } +} + +void ED_gizmo_draw_preset_box( + const struct wmGizmo *gz, float mat[4][4], int select_id) +{ + ed_gizmo_draw_preset_geometry(gz, mat, select_id, &wm_gizmo_geom_data_cube); +} + +void ED_gizmo_draw_preset_arrow( + const struct wmGizmo *gz, float mat[4][4], int axis, int select_id) +{ + float mat_rotate[4][4]; + single_axis_convert(OB_POSZ, mat, axis, mat_rotate); + ed_gizmo_draw_preset_geometry(gz, mat_rotate, select_id, &wm_gizmo_geom_data_arrow); +} + +void ED_gizmo_draw_preset_circle( + const struct wmGizmo *gz, float mat[4][4], int axis, int select_id) +{ + float mat_rotate[4][4]; + single_axis_convert(OB_POSZ, mat, axis, mat_rotate); + ed_gizmo_draw_preset_geometry(gz, mat_rotate, select_id, &wm_gizmo_geom_data_dial); +} + +void ED_gizmo_draw_preset_facemap( + const bContext *C, const struct wmGizmo *gz, struct Scene *scene, Object *ob, const int facemap, int select_id) +{ + const bool is_select = (select_id != -1); + const bool is_highlight = is_select && (gz->state & WM_GIZMO_STATE_HIGHLIGHT) != 0; + + float color[4]; + gizmo_color_get(gz, is_highlight, color); + + if (is_select) { + GPU_select_load_id(select_id); + } + + GPU_matrix_push(); + GPU_matrix_mul(ob->obmat); + ED_draw_object_facemap(CTX_data_depsgraph(C), scene, ob, color, facemap); + GPU_matrix_pop(); + + if (is_select) { + GPU_select_load_id(-1); + } +} diff --git a/source/blender/editors/gizmo_library/gizmo_library_utils.c b/source/blender/editors/gizmo_library/gizmo_library_utils.c new file mode 100644 index 00000000000..350c9922179 --- /dev/null +++ b/source/blender/editors/gizmo_library/gizmo_library_utils.c @@ -0,0 +1,246 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2015 Blender Foundation. + * All rights reserved. + * + * Contributor(s): none yet. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file gizmo_library_utils.c + * \ingroup edgizmolib + * + * \name Gizmo Library Utilities + * + * \brief This file contains functions for common behaviors of gizmos. + */ + +#include "BLI_math.h" +#include "BLI_listbase.h" + +#include "DNA_view3d_types.h" +#include "DNA_screen_types.h" + +#include "BKE_context.h" + +#include "RNA_access.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "ED_view3d.h" + +/* own includes */ +#include "gizmo_library_intern.h" + +/* factor for precision tweaking */ +#define GIZMO_PRECISION_FAC 0.05f + + +BLI_INLINE float gizmo_offset_from_value_constr( + const float range_fac, const float min, const float range, const float value, + const bool inverted) +{ + return inverted ? (range_fac * (min + range - value) / range) : (range_fac * (value / range)); +} + +BLI_INLINE float gizmo_value_from_offset_constr( + const float range_fac, const float min, const float range, const float value, + const bool inverted) +{ + return inverted ? (min + range - (value * range / range_fac)) : (value * range / range_fac); +} + +float gizmo_offset_from_value( + GizmoCommonData *data, const float value, const bool constrained, const bool inverted) +{ + if (constrained) + return gizmo_offset_from_value_constr(data->range_fac, data->min, data->range, value, inverted); + + return value; +} + +float gizmo_value_from_offset( + GizmoCommonData *data, GizmoInteraction *inter, const float offset, + const bool constrained, const bool inverted, const bool use_precision) +{ + const float max = data->min + data->range; + + if (use_precision) { + /* add delta offset of this step to total precision_offset */ + inter->precision_offset += offset - inter->prev_offset; + } + inter->prev_offset = offset; + + float ofs_new = inter->init_offset + offset - inter->precision_offset * (1.0f - GIZMO_PRECISION_FAC); + float value; + + if (constrained) { + value = gizmo_value_from_offset_constr(data->range_fac, data->min, data->range, ofs_new, inverted); + } + else { + value = ofs_new; + } + + /* clamp to custom range */ + if (data->flag & GIZMO_CUSTOM_RANGE_SET) { + CLAMP(value, data->min, max); + } + + return value; +} + +void gizmo_property_data_update( + wmGizmo *gz, GizmoCommonData *data, wmGizmoProperty *gz_prop, + const bool constrained, const bool inverted) +{ + if (gz_prop->custom_func.value_get_fn != NULL) { + /* pass */ + } + else if (gz_prop->prop != NULL) { + /* pass */ + } + else { + data->offset = 0.0f; + return; + } + + float value = WM_gizmo_target_property_float_get(gz, gz_prop); + + if (constrained) { + if ((data->flag & GIZMO_CUSTOM_RANGE_SET) == 0) { + float range[2]; + if (WM_gizmo_target_property_float_range_get(gz, gz_prop, range)) { + data->range = range[1] - range[0]; + data->min = range[0]; + } + else { + BLI_assert(0); + } + } + data->offset = gizmo_offset_from_value_constr(data->range_fac, data->min, data->range, value, inverted); + } + else { + data->offset = value; + } +} + +void gizmo_property_value_reset( + bContext *C, const wmGizmo *gz, GizmoInteraction *inter, + wmGizmoProperty *gz_prop) +{ + WM_gizmo_target_property_float_set(C, gz, gz_prop, inter->init_value); +} + +/* -------------------------------------------------------------------- */ + +void gizmo_color_get( + const wmGizmo *gz, const bool highlight, + float r_col[4]) +{ + if (highlight && !(gz->flag & WM_GIZMO_DRAW_HOVER)) { + copy_v4_v4(r_col, gz->color_hi); + } + else { + copy_v4_v4(r_col, gz->color); + } +} + +/* -------------------------------------------------------------------- */ + +/** + * Takes mouse coordinates and returns them in relation to the gizmo. + * Both 2D & 3D supported, use so we can use 2D gizmos in the 3D view. + */ +bool gizmo_window_project_2d( + bContext *C, const struct wmGizmo *gz, const float mval[2], int axis, bool use_offset, + float r_co[2]) +{ + float mat[4][4]; + { + float mat_identity[4][4]; + struct WM_GizmoMatrixParams params = {NULL}; + if (use_offset == false) { + unit_m4(mat_identity); + params.matrix_offset = mat_identity; + } + WM_gizmo_calc_matrix_final_params(gz, ¶ms, mat); + } + + /* rotate mouse in relation to the center and relocate it */ + if (gz->parent_gzgroup->type->flag & WM_GIZMOGROUPTYPE_3D) { + /* For 3d views, transform 2D mouse pos onto plane. */ + ARegion *ar = CTX_wm_region(C); + + float plane[4], co[3]; + plane_from_point_normal_v3(plane, mat[3], mat[2]); + if (ED_view3d_win_to_3d_on_plane(ar, plane, mval, true, co)) { + float imat[4][4]; + invert_m4_m4(imat, mat); + mul_m4_v3(imat, co); + r_co[0] = co[(axis + 1) % 3]; + r_co[1] = co[(axis + 2) % 3]; + return true; + } + return false; + } + else { + float co[3] = {mval[0], mval[1], 0.0f}; + float imat[4][4]; + invert_m4_m4(imat, mat); + mul_m4_v3(imat, co); + copy_v2_v2(r_co, co); + return true; + } +} + +bool gizmo_window_project_3d( + bContext *C, const struct wmGizmo *gz, const float mval[2], bool use_offset, + float r_co[3]) +{ + float mat[4][4]; + { + float mat_identity[4][4]; + struct WM_GizmoMatrixParams params = {NULL}; + if (use_offset == false) { + unit_m4(mat_identity); + params.matrix_offset = mat_identity; + } + WM_gizmo_calc_matrix_final_params(gz, ¶ms, mat); + } + + if (gz->parent_gzgroup->type->flag & WM_GIZMOGROUPTYPE_3D) { + View3D *v3d = CTX_wm_view3d(C); + ARegion *ar = CTX_wm_region(C); + /* Note: we might want a custom reference point passed in, + * instead of the gizmo center. */ + ED_view3d_win_to_3d(v3d, ar, mat[3], mval, r_co); + invert_m4(mat); + mul_m4_v3(mat, r_co); + return true; + } + else { + float co[3] = {mval[0], mval[1], 0.0f}; + float imat[4][4]; + invert_m4_m4(imat, mat); + mul_m4_v3(imat, co); + copy_v2_v2(r_co, co); + return true; + } +} diff --git a/source/blender/editors/gizmo_library/gizmo_types/arrow2d_gizmo.c b/source/blender/editors/gizmo_library/gizmo_types/arrow2d_gizmo.c new file mode 100644 index 00000000000..fb3583b7b27 --- /dev/null +++ b/source/blender/editors/gizmo_library/gizmo_types/arrow2d_gizmo.c @@ -0,0 +1,225 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2016 Blender Foundation. + * All rights reserved. + * + * Contributor(s): none yet. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file arrow2d_gizmo.c + * \ingroup edgizmolib + * + * \name 2D Arrow Gizmo + * + * \brief Simple arrow gizmo which is dragged into a certain direction. + */ + +#include "BLI_listbase.h" +#include "BLI_math.h" +#include "BLI_rect.h" + +#include "DNA_windowmanager_types.h" + +#include "BKE_context.h" + +#include "BIF_gl.h" + +#include "GPU_draw.h" +#include "GPU_immediate.h" +#include "GPU_matrix.h" +#include "GPU_state.h" + +#include "MEM_guardedalloc.h" + +#include "RNA_access.h" +#include "RNA_define.h" + +#include "WM_types.h" + +#include "ED_screen.h" +#include "ED_gizmo_library.h" + +/* own includes */ +#include "WM_api.h" + +#include "../gizmo_library_intern.h" + +static void arrow2d_draw_geom(wmGizmo *gz, const float matrix[4][4], const float color[4]) +{ + const float size = 0.11f; + const float size_breadth = size / 2.0f; + const float size_length = size * 1.7f; + /* Subtract the length so the arrow fits in the hotspot. */ + const float arrow_length = RNA_float_get(gz->ptr, "length") - size_length; + const float arrow_angle = RNA_float_get(gz->ptr, "angle"); + + uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); + + GPU_matrix_push(); + GPU_matrix_mul(matrix); + GPU_matrix_rotate_2d(RAD2DEGF(arrow_angle)); + + immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); + + immUniformColor4fv(color); + + immBegin(GPU_PRIM_LINES, 2); + immVertex2f(pos, 0.0f, 0.0f); + immVertex2f(pos, 0.0f, arrow_length); + immEnd(); + + immBegin(GPU_PRIM_TRIS, 3); + immVertex2f(pos, size_breadth, arrow_length); + immVertex2f(pos, -size_breadth, arrow_length); + immVertex2f(pos, 0.0f, arrow_length + size_length); + immEnd(); + + immUnbindProgram(); + + GPU_matrix_pop(); +} + +static void gizmo_arrow2d_draw(const bContext *UNUSED(C), wmGizmo *gz) +{ + float color[4]; + + float matrix_final[4][4]; + + gizmo_color_get(gz, gz->state & WM_GIZMO_STATE_HIGHLIGHT, color); + + GPU_line_width(gz->line_width); + + WM_gizmo_calc_matrix_final(gz, matrix_final); + + GPU_blend(true); + arrow2d_draw_geom(gz, matrix_final, color); + GPU_blend(false); + + if (gz->interaction_data) { + GizmoInteraction *inter = gz->interaction_data; + + GPU_blend(true); + arrow2d_draw_geom(gz, inter->init_matrix_final, (const float[4]){0.5f, 0.5f, 0.5f, 0.5f}); + GPU_blend(false); + } +} + +static void gizmo_arrow2d_setup(wmGizmo *gz) +{ + gz->flag |= WM_GIZMO_DRAW_MODAL; +} + +static int gizmo_arrow2d_invoke( + bContext *UNUSED(C), wmGizmo *gz, const wmEvent *UNUSED(event)) +{ + GizmoInteraction *inter = MEM_callocN(sizeof(GizmoInteraction), __func__); + + copy_m4_m4(inter->init_matrix_basis, gz->matrix_basis); + WM_gizmo_calc_matrix_final(gz, inter->init_matrix_final); + + gz->interaction_data = inter; + + return OPERATOR_RUNNING_MODAL; +} + +static int gizmo_arrow2d_test_select( + bContext *UNUSED(C), wmGizmo *gz, const int mval[2]) +{ + const float mval_fl[2] = {UNPACK2(mval)}; + const float arrow_length = RNA_float_get(gz->ptr, "length"); + const float arrow_angle = RNA_float_get(gz->ptr, "angle"); + const float line_len = arrow_length * gz->scale_final; + float mval_local[2]; + + copy_v2_v2(mval_local, mval_fl); + sub_v2_v2(mval_local, gz->matrix_basis[3]); + + float line[2][2]; + line[0][0] = line[0][1] = line[1][0] = 0.0f; + line[1][1] = line_len; + + /* rotate only if needed */ + if (arrow_angle != 0.0f) { + float rot_point[2]; + copy_v2_v2(rot_point, line[1]); + rotate_v2_v2fl(line[1], rot_point, arrow_angle); + } + + /* arrow line intersection check */ + float isect_1[2], isect_2[2]; + const int isect = isect_line_sphere_v2( + line[0], line[1], mval_local, GIZMO_HOTSPOT + gz->line_width * 0.5f, + isect_1, isect_2); + + if (isect > 0) { + float line_ext[2][2]; /* extended line for segment check including hotspot */ + copy_v2_v2(line_ext[0], line[0]); + line_ext[1][0] = line[1][0] + GIZMO_HOTSPOT * ((line[1][0] - line[0][0]) / line_len); + line_ext[1][1] = line[1][1] + GIZMO_HOTSPOT * ((line[1][1] - line[0][1]) / line_len); + + const float lambda_1 = line_point_factor_v2(isect_1, line_ext[0], line_ext[1]); + if (isect == 1) { + if (IN_RANGE_INCL(lambda_1, 0.0f, 1.0f)) { + return 0; + } + } + else { + BLI_assert(isect == 2); + const float lambda_2 = line_point_factor_v2(isect_2, line_ext[0], line_ext[1]); + if (IN_RANGE_INCL(lambda_1, 0.0f, 1.0f) && IN_RANGE_INCL(lambda_2, 0.0f, 1.0f)) { + return 0; + } + } + } + + return -1; +} + +/* -------------------------------------------------------------------- */ +/** \name 2D Arrow Gizmo API + * + * \{ */ + +static void GIZMO_GT_arrow_2d(wmGizmoType *gzt) +{ + /* identifiers */ + gzt->idname = "GIZMO_GT_arrow_2d"; + + /* api callbacks */ + gzt->draw = gizmo_arrow2d_draw; + gzt->setup = gizmo_arrow2d_setup; + gzt->invoke = gizmo_arrow2d_invoke; + gzt->test_select = gizmo_arrow2d_test_select; + + gzt->struct_size = sizeof(wmGizmo); + + /* rna */ + RNA_def_float(gzt->srna, "length", 1.0f, 0.0f, FLT_MAX, "Arrow Line Length", "", 0.0f, FLT_MAX); + RNA_def_float_rotation( + gzt->srna, "angle", 0, NULL, DEG2RADF(-360.0f), DEG2RADF(360.0f), + "Roll", "", DEG2RADF(-360.0f), DEG2RADF(360.0f)); +} + +void ED_gizmotypes_arrow_2d(void) +{ + WM_gizmotype_append(GIZMO_GT_arrow_2d); +} + +/** \} */ diff --git a/source/blender/editors/gizmo_library/gizmo_types/arrow3d_gizmo.c b/source/blender/editors/gizmo_library/gizmo_types/arrow3d_gizmo.c new file mode 100644 index 00000000000..4e41f95a063 --- /dev/null +++ b/source/blender/editors/gizmo_library/gizmo_types/arrow3d_gizmo.c @@ -0,0 +1,491 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2014 Blender Foundation. + * All rights reserved. + * + * Contributor(s): Blender Foundation + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file arrow3d_gizmo.c + * \ingroup edgizmolib + * + * \name Arrow Gizmo + * + * 3D Gizmo + * + * \brief Simple arrow gizmo which is dragged into a certain direction. + * The arrow head can have varying shapes, e.g. cone, box, etc. + * + * - `matrix[0]` is derived from Y and Z. + * - `matrix[1]` is 'up' for gizmo types that have an up. + * - `matrix[2]` is the arrow direction (for all arrowes). + */ + +#include "BIF_gl.h" + +#include "BLI_math.h" + +#include "DNA_view3d_types.h" + +#include "BKE_context.h" + +#include "GPU_draw.h" +#include "GPU_immediate.h" +#include "GPU_immediate_util.h" +#include "GPU_matrix.h" +#include "GPU_select.h" +#include "GPU_state.h" + +#include "MEM_guardedalloc.h" + +#include "RNA_access.h" +#include "RNA_define.h" + +#include "WM_types.h" +#include "WM_api.h" + +#include "ED_view3d.h" +#include "ED_screen.h" +#include "ED_gizmo_library.h" + +/* own includes */ +#include "../gizmo_geometry.h" +#include "../gizmo_library_intern.h" + +/* to use custom arrows exported to geom_arrow_gizmo.c */ +//#define USE_GIZMO_CUSTOM_ARROWS + +typedef struct ArrowGizmo3D { + wmGizmo gizmo; + GizmoCommonData data; +} ArrowGizmo3D; + + +/* -------------------------------------------------------------------- */ + +static void gizmo_arrow_matrix_basis_get(const wmGizmo *gz, float r_matrix[4][4]) +{ + ArrowGizmo3D *arrow = (ArrowGizmo3D *)gz; + + copy_m4_m4(r_matrix, arrow->gizmo.matrix_basis); + madd_v3_v3fl(r_matrix[3], arrow->gizmo.matrix_basis[2], arrow->data.offset); +} + +static void arrow_draw_geom(const ArrowGizmo3D *arrow, const bool select, const float color[4]) +{ + uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT); + bool unbind_shader = true; + const int draw_style = RNA_enum_get(arrow->gizmo.ptr, "draw_style"); + const int draw_options = RNA_enum_get(arrow->gizmo.ptr, "draw_options"); + + immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); + + if (draw_style == ED_GIZMO_ARROW_STYLE_CROSS) { + immUniformColor4fv(color); + + immBegin(GPU_PRIM_LINES, 4); + immVertex3f(pos, -1.0f, 0.0f, 0.0f); + immVertex3f(pos, 1.0f, 0.0f, 0.0f); + immVertex3f(pos, 0.0f, -1.0f, 0.0f); + immVertex3f(pos, 0.0f, 1.0f, 0.0f); + immEnd(); + } + else if (draw_style == ED_GIZMO_ARROW_STYLE_CONE) { + float aspect[2]; + RNA_float_get_array(arrow->gizmo.ptr, "aspect", aspect); + const float unitx = aspect[0]; + const float unity = aspect[1]; + const float vec[4][3] = { + {-unitx, -unity, 0}, + { unitx, -unity, 0}, + { unitx, unity, 0}, + {-unitx, unity, 0}, + }; + + GPU_line_width(arrow->gizmo.line_width); + wm_gizmo_vec_draw(color, vec, ARRAY_SIZE(vec), pos, GPU_PRIM_LINE_LOOP); + } + else { +#ifdef USE_GIZMO_CUSTOM_ARROWS + wm_gizmo_geometryinfo_draw(&wm_gizmo_geom_data_arrow, select, color); +#else + const float arrow_length = RNA_float_get(arrow->gizmo.ptr, "length"); + + const float vec[2][3] = { + {0.0f, 0.0f, 0.0f}, + {0.0f, 0.0f, arrow_length}, + }; + + if (draw_options & ED_GIZMO_ARROW_DRAW_FLAG_STEM) { + GPU_line_width(arrow->gizmo.line_width); + wm_gizmo_vec_draw(color, vec, ARRAY_SIZE(vec), pos, GPU_PRIM_LINE_STRIP); + } + else { + immUniformColor4fv(color); + } + + /* *** draw arrow head *** */ + + GPU_matrix_push(); + + if (draw_style == ED_GIZMO_ARROW_STYLE_BOX) { + const float size = 0.05f; + + /* translate to line end with some extra offset so box starts exactly where line ends */ + GPU_matrix_translate_3f(0.0f, 0.0f, arrow_length + size); + /* scale down to box size */ + GPU_matrix_scale_3f(size, size, size); + + /* draw cube */ + immUnbindProgram(); + unbind_shader = false; + wm_gizmo_geometryinfo_draw(&wm_gizmo_geom_data_cube, select, color); + } + else { + BLI_assert(draw_style == ED_GIZMO_ARROW_STYLE_NORMAL); + + const float len = 0.25f; + const float width = 0.06f; + + /* translate to line end */ + GPU_matrix_translate_3f(0.0f, 0.0f, arrow_length); + + imm_draw_circle_fill_3d(pos, 0.0, 0.0, width, 8); + imm_draw_cylinder_fill_3d(pos, width, 0.0, len, 8, 1); + } + + GPU_matrix_pop(); +#endif /* USE_GIZMO_CUSTOM_ARROWS */ + } + + if (unbind_shader) { + immUnbindProgram(); + } +} + +static void arrow_draw_intern(ArrowGizmo3D *arrow, const bool select, const bool highlight) +{ + wmGizmo *gz = &arrow->gizmo; + float color[4]; + float matrix_final[4][4]; + + gizmo_color_get(gz, highlight, color); + + WM_gizmo_calc_matrix_final(gz, matrix_final); + + GPU_matrix_push(); + GPU_matrix_mul(matrix_final); + GPU_blend(true); + arrow_draw_geom(arrow, select, color); + GPU_blend(false); + + GPU_matrix_pop(); + + if (gz->interaction_data) { + GizmoInteraction *inter = gz->interaction_data; + + GPU_matrix_push(); + GPU_matrix_mul(inter->init_matrix_final); + + + GPU_blend(true); + arrow_draw_geom(arrow, select, (const float[4]){0.5f, 0.5f, 0.5f, 0.5f}); + GPU_blend(false); + + GPU_matrix_pop(); + } +} + +static void gizmo_arrow_draw_select( + const bContext *UNUSED(C), wmGizmo *gz, + int select_id) +{ + GPU_select_load_id(select_id); + arrow_draw_intern((ArrowGizmo3D *)gz, true, false); +} + +static void gizmo_arrow_draw(const bContext *UNUSED(C), wmGizmo *gz) +{ + arrow_draw_intern((ArrowGizmo3D *)gz, false, (gz->state & WM_GIZMO_STATE_HIGHLIGHT) != 0); +} + +/** + * Calculate arrow offset independent from prop min value, + * meaning the range will not be offset by min value first. + */ +static int gizmo_arrow_modal( + bContext *C, wmGizmo *gz, const wmEvent *event, + eWM_GizmoFlagTweak tweak_flag) +{ + if (event->type != MOUSEMOVE) { + return OPERATOR_RUNNING_MODAL; + } + ArrowGizmo3D *arrow = (ArrowGizmo3D *)gz; + GizmoInteraction *inter = gz->interaction_data; + ARegion *ar = CTX_wm_region(C); + RegionView3D *rv3d = ar->regiondata; + + float offset[3]; + float facdir = 1.0f; + + /* (src, dst) */ + struct { + float mval[2]; + float ray_origin[3], ray_direction[3]; + float location[3]; + } proj[2] = { + {.mval = {UNPACK2(inter->init_mval)}}, + {.mval = {UNPACK2(event->mval)}}, + }; + + float arrow_co[3]; + float arrow_no[3]; + copy_v3_v3(arrow_co, inter->init_matrix_basis[3]); + normalize_v3_v3(arrow_no, arrow->gizmo.matrix_basis[2]); + + int ok = 0; + + for (int j = 0; j < 2; j++) { + ED_view3d_win_to_ray( + ar, proj[j].mval, + proj[j].ray_origin, proj[j].ray_direction); + /* Force Y axis if we're view aligned */ + if (j == 0) { + if (RAD2DEGF(acosf(dot_v3v3(proj[j].ray_direction, arrow->gizmo.matrix_basis[2]))) < 5.0f) { + normalize_v3_v3(arrow_no, rv3d->viewinv[1]); + } + } + + float arrow_no_proj[3]; + project_plane_v3_v3v3(arrow_no_proj, arrow_no, proj[j].ray_direction); + + normalize_v3(arrow_no_proj); + + float plane[4]; + plane_from_point_normal_v3(plane, proj[j].ray_origin, arrow_no_proj); + + float lambda; + if (isect_ray_plane_v3(arrow_co, arrow_no, plane, &lambda, false)) { + madd_v3_v3v3fl(proj[j].location, arrow_co, arrow_no, lambda); + ok++; + } + } + + if (ok != 2) { + return OPERATOR_RUNNING_MODAL; + } + + sub_v3_v3v3(offset, proj[1].location, proj[0].location); + facdir = dot_v3v3(arrow_no, offset) < 0.0f ? -1 : 1; + + GizmoCommonData *data = &arrow->data; + const float ofs_new = facdir * len_v3(offset); + + wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset"); + + /* set the property for the operator and call its modal function */ + if (WM_gizmo_target_property_is_valid(gz_prop)) { + const int transform_flag = RNA_enum_get(arrow->gizmo.ptr, "transform"); + const bool constrained = (transform_flag & ED_GIZMO_ARROW_XFORM_FLAG_CONSTRAINED) != 0; + const bool inverted = (transform_flag & ED_GIZMO_ARROW_XFORM_FLAG_INVERTED) != 0; + const bool use_precision = (tweak_flag & WM_GIZMO_TWEAK_PRECISE) != 0; + float value = gizmo_value_from_offset(data, inter, ofs_new, constrained, inverted, use_precision); + + WM_gizmo_target_property_float_set(C, gz, gz_prop, value); + /* get clamped value */ + value = WM_gizmo_target_property_float_get(gz, gz_prop); + + data->offset = gizmo_offset_from_value(data, value, constrained, inverted); + } + else { + data->offset = ofs_new; + } + + /* tag the region for redraw */ + ED_region_tag_redraw(ar); + WM_event_add_mousemove(C); + + return OPERATOR_RUNNING_MODAL; +} + +static void gizmo_arrow_setup(wmGizmo *gz) +{ + ArrowGizmo3D *arrow = (ArrowGizmo3D *)gz; + + arrow->gizmo.flag |= WM_GIZMO_DRAW_MODAL; + + arrow->data.range_fac = 1.0f; +} + +static int gizmo_arrow_invoke( + bContext *UNUSED(C), wmGizmo *gz, const wmEvent *event) +{ + ArrowGizmo3D *arrow = (ArrowGizmo3D *)gz; + GizmoInteraction *inter = MEM_callocN(sizeof(GizmoInteraction), __func__); + wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset"); + + /* Some gizmos don't use properties. */ + if (WM_gizmo_target_property_is_valid(gz_prop)) { + inter->init_value = WM_gizmo_target_property_float_get(gz, gz_prop); + } + + inter->init_offset = arrow->data.offset; + + inter->init_mval[0] = event->mval[0]; + inter->init_mval[1] = event->mval[1]; + + gizmo_arrow_matrix_basis_get(gz, inter->init_matrix_basis); + WM_gizmo_calc_matrix_final(gz, inter->init_matrix_final); + + gz->interaction_data = inter; + + return OPERATOR_RUNNING_MODAL; +} + +static void gizmo_arrow_property_update(wmGizmo *gz, wmGizmoProperty *gz_prop) +{ + ArrowGizmo3D *arrow = (ArrowGizmo3D *)gz; + const int transform_flag = RNA_enum_get(arrow->gizmo.ptr, "transform"); + const bool constrained = (transform_flag & ED_GIZMO_ARROW_XFORM_FLAG_CONSTRAINED) != 0; + const bool inverted = (transform_flag & ED_GIZMO_ARROW_XFORM_FLAG_INVERTED) != 0; + gizmo_property_data_update(gz, &arrow->data, gz_prop, constrained, inverted); +} + +static void gizmo_arrow_exit(bContext *C, wmGizmo *gz, const bool cancel) +{ + ArrowGizmo3D *arrow = (ArrowGizmo3D *)gz; + GizmoCommonData *data = &arrow->data; + wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset"); + const bool is_prop_valid = WM_gizmo_target_property_is_valid(gz_prop); + + if (!cancel) { + /* Assign incase applying the opetration needs an updated offset + * editmesh bisect needs this. */ + if (is_prop_valid) { + data->offset = WM_gizmo_target_property_float_get(gz, gz_prop); + } + return; + } + + GizmoInteraction *inter = gz->interaction_data; + if (is_prop_valid) { + gizmo_property_value_reset(C, gz, inter, gz_prop); + } + data->offset = inter->init_offset; +} + + +/* -------------------------------------------------------------------- */ +/** \name Arrow Gizmo API + * + * \{ */ + +/** + * Define a custom property UI range + * + * \note Needs to be called before WM_gizmo_target_property_def_rna! + */ +void ED_gizmo_arrow3d_set_ui_range(wmGizmo *gz, const float min, const float max) +{ + ArrowGizmo3D *arrow = (ArrowGizmo3D *)gz; + + BLI_assert(min < max); + BLI_assert(!(WM_gizmo_target_property_is_valid(WM_gizmo_target_property_find(gz, "offset")) && + "Make sure this function is called before WM_gizmo_target_property_def_rna")); + + arrow->data.range = max - min; + arrow->data.min = min; + arrow->data.flag |= GIZMO_CUSTOM_RANGE_SET; +} + +/** + * Define a custom factor for arrow min/max distance + * + * \note Needs to be called before WM_gizmo_target_property_def_rna! + */ +void ED_gizmo_arrow3d_set_range_fac(wmGizmo *gz, const float range_fac) +{ + ArrowGizmo3D *arrow = (ArrowGizmo3D *)gz; + BLI_assert(!(WM_gizmo_target_property_is_valid(WM_gizmo_target_property_find(gz, "offset")) && + "Make sure this function is called before WM_gizmo_target_property_def_rna")); + + arrow->data.range_fac = range_fac; +} + +static void GIZMO_GT_arrow_3d(wmGizmoType *gzt) +{ + /* identifiers */ + gzt->idname = "GIZMO_GT_arrow_3d"; + + /* api callbacks */ + gzt->draw = gizmo_arrow_draw; + gzt->draw_select = gizmo_arrow_draw_select; + gzt->matrix_basis_get = gizmo_arrow_matrix_basis_get; + gzt->modal = gizmo_arrow_modal; + gzt->setup = gizmo_arrow_setup; + gzt->invoke = gizmo_arrow_invoke; + gzt->property_update = gizmo_arrow_property_update; + gzt->exit = gizmo_arrow_exit; + + gzt->struct_size = sizeof(ArrowGizmo3D); + + /* rna */ + static EnumPropertyItem rna_enum_draw_style_items[] = { + {ED_GIZMO_ARROW_STYLE_NORMAL, "NORMAL", 0, "Normal", ""}, + {ED_GIZMO_ARROW_STYLE_CROSS, "CROSS", 0, "Cross", ""}, + {ED_GIZMO_ARROW_STYLE_BOX, "BOX", 0, "Box", ""}, + {ED_GIZMO_ARROW_STYLE_CONE, "CONE", 0, "Cone", ""}, + {0, NULL, 0, NULL, NULL} + }; + static EnumPropertyItem rna_enum_draw_options_items[] = { + {ED_GIZMO_ARROW_DRAW_FLAG_STEM, "STEM", 0, "Stem", ""}, + {0, NULL, 0, NULL, NULL} + }; + static EnumPropertyItem rna_enum_transform_items[] = { + {ED_GIZMO_ARROW_XFORM_FLAG_INVERTED, "INVERT", 0, "Inverted", ""}, + {ED_GIZMO_ARROW_XFORM_FLAG_CONSTRAINED, "CONSTRAIN", 0, "Constrained", ""}, + {0, NULL, 0, NULL, NULL} + }; + + RNA_def_enum( + gzt->srna, "draw_style", rna_enum_draw_style_items, + ED_GIZMO_ARROW_STYLE_NORMAL, + "Draw Style", ""); + RNA_def_enum_flag( + gzt->srna, "draw_options", rna_enum_draw_options_items, + ED_GIZMO_ARROW_DRAW_FLAG_STEM, + "Draw Options", ""); + RNA_def_enum_flag( + gzt->srna, "transform", rna_enum_transform_items, + 0, + "Transform", ""); + + RNA_def_float(gzt->srna, "length", 1.0f, 0.0f, FLT_MAX, "Arrow Line Length", "", 0.0f, FLT_MAX); + RNA_def_float_vector(gzt->srna, "aspect", 2, NULL, 0, FLT_MAX, "Aspect", "Cone/box style only", 0.0f, FLT_MAX); + + WM_gizmotype_target_property_def(gzt, "offset", PROP_FLOAT, 1); +} + +void ED_gizmotypes_arrow_3d(void) +{ + WM_gizmotype_append(GIZMO_GT_arrow_3d); +} + +/** \} */ diff --git a/source/blender/editors/gizmo_library/gizmo_types/blank3d_gizmo.c b/source/blender/editors/gizmo_library/gizmo_types/blank3d_gizmo.c new file mode 100644 index 00000000000..6bff72d75cd --- /dev/null +++ b/source/blender/editors/gizmo_library/gizmo_types/blank3d_gizmo.c @@ -0,0 +1,89 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2014 Blender Foundation. + * All rights reserved. + * + * Contributor(s): Blender Foundation + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file blank3d_gizmo.c + * \ingroup edgizmolib + * + * \name Blank Gizmo + * + * \brief Gizmo to use as a fallback (catch events). + */ + +#include "BLI_math.h" + +#include "BKE_context.h" + +#include "ED_view3d.h" +#include "ED_gizmo_library.h" + +#include "WM_types.h" +#include "WM_api.h" + +/* own includes */ +#include "../gizmo_geometry.h" +#include "../gizmo_library_intern.h" + + +static void gizmo_blank_draw(const bContext *UNUSED(C), wmGizmo *UNUSED(gz)) +{ + /* pass */ +} + +static int gizmo_blank_invoke( + bContext *UNUSED(C), wmGizmo *UNUSED(gz), const wmEvent *UNUSED(event)) +{ + return OPERATOR_RUNNING_MODAL; +} + +static int gizmo_blank_test_select( + bContext *UNUSED(C), wmGizmo *UNUSED(gz), const int UNUSED(mval[2])) +{ + return 0; +} + +/* -------------------------------------------------------------------- */ +/** \name Blank Gizmo API + * + * \{ */ + +static void GIZMO_GT_blank_3d(wmGizmoType *gzt) +{ + /* identifiers */ + gzt->idname = "GIZMO_GT_blank_3d"; + + /* api callbacks */ + gzt->draw = gizmo_blank_draw; + gzt->invoke = gizmo_blank_invoke; + gzt->test_select = gizmo_blank_test_select; + + gzt->struct_size = sizeof(wmGizmo); +} + +void ED_gizmotypes_blank_3d(void) +{ + WM_gizmotype_append(GIZMO_GT_blank_3d); +} + +/** \} */ diff --git a/source/blender/editors/gizmo_library/gizmo_types/button2d_gizmo.c b/source/blender/editors/gizmo_library/gizmo_types/button2d_gizmo.c new file mode 100644 index 00000000000..6ba85037b8b --- /dev/null +++ b/source/blender/editors/gizmo_library/gizmo_types/button2d_gizmo.c @@ -0,0 +1,324 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file button2d_gizmo.c + * \ingroup edgizmolib + * + * \name Button Gizmo + * + * 2D Gizmo, also works in 3D views. + * + * \brief Single click button action for use in gizmo groups. + * + * \note Currently only basic icon & vector-shape buttons are supported. + * + */ + +#include "MEM_guardedalloc.h" + +#include "BLI_math.h" + +#include "BKE_context.h" + +#include "BIF_gl.h" +#include "BIF_glutil.h" + +#include "GPU_immediate.h" +#include "GPU_immediate_util.h" +#include "GPU_matrix.h" +#include "GPU_select.h" +#include "GPU_batch.h" +#include "GPU_batch_utils.h" +#include "GPU_state.h" + +#include "RNA_access.h" +#include "RNA_define.h" +#include "RNA_enum_types.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "ED_screen.h" +#include "ED_view3d.h" +#include "ED_gizmo_library.h" + +#include "UI_interface.h" +#include "UI_interface_icons.h" +#include "UI_resources.h" + +/* own includes */ +#include "../gizmo_geometry.h" +#include "../gizmo_library_intern.h" + +typedef struct ButtonGizmo2D { + wmGizmo gizmo; + bool is_init; + /* Use an icon or shape */ + int icon; + GPUBatch *shape_batch[2]; +} ButtonGizmo2D; + +#define CIRCLE_RESOLUTION 32 + +/* -------------------------------------------------------------------- */ + +static void button2d_geom_draw_backdrop( + const wmGizmo *gz, const float color[4], const bool select) +{ + GPU_line_width(gz->line_width); + + GPUVertFormat *format = immVertexFormat(); + uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); + + immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); + + immUniformColor4fv(color); + + /* TODO, other draw styles */ + imm_draw_circle_fill_2d(pos, 0, 0, 1.0f, CIRCLE_RESOLUTION); + + immUnbindProgram(); + + UNUSED_VARS(select); +} + +static void button2d_draw_intern( + const bContext *C, wmGizmo *gz, + const bool select, const bool highlight) +{ + ButtonGizmo2D *button = (ButtonGizmo2D *)gz; + + const int draw_options = RNA_enum_get(gz->ptr, "draw_options"); + if (button->is_init == false) { + button->is_init = true; + PropertyRNA *prop = RNA_struct_find_property(gz->ptr, "icon"); + if (RNA_property_is_set(gz->ptr, prop)) { + button->icon = RNA_property_enum_get(gz->ptr, prop); + } + else { + prop = RNA_struct_find_property(gz->ptr, "shape"); + const uint polys_len = RNA_property_string_length(gz->ptr, prop); + /* We shouldn't need the +1, but a NULL char is set. */ + char *polys = MEM_mallocN(polys_len + 1, __func__); + RNA_property_string_get(gz->ptr, prop, polys); + button->shape_batch[0] = GPU_batch_tris_from_poly_2d_encoded((uchar *)polys, polys_len, NULL); + button->shape_batch[1] = GPU_batch_wire_from_poly_2d_encoded((uchar *)polys, polys_len, NULL); + MEM_freeN(polys); + } + } + + float color[4]; + float matrix_final[4][4]; + + gizmo_color_get(gz, highlight, color); + WM_gizmo_calc_matrix_final(gz, matrix_final); + + + bool is_3d = (gz->parent_gzgroup->type->flag & WM_GIZMOGROUPTYPE_3D) != 0; + + if ((select == false) && + (draw_options & ED_GIZMO_BUTTON_SHOW_HELPLINE)) + { + float matrix_final_no_offset[4][4]; + WM_gizmo_calc_matrix_final_no_offset(gz, matrix_final_no_offset); + uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT); + immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); + immUniformColor4fv(color); + GPU_line_width(gz->line_width); + immUniformColor4fv(color); + immBegin(GPU_PRIM_LINE_STRIP, 2); + immVertex3fv(pos, matrix_final[3]); + immVertex3fv(pos, matrix_final_no_offset[3]); + immEnd(); + immUnbindProgram(); + } + + bool need_to_pop = true; + GPU_matrix_push(); + GPU_matrix_mul(matrix_final); + + if (is_3d) { + RegionView3D *rv3d = CTX_wm_region_view3d(C); + float matrix_align[4][4]; + float matrix_final_unit[4][4]; + normalize_m4_m4(matrix_final_unit, matrix_final); + mul_m4_m4m4(matrix_align, rv3d->viewmat, matrix_final_unit); + zero_v3(matrix_align[3]); + transpose_m4(matrix_align); + GPU_matrix_mul(matrix_align); + } + + if (select) { + BLI_assert(is_3d); + button2d_geom_draw_backdrop(gz, color, select); + } + else { + + GPU_blend(true); + if (button->shape_batch[0] != NULL) { + GPU_line_smooth(true); + GPU_polygon_smooth(false); + GPU_line_width(1.0f); + for (uint i = 0; i < ARRAY_SIZE(button->shape_batch) && button->shape_batch[i]; i++) { + /* Invert line color for wire. */ + GPU_batch_program_set_builtin(button->shape_batch[i], GPU_SHADER_2D_UNIFORM_COLOR); + GPU_batch_uniform_4f(button->shape_batch[i], "color", UNPACK4(color)); + GPU_batch_draw(button->shape_batch[i]); + + if (draw_options & ED_GIZMO_BUTTON_SHOW_OUTLINE) { + color[0] = 1.0f - color[0]; + color[1] = 1.0f - color[1]; + color[2] = 1.0f - color[2]; + } + } + GPU_line_smooth(false); + GPU_polygon_smooth(true); + } + else if (button->icon != ICON_NONE) { + button2d_geom_draw_backdrop(gz, color, select); + float size[2]; + if (is_3d) { + const float fac = 2.0f; + GPU_matrix_translate_2f(-(fac / 2), -(fac / 2)); + GPU_matrix_scale_2f(fac / (ICON_DEFAULT_WIDTH * UI_DPI_FAC), fac / (ICON_DEFAULT_HEIGHT * UI_DPI_FAC)); + size[0] = 1.0f; + size[1] = 1.0f; + } + else { + size[0] = gz->matrix_basis[3][0] - (ICON_DEFAULT_WIDTH / 2.0) * UI_DPI_FAC; + size[1] = gz->matrix_basis[3][1] - (ICON_DEFAULT_HEIGHT / 2.0) * UI_DPI_FAC; + GPU_matrix_pop(); + need_to_pop = false; + } + UI_icon_draw(size[0], size[1], button->icon); + } + GPU_blend(false); + } + + if (need_to_pop) { + GPU_matrix_pop(); + } +} + +static void gizmo_button2d_draw_select(const bContext *C, wmGizmo *gz, int select_id) +{ + GPU_select_load_id(select_id); + button2d_draw_intern(C, gz, true, false); +} + +static void gizmo_button2d_draw(const bContext *C, wmGizmo *gz) +{ + const bool is_highlight = (gz->state & WM_GIZMO_STATE_HIGHLIGHT) != 0; + + GPU_blend(true); + button2d_draw_intern(C, gz, false, is_highlight); + GPU_blend(false); +} + +static int gizmo_button2d_test_select( + bContext *C, wmGizmo *gz, const int mval[2]) +{ + float point_local[2]; + + if (0) { + /* correct, but unnecessarily slow. */ + if (gizmo_window_project_2d( + C, gz, (const float[2]){UNPACK2(mval)}, 2, true, point_local) == false) + { + return -1; + } + } + else { + copy_v2_v2(point_local, (float[2]){UNPACK2(mval)}); + sub_v2_v2(point_local, gz->matrix_basis[3]); + mul_v2_fl(point_local, 1.0f / (gz->scale_basis * UI_DPI_FAC)); + } + /* The 'gz->scale_final' is already applied when projecting. */ + if (len_squared_v2(point_local) < 1.0f) { + return 0; + } + + return -1; +} + +static int gizmo_button2d_cursor_get(wmGizmo *gz) +{ + if (RNA_boolean_get(gz->ptr, "show_drag")) { + return BC_NSEW_SCROLLCURSOR; + } + return CURSOR_STD; +} + +static void gizmo_button2d_free(wmGizmo *gz) +{ + ButtonGizmo2D *shape = (ButtonGizmo2D *)gz; + + for (uint i = 0; i < ARRAY_SIZE(shape->shape_batch); i++) { + GPU_BATCH_DISCARD_SAFE(shape->shape_batch[i]); + } +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Button Gizmo API + * + * \{ */ + +static void GIZMO_GT_button_2d(wmGizmoType *gzt) +{ + /* identifiers */ + gzt->idname = "GIZMO_GT_button_2d"; + + /* api callbacks */ + gzt->draw = gizmo_button2d_draw; + gzt->draw_select = gizmo_button2d_draw_select; + gzt->test_select = gizmo_button2d_test_select; + gzt->cursor_get = gizmo_button2d_cursor_get; + gzt->free = gizmo_button2d_free; + + gzt->struct_size = sizeof(ButtonGizmo2D); + + /* rna */ + static EnumPropertyItem rna_enum_draw_options[] = { + {ED_GIZMO_BUTTON_SHOW_OUTLINE, "OUTLINE", 0, "Outline", ""}, + {ED_GIZMO_BUTTON_SHOW_HELPLINE, "HELPLINE", 0, "Help Line", ""}, + {0, NULL, 0, NULL, NULL} + }; + PropertyRNA *prop; + + RNA_def_enum_flag(gzt->srna, "draw_options", rna_enum_draw_options, 0, "Draw Options", ""); + + prop = RNA_def_property(gzt->srna, "icon", PROP_ENUM, PROP_NONE); + RNA_def_property_enum_items(prop, rna_enum_icon_items); + + /* Passed to 'GPU_batch_tris_from_poly_2d_encoded' */ + RNA_def_property(gzt->srna, "shape", PROP_STRING, PROP_BYTESTRING); + + /* Currently only used for cursor display. */ + RNA_def_boolean(gzt->srna, "show_drag", true, "Show Drag", ""); +} + +void ED_gizmotypes_button_2d(void) +{ + WM_gizmotype_append(GIZMO_GT_button_2d); +} + +/** \} */ // Button Gizmo API diff --git a/source/blender/editors/gizmo_library/gizmo_types/cage2d_gizmo.c b/source/blender/editors/gizmo_library/gizmo_types/cage2d_gizmo.c new file mode 100644 index 00000000000..0646d689812 --- /dev/null +++ b/source/blender/editors/gizmo_library/gizmo_types/cage2d_gizmo.c @@ -0,0 +1,1103 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2014 Blender Foundation. + * All rights reserved. + * + * Contributor(s): Blender Foundation + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file cage2d_gizmo.c + * \ingroup edgizmolib + * + * \name Cage Gizmo + * + * 2D Gizmo + * + * \brief Rectangular gizmo acting as a 'cage' around its content. + * Interacting scales or translates the gizmo. + */ + +#include "MEM_guardedalloc.h" + +#include "BLI_math.h" +#include "BLI_dial_2d.h" +#include "BLI_rect.h" + +#include "BKE_context.h" + +#include "BIF_gl.h" + +#include "GPU_matrix.h" +#include "GPU_shader.h" +#include "GPU_immediate.h" +#include "GPU_immediate_util.h" +#include "GPU_select.h" +#include "GPU_state.h" + +#include "RNA_access.h" +#include "RNA_define.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "ED_screen.h" +#include "ED_view3d.h" +#include "ED_gizmo_library.h" + +/* own includes */ +#include "../gizmo_library_intern.h" + +#define GIZMO_RESIZER_SIZE 10.0f +#define GIZMO_MARGIN_OFFSET_SCALE 1.5f + +static void gizmo_calc_rect_view_scale( + const wmGizmo *gz, const float dims[2], float scale[2]) +{ + float matrix_final_no_offset[4][4]; + float asp[2] = {1.0f, 1.0f}; + if (dims[0] > dims[1]) { + asp[0] = dims[1] / dims[0]; + } + else { + asp[1] = dims[0] / dims[1]; + } + float x_axis[3], y_axis[3]; + WM_gizmo_calc_matrix_final_no_offset(gz, matrix_final_no_offset); + mul_v3_mat3_m4v3(x_axis, matrix_final_no_offset, gz->matrix_offset[0]); + mul_v3_mat3_m4v3(y_axis, matrix_final_no_offset, gz->matrix_offset[1]); + + mul_v2_v2(x_axis, asp); + mul_v2_v2(y_axis, asp); + + scale[0] = 1.0f / len_v3(x_axis); + scale[1] = 1.0f / len_v3(y_axis); +} + +static void gizmo_calc_rect_view_margin( + const wmGizmo *gz, const float dims[2], float margin[2]) +{ + float handle_size; + if (gz->parent_gzgroup->type->flag & WM_GIZMOGROUPTYPE_3D) { + handle_size = 0.15f; + } + else { + handle_size = GIZMO_RESIZER_SIZE; + } + handle_size *= gz->scale_final; + float scale_xy[2]; + gizmo_calc_rect_view_scale(gz, dims, scale_xy); + margin[0] = ((handle_size * scale_xy[0])); + margin[1] = ((handle_size * scale_xy[1])); +} + +/* -------------------------------------------------------------------- */ + +static void gizmo_rect_pivot_from_scale_part(int part, float r_pt[2], bool r_constrain_axis[2]) +{ + bool x = true, y = true; + switch (part) { + case ED_GIZMO_CAGE2D_PART_SCALE_MIN_X: { ARRAY_SET_ITEMS(r_pt, 0.5, 0.0); x = false; break; } + case ED_GIZMO_CAGE2D_PART_SCALE_MAX_X: { ARRAY_SET_ITEMS(r_pt, -0.5, 0.0); x = false; break; } + case ED_GIZMO_CAGE2D_PART_SCALE_MIN_Y: { ARRAY_SET_ITEMS(r_pt, 0.0, 0.5); y = false; break; } + case ED_GIZMO_CAGE2D_PART_SCALE_MAX_Y: { ARRAY_SET_ITEMS(r_pt, 0.0, -0.5); y = false; break; } + case ED_GIZMO_CAGE2D_PART_SCALE_MIN_X_MIN_Y: { ARRAY_SET_ITEMS(r_pt, 0.5, 0.5); x = y = false; break; } + case ED_GIZMO_CAGE2D_PART_SCALE_MIN_X_MAX_Y: { ARRAY_SET_ITEMS(r_pt, 0.5, -0.5); x = y = false; break; } + case ED_GIZMO_CAGE2D_PART_SCALE_MAX_X_MIN_Y: { ARRAY_SET_ITEMS(r_pt, -0.5, 0.5); x = y = false; break; } + case ED_GIZMO_CAGE2D_PART_SCALE_MAX_X_MAX_Y: { ARRAY_SET_ITEMS(r_pt, -0.5, -0.5); x = y = false; break; } + default: BLI_assert(0); + } + r_constrain_axis[0] = x; + r_constrain_axis[1] = y; +} + +/* -------------------------------------------------------------------- */ +/** \name Box Draw Style + * + * Useful for 3D views, see: #ED_GIZMO_CAGE2D_STYLE_BOX + * \{ */ + +static void cage2d_draw_box_corners( + const rctf *r, const float margin[2], const float color[3]) +{ + uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); + + immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); + immUniformColor3fv(color); + + immBegin(GPU_PRIM_LINES, 16); + + immVertex2f(pos, r->xmin, r->ymin + margin[1]); + immVertex2f(pos, r->xmin, r->ymin); + immVertex2f(pos, r->xmin, r->ymin); + immVertex2f(pos, r->xmin + margin[0], r->ymin); + + immVertex2f(pos, r->xmax, r->ymin + margin[1]); + immVertex2f(pos, r->xmax, r->ymin); + immVertex2f(pos, r->xmax, r->ymin); + immVertex2f(pos, r->xmax - margin[0], r->ymin); + + immVertex2f(pos, r->xmax, r->ymax - margin[1]); + immVertex2f(pos, r->xmax, r->ymax); + immVertex2f(pos, r->xmax, r->ymax); + immVertex2f(pos, r->xmax - margin[0], r->ymax); + + immVertex2f(pos, r->xmin, r->ymax - margin[1]); + immVertex2f(pos, r->xmin, r->ymax); + immVertex2f(pos, r->xmin, r->ymax); + immVertex2f(pos, r->xmin + margin[0], r->ymax); + + immEnd(); + + immUnbindProgram(); +} + +static void cage2d_draw_box_interaction( + const float color[4], const int highlighted, + const float size[2], const float margin[2], + const float line_width, const bool is_solid, const int draw_options) +{ + /* 4 verts for translate, otherwise only 3 are used. */ + float verts[4][2]; + uint verts_len = 0; + GPUPrimType prim_type = GPU_PRIM_NONE; + + switch (highlighted) { + case ED_GIZMO_CAGE2D_PART_SCALE_MIN_X: + { + rctf r = { + .xmin = -size[0], .xmax = -size[0] + margin[0], + .ymin = -size[1] + margin[1], .ymax = size[1] - margin[1], + }; + ARRAY_SET_ITEMS(verts[0], r.xmin, r.ymin); + ARRAY_SET_ITEMS(verts[1], r.xmin, r.ymax); + verts_len = 2; + if (is_solid) { + ARRAY_SET_ITEMS(verts[2], r.xmax, r.ymax); + ARRAY_SET_ITEMS(verts[3], r.xmax, r.ymin); + verts_len += 2; + prim_type = GPU_PRIM_TRI_FAN; + } + else { + prim_type = GPU_PRIM_LINE_STRIP; + } + break; + } + case ED_GIZMO_CAGE2D_PART_SCALE_MAX_X: + { + rctf r = { + .xmin = size[0] - margin[0], .xmax = size[0], + .ymin = -size[1] + margin[1], .ymax = size[1] - margin[1], + }; + ARRAY_SET_ITEMS(verts[0], r.xmax, r.ymin); + ARRAY_SET_ITEMS(verts[1], r.xmax, r.ymax); + verts_len = 2; + if (is_solid) { + ARRAY_SET_ITEMS(verts[2], r.xmin, r.ymax); + ARRAY_SET_ITEMS(verts[3], r.xmin, r.ymin); + verts_len += 2; + prim_type = GPU_PRIM_TRI_FAN; + } + else { + prim_type = GPU_PRIM_LINE_STRIP; + } + break; + } + case ED_GIZMO_CAGE2D_PART_SCALE_MIN_Y: + { + rctf r = { + .xmin = -size[0] + margin[0], .xmax = size[0] - margin[0], + .ymin = -size[1], .ymax = -size[1] + margin[1], + }; + ARRAY_SET_ITEMS(verts[0], r.xmin, r.ymin); + ARRAY_SET_ITEMS(verts[1], r.xmax, r.ymin); + verts_len = 2; + if (is_solid) { + ARRAY_SET_ITEMS(verts[2], r.xmax, r.ymax); + ARRAY_SET_ITEMS(verts[3], r.xmin, r.ymax); + verts_len += 2; + prim_type = GPU_PRIM_TRI_FAN; + } + else { + prim_type = GPU_PRIM_LINE_STRIP; + } + break; + } + case ED_GIZMO_CAGE2D_PART_SCALE_MAX_Y: + { + rctf r = { + .xmin = -size[0] + margin[0], .xmax = size[0] - margin[0], + .ymin = size[1] - margin[1], .ymax = size[1], + }; + ARRAY_SET_ITEMS(verts[0], r.xmin, r.ymax); + ARRAY_SET_ITEMS(verts[1], r.xmax, r.ymax); + verts_len = 2; + if (is_solid) { + ARRAY_SET_ITEMS(verts[2], r.xmax, r.ymin); + ARRAY_SET_ITEMS(verts[3], r.xmin, r.ymin); + verts_len += 2; + prim_type = GPU_PRIM_TRI_FAN; + } + else { + prim_type = GPU_PRIM_LINE_STRIP; + } + break; + } + case ED_GIZMO_CAGE2D_PART_SCALE_MIN_X_MIN_Y: + { + rctf r = { + .xmin = -size[0], .xmax = -size[0] + margin[0], + .ymin = -size[1], .ymax = -size[1] + margin[1], + }; + ARRAY_SET_ITEMS(verts[0], r.xmax, r.ymin); + ARRAY_SET_ITEMS(verts[1], r.xmax, r.ymax); + ARRAY_SET_ITEMS(verts[2], r.xmin, r.ymax); + verts_len = 3; + if (is_solid) { + ARRAY_SET_ITEMS(verts[3], r.xmin, r.ymin); + verts_len += 1; + prim_type = GPU_PRIM_TRI_FAN; + } + else { + prim_type = GPU_PRIM_LINE_STRIP; + } + break; + } + case ED_GIZMO_CAGE2D_PART_SCALE_MIN_X_MAX_Y: + { + rctf r = { + .xmin = -size[0], .xmax = -size[0] + margin[0], + .ymin = size[1] - margin[1], .ymax = size[1], + }; + ARRAY_SET_ITEMS(verts[0], r.xmax, r.ymax); + ARRAY_SET_ITEMS(verts[1], r.xmax, r.ymin); + ARRAY_SET_ITEMS(verts[2], r.xmin, r.ymin); + verts_len = 3; + if (is_solid) { + ARRAY_SET_ITEMS(verts[3], r.xmin, r.ymax); + verts_len += 1; + prim_type = GPU_PRIM_TRI_FAN; + } + else { + prim_type = GPU_PRIM_LINE_STRIP; + } + break; + } + case ED_GIZMO_CAGE2D_PART_SCALE_MAX_X_MIN_Y: + { + rctf r = { + .xmin = size[0] - margin[0], .xmax = size[0], + .ymin = -size[1], .ymax = -size[1] + margin[1], + }; + ARRAY_SET_ITEMS(verts[0], r.xmin, r.ymin); + ARRAY_SET_ITEMS(verts[1], r.xmin, r.ymax); + ARRAY_SET_ITEMS(verts[2], r.xmax, r.ymax); + verts_len = 3; + if (is_solid) { + ARRAY_SET_ITEMS(verts[3], r.xmax, r.ymin); + verts_len += 1; + prim_type = GPU_PRIM_TRI_FAN; + } + else { + prim_type = GPU_PRIM_LINE_STRIP; + } + break; + } + case ED_GIZMO_CAGE2D_PART_SCALE_MAX_X_MAX_Y: + { + rctf r = { + .xmin = size[0] - margin[0], .xmax = size[0], + .ymin = size[1] - margin[1], .ymax = size[1], + }; + ARRAY_SET_ITEMS(verts[0], r.xmin, r.ymax); + ARRAY_SET_ITEMS(verts[1], r.xmin, r.ymin); + ARRAY_SET_ITEMS(verts[2], r.xmax, r.ymin); + verts_len = 3; + if (is_solid) { + ARRAY_SET_ITEMS(verts[3], r.xmax, r.ymax); + verts_len += 1; + prim_type = GPU_PRIM_TRI_FAN; + } + else { + prim_type = GPU_PRIM_LINE_STRIP; + } + break; + } + case ED_GIZMO_CAGE2D_PART_ROTATE: + { + const float rotate_pt[2] = {0.0f, size[1] + margin[1]}; + const rctf r_rotate = { + .xmin = rotate_pt[0] - margin[0] / 2.0f, + .xmax = rotate_pt[0] + margin[0] / 2.0f, + .ymin = rotate_pt[1] - margin[1] / 2.0f, + .ymax = rotate_pt[1] + margin[1] / 2.0f, + }; + + ARRAY_SET_ITEMS(verts[0], r_rotate.xmin, r_rotate.ymin); + ARRAY_SET_ITEMS(verts[1], r_rotate.xmin, r_rotate.ymax); + ARRAY_SET_ITEMS(verts[2], r_rotate.xmax, r_rotate.ymax); + ARRAY_SET_ITEMS(verts[3], r_rotate.xmax, r_rotate.ymin); + verts_len = 4; + if (is_solid) { + prim_type = GPU_PRIM_TRI_FAN; + } + else { + prim_type = GPU_PRIM_LINE_STRIP; + } + break; + } + + case ED_GIZMO_CAGE2D_PART_TRANSLATE: + if (draw_options & ED_GIZMO_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE) { + ARRAY_SET_ITEMS(verts[0], -margin[0] / 2, -margin[1] / 2); + ARRAY_SET_ITEMS(verts[1], margin[0] / 2, margin[1] / 2); + ARRAY_SET_ITEMS(verts[2], -margin[0] / 2, margin[1] / 2); + ARRAY_SET_ITEMS(verts[3], margin[0] / 2, -margin[1] / 2); + verts_len = 4; + if (is_solid) { + prim_type = GPU_PRIM_TRI_FAN; + } + else { + prim_type = GPU_PRIM_LINES; + } + } + else { + /* Only used for 3D view selection, never displayed to the user. */ + ARRAY_SET_ITEMS(verts[0], -size[0], -size[1]); + ARRAY_SET_ITEMS(verts[1], -size[0], size[1]); + ARRAY_SET_ITEMS(verts[2], size[0], size[1]); + ARRAY_SET_ITEMS(verts[3], size[0], -size[1]); + verts_len = 4; + if (is_solid) { + prim_type = GPU_PRIM_TRI_FAN; + } + else { + /* unreachable */ + BLI_assert(0); + prim_type = GPU_PRIM_LINE_STRIP; + } + } + break; + default: + return; + } + + BLI_assert(prim_type != GPU_PRIM_NONE); + + GPUVertFormat *format = immVertexFormat(); + struct { + uint pos, col; + } attr_id = { + .pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT), + .col = GPU_vertformat_attr_add(format, "color", GPU_COMP_F32, 3, GPU_FETCH_FLOAT), + }; + immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR); + + { + if (is_solid) { + BLI_assert(ELEM(prim_type, GPU_PRIM_TRI_FAN)); + immBegin(prim_type, verts_len); + immAttrib3f(attr_id.col, 0.0f, 0.0f, 0.0f); + for (uint i = 0; i < verts_len; i++) { + immVertex2fv(attr_id.pos, verts[i]); + } + immEnd(); + } + else { + BLI_assert(ELEM(prim_type, GPU_PRIM_LINE_STRIP, GPU_PRIM_LINES)); + GPU_line_width(line_width + 3.0f); + + immBegin(prim_type, verts_len); + immAttrib3f(attr_id.col, 0.0f, 0.0f, 0.0f); + for (uint i = 0; i < verts_len; i++) { + immVertex2fv(attr_id.pos, verts[i]); + } + immEnd(); + + GPU_line_width(line_width); + + immBegin(prim_type, verts_len); + immAttrib3fv(attr_id.col, color); + for (uint i = 0; i < verts_len; i++) { + immVertex2fv(attr_id.pos, verts[i]); + } + immEnd(); + } + } + + immUnbindProgram(); +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Circle Draw Style + * + * Useful for 2D views, see: #ED_GIZMO_CAGE2D_STYLE_CIRCLE + * \{ */ + +static void imm_draw_point_aspect_2d( + uint pos, float x, float y, float rad_x, float rad_y, bool solid) +{ + immBegin(solid ? GPU_PRIM_TRI_FAN : GPU_PRIM_LINE_LOOP, 4); + immVertex2f(pos, x - rad_x, y - rad_y); + immVertex2f(pos, x - rad_x, y + rad_y); + immVertex2f(pos, x + rad_x, y + rad_y); + immVertex2f(pos, x + rad_x, y - rad_y); + immEnd(); +} + +static void cage2d_draw_circle_wire( + const rctf *r, const float margin[2], const float color[3], + const int transform_flag, const int draw_options) +{ + uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); + + immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); + immUniformColor3fv(color); + + immBegin(GPU_PRIM_LINE_LOOP, 4); + immVertex2f(pos, r->xmin, r->ymin); + immVertex2f(pos, r->xmax, r->ymin); + immVertex2f(pos, r->xmax, r->ymax); + immVertex2f(pos, r->xmin, r->ymax); + immEnd(); + + if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_ROTATE) { + immBegin(GPU_PRIM_LINE_LOOP, 2); + immVertex2f(pos, BLI_rctf_cent_x(r), r->ymax); + immVertex2f(pos, BLI_rctf_cent_x(r), r->ymax + margin[1]); + immEnd(); + } + + if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE) { + if (draw_options & ED_GIZMO_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE) { + const float rad[2] = {margin[0] / 2, margin[1] / 2}; + const float center[2] = {BLI_rctf_cent_x(r), BLI_rctf_cent_y(r)}; + + immBegin(GPU_PRIM_LINES, 4); + immVertex2f(pos, center[0] - rad[0], center[1] - rad[1]); + immVertex2f(pos, center[0] + rad[0], center[1] + rad[1]); + immVertex2f(pos, center[0] + rad[0], center[1] - rad[1]); + immVertex2f(pos, center[0] - rad[0], center[1] + rad[1]); + immEnd(); + } + } + + immUnbindProgram(); +} + +static void cage2d_draw_circle_handles( + const rctf *r, const float margin[2], const float color[3], + const int transform_flag, + bool solid) +{ + uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); + void (*circle_fn)(uint, float, float, float, float, int) = + (solid) ? imm_draw_circle_fill_aspect_2d : imm_draw_circle_wire_aspect_2d; + const int resolu = 12; + const float rad[2] = {margin[0] / 3, margin[1] / 3}; + + immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); + immUniformColor3fv(color); + + /* should really divide by two, but looks too bulky. */ + { + imm_draw_point_aspect_2d(pos, r->xmin, r->ymin, rad[0], rad[1], solid); + imm_draw_point_aspect_2d(pos, r->xmax, r->ymin, rad[0], rad[1], solid); + imm_draw_point_aspect_2d(pos, r->xmax, r->ymax, rad[0], rad[1], solid); + imm_draw_point_aspect_2d(pos, r->xmin, r->ymax, rad[0], rad[1], solid); + } + + if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_ROTATE) { + const float handle[2] = {BLI_rctf_cent_x(r), r->ymax + (margin[1] * GIZMO_MARGIN_OFFSET_SCALE)}; + circle_fn(pos, handle[0], handle[1], rad[0], rad[1], resolu); + } + + immUnbindProgram(); +} + +/** \} */ + +static void gizmo_cage2d_draw_intern( + wmGizmo *gz, const bool select, const bool highlight, const int select_id) +{ + // const bool use_clamp = (gz->parent_gzgroup->type->flag & WM_GIZMOGROUPTYPE_3D) == 0; + float dims[2]; + RNA_float_get_array(gz->ptr, "dimensions", dims); + float matrix_final[4][4]; + + const int transform_flag = RNA_enum_get(gz->ptr, "transform"); + const int draw_style = RNA_enum_get(gz->ptr, "draw_style"); + const int draw_options = RNA_enum_get(gz->ptr, "draw_options"); + + const float size_real[2] = {dims[0] / 2.0f, dims[1] / 2.0f}; + + WM_gizmo_calc_matrix_final(gz, matrix_final); + + GPU_matrix_push(); + GPU_matrix_mul(matrix_final); + + float margin[2]; + gizmo_calc_rect_view_margin(gz, dims, margin); + + /* Handy for quick testing draw (if it's outside bounds). */ + if (false) { + GPU_blend(true); + uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); + immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); + immUniformColor4fv((const float[4]){1, 1, 1, 0.5f}); + float s = 0.5f; + immRectf(pos, -s, -s, s, s); + immUnbindProgram(); + GPU_blend(false); + } + + if (select) { + /* expand for hotspot */ + const float size[2] = {size_real[0] + margin[0] / 2, size_real[1] + margin[1] / 2}; + + if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE) { + int scale_parts[] = { + ED_GIZMO_CAGE2D_PART_SCALE_MIN_X, + ED_GIZMO_CAGE2D_PART_SCALE_MAX_X, + ED_GIZMO_CAGE2D_PART_SCALE_MIN_Y, + ED_GIZMO_CAGE2D_PART_SCALE_MAX_Y, + + ED_GIZMO_CAGE2D_PART_SCALE_MIN_X_MIN_Y, + ED_GIZMO_CAGE2D_PART_SCALE_MIN_X_MAX_Y, + ED_GIZMO_CAGE2D_PART_SCALE_MAX_X_MIN_Y, + ED_GIZMO_CAGE2D_PART_SCALE_MAX_X_MAX_Y, + }; + for (int i = 0; i < ARRAY_SIZE(scale_parts); i++) { + GPU_select_load_id(select_id | scale_parts[i]); + cage2d_draw_box_interaction( + gz->color, scale_parts[i], size, margin, gz->line_width, true, draw_options); + } + } + if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE) { + const int transform_part = ED_GIZMO_CAGE2D_PART_TRANSLATE; + GPU_select_load_id(select_id | transform_part); + cage2d_draw_box_interaction( + gz->color, transform_part, size, margin, gz->line_width, true, draw_options); + } + if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_ROTATE) { + cage2d_draw_box_interaction( + gz->color, ED_GIZMO_CAGE2D_PART_ROTATE, size_real, margin, gz->line_width, true, draw_options); + } + } + else { + const rctf r = { + .xmin = -size_real[0], + .ymin = -size_real[1], + .xmax = size_real[0], + .ymax = size_real[1], + }; + if (draw_style == ED_GIZMO_CAGE2D_STYLE_BOX) { + /* corner gizmos */ + GPU_line_width(gz->line_width + 3.0f); + cage2d_draw_box_corners(&r, margin, (const float[3]){0, 0, 0}); + + /* corner gizmos */ + float color[4]; + gizmo_color_get(gz, highlight, color); + GPU_line_width(gz->line_width); + cage2d_draw_box_corners(&r, margin, color); + + bool show = false; + if (gz->highlight_part == ED_GIZMO_CAGE2D_PART_TRANSLATE) { + /* Only show if we're drawing the center handle + * otherwise the entire rectangle is the hotspot. */ + if (draw_options & ED_GIZMO_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE) { + show = true; + } + } + else { + show = true; + } + + if (show) { + cage2d_draw_box_interaction( + gz->color, gz->highlight_part, size_real, margin, gz->line_width, false, draw_options); + } + + if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_ROTATE) { + cage2d_draw_box_interaction( + gz->color, ED_GIZMO_CAGE2D_PART_ROTATE, size_real, margin, gz->line_width, false, draw_options); + } + } + else if (draw_style == ED_GIZMO_CAGE2D_STYLE_CIRCLE) { + float color[4]; + gizmo_color_get(gz, highlight, color); + + GPU_line_smooth(true); + GPU_blend(true); + + GPU_line_width(gz->line_width + 3.0f); + cage2d_draw_circle_wire(&r, margin, (const float[3]){0, 0, 0}, transform_flag, draw_options); + GPU_line_width(gz->line_width); + cage2d_draw_circle_wire(&r, margin, color, transform_flag, draw_options); + + + /* corner gizmos */ + cage2d_draw_circle_handles(&r, margin, color, transform_flag, true); + cage2d_draw_circle_handles(&r, margin, (const float[3]){0, 0, 0}, transform_flag, false); + + GPU_blend(false); + GPU_line_smooth(false); + } + else { + BLI_assert(0); + } + } + + GPU_line_width(1.0); + GPU_matrix_pop(); +} + +/** + * For when we want to draw 2d cage in 3d views. + */ +static void gizmo_cage2d_draw_select(const bContext *UNUSED(C), wmGizmo *gz, int select_id) +{ + gizmo_cage2d_draw_intern(gz, true, false, select_id); +} + +static void gizmo_cage2d_draw(const bContext *UNUSED(C), wmGizmo *gz) +{ + const bool is_highlight = (gz->state & WM_GIZMO_STATE_HIGHLIGHT) != 0; + gizmo_cage2d_draw_intern(gz, false, is_highlight, -1); +} + +static int gizmo_cage2d_get_cursor(wmGizmo *gz) +{ + int highlight_part = gz->highlight_part; + + if (gz->parent_gzgroup->type->flag & WM_GIZMOGROUPTYPE_3D) { + return BC_NSEW_SCROLLCURSOR; + } + + switch (highlight_part) { + case ED_GIZMO_CAGE2D_PART_TRANSLATE: + return BC_NSEW_SCROLLCURSOR; + case ED_GIZMO_CAGE2D_PART_SCALE_MIN_X: + case ED_GIZMO_CAGE2D_PART_SCALE_MAX_X: + return CURSOR_X_MOVE; + case ED_GIZMO_CAGE2D_PART_SCALE_MIN_Y: + case ED_GIZMO_CAGE2D_PART_SCALE_MAX_Y: + return CURSOR_Y_MOVE; + + /* TODO diagonal cursor */ + case ED_GIZMO_CAGE2D_PART_SCALE_MIN_X_MIN_Y: + case ED_GIZMO_CAGE2D_PART_SCALE_MAX_X_MIN_Y: + return BC_NSEW_SCROLLCURSOR; + case ED_GIZMO_CAGE2D_PART_SCALE_MIN_X_MAX_Y: + case ED_GIZMO_CAGE2D_PART_SCALE_MAX_X_MAX_Y: + return BC_NSEW_SCROLLCURSOR; + case ED_GIZMO_CAGE2D_PART_ROTATE: + return BC_CROSSCURSOR; + default: + return CURSOR_STD; + } +} + +static int gizmo_cage2d_test_select( + bContext *C, wmGizmo *gz, const int mval[2]) +{ + float point_local[2]; + float dims[2]; + RNA_float_get_array(gz->ptr, "dimensions", dims); + const float size_real[2] = {dims[0] / 2.0f, dims[1] / 2.0f}; + + if (gizmo_window_project_2d( + C, gz, (const float[2]){UNPACK2(mval)}, 2, true, point_local) == false) + { + return -1; + } + + float margin[2]; + gizmo_calc_rect_view_margin(gz, dims, margin); + /* expand for hotspot */ + const float size[2] = {size_real[0] + margin[0] / 2, size_real[1] + margin[1] / 2}; + + const int transform_flag = RNA_enum_get(gz->ptr, "transform"); + const int draw_options = RNA_enum_get(gz->ptr, "draw_options"); + + if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE) { + rctf r; + if (draw_options & ED_GIZMO_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE) { + r.xmin = -margin[0] / 2; + r.ymin = -margin[1] / 2; + r.xmax = margin[0] / 2; + r.ymax = margin[1] / 2; + } + else { + r.xmin = -size[0] + margin[0]; + r.ymin = -size[1] + margin[1]; + r.xmax = size[0] - margin[0]; + r.ymax = size[1] - margin[1]; + }; + bool isect = BLI_rctf_isect_pt_v(&r, point_local); + if (isect) { + return ED_GIZMO_CAGE2D_PART_TRANSLATE; + } + } + + /* if gizmo does not have a scale intersection, don't do it */ + if (transform_flag & (ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE | ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE_UNIFORM)) { + const rctf r_xmin = {.xmin = -size[0], .ymin = -size[1], .xmax = -size[0] + margin[0], .ymax = size[1]}; + const rctf r_xmax = {.xmin = size[0] - margin[0], .ymin = -size[1], .xmax = size[0], .ymax = size[1]}; + const rctf r_ymin = {.xmin = -size[0], .ymin = -size[1], .xmax = size[0], .ymax = -size[1] + margin[1]}; + const rctf r_ymax = {.xmin = -size[0], .ymin = size[1] - margin[1], .xmax = size[0], .ymax = size[1]}; + + if (BLI_rctf_isect_pt_v(&r_xmin, point_local)) { + if (BLI_rctf_isect_pt_v(&r_ymin, point_local)) { + return ED_GIZMO_CAGE2D_PART_SCALE_MIN_X_MIN_Y; + } + if (BLI_rctf_isect_pt_v(&r_ymax, point_local)) { + return ED_GIZMO_CAGE2D_PART_SCALE_MIN_X_MAX_Y; + } + return ED_GIZMO_CAGE2D_PART_SCALE_MIN_X; + } + if (BLI_rctf_isect_pt_v(&r_xmax, point_local)) { + if (BLI_rctf_isect_pt_v(&r_ymin, point_local)) { + return ED_GIZMO_CAGE2D_PART_SCALE_MAX_X_MIN_Y; + } + if (BLI_rctf_isect_pt_v(&r_ymax, point_local)) { + return ED_GIZMO_CAGE2D_PART_SCALE_MAX_X_MAX_Y; + } + return ED_GIZMO_CAGE2D_PART_SCALE_MAX_X; + } + if (BLI_rctf_isect_pt_v(&r_ymin, point_local)) { + return ED_GIZMO_CAGE2D_PART_SCALE_MIN_Y; + } + if (BLI_rctf_isect_pt_v(&r_ymax, point_local)) { + return ED_GIZMO_CAGE2D_PART_SCALE_MAX_Y; + } + } + + if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_ROTATE) { + /* Rotate: + * (*) <-- hot spot is here! + * +---+ + * | | + * +---+ */ + const float r_rotate_pt[2] = {0.0f, size_real[1] + (margin[1] * GIZMO_MARGIN_OFFSET_SCALE)}; + const rctf r_rotate = { + .xmin = r_rotate_pt[0] - margin[0] / 2.0f, + .xmax = r_rotate_pt[0] + margin[0] / 2.0f, + .ymin = r_rotate_pt[1] - margin[1] / 2.0f, + .ymax = r_rotate_pt[1] + margin[1] / 2.0f, + }; + + if (BLI_rctf_isect_pt_v(&r_rotate, point_local)) { + return ED_GIZMO_CAGE2D_PART_ROTATE; + } + } + + return -1; +} + +typedef struct RectTransformInteraction { + float orig_mouse[2]; + float orig_matrix_offset[4][4]; + float orig_matrix_final_no_offset[4][4]; + Dial *dial; +} RectTransformInteraction; + +static void gizmo_cage2d_setup(wmGizmo *gz) +{ + gz->flag |= WM_GIZMO_DRAW_MODAL | WM_GIZMO_DRAW_NO_SCALE; +} + +static int gizmo_cage2d_invoke( + bContext *C, wmGizmo *gz, const wmEvent *event) +{ + RectTransformInteraction *data = MEM_callocN(sizeof(RectTransformInteraction), "cage_interaction"); + + copy_m4_m4(data->orig_matrix_offset, gz->matrix_offset); + WM_gizmo_calc_matrix_final_no_offset(gz, data->orig_matrix_final_no_offset); + + if (gizmo_window_project_2d( + C, gz, (const float[2]){UNPACK2(event->mval)}, 2, false, data->orig_mouse) == 0) + { + zero_v2(data->orig_mouse); + } + + gz->interaction_data = data; + + return OPERATOR_RUNNING_MODAL; +} + +static int gizmo_cage2d_modal( + bContext *C, wmGizmo *gz, const wmEvent *event, + eWM_GizmoFlagTweak UNUSED(tweak_flag)) +{ + if (event->type != MOUSEMOVE) { + return OPERATOR_RUNNING_MODAL; + } + /* For transform logic to be manageable we operate in -0.5..0.5 2D space, + * no matter the size of the rectangle, mouse coorts are scaled to unit space. + * The mouse coords have been projected into the matrix so we don't need to worry about axis alignment. + * + * - The cursor offset are multiplied by 'dims'. + * - Matrix translation is also multiplied by 'dims'. + */ + RectTransformInteraction *data = gz->interaction_data; + float point_local[2]; + + float dims[2]; + RNA_float_get_array(gz->ptr, "dimensions", dims); + + { + float matrix_back[4][4]; + copy_m4_m4(matrix_back, gz->matrix_offset); + copy_m4_m4(gz->matrix_offset, data->orig_matrix_offset); + + bool ok = gizmo_window_project_2d( + C, gz, (const float[2]){UNPACK2(event->mval)}, 2, false, point_local); + copy_m4_m4(gz->matrix_offset, matrix_back); + if (!ok) { + return OPERATOR_RUNNING_MODAL; + } + } + + const int transform_flag = RNA_enum_get(gz->ptr, "transform"); + wmGizmoProperty *gz_prop; + + gz_prop = WM_gizmo_target_property_find(gz, "matrix"); + if (gz_prop->type != NULL) { + WM_gizmo_target_property_float_get_array(gz, gz_prop, &gz->matrix_offset[0][0]); + } + + if (gz->highlight_part == ED_GIZMO_CAGE2D_PART_TRANSLATE) { + /* do this to prevent clamping from changing size */ + copy_m4_m4(gz->matrix_offset, data->orig_matrix_offset); + gz->matrix_offset[3][0] = data->orig_matrix_offset[3][0] + (point_local[0] - data->orig_mouse[0]); + gz->matrix_offset[3][1] = data->orig_matrix_offset[3][1] + (point_local[1] - data->orig_mouse[1]); + } + else if (gz->highlight_part == ED_GIZMO_CAGE2D_PART_ROTATE) { + +#define MUL_V2_V3_M4_FINAL(test_co, mouse_co) \ + mul_v3_m4v3(test_co, data->orig_matrix_final_no_offset, ((const float[3]){UNPACK2(mouse_co), 0.0})) + + float test_co[3]; + + if (data->dial == NULL) { + MUL_V2_V3_M4_FINAL(test_co, data->orig_matrix_offset[3]); + + data->dial = BLI_dial_initialize(test_co, FLT_EPSILON); + + MUL_V2_V3_M4_FINAL(test_co, data->orig_mouse); + BLI_dial_angle(data->dial, test_co); + } + + /* rotate */ + MUL_V2_V3_M4_FINAL(test_co, point_local); + const float angle = BLI_dial_angle(data->dial, test_co); + + float matrix_space_inv[4][4]; + float matrix_rotate[4][4]; + float pivot[3]; + + copy_v3_v3(pivot, data->orig_matrix_offset[3]); + + invert_m4_m4(matrix_space_inv, gz->matrix_space); + + unit_m4(matrix_rotate); + mul_m4_m4m4(matrix_rotate, matrix_rotate, matrix_space_inv); + rotate_m4(matrix_rotate, 'Z', -angle); + mul_m4_m4m4(matrix_rotate, matrix_rotate, gz->matrix_space); + + zero_v3(matrix_rotate[3]); + transform_pivot_set_m4(matrix_rotate, pivot); + + mul_m4_m4m4(gz->matrix_offset, matrix_rotate, data->orig_matrix_offset); + +#undef MUL_V2_V3_M4_FINAL + } + else { + /* scale */ + copy_m4_m4(gz->matrix_offset, data->orig_matrix_offset); + float pivot[2]; + bool constrain_axis[2] = {false}; + + if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE) { + gizmo_rect_pivot_from_scale_part(gz->highlight_part, pivot, constrain_axis); + } + else { + zero_v2(pivot); + } + + /* Cursor deltas scaled to (-0.5..0.5). */ + float delta_orig[2], delta_curr[2]; + for (int i = 0; i < 2; i++) { + delta_orig[i] = ((data->orig_mouse[i] - data->orig_matrix_offset[3][i]) / dims[i]) - pivot[i]; + delta_curr[i] = ((point_local[i] - data->orig_matrix_offset[3][i]) / dims[i]) - pivot[i]; + } + + float scale[2] = {1.0f, 1.0f}; + for (int i = 0; i < 2; i++) { + if (constrain_axis[i] == false) { + if (delta_orig[i] < 0.0f) { + delta_orig[i] *= -1.0f; + delta_curr[i] *= -1.0f; + } + const int sign = signum_i(scale[i]); + + scale[i] = 1.0f + ((delta_curr[i] - delta_orig[i]) / len_v3(data->orig_matrix_offset[i])); + + if ((transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE_SIGNED) == 0) { + if (sign != signum_i(scale[i])) { + scale[i] = 0.0f; + } + } + } + } + + if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE_UNIFORM) { + if (constrain_axis[0] == false && constrain_axis[1] == false) { + scale[1] = scale[0] = (scale[1] + scale[0]) / 2.0f; + } + else if (constrain_axis[0] == false) { + scale[1] = scale[0]; + } + else if (constrain_axis[1] == false) { + scale[0] = scale[1]; + } + else { + BLI_assert(0); + } + } + + /* scale around pivot */ + float matrix_scale[4][4]; + unit_m4(matrix_scale); + + mul_v3_fl(matrix_scale[0], scale[0]); + mul_v3_fl(matrix_scale[1], scale[1]); + + transform_pivot_set_m4(matrix_scale, (const float[3]){pivot[0] * dims[0], pivot[1] * dims[1], 0.0f}); + mul_m4_m4m4(gz->matrix_offset, data->orig_matrix_offset, matrix_scale); + } + + if (gz_prop->type != NULL) { + WM_gizmo_target_property_float_set_array(C, gz, gz_prop, &gz->matrix_offset[0][0]); + } + + /* tag the region for redraw */ + ED_region_tag_redraw(CTX_wm_region(C)); + WM_event_add_mousemove(C); + + return OPERATOR_RUNNING_MODAL; +} + +static void gizmo_cage2d_property_update(wmGizmo *gz, wmGizmoProperty *gz_prop) +{ + if (STREQ(gz_prop->type->idname, "matrix")) { + if (WM_gizmo_target_property_array_length(gz, gz_prop) == 16) { + WM_gizmo_target_property_float_get_array(gz, gz_prop, &gz->matrix_offset[0][0]); + } + else { + BLI_assert(0); + } + } + else { + BLI_assert(0); + } +} + +static void gizmo_cage2d_exit(bContext *C, wmGizmo *gz, const bool cancel) +{ + RectTransformInteraction *data = gz->interaction_data; + + MEM_SAFE_FREE(data->dial); + + if (!cancel) + return; + + wmGizmoProperty *gz_prop; + + /* reset properties */ + gz_prop = WM_gizmo_target_property_find(gz, "matrix"); + if (gz_prop->type != NULL) { + WM_gizmo_target_property_float_set_array(C, gz, gz_prop, &data->orig_matrix_offset[0][0]); + } + + copy_m4_m4(gz->matrix_offset, data->orig_matrix_offset); +} + + +/* -------------------------------------------------------------------- */ +/** \name Cage Gizmo API + * + * \{ */ + +static void GIZMO_GT_cage_2d(wmGizmoType *gzt) +{ + /* identifiers */ + gzt->idname = "GIZMO_GT_cage_2d"; + + /* api callbacks */ + gzt->draw = gizmo_cage2d_draw; + gzt->draw_select = gizmo_cage2d_draw_select; + gzt->test_select = gizmo_cage2d_test_select; + gzt->setup = gizmo_cage2d_setup; + gzt->invoke = gizmo_cage2d_invoke; + gzt->property_update = gizmo_cage2d_property_update; + gzt->modal = gizmo_cage2d_modal; + gzt->exit = gizmo_cage2d_exit; + gzt->cursor_get = gizmo_cage2d_get_cursor; + + gzt->struct_size = sizeof(wmGizmo); + + /* rna */ + static EnumPropertyItem rna_enum_draw_style[] = { + {ED_GIZMO_CAGE2D_STYLE_BOX, "BOX", 0, "Box", ""}, + {ED_GIZMO_CAGE2D_STYLE_CIRCLE, "CIRCLE", 0, "Circle", ""}, + {0, NULL, 0, NULL, NULL} + }; + static EnumPropertyItem rna_enum_transform[] = { + {ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE, "TRANSLATE", 0, "Move", ""}, + {ED_GIZMO_CAGE2D_XFORM_FLAG_ROTATE, "ROTATE", 0, "Rotate", ""}, + {ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE, "SCALE", 0, "Scale", ""}, + {ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE_UNIFORM, "SCALE_UNIFORM", 0, "Scale Uniform", ""}, + {0, NULL, 0, NULL, NULL} + }; + static EnumPropertyItem rna_enum_draw_options[] = { + {ED_GIZMO_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE, "XFORM_CENTER_HANDLE", 0, "Center Handle", ""}, + {0, NULL, 0, NULL, NULL} + }; + static float unit_v2[2] = {1.0f, 1.0f}; + RNA_def_float_vector(gzt->srna, "dimensions", 2, unit_v2, 0, FLT_MAX, "Dimensions", "", 0.0f, FLT_MAX); + RNA_def_enum_flag(gzt->srna, "transform", rna_enum_transform, 0, "Transform Options", ""); + RNA_def_enum(gzt->srna, "draw_style", rna_enum_draw_style, ED_GIZMO_CAGE2D_STYLE_CIRCLE, "Draw Style", ""); + RNA_def_enum_flag( + gzt->srna, "draw_options", rna_enum_draw_options, + ED_GIZMO_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE, "Draw Options", ""); + + WM_gizmotype_target_property_def(gzt, "matrix", PROP_FLOAT, 16); +} + +void ED_gizmotypes_cage_2d(void) +{ + WM_gizmotype_append(GIZMO_GT_cage_2d); +} + +/** \} */ diff --git a/source/blender/editors/gizmo_library/gizmo_types/cage3d_gizmo.c b/source/blender/editors/gizmo_library/gizmo_types/cage3d_gizmo.c new file mode 100644 index 00000000000..3bbe13362d0 --- /dev/null +++ b/source/blender/editors/gizmo_library/gizmo_types/cage3d_gizmo.c @@ -0,0 +1,695 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2014 Blender Foundation. + * All rights reserved. + * + * Contributor(s): Blender Foundation + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file cage3d_gizmo.c + * \ingroup edgizmolib + * + * \name Cage Gizmo + * + * 2D Gizmo + * + * \brief Rectangular gizmo acting as a 'cage' around its content. + * Interacting scales or translates the gizmo. + */ + +#include "MEM_guardedalloc.h" + +#include "BLI_math.h" +#include "BLI_rect.h" + +#include "BKE_context.h" + +#include "BIF_gl.h" + +#include "GPU_matrix.h" +#include "GPU_shader.h" +#include "GPU_immediate.h" +#include "GPU_immediate_util.h" +#include "GPU_select.h" +#include "GPU_state.h" + +#include "RNA_access.h" +#include "RNA_define.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "ED_screen.h" +#include "ED_view3d.h" +#include "ED_gizmo_library.h" + +/* own includes */ +#include "../gizmo_library_intern.h" + +#define GIZMO_RESIZER_SIZE 10.0f +#define GIZMO_MARGIN_OFFSET_SCALE 1.5f + +static void gizmo_calc_matrix_final_no_offset( + const wmGizmo *gz, float orig_matrix_final_no_offset[4][4], bool use_space) +{ + float mat_identity[4][4]; + struct WM_GizmoMatrixParams params = {NULL}; + unit_m4(mat_identity); + if (use_space == false) { + params.matrix_basis = mat_identity; + } + params.matrix_offset = mat_identity; + WM_gizmo_calc_matrix_final_params(gz, ¶ms, orig_matrix_final_no_offset); +} + +static void gizmo_calc_rect_view_scale( + const wmGizmo *gz, const float dims[3], float scale[3]) +{ + UNUSED_VARS(dims); + + /* Unlike cage2d, no need to correct for aspect. */ + float matrix_final_no_offset[4][4]; + + float x_axis[3], y_axis[3], z_axis[3]; + gizmo_calc_matrix_final_no_offset(gz, matrix_final_no_offset, false); + mul_v3_mat3_m4v3(x_axis, matrix_final_no_offset, gz->matrix_offset[0]); + mul_v3_mat3_m4v3(y_axis, matrix_final_no_offset, gz->matrix_offset[1]); + mul_v3_mat3_m4v3(z_axis, matrix_final_no_offset, gz->matrix_offset[2]); + + scale[0] = 1.0f / len_v3(x_axis); + scale[1] = 1.0f / len_v3(y_axis); + scale[2] = 1.0f / len_v3(z_axis); +} + +static void gizmo_calc_rect_view_margin( + const wmGizmo *gz, const float dims[3], float margin[3]) +{ + float handle_size; + if (gz->parent_gzgroup->type->flag & WM_GIZMOGROUPTYPE_3D) { + handle_size = 0.15f; + } + else { + handle_size = GIZMO_RESIZER_SIZE; + } + // XXX, the scale isn't taking offset into account, we need to calculate scale per handle! + // handle_size *= gz->scale_final; + + float scale_xyz[3]; + gizmo_calc_rect_view_scale(gz, dims, scale_xyz); + margin[0] = ((handle_size * scale_xyz[0])); + margin[1] = ((handle_size * scale_xyz[1])); + margin[2] = ((handle_size * scale_xyz[2])); +} + +/* -------------------------------------------------------------------- */ + +static void gizmo_rect_pivot_from_scale_part(int part, float r_pt[3], bool r_constrain_axis[3]) +{ + if (part >= ED_GIZMO_CAGE3D_PART_SCALE_MIN_X_MIN_Y_MIN_Z && + part <= ED_GIZMO_CAGE3D_PART_SCALE_MAX_X_MAX_Y_MAX_Z) + { + int index = (part - ED_GIZMO_CAGE3D_PART_SCALE_MIN_X_MIN_Y_MIN_Z); + int range[3]; + range[2] = index % 3; + index = index / 3; + range[1] = index % 3; + index = index / 3; + range[0] = index % 3; + + const float sign[3] = {0.5f, 0.0f, -0.5f}; + for (int i = 0; i < 3; i++) { + r_pt[i] = sign[range[i]]; + r_constrain_axis[i] = (range[i] == 1); + } + } +} + +/* -------------------------------------------------------------------- */ +/** \name Box Draw Style + * + * Useful for 3D views, see: #ED_GIZMO_CAGE2D_STYLE_BOX + * \{ */ + +static void cage3d_draw_box_corners( + const float r[3], const float margin[3], const float color[3]) +{ + uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT); + UNUSED_VARS(margin); + + immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); + immUniformColor3fv(color); + + imm_draw_cube_wire_3d(pos, (float[3]){0}, r); + + immUnbindProgram(); +} + +static void cage3d_draw_box_interaction( + const float color[4], const int highlighted, + const float size[3], const float margin[3]) +{ + if (highlighted >= ED_GIZMO_CAGE3D_PART_SCALE_MIN_X_MIN_Y_MIN_Z && + highlighted <= ED_GIZMO_CAGE3D_PART_SCALE_MAX_X_MAX_Y_MAX_Z) + { + int index = (highlighted - ED_GIZMO_CAGE3D_PART_SCALE_MIN_X_MIN_Y_MIN_Z); + int range[3]; + range[2] = index % 3; + index = index / 3; + range[1] = index % 3; + index = index / 3; + range[0] = index % 3; + + const float sign[3] = {-1.0f, 0.0f, 1.0f}; + float co[3]; + + for (int i = 0; i < 3; i++) { + co[i] = size[i] * sign[range[i]]; + } + const float rad[3] = {margin[0] / 3, margin[1] / 3, margin[2] / 3}; + + { + uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT); + immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); + immUniformColor3fv(color); + imm_draw_cube_fill_3d(pos, co, rad); + immUnbindProgram(); + } + } +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Circle Draw Style + * + * Useful for 2D views, see: #ED_GIZMO_CAGE2D_STYLE_CIRCLE + * \{ */ + +static void imm_draw_point_aspect_3d( + uint pos, const float co[3], const float rad[3], bool solid) +{ + if (solid) { + imm_draw_cube_fill_3d(pos, co, rad); + } + else { + imm_draw_cube_wire_3d(pos, co, rad); + } +} + +static void cage3d_draw_circle_wire( + const float r[3], const float margin[3], const float color[3], + const int transform_flag, const int draw_options) +{ + uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT); + + immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); + immUniformColor3fv(color); + + imm_draw_cube_wire_3d(pos, (float[3]){0}, r); + +#if 0 + if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE) { + if (draw_options & ED_GIZMO_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE) { + const float rad[2] = {margin[0] / 2, margin[1] / 2}; + const float center[2] = {0.0f, 0.0f}; + + immBegin(GPU_PRIM_LINES, 4); + immVertex2f(pos, center[0] - rad[0], center[1] - rad[1]); + immVertex2f(pos, center[0] + rad[0], center[1] + rad[1]); + immVertex2f(pos, center[0] + rad[0], center[1] - rad[1]); + immVertex2f(pos, center[0] - rad[0], center[1] + rad[1]); + immEnd(); + } + } +#else + UNUSED_VARS(margin, transform_flag, draw_options); +#endif + + + immUnbindProgram(); +} + +static void cage3d_draw_circle_handles( + const RegionView3D *rv3d, const float matrix_final[4][4], + const float r[3], const float margin[3], const float color[3], + bool solid, float scale) +{ + uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT); + const float rad[3] = {margin[0] / 3, margin[1] / 3, margin[2] / 3}; + + immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); + immUniformColor3fv(color); + + float sign[3] = {-1.0f, 0.0f, 1.0f}; + for (int x = 0; x < 3; x++) { + for (int y = 0; y < 3; y++) { + for (int z = 0; z < 3; z++) { + if (x == 1 && y == 1 && z == 1) { + continue; + } + const float co[3] = {r[0] * sign[x], r[1] * sign[y], r[2] * sign[z]}; + float co_test[3]; + mul_v3_m4v3(co_test, matrix_final, co); + float rad_scale[3]; + mul_v3_v3fl(rad_scale, rad, ED_view3d_pixel_size(rv3d, co_test) * scale); + imm_draw_point_aspect_3d(pos, co, rad_scale, solid); + } + } + } + + immUnbindProgram(); +} + +/** \} */ + +static void gizmo_cage3d_draw_intern( + RegionView3D *rv3d, + wmGizmo *gz, const bool select, const bool highlight, const int select_id) +{ + // const bool use_clamp = (gz->parent_gzgroup->type->flag & WM_GIZMOGROUPTYPE_3D) == 0; + float dims[3]; + RNA_float_get_array(gz->ptr, "dimensions", dims); + float matrix_final[4][4]; + + const int transform_flag = RNA_enum_get(gz->ptr, "transform"); + const int draw_style = RNA_enum_get(gz->ptr, "draw_style"); + const int draw_options = RNA_enum_get(gz->ptr, "draw_options"); + + const float size_real[3] = {dims[0] / 2.0f, dims[1] / 2.0f, dims[2] / 2.0f}; + + WM_gizmo_calc_matrix_final(gz, matrix_final); + + GPU_matrix_push(); + GPU_matrix_mul(matrix_final); + + float margin[3]; + gizmo_calc_rect_view_margin(gz, dims, margin); + + /* Handy for quick testing draw (if it's outside bounds). */ + if (false) { + GPU_blend(true); + uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT); + immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); + immUniformColor4fv((const float[4]){1, 1, 1, 0.5f}); + float s = 0.5f; + immRectf(pos, -s, -s, s, s); + immUnbindProgram(); + GPU_blend(false); + } + + if (select) { + /* expand for hotspot */ +#if 0 + const float size[3] = { + size_real[0] + margin[0] / 2, + size_real[1] + margin[1] / 2, + size_real[2] + margin[2] / 2, + }; +#else + /* just use same value for now. */ + const float size[3] = {UNPACK3(size_real)}; +#endif + + + if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE) { + for (int i = ED_GIZMO_CAGE3D_PART_SCALE_MIN_X_MIN_Y_MIN_Z; + i <= ED_GIZMO_CAGE3D_PART_SCALE_MAX_X_MAX_Y_MAX_Z; + i++) + { + if (i == ED_GIZMO_CAGE3D_PART_SCALE_MID_X_MID_Y_MID_Z) { + continue; + } + GPU_select_load_id(select_id | i); + cage3d_draw_box_interaction( + gz->color, i, size, margin); + } + } + if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE) { + const int transform_part = ED_GIZMO_CAGE3D_PART_TRANSLATE; + GPU_select_load_id(select_id | transform_part); + cage3d_draw_box_interaction( + gz->color, transform_part, size, margin); + } + } + else { +#if 0 + const rctf _r = { + .xmin = -size_real[0], + .ymin = -size_real[1], + .xmax = size_real[0], + .ymax = size_real[1], + }; +#endif + if (draw_style == ED_GIZMO_CAGE2D_STYLE_BOX) { + /* corner gizmos */ + GPU_line_width(gz->line_width + 3.0f); + cage3d_draw_box_corners(size_real, margin, (const float[3]){0, 0, 0}); + + /* corner gizmos */ + float color[4]; + gizmo_color_get(gz, highlight, color); + GPU_line_width(gz->line_width); + cage3d_draw_box_corners(size_real, margin, color); + + bool show = false; + if (gz->highlight_part == ED_GIZMO_CAGE3D_PART_TRANSLATE) { + /* Only show if we're drawing the center handle + * otherwise the entire rectangle is the hotspot. */ + if (draw_options & ED_GIZMO_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE) { + show = true; + } + } + else { + show = true; + } + + if (show) { + cage3d_draw_box_interaction( + gz->color, gz->highlight_part, size_real, margin); + } + } + else if (draw_style == ED_GIZMO_CAGE2D_STYLE_CIRCLE) { + float color[4]; + gizmo_color_get(gz, highlight, color); + + GPU_line_smooth(true); + GPU_polygon_smooth(true); + GPU_blend(true); + + GPU_line_width(gz->line_width + 3.0f); + cage3d_draw_circle_wire(size_real, margin, (const float[3]){0, 0, 0}, transform_flag, draw_options); + GPU_line_width(gz->line_width); + cage3d_draw_circle_wire(size_real, margin, color, transform_flag, draw_options); + + /* corner gizmos */ + cage3d_draw_circle_handles(rv3d, matrix_final, size_real, margin, (const float[3]){0, 0, 0}, true, 60); + cage3d_draw_circle_handles(rv3d, matrix_final, size_real, margin, color, true, 40); + + GPU_blend(false); + GPU_polygon_smooth(false); + GPU_line_smooth(false); + } + else { + BLI_assert(0); + } + } + + GPU_line_width(1.0); + GPU_matrix_pop(); +} + +/** + * For when we want to draw 3d cage in 3d views. + */ +static void gizmo_cage3d_draw_select(const bContext *C, wmGizmo *gz, int select_id) +{ + ARegion *ar = CTX_wm_region(C); + RegionView3D *rv3d = ar->regiondata; + gizmo_cage3d_draw_intern(rv3d, gz, true, false, select_id); +} + +static void gizmo_cage3d_draw(const bContext *C, wmGizmo *gz) +{ + ARegion *ar = CTX_wm_region(C); + RegionView3D *rv3d = ar->regiondata; + const bool is_highlight = (gz->state & WM_GIZMO_STATE_HIGHLIGHT) != 0; + gizmo_cage3d_draw_intern(rv3d, gz, false, is_highlight, -1); +} + +static int gizmo_cage3d_get_cursor(wmGizmo *gz) +{ + if (gz->parent_gzgroup->type->flag & WM_GIZMOGROUPTYPE_3D) { + return BC_NSEW_SCROLLCURSOR; + } + + return CURSOR_STD; +} + +typedef struct RectTransformInteraction { + float orig_mouse[3]; + float orig_matrix_offset[4][4]; + float orig_matrix_final_no_offset[4][4]; +} RectTransformInteraction; + +static void gizmo_cage3d_setup(wmGizmo *gz) +{ + gz->flag |= /* WM_GIZMO_DRAW_MODAL | */ /* TODO */ + WM_GIZMO_DRAW_NO_SCALE; +} + +static int gizmo_cage3d_invoke( + bContext *C, wmGizmo *gz, const wmEvent *event) +{ + RectTransformInteraction *data = MEM_callocN(sizeof(RectTransformInteraction), "cage_interaction"); + + copy_m4_m4(data->orig_matrix_offset, gz->matrix_offset); + gizmo_calc_matrix_final_no_offset(gz, data->orig_matrix_final_no_offset, true); + + if (gizmo_window_project_3d( + C, gz, (const float[2]){UNPACK2(event->mval)}, false, data->orig_mouse) == 0) + { + zero_v3(data->orig_mouse); + } + + gz->interaction_data = data; + + return OPERATOR_RUNNING_MODAL; +} + +static int gizmo_cage3d_modal( + bContext *C, wmGizmo *gz, const wmEvent *event, + eWM_GizmoFlagTweak UNUSED(tweak_flag)) +{ + if (event->type != MOUSEMOVE) { + return OPERATOR_RUNNING_MODAL; + } + /* For transform logic to be manageable we operate in -0.5..0.5 2D space, + * no matter the size of the rectangle, mouse coorts are scaled to unit space. + * The mouse coords have been projected into the matrix so we don't need to worry about axis alignment. + * + * - The cursor offset are multiplied by 'dims'. + * - Matrix translation is also multiplied by 'dims'. + */ + RectTransformInteraction *data = gz->interaction_data; + float point_local[3]; + + float dims[3]; + RNA_float_get_array(gz->ptr, "dimensions", dims); + + { + float matrix_back[4][4]; + copy_m4_m4(matrix_back, gz->matrix_offset); + copy_m4_m4(gz->matrix_offset, data->orig_matrix_offset); + + bool ok = gizmo_window_project_3d( + C, gz, (const float[2]){UNPACK2(event->mval)}, false, point_local); + copy_m4_m4(gz->matrix_offset, matrix_back); + if (!ok) { + return OPERATOR_RUNNING_MODAL; + } + } + + const int transform_flag = RNA_enum_get(gz->ptr, "transform"); + wmGizmoProperty *gz_prop; + + gz_prop = WM_gizmo_target_property_find(gz, "matrix"); + if (gz_prop->type != NULL) { + WM_gizmo_target_property_float_get_array(gz, gz_prop, &gz->matrix_offset[0][0]); + } + + if (gz->highlight_part == ED_GIZMO_CAGE3D_PART_TRANSLATE) { + /* do this to prevent clamping from changing size */ + copy_m4_m4(gz->matrix_offset, data->orig_matrix_offset); + gz->matrix_offset[3][0] = data->orig_matrix_offset[3][0] + (point_local[0] - data->orig_mouse[0]); + gz->matrix_offset[3][1] = data->orig_matrix_offset[3][1] + (point_local[1] - data->orig_mouse[1]); + gz->matrix_offset[3][2] = data->orig_matrix_offset[3][2] + (point_local[2] - data->orig_mouse[2]); + } + else if (gz->highlight_part == ED_GIZMO_CAGE3D_PART_ROTATE) { + /* TODO (if needed) */ + } + else { + /* scale */ + copy_m4_m4(gz->matrix_offset, data->orig_matrix_offset); + float pivot[3]; + bool constrain_axis[3] = {false}; + + if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE) { + gizmo_rect_pivot_from_scale_part(gz->highlight_part, pivot, constrain_axis); + } + else { + zero_v3(pivot); + } + + /* Cursor deltas scaled to (-0.5..0.5). */ + float delta_orig[3], delta_curr[3]; + + for (int i = 0; i < 3; i++) { + delta_orig[i] = ((data->orig_mouse[i] - data->orig_matrix_offset[3][i]) / dims[i]) - pivot[i]; + delta_curr[i] = ((point_local[i] - data->orig_matrix_offset[3][i]) / dims[i]) - pivot[i]; + } + + float scale[3] = {1.0f, 1.0f, 1.0f}; + for (int i = 0; i < 3; i++) { + if (constrain_axis[i] == false) { + if (delta_orig[i] < 0.0f) { + delta_orig[i] *= -1.0f; + delta_curr[i] *= -1.0f; + } + const int sign = signum_i(scale[i]); + + scale[i] = 1.0f + ((delta_curr[i] - delta_orig[i]) / len_v3(data->orig_matrix_offset[i])); + + if ((transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE_SIGNED) == 0) { + if (sign != signum_i(scale[i])) { + scale[i] = 0.0f; + } + } + } + } + + if (transform_flag & ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE_UNIFORM) { + if (constrain_axis[0] == false && constrain_axis[1] == false) { + scale[1] = scale[0] = (scale[1] + scale[0]) / 2.0f; + } + else if (constrain_axis[0] == false) { + scale[1] = scale[0]; + } + else if (constrain_axis[1] == false) { + scale[0] = scale[1]; + } + else { + BLI_assert(0); + } + } + + /* scale around pivot */ + float matrix_scale[4][4]; + unit_m4(matrix_scale); + + mul_v3_fl(matrix_scale[0], scale[0]); + mul_v3_fl(matrix_scale[1], scale[1]); + mul_v3_fl(matrix_scale[2], scale[2]); + + transform_pivot_set_m4( + matrix_scale, + (const float[3]){pivot[0] * dims[0], pivot[1] * dims[1], pivot[2] * dims[2]}); + mul_m4_m4m4(gz->matrix_offset, data->orig_matrix_offset, matrix_scale); + } + + if (gz_prop->type != NULL) { + WM_gizmo_target_property_float_set_array(C, gz, gz_prop, &gz->matrix_offset[0][0]); + } + + /* tag the region for redraw */ + ED_region_tag_redraw(CTX_wm_region(C)); + WM_event_add_mousemove(C); + + return OPERATOR_RUNNING_MODAL; +} + +static void gizmo_cage3d_property_update(wmGizmo *gz, wmGizmoProperty *gz_prop) +{ + if (STREQ(gz_prop->type->idname, "matrix")) { + if (WM_gizmo_target_property_array_length(gz, gz_prop) == 16) { + WM_gizmo_target_property_float_get_array(gz, gz_prop, &gz->matrix_offset[0][0]); + } + else { + BLI_assert(0); + } + } + else { + BLI_assert(0); + } +} + +static void gizmo_cage3d_exit(bContext *C, wmGizmo *gz, const bool cancel) +{ + RectTransformInteraction *data = gz->interaction_data; + + if (!cancel) + return; + + wmGizmoProperty *gz_prop; + + /* reset properties */ + gz_prop = WM_gizmo_target_property_find(gz, "matrix"); + if (gz_prop->type != NULL) { + WM_gizmo_target_property_float_set_array(C, gz, gz_prop, &data->orig_matrix_offset[0][0]); + } + + copy_m4_m4(gz->matrix_offset, data->orig_matrix_offset); +} + + +/* -------------------------------------------------------------------- */ +/** \name Cage Gizmo API + * + * \{ */ + +static void GIZMO_GT_cage_3d(wmGizmoType *gzt) +{ + /* identifiers */ + gzt->idname = "GIZMO_GT_cage_3d"; + + /* api callbacks */ + gzt->draw = gizmo_cage3d_draw; + gzt->draw_select = gizmo_cage3d_draw_select; + gzt->setup = gizmo_cage3d_setup; + gzt->invoke = gizmo_cage3d_invoke; + gzt->property_update = gizmo_cage3d_property_update; + gzt->modal = gizmo_cage3d_modal; + gzt->exit = gizmo_cage3d_exit; + gzt->cursor_get = gizmo_cage3d_get_cursor; + + gzt->struct_size = sizeof(wmGizmo); + + /* rna */ + static EnumPropertyItem rna_enum_draw_style[] = { + {ED_GIZMO_CAGE2D_STYLE_BOX, "BOX", 0, "Box", ""}, + {ED_GIZMO_CAGE2D_STYLE_CIRCLE, "CIRCLE", 0, "Circle", ""}, + {0, NULL, 0, NULL, NULL} + }; + static EnumPropertyItem rna_enum_transform[] = { + {ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE, "TRANSLATE", 0, "Move", ""}, + {ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE, "SCALE", 0, "Scale", ""}, + {ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE_UNIFORM, "SCALE_UNIFORM", 0, "Scale Uniform", ""}, + {0, NULL, 0, NULL, NULL} + }; + static EnumPropertyItem rna_enum_draw_options[] = { + {ED_GIZMO_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE, "XFORM_CENTER_HANDLE", 0, "Center Handle", ""}, + {0, NULL, 0, NULL, NULL} + }; + static float unit_v3[3] = {1.0f, 1.0f, 1.0f}; + RNA_def_float_vector(gzt->srna, "dimensions", 3, unit_v3, 0, FLT_MAX, "Dimensions", "", 0.0f, FLT_MAX); + RNA_def_enum_flag(gzt->srna, "transform", rna_enum_transform, 0, "Transform Options", ""); + RNA_def_enum(gzt->srna, "draw_style", rna_enum_draw_style, ED_GIZMO_CAGE2D_STYLE_CIRCLE, "Draw Style", ""); + RNA_def_enum_flag( + gzt->srna, "draw_options", rna_enum_draw_options, + ED_GIZMO_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE, "Draw Options", ""); + + WM_gizmotype_target_property_def(gzt, "matrix", PROP_FLOAT, 16); +} + +void ED_gizmotypes_cage_3d(void) +{ + WM_gizmotype_append(GIZMO_GT_cage_3d); +} + +/** \} */ diff --git a/source/blender/editors/gizmo_library/gizmo_types/dial3d_gizmo.c b/source/blender/editors/gizmo_library/gizmo_types/dial3d_gizmo.c new file mode 100644 index 00000000000..48ac16290df --- /dev/null +++ b/source/blender/editors/gizmo_library/gizmo_types/dial3d_gizmo.c @@ -0,0 +1,572 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2014 Blender Foundation. + * All rights reserved. + * + * Contributor(s): Blender Foundation + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file dial3d_gizmo.c + * \ingroup edgizmolib + * + * \name Dial Gizmo + * + * 3D Gizmo + * + * \brief Circle shaped gizmo for circular interaction. + * Currently no own handling, use with operator only. + * + * - `matrix[0]` is derived from Y and Z. + * - `matrix[1]` is 'up' when DialGizmo.use_start_y_axis is set. + * - `matrix[2]` is the axis the dial rotates around (all dials). + */ + +#include "MEM_guardedalloc.h" + +#include "BLI_math.h" + +#include "BKE_context.h" + +#include "BIF_gl.h" +#include "BIF_glutil.h" + +#include "GPU_immediate.h" +#include "GPU_immediate_util.h" +#include "GPU_matrix.h" +#include "GPU_select.h" +#include "GPU_state.h" + +#include "RNA_access.h" +#include "RNA_define.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "ED_screen.h" +#include "ED_view3d.h" +#include "ED_gizmo_library.h" + +/* own includes */ +#include "../gizmo_geometry.h" +#include "../gizmo_library_intern.h" + +/* To use custom dials exported to geom_dial_gizmo.c */ +// #define USE_GIZMO_CUSTOM_DIAL + +static int gizmo_dial_modal( + bContext *C, wmGizmo *gz, const wmEvent *event, + eWM_GizmoFlagTweak tweak_flag); + +typedef struct DialInteraction { + struct { + float mval[2]; + /* Only for when using properties. */ + float prop_angle; + } init; + struct { + /* Cache the last angle to detect rotations bigger than -/+ PI. */ + eWM_GizmoFlagTweak tweak_flag; + float angle; + } prev; + + /* Number of full rotations. */ + int rotations; + bool has_drag; + + /* Final output values, used for drawing. */ + struct { + float angle_ofs; + float angle_delta; + } output; +} DialInteraction; + +#define DIAL_WIDTH 1.0f +#define DIAL_RESOLUTION 48 + +/* Could make option, negative to clip more (don't show when view aligned). */ +#define DIAL_CLIP_BIAS 0.02 + +/* -------------------------------------------------------------------- */ + +static void dial_geom_draw( + const wmGizmo *gz, const float color[4], const bool select, + float axis_modal_mat[4][4], float clip_plane[4], + const float arc_partial_angle, const float arc_inner_factor) +{ +#ifdef USE_GIZMO_CUSTOM_DIAL + UNUSED_VARS(gz, axis_modal_mat, clip_plane); + wm_gizmo_geometryinfo_draw(&wm_gizmo_geom_data_dial, select, color); +#else + const int draw_options = RNA_enum_get(gz->ptr, "draw_options"); + const bool filled = (draw_options & ED_GIZMO_DIAL_DRAW_FLAG_FILL) != 0; + + GPU_line_width(gz->line_width); + + GPUVertFormat *format = immVertexFormat(); + uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); + + if (clip_plane) { + immBindBuiltinProgram(GPU_SHADER_3D_CLIPPED_UNIFORM_COLOR); + float clip_plane_f[4] = {clip_plane[0], clip_plane[1], clip_plane[2], clip_plane[3]}; + immUniform4fv("ClipPlane", clip_plane_f); + immUniformMatrix4fv("ModelMatrix", axis_modal_mat); + } + else { + immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); + } + + immUniformColor4fv(color); + + if (filled) { + imm_draw_circle_fill_2d(pos, 0, 0, 1.0, DIAL_RESOLUTION); + } + else { + if (arc_partial_angle == 0.0f) { + imm_draw_circle_wire_2d(pos, 0, 0, 1.0, DIAL_RESOLUTION); + if (arc_inner_factor != 0.0f) { + imm_draw_circle_wire_2d(pos, 0, 0, arc_inner_factor, DIAL_RESOLUTION); + } + } + else { + float arc_partial_deg = RAD2DEGF((M_PI * 2) - arc_partial_angle); + imm_draw_circle_partial_wire_2d( + pos, 0, 0, 1.0, DIAL_RESOLUTION, + -arc_partial_deg / 2, arc_partial_deg); +#if 0 + if (arc_inner_factor != 0.0f) { + BLI_assert(0); + } +#endif + } + } + + immUnbindProgram(); + + UNUSED_VARS(select); +#endif +} + +/** + * Draws a line from (0, 0, 0) to \a co_outer, at \a angle. + */ +static void dial_ghostarc_draw_helpline( + const float angle, const float co_outer[3], const float color[4]) +{ + GPU_matrix_push(); + GPU_matrix_rotate_3f(RAD2DEGF(angle), 0.0f, 0.0f, -1.0f); + + uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT); + + immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); + + immUniformColor4fv(color); + + immBegin(GPU_PRIM_LINE_STRIP, 2); + immVertex3f(pos, 0.0f, 0, 0.0f); + immVertex3fv(pos, co_outer); + immEnd(); + + immUnbindProgram(); + + GPU_matrix_pop(); +} + +static void dial_ghostarc_draw( + const float angle_ofs, const float angle_delta, + const float arc_inner_factor, const float color[4]) +{ + const float width_inner = DIAL_WIDTH; + GPUVertFormat *format = immVertexFormat(); + uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); + immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); + + if (arc_inner_factor != 0.0) { + float color_dark[4] = {0}; + color_dark[3] = color[3] / 2; + immUniformColor4fv(color_dark); + imm_draw_disk_partial_fill_2d( + pos, 0, 0, arc_inner_factor, width_inner, DIAL_RESOLUTION, RAD2DEGF(angle_ofs), RAD2DEGF(M_PI * 2)); + } + + immUniformColor4fv(color); + imm_draw_disk_partial_fill_2d( + pos, 0, 0, arc_inner_factor, width_inner, DIAL_RESOLUTION, RAD2DEGF(angle_ofs), RAD2DEGF(angle_delta)); + immUnbindProgram(); +} + +static void dial_ghostarc_get_angles( + const wmGizmo *gz, + const wmEvent *event, + const ARegion *ar, + float mat[4][4], const float co_outer[3], + float *r_start, float *r_delta) +{ + DialInteraction *inter = gz->interaction_data; + const RegionView3D *rv3d = ar->regiondata; + const float mval[2] = {event->x - ar->winrct.xmin, event->y - ar->winrct.ymin}; + + /* We might need to invert the direction of the angles. */ + float view_vec[3], axis_vec[3]; + ED_view3d_global_to_vector(rv3d, gz->matrix_basis[3], view_vec); + normalize_v3_v3(axis_vec, gz->matrix_basis[2]); + + float proj_outer_rel[3]; + mul_v3_project_m4_v3(proj_outer_rel, mat, co_outer); + sub_v3_v3(proj_outer_rel, gz->matrix_basis[3]); + + float proj_mval_new_rel[3]; + float proj_mval_init_rel[3]; + float dial_plane[4]; + + plane_from_point_normal_v3(dial_plane, gz->matrix_basis[3], axis_vec); + + if (!ED_view3d_win_to_3d_on_plane(ar, dial_plane, inter->init.mval, false, proj_mval_init_rel)) { + goto fail; + } + sub_v3_v3(proj_mval_init_rel, gz->matrix_basis[3]); + + if (!ED_view3d_win_to_3d_on_plane(ar, dial_plane, mval, false, proj_mval_new_rel)) { + goto fail; + } + sub_v3_v3(proj_mval_new_rel, gz->matrix_basis[3]); + + const int draw_options = RNA_enum_get(gz->ptr, "draw_options"); + + /* Start direction from mouse or set by user. */ + const float *proj_init_rel = + (draw_options & ED_GIZMO_DIAL_DRAW_FLAG_ANGLE_START_Y) ? + gz->matrix_basis[1] : proj_mval_init_rel; + + /* Return angles. */ + const float start = angle_wrap_rad(angle_signed_on_axis_v3v3_v3(proj_outer_rel, proj_init_rel, axis_vec)); + const float delta = angle_wrap_rad(angle_signed_on_axis_v3v3_v3(proj_mval_init_rel, proj_mval_new_rel, axis_vec)); + + /* Change of sign, we passed the 180 degree threshold. This means we need to add a turn + * to distinguish between transition from 0 to -1 and -PI to +PI, use comparison with PI/2. + * Logic taken from #BLI_dial_angle */ + if ((delta * inter->prev.angle < 0.0f) && + (fabsf(inter->prev.angle) > (float)M_PI_2)) + { + if (inter->prev.angle < 0.0f) { + inter->rotations--; + } + else { + inter->rotations++; + } + } + inter->prev.angle = delta; + + const bool wrap_angle = RNA_boolean_get(gz->ptr, "wrap_angle"); + const double delta_final = (double)delta + ((2 * M_PI) * (double)inter->rotations); + *r_start = start; + *r_delta = (float)(wrap_angle ? fmod(delta_final, 2 * M_PI) : delta_final); + return; + + /* If we can't project (unlikely). */ +fail: + *r_start = 0.0; + *r_delta = 0.0; +} + +static void dial_ghostarc_draw_with_helplines( + const float angle_ofs, const float angle_delta, + const float arc_inner_factor, const float color_helpline[4], const int draw_options) +{ + /* Coordinate at which the arc drawing will be started. */ + const float co_outer[4] = {0.0f, DIAL_WIDTH, 0.0f}; + dial_ghostarc_draw(angle_ofs, angle_delta, arc_inner_factor, (const float[4]){0.8f, 0.8f, 0.8f, 0.4f}); + GPU_line_width(1.0f); + dial_ghostarc_draw_helpline(angle_ofs, co_outer, color_helpline); + if (draw_options & ED_GIZMO_DIAL_DRAW_FLAG_ANGLE_VALUE) { + GPU_line_width(3.0f); + } + dial_ghostarc_draw_helpline(angle_ofs + angle_delta, co_outer, color_helpline); +} + +static void dial_draw_intern( + const bContext *C, wmGizmo *gz, + const bool select, const bool highlight, float clip_plane[4]) +{ + float matrix_final[4][4]; + float color[4]; + + BLI_assert(CTX_wm_area(C)->spacetype == SPACE_VIEW3D); + + gizmo_color_get(gz, highlight, color); + + WM_gizmo_calc_matrix_final(gz, matrix_final); + + GPU_matrix_push(); + GPU_matrix_mul(matrix_final); + + /* FIXME(campbell): look into removing this. */ + if ((gz->flag & WM_GIZMO_DRAW_VALUE) && + (gz->state & WM_GIZMO_STATE_MODAL)) + { + /* XXX, View3D rotation gizmo doesn't call modal. */ + if (!WM_gizmo_target_property_is_valid_any(gz)) { + wmWindow *win = CTX_wm_window(C); + gizmo_dial_modal((bContext *)C, gz, win->eventstate, 0); + } + } + + GPU_polygon_smooth(false); + + + const float arc_partial_angle = RNA_float_get(gz->ptr, "arc_partial_angle"); + const float arc_inner_factor = RNA_float_get(gz->ptr, "arc_inner_factor"); + if (select == false) { + float angle_ofs = 0.0f; + float angle_delta = 0.0f; + bool show_ghostarc = false; + + /* Draw rotation indicator arc first. */ + wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset"); + const int draw_options = RNA_enum_get(gz->ptr, "draw_options"); + + if (WM_gizmo_target_property_is_valid(gz_prop) && + (draw_options & ED_GIZMO_DIAL_DRAW_FLAG_ANGLE_VALUE)) + { + angle_ofs = 0.0f; + angle_delta = WM_gizmo_target_property_float_get(gz, gz_prop); + show_ghostarc = true; + } + else if ((gz->flag & WM_GIZMO_DRAW_VALUE) && + (gz->state & WM_GIZMO_STATE_MODAL)) + { + DialInteraction *inter = gz->interaction_data; + angle_ofs = inter->output.angle_ofs; + angle_delta = inter->output.angle_delta; + show_ghostarc = true; + } + + if (show_ghostarc) { + dial_ghostarc_draw_with_helplines(angle_ofs, angle_delta, arc_inner_factor, color, draw_options); + if ((draw_options & ED_GIZMO_DIAL_DRAW_FLAG_ANGLE_MIRROR) != 0) { + angle_ofs += M_PI; + dial_ghostarc_draw_with_helplines(angle_ofs, angle_delta, arc_inner_factor, color, draw_options); + } + } + } + + /* Draw actual dial gizmo. */ + dial_geom_draw(gz, color, select, gz->matrix_basis, clip_plane, arc_partial_angle, arc_inner_factor); + + GPU_matrix_pop(); +} + +static void gizmo_dial_draw_select(const bContext *C, wmGizmo *gz, int select_id) +{ + float clip_plane_buf[4]; + const int draw_options = RNA_enum_get(gz->ptr, "draw_options"); + float *clip_plane = (draw_options & ED_GIZMO_DIAL_DRAW_FLAG_CLIP) ? clip_plane_buf : NULL; + + if (clip_plane) { + ARegion *ar = CTX_wm_region(C); + RegionView3D *rv3d = ar->regiondata; + + copy_v3_v3(clip_plane, rv3d->viewinv[2]); + clip_plane[3] = -dot_v3v3(rv3d->viewinv[2], gz->matrix_basis[3]); + clip_plane[3] += DIAL_CLIP_BIAS; + glEnable(GL_CLIP_DISTANCE0); + } + + GPU_select_load_id(select_id); + dial_draw_intern(C, gz, true, false, clip_plane); + + if (clip_plane) { + glDisable(GL_CLIP_DISTANCE0); + } +} + +static void gizmo_dial_draw(const bContext *C, wmGizmo *gz) +{ + const bool is_modal = gz->state & WM_GIZMO_STATE_MODAL; + const bool is_highlight = (gz->state & WM_GIZMO_STATE_HIGHLIGHT) != 0; + float clip_plane_buf[4]; + const int draw_options = RNA_enum_get(gz->ptr, "draw_options"); + float *clip_plane = (!is_modal && (draw_options & ED_GIZMO_DIAL_DRAW_FLAG_CLIP)) ? clip_plane_buf : NULL; + + if (clip_plane) { + ARegion *ar = CTX_wm_region(C); + RegionView3D *rv3d = ar->regiondata; + + copy_v3_v3(clip_plane, rv3d->viewinv[2]); + clip_plane[3] = -dot_v3v3(rv3d->viewinv[2], gz->matrix_basis[3]); + clip_plane[3] += DIAL_CLIP_BIAS; + + glEnable(GL_CLIP_DISTANCE0); + } + + GPU_blend(true); + dial_draw_intern(C, gz, false, is_highlight, clip_plane); + GPU_blend(false); + + if (clip_plane) { + glDisable(GL_CLIP_DISTANCE0); + } +} + +static int gizmo_dial_modal( + bContext *C, wmGizmo *gz, const wmEvent *event, + eWM_GizmoFlagTweak tweak_flag) +{ + DialInteraction *inter = gz->interaction_data; + if ((event->type != MOUSEMOVE) && (inter->prev.tweak_flag == tweak_flag)) { + return OPERATOR_RUNNING_MODAL; + } + /* Coordinate at which the arc drawing will be started. */ + const float co_outer[4] = {0.0f, DIAL_WIDTH, 0.0f}; + float angle_ofs, angle_delta; + + dial_ghostarc_get_angles( + gz, event, CTX_wm_region(C), gz->matrix_basis, co_outer, &angle_ofs, &angle_delta); + + if (tweak_flag & WM_GIZMO_TWEAK_SNAP) { + const double snap = DEG2RAD(5); + angle_delta = (float)roundf((double)angle_delta / snap) * snap; + } + if (tweak_flag & WM_GIZMO_TWEAK_PRECISE) { + angle_delta *= 0.1f; + } + if (angle_delta != 0.0f) { + inter->has_drag = true; + } + + inter->output.angle_delta = angle_delta; + inter->output.angle_ofs = angle_ofs; + + /* Set the property for the operator and call its modal function. */ + wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset"); + if (WM_gizmo_target_property_is_valid(gz_prop)) { + WM_gizmo_target_property_float_set(C, gz, gz_prop, inter->init.prop_angle + angle_delta); + } + + inter->prev.tweak_flag = tweak_flag; + + return OPERATOR_RUNNING_MODAL; +} + +static void gizmo_dial_exit(bContext *C, wmGizmo *gz, const bool cancel) +{ + DialInteraction *inter = gz->interaction_data; + bool use_reset_value = false; + float reset_value = 0.0f; + if (cancel) { + /* Set the property for the operator and call its modal function. */ + wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset"); + if (WM_gizmo_target_property_is_valid(gz_prop)) { + use_reset_value = true; + reset_value = inter->init.prop_angle; + } + } + else { + if (inter->has_drag == false) { + PropertyRNA *prop = RNA_struct_find_property(gz->ptr, "click_value"); + if (RNA_property_is_set(gz->ptr, prop)) { + use_reset_value = true; + reset_value = RNA_property_float_get(gz->ptr, prop); + } + } + } + + if (use_reset_value) { + wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset"); + if (WM_gizmo_target_property_is_valid(gz_prop)) { + WM_gizmo_target_property_float_set(C, gz, gz_prop, reset_value); + } + } + +} + + +static void gizmo_dial_setup(wmGizmo *gz) +{ + const float dir_default[3] = {0.0f, 0.0f, 1.0f}; + + /* defaults */ + copy_v3_v3(gz->matrix_basis[2], dir_default); +} + +static int gizmo_dial_invoke( + bContext *UNUSED(C), wmGizmo *gz, const wmEvent *event) +{ + DialInteraction *inter = MEM_callocN(sizeof(DialInteraction), __func__); + + inter->init.mval[0] = event->mval[0]; + inter->init.mval[1] = event->mval[1]; + + wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset"); + if (WM_gizmo_target_property_is_valid(gz_prop)) { + inter->init.prop_angle = WM_gizmo_target_property_float_get(gz, gz_prop); + } + + gz->interaction_data = inter; + + return OPERATOR_RUNNING_MODAL; +} + +/* -------------------------------------------------------------------- */ +/** \name Dial Gizmo API + * + * \{ */ + +static void GIZMO_GT_dial_3d(wmGizmoType *gzt) +{ + /* identifiers */ + gzt->idname = "GIZMO_GT_dial_3d"; + + /* api callbacks */ + gzt->draw = gizmo_dial_draw; + gzt->draw_select = gizmo_dial_draw_select; + gzt->setup = gizmo_dial_setup; + gzt->invoke = gizmo_dial_invoke; + gzt->modal = gizmo_dial_modal; + gzt->exit = gizmo_dial_exit; + + gzt->struct_size = sizeof(wmGizmo); + + /* rna */ + static EnumPropertyItem rna_enum_draw_options[] = { + {ED_GIZMO_DIAL_DRAW_FLAG_CLIP, "CLIP", 0, "Clipped", ""}, + {ED_GIZMO_DIAL_DRAW_FLAG_FILL, "FILL", 0, "Filled", ""}, + {ED_GIZMO_DIAL_DRAW_FLAG_ANGLE_MIRROR, "ANGLE_MIRROR", 0, "Angle Mirror", ""}, + {ED_GIZMO_DIAL_DRAW_FLAG_ANGLE_START_Y, "ANGLE_START_Y", 0, "Angle Start Y", ""}, + {ED_GIZMO_DIAL_DRAW_FLAG_ANGLE_VALUE, "ANGLE_VALUE", 0, "Show Angle Value", ""}, + {0, NULL, 0, NULL, NULL} + }; + RNA_def_enum_flag(gzt->srna, "draw_options", rna_enum_draw_options, 0, "Draw Options", ""); + RNA_def_boolean(gzt->srna, "wrap_angle", true, "Wrap Angle", ""); + RNA_def_float_factor(gzt->srna, "arc_inner_factor", 0.0f, 0.0f, 1.0f, "Arc Inner Factor", "", 0.0f, 1.0f); + RNA_def_float_factor(gzt->srna, "arc_partial_angle", 0.0f, 0.0f, M_PI * 2, "Show Partial Dial", "", 0.0f, M_PI * 2); + RNA_def_float( + gzt->srna, "click_value", 0.0f, -FLT_MAX, FLT_MAX, + "Click Value", "Value to use for a single click action", + -FLT_MAX, FLT_MAX); + + WM_gizmotype_target_property_def(gzt, "offset", PROP_FLOAT, 1); +} + +void ED_gizmotypes_dial_3d(void) +{ + WM_gizmotype_append(GIZMO_GT_dial_3d); +} + +/** \} */ diff --git a/source/blender/editors/gizmo_library/gizmo_types/move3d_gizmo.c b/source/blender/editors/gizmo_library/gizmo_types/move3d_gizmo.c new file mode 100644 index 00000000000..b91a193e9a0 --- /dev/null +++ b/source/blender/editors/gizmo_library/gizmo_types/move3d_gizmo.c @@ -0,0 +1,466 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file move3d_gizmo.c + * \ingroup edgizmolib + * + * \name Move Gizmo + * + * 3D Gizmo, also works in 2D views. + * + * \brief Simple gizmo to move and translate. + * + * - `matrix[0]` is derived from Y and Z. + * - `matrix[1]` currently not used. + * - `matrix[2]` is the widget direction (for all gizmos). + * + */ + +#include "MEM_guardedalloc.h" + +#include "BLI_math.h" + +#include "BKE_context.h" + +#include "BIF_gl.h" +#include "BIF_glutil.h" + +#include "GPU_immediate.h" +#include "GPU_immediate_util.h" +#include "GPU_matrix.h" +#include "GPU_select.h" +#include "GPU_state.h" + +#include "RNA_access.h" +#include "RNA_define.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "ED_screen.h" +#include "ED_view3d.h" +#include "ED_gizmo_library.h" +#include "ED_transform_snap_object_context.h" + +/* own includes */ +#include "../gizmo_geometry.h" +#include "../gizmo_library_intern.h" + +#define MVAL_MAX_PX_DIST 12.0f + +typedef struct MoveGizmo3D { + wmGizmo gizmo; + /* Added to 'matrix_basis' when calculating the matrix. */ + float prop_co[3]; +} MoveGizmo3D; + +static void gizmo_move_matrix_basis_get(const wmGizmo *gz, float r_matrix[4][4]) +{ + MoveGizmo3D *move = (MoveGizmo3D *)gz; + + copy_m4_m4(r_matrix, move->gizmo.matrix_basis); + add_v3_v3(r_matrix[3], move->prop_co); +} + +static int gizmo_move_modal( + bContext *C, wmGizmo *gz, const wmEvent *event, + eWM_GizmoFlagTweak tweak_flag); + +typedef struct MoveInteraction { + struct { + float mval[2]; + /* Only for when using properties. */ + float prop_co[3]; + float matrix_final[4][4]; + } init; + struct { + eWM_GizmoFlagTweak tweak_flag; + } prev; + + /* We could have other snap contexts, for now only support 3D view. */ + struct SnapObjectContext *snap_context_v3d; + +} MoveInteraction; + +#define DIAL_RESOLUTION 32 + +/* -------------------------------------------------------------------- */ + +static void move_geom_draw( + const wmGizmo *gz, const float color[4], const bool select, const int draw_options) +{ +#ifdef USE_GIZMO_CUSTOM_DIAL + UNUSED_VARS(move3d, col, axis_modal_mat); + wm_gizmo_geometryinfo_draw(&wm_gizmo_geom_data_move3d, select); +#else + const int draw_style = RNA_enum_get(gz->ptr, "draw_style"); + const bool filled = (draw_options & ED_GIZMO_MOVE_DRAW_FLAG_FILL) != 0; + + GPU_line_width(gz->line_width); + + GPUVertFormat *format = immVertexFormat(); + uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); + + immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); + + immUniformColor4fv(color); + + if (draw_style == ED_GIZMO_MOVE_STYLE_RING_2D) { + if (filled) { + imm_draw_circle_fill_2d(pos, 0, 0, 1.0f, DIAL_RESOLUTION); + } + else { + imm_draw_circle_wire_2d(pos, 0, 0, 1.0f, DIAL_RESOLUTION); + } + } + else if (draw_style == ED_GIZMO_MOVE_STYLE_CROSS_2D) { + immBegin(GPU_PRIM_LINES, 4); + immVertex2f(pos, 1.0f, 1.0f); + immVertex2f(pos, -1.0f, -1.0f); + + immVertex2f(pos, -1.0f, 1.0f); + immVertex2f(pos, 1.0f, -1.0f); + immEnd(); + } + else { + BLI_assert(0); + } + + immUnbindProgram(); + + UNUSED_VARS(select); +#endif +} + +static void move3d_get_translate( + const wmGizmo *gz, const wmEvent *event, const ARegion *ar, + float co_delta[3]) +{ + MoveInteraction *inter = gz->interaction_data; + const float mval_delta[2] = { + event->mval[0] - inter->init.mval[0], + event->mval[1] - inter->init.mval[1], + }; + + RegionView3D *rv3d = ar->regiondata; + float co_ref[3]; + mul_v3_mat3_m4v3(co_ref, gz->matrix_space, inter->init.prop_co); + const float zfac = ED_view3d_calc_zfac(rv3d, co_ref, NULL); + + ED_view3d_win_to_delta(ar, mval_delta, co_delta, zfac); + + float matrix_space_inv[3][3]; + copy_m3_m4(matrix_space_inv, gz->matrix_space); + invert_m3(matrix_space_inv); + mul_m3_v3(matrix_space_inv, co_delta); +} + +static void move3d_draw_intern( + const bContext *C, wmGizmo *gz, + const bool select, const bool highlight) +{ + MoveInteraction *inter = gz->interaction_data; + const int draw_options = RNA_enum_get(gz->ptr, "draw_options"); + const bool align_view = (draw_options & ED_GIZMO_MOVE_DRAW_FLAG_ALIGN_VIEW) != 0; + float color[4]; + float matrix_final[4][4]; + float matrix_align[4][4]; + + gizmo_color_get(gz, highlight, color); + WM_gizmo_calc_matrix_final(gz, matrix_final); + + GPU_matrix_push(); + GPU_matrix_mul(matrix_final); + + if (align_view) { + float matrix_final_unit[4][4]; + RegionView3D *rv3d = CTX_wm_region_view3d(C); + normalize_m4_m4(matrix_final_unit, matrix_final); + mul_m4_m4m4(matrix_align, rv3d->viewmat, matrix_final_unit); + zero_v3(matrix_align[3]); + transpose_m4(matrix_align); + GPU_matrix_mul(matrix_align); + } + + GPU_blend(true); + move_geom_draw(gz, color, select, draw_options); + GPU_blend(false); + GPU_matrix_pop(); + + if (gz->interaction_data) { + GPU_matrix_push(); + GPU_matrix_mul(inter->init.matrix_final); + + if (align_view) { + GPU_matrix_mul(matrix_align); + } + + GPU_blend(true); + move_geom_draw(gz, (const float[4]){0.5f, 0.5f, 0.5f, 0.5f}, select, draw_options); + GPU_blend(false); + GPU_matrix_pop(); + } +} + +static void gizmo_move_draw_select(const bContext *C, wmGizmo *gz, int select_id) +{ + GPU_select_load_id(select_id); + move3d_draw_intern(C, gz, true, false); +} + +static void gizmo_move_draw(const bContext *C, wmGizmo *gz) +{ + const bool is_modal = gz->state & WM_GIZMO_STATE_MODAL; + const bool is_highlight = (gz->state & WM_GIZMO_STATE_HIGHLIGHT) != 0; + + (void)is_modal; + + GPU_blend(true); + move3d_draw_intern(C, gz, false, is_highlight); + GPU_blend(false); +} + +static int gizmo_move_modal( + bContext *C, wmGizmo *gz, const wmEvent *event, + eWM_GizmoFlagTweak tweak_flag) +{ + MoveInteraction *inter = gz->interaction_data; + if ((event->type != MOUSEMOVE) && (inter->prev.tweak_flag == tweak_flag)) { + return OPERATOR_RUNNING_MODAL; + } + MoveGizmo3D *move = (MoveGizmo3D *)gz; + ARegion *ar = CTX_wm_region(C); + + float prop_delta[3]; + if (CTX_wm_area(C)->spacetype == SPACE_VIEW3D) { + move3d_get_translate(gz, event, ar, prop_delta); + } + else { + float mval_proj_init[2], mval_proj_curr[2]; + if ((gizmo_window_project_2d( + C, gz, inter->init.mval, 2, false, mval_proj_init) == false) || + (gizmo_window_project_2d( + C, gz, (const float[2]){UNPACK2(event->mval)}, 2, false, mval_proj_curr) == false)) + { + return OPERATOR_RUNNING_MODAL; + } + sub_v2_v2v2(prop_delta, mval_proj_curr, mval_proj_init); + prop_delta[2] = 0.0f; + } + + if (tweak_flag & WM_GIZMO_TWEAK_PRECISE) { + mul_v3_fl(prop_delta, 0.1f); + } + + add_v3_v3v3(move->prop_co, inter->init.prop_co, prop_delta); + + if (tweak_flag & WM_GIZMO_TWEAK_SNAP) { + if (inter->snap_context_v3d) { + float dist_px = MVAL_MAX_PX_DIST * U.pixelsize; + const float mval_fl[2] = {UNPACK2(event->mval)}; + float co[3]; + if (ED_transform_snap_object_project_view3d( + inter->snap_context_v3d, + (SCE_SNAP_MODE_VERTEX | SCE_SNAP_MODE_EDGE | SCE_SNAP_MODE_FACE), + &(const struct SnapObjectParams){ + .snap_select = SNAP_ALL, + .use_object_edit_cage = true, + .use_occlusion_test = true, + }, + mval_fl, &dist_px, + co, NULL)) + { + float matrix_space_inv[4][4]; + invert_m4_m4(matrix_space_inv, gz->matrix_space); + mul_v3_m4v3(move->prop_co, matrix_space_inv, co); + } + } + } + + /* set the property for the operator and call its modal function */ + wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset"); + if (WM_gizmo_target_property_is_valid(gz_prop)) { + WM_gizmo_target_property_float_set_array(C, gz, gz_prop, move->prop_co); + } + else { + zero_v3(move->prop_co); + } + + ED_region_tag_redraw(ar); + + inter->prev.tweak_flag = tweak_flag; + + return OPERATOR_RUNNING_MODAL; +} + +static void gizmo_move_exit(bContext *C, wmGizmo *gz, const bool cancel) +{ + MoveInteraction *inter = gz->interaction_data; + bool use_reset_value = false; + const float *reset_value = NULL; + if (cancel) { + /* Set the property for the operator and call its modal function. */ + wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset"); + if (WM_gizmo_target_property_is_valid(gz_prop)) { + use_reset_value = true; + reset_value = inter->init.prop_co; + } + } + + if (use_reset_value) { + wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset"); + if (WM_gizmo_target_property_is_valid(gz_prop)) { + WM_gizmo_target_property_float_set_array(C, gz, gz_prop, reset_value); + } + } + + if (inter->snap_context_v3d) { + ED_transform_snap_object_context_destroy(inter->snap_context_v3d); + inter->snap_context_v3d = NULL; + } +} + +static int gizmo_move_invoke( + bContext *C, wmGizmo *gz, const wmEvent *event) +{ + const bool use_snap = RNA_boolean_get(gz->ptr, "use_snap"); + + MoveInteraction *inter = MEM_callocN(sizeof(MoveInteraction), __func__); + inter->init.mval[0] = event->mval[0]; + inter->init.mval[1] = event->mval[1]; + +#if 0 + copy_v3_v3(inter->init.prop_co, move->prop_co); +#else + wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset"); + if (WM_gizmo_target_property_is_valid(gz_prop)) { + WM_gizmo_target_property_float_get_array(gz, gz_prop, inter->init.prop_co); + } +#endif + + WM_gizmo_calc_matrix_final(gz, inter->init.matrix_final); + + if (use_snap) { + ScrArea *sa = CTX_wm_area(C); + if (sa) { + switch (sa->spacetype) { + case SPACE_VIEW3D: + { + inter->snap_context_v3d = ED_transform_snap_object_context_create_view3d( + CTX_data_main(C), CTX_data_scene(C), CTX_data_depsgraph(C), 0, + CTX_wm_region(C), CTX_wm_view3d(C)); + break; + } + default: + /* Not yet supported. */ + BLI_assert(0); + } + } + } + + gz->interaction_data = inter; + + return OPERATOR_RUNNING_MODAL; +} + + +static int gizmo_move_test_select( + bContext *C, wmGizmo *gz, const int mval[2]) +{ + float point_local[2]; + + if (gizmo_window_project_2d( + C, gz, (const float[2]){UNPACK2(mval)}, 2, true, point_local) == false) + { + return -1; + } + + /* The 'gz->scale_final' is already applied when projecting. */ + if (len_squared_v2(point_local) < 1.0f) { + return 0; + } + + return -1; +} + +static void gizmo_move_property_update(wmGizmo *gz, wmGizmoProperty *gz_prop) +{ + MoveGizmo3D *move = (MoveGizmo3D *)gz; + if (WM_gizmo_target_property_is_valid(gz_prop)) { + WM_gizmo_target_property_float_get_array(gz, gz_prop, move->prop_co); + } + else { + zero_v3(move->prop_co); + } +} + +static int gizmo_move_cursor_get(wmGizmo *UNUSED(gz)) +{ + return BC_NSEW_SCROLLCURSOR; +} + +/* -------------------------------------------------------------------- */ +/** \name Move Gizmo API + * + * \{ */ + +static void GIZMO_GT_move_3d(wmGizmoType *gzt) +{ + /* identifiers */ + gzt->idname = "GIZMO_GT_move_3d"; + + /* api callbacks */ + gzt->draw = gizmo_move_draw; + gzt->draw_select = gizmo_move_draw_select; + gzt->test_select = gizmo_move_test_select; + gzt->matrix_basis_get = gizmo_move_matrix_basis_get; + gzt->invoke = gizmo_move_invoke; + gzt->property_update = gizmo_move_property_update; + gzt->modal = gizmo_move_modal; + gzt->exit = gizmo_move_exit; + gzt->cursor_get = gizmo_move_cursor_get; + + gzt->struct_size = sizeof(MoveGizmo3D); + + /* rna */ + static EnumPropertyItem rna_enum_draw_style[] = { + {ED_GIZMO_MOVE_STYLE_RING_2D, "RING_2D", 0, "Ring", ""}, + {ED_GIZMO_MOVE_STYLE_CROSS_2D, "CROSS_2D", 0, "Ring", ""}, + {0, NULL, 0, NULL, NULL} + }; + static EnumPropertyItem rna_enum_draw_options[] = { + {ED_GIZMO_MOVE_DRAW_FLAG_FILL, "FILL", 0, "Filled", ""}, + {ED_GIZMO_MOVE_DRAW_FLAG_ALIGN_VIEW, "ALIGN_VIEW", 0, "Align View", ""}, + {0, NULL, 0, NULL, NULL} + }; + + RNA_def_enum(gzt->srna, "draw_style", rna_enum_draw_style, ED_GIZMO_MOVE_STYLE_RING_2D, "Draw Style", ""); + RNA_def_enum_flag(gzt->srna, "draw_options", rna_enum_draw_options, 0, "Draw Options", ""); + RNA_def_boolean(gzt->srna, "use_snap", false, "Use Snap", ""); + + WM_gizmotype_target_property_def(gzt, "offset", PROP_FLOAT, 3); +} + +void ED_gizmotypes_move_3d(void) +{ + WM_gizmotype_append(GIZMO_GT_move_3d); +} + +/** \} */ // Move Gizmo API diff --git a/source/blender/editors/gizmo_library/gizmo_types/primitive3d_gizmo.c b/source/blender/editors/gizmo_library/gizmo_types/primitive3d_gizmo.c new file mode 100644 index 00000000000..d5f1c23971c --- /dev/null +++ b/source/blender/editors/gizmo_library/gizmo_types/primitive3d_gizmo.c @@ -0,0 +1,191 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file primitive3d_gizmo.c + * \ingroup edgizmolib + * + * \name Primitive Gizmo + * + * 3D Gizmo + * + * \brief Gizmo with primitive drawing type (plane, cube, etc.). + * Currently only plane primitive supported without own handling, use with operator only. + */ + +#include "MEM_guardedalloc.h" + +#include "BLI_math.h" + +#include "DNA_view3d_types.h" + +#include "BKE_context.h" + +#include "BIF_gl.h" + +#include "GPU_immediate.h" +#include "GPU_matrix.h" +#include "GPU_select.h" +#include "GPU_state.h" + +#include "RNA_access.h" +#include "RNA_define.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "ED_gizmo_library.h" + +/* own includes */ +#include "../gizmo_library_intern.h" + +static float verts_plane[4][3] = { + {-1, -1, 0}, + { 1, -1, 0}, + { 1, 1, 0}, + {-1, 1, 0}, +}; + + +/* -------------------------------------------------------------------- */ + +static void gizmo_primitive_draw_geom( + const float col_inner[4], const float col_outer[4], const int draw_style) +{ + float (*verts)[3]; + uint vert_count = 0; + + if (draw_style == ED_GIZMO_PRIMITIVE_STYLE_PLANE) { + verts = verts_plane; + vert_count = ARRAY_SIZE(verts_plane); + } + + if (vert_count > 0) { + uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT); + immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); + wm_gizmo_vec_draw(col_inner, verts, vert_count, pos, GPU_PRIM_TRI_FAN); + wm_gizmo_vec_draw(col_outer, verts, vert_count, pos, GPU_PRIM_LINE_LOOP); + immUnbindProgram(); + } +} + +static void gizmo_primitive_draw_intern( + wmGizmo *gz, const bool UNUSED(select), + const bool highlight) +{ + float color_inner[4], color_outer[4]; + float matrix_final[4][4]; + const int draw_style = RNA_enum_get(gz->ptr, "draw_style"); + + gizmo_color_get(gz, highlight, color_outer); + copy_v4_v4(color_inner, color_outer); + color_inner[3] *= 0.5f; + + WM_gizmo_calc_matrix_final(gz, matrix_final); + + GPU_matrix_push(); + GPU_matrix_mul(matrix_final); + + GPU_blend(true); + gizmo_primitive_draw_geom(color_inner, color_outer, draw_style); + GPU_blend(false); + + GPU_matrix_pop(); + + if (gz->interaction_data) { + GizmoInteraction *inter = gz->interaction_data; + + copy_v4_fl(color_inner, 0.5f); + copy_v3_fl(color_outer, 0.5f); + color_outer[3] = 0.8f; + + GPU_matrix_push(); + GPU_matrix_mul(inter->init_matrix_final); + + GPU_blend(true); + gizmo_primitive_draw_geom(color_inner, color_outer, draw_style); + GPU_blend(false); + + GPU_matrix_pop(); + } +} + +static void gizmo_primitive_draw_select( + const bContext *UNUSED(C), wmGizmo *gz, + int select_id) +{ + GPU_select_load_id(select_id); + gizmo_primitive_draw_intern(gz, true, false); +} + +static void gizmo_primitive_draw(const bContext *UNUSED(C), wmGizmo *gz) +{ + gizmo_primitive_draw_intern( + gz, false, + (gz->state & WM_GIZMO_STATE_HIGHLIGHT)); +} + +static void gizmo_primitive_setup(wmGizmo *gz) +{ + gz->flag |= WM_GIZMO_DRAW_MODAL; +} + +static int gizmo_primitive_invoke( + bContext *UNUSED(C), wmGizmo *gz, const wmEvent *UNUSED(event)) +{ + GizmoInteraction *inter = MEM_callocN(sizeof(GizmoInteraction), __func__); + + WM_gizmo_calc_matrix_final(gz, inter->init_matrix_final); + + gz->interaction_data = inter; + + return OPERATOR_RUNNING_MODAL; +} + +/* -------------------------------------------------------------------- */ +/** \name Primitive Gizmo API + * + * \{ */ + +static void GIZMO_GT_primitive_3d(wmGizmoType *gzt) +{ + /* identifiers */ + gzt->idname = "GIZMO_GT_primitive_3d"; + + /* api callbacks */ + gzt->draw = gizmo_primitive_draw; + gzt->draw_select = gizmo_primitive_draw_select; + gzt->setup = gizmo_primitive_setup; + gzt->invoke = gizmo_primitive_invoke; + + gzt->struct_size = sizeof(wmGizmo); + + static EnumPropertyItem rna_enum_draw_style[] = { + {ED_GIZMO_PRIMITIVE_STYLE_PLANE, "PLANE", 0, "Plane", ""}, + {0, NULL, 0, NULL, NULL} + }; + RNA_def_enum(gzt->srna, "draw_style", rna_enum_draw_style, ED_GIZMO_PRIMITIVE_STYLE_PLANE, "Draw Style", ""); +} + +void ED_gizmotypes_primitive_3d(void) +{ + WM_gizmotype_append(GIZMO_GT_primitive_3d); +} + +/** \} */ diff --git a/source/blender/editors/gizmo_library/gizmo_types/value2d_gizmo.c b/source/blender/editors/gizmo_library/gizmo_types/value2d_gizmo.c new file mode 100644 index 00000000000..46a6b8f3a5f --- /dev/null +++ b/source/blender/editors/gizmo_library/gizmo_types/value2d_gizmo.c @@ -0,0 +1,198 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file value2d_gizmo.c + * \ingroup edgizmolib + * + * \name Value Gizmo + * + * \brief Gizmo that can be used to click and drag a value. + * + * Use this in cases where it may be useful to have a tool, + * but the tool doesn't relate to an on-screen handle. + * eg: smooth or randomize. + * + * Exactly how this maps X/Y axis, and draws - may change. + * The purpose here is to avoid having to write custom modal handlers for each operator. + * + * So we can use a single gizmo to make redoing an operator seem modal. + */ + +#include <float.h> +#include <math.h> + +#include "MEM_guardedalloc.h" + +#include "BKE_context.h" +#include "BLI_string.h" + +#include "ED_screen.h" +#include "ED_gizmo_library.h" + +#include "WM_types.h" +#include "WM_api.h" + +/* own includes */ +#include "../gizmo_geometry.h" +#include "../gizmo_library_intern.h" + +/* -------------------------------------------------------------------- */ +/** \name Value Gizmo + * + * \{ */ + +typedef struct ValueInteraction { + struct { + float mval[2]; + float prop_value; + } init; + struct { + float prop_value; + eWM_GizmoFlagTweak tweak_flag; + } prev; + float range[2]; +} ValueInteraction; + +static void gizmo_value_draw(const bContext *UNUSED(C), wmGizmo *UNUSED(gz)) +{ + /* pass */ +} + +static int gizmo_value_modal( + bContext *C, wmGizmo *gz, const wmEvent *event, + eWM_GizmoFlagTweak tweak_flag) +{ + ValueInteraction *inter = gz->interaction_data; + if ((event->type != MOUSEMOVE) && (inter->prev.tweak_flag == tweak_flag)) { + return OPERATOR_RUNNING_MODAL; + } + ARegion *ar = CTX_wm_region(C); + const float value_scale = 4.0f; /* Could be option. */ + const float value_range = inter->range[1] - inter->range[0]; + float value_delta = ( + inter->init.prop_value + + (((event->mval[0] - inter->init.mval[0]) / ar->winx) * value_range)) * value_scale; + + + if (tweak_flag & WM_GIZMO_TWEAK_SNAP) { + const double snap = 0.1; + value_delta = (float)roundf((double)value_delta / snap) * snap; + + } + if (tweak_flag & WM_GIZMO_TWEAK_PRECISE) { + value_delta *= 0.1f; + } + const float value_final = inter->init.prop_value + value_delta; + + if (value_final != inter->prev.prop_value) { + /* set the property for the operator and call its modal function */ + wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset"); + if (WM_gizmo_target_property_is_valid(gz_prop)) { + WM_gizmo_target_property_float_set(C, gz, gz_prop, value_final); + } + + { + ScrArea *sa = CTX_wm_area(C); + char str[64]; + SNPRINTF(str, "%.4f", value_final); + ED_area_status_text(sa, str); + } + } + + inter->prev.prop_value = value_final; + inter->prev.tweak_flag = tweak_flag; + + return OPERATOR_RUNNING_MODAL; +} + + +static int gizmo_value_invoke( + bContext *UNUSED(C), wmGizmo *gz, const wmEvent *event) +{ + ValueInteraction *inter = MEM_callocN(sizeof(ValueInteraction), __func__); + + inter->init.mval[0] = event->mval[0]; + inter->init.mval[1] = event->mval[1]; + inter->prev.prop_value = -FLT_MAX; + + wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset"); + if (WM_gizmo_target_property_is_valid(gz_prop)) { + inter->init.prop_value = WM_gizmo_target_property_float_get(gz, gz_prop); + if (!WM_gizmo_target_property_float_range_get(gz, gz_prop, inter->range)) { + inter->range[0] = 0.0f; + inter->range[1] = 1.0f; + } + } + + gz->interaction_data = inter; + + return OPERATOR_RUNNING_MODAL; +} + +static void gizmo_value_exit(bContext *C, wmGizmo *gz, const bool cancel) +{ + ScrArea *sa = CTX_wm_area(C); + ED_area_status_text(sa, NULL); + if (cancel) { + ValueInteraction *inter = gz->interaction_data; + wmGizmoProperty *gz_prop = WM_gizmo_target_property_find(gz, "offset"); + if (WM_gizmo_target_property_is_valid(gz_prop)) { + WM_gizmo_target_property_float_set(C, gz, gz_prop, inter->init.prop_value); + } + } +} + +static int gizmo_value_test_select( + bContext *UNUSED(C), wmGizmo *UNUSED(gz), const int UNUSED(mval[2])) +{ + return 0; +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Value Gizmo API + * + * \{ */ + +static void GIZMO_GT_value_2d(wmGizmoType *gzt) +{ + /* identifiers */ + gzt->idname = "GIZMO_GT_value_2d"; + + /* api callbacks */ + gzt->draw = gizmo_value_draw; + gzt->invoke = gizmo_value_invoke; + gzt->exit = gizmo_value_exit; + gzt->modal = gizmo_value_modal; + gzt->test_select = gizmo_value_test_select; + + gzt->struct_size = sizeof(wmGizmo); + + WM_gizmotype_target_property_def(gzt, "offset", PROP_FLOAT, 1); + /* Options: relative / absolute */ +} + +void ED_gizmotypes_value_2d(void) +{ + WM_gizmotype_append(GIZMO_GT_value_2d); +} + +/** \} */ |