Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / space_view3d / view3d_gizmo_camera.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file blender/editors/space_view3d/view3d_gizmo_camera.c
22  *  \ingroup spview3d
23  */
24
25
26 #include "BLI_blenlib.h"
27 #include "BLI_math.h"
28 #include "BLI_utildefines.h"
29
30 #include "BKE_camera.h"
31 #include "BKE_context.h"
32
33 #include "DNA_object_types.h"
34 #include "DNA_camera_types.h"
35
36 #include "ED_armature.h"
37 #include "ED_screen.h"
38 #include "ED_gizmo_library.h"
39
40 #include "UI_resources.h"
41
42 #include "MEM_guardedalloc.h"
43
44 #include "RNA_access.h"
45
46 #include "WM_api.h"
47 #include "WM_types.h"
48 #include "WM_message.h"
49
50 #include "view3d_intern.h"  /* own include */
51
52
53 /* -------------------------------------------------------------------- */
54
55 /** \name Camera Gizmos
56  * \{ */
57
58 struct CameraWidgetGroup {
59         wmGizmo *dop_dist;
60         wmGizmo *focal_len;
61         wmGizmo *ortho_scale;
62 };
63
64 static bool WIDGETGROUP_camera_poll(const bContext *C, wmGizmoGroupType *UNUSED(gzgt))
65 {
66         View3D *v3d = CTX_wm_view3d(C);
67         if ((v3d->flag2 & V3D_RENDER_OVERRIDE) ||
68             (v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_CONTEXT)))
69         {
70                 return false;
71         }
72
73         Object *ob = CTX_data_active_object(C);
74         if (ob && ob->type == OB_CAMERA) {
75                 Camera *camera = ob->data;
76                 /* TODO: support overrides. */
77                 if (camera->id.lib == NULL) {
78                         return true;
79                 }
80         }
81         return false;
82 }
83
84 static void WIDGETGROUP_camera_setup(const bContext *C, wmGizmoGroup *gzgroup)
85 {
86         Object *ob = CTX_data_active_object(C);
87         float dir[3];
88
89         const wmGizmoType *gzt_arrow = WM_gizmotype_find("GIZMO_GT_arrow_3d", true);
90
91         struct CameraWidgetGroup *cagzgroup = MEM_callocN(sizeof(struct CameraWidgetGroup), __func__);
92         gzgroup->customdata = cagzgroup;
93
94         negate_v3_v3(dir, ob->obmat[2]);
95
96         /* dof distance */
97         {
98                 wmGizmo *gz;
99                 gz = cagzgroup->dop_dist = WM_gizmo_new_ptr(gzt_arrow, gzgroup, NULL);
100                 RNA_enum_set(gz->ptr, "draw_style",  ED_GIZMO_ARROW_STYLE_CROSS);
101                 WM_gizmo_set_flag(gz, WM_GIZMO_DRAW_HOVER, true);
102
103                 UI_GetThemeColor3fv(TH_GIZMO_A, gz->color);
104                 UI_GetThemeColor3fv(TH_GIZMO_HI, gz->color_hi);
105         }
106
107         /* focal length
108          * - logic/calculations are similar to BKE_camera_view_frame_ex, better keep in sync */
109         {
110                 wmGizmo *gz;
111                 gz = cagzgroup->focal_len = WM_gizmo_new_ptr(gzt_arrow, gzgroup, NULL);
112                 gz->flag |= WM_GIZMO_DRAW_NO_SCALE;
113                 RNA_enum_set(gz->ptr, "draw_style",  ED_GIZMO_ARROW_STYLE_CONE);
114                 RNA_enum_set(gz->ptr, "transform",  ED_GIZMO_ARROW_XFORM_FLAG_CONSTRAINED);
115
116                 UI_GetThemeColor3fv(TH_GIZMO_PRIMARY, gz->color);
117                 UI_GetThemeColor3fv(TH_GIZMO_HI, gz->color_hi);
118
119                 gz = cagzgroup->ortho_scale = WM_gizmo_new_ptr(gzt_arrow, gzgroup, NULL);
120                 gz->flag |= WM_GIZMO_DRAW_NO_SCALE;
121                 RNA_enum_set(gz->ptr, "draw_style",  ED_GIZMO_ARROW_STYLE_CONE);
122                 RNA_enum_set(gz->ptr, "transform",  ED_GIZMO_ARROW_XFORM_FLAG_CONSTRAINED);
123
124                 UI_GetThemeColor3fv(TH_GIZMO_PRIMARY, gz->color);
125                 UI_GetThemeColor3fv(TH_GIZMO_HI, gz->color_hi);
126         }
127 }
128
129 static void WIDGETGROUP_camera_refresh(const bContext *C, wmGizmoGroup *gzgroup)
130 {
131         if (!gzgroup->customdata)
132                 return;
133
134         struct CameraWidgetGroup *cagzgroup = gzgroup->customdata;
135         Object *ob = CTX_data_active_object(C);
136         Camera *ca = ob->data;
137         PointerRNA camera_ptr;
138         float dir[3];
139
140         const float ob_scale_inv[3] = {
141                 1.0f / len_v3(ob->obmat[0]),
142                 1.0f / len_v3(ob->obmat[1]),
143                 1.0f / len_v3(ob->obmat[2]),
144         };
145         const float ob_scale_uniform_inv = (ob_scale_inv[0] + ob_scale_inv[1] + ob_scale_inv[2]) / 3.0f;
146
147         RNA_pointer_create(&ca->id, &RNA_Camera, ca, &camera_ptr);
148
149         negate_v3_v3(dir, ob->obmat[2]);
150
151         if (ca->flag & CAM_SHOWLIMITS) {
152                 WM_gizmo_set_matrix_location(cagzgroup->dop_dist, ob->obmat[3]);
153                 WM_gizmo_set_matrix_rotation_from_yz_axis(cagzgroup->dop_dist, ob->obmat[1], dir);
154                 WM_gizmo_set_scale(cagzgroup->dop_dist, ca->drawsize);
155                 WM_gizmo_set_flag(cagzgroup->dop_dist, WM_GIZMO_HIDDEN, false);
156
157                 /* need to set property here for undo. TODO would prefer to do this in _init */
158                 WM_gizmo_target_property_def_rna(cagzgroup->dop_dist, "offset", &camera_ptr, "dof_distance", -1);
159         }
160         else {
161                 WM_gizmo_set_flag(cagzgroup->dop_dist, WM_GIZMO_HIDDEN, true);
162         }
163
164         /* TODO - make focal length/ortho ob_scale_inv widget optional */
165         const Scene *scene = CTX_data_scene(C);
166         const float aspx = (float)scene->r.xsch * scene->r.xasp;
167         const float aspy = (float)scene->r.ysch * scene->r.yasp;
168         const bool is_ortho = (ca->type == CAM_ORTHO);
169         const int sensor_fit = BKE_camera_sensor_fit(ca->sensor_fit, aspx, aspy);
170         wmGizmo *widget = is_ortho ? cagzgroup->ortho_scale : cagzgroup->focal_len;
171         float scale_matrix;
172         if (true) {
173                 float offset[3];
174                 float aspect[2];
175
176                 WM_gizmo_set_flag(widget, WM_GIZMO_HIDDEN, false);
177                 WM_gizmo_set_flag(is_ortho ? cagzgroup->focal_len : cagzgroup->ortho_scale, WM_GIZMO_HIDDEN, true);
178
179
180                 /* account for lens shifting */
181                 offset[0] = ((ob->size[0] > 0.0f) ? -2.0f : 2.0f) * ca->shiftx;
182                 offset[1] = 2.0f * ca->shifty;
183                 offset[2] = 0.0f;
184
185                 /* get aspect */
186                 aspect[0] = (sensor_fit == CAMERA_SENSOR_FIT_HOR) ? 1.0f : aspx / aspy;
187                 aspect[1] = (sensor_fit == CAMERA_SENSOR_FIT_HOR) ? aspy / aspx : 1.0f;
188
189                 unit_m4(widget->matrix_basis);
190                 WM_gizmo_set_matrix_location(widget, ob->obmat[3]);
191                 WM_gizmo_set_matrix_rotation_from_yz_axis(widget, ob->obmat[1], dir);
192
193                 if (is_ortho) {
194                         scale_matrix = ca->ortho_scale * 0.5f;
195                 }
196                 else {
197                         scale_matrix = ca->drawsize / ob_scale_uniform_inv;
198                 }
199                 mul_v3_fl(widget->matrix_basis[0], scale_matrix);
200                 mul_v3_fl(widget->matrix_basis[1], scale_matrix);
201
202                 RNA_float_set_array(widget->ptr, "aspect", aspect);
203
204                 WM_gizmo_set_matrix_offset_location(widget, offset);
205         }
206
207         /* define & update properties */
208         {
209                 const char *propname = is_ortho ? "ortho_scale" : "lens";
210                 PropertyRNA *prop = RNA_struct_find_property(&camera_ptr, propname);
211                 const wmGizmoPropertyType *gz_prop_type = WM_gizmotype_target_property_find(widget->type, "offset");
212
213                 WM_gizmo_target_property_clear_rna_ptr(widget, gz_prop_type);
214
215                 float min, max, range;
216                 float step, precision;
217
218                 /* get property range */
219                 RNA_property_float_ui_range(&camera_ptr, prop, &min, &max, &step, &precision);
220                 range = max - min;
221
222                 ED_gizmo_arrow3d_set_range_fac(
223                         widget, is_ortho ?
224                         (ca->drawsize * range) :
225                         (scale_matrix * range /
226                          /* Half sensor, intentionally use sensor from camera and not calculated above. */
227                          (0.5f * ((ca->sensor_fit == CAMERA_SENSOR_FIT_HOR) ? ca->sensor_x : ca->sensor_x))));
228
229                 WM_gizmo_target_property_def_rna_ptr(widget, gz_prop_type, &camera_ptr, prop, -1);
230         }
231
232 }
233
234 static void WIDGETGROUP_camera_message_subscribe(
235         const bContext *C, wmGizmoGroup *gzgroup, struct wmMsgBus *mbus)
236 {
237         ARegion *ar = CTX_wm_region(C);
238         Object *ob = CTX_data_active_object(C);
239         Camera *ca = ob->data;
240
241         wmMsgSubscribeValue msg_sub_value_gz_tag_refresh = {
242                 .owner = ar,
243                 .user_data = gzgroup->parent_gzmap,
244                 .notify = WM_gizmo_do_msg_notify_tag_refresh,
245         };
246
247         {
248                 extern PropertyRNA rna_Camera_dof_distance;
249                 extern PropertyRNA rna_Camera_display_size;
250                 extern PropertyRNA rna_Camera_ortho_scale;
251                 extern PropertyRNA rna_Camera_sensor_fit;
252                 extern PropertyRNA rna_Camera_sensor_width;
253                 extern PropertyRNA rna_Camera_shift_x;
254                 extern PropertyRNA rna_Camera_shift_y;
255                 extern PropertyRNA rna_Camera_type;
256                 extern PropertyRNA rna_Camera_lens;
257                 const PropertyRNA *props[] = {
258                         &rna_Camera_dof_distance,
259                         &rna_Camera_display_size,
260                         &rna_Camera_ortho_scale,
261                         &rna_Camera_sensor_fit,
262                         &rna_Camera_sensor_width,
263                         &rna_Camera_shift_x,
264                         &rna_Camera_shift_y,
265                         &rna_Camera_type,
266                         &rna_Camera_lens,
267                 };
268
269                 PointerRNA idptr;
270                 RNA_id_pointer_create(&ca->id, &idptr);
271
272                 for (int i = 0; i < ARRAY_SIZE(props); i++) {
273                         WM_msg_subscribe_rna(mbus, &idptr, props[i], &msg_sub_value_gz_tag_refresh, __func__);
274                 }
275         }
276
277         /* Subscribe to render settings */
278         {
279                 WM_msg_subscribe_rna_anon_prop(mbus, RenderSettings, resolution_x, &msg_sub_value_gz_tag_refresh);
280                 WM_msg_subscribe_rna_anon_prop(mbus, RenderSettings, resolution_y, &msg_sub_value_gz_tag_refresh);
281                 WM_msg_subscribe_rna_anon_prop(mbus, RenderSettings, pixel_aspect_x, &msg_sub_value_gz_tag_refresh);
282                 WM_msg_subscribe_rna_anon_prop(mbus, RenderSettings, pixel_aspect_y, &msg_sub_value_gz_tag_refresh);
283         }
284 }
285
286 void VIEW3D_GGT_camera(wmGizmoGroupType *gzgt)
287 {
288         gzgt->name = "Camera Widgets";
289         gzgt->idname = "VIEW3D_GGT_camera";
290
291         gzgt->flag = (WM_GIZMOGROUPTYPE_PERSISTENT |
292                      WM_GIZMOGROUPTYPE_3D |
293                      WM_GIZMOGROUPTYPE_DEPTH_3D);
294
295         gzgt->poll = WIDGETGROUP_camera_poll;
296         gzgt->setup = WIDGETGROUP_camera_setup;
297         gzgt->refresh = WIDGETGROUP_camera_refresh;
298         gzgt->message_subscribe = WIDGETGROUP_camera_message_subscribe;
299 }
300
301 /** \} */
302
303 /* -------------------------------------------------------------------- */
304
305 /** \name CameraView Gizmos
306  * \{ */
307
308 struct CameraViewWidgetGroup {
309         wmGizmo *border;
310
311         struct {
312                 rctf *edit_border;
313                 rctf view_border;
314         } state;
315 };
316
317 /* scale callbacks */
318 static void gizmo_render_border_prop_matrix_get(
319         const wmGizmo *UNUSED(gz), wmGizmoProperty *gz_prop,
320         void *value_p)
321 {
322         float (*matrix)[4] = value_p;
323         BLI_assert(gz_prop->type->array_length == 16);
324         struct CameraViewWidgetGroup *viewgroup = gz_prop->custom_func.user_data;
325         const rctf *border = viewgroup->state.edit_border;
326
327         unit_m4(matrix);
328         matrix[0][0] = BLI_rctf_size_x(border);
329         matrix[1][1] = BLI_rctf_size_y(border);
330         matrix[3][0] = BLI_rctf_cent_x(border);
331         matrix[3][1] = BLI_rctf_cent_y(border);
332 }
333
334 static void gizmo_render_border_prop_matrix_set(
335         const wmGizmo *UNUSED(gz), wmGizmoProperty *gz_prop,
336         const void *value_p)
337 {
338         const float (*matrix)[4] = value_p;
339         struct CameraViewWidgetGroup *viewgroup = gz_prop->custom_func.user_data;
340         rctf *border = viewgroup->state.edit_border;
341         BLI_assert(gz_prop->type->array_length == 16);
342
343         BLI_rctf_resize(border, len_v3(matrix[0]), len_v3(matrix[1]));
344         BLI_rctf_recenter(border, matrix[3][0], matrix[3][1]);
345         BLI_rctf_isect(&(rctf){.xmin = 0, .ymin = 0, .xmax = 1, .ymax = 1}, border, border);
346 }
347
348 static bool WIDGETGROUP_camera_view_poll(const bContext *C, wmGizmoGroupType *UNUSED(gzgt))
349 {
350         Scene *scene = CTX_data_scene(C);
351
352         /* This is just so the border isn't always in the way,
353          * stealing mouse clicks from regular usage.
354          * We could change the rules for when to show. */
355         {
356                 ViewLayer *view_layer = CTX_data_view_layer(C);
357                 if (scene->camera != OBACT(view_layer)) {
358                         return false;
359                 }
360         }
361
362         View3D *v3d = CTX_wm_view3d(C);
363         if ((v3d->flag2 & V3D_RENDER_OVERRIDE) ||
364             (v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_CONTEXT)))
365         {
366                 return false;
367         }
368
369         ARegion *ar = CTX_wm_region(C);
370         RegionView3D *rv3d = ar->regiondata;
371         if (rv3d->persp == RV3D_CAMOB) {
372                 if (scene->r.mode & R_BORDER) {
373                         /* TODO: support overrides. */
374                         if (scene->id.lib == NULL) {
375                                 return true;
376                         }
377                 }
378         }
379         else if (v3d->flag2 & V3D_RENDER_BORDER) {
380                 return true;
381         }
382         return false;
383 }
384
385 static void WIDGETGROUP_camera_view_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
386 {
387         struct CameraViewWidgetGroup *viewgroup = MEM_mallocN(sizeof(struct CameraViewWidgetGroup), __func__);
388
389         viewgroup->border = WM_gizmo_new("GIZMO_GT_cage_2d", gzgroup, NULL);
390
391         RNA_enum_set(viewgroup->border->ptr, "transform",
392                      ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE | ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE);
393         /* Box style is more subtle in this case. */
394         RNA_enum_set(viewgroup->border->ptr, "draw_style", ED_GIZMO_CAGE2D_STYLE_BOX);
395
396
397         gzgroup->customdata = viewgroup;
398 }
399
400 static void WIDGETGROUP_camera_view_draw_prepare(const bContext *C, wmGizmoGroup *gzgroup)
401 {
402         struct CameraViewWidgetGroup *viewgroup = gzgroup->customdata;
403
404         ARegion *ar = CTX_wm_region(C);
405         struct Depsgraph *depsgraph = CTX_data_depsgraph(C);
406         RegionView3D *rv3d = ar->regiondata;
407         if (rv3d->persp == RV3D_CAMOB) {
408                 Scene *scene = CTX_data_scene(C);
409                 View3D *v3d = CTX_wm_view3d(C);
410                 ED_view3d_calc_camera_border(scene, depsgraph, ar, v3d, rv3d, &viewgroup->state.view_border, false);
411         }
412         else {
413                 viewgroup->state.view_border = (rctf){.xmin = 0, .ymin = 0, .xmax = ar->winx, .ymax = ar->winy};
414         }
415
416         wmGizmo *gz = viewgroup->border;
417         unit_m4(gz->matrix_space);
418         mul_v3_fl(gz->matrix_space[0], BLI_rctf_size_x(&viewgroup->state.view_border));
419         mul_v3_fl(gz->matrix_space[1], BLI_rctf_size_y(&viewgroup->state.view_border));
420         gz->matrix_space[3][0] = viewgroup->state.view_border.xmin;
421         gz->matrix_space[3][1] = viewgroup->state.view_border.ymin;
422 }
423
424 static void WIDGETGROUP_camera_view_refresh(const bContext *C, wmGizmoGroup *gzgroup)
425 {
426         struct CameraViewWidgetGroup *viewgroup = gzgroup->customdata;
427
428         View3D *v3d = CTX_wm_view3d(C);
429         ARegion *ar = CTX_wm_region(C);
430         RegionView3D *rv3d = ar->regiondata;
431         Scene *scene = CTX_data_scene(C);
432
433         {
434                 wmGizmo *gz = viewgroup->border;
435                 WM_gizmo_set_flag(gz, WM_GIZMO_HIDDEN, false);
436
437                 RNA_enum_set(viewgroup->border->ptr, "transform",
438                              ED_GIZMO_CAGE2D_XFORM_FLAG_TRANSLATE | ED_GIZMO_CAGE2D_XFORM_FLAG_SCALE);
439
440                 if (rv3d->persp == RV3D_CAMOB) {
441                         viewgroup->state.edit_border = &scene->r.border;
442                 }
443                 else {
444                         viewgroup->state.edit_border = &v3d->render_border;
445                 }
446
447                 WM_gizmo_target_property_def_func(
448                         gz, "matrix",
449                         &(const struct wmGizmoPropertyFnParams) {
450                             .value_get_fn = gizmo_render_border_prop_matrix_get,
451                             .value_set_fn = gizmo_render_border_prop_matrix_set,
452                             .range_get_fn = NULL,
453                             .user_data = viewgroup,
454                         });
455         }
456
457 }
458
459 void VIEW3D_GGT_camera_view(wmGizmoGroupType *gzgt)
460 {
461         gzgt->name = "Camera View Widgets";
462         gzgt->idname = "VIEW3D_GGT_camera_view";
463
464         gzgt->flag = (WM_GIZMOGROUPTYPE_PERSISTENT |
465                      WM_GIZMOGROUPTYPE_SCALE);
466
467         gzgt->poll = WIDGETGROUP_camera_view_poll;
468         gzgt->setup = WIDGETGROUP_camera_view_setup;
469         gzgt->draw_prepare = WIDGETGROUP_camera_view_draw_prepare;
470         gzgt->refresh = WIDGETGROUP_camera_view_refresh;
471 }
472
473 /** \} */