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