diff options
Diffstat (limited to 'source/blender/editors/manipulator_library')
17 files changed, 5675 insertions, 0 deletions
diff --git a/source/blender/editors/manipulator_library/CMakeLists.txt b/source/blender/editors/manipulator_library/CMakeLists.txt new file mode 100644 index 00000000000..86e1bb3b6d7 --- /dev/null +++ b/source/blender/editors/manipulator_library/CMakeLists.txt @@ -0,0 +1,60 @@ +# ***** 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 + manipulator_draw_utils.c + manipulator_geometry.h + manipulator_library_intern.h + manipulator_library_presets.c + manipulator_library_utils.c + geometry/geom_arrow_manipulator.c + geometry/geom_cube_manipulator.c + geometry/geom_dial_manipulator.c + manipulator_types/arrow2d_manipulator.c + manipulator_types/arrow3d_manipulator.c + manipulator_types/button2d_manipulator.c + manipulator_types/cage2d_manipulator.c + manipulator_types/cage3d_manipulator.c + manipulator_types/dial3d_manipulator.c + manipulator_types/grab3d_manipulator.c + manipulator_types/primitive3d_manipulator.c +) + +add_definitions(${GL_DEFINITIONS}) + +blender_add_lib(bf_editor_manipulator_library "${SRC}" "${INC}" "${INC_SYS}") diff --git a/source/blender/editors/manipulator_library/geometry/geom_arrow_manipulator.c b/source/blender/editors/manipulator_library/geometry/geom_arrow_manipulator.c new file mode 100644 index 00000000000..34f7d73589c --- /dev/null +++ b/source/blender/editors/manipulator_library/geometry/geom_arrow_manipulator.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_manipulator.c + * \ingroup wm + */ + +#include "../manipulator_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, +}; + +ManipulatorGeomInfo wm_manipulator_geom_data_arrow = { + .nverts = 25, + .ntris = 46, + .verts = verts, + .normals = normals, + .indices = indices, +}; diff --git a/source/blender/editors/manipulator_library/geometry/geom_cube_manipulator.c b/source/blender/editors/manipulator_library/geometry/geom_cube_manipulator.c new file mode 100644 index 00000000000..cee8e1e22ee --- /dev/null +++ b/source/blender/editors/manipulator_library/geometry/geom_cube_manipulator.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_manipulator.c + * \ingroup wm + */ + +#include "../manipulator_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, +}; + +ManipulatorGeomInfo wm_manipulator_geom_data_cube = { + .nverts = 8, + .ntris = 12, + .verts = verts, + .normals = normals, + .indices = indices, +}; diff --git a/source/blender/editors/manipulator_library/geometry/geom_dial_manipulator.c b/source/blender/editors/manipulator_library/geometry/geom_dial_manipulator.c new file mode 100644 index 00000000000..811fc872a81 --- /dev/null +++ b/source/blender/editors/manipulator_library/geometry/geom_dial_manipulator.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_manipulator.c + * \ingroup wm + */ + +#include "../manipulator_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, +}; + +ManipulatorGeomInfo wm_manipulator_geom_data_dial = { + .nverts = 192, + .ntris = 384, + .verts = verts, + .normals = normals, + .indices = indices, +}; diff --git a/source/blender/editors/manipulator_library/manipulator_draw_utils.c b/source/blender/editors/manipulator_library/manipulator_draw_utils.c new file mode 100644 index 00000000000..430841311aa --- /dev/null +++ b/source/blender/editors/manipulator_library/manipulator_draw_utils.c @@ -0,0 +1,131 @@ +/* + * ***** 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 manipulator_draw_utils.c + * \ingroup wm + */ + +#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 "MEM_guardedalloc.h" + +#include "RNA_access.h" + +#include "WM_api.h" +#include "WM_types.h" + +/* only for own init/exit calls (wm_manipulatortype_init/wm_manipulatortype_free) */ +#include "wm.h" + +/* own includes */ +#include "manipulator_library_intern.h" + +/** + * Main draw call for ManipulatorGeomInfo data + */ +void wm_manipulator_geometryinfo_draw(const ManipulatorGeomInfo *info, const bool select, const float color[4]) +{ + /* TODO store the Batches inside the ManipulatorGeomInfo and updated it when geom changes + * So we don't need to re-created and discard it every time */ + + const bool use_lighting = true || (!select && ((U.manipulator_flag & USER_MANIPULATOR_SHADED) != 0)); + Gwn_VertBuf *vbo; + Gwn_IndexBuf *el; + Gwn_Batch *batch; + Gwn_IndexBufBuilder elb = {0}; + + Gwn_VertFormat format = {0}; + uint pos_id = GWN_vertformat_attr_add(&format, "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT); + uint nor_id; + + if (use_lighting) { + nor_id = GWN_vertformat_attr_add(&format, "nor", GWN_COMP_I16, 3, GWN_FETCH_INT_TO_FLOAT_UNIT); + } + + /* Elements */ + GWN_indexbuf_init(&elb, GWN_PRIM_TRIS, info->ntris, info->nverts); + for (int i = 0; i < info->ntris; ++i) { + const unsigned short *idx = &info->indices[i * 3]; + GWN_indexbuf_add_tri_verts(&elb, idx[0], idx[1], idx[2]); + } + el = GWN_indexbuf_build(&elb); + + vbo = GWN_vertbuf_create_with_format(&format); + GWN_vertbuf_data_alloc(vbo, info->nverts); + + GWN_vertbuf_attr_fill(vbo, pos_id, info->verts); + + if (use_lighting) { + /* Normals are expected to be smooth. */ + GWN_vertbuf_attr_fill(vbo, nor_id, info->normals); + } + + batch = GWN_batch_create_ex(GWN_PRIM_TRIS, vbo, el, GWN_BATCH_OWNS_VBO | GWN_BATCH_OWNS_INDEX); + GWN_batch_program_set_builtin(batch, GPU_SHADER_3D_UNIFORM_COLOR); + + GWN_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); + glEnable(GL_DEPTH_TEST); +#endif + + GWN_batch_draw(batch); + +#if 0 + glDisable(GL_DEPTH_TEST); + glDisable(GL_CULL_FACE); +#endif + + + GWN_batch_discard(batch); +} + +void wm_manipulator_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/manipulator_library/manipulator_geometry.h b/source/blender/editors/manipulator_library/manipulator_geometry.h new file mode 100644 index 00000000000..2083f9d4d31 --- /dev/null +++ b/source/blender/editors/manipulator_library/manipulator_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 manipulator_geometry.h + * \ingroup wm + * + * \name Manipulator Geometry + * + * \brief Prototypes for arrays defining the manipulator geometry. The actual definitions can be found in files usually + * called geom_xxx_manipulator.c + */ + + +#ifndef __MANIPULATOR_GEOMETRY_H__ +#define __MANIPULATOR_GEOMETRY_H__ + +typedef struct ManipulatorGeomInfo { + int nverts; + int ntris; + const float (*verts)[3]; + const float (*normals)[3]; + const unsigned short *indices; +} ManipulatorGeomInfo; + +/* arrow manipulator */ +extern ManipulatorGeomInfo wm_manipulator_geom_data_arrow; + +/* cube manipulator */ +extern ManipulatorGeomInfo wm_manipulator_geom_data_cube; + +/* dial manipulator */ +extern ManipulatorGeomInfo wm_manipulator_geom_data_dial; + +#endif /* __MANIPULATOR_GEOMETRY_H__ */ diff --git a/source/blender/editors/manipulator_library/manipulator_library_intern.h b/source/blender/editors/manipulator_library/manipulator_library_intern.h new file mode 100644 index 00000000000..01ca217fc0a --- /dev/null +++ b/source/blender/editors/manipulator_library/manipulator_library_intern.h @@ -0,0 +1,112 @@ +/* + * ***** 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 manipulator_library_intern.h + * \ingroup wm + */ + +#ifndef __MANIPULATOR_LIBRARY_INTERN_H__ +#define __MANIPULATOR_LIBRARY_INTERN_H__ + +/* distance around which manipulators respond to input (and get highlighted) */ +#define MANIPULATOR_HOTSPOT 14.0f + +/** + * Data for common interactions. Used in manipulator_library_utils.c functions. + */ +typedef struct ManipulatorCommonData { + int flag; + + float range_fac; /* factor for arrow min/max distance */ + float offset; + + /* property range for constrained manipulators */ + float range; + /* min/max value for constrained manipulators */ + float min, max; +} ManipulatorCommonData; + +typedef struct ManipulatorInteraction { + 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; +} ManipulatorInteraction; + +/* ManipulatorCommonData->flag */ +enum { + MANIPULATOR_CUSTOM_RANGE_SET = (1 << 0), +}; + + +float manipulator_offset_from_value( + ManipulatorCommonData *data, const float value, + const bool constrained, const bool inverted); +float manipulator_value_from_offset( + ManipulatorCommonData *data, ManipulatorInteraction *inter, const float offset, + const bool constrained, const bool inverted, const bool use_precision); + +void manipulator_property_data_update( + struct wmManipulator *mpr, ManipulatorCommonData *data, wmManipulatorProperty *mpr_prop, + const bool constrained, const bool inverted); + +void manipulator_property_value_reset( + bContext *C, const struct wmManipulator *mpr, ManipulatorInteraction *inter, wmManipulatorProperty *mpr_prop); + + +/* -------------------------------------------------------------------- */ + +void manipulator_color_get( + const struct wmManipulator *mpr, const bool highlight, + float r_color[4]); + +bool manipulator_window_project_2d( + bContext *C, const struct wmManipulator *mpr, const float mval[2], int axis, bool use_offset, + float r_co[2]); + +bool manipulator_window_project_3d( + bContext *C, const struct wmManipulator *mpr, const float mval[2], bool use_offset, + float r_co[3]); + +/* -------------------------------------------------------------------- */ +/* Manipulator drawing */ + +#include "manipulator_geometry.h" + +void wm_manipulator_geometryinfo_draw(const struct ManipulatorGeomInfo *info, const bool select, const float color[4]); +void wm_manipulator_vec_draw( + const float color[4], const float (*verts)[3], uint vert_count, + uint pos, uint primitive_type); + + +#endif /* __MANIPULATOR_LIBRARY_INTERN_H__ */ + diff --git a/source/blender/editors/manipulator_library/manipulator_library_presets.c b/source/blender/editors/manipulator_library/manipulator_library_presets.c new file mode 100644 index 00000000000..cccf484f29d --- /dev/null +++ b/source/blender/editors/manipulator_library/manipulator_library_presets.c @@ -0,0 +1,151 @@ +/* + * ***** 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/manipulator_library/manipulator_library_presets.c + * \ingroup wm + * + * \name Manipulator Lib Presets + * + * \brief Preset shapes that can be drawn from any manipulator 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_manipulator_library.h" /* own include */ +#include "manipulator_library_intern.h" /* own include */ + +/* TODO, this is to be used by RNA. might move to ED_manipulator_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_manipulator_draw_preset_geometry( + const struct wmManipulator *mpr, float mat[4][4], int select_id, + const ManipulatorGeomInfo *info) +{ + const bool is_select = (select_id != -1); + const bool is_highlight = is_select && (mpr->state & WM_MANIPULATOR_STATE_HIGHLIGHT) != 0; + + float color[4]; + manipulator_color_get(mpr, is_highlight, color); + + if (is_select) { + GPU_select_load_id(select_id); + } + + gpuPushMatrix(); + gpuMultMatrix(mat); + wm_manipulator_geometryinfo_draw(info, is_select, color); + gpuPopMatrix(); + + if (is_select) { + GPU_select_load_id(-1); + } +} + +void ED_manipulator_draw_preset_box( + const struct wmManipulator *mpr, float mat[4][4], int select_id) +{ + ed_manipulator_draw_preset_geometry(mpr, mat, select_id, &wm_manipulator_geom_data_cube); +} + +void ED_manipulator_draw_preset_arrow( + const struct wmManipulator *mpr, float mat[4][4], int axis, int select_id) +{ + float mat_rotate[4][4]; + single_axis_convert(OB_POSZ, mat, axis, mat_rotate); + ed_manipulator_draw_preset_geometry(mpr, mat_rotate, select_id, &wm_manipulator_geom_data_arrow); +} + +void ED_manipulator_draw_preset_circle( + const struct wmManipulator *mpr, float mat[4][4], int axis, int select_id) +{ + float mat_rotate[4][4]; + single_axis_convert(OB_POSZ, mat, axis, mat_rotate); + ed_manipulator_draw_preset_geometry(mpr, mat_rotate, select_id, &wm_manipulator_geom_data_dial); +} + +void ED_manipulator_draw_preset_facemap( + const bContext *C, const struct wmManipulator *mpr, struct Scene *scene, Object *ob, const int facemap, int select_id) +{ + const bool is_select = (select_id != -1); + const bool is_highlight = is_select && (mpr->state & WM_MANIPULATOR_STATE_HIGHLIGHT) != 0; + + float color[4]; + manipulator_color_get(mpr, is_highlight, color); + + if (is_select) { + GPU_select_load_id(select_id); + } + + gpuPushMatrix(); + gpuMultMatrix(ob->obmat); + ED_draw_object_facemap(CTX_data_depsgraph(C), scene, ob, color, facemap); + gpuPopMatrix(); + + if (is_select) { + GPU_select_load_id(-1); + } +} + diff --git a/source/blender/editors/manipulator_library/manipulator_library_utils.c b/source/blender/editors/manipulator_library/manipulator_library_utils.c new file mode 100644 index 00000000000..957f0abdd71 --- /dev/null +++ b/source/blender/editors/manipulator_library/manipulator_library_utils.c @@ -0,0 +1,256 @@ +/* + * ***** 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 manipulator_library_utils.c + * \ingroup wm + * + * \name Manipulator Library Utilities + * + * \brief This file contains functions for common behaviors of manipulators. + */ + +#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 "manipulator_library_intern.h" + +/* factor for precision tweaking */ +#define MANIPULATOR_PRECISION_FAC 0.05f + + +BLI_INLINE float manipulator_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 manipulator_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 manipulator_offset_from_value( + ManipulatorCommonData *data, const float value, const bool constrained, const bool inverted) +{ + if (constrained) + return manipulator_offset_from_value_constr(data->range_fac, data->min, data->range, value, inverted); + + return value; +} + +float manipulator_value_from_offset( + ManipulatorCommonData *data, ManipulatorInteraction *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 - MANIPULATOR_PRECISION_FAC); + float value; + + if (constrained) { + value = manipulator_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 & MANIPULATOR_CUSTOM_RANGE_SET) { + CLAMP(value, data->min, max); + } + + return value; +} + +void manipulator_property_data_update( + wmManipulator *mpr, ManipulatorCommonData *data, wmManipulatorProperty *mpr_prop, + const bool constrained, const bool inverted) +{ + if (mpr_prop->custom_func.value_get_fn != NULL) { + /* pass */ + } + else if (mpr_prop->prop != NULL) { + /* pass */ + } + else { + data->offset = 0.0f; + return; + } + + float value = WM_manipulator_target_property_value_get(mpr, mpr_prop); + + if (constrained) { + if ((data->flag & MANIPULATOR_CUSTOM_RANGE_SET) == 0) { + float range[2]; + if (WM_manipulator_target_property_range_get(mpr, mpr_prop, range)) { + data->range = range[1] - range[0]; + data->min = range[0]; + } + else { + BLI_assert(0); + } + } + data->offset = manipulator_offset_from_value_constr(data->range_fac, data->min, data->range, value, inverted); + } + else { + data->offset = value; + } +} + +void manipulator_property_value_reset( + bContext *C, const wmManipulator *mpr, ManipulatorInteraction *inter, + wmManipulatorProperty *mpr_prop) +{ + WM_manipulator_target_property_value_set(C, mpr, mpr_prop, inter->init_value); +} + +/* -------------------------------------------------------------------- */ + +void manipulator_color_get( + const wmManipulator *mpr, const bool highlight, + float r_col[4]) +{ + if (highlight && !(mpr->flag & WM_MANIPULATOR_DRAW_HOVER)) { + copy_v4_v4(r_col, mpr->color_hi); + } + else { + copy_v4_v4(r_col, mpr->color); + } +} + +/* -------------------------------------------------------------------- */ + +/** + * Takes mouse coordinates and returns them in relation to the manipulator. + * Both 2D & 3D supported, use so we can use 2D manipulators in the 3D view. + */ +bool manipulator_window_project_2d( + bContext *C, const struct wmManipulator *mpr, const float mval[2], int axis, bool use_offset, + float r_co[2]) +{ + float mat[4][4]; + { + float mat_identity[4][4]; + struct WM_ManipulatorMatrixParams params = {NULL}; + if (use_offset == false) { + unit_m4(mat_identity); + params.matrix_offset = mat_identity; + } + WM_manipulator_calc_matrix_final_params(mpr, ¶ms, mat); + } + + /* rotate mouse in relation to the center and relocate it */ + if (mpr->parent_mgroup->type->flag & WM_MANIPULATORGROUPTYPE_3D) { + /* For 3d views, transform 2D mouse pos onto plane. */ + View3D *v3d = CTX_wm_view3d(C); + ARegion *ar = CTX_wm_region(C); + + float plane[4]; + + plane_from_point_normal_v3(plane, mat[3], mat[2]); + + float ray_origin[3], ray_direction[3]; + + if (ED_view3d_win_to_ray(CTX_data_depsgraph(C), ar, v3d, mval, ray_origin, ray_direction, false)) { + float lambda; + if (isect_ray_plane_v3(ray_origin, ray_direction, plane, &lambda, true)) { + float co[3]; + madd_v3_v3v3fl(co, ray_origin, ray_direction, lambda); + 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_mat3_m4_v3(imat, co); + copy_v2_v2(r_co, co); + return true; + } +} + +bool manipulator_window_project_3d( + bContext *C, const struct wmManipulator *mpr, const float mval[2], bool use_offset, + float r_co[3]) +{ + float mat[4][4]; + { + float mat_identity[4][4]; + struct WM_ManipulatorMatrixParams params = {NULL}; + if (use_offset == false) { + unit_m4(mat_identity); + params.matrix_offset = mat_identity; + } + WM_manipulator_calc_matrix_final_params(mpr, ¶ms, mat); + } + + if (mpr->parent_mgroup->type->flag & WM_MANIPULATORGROUPTYPE_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 manipulator 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/manipulator_library/manipulator_types/arrow2d_manipulator.c b/source/blender/editors/manipulator_library/manipulator_types/arrow2d_manipulator.c new file mode 100644 index 00000000000..749e92e25fb --- /dev/null +++ b/source/blender/editors/manipulator_library/manipulator_types/arrow2d_manipulator.c @@ -0,0 +1,224 @@ +/* + * ***** 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_manipulator.c + * \ingroup wm + * + * \name 2D Arrow Manipulator + * + * \brief Simple arrow manipulator 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 "MEM_guardedalloc.h" + +#include "RNA_access.h" +#include "RNA_define.h" + +#include "WM_types.h" + +#include "ED_screen.h" +#include "ED_manipulator_library.h" + +/* own includes */ +#include "WM_api.h" + +#include "../manipulator_library_intern.h" + +static void arrow2d_draw_geom(wmManipulator *mpr, 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(mpr->ptr, "length") - size_length; + const float arrow_angle = RNA_float_get(mpr->ptr, "angle"); + + uint pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT); + + gpuPushMatrix(); + gpuMultMatrix(matrix); + gpuRotate2D(RAD2DEGF(arrow_angle)); + + immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); + + immUniformColor4fv(color); + + immBegin(GWN_PRIM_LINES, 2); + immVertex2f(pos, 0.0f, 0.0f); + immVertex2f(pos, 0.0f, arrow_length); + immEnd(); + + immBegin(GWN_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(); + + gpuPopMatrix(); +} + +static void manipulator_arrow2d_draw(const bContext *UNUSED(C), wmManipulator *mpr) +{ + float color[4]; + + float matrix_final[4][4]; + + manipulator_color_get(mpr, mpr->state & WM_MANIPULATOR_STATE_HIGHLIGHT, color); + + glLineWidth(mpr->line_width); + + WM_manipulator_calc_matrix_final(mpr, matrix_final); + + glEnable(GL_BLEND); + arrow2d_draw_geom(mpr, matrix_final, color); + glDisable(GL_BLEND); + + if (mpr->interaction_data) { + ManipulatorInteraction *inter = mpr->interaction_data; + + glEnable(GL_BLEND); + arrow2d_draw_geom(mpr, inter->init_matrix_final, (const float[4]){0.5f, 0.5f, 0.5f, 0.5f}); + glDisable(GL_BLEND); + } +} + +static void manipulator_arrow2d_setup(wmManipulator *mpr) +{ + mpr->flag |= WM_MANIPULATOR_DRAW_MODAL; +} + +static int manipulator_arrow2d_invoke( + bContext *UNUSED(C), wmManipulator *mpr, const wmEvent *UNUSED(event)) +{ + ManipulatorInteraction *inter = MEM_callocN(sizeof(ManipulatorInteraction), __func__); + + copy_m4_m4(inter->init_matrix_basis, mpr->matrix_basis); + WM_manipulator_calc_matrix_final(mpr, inter->init_matrix_final); + + mpr->interaction_data = inter; + + return OPERATOR_RUNNING_MODAL; +} + +static int manipulator_arrow2d_test_select( + bContext *UNUSED(C), wmManipulator *mpr, const wmEvent *event) +{ + const float mval[2] = {event->mval[0], event->mval[1]}; + const float arrow_length = RNA_float_get(mpr->ptr, "length"); + const float arrow_angle = RNA_float_get(mpr->ptr, "angle"); + const float line_len = arrow_length * mpr->scale_final; + float mval_local[2]; + + copy_v2_v2(mval_local, mval); + sub_v2_v2(mval_local, mpr->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, MANIPULATOR_HOTSPOT + mpr->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] + MANIPULATOR_HOTSPOT * ((line[1][0] - line[0][0]) / line_len); + line_ext[1][1] = line[1][1] + MANIPULATOR_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 Manipulator API + * + * \{ */ + +static void MANIPULATOR_WT_arrow_2d(wmManipulatorType *wt) +{ + /* identifiers */ + wt->idname = "MANIPULATOR_WT_arrow_2d"; + + /* api callbacks */ + wt->draw = manipulator_arrow2d_draw; + wt->setup = manipulator_arrow2d_setup; + wt->invoke = manipulator_arrow2d_invoke; + wt->test_select = manipulator_arrow2d_test_select; + + wt->struct_size = sizeof(wmManipulator); + + /* rna */ + RNA_def_float(wt->srna, "length", 1.0f, 0.0f, FLT_MAX, "Arrow Line Length", "", 0.0f, FLT_MAX); + RNA_def_float_rotation( + wt->srna, "angle", 0, NULL, DEG2RADF(-360.0f), DEG2RADF(360.0f), + "Roll", "", DEG2RADF(-360.0f), DEG2RADF(360.0f)); +} + +void ED_manipulatortypes_arrow_2d(void) +{ + WM_manipulatortype_append(MANIPULATOR_WT_arrow_2d); +} + +/** \} */ diff --git a/source/blender/editors/manipulator_library/manipulator_types/arrow3d_manipulator.c b/source/blender/editors/manipulator_library/manipulator_types/arrow3d_manipulator.c new file mode 100644 index 00000000000..8516b9d8244 --- /dev/null +++ b/source/blender/editors/manipulator_library/manipulator_types/arrow3d_manipulator.c @@ -0,0 +1,497 @@ +/* + * ***** 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_manipulator.c + * \ingroup wm + * + * \name Arrow Manipulator + * + * 3D Manipulator + * + * \brief Simple arrow manipulator 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 manipulator 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 "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_manipulator_library.h" + +/* own includes */ +#include "../manipulator_geometry.h" +#include "../manipulator_library_intern.h" + +/* to use custom arrows exported to geom_arrow_manipulator.c */ +//#define USE_MANIPULATOR_CUSTOM_ARROWS + +typedef struct ArrowManipulator3D { + wmManipulator manipulator; + ManipulatorCommonData data; +} ArrowManipulator3D; + + +/* -------------------------------------------------------------------- */ + +static void manipulator_arrow_matrix_basis_get(const wmManipulator *mpr, float r_matrix[4][4]) +{ + ArrowManipulator3D *arrow = (ArrowManipulator3D *)mpr; + + copy_m4_m4(r_matrix, arrow->manipulator.matrix_basis); + madd_v3_v3fl(r_matrix[3], arrow->manipulator.matrix_basis[2], arrow->data.offset); +} + +static void arrow_draw_geom(const ArrowManipulator3D *arrow, const bool select, const float color[4]) +{ + uint pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT); + bool unbind_shader = true; + const int draw_style = RNA_enum_get(arrow->manipulator.ptr, "draw_style"); + const int draw_options = RNA_enum_get(arrow->manipulator.ptr, "draw_options"); + + immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); + + if (draw_style == ED_MANIPULATOR_ARROW_STYLE_CROSS) { + immUniformColor4fv(color); + + immBegin(GWN_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_MANIPULATOR_ARROW_STYLE_CONE) { + float aspect[2]; + RNA_float_get_array(arrow->manipulator.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}, + }; + + glLineWidth(arrow->manipulator.line_width); + wm_manipulator_vec_draw(color, vec, ARRAY_SIZE(vec), pos, GWN_PRIM_LINE_LOOP); + } + else { +#ifdef USE_MANIPULATOR_CUSTOM_ARROWS + wm_manipulator_geometryinfo_draw(&wm_manipulator_geom_data_arrow, select, color); +#else + const float arrow_length = RNA_float_get(arrow->manipulator.ptr, "length"); + + const float vec[2][3] = { + {0.0f, 0.0f, 0.0f}, + {0.0f, 0.0f, arrow_length}, + }; + + if (draw_options & ED_MANIPULATOR_ARROW_DRAW_FLAG_STEM) { + glLineWidth(arrow->manipulator.line_width); + wm_manipulator_vec_draw(color, vec, ARRAY_SIZE(vec), pos, GWN_PRIM_LINE_STRIP); + } + else { + immUniformColor4fv(color); + } + + /* *** draw arrow head *** */ + + gpuPushMatrix(); + + if (draw_style == ED_MANIPULATOR_ARROW_STYLE_BOX) { + const float size = 0.05f; + + /* translate to line end with some extra offset so box starts exactly where line ends */ + gpuTranslate3f(0.0f, 0.0f, arrow_length + size); + /* scale down to box size */ + gpuScale3f(size, size, size); + + /* draw cube */ + immUnbindProgram(); + unbind_shader = false; + wm_manipulator_geometryinfo_draw(&wm_manipulator_geom_data_cube, select, color); + } + else { + BLI_assert(draw_style == ED_MANIPULATOR_ARROW_STYLE_NORMAL); + + const float len = 0.25f; + const float width = 0.06f; + const bool use_lighting = (!select && ((U.manipulator_flag & USER_MANIPULATOR_SHADED) != 0)); + + /* translate to line end */ + gpuTranslate3f(0.0f, 0.0f, arrow_length); + + if (use_lighting) { + immUnbindProgram(); + immBindBuiltinProgram(GPU_SHADER_3D_SMOOTH_COLOR); + } + + imm_draw_circle_fill_3d(pos, 0.0, 0.0, width, 8); + imm_draw_cylinder_fill_3d(pos, width, 0.0, len, 8, 1); + } + + gpuPopMatrix(); +#endif /* USE_MANIPULATOR_CUSTOM_ARROWS */ + } + + if (unbind_shader) { + immUnbindProgram(); + } +} + +static void arrow_draw_intern(ArrowManipulator3D *arrow, const bool select, const bool highlight) +{ + wmManipulator *mpr = &arrow->manipulator; + float color[4]; + float matrix_final[4][4]; + + manipulator_color_get(mpr, highlight, color); + + WM_manipulator_calc_matrix_final(mpr, matrix_final); + + gpuPushMatrix(); + gpuMultMatrix(matrix_final); + glEnable(GL_BLEND); + arrow_draw_geom(arrow, select, color); + glDisable(GL_BLEND); + + gpuPopMatrix(); + + if (mpr->interaction_data) { + ManipulatorInteraction *inter = mpr->interaction_data; + + gpuPushMatrix(); + gpuMultMatrix(inter->init_matrix_final); + + + glEnable(GL_BLEND); + arrow_draw_geom(arrow, select, (const float[4]){0.5f, 0.5f, 0.5f, 0.5f}); + glDisable(GL_BLEND); + + gpuPopMatrix(); + } +} + +static void manipulator_arrow_draw_select( + const bContext *UNUSED(C), wmManipulator *mpr, + int select_id) +{ + GPU_select_load_id(select_id); + arrow_draw_intern((ArrowManipulator3D *)mpr, true, false); +} + +static void manipulator_arrow_draw(const bContext *UNUSED(C), wmManipulator *mpr) +{ + arrow_draw_intern((ArrowManipulator3D *)mpr, false, (mpr->state & WM_MANIPULATOR_STATE_HIGHLIGHT) != 0); +} + +/** + * Calculate arrow offset independent from prop min value, + * meaning the range will not be offset by min value first. + */ +static int manipulator_arrow_modal( + bContext *C, wmManipulator *mpr, const wmEvent *event, + eWM_ManipulatorTweak tweak_flag) +{ + ArrowManipulator3D *arrow = (ArrowManipulator3D *)mpr; + ManipulatorInteraction *inter = mpr->interaction_data; + View3D *v3d = CTX_wm_view3d(C); + 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->manipulator.matrix_basis[2]); + + int ok = 0; + + for (int j = 0; j < 2; j++) { + if (ED_view3d_win_to_ray( + CTX_data_depsgraph(C), + ar, v3d, proj[j].mval, + proj[j].ray_origin, proj[j].ray_direction, false)) + { + /* Force Y axis if we're view aligned */ + if (j == 0) { + if (RAD2DEGF(acosf(dot_v3v3(proj[j].ray_direction, arrow->manipulator.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; + + ManipulatorCommonData *data = &arrow->data; + const float ofs_new = facdir * len_v3(offset); + + wmManipulatorProperty *mpr_prop = WM_manipulator_target_property_find(mpr, "offset"); + + /* set the property for the operator and call its modal function */ + if (WM_manipulator_target_property_is_valid(mpr_prop)) { + const int transform_flag = RNA_enum_get(arrow->manipulator.ptr, "transform"); + const bool constrained = (transform_flag & ED_MANIPULATOR_ARROW_XFORM_FLAG_CONSTRAINED) != 0; + const bool inverted = (transform_flag & ED_MANIPULATOR_ARROW_XFORM_FLAG_INVERTED) != 0; + const bool use_precision = (tweak_flag & WM_MANIPULATOR_TWEAK_PRECISE) != 0; + float value = manipulator_value_from_offset(data, inter, ofs_new, constrained, inverted, use_precision); + + WM_manipulator_target_property_value_set(C, mpr, mpr_prop, value); + /* get clamped value */ + value = WM_manipulator_target_property_value_get(mpr, mpr_prop); + + data->offset = manipulator_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 manipulator_arrow_setup(wmManipulator *mpr) +{ + ArrowManipulator3D *arrow = (ArrowManipulator3D *)mpr; + + arrow->manipulator.flag |= WM_MANIPULATOR_DRAW_MODAL; + + arrow->data.range_fac = 1.0f; +} + +static int manipulator_arrow_invoke( + bContext *UNUSED(C), wmManipulator *mpr, const wmEvent *event) +{ + ArrowManipulator3D *arrow = (ArrowManipulator3D *)mpr; + ManipulatorInteraction *inter = MEM_callocN(sizeof(ManipulatorInteraction), __func__); + wmManipulatorProperty *mpr_prop = WM_manipulator_target_property_find(mpr, "offset"); + + /* Some manipulators don't use properties. */ + if (WM_manipulator_target_property_is_valid(mpr_prop)) { + inter->init_value = WM_manipulator_target_property_value_get(mpr, mpr_prop); + } + + inter->init_offset = arrow->data.offset; + + inter->init_mval[0] = event->mval[0]; + inter->init_mval[1] = event->mval[1]; + + manipulator_arrow_matrix_basis_get(mpr, inter->init_matrix_basis); + WM_manipulator_calc_matrix_final(mpr, inter->init_matrix_final); + + mpr->interaction_data = inter; + + return OPERATOR_RUNNING_MODAL; +} + +static void manipulator_arrow_property_update(wmManipulator *mpr, wmManipulatorProperty *mpr_prop) +{ + ArrowManipulator3D *arrow = (ArrowManipulator3D *)mpr; + const int transform_flag = RNA_enum_get(arrow->manipulator.ptr, "transform"); + const bool constrained = (transform_flag & ED_MANIPULATOR_ARROW_XFORM_FLAG_CONSTRAINED) != 0; + const bool inverted = (transform_flag & ED_MANIPULATOR_ARROW_XFORM_FLAG_INVERTED) != 0; + manipulator_property_data_update(mpr, &arrow->data, mpr_prop, constrained, inverted); +} + +static void manipulator_arrow_exit(bContext *C, wmManipulator *mpr, const bool cancel) +{ + ArrowManipulator3D *arrow = (ArrowManipulator3D *)mpr; + ManipulatorCommonData *data = &arrow->data; + wmManipulatorProperty *mpr_prop = WM_manipulator_target_property_find(mpr, "offset"); + const bool is_prop_valid = WM_manipulator_target_property_is_valid(mpr_prop); + + if (!cancel) { + /* Assign incase applying the opetration needs an updated offset + * editmesh bisect needs this. */ + if (is_prop_valid) { + data->offset = WM_manipulator_target_property_value_get(mpr, mpr_prop); + } + return; + } + + ManipulatorInteraction *inter = mpr->interaction_data; + if (is_prop_valid) { + manipulator_property_value_reset(C, mpr, inter, mpr_prop); + } + data->offset = inter->init_offset; +} + + +/* -------------------------------------------------------------------- */ +/** \name Arrow Manipulator API + * + * \{ */ + +/** + * Define a custom property UI range + * + * \note Needs to be called before WM_manipulator_target_property_def_rna! + */ +void ED_manipulator_arrow3d_set_ui_range(wmManipulator *mpr, const float min, const float max) +{ + ArrowManipulator3D *arrow = (ArrowManipulator3D *)mpr; + + BLI_assert(min < max); + BLI_assert(!(WM_manipulator_target_property_is_valid(WM_manipulator_target_property_find(mpr, "offset")) && + "Make sure this function is called before WM_manipulator_target_property_def_rna")); + + arrow->data.range = max - min; + arrow->data.min = min; + arrow->data.flag |= MANIPULATOR_CUSTOM_RANGE_SET; +} + +/** + * Define a custom factor for arrow min/max distance + * + * \note Needs to be called before WM_manipulator_target_property_def_rna! + */ +void ED_manipulator_arrow3d_set_range_fac(wmManipulator *mpr, const float range_fac) +{ + ArrowManipulator3D *arrow = (ArrowManipulator3D *)mpr; + BLI_assert(!(WM_manipulator_target_property_is_valid(WM_manipulator_target_property_find(mpr, "offset")) && + "Make sure this function is called before WM_manipulator_target_property_def_rna")); + + arrow->data.range_fac = range_fac; +} + +static void MANIPULATOR_WT_arrow_3d(wmManipulatorType *wt) +{ + /* identifiers */ + wt->idname = "MANIPULATOR_WT_arrow_3d"; + + /* api callbacks */ + wt->draw = manipulator_arrow_draw; + wt->draw_select = manipulator_arrow_draw_select; + wt->matrix_basis_get = manipulator_arrow_matrix_basis_get; + wt->modal = manipulator_arrow_modal; + wt->setup = manipulator_arrow_setup; + wt->invoke = manipulator_arrow_invoke; + wt->property_update = manipulator_arrow_property_update; + wt->exit = manipulator_arrow_exit; + + wt->struct_size = sizeof(ArrowManipulator3D); + + /* rna */ + static EnumPropertyItem rna_enum_draw_style_items[] = { + {ED_MANIPULATOR_ARROW_STYLE_NORMAL, "NORMAL", 0, "Normal", ""}, + {ED_MANIPULATOR_ARROW_STYLE_CROSS, "CROSS", 0, "Cross", ""}, + {ED_MANIPULATOR_ARROW_STYLE_BOX, "BOX", 0, "Box", ""}, + {ED_MANIPULATOR_ARROW_STYLE_CONE, "CONE", 0, "Cone", ""}, + {0, NULL, 0, NULL, NULL} + }; + static EnumPropertyItem rna_enum_draw_options_items[] = { + {ED_MANIPULATOR_ARROW_DRAW_FLAG_STEM, "STEM", 0, "Stem", ""}, + {0, NULL, 0, NULL, NULL} + }; + static EnumPropertyItem rna_enum_transform_items[] = { + {ED_MANIPULATOR_ARROW_XFORM_FLAG_INVERTED, "INVERT", 0, "Inverted", ""}, + {ED_MANIPULATOR_ARROW_XFORM_FLAG_CONSTRAINED, "CONSTRAIN", 0, "Constrained", ""}, + {0, NULL, 0, NULL, NULL} + }; + + RNA_def_enum( + wt->srna, "draw_style", rna_enum_draw_style_items, + ED_MANIPULATOR_ARROW_STYLE_NORMAL, + "Draw Style", ""); + RNA_def_enum_flag( + wt->srna, "draw_options", rna_enum_draw_options_items, + ED_MANIPULATOR_ARROW_DRAW_FLAG_STEM, + "Draw Options", ""); + RNA_def_enum_flag( + wt->srna, "transform", rna_enum_transform_items, + 0, + "Transform", ""); + + RNA_def_float(wt->srna, "length", 1.0f, 0.0f, FLT_MAX, "Arrow Line Length", "", 0.0f, FLT_MAX); + RNA_def_float_vector(wt->srna, "aspect", 2, NULL, 0, FLT_MAX, "Aspect", "Cone/box style only", 0.0f, FLT_MAX); + + WM_manipulatortype_target_property_def(wt, "offset", PROP_FLOAT, 1); +} + +void ED_manipulatortypes_arrow_3d(void) +{ + WM_manipulatortype_append(MANIPULATOR_WT_arrow_3d); +} + +/** \} */ diff --git a/source/blender/editors/manipulator_library/manipulator_types/button2d_manipulator.c b/source/blender/editors/manipulator_library/manipulator_types/button2d_manipulator.c new file mode 100644 index 00000000000..86c3b4a09de --- /dev/null +++ b/source/blender/editors/manipulator_library/manipulator_types/button2d_manipulator.c @@ -0,0 +1,321 @@ +/* + * ***** 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_manipulator.c + * \ingroup wm + * + * \name Button Manipulator + * + * 2D Manipulator, also works in 3D views. + * + * \brief Single click button action for use in manipulator 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 "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_manipulator_library.h" + +#include "UI_interface.h" +#include "UI_interface_icons.h" +#include "UI_resources.h" + +/* own includes */ +#include "../manipulator_geometry.h" +#include "../manipulator_library_intern.h" + +typedef struct ButtonManipulator2D { + wmManipulator manipulator; + bool is_init; + /* Use an icon or shape */ + int icon; + Gwn_Batch *shape_batch[2]; +} ButtonManipulator2D; + +#define CIRCLE_RESOLUTION 32 + +/* -------------------------------------------------------------------- */ + +static void button2d_geom_draw_backdrop( + const wmManipulator *mpr, const float color[4], const bool select) +{ + glLineWidth(mpr->line_width); + + Gwn_VertFormat *format = immVertexFormat(); + uint pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_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, wmManipulator *mpr, + const bool select, const bool highlight) +{ + ButtonManipulator2D *button = (ButtonManipulator2D *)mpr; + + const int draw_options = RNA_enum_get(mpr->ptr, "draw_options"); + if (button->is_init == false) { + button->is_init = true; + PropertyRNA *prop = RNA_struct_find_property(mpr->ptr, "icon"); + if (RNA_property_is_set(mpr->ptr, prop)) { + button->icon = RNA_property_enum_get(mpr->ptr, prop); + } + else { + prop = RNA_struct_find_property(mpr->ptr, "shape"); + const uint polys_len = RNA_property_string_length(mpr->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(mpr->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]; + + manipulator_color_get(mpr, highlight, color); + WM_manipulator_calc_matrix_final(mpr, matrix_final); + + + bool is_3d = (mpr->parent_mgroup->type->flag & WM_MANIPULATORGROUPTYPE_3D) != 0; + + + if (draw_options & ED_MANIPULATOR_BUTTON_SHOW_HELPLINE) { + float matrix_final_no_offset[4][4]; + WM_manipulator_calc_matrix_final_no_offset(mpr, matrix_final_no_offset); + uint pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT); + immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); + immUniformColor4fv(color); + glLineWidth(mpr->line_width); + immUniformColor4fv(color); + immBegin(GWN_PRIM_LINE_STRIP, 2); + immVertex3fv(pos, matrix_final[3]); + immVertex3fv(pos, matrix_final_no_offset[3]); + immEnd(); + immUnbindProgram(); + } + + bool need_to_pop = true; + gpuPushMatrix(); + gpuMultMatrix(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); + gpuMultMatrix(matrix_align); + } + + if (select) { + BLI_assert(is_3d); + button2d_geom_draw_backdrop(mpr, color, select); + } + else { + + glEnable(GL_BLEND); + if (button->shape_batch[0] != NULL) { + glEnable(GL_LINE_SMOOTH); + glDisable(GL_POLYGON_SMOOTH); + glLineWidth(1.0f); + for (uint i = 0; i < ARRAY_SIZE(button->shape_batch) && button->shape_batch[i]; i++) { + /* Invert line color for wire. */ + GWN_batch_program_set_builtin(button->shape_batch[i], GPU_SHADER_2D_UNIFORM_COLOR); + GWN_batch_uniform_4f(button->shape_batch[i], "color", UNPACK4(color)); + GWN_batch_draw(button->shape_batch[i]); + + if (draw_options & ED_MANIPULATOR_BUTTON_SHOW_OUTLINE) { + color[0] = 1.0f - color[0]; + color[1] = 1.0f - color[1]; + color[2] = 1.0f - color[2]; + } + } + glDisable(GL_LINE_SMOOTH); + glEnable(GL_POLYGON_SMOOTH); + } + else if (button->icon != ICON_NONE) { + button2d_geom_draw_backdrop(mpr, color, select); + float size[2]; + if (is_3d) { + const float fac = 2.0f; + gpuTranslate2f(-(fac / 2), -(fac / 2)); + gpuScale2f(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] = mpr->matrix_basis[3][0] - (ICON_DEFAULT_WIDTH / 2.0) * UI_DPI_FAC; + size[1] = mpr->matrix_basis[3][1] - (ICON_DEFAULT_HEIGHT / 2.0) * UI_DPI_FAC; + gpuPopMatrix(); + need_to_pop = false; + } + UI_icon_draw(size[0], size[1], button->icon); + } + glDisable(GL_BLEND); + } + + if (need_to_pop) { + gpuPopMatrix(); + } +} + +static void manipulator_button2d_draw_select(const bContext *C, wmManipulator *mpr, int select_id) +{ + GPU_select_load_id(select_id); + button2d_draw_intern(C, mpr, true, false); +} + +static void manipulator_button2d_draw(const bContext *C, wmManipulator *mpr) +{ + const bool is_highlight = (mpr->state & WM_MANIPULATOR_STATE_HIGHLIGHT) != 0; + + glEnable(GL_BLEND); + button2d_draw_intern(C, mpr, false, is_highlight); + glDisable(GL_BLEND); +} + +static int manipulator_button2d_test_select( + bContext *C, wmManipulator *mpr, const wmEvent *event) +{ + float point_local[2]; + + if (0) { + /* correct, but unnecessarily slow. */ + if (manipulator_window_project_2d( + C, mpr, (const float[2]){UNPACK2(event->mval)}, 2, true, point_local) == false) + { + return -1; + } + } + else { + copy_v2_v2(point_local, (float[2]){UNPACK2(event->mval)}); + sub_v2_v2(point_local, mpr->matrix_basis[3]); + mul_v2_fl(point_local, 1.0f / (mpr->scale_basis * UI_DPI_FAC)); + } + /* The 'mpr->scale_final' is already applied when projecting. */ + if (len_squared_v2(point_local) < 1.0f) { + return 0; + } + + return -1; +} + +static int manipulator_button2d_cursor_get(wmManipulator *mpr) +{ + if (RNA_boolean_get(mpr->ptr, "show_drag")) { + return BC_NSEW_SCROLLCURSOR; + } + return CURSOR_STD; +} + +static void manipulator_button2d_free(wmManipulator *mpr) +{ + ButtonManipulator2D *shape = (ButtonManipulator2D *)mpr; + + for (uint i = 0; i < ARRAY_SIZE(shape->shape_batch); i++) { + GWN_BATCH_DISCARD_SAFE(shape->shape_batch[i]); + } +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Button Manipulator API + * + * \{ */ + +static void MANIPULATOR_WT_button_2d(wmManipulatorType *wt) +{ + /* identifiers */ + wt->idname = "MANIPULATOR_WT_button_2d"; + + /* api callbacks */ + wt->draw = manipulator_button2d_draw; + wt->draw_select = manipulator_button2d_draw_select; + wt->test_select = manipulator_button2d_test_select; + wt->cursor_get = manipulator_button2d_cursor_get; + wt->free = manipulator_button2d_free; + + wt->struct_size = sizeof(ButtonManipulator2D); + + /* rna */ + static EnumPropertyItem rna_enum_draw_options[] = { + {ED_MANIPULATOR_BUTTON_SHOW_OUTLINE, "OUTLINE", 0, "Outline", ""}, + {ED_MANIPULATOR_BUTTON_SHOW_HELPLINE, "HELPLINE", 0, "Help Line", ""}, + {0, NULL, 0, NULL, NULL} + }; + PropertyRNA *prop; + + RNA_def_enum_flag(wt->srna, "draw_options", rna_enum_draw_options, 0, "Draw Options", ""); + + prop = RNA_def_property(wt->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(wt->srna, "shape", PROP_STRING, PROP_BYTESTRING); + + /* Currently only used for cursor display. */ + RNA_def_boolean(wt->srna, "show_drag", true, "Show Drag", ""); +} + +void ED_manipulatortypes_button_2d(void) +{ + WM_manipulatortype_append(MANIPULATOR_WT_button_2d); +} + +/** \} */ // Button Manipulator API diff --git a/source/blender/editors/manipulator_library/manipulator_types/cage2d_manipulator.c b/source/blender/editors/manipulator_library/manipulator_types/cage2d_manipulator.c new file mode 100644 index 00000000000..fe748f33d35 --- /dev/null +++ b/source/blender/editors/manipulator_library/manipulator_types/cage2d_manipulator.c @@ -0,0 +1,1099 @@ +/* + * ***** 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_manipulator.c + * \ingroup wm + * + * \name Cage Manipulator + * + * 2D Manipulator + * + * \brief Rectangular manipulator acting as a 'cage' around its content. + * Interacting scales or translates the manipulator. + */ + +#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 "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_manipulator_library.h" + +/* own includes */ +#include "../manipulator_library_intern.h" + +#define MANIPULATOR_RESIZER_SIZE 10.0f +#define MANIPULATOR_MARGIN_OFFSET_SCALE 1.5f + +static void manipulator_calc_rect_view_scale( + const wmManipulator *mpr, 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_manipulator_calc_matrix_final_no_offset(mpr, matrix_final_no_offset); + mul_v3_mat3_m4v3(x_axis, matrix_final_no_offset, mpr->matrix_offset[0]); + mul_v3_mat3_m4v3(y_axis, matrix_final_no_offset, mpr->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 manipulator_calc_rect_view_margin( + const wmManipulator *mpr, const float dims[2], float margin[2]) +{ + float handle_size; + if (mpr->parent_mgroup->type->flag & WM_MANIPULATORGROUPTYPE_3D) { + handle_size = 0.15f; + } + else { + handle_size = MANIPULATOR_RESIZER_SIZE; + } + handle_size *= mpr->scale_final; + float scale_xy[2]; + manipulator_calc_rect_view_scale(mpr, dims, scale_xy); + margin[0] = ((handle_size * scale_xy[0])); + margin[1] = ((handle_size * scale_xy[1])); +} + +/* -------------------------------------------------------------------- */ + +static void manipulator_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_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X: { ARRAY_SET_ITEMS(r_pt, 0.5, 0.0); x = false; break; } + case ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X: { ARRAY_SET_ITEMS(r_pt, -0.5, 0.0); x = false; break; } + case ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_Y: { ARRAY_SET_ITEMS(r_pt, 0.0, 0.5); y = false; break; } + case ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_Y: { ARRAY_SET_ITEMS(r_pt, 0.0, -0.5); y = false; break; } + case ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X_MIN_Y: { ARRAY_SET_ITEMS(r_pt, 0.5, 0.5); x = y = false; break; } + case ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X_MAX_Y: { ARRAY_SET_ITEMS(r_pt, 0.5, -0.5); x = y = false; break; } + case ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X_MIN_Y: { ARRAY_SET_ITEMS(r_pt, -0.5, 0.5); x = y = false; break; } + case ED_MANIPULATOR_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_MANIPULATOR_CAGE2D_STYLE_BOX + * \{ */ + +static void cage2d_draw_box_corners( + const rctf *r, const float margin[2], const float color[3]) +{ + uint pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT); + + immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); + immUniformColor3fv(color); + + immBegin(GWN_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; + Gwn_PrimType prim_type = GWN_PRIM_NONE; + + switch (highlighted) { + case ED_MANIPULATOR_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 = GWN_PRIM_TRI_FAN; + } + else { + prim_type = GWN_PRIM_LINE_STRIP; + } + break; + } + case ED_MANIPULATOR_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 = GWN_PRIM_TRI_FAN; + } + else { + prim_type = GWN_PRIM_LINE_STRIP; + } + break; + } + case ED_MANIPULATOR_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 = GWN_PRIM_TRI_FAN; + } + else { + prim_type = GWN_PRIM_LINE_STRIP; + } + break; + } + case ED_MANIPULATOR_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 = GWN_PRIM_TRI_FAN; + } + else { + prim_type = GWN_PRIM_LINE_STRIP; + } + break; + } + case ED_MANIPULATOR_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 = GWN_PRIM_TRI_FAN; + } + else { + prim_type = GWN_PRIM_LINE_STRIP; + } + break; + } + case ED_MANIPULATOR_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 = GWN_PRIM_TRI_FAN; + } + else { + prim_type = GWN_PRIM_LINE_STRIP; + } + break; + } + case ED_MANIPULATOR_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 = GWN_PRIM_TRI_FAN; + } + else { + prim_type = GWN_PRIM_LINE_STRIP; + } + break; + } + case ED_MANIPULATOR_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 = GWN_PRIM_TRI_FAN; + } + else { + prim_type = GWN_PRIM_LINE_STRIP; + } + break; + } + case ED_MANIPULATOR_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 = GWN_PRIM_TRI_FAN; + } + else { + prim_type = GWN_PRIM_LINE_STRIP; + } + break; + } + + case ED_MANIPULATOR_CAGE2D_PART_TRANSLATE: + if (draw_options & ED_MANIPULATOR_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 = GWN_PRIM_TRI_FAN; + } + else { + prim_type = GWN_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 = GWN_PRIM_TRI_FAN; + } + else { + /* unreachable */ + BLI_assert(0); + prim_type = GWN_PRIM_LINE_STRIP; + } + } + break; + default: + return; + } + + BLI_assert(prim_type != GWN_PRIM_NONE); + + Gwn_VertFormat *format = immVertexFormat(); + struct { + uint pos, col; + } attr_id = { + .pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT), + .col = GWN_vertformat_attr_add(format, "color", GWN_COMP_F32, 3, GWN_FETCH_FLOAT), + }; + immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR); + + { + if (is_solid) { + BLI_assert(ELEM(prim_type, GWN_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, GWN_PRIM_LINE_STRIP, GWN_PRIM_LINES)); + glLineWidth(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(); + + glLineWidth(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_MANIPULATOR_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 ? GWN_PRIM_TRI_FAN : GWN_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 = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT); + + immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR); + immUniformColor3fv(color); + + immBegin(GWN_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_MANIPULATOR_CAGE2D_XFORM_FLAG_ROTATE) { + immBegin(GWN_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_MANIPULATOR_CAGE2D_XFORM_FLAG_TRANSLATE) { + if (draw_options & ED_MANIPULATOR_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(GWN_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 = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_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_MANIPULATOR_CAGE2D_XFORM_FLAG_ROTATE) { + const float handle[2] = {BLI_rctf_cent_x(r), r->ymax + (margin[1] * MANIPULATOR_MARGIN_OFFSET_SCALE)}; + circle_fn(pos, handle[0], handle[1], rad[0], rad[1], resolu); + } + + immUnbindProgram(); +} + +/** \} */ + +static void manipulator_cage2d_draw_intern( + wmManipulator *mpr, const bool select, const bool highlight, const int select_id) +{ + // const bool use_clamp = (mpr->parent_mgroup->type->flag & WM_MANIPULATORGROUPTYPE_3D) == 0; + float dims[2]; + RNA_float_get_array(mpr->ptr, "dimensions", dims); + float matrix_final[4][4]; + + const int transform_flag = RNA_enum_get(mpr->ptr, "transform"); + const int draw_style = RNA_enum_get(mpr->ptr, "draw_style"); + const int draw_options = RNA_enum_get(mpr->ptr, "draw_options"); + + const float size_real[2] = {dims[0] / 2.0f, dims[1] / 2.0f}; + + WM_manipulator_calc_matrix_final(mpr, matrix_final); + + gpuPushMatrix(); + gpuMultMatrix(matrix_final); + + float margin[2]; + manipulator_calc_rect_view_margin(mpr, dims, margin); + + /* Handy for quick testing draw (if it's outside bounds). */ + if (false) { + glEnable(GL_BLEND); + uint pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_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(); + glDisable(GL_BLEND); + } + + 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_MANIPULATOR_CAGE2D_XFORM_FLAG_SCALE) { + int scale_parts[] = { + ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X, + ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X, + ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_Y, + ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_Y, + + ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X_MIN_Y, + ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X_MAX_Y, + ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X_MIN_Y, + ED_MANIPULATOR_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( + mpr->color, scale_parts[i], size, margin, mpr->line_width, true, draw_options); + } + } + if (transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_TRANSLATE) { + const int transform_part = ED_MANIPULATOR_CAGE2D_PART_TRANSLATE; + GPU_select_load_id(select_id | transform_part); + cage2d_draw_box_interaction( + mpr->color, transform_part, size, margin, mpr->line_width, true, draw_options); + } + if (transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_ROTATE) { + cage2d_draw_box_interaction( + mpr->color, ED_MANIPULATOR_CAGE2D_PART_ROTATE, size_real, margin, mpr->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_MANIPULATOR_CAGE2D_STYLE_BOX) { + /* corner manipulators */ + glLineWidth(mpr->line_width + 3.0f); + cage2d_draw_box_corners(&r, margin, (const float[3]){0, 0, 0}); + + /* corner manipulators */ + float color[4]; + manipulator_color_get(mpr, highlight, color); + glLineWidth(mpr->line_width); + cage2d_draw_box_corners(&r, margin, color); + + bool show = false; + if (mpr->highlight_part == ED_MANIPULATOR_CAGE2D_PART_TRANSLATE) { + /* Only show if we're drawing the center handle + * otherwise the entire rectangle is the hotspot. */ + if (draw_options & ED_MANIPULATOR_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE) { + show = true; + } + } + else { + show = true; + } + + if (show) { + cage2d_draw_box_interaction( + mpr->color, mpr->highlight_part, size_real, margin, mpr->line_width, false, draw_options); + } + + if (transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_ROTATE) { + cage2d_draw_box_interaction( + mpr->color, ED_MANIPULATOR_CAGE2D_PART_ROTATE, size_real, margin, mpr->line_width, false, draw_options); + } + } + else if (draw_style == ED_MANIPULATOR_CAGE2D_STYLE_CIRCLE) { + float color[4]; + manipulator_color_get(mpr, highlight, color); + + glEnable(GL_LINE_SMOOTH); + glEnable(GL_BLEND); + + glLineWidth(mpr->line_width + 3.0f); + cage2d_draw_circle_wire(&r, margin, (const float[3]){0, 0, 0}, transform_flag, draw_options); + glLineWidth(mpr->line_width); + cage2d_draw_circle_wire(&r, margin, color, transform_flag, draw_options); + + + /* corner manipulators */ + 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); + + glDisable(GL_BLEND); + glDisable(GL_LINE_SMOOTH); + } + else { + BLI_assert(0); + } + } + + glLineWidth(1.0); + gpuPopMatrix(); +} + +/** + * For when we want to draw 2d cage in 3d views. + */ +static void manipulator_cage2d_draw_select(const bContext *UNUSED(C), wmManipulator *mpr, int select_id) +{ + manipulator_cage2d_draw_intern(mpr, true, false, select_id); +} + +static void manipulator_cage2d_draw(const bContext *UNUSED(C), wmManipulator *mpr) +{ + const bool is_highlight = (mpr->state & WM_MANIPULATOR_STATE_HIGHLIGHT) != 0; + manipulator_cage2d_draw_intern(mpr, false, is_highlight, -1); +} + +static int manipulator_cage2d_get_cursor(wmManipulator *mpr) +{ + int highlight_part = mpr->highlight_part; + + if (mpr->parent_mgroup->type->flag & WM_MANIPULATORGROUPTYPE_3D) { + return BC_NSEW_SCROLLCURSOR; + } + + switch (highlight_part) { + case ED_MANIPULATOR_CAGE2D_PART_TRANSLATE: + return BC_NSEW_SCROLLCURSOR; + case ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X: + case ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X: + return CURSOR_X_MOVE; + case ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_Y: + case ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_Y: + return CURSOR_Y_MOVE; + + /* TODO diagonal cursor */ + case ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X_MIN_Y: + case ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X_MIN_Y: + return BC_NSEW_SCROLLCURSOR; + case ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X_MAX_Y: + case ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X_MAX_Y: + return BC_NSEW_SCROLLCURSOR; + case ED_MANIPULATOR_CAGE2D_PART_ROTATE: + return BC_CROSSCURSOR; + default: + return CURSOR_STD; + } +} + +static int manipulator_cage2d_test_select( + bContext *C, wmManipulator *mpr, const wmEvent *event) +{ + float point_local[2]; + float dims[2]; + RNA_float_get_array(mpr->ptr, "dimensions", dims); + const float size_real[2] = {dims[0] / 2.0f, dims[1] / 2.0f}; + + if (manipulator_window_project_2d( + C, mpr, (const float[2]){UNPACK2(event->mval)}, 2, true, point_local) == false) + { + return -1; + } + + float margin[2]; + manipulator_calc_rect_view_margin(mpr, 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(mpr->ptr, "transform"); + const int draw_options = RNA_enum_get(mpr->ptr, "draw_options"); + + if (transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_TRANSLATE) { + rctf r; + if (draw_options & ED_MANIPULATOR_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_MANIPULATOR_CAGE2D_PART_TRANSLATE; + } + } + + /* if manipulator does not have a scale intersection, don't do it */ + if (transform_flag & (ED_MANIPULATOR_CAGE2D_XFORM_FLAG_SCALE | ED_MANIPULATOR_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_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X_MIN_Y; + } + if (BLI_rctf_isect_pt_v(&r_ymax, point_local)) { + return ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X_MAX_Y; + } + return ED_MANIPULATOR_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_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X_MIN_Y; + } + if (BLI_rctf_isect_pt_v(&r_ymax, point_local)) { + return ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X_MAX_Y; + } + return ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X; + } + if (BLI_rctf_isect_pt_v(&r_ymin, point_local)) { + return ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_Y; + } + if (BLI_rctf_isect_pt_v(&r_ymax, point_local)) { + return ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_Y; + } + } + + if (transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_ROTATE) { + /* Rotate: + * (*) <-- hot spot is here! + * +---+ + * | | + * +---+ */ + const float r_rotate_pt[2] = {0.0f, size_real[1] + (margin[1] * MANIPULATOR_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_MANIPULATOR_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 manipulator_cage2d_setup(wmManipulator *mpr) +{ + mpr->flag |= WM_MANIPULATOR_DRAW_MODAL | WM_MANIPULATOR_DRAW_NO_SCALE; +} + +static int manipulator_cage2d_invoke( + bContext *C, wmManipulator *mpr, const wmEvent *event) +{ + RectTransformInteraction *data = MEM_callocN(sizeof(RectTransformInteraction), "cage_interaction"); + + copy_m4_m4(data->orig_matrix_offset, mpr->matrix_offset); + WM_manipulator_calc_matrix_final_no_offset(mpr, data->orig_matrix_final_no_offset); + + if (manipulator_window_project_2d( + C, mpr, (const float[2]){UNPACK2(event->mval)}, 2, false, data->orig_mouse) == 0) + { + zero_v2(data->orig_mouse); + } + + mpr->interaction_data = data; + + return OPERATOR_RUNNING_MODAL; +} + +static int manipulator_cage2d_modal( + bContext *C, wmManipulator *mpr, const wmEvent *event, + eWM_ManipulatorTweak UNUSED(tweak_flag)) +{ + /* For transform logic to be managable 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 = mpr->interaction_data; + float point_local[2]; + + float dims[2]; + RNA_float_get_array(mpr->ptr, "dimensions", dims); + + { + float matrix_back[4][4]; + copy_m4_m4(matrix_back, mpr->matrix_offset); + copy_m4_m4(mpr->matrix_offset, data->orig_matrix_offset); + + bool ok = manipulator_window_project_2d( + C, mpr, (const float[2]){UNPACK2(event->mval)}, 2, false, point_local); + copy_m4_m4(mpr->matrix_offset, matrix_back); + if (!ok) { + return OPERATOR_RUNNING_MODAL; + } + } + + const int transform_flag = RNA_enum_get(mpr->ptr, "transform"); + wmManipulatorProperty *mpr_prop; + + mpr_prop = WM_manipulator_target_property_find(mpr, "matrix"); + if (mpr_prop->type != NULL) { + WM_manipulator_target_property_value_get_array(mpr, mpr_prop, &mpr->matrix_offset[0][0]); + } + + if (mpr->highlight_part == ED_MANIPULATOR_CAGE2D_PART_TRANSLATE) { + /* do this to prevent clamping from changing size */ + copy_m4_m4(mpr->matrix_offset, data->orig_matrix_offset); + mpr->matrix_offset[3][0] = data->orig_matrix_offset[3][0] + (point_local[0] - data->orig_mouse[0]); + mpr->matrix_offset[3][1] = data->orig_matrix_offset[3][1] + (point_local[1] - data->orig_mouse[1]); + } + else if (mpr->highlight_part == ED_MANIPULATOR_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, mpr->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, mpr->matrix_space); + + zero_v3(matrix_rotate[3]); + transform_pivot_set_m4(matrix_rotate, pivot); + + mul_m4_m4m4(mpr->matrix_offset, matrix_rotate, data->orig_matrix_offset); + +#undef MUL_V2_V3_M4_FINAL + } + else { + /* scale */ + copy_m4_m4(mpr->matrix_offset, data->orig_matrix_offset); + float pivot[2]; + bool constrain_axis[2] = {false}; + + if (transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_TRANSLATE) { + manipulator_rect_pivot_from_scale_part(mpr->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_MANIPULATOR_CAGE2D_XFORM_FLAG_SCALE_SIGNED) == 0) { + if (sign != signum_i(scale[i])) { + scale[i] = 0.0f; + } + } + } + } + + if (transform_flag & ED_MANIPULATOR_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(mpr->matrix_offset, data->orig_matrix_offset, matrix_scale); + } + + if (mpr_prop->type != NULL) { + WM_manipulator_target_property_value_set_array(C, mpr, mpr_prop, &mpr->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 manipulator_cage2d_property_update(wmManipulator *mpr, wmManipulatorProperty *mpr_prop) +{ + if (STREQ(mpr_prop->type->idname, "matrix")) { + if (WM_manipulator_target_property_array_length(mpr, mpr_prop) == 16) { + WM_manipulator_target_property_value_get_array(mpr, mpr_prop, &mpr->matrix_offset[0][0]); + } + else { + BLI_assert(0); + } + } + else { + BLI_assert(0); + } +} + +static void manipulator_cage2d_exit(bContext *C, wmManipulator *mpr, const bool cancel) +{ + RectTransformInteraction *data = mpr->interaction_data; + + MEM_SAFE_FREE(data->dial); + + if (!cancel) + return; + + wmManipulatorProperty *mpr_prop; + + /* reset properties */ + mpr_prop = WM_manipulator_target_property_find(mpr, "matrix"); + if (mpr_prop->type != NULL) { + WM_manipulator_target_property_value_set_array(C, mpr, mpr_prop, &data->orig_matrix_offset[0][0]); + } + + copy_m4_m4(mpr->matrix_offset, data->orig_matrix_offset); +} + + +/* -------------------------------------------------------------------- */ +/** \name Cage Manipulator API + * + * \{ */ + +static void MANIPULATOR_WT_cage_2d(wmManipulatorType *wt) +{ + /* identifiers */ + wt->idname = "MANIPULATOR_WT_cage_2d"; + + /* api callbacks */ + wt->draw = manipulator_cage2d_draw; + wt->draw_select = manipulator_cage2d_draw_select; + wt->test_select = manipulator_cage2d_test_select; + wt->setup = manipulator_cage2d_setup; + wt->invoke = manipulator_cage2d_invoke; + wt->property_update = manipulator_cage2d_property_update; + wt->modal = manipulator_cage2d_modal; + wt->exit = manipulator_cage2d_exit; + wt->cursor_get = manipulator_cage2d_get_cursor; + + wt->struct_size = sizeof(wmManipulator); + + /* rna */ + static EnumPropertyItem rna_enum_draw_style[] = { + {ED_MANIPULATOR_CAGE2D_STYLE_BOX, "BOX", 0, "Box", ""}, + {ED_MANIPULATOR_CAGE2D_STYLE_CIRCLE, "CIRCLE", 0, "Circle", ""}, + {0, NULL, 0, NULL, NULL} + }; + static EnumPropertyItem rna_enum_transform[] = { + {ED_MANIPULATOR_CAGE2D_XFORM_FLAG_TRANSLATE, "TRANSLATE", 0, "Translate", ""}, + {ED_MANIPULATOR_CAGE2D_XFORM_FLAG_ROTATE, "ROTATE", 0, "Rotate", ""}, + {ED_MANIPULATOR_CAGE2D_XFORM_FLAG_SCALE, "SCALE", 0, "Scale", ""}, + {ED_MANIPULATOR_CAGE2D_XFORM_FLAG_SCALE_UNIFORM, "SCALE_UNIFORM", 0, "Scale Uniform", ""}, + {0, NULL, 0, NULL, NULL} + }; + static EnumPropertyItem rna_enum_draw_options[] = { + {ED_MANIPULATOR_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(wt->srna, "dimensions", 2, unit_v2, 0, FLT_MAX, "Dimensions", "", 0.0f, FLT_MAX); + RNA_def_enum_flag(wt->srna, "transform", rna_enum_transform, 0, "Transform Options", ""); + RNA_def_enum(wt->srna, "draw_style", rna_enum_draw_style, ED_MANIPULATOR_CAGE2D_STYLE_CIRCLE, "Draw Style", ""); + RNA_def_enum_flag( + wt->srna, "draw_options", rna_enum_draw_options, + ED_MANIPULATOR_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE, "Draw Options", ""); + + WM_manipulatortype_target_property_def(wt, "matrix", PROP_FLOAT, 16); +} + +void ED_manipulatortypes_cage_2d(void) +{ + WM_manipulatortype_append(MANIPULATOR_WT_cage_2d); +} + +/** \} */ diff --git a/source/blender/editors/manipulator_library/manipulator_types/cage3d_manipulator.c b/source/blender/editors/manipulator_library/manipulator_types/cage3d_manipulator.c new file mode 100644 index 00000000000..6c2e2bd564d --- /dev/null +++ b/source/blender/editors/manipulator_library/manipulator_types/cage3d_manipulator.c @@ -0,0 +1,691 @@ +/* + * ***** 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_manipulator.c + * \ingroup wm + * + * \name Cage Manipulator + * + * 2D Manipulator + * + * \brief Rectangular manipulator acting as a 'cage' around its content. + * Interacting scales or translates the manipulator. + */ + +#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 "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_manipulator_library.h" + +/* own includes */ +#include "../manipulator_library_intern.h" + +#define MANIPULATOR_RESIZER_SIZE 10.0f +#define MANIPULATOR_MARGIN_OFFSET_SCALE 1.5f + +static void manipulator_calc_matrix_final_no_offset( + const wmManipulator *mpr, float orig_matrix_final_no_offset[4][4], bool use_space) +{ + float mat_identity[4][4]; + struct WM_ManipulatorMatrixParams params = {NULL}; + unit_m4(mat_identity); + if (use_space == false) { + params.matrix_basis = mat_identity; + } + params.matrix_offset = mat_identity; + WM_manipulator_calc_matrix_final_params(mpr, ¶ms, orig_matrix_final_no_offset); +} + +static void manipulator_calc_rect_view_scale( + const wmManipulator *mpr, 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]; + manipulator_calc_matrix_final_no_offset(mpr, matrix_final_no_offset, false); + mul_v3_mat3_m4v3(x_axis, matrix_final_no_offset, mpr->matrix_offset[0]); + mul_v3_mat3_m4v3(y_axis, matrix_final_no_offset, mpr->matrix_offset[1]); + mul_v3_mat3_m4v3(z_axis, matrix_final_no_offset, mpr->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 manipulator_calc_rect_view_margin( + const wmManipulator *mpr, const float dims[3], float margin[3]) +{ + float handle_size; + if (mpr->parent_mgroup->type->flag & WM_MANIPULATORGROUPTYPE_3D) { + handle_size = 0.15f; + } + else { + handle_size = MANIPULATOR_RESIZER_SIZE; + } + // XXX, the scale isn't taking offset into account, we need to calculate scale per handle! + // handle_size *= mpr->scale_final; + + float scale_xyz[3]; + manipulator_calc_rect_view_scale(mpr, 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 manipulator_rect_pivot_from_scale_part(int part, float r_pt[3], bool r_constrain_axis[3]) +{ + if (part >= ED_MANIPULATOR_CAGE3D_PART_SCALE_MIN_X_MIN_Y_MIN_Z && + part <= ED_MANIPULATOR_CAGE3D_PART_SCALE_MAX_X_MAX_Y_MAX_Z) + { + int index = (part - ED_MANIPULATOR_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_MANIPULATOR_CAGE2D_STYLE_BOX + * \{ */ + +static void cage3d_draw_box_corners( + const float r[3], const float margin[3], const float color[3]) +{ + uint pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 3, GWN_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_MANIPULATOR_CAGE3D_PART_SCALE_MIN_X_MIN_Y_MIN_Z && + highlighted <= ED_MANIPULATOR_CAGE3D_PART_SCALE_MAX_X_MAX_Y_MAX_Z) + { + int index = (highlighted - ED_MANIPULATOR_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 = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 3, GWN_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_MANIPULATOR_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 = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 3, GWN_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_MANIPULATOR_CAGE2D_XFORM_FLAG_TRANSLATE) { + if (draw_options & ED_MANIPULATOR_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(GWN_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 = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 3, GWN_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 manipulator_cage3d_draw_intern( + RegionView3D *rv3d, + wmManipulator *mpr, const bool select, const bool highlight, const int select_id) +{ + // const bool use_clamp = (mpr->parent_mgroup->type->flag & WM_MANIPULATORGROUPTYPE_3D) == 0; + float dims[3]; + RNA_float_get_array(mpr->ptr, "dimensions", dims); + float matrix_final[4][4]; + + const int transform_flag = RNA_enum_get(mpr->ptr, "transform"); + const int draw_style = RNA_enum_get(mpr->ptr, "draw_style"); + const int draw_options = RNA_enum_get(mpr->ptr, "draw_options"); + + const float size_real[3] = {dims[0] / 2.0f, dims[1] / 2.0f, dims[2] / 2.0f}; + + WM_manipulator_calc_matrix_final(mpr, matrix_final); + + gpuPushMatrix(); + gpuMultMatrix(matrix_final); + + float margin[3]; + manipulator_calc_rect_view_margin(mpr, dims, margin); + + /* Handy for quick testing draw (if it's outside bounds). */ + if (false) { + glEnable(GL_BLEND); + uint pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 3, GWN_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(); + glDisable(GL_BLEND); + } + + 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_MANIPULATOR_CAGE2D_XFORM_FLAG_SCALE) { + for (int i = ED_MANIPULATOR_CAGE3D_PART_SCALE_MIN_X_MIN_Y_MIN_Z; + i <= ED_MANIPULATOR_CAGE3D_PART_SCALE_MAX_X_MAX_Y_MAX_Z; + i++) + { + if (i == ED_MANIPULATOR_CAGE3D_PART_SCALE_MID_X_MID_Y_MID_Z) { + continue; + } + GPU_select_load_id(select_id | i); + cage3d_draw_box_interaction( + mpr->color, i, size, margin); + } + } + if (transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_TRANSLATE) { + const int transform_part = ED_MANIPULATOR_CAGE3D_PART_TRANSLATE; + GPU_select_load_id(select_id | transform_part); + cage3d_draw_box_interaction( + mpr->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_MANIPULATOR_CAGE2D_STYLE_BOX) { + /* corner manipulators */ + glLineWidth(mpr->line_width + 3.0f); + cage3d_draw_box_corners(size_real, margin, (const float[3]){0, 0, 0}); + + /* corner manipulators */ + float color[4]; + manipulator_color_get(mpr, highlight, color); + glLineWidth(mpr->line_width); + cage3d_draw_box_corners(size_real, margin, color); + + bool show = false; + if (mpr->highlight_part == ED_MANIPULATOR_CAGE3D_PART_TRANSLATE) { + /* Only show if we're drawing the center handle + * otherwise the entire rectangle is the hotspot. */ + if (draw_options & ED_MANIPULATOR_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE) { + show = true; + } + } + else { + show = true; + } + + if (show) { + cage3d_draw_box_interaction( + mpr->color, mpr->highlight_part, size_real, margin); + } + } + else if (draw_style == ED_MANIPULATOR_CAGE2D_STYLE_CIRCLE) { + float color[4]; + manipulator_color_get(mpr, highlight, color); + + glEnable(GL_LINE_SMOOTH); + glEnable(GL_POLYGON_SMOOTH); + glEnable(GL_BLEND); + + glLineWidth(mpr->line_width + 3.0f); + cage3d_draw_circle_wire(size_real, margin, (const float[3]){0, 0, 0}, transform_flag, draw_options); + glLineWidth(mpr->line_width); + cage3d_draw_circle_wire(size_real, margin, color, transform_flag, draw_options); + + /* corner manipulators */ + 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); + + glDisable(GL_BLEND); + glDisable(GL_POLYGON_SMOOTH); + glDisable(GL_LINE_SMOOTH); + } + else { + BLI_assert(0); + } + } + + glLineWidth(1.0); + gpuPopMatrix(); +} + +/** + * For when we want to draw 3d cage in 3d views. + */ +static void manipulator_cage3d_draw_select(const bContext *C, wmManipulator *mpr, int select_id) +{ + ARegion *ar = CTX_wm_region(C); + RegionView3D *rv3d = ar->regiondata; + manipulator_cage3d_draw_intern(rv3d, mpr, true, false, select_id); +} + +static void manipulator_cage3d_draw(const bContext *C, wmManipulator *mpr) +{ + ARegion *ar = CTX_wm_region(C); + RegionView3D *rv3d = ar->regiondata; + const bool is_highlight = (mpr->state & WM_MANIPULATOR_STATE_HIGHLIGHT) != 0; + manipulator_cage3d_draw_intern(rv3d, mpr, false, is_highlight, -1); +} + +static int manipulator_cage3d_get_cursor(wmManipulator *mpr) +{ + if (mpr->parent_mgroup->type->flag & WM_MANIPULATORGROUPTYPE_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 manipulator_cage3d_setup(wmManipulator *mpr) +{ + mpr->flag |= /* WM_MANIPULATOR_DRAW_MODAL | */ /* TODO */ + WM_MANIPULATOR_DRAW_NO_SCALE; +} + +static int manipulator_cage3d_invoke( + bContext *C, wmManipulator *mpr, const wmEvent *event) +{ + RectTransformInteraction *data = MEM_callocN(sizeof(RectTransformInteraction), "cage_interaction"); + + copy_m4_m4(data->orig_matrix_offset, mpr->matrix_offset); + manipulator_calc_matrix_final_no_offset(mpr, data->orig_matrix_final_no_offset, true); + + if (manipulator_window_project_3d( + C, mpr, (const float[2]){UNPACK2(event->mval)}, false, data->orig_mouse) == 0) + { + zero_v3(data->orig_mouse); + } + + mpr->interaction_data = data; + + return OPERATOR_RUNNING_MODAL; +} + +static int manipulator_cage3d_modal( + bContext *C, wmManipulator *mpr, const wmEvent *event, + eWM_ManipulatorTweak UNUSED(tweak_flag)) +{ + /* For transform logic to be managable 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 = mpr->interaction_data; + float point_local[3]; + + float dims[3]; + RNA_float_get_array(mpr->ptr, "dimensions", dims); + + { + float matrix_back[4][4]; + copy_m4_m4(matrix_back, mpr->matrix_offset); + copy_m4_m4(mpr->matrix_offset, data->orig_matrix_offset); + + bool ok = manipulator_window_project_3d( + C, mpr, (const float[2]){UNPACK2(event->mval)}, false, point_local); + copy_m4_m4(mpr->matrix_offset, matrix_back); + if (!ok) { + return OPERATOR_RUNNING_MODAL; + } + } + + const int transform_flag = RNA_enum_get(mpr->ptr, "transform"); + wmManipulatorProperty *mpr_prop; + + mpr_prop = WM_manipulator_target_property_find(mpr, "matrix"); + if (mpr_prop->type != NULL) { + WM_manipulator_target_property_value_get_array(mpr, mpr_prop, &mpr->matrix_offset[0][0]); + } + + if (mpr->highlight_part == ED_MANIPULATOR_CAGE3D_PART_TRANSLATE) { + /* do this to prevent clamping from changing size */ + copy_m4_m4(mpr->matrix_offset, data->orig_matrix_offset); + mpr->matrix_offset[3][0] = data->orig_matrix_offset[3][0] + (point_local[0] - data->orig_mouse[0]); + mpr->matrix_offset[3][1] = data->orig_matrix_offset[3][1] + (point_local[1] - data->orig_mouse[1]); + mpr->matrix_offset[3][2] = data->orig_matrix_offset[3][2] + (point_local[2] - data->orig_mouse[2]); + } + else if (mpr->highlight_part == ED_MANIPULATOR_CAGE3D_PART_ROTATE) { + /* TODO (if needed) */ + } + else { + /* scale */ + copy_m4_m4(mpr->matrix_offset, data->orig_matrix_offset); + float pivot[3]; + bool constrain_axis[3] = {false}; + + if (transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_TRANSLATE) { + manipulator_rect_pivot_from_scale_part(mpr->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_MANIPULATOR_CAGE2D_XFORM_FLAG_SCALE_SIGNED) == 0) { + if (sign != signum_i(scale[i])) { + scale[i] = 0.0f; + } + } + } + } + + if (transform_flag & ED_MANIPULATOR_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(mpr->matrix_offset, data->orig_matrix_offset, matrix_scale); + } + + if (mpr_prop->type != NULL) { + WM_manipulator_target_property_value_set_array(C, mpr, mpr_prop, &mpr->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 manipulator_cage3d_property_update(wmManipulator *mpr, wmManipulatorProperty *mpr_prop) +{ + if (STREQ(mpr_prop->type->idname, "matrix")) { + if (WM_manipulator_target_property_array_length(mpr, mpr_prop) == 16) { + WM_manipulator_target_property_value_get_array(mpr, mpr_prop, &mpr->matrix_offset[0][0]); + } + else { + BLI_assert(0); + } + } + else { + BLI_assert(0); + } +} + +static void manipulator_cage3d_exit(bContext *C, wmManipulator *mpr, const bool cancel) +{ + RectTransformInteraction *data = mpr->interaction_data; + + if (!cancel) + return; + + wmManipulatorProperty *mpr_prop; + + /* reset properties */ + mpr_prop = WM_manipulator_target_property_find(mpr, "matrix"); + if (mpr_prop->type != NULL) { + WM_manipulator_target_property_value_set_array(C, mpr, mpr_prop, &data->orig_matrix_offset[0][0]); + } + + copy_m4_m4(mpr->matrix_offset, data->orig_matrix_offset); +} + + +/* -------------------------------------------------------------------- */ +/** \name Cage Manipulator API + * + * \{ */ + +static void MANIPULATOR_WT_cage_3d(wmManipulatorType *wt) +{ + /* identifiers */ + wt->idname = "MANIPULATOR_WT_cage_3d"; + + /* api callbacks */ + wt->draw = manipulator_cage3d_draw; + wt->draw_select = manipulator_cage3d_draw_select; + wt->setup = manipulator_cage3d_setup; + wt->invoke = manipulator_cage3d_invoke; + wt->property_update = manipulator_cage3d_property_update; + wt->modal = manipulator_cage3d_modal; + wt->exit = manipulator_cage3d_exit; + wt->cursor_get = manipulator_cage3d_get_cursor; + + wt->struct_size = sizeof(wmManipulator); + + /* rna */ + static EnumPropertyItem rna_enum_draw_style[] = { + {ED_MANIPULATOR_CAGE2D_STYLE_BOX, "BOX", 0, "Box", ""}, + {ED_MANIPULATOR_CAGE2D_STYLE_CIRCLE, "CIRCLE", 0, "Circle", ""}, + {0, NULL, 0, NULL, NULL} + }; + static EnumPropertyItem rna_enum_transform[] = { + {ED_MANIPULATOR_CAGE2D_XFORM_FLAG_TRANSLATE, "TRANSLATE", 0, "Translate", ""}, + {ED_MANIPULATOR_CAGE2D_XFORM_FLAG_SCALE, "SCALE", 0, "Scale", ""}, + {ED_MANIPULATOR_CAGE2D_XFORM_FLAG_SCALE_UNIFORM, "SCALE_UNIFORM", 0, "Scale Uniform", ""}, + {0, NULL, 0, NULL, NULL} + }; + static EnumPropertyItem rna_enum_draw_options[] = { + {ED_MANIPULATOR_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(wt->srna, "dimensions", 3, unit_v3, 0, FLT_MAX, "Dimensions", "", 0.0f, FLT_MAX); + RNA_def_enum_flag(wt->srna, "transform", rna_enum_transform, 0, "Transform Options", ""); + RNA_def_enum(wt->srna, "draw_style", rna_enum_draw_style, ED_MANIPULATOR_CAGE2D_STYLE_CIRCLE, "Draw Style", ""); + RNA_def_enum_flag( + wt->srna, "draw_options", rna_enum_draw_options, + ED_MANIPULATOR_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE, "Draw Options", ""); + + WM_manipulatortype_target_property_def(wt, "matrix", PROP_FLOAT, 16); +} + +void ED_manipulatortypes_cage_3d(void) +{ + WM_manipulatortype_append(MANIPULATOR_WT_cage_3d); +} + +/** \} */ diff --git a/source/blender/editors/manipulator_library/manipulator_types/dial3d_manipulator.c b/source/blender/editors/manipulator_library/manipulator_types/dial3d_manipulator.c new file mode 100644 index 00000000000..643a379cbb0 --- /dev/null +++ b/source/blender/editors/manipulator_library/manipulator_types/dial3d_manipulator.c @@ -0,0 +1,486 @@ +/* + * ***** 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_manipulator.c + * \ingroup wm + * + * \name Dial Manipulator + * + * 3D Manipulator + * + * \brief Circle shaped manipulator for circular interaction. + * Currently no own handling, use with operator only. + * + * - `matrix[0]` is derived from Y and Z. + * - `matrix[1]` is 'up' when DialManipulator.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 "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_manipulator_library.h" + +/* own includes */ +#include "../manipulator_geometry.h" +#include "../manipulator_library_intern.h" + +/* to use custom dials exported to geom_dial_manipulator.c */ +// #define USE_MANIPULATOR_CUSTOM_DIAL + +static int manipulator_dial_modal( + bContext *C, wmManipulator *mpr, const wmEvent *event, + eWM_ManipulatorTweak tweak_flag); + +typedef struct DialInteraction { + float init_mval[2]; + + /* only for when using properties */ + float init_prop_angle; + + /* cache the last angle to detect rotations bigger than -/+ PI */ + float last_angle; + /* number of full rotations */ + int rotations; + + /* 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 + +/** + * We can't use this for the #wmManipulatorType.matrix_basis_get callback, it conflicts with depth picking. + */ +static void dial_calc_matrix(const wmManipulator *mpr, float mat[4][4]) +{ + float rot[3][3]; + const float up[3] = {0.0f, 0.0f, 1.0f}; + + rotation_between_vecs_to_mat3(rot, up, mpr->matrix_basis[2]); + copy_m4_m3(mat, rot); + copy_v3_v3(mat[3], mpr->matrix_basis[3]); +} + +/* -------------------------------------------------------------------- */ + +static void dial_geom_draw( + const wmManipulator *mpr, const float color[4], const bool select, + float axis_modal_mat[4][4], float clip_plane[4]) +{ +#ifdef USE_MANIPULATOR_CUSTOM_DIAL + UNUSED_VARS(dial, col, axis_modal_mat, clip_plane); + wm_manipulator_geometryinfo_draw(&wm_manipulator_geom_data_dial, select); +#else + const int draw_options = RNA_enum_get(mpr->ptr, "draw_options"); + const bool filled = (draw_options & ED_MANIPULATOR_DIAL_DRAW_FLAG_FILL) != 0; + + glLineWidth(mpr->line_width); + + Gwn_VertFormat *format = immVertexFormat(); + uint pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_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 { + imm_draw_circle_wire_2d(pos, 0, 0, 1.0, DIAL_RESOLUTION); + } + + 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]) +{ + glLineWidth(1.0f); + + gpuPushMatrix(); + gpuRotate3f(RAD2DEGF(angle), 0.0f, 0.0f, -1.0f); + + uint pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT); + + immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); + + immUniformColor4fv(color); + + immBegin(GWN_PRIM_LINE_STRIP, 2); + immVertex3f(pos, 0.0f, 0.0f, 0.0f); + immVertex3fv(pos, co_outer); + immEnd(); + + immUnbindProgram(); + + gpuPopMatrix(); +} + +static void dial_ghostarc_draw( + const wmManipulator *mpr, const float angle_ofs, const float angle_delta, const float color[4]) +{ + const float width_inner = DIAL_WIDTH - mpr->line_width * 0.5f / U.manipulator_size; + + Gwn_VertFormat *format = immVertexFormat(); + uint pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT); + immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); + immUniformColor4fv(color); + imm_draw_disk_partial_fill_2d( + pos, 0, 0, 0.0, width_inner, DIAL_RESOLUTION, RAD2DEGF(angle_ofs), RAD2DEGF(angle_delta)); + immUnbindProgram(); +} + +static void dial_ghostarc_get_angles( + struct Depsgraph *depsgraph, + const wmManipulator *mpr, + const wmEvent *event, + const ARegion *ar, const View3D *v3d, + float mat[4][4], const float co_outer[3], + float *r_start, float *r_delta) +{ + DialInteraction *inter = mpr->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, mpr->matrix_basis[3], view_vec); + normalize_v3_v3(axis_vec, mpr->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, mpr->matrix_basis[3]); + + float proj_mval_new_rel[3]; + float proj_mval_init_rel[3]; + float dial_plane[4]; + float ray_co[3], ray_no[3]; + float ray_lambda; + + plane_from_point_normal_v3(dial_plane, mpr->matrix_basis[3], axis_vec); + + if (!ED_view3d_win_to_ray(depsgraph, ar, v3d, inter->init_mval, ray_co, ray_no, false) || + !isect_ray_plane_v3(ray_co, ray_no, dial_plane, &ray_lambda, false)) + { + goto fail; + } + madd_v3_v3v3fl(proj_mval_init_rel, ray_co, ray_no, ray_lambda); + sub_v3_v3(proj_mval_init_rel, mpr->matrix_basis[3]); + + if (!ED_view3d_win_to_ray(depsgraph, ar, v3d, mval, ray_co, ray_no, false) || + !isect_ray_plane_v3(ray_co, ray_no, dial_plane, &ray_lambda, false)) + { + goto fail; + } + madd_v3_v3v3fl(proj_mval_new_rel, ray_co, ray_no, ray_lambda); + sub_v3_v3(proj_mval_new_rel, mpr->matrix_basis[3]); + + const int draw_options = RNA_enum_get(mpr->ptr, "draw_options"); + + /* Start direction from mouse or set by user */ + const float *proj_init_rel = + (draw_options & ED_MANIPULATOR_DIAL_DRAW_FLAG_ANGLE_START_Y) ? + mpr->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->last_angle < 0.0f) && + (fabsf(inter->last_angle) > (float)M_PI_2)) + { + if (inter->last_angle < 0.0f) + inter->rotations--; + else + inter->rotations++; + } + inter->last_angle = delta; + + *r_start = start; + *r_delta = fmod(delta + 2.0f * (float)M_PI * inter->rotations, 2 * (float)M_PI); + return; + + /* If we can't project (unlikely). */ +fail: + *r_start = 0.0; + *r_delta = 0.0; +} + +static void dial_draw_intern( + const bContext *C, wmManipulator *mpr, + const bool select, const bool highlight, float clip_plane[4]) +{ + float matrix_basis_adjust[4][4]; + float matrix_final[4][4]; + float color[4]; + + BLI_assert(CTX_wm_area(C)->spacetype == SPACE_VIEW3D); + + manipulator_color_get(mpr, highlight, color); + + dial_calc_matrix(mpr, matrix_basis_adjust); + + WM_manipulator_calc_matrix_final_params( + mpr, &((struct WM_ManipulatorMatrixParams) { + .matrix_basis = (void *)matrix_basis_adjust, + }), matrix_final); + + gpuPushMatrix(); + gpuMultMatrix(matrix_final); + + /* draw rotation indicator arc first */ + if ((mpr->flag & WM_MANIPULATOR_DRAW_VALUE) && + (mpr->state & WM_MANIPULATOR_STATE_MODAL)) + { + const float co_outer[4] = {0.0f, DIAL_WIDTH, 0.0f}; /* coordinate at which the arc drawing will be started */ + + DialInteraction *inter = mpr->interaction_data; + + /* XXX, View3D rotation manipulator doesn't call modal. */ + if (!WM_manipulator_target_property_is_valid_any(mpr)) { + wmWindow *win = CTX_wm_window(C); + manipulator_dial_modal((bContext *)C, mpr, win->eventstate, 0); + } + + float angle_ofs = inter->output.angle_ofs; + float angle_delta = inter->output.angle_delta; + + /* draw! */ + for (int i = 0; i < 2; i++) { + glDisable(GL_POLYGON_SMOOTH); + dial_ghostarc_draw(mpr, angle_ofs, angle_delta, (const float[4]){0.8f, 0.8f, 0.8f, 0.4f}); + glEnable(GL_POLYGON_SMOOTH); + + dial_ghostarc_draw_helpline(angle_ofs, co_outer, color); /* starting position */ + dial_ghostarc_draw_helpline(angle_ofs + angle_delta, co_outer, color); /* starting position + current value */ + + if (i == 0) { + const int draw_options = RNA_enum_get(mpr->ptr, "draw_options"); + if ((draw_options & ED_MANIPULATOR_DIAL_DRAW_FLAG_ANGLE_MIRROR) == 0) { + break; + } + } + + angle_ofs += (float)M_PI; + } + } + + /* draw actual dial manipulator */ + dial_geom_draw(mpr, color, select, matrix_basis_adjust, clip_plane); + + gpuPopMatrix(); +} + +static void manipulator_dial_draw_select(const bContext *C, wmManipulator *mpr, int select_id) +{ + float clip_plane_buf[4]; + const int draw_options = RNA_enum_get(mpr->ptr, "draw_options"); + float *clip_plane = (draw_options & ED_MANIPULATOR_DIAL_DRAW_FLAG_CLIP) ? clip_plane_buf : NULL; + + /* enable clipping if needed */ + 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], mpr->matrix_basis[3]); + clip_plane[3] += DIAL_CLIP_BIAS * mpr->scale_final; + glEnable(GL_CLIP_DISTANCE0); + } + + GPU_select_load_id(select_id); + dial_draw_intern(C, mpr, true, false, clip_plane); + + if (clip_plane) { + glDisable(GL_CLIP_DISTANCE0); + } +} + +static void manipulator_dial_draw(const bContext *C, wmManipulator *mpr) +{ + const bool is_modal = mpr->state & WM_MANIPULATOR_STATE_MODAL; + const bool is_highlight = (mpr->state & WM_MANIPULATOR_STATE_HIGHLIGHT) != 0; + float clip_plane_buf[4]; + const int draw_options = RNA_enum_get(mpr->ptr, "draw_options"); + float *clip_plane = (!is_modal && (draw_options & ED_MANIPULATOR_DIAL_DRAW_FLAG_CLIP)) ? clip_plane_buf : NULL; + + /* enable clipping if needed */ + 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], mpr->matrix_basis[3]); + clip_plane[3] += DIAL_CLIP_BIAS * mpr->scale_final; + + glEnable(GL_CLIP_DISTANCE0); + } + + glEnable(GL_BLEND); + dial_draw_intern(C, mpr, false, is_highlight, clip_plane); + glDisable(GL_BLEND); + + if (clip_plane) { + glDisable(GL_CLIP_DISTANCE0); + } +} + +static int manipulator_dial_modal( + bContext *C, wmManipulator *mpr, const wmEvent *event, + eWM_ManipulatorTweak UNUSED(tweak_flag)) +{ + const float co_outer[4] = {0.0f, DIAL_WIDTH, 0.0f}; /* coordinate at which the arc drawing will be started */ + float angle_ofs, angle_delta; + + float matrix[4][4]; + + dial_calc_matrix(mpr, matrix); + + dial_ghostarc_get_angles( + CTX_data_depsgraph(C), + mpr, event, CTX_wm_region(C), CTX_wm_view3d(C), matrix, co_outer, &angle_ofs, &angle_delta); + + DialInteraction *inter = mpr->interaction_data; + + inter->output.angle_delta = angle_delta; + inter->output.angle_ofs = angle_ofs; + + /* set the property for the operator and call its modal function */ + wmManipulatorProperty *mpr_prop = WM_manipulator_target_property_find(mpr, "offset"); + if (WM_manipulator_target_property_is_valid(mpr_prop)) { + WM_manipulator_target_property_value_set(C, mpr, mpr_prop, inter->init_prop_angle + angle_delta); + } + return OPERATOR_RUNNING_MODAL; +} + + +static void manipulator_dial_setup(wmManipulator *mpr) +{ + const float dir_default[3] = {0.0f, 0.0f, 1.0f}; + + /* defaults */ + copy_v3_v3(mpr->matrix_basis[2], dir_default); +} + +static int manipulator_dial_invoke( + bContext *UNUSED(C), wmManipulator *mpr, const wmEvent *event) +{ + DialInteraction *inter = MEM_callocN(sizeof(DialInteraction), __func__); + + inter->init_mval[0] = event->mval[0]; + inter->init_mval[1] = event->mval[1]; + + wmManipulatorProperty *mpr_prop = WM_manipulator_target_property_find(mpr, "offset"); + if (WM_manipulator_target_property_is_valid(mpr_prop)) { + inter->init_prop_angle = WM_manipulator_target_property_value_get(mpr, mpr_prop); + } + + mpr->interaction_data = inter; + + return OPERATOR_RUNNING_MODAL; +} + +/* -------------------------------------------------------------------- */ +/** \name Dial Manipulator API + * + * \{ */ + +static void MANIPULATOR_WT_dial_3d(wmManipulatorType *wt) +{ + /* identifiers */ + wt->idname = "MANIPULATOR_WT_dial_3d"; + + /* api callbacks */ + wt->draw = manipulator_dial_draw; + wt->draw_select = manipulator_dial_draw_select; + wt->setup = manipulator_dial_setup; + wt->invoke = manipulator_dial_invoke; + wt->modal = manipulator_dial_modal; + + wt->struct_size = sizeof(wmManipulator); + + /* rna */ + static EnumPropertyItem rna_enum_draw_options[] = { + {ED_MANIPULATOR_DIAL_DRAW_FLAG_CLIP, "CLIP", 0, "Clipped", ""}, + {ED_MANIPULATOR_DIAL_DRAW_FLAG_FILL, "FILL", 0, "Filled", ""}, + {ED_MANIPULATOR_DIAL_DRAW_FLAG_ANGLE_MIRROR, "ANGLE_MIRROR", 0, "Angle Mirror", ""}, + {ED_MANIPULATOR_DIAL_DRAW_FLAG_ANGLE_START_Y, "ANGLE_START_Y", 0, "Angle Start Y", ""}, + {0, NULL, 0, NULL, NULL} + }; + RNA_def_enum_flag(wt->srna, "draw_options", rna_enum_draw_options, 0, "Draw Options", ""); + + WM_manipulatortype_target_property_def(wt, "offset", PROP_FLOAT, 1); +} + +void ED_manipulatortypes_dial_3d(void) +{ + WM_manipulatortype_append(MANIPULATOR_WT_dial_3d); +} + +/** \} */ diff --git a/source/blender/editors/manipulator_library/manipulator_types/grab3d_manipulator.c b/source/blender/editors/manipulator_library/manipulator_types/grab3d_manipulator.c new file mode 100644 index 00000000000..e2d1979b7a6 --- /dev/null +++ b/source/blender/editors/manipulator_library/manipulator_types/grab3d_manipulator.c @@ -0,0 +1,374 @@ +/* + * ***** 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 grab3d_manipulator.c + * \ingroup wm + * + * \name Grab Manipulator + * + * 3D Manipulator, also works in 2D views. + * + * \brief Simple manipulator to grab and translate. + * + * - `matrix[0]` is derived from Y and Z. + * - `matrix[1]` currently not used. + * - `matrix[2]` is the widget direction (for all manipulators). + * + */ + +#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 "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_manipulator_library.h" + +/* own includes */ +#include "../manipulator_geometry.h" +#include "../manipulator_library_intern.h" + +typedef struct GrabManipulator3D { + wmManipulator manipulator; + /* Added to 'matrix_basis' when calculating the matrix. */ + float prop_co[3]; +} GrabManipulator3D; + +static void manipulator_grab_matrix_basis_get(const wmManipulator *mpr, float r_matrix[4][4]) +{ + GrabManipulator3D *grab = (GrabManipulator3D *)mpr; + + copy_m4_m4(r_matrix, grab->manipulator.matrix_basis); + add_v3_v3(r_matrix[3], grab->prop_co); +} + +static int manipulator_grab_modal( + bContext *C, wmManipulator *mpr, const wmEvent *event, + eWM_ManipulatorTweak tweak_flag); + +typedef struct GrabInteraction { + float init_mval[2]; + + /* only for when using properties */ + float init_prop_co[3]; + + float init_matrix_final[4][4]; +} GrabInteraction; + +#define DIAL_RESOLUTION 32 + +/* -------------------------------------------------------------------- */ + +static void grab_geom_draw( + const wmManipulator *mpr, const float color[4], const bool select, const int draw_options) +{ +#ifdef USE_MANIPULATOR_CUSTOM_DIAL + UNUSED_VARS(grab3d, col, axis_modal_mat); + wm_manipulator_geometryinfo_draw(&wm_manipulator_geom_data_grab3d, select); +#else + const int draw_style = RNA_enum_get(mpr->ptr, "draw_style"); + const bool filled = (draw_options & ED_MANIPULATOR_GRAB_DRAW_FLAG_FILL) != 0; + + glLineWidth(mpr->line_width); + + Gwn_VertFormat *format = immVertexFormat(); + uint pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT); + + immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); + + immUniformColor4fv(color); + + if (draw_style == ED_MANIPULATOR_GRAB_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_MANIPULATOR_GRAB_STYLE_CROSS_2D) { + immBegin(GWN_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 grab3d_get_translate( + const wmManipulator *mpr, const wmEvent *event, const ARegion *ar, + float co_delta[3]) +{ + GrabInteraction *inter = mpr->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, mpr->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, mpr->matrix_space); + invert_m3(matrix_space_inv); + mul_m3_v3(matrix_space_inv, co_delta); +} + +static void grab3d_draw_intern( + const bContext *C, wmManipulator *mpr, + const bool select, const bool highlight) +{ + GrabInteraction *inter = mpr->interaction_data; + const int draw_options = RNA_enum_get(mpr->ptr, "draw_options"); + const bool align_view = (draw_options & ED_MANIPULATOR_GRAB_DRAW_FLAG_ALIGN_VIEW) != 0; + float color[4]; + float matrix_final[4][4]; + float matrix_align[4][4]; + + manipulator_color_get(mpr, highlight, color); + WM_manipulator_calc_matrix_final(mpr, matrix_final); + + gpuPushMatrix(); + gpuMultMatrix(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); + gpuMultMatrix(matrix_align); + } + + glEnable(GL_BLEND); + grab_geom_draw(mpr, color, select, draw_options); + glDisable(GL_BLEND); + gpuPopMatrix(); + + if (mpr->interaction_data) { + gpuPushMatrix(); + gpuMultMatrix(inter->init_matrix_final); + + if (align_view) { + gpuMultMatrix(matrix_align); + } + + glEnable(GL_BLEND); + grab_geom_draw(mpr, (const float[4]){0.5f, 0.5f, 0.5f, 0.5f}, select, draw_options); + glDisable(GL_BLEND); + gpuPopMatrix(); + } +} + +static void manipulator_grab_draw_select(const bContext *C, wmManipulator *mpr, int select_id) +{ + GPU_select_load_id(select_id); + grab3d_draw_intern(C, mpr, true, false); +} + +static void manipulator_grab_draw(const bContext *C, wmManipulator *mpr) +{ + const bool is_modal = mpr->state & WM_MANIPULATOR_STATE_MODAL; + const bool is_highlight = (mpr->state & WM_MANIPULATOR_STATE_HIGHLIGHT) != 0; + + (void)is_modal; + + glEnable(GL_BLEND); + grab3d_draw_intern(C, mpr, false, is_highlight); + glDisable(GL_BLEND); +} + +static int manipulator_grab_modal( + bContext *C, wmManipulator *mpr, const wmEvent *event, + eWM_ManipulatorTweak UNUSED(tweak_flag)) +{ + GrabManipulator3D *grab = (GrabManipulator3D *)mpr; + GrabInteraction *inter = mpr->interaction_data; + ARegion *ar = CTX_wm_region(C); + + float prop_delta[3]; + if (CTX_wm_area(C)->spacetype == SPACE_VIEW3D) { + grab3d_get_translate(mpr, event, ar, prop_delta); + } + else { + float mval_proj_init[2], mval_proj_curr[2]; + if ((manipulator_window_project_2d( + C, mpr, inter->init_mval, 2, false, mval_proj_init) == false) || + (manipulator_window_project_2d( + C, mpr, (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; + } + add_v3_v3v3(grab->prop_co, inter->init_prop_co, prop_delta); + + /* set the property for the operator and call its modal function */ + wmManipulatorProperty *mpr_prop = WM_manipulator_target_property_find(mpr, "offset"); + if (WM_manipulator_target_property_is_valid(mpr_prop)) { + WM_manipulator_target_property_value_set_array(C, mpr, mpr_prop, grab->prop_co); + } + else { + zero_v3(grab->prop_co); + } + + ED_region_tag_redraw(ar); + + return OPERATOR_RUNNING_MODAL; +} + +static int manipulator_grab_invoke( + bContext *UNUSED(C), wmManipulator *mpr, const wmEvent *event) +{ + GrabInteraction *inter = MEM_callocN(sizeof(GrabInteraction), __func__); + + inter->init_mval[0] = event->mval[0]; + inter->init_mval[1] = event->mval[1]; + +#if 0 + copy_v3_v3(inter->init_prop_co, grab->prop_co); +#else + wmManipulatorProperty *mpr_prop = WM_manipulator_target_property_find(mpr, "offset"); + if (WM_manipulator_target_property_is_valid(mpr_prop)) { + WM_manipulator_target_property_value_get_array(mpr, mpr_prop, inter->init_prop_co); + } +#endif + + WM_manipulator_calc_matrix_final(mpr, inter->init_matrix_final); + + mpr->interaction_data = inter; + + return OPERATOR_RUNNING_MODAL; +} + + +static int manipulator_grab_test_select( + bContext *C, wmManipulator *mpr, const wmEvent *event) +{ + float point_local[2]; + + if (manipulator_window_project_2d( + C, mpr, (const float[2]){UNPACK2(event->mval)}, 2, true, point_local) == false) + { + return -1; + } + + /* The 'mpr->scale_final' is already applied when projecting. */ + if (len_squared_v2(point_local) < 1.0f) { + return 0; + } + + return -1; +} + +static void manipulator_grab_property_update(wmManipulator *mpr, wmManipulatorProperty *mpr_prop) +{ + GrabManipulator3D *grab = (GrabManipulator3D *)mpr; + if (WM_manipulator_target_property_is_valid(mpr_prop)) { + WM_manipulator_target_property_value_get_array(mpr, mpr_prop, grab->prop_co); + } + else { + zero_v3(grab->prop_co); + } +} + +static int manipulator_grab_cursor_get(wmManipulator *UNUSED(mpr)) +{ + return BC_NSEW_SCROLLCURSOR; +} + +/* -------------------------------------------------------------------- */ +/** \name Grab Manipulator API + * + * \{ */ + +static void MANIPULATOR_WT_grab_3d(wmManipulatorType *wt) +{ + /* identifiers */ + wt->idname = "MANIPULATOR_WT_grab_3d"; + + /* api callbacks */ + wt->draw = manipulator_grab_draw; + wt->draw_select = manipulator_grab_draw_select; + wt->test_select = manipulator_grab_test_select; + wt->matrix_basis_get = manipulator_grab_matrix_basis_get; + wt->invoke = manipulator_grab_invoke; + wt->property_update = manipulator_grab_property_update; + wt->modal = manipulator_grab_modal; + wt->cursor_get = manipulator_grab_cursor_get; + + wt->struct_size = sizeof(GrabManipulator3D); + + /* rna */ + static EnumPropertyItem rna_enum_draw_style[] = { + {ED_MANIPULATOR_GRAB_STYLE_RING_2D, "RING_2D", 0, "Ring", ""}, + {ED_MANIPULATOR_GRAB_STYLE_CROSS_2D, "CROSS_2D", 0, "Ring", ""}, + {0, NULL, 0, NULL, NULL} + }; + static EnumPropertyItem rna_enum_draw_options[] = { + {ED_MANIPULATOR_GRAB_DRAW_FLAG_FILL, "FILL", 0, "Filled", ""}, + {ED_MANIPULATOR_GRAB_DRAW_FLAG_ALIGN_VIEW, "ALIGN_VIEW", 0, "Align View", ""}, + {0, NULL, 0, NULL, NULL} + }; + + RNA_def_enum(wt->srna, "draw_style", rna_enum_draw_style, ED_MANIPULATOR_GRAB_STYLE_RING_2D, "Draw Style", ""); + RNA_def_enum_flag(wt->srna, "draw_options", rna_enum_draw_options, 0, "Draw Options", ""); + + WM_manipulatortype_target_property_def(wt, "offset", PROP_FLOAT, 3); +} + +void ED_manipulatortypes_grab_3d(void) +{ + WM_manipulatortype_append(MANIPULATOR_WT_grab_3d); +} + +/** \} */ // Grab Manipulator API diff --git a/source/blender/editors/manipulator_library/manipulator_types/primitive3d_manipulator.c b/source/blender/editors/manipulator_library/manipulator_types/primitive3d_manipulator.c new file mode 100644 index 00000000000..531cf742e6f --- /dev/null +++ b/source/blender/editors/manipulator_library/manipulator_types/primitive3d_manipulator.c @@ -0,0 +1,190 @@ +/* + * ***** 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_manipulator.c + * \ingroup wm + * + * \name Primitive Manipulator + * + * 3D Manipulator + * + * \brief Manipulator 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 "RNA_access.h" +#include "RNA_define.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "ED_manipulator_library.h" + +/* own includes */ +#include "../manipulator_library_intern.h" + +static float verts_plane[4][3] = { + {-1, -1, 0}, + { 1, -1, 0}, + { 1, 1, 0}, + {-1, 1, 0}, +}; + + +/* -------------------------------------------------------------------- */ + +static void manipulator_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_MANIPULATOR_PRIMITIVE_STYLE_PLANE) { + verts = verts_plane; + vert_count = ARRAY_SIZE(verts_plane); + } + + if (vert_count > 0) { + uint pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT); + immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); + wm_manipulator_vec_draw(col_inner, verts, vert_count, pos, GWN_PRIM_TRI_FAN); + wm_manipulator_vec_draw(col_outer, verts, vert_count, pos, GWN_PRIM_LINE_LOOP); + immUnbindProgram(); + } +} + +static void manipulator_primitive_draw_intern( + wmManipulator *mpr, 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(mpr->ptr, "draw_style"); + + manipulator_color_get(mpr, highlight, color_outer); + copy_v4_v4(color_inner, color_outer); + color_inner[3] *= 0.5f; + + WM_manipulator_calc_matrix_final(mpr, matrix_final); + + gpuPushMatrix(); + gpuMultMatrix(matrix_final); + + glEnable(GL_BLEND); + manipulator_primitive_draw_geom(color_inner, color_outer, draw_style); + glDisable(GL_BLEND); + + gpuPopMatrix(); + + if (mpr->interaction_data) { + ManipulatorInteraction *inter = mpr->interaction_data; + + copy_v4_fl(color_inner, 0.5f); + copy_v3_fl(color_outer, 0.5f); + color_outer[3] = 0.8f; + + gpuPushMatrix(); + gpuMultMatrix(inter->init_matrix_final); + + glEnable(GL_BLEND); + manipulator_primitive_draw_geom(color_inner, color_outer, draw_style); + glDisable(GL_BLEND); + + gpuPopMatrix(); + } +} + +static void manipulator_primitive_draw_select( + const bContext *UNUSED(C), wmManipulator *mpr, + int select_id) +{ + GPU_select_load_id(select_id); + manipulator_primitive_draw_intern(mpr, true, false); +} + +static void manipulator_primitive_draw(const bContext *UNUSED(C), wmManipulator *mpr) +{ + manipulator_primitive_draw_intern( + mpr, false, + (mpr->state & WM_MANIPULATOR_STATE_HIGHLIGHT)); +} + +static void manipulator_primitive_setup(wmManipulator *mpr) +{ + mpr->flag |= WM_MANIPULATOR_DRAW_MODAL; +} + +static int manipulator_primitive_invoke( + bContext *UNUSED(C), wmManipulator *mpr, const wmEvent *UNUSED(event)) +{ + ManipulatorInteraction *inter = MEM_callocN(sizeof(ManipulatorInteraction), __func__); + + WM_manipulator_calc_matrix_final(mpr, inter->init_matrix_final); + + mpr->interaction_data = inter; + + return OPERATOR_RUNNING_MODAL; +} + +/* -------------------------------------------------------------------- */ +/** \name Primitive Manipulator API + * + * \{ */ + +static void MANIPULATOR_WT_primitive_3d(wmManipulatorType *wt) +{ + /* identifiers */ + wt->idname = "MANIPULATOR_WT_primitive_3d"; + + /* api callbacks */ + wt->draw = manipulator_primitive_draw; + wt->draw_select = manipulator_primitive_draw_select; + wt->setup = manipulator_primitive_setup; + wt->invoke = manipulator_primitive_invoke; + + wt->struct_size = sizeof(wmManipulator); + + static EnumPropertyItem rna_enum_draw_style[] = { + {ED_MANIPULATOR_PRIMITIVE_STYLE_PLANE, "PLANE", 0, "Plane", ""}, + {0, NULL, 0, NULL, NULL} + }; + RNA_def_enum(wt->srna, "draw_style", rna_enum_draw_style, ED_MANIPULATOR_PRIMITIVE_STYLE_PLANE, "Draw Style", ""); +} + +void ED_manipulatortypes_primitive_3d(void) +{ + WM_manipulatortype_append(MANIPULATOR_WT_primitive_3d); +} + +/** \} */ |