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