Cleanup: rename the curveInX etc bbone DNA fields to curve_in_x etc.
[blender.git] / source / blender / editors / space_view3d / view3d_gizmo_armature.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16
17 /** \file
18  * \ingroup spview3d
19  */
20
21 #include "BLI_blenlib.h"
22 #include "BLI_math.h"
23 #include "BLI_utildefines.h"
24
25 #include "BKE_armature.h"
26 #include "BKE_action.h"
27 #include "BKE_context.h"
28 #include "BKE_object.h"
29
30 #include "DNA_object_types.h"
31 #include "DNA_armature_types.h"
32
33 #include "ED_armature.h"
34 #include "ED_screen.h"
35 #include "ED_gizmo_library.h"
36
37 #include "UI_resources.h"
38
39 #include "MEM_guardedalloc.h"
40
41 #include "RNA_access.h"
42
43 #include "WM_api.h"
44 #include "WM_types.h"
45
46 #include "view3d_intern.h" /* own include */
47
48 /* -------------------------------------------------------------------- */
49 /** \name Armature Spline Gizmo
50  *
51  * \{ */
52
53 /*
54  * TODO(campbell): Current conversion is a approximation (usable not correct),
55  * we'll need to take the next/previous bones into account to get the tangent directions.
56  * First last matrices from 'BKE_pchan_bbone_spline_setup' are close but also not quite accurate
57  * since they're not at either end-points on the curve.
58  *
59  * Likely we'll need a function especially to get the first/last orientations.
60  */
61
62 #define BBONE_SCALE_Y 3.0f
63
64 struct BoneSplineHandle {
65   wmGizmo *gizmo;
66   bPoseChannel *pchan;
67   /* We could remove, keep since at the moment for checking the conversion. */
68   float co[3];
69   int index;
70 };
71
72 struct BoneSplineWidgetGroup {
73   struct BoneSplineHandle handles[2];
74 };
75
76 static void gizmo_bbone_offset_get(const wmGizmo *UNUSED(gz),
77                                    wmGizmoProperty *gz_prop,
78                                    void *value_p)
79 {
80   struct BoneSplineHandle *bh = gz_prop->custom_func.user_data;
81   bPoseChannel *pchan = bh->pchan;
82
83   float *value = value_p;
84   BLI_assert(gz_prop->type->array_length == 3);
85
86   if (bh->index == 0) {
87     bh->co[1] = pchan->bone->ease1 / BBONE_SCALE_Y;
88     bh->co[0] = pchan->curve_in_x;
89     bh->co[2] = pchan->curve_in_y;
90   }
91   else {
92     bh->co[1] = -pchan->bone->ease2 / BBONE_SCALE_Y;
93     bh->co[0] = pchan->curve_out_x;
94     bh->co[2] = pchan->curve_out_y;
95   }
96   copy_v3_v3(value, bh->co);
97 }
98
99 static void gizmo_bbone_offset_set(const wmGizmo *UNUSED(gz),
100                                    wmGizmoProperty *gz_prop,
101                                    const void *value_p)
102 {
103   struct BoneSplineHandle *bh = gz_prop->custom_func.user_data;
104   bPoseChannel *pchan = bh->pchan;
105
106   const float *value = value_p;
107
108   BLI_assert(gz_prop->type->array_length == 3);
109   copy_v3_v3(bh->co, value);
110
111   if (bh->index == 0) {
112     pchan->bone->ease1 = max_ff(0.0f, bh->co[1] * BBONE_SCALE_Y);
113     pchan->curve_in_x = bh->co[0];
114     pchan->curve_in_y = bh->co[2];
115   }
116   else {
117     pchan->bone->ease2 = max_ff(0.0f, -bh->co[1] * BBONE_SCALE_Y);
118     pchan->curve_out_x = bh->co[0];
119     pchan->curve_out_y = bh->co[2];
120   }
121 }
122
123 static bool WIDGETGROUP_armature_spline_poll(const bContext *C, wmGizmoGroupType *UNUSED(gzgt))
124 {
125   View3D *v3d = CTX_wm_view3d(C);
126   if (v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_CONTEXT)) {
127     return false;
128   }
129
130   ViewLayer *view_layer = CTX_data_view_layer(C);
131   Base *base = BASACT(view_layer);
132   if (base && BASE_SELECTABLE(v3d, base)) {
133     Object *ob = BKE_object_pose_armature_get(base->object);
134     if (ob) {
135       const bArmature *arm = ob->data;
136       if (arm->drawtype == ARM_B_BONE) {
137         bPoseChannel *pchan = BKE_pose_channel_active(ob);
138         if (pchan && pchan->bone->segments > 1) {
139           return true;
140         }
141       }
142     }
143   }
144   return false;
145 }
146
147 static void WIDGETGROUP_armature_spline_setup(const bContext *C, wmGizmoGroup *gzgroup)
148 {
149   ViewLayer *view_layer = CTX_data_view_layer(C);
150   Object *ob = BKE_object_pose_armature_get(OBACT(view_layer));
151   bPoseChannel *pchan = BKE_pose_channel_active(ob);
152
153   const wmGizmoType *gzt_move = WM_gizmotype_find("GIZMO_GT_move_3d", true);
154
155   struct BoneSplineWidgetGroup *bspline_group = MEM_callocN(sizeof(struct BoneSplineWidgetGroup),
156                                                             __func__);
157   gzgroup->customdata = bspline_group;
158
159   /* Handles */
160   for (int i = 0; i < ARRAY_SIZE(bspline_group->handles); i++) {
161     wmGizmo *gz;
162     gz = bspline_group->handles[i].gizmo = WM_gizmo_new_ptr(gzt_move, gzgroup, NULL);
163     RNA_enum_set(gz->ptr, "draw_style", ED_GIZMO_MOVE_STYLE_RING_2D);
164     RNA_enum_set(gz->ptr,
165                  "draw_options",
166                  ED_GIZMO_MOVE_DRAW_FLAG_FILL | ED_GIZMO_MOVE_DRAW_FLAG_ALIGN_VIEW);
167     WM_gizmo_set_flag(gz, WM_GIZMO_DRAW_VALUE, true);
168
169     UI_GetThemeColor3fv(TH_GIZMO_PRIMARY, gz->color);
170     UI_GetThemeColor3fv(TH_GIZMO_HI, gz->color_hi);
171
172     gz->scale_basis = 0.06f;
173
174     if (i == 0) {
175       copy_v3_v3(gz->matrix_basis[3], pchan->loc);
176     }
177   }
178 }
179
180 static void WIDGETGROUP_armature_spline_refresh(const bContext *C, wmGizmoGroup *gzgroup)
181 {
182   ViewLayer *view_layer = CTX_data_view_layer(C);
183   Object *ob = BKE_object_pose_armature_get(OBACT(view_layer));
184
185   if (!gzgroup->customdata) {
186     return;
187   }
188
189   struct BoneSplineWidgetGroup *bspline_group = gzgroup->customdata;
190   bPoseChannel *pchan = BKE_pose_channel_active(ob);
191
192   /* Handles */
193   for (int i = 0; i < ARRAY_SIZE(bspline_group->handles); i++) {
194     wmGizmo *gz = bspline_group->handles[i].gizmo;
195     bspline_group->handles[i].pchan = pchan;
196     bspline_group->handles[i].index = i;
197
198     float mat[4][4];
199     mul_m4_m4m4(mat, ob->obmat, (i == 0) ? pchan->disp_mat : pchan->disp_tail_mat);
200     copy_m4_m4(gz->matrix_space, mat);
201
202     /* need to set property here for undo. TODO would prefer to do this in _init */
203     WM_gizmo_target_property_def_func(gz,
204                                       "offset",
205                                       &(const struct wmGizmoPropertyFnParams){
206                                           .value_get_fn = gizmo_bbone_offset_get,
207                                           .value_set_fn = gizmo_bbone_offset_set,
208                                           .range_get_fn = NULL,
209                                           .user_data = &bspline_group->handles[i],
210                                       });
211   }
212 }
213
214 void VIEW3D_GGT_armature_spline(wmGizmoGroupType *gzgt)
215 {
216   gzgt->name = "Armature Spline Widgets";
217   gzgt->idname = "VIEW3D_GGT_armature_spline";
218
219   gzgt->flag = (WM_GIZMOGROUPTYPE_PERSISTENT | WM_GIZMOGROUPTYPE_3D);
220
221   gzgt->poll = WIDGETGROUP_armature_spline_poll;
222   gzgt->setup = WIDGETGROUP_armature_spline_setup;
223   gzgt->refresh = WIDGETGROUP_armature_spline_refresh;
224 }
225
226 /** \} */