Fix T60781: DOF Distance (pick) crashes
[blender.git] / source / blender / editors / interface / interface_eyedropper_depth.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  * The Original Code is Copyright (C) 2009 Blender Foundation.
19  * All rights reserved.
20  *
21  * ***** END GPL LICENSE BLOCK *****
22  */
23
24 /** \file blender/editors/interface/interface_eyedropper_depth.c
25  *  \ingroup edinterface
26  *
27  * This file defines an eyedropper for picking 3D depth value (primary use is depth-of-field).
28  *
29  * Defines:
30  * - #UI_OT_eyedropper_depth
31  */
32
33 #include "MEM_guardedalloc.h"
34
35 #include "DNA_space_types.h"
36 #include "DNA_screen_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_view3d_types.h"
39
40 #include "BLI_string.h"
41 #include "BLI_math_vector.h"
42
43 #include "BKE_context.h"
44 #include "BKE_main.h"
45 #include "BKE_screen.h"
46 #include "BKE_unit.h"
47
48 #include "DEG_depsgraph.h"
49
50 #include "RNA_access.h"
51
52 #include "UI_interface.h"
53
54 #include "WM_api.h"
55 #include "WM_types.h"
56
57 #include "ED_screen.h"
58 #include "ED_space_api.h"
59 #include "ED_view3d.h"
60
61 #include "interface_intern.h"
62 #include "interface_eyedropper_intern.h"
63
64 /**
65  * \note #DepthDropper is only internal name to avoid confusion with other kinds of eye-droppers.
66  */
67 typedef struct DepthDropper {
68         PointerRNA ptr;
69         PropertyRNA *prop;
70         bool is_undo;
71
72         bool is_set;
73         float init_depth; /* for resetting on cancel */
74
75         bool  accum_start; /* has mouse been presed */
76         float accum_depth;
77         int   accum_tot;
78
79         ARegionType *art;
80         void *draw_handle_pixel;
81         char name[200];
82 } DepthDropper;
83
84
85 static void depthdropper_draw_cb(const struct bContext *C, ARegion *ar, void *arg)
86 {
87         DepthDropper *ddr = arg;
88         eyedropper_draw_cursor_text(C, ar, ddr->name);
89 }
90
91
92 static int depthdropper_init(bContext *C, wmOperator *op)
93 {
94         int index_dummy;
95
96         SpaceType *st;
97         ARegionType *art;
98
99         st = BKE_spacetype_from_id(SPACE_VIEW3D);
100         art = BKE_regiontype_from_id(st, RGN_TYPE_WINDOW);
101
102         DepthDropper *ddr = MEM_callocN(sizeof(DepthDropper), __func__);
103
104         uiBut *but = UI_context_active_but_prop_get(C, &ddr->ptr, &ddr->prop, &index_dummy);
105
106         /* fallback to the active camera's dof */
107         if (ddr->prop == NULL) {
108                 RegionView3D *rv3d = CTX_wm_region_view3d(C);
109                 if (rv3d && rv3d->persp == RV3D_CAMOB) {
110                         View3D *v3d = CTX_wm_view3d(C);
111                         if (v3d->camera && v3d->camera->data && !ID_IS_LINKED(v3d->camera->data)) {
112                                 RNA_id_pointer_create(v3d->camera->data, &ddr->ptr);
113                                 ddr->prop = RNA_struct_find_property(&ddr->ptr, "dof_distance");
114                                 ddr->is_undo = true;
115                         }
116                 }
117         }
118         else {
119                 ddr->is_undo = UI_but_flag_is_set(but, UI_BUT_UNDO);
120         }
121
122         if ((ddr->ptr.data == NULL) ||
123             (ddr->prop == NULL) ||
124             (RNA_property_editable(&ddr->ptr, ddr->prop) == false) ||
125             (RNA_property_type(ddr->prop) != PROP_FLOAT))
126         {
127                 MEM_freeN(ddr);
128                 return false;
129         }
130         op->customdata = ddr;
131
132         ddr->art = art;
133         ddr->draw_handle_pixel = ED_region_draw_cb_activate(art, depthdropper_draw_cb, ddr, REGION_DRAW_POST_PIXEL);
134         ddr->init_depth = RNA_property_float_get(&ddr->ptr, ddr->prop);
135
136         return true;
137 }
138
139 static void depthdropper_exit(bContext *C, wmOperator *op)
140 {
141         WM_cursor_modal_restore(CTX_wm_window(C));
142
143         if (op->customdata) {
144                 DepthDropper *ddr = (DepthDropper *)op->customdata;
145
146                 if (ddr->art) {
147                         ED_region_draw_cb_exit(ddr->art, ddr->draw_handle_pixel);
148                 }
149
150                 MEM_freeN(op->customdata);
151
152                 op->customdata = NULL;
153         }
154 }
155
156 /* *** depthdropper id helper functions *** */
157 /**
158  * \brief get the ID from the screen.
159  *
160  */
161 static void depthdropper_depth_sample_pt(bContext *C, DepthDropper *ddr, int mx, int my, float *r_depth)
162 {
163         /* we could use some clever */
164         bScreen *screen = CTX_wm_screen(C);
165         ScrArea *sa = BKE_screen_find_area_xy(screen, SPACE_TYPE_ANY, mx, my);
166         Scene *scene = CTX_data_scene(C);
167
168         ScrArea *area_prev = CTX_wm_area(C);
169         ARegion *ar_prev = CTX_wm_region(C);
170
171         ddr->name[0] = '\0';
172
173         if (sa) {
174                 if (sa->spacetype == SPACE_VIEW3D) {
175                         ARegion *ar = BKE_area_find_region_xy(sa, RGN_TYPE_WINDOW, mx, my);
176                         if (ar) {
177                                 struct Depsgraph *depsgraph = CTX_data_depsgraph(C);
178                                 View3D *v3d = sa->spacedata.first;
179                                 RegionView3D *rv3d = ar->regiondata;
180                                 /* weak, we could pass in some reference point */
181                                 const float *view_co = v3d->camera ? v3d->camera->obmat[3] : rv3d->viewinv[3];
182                                 const int mval[2] = {
183                                     mx - ar->winrct.xmin,
184                                     my - ar->winrct.ymin};
185                                 float co[3];
186
187                                 CTX_wm_area_set(C, sa);
188                                 CTX_wm_region_set(C, ar);
189
190                                 /* grr, always draw else we leave stale text */
191                                 ED_region_tag_redraw(ar);
192
193                                 view3d_operator_needs_opengl(C);
194
195                                 if (ED_view3d_autodist(depsgraph, ar, v3d, mval, co, true, NULL)) {
196                                         const float mval_center_fl[2] = {
197                                             (float)ar->winx / 2,
198                                             (float)ar->winy / 2};
199                                         float co_align[3];
200
201                                         /* quick way to get view-center aligned point */
202                                         ED_view3d_win_to_3d(v3d, ar, co, mval_center_fl, co_align);
203
204                                         *r_depth = len_v3v3(view_co, co_align);
205
206                                         bUnit_AsString2(
207                                                 ddr->name, sizeof(ddr->name), (double)*r_depth,
208                                                 4, B_UNIT_LENGTH, &scene->unit, false);
209                                 }
210                                 else {
211                                         BLI_strncpy(ddr->name, "Nothing under cursor", sizeof(ddr->name));
212                                 }
213                         }
214                 }
215         }
216
217         CTX_wm_area_set(C, area_prev);
218         CTX_wm_region_set(C, ar_prev);
219 }
220
221 /* sets the sample depth RGB, maintaining A */
222 static void depthdropper_depth_set(bContext *C, DepthDropper *ddr, const float depth)
223 {
224         RNA_property_float_set(&ddr->ptr, ddr->prop, depth);
225         ddr->is_set = true;
226         RNA_property_update(C, &ddr->ptr, ddr->prop);
227 }
228
229 /* set sample from accumulated values */
230 static void depthdropper_depth_set_accum(bContext *C, DepthDropper *ddr)
231 {
232         float depth = ddr->accum_depth;
233         if (ddr->accum_tot) {
234                 depth /= (float)ddr->accum_tot;
235         }
236         depthdropper_depth_set(C, ddr, depth);
237 }
238
239 /* single point sample & set */
240 static void depthdropper_depth_sample(bContext *C, DepthDropper *ddr, int mx, int my)
241 {
242         float depth = -1.0f;
243         if (depth != -1.0f) {
244                 depthdropper_depth_sample_pt(C, ddr, mx, my, &depth);
245                 depthdropper_depth_set(C, ddr, depth);
246         }
247 }
248
249 static void depthdropper_depth_sample_accum(bContext *C, DepthDropper *ddr, int mx, int my)
250 {
251         float depth = -1.0f;
252         depthdropper_depth_sample_pt(C, ddr, mx, my, &depth);
253         if (depth != -1.0f) {
254                 ddr->accum_depth += depth;
255                 ddr->accum_tot++;
256         }
257 }
258
259 static void depthdropper_cancel(bContext *C, wmOperator *op)
260 {
261         DepthDropper *ddr = op->customdata;
262         if (ddr->is_set) {
263                 depthdropper_depth_set(C, ddr, ddr->init_depth);
264         }
265         depthdropper_exit(C, op);
266 }
267
268 /* main modal status check */
269 static int depthdropper_modal(bContext *C, wmOperator *op, const wmEvent *event)
270 {
271         DepthDropper *ddr = (DepthDropper *)op->customdata;
272
273         /* handle modal keymap */
274         if (event->type == EVT_MODAL_MAP) {
275                 switch (event->val) {
276                         case EYE_MODAL_CANCEL:
277                                 depthdropper_cancel(C, op);
278                                 return OPERATOR_CANCELLED;
279                         case EYE_MODAL_SAMPLE_CONFIRM:
280                         {
281                                 const bool is_undo = ddr->is_undo;
282                                 if (ddr->accum_tot == 0) {
283                                         depthdropper_depth_sample(C, ddr, event->x, event->y);
284                                 }
285                                 else {
286                                         depthdropper_depth_set_accum(C, ddr);
287                                 }
288                                 depthdropper_exit(C, op);
289                                 /* Could support finished & undo-skip. */
290                                 return is_undo ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
291                         }
292                         case EYE_MODAL_SAMPLE_BEGIN:
293                                 /* enable accum and make first sample */
294                                 ddr->accum_start = true;
295                                 depthdropper_depth_sample_accum(C, ddr, event->x, event->y);
296                                 break;
297                         case EYE_MODAL_SAMPLE_RESET:
298                                 ddr->accum_tot = 0;
299                                 ddr->accum_depth = 0.0f;
300                                 depthdropper_depth_sample_accum(C, ddr, event->x, event->y);
301                                 depthdropper_depth_set_accum(C, ddr);
302                                 break;
303                 }
304         }
305         else if (event->type == MOUSEMOVE) {
306                 if (ddr->accum_start) {
307                         /* button is pressed so keep sampling */
308                         depthdropper_depth_sample_accum(C, ddr, event->x, event->y);
309                         depthdropper_depth_set_accum(C, ddr);
310                 }
311         }
312
313         return OPERATOR_RUNNING_MODAL;
314 }
315
316 /* Modal Operator init */
317 static int depthdropper_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
318 {
319         /* init */
320         if (depthdropper_init(C, op)) {
321                 WM_cursor_modal_set(CTX_wm_window(C), BC_EYEDROPPER_CURSOR);
322
323                 /* add temp handler */
324                 WM_event_add_modal_handler(C, op);
325
326                 return OPERATOR_RUNNING_MODAL;
327         }
328         else {
329                 return OPERATOR_CANCELLED;
330         }
331 }
332
333 /* Repeat operator */
334 static int depthdropper_exec(bContext *C, wmOperator *op)
335 {
336         /* init */
337         if (depthdropper_init(C, op)) {
338                 /* cleanup */
339                 depthdropper_exit(C, op);
340
341                 return OPERATOR_FINISHED;
342         }
343         else {
344                 return OPERATOR_CANCELLED;
345         }
346 }
347
348 static bool depthdropper_poll(bContext *C)
349 {
350         PointerRNA ptr;
351         PropertyRNA *prop;
352         int index_dummy;
353         uiBut *but;
354
355         /* check if there's an active button taking depth value */
356         if ((CTX_wm_window(C) != NULL) &&
357             (but = UI_context_active_but_prop_get(C, &ptr, &prop, &index_dummy)) &&
358             (but->type == UI_BTYPE_NUM) &&
359             (prop != NULL))
360         {
361                 if ((RNA_property_type(prop) == PROP_FLOAT) &&
362                     (RNA_property_subtype(prop) & PROP_UNIT_LENGTH) &&
363                     (RNA_property_array_check(prop) == false))
364                 {
365                         return 1;
366                 }
367         }
368         else {
369                 RegionView3D *rv3d = CTX_wm_region_view3d(C);
370                 if (rv3d && rv3d->persp == RV3D_CAMOB) {
371                         View3D *v3d = CTX_wm_view3d(C);
372                         if (v3d->camera && v3d->camera->data && !ID_IS_LINKED(v3d->camera->data)) {
373                                 return 1;
374                         }
375                 }
376         }
377
378         return 0;
379 }
380
381 void UI_OT_eyedropper_depth(wmOperatorType *ot)
382 {
383         /* identifiers */
384         ot->name = "Eyedropper Depth";
385         ot->idname = "UI_OT_eyedropper_depth";
386         ot->description = "Sample depth from the 3D view";
387
388         /* api callbacks */
389         ot->invoke = depthdropper_invoke;
390         ot->modal = depthdropper_modal;
391         ot->cancel = depthdropper_cancel;
392         ot->exec = depthdropper_exec;
393         ot->poll = depthdropper_poll;
394
395         /* flags */
396         ot->flag = OPTYPE_UNDO | OPTYPE_BLOCKING | OPTYPE_INTERNAL;
397
398         /* properties */
399 }