2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2009 Blender Foundation.
19 * All rights reserved.
21 * ***** END GPL LICENSE BLOCK *****
24 /** \file blender/editors/interface/interface_eyedropper_depth.c
25 * \ingroup edinterface
27 * This file defines an eyedropper for picking 3D depth value (primary use is depth-of-field).
30 * - #UI_OT_eyedropper_depth
33 #include "MEM_guardedalloc.h"
35 #include "DNA_space_types.h"
36 #include "DNA_screen_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_view3d_types.h"
40 #include "BLI_string.h"
41 #include "BLI_math_vector.h"
43 #include "BKE_context.h"
45 #include "BKE_screen.h"
48 #include "RNA_access.h"
50 #include "UI_interface.h"
55 #include "ED_screen.h"
56 #include "ED_space_api.h"
57 #include "ED_view3d.h"
59 #include "interface_intern.h"
60 #include "interface_eyedropper_intern.h"
63 * \note #DepthDropper is only internal name to avoid confusion with other kinds of eye-droppers.
65 typedef struct DepthDropper {
69 float init_depth; /* for resetting on cancel */
71 bool accum_start; /* has mouse been presed */
76 void *draw_handle_pixel;
81 static void depthdropper_draw_cb(const struct bContext *C, ARegion *ar, void *arg)
83 DepthDropper *ddr = arg;
84 eyedropper_draw_cursor_text(C, ar, ddr->name);
88 static int depthdropper_init(bContext *C, wmOperator *op)
96 st = BKE_spacetype_from_id(SPACE_VIEW3D);
97 art = BKE_regiontype_from_id(st, RGN_TYPE_WINDOW);
99 op->customdata = ddr = MEM_callocN(sizeof(DepthDropper), "DepthDropper");
101 UI_context_active_but_prop_get(C, &ddr->ptr, &ddr->prop, &index_dummy);
103 /* fallback to the active camera's dof */
104 if (ddr->prop == NULL) {
105 RegionView3D *rv3d = CTX_wm_region_view3d(C);
106 if (rv3d && rv3d->persp == RV3D_CAMOB) {
107 View3D *v3d = CTX_wm_view3d(C);
108 if (v3d->camera && v3d->camera->data && !ID_IS_LINKED(v3d->camera->data)) {
109 RNA_id_pointer_create(v3d->camera->data, &ddr->ptr);
110 ddr->prop = RNA_struct_find_property(&ddr->ptr, "dof_distance");
115 if ((ddr->ptr.data == NULL) ||
116 (ddr->prop == NULL) ||
117 (RNA_property_editable(&ddr->ptr, ddr->prop) == false) ||
118 (RNA_property_type(ddr->prop) != PROP_FLOAT))
124 ddr->draw_handle_pixel = ED_region_draw_cb_activate(art, depthdropper_draw_cb, ddr, REGION_DRAW_POST_PIXEL);
125 ddr->init_depth = RNA_property_float_get(&ddr->ptr, ddr->prop);
130 static void depthdropper_exit(bContext *C, wmOperator *op)
132 WM_cursor_modal_restore(CTX_wm_window(C));
134 if (op->customdata) {
135 DepthDropper *ddr = (DepthDropper *)op->customdata;
138 ED_region_draw_cb_exit(ddr->art, ddr->draw_handle_pixel);
141 MEM_freeN(op->customdata);
143 op->customdata = NULL;
147 /* *** depthdropper id helper functions *** */
149 * \brief get the ID from the screen.
152 static void depthdropper_depth_sample_pt(bContext *C, DepthDropper *ddr, int mx, int my, float *r_depth)
154 /* we could use some clever */
155 Main *bmain = CTX_data_main(C);
156 wmWindow *win = CTX_wm_window(C);
157 ScrArea *sa = BKE_screen_find_area_xy(win->screen, SPACE_TYPE_ANY, mx, my);
158 Scene *scene = win->screen->scene;
159 UnitSettings *unit = &scene->unit;
160 const bool do_split = (unit->flag & USER_UNIT_OPT_SPLIT) != 0;
162 ScrArea *area_prev = CTX_wm_area(C);
163 ARegion *ar_prev = CTX_wm_region(C);
168 if (sa->spacetype == SPACE_VIEW3D) {
169 ARegion *ar = BKE_area_find_region_xy(sa, RGN_TYPE_WINDOW, mx, my);
171 View3D *v3d = sa->spacedata.first;
172 RegionView3D *rv3d = ar->regiondata;
173 /* weak, we could pass in some reference point */
174 const float *view_co = v3d->camera ? v3d->camera->obmat[3] : rv3d->viewinv[3];
175 const int mval[2] = {
176 mx - ar->winrct.xmin,
177 my - ar->winrct.ymin};
180 CTX_wm_area_set(C, sa);
181 CTX_wm_region_set(C, ar);
183 /* grr, always draw else we leave stale text */
184 ED_region_tag_redraw(ar);
186 view3d_operator_needs_opengl(C);
188 if (ED_view3d_autodist(bmain, scene, ar, v3d, mval, co, true, NULL)) {
189 const float mval_center_fl[2] = {
191 (float)ar->winy / 2};
194 /* quick way to get view-center aligned point */
195 ED_view3d_win_to_3d(v3d, ar, co, mval_center_fl, co_align);
197 *r_depth = len_v3v3(view_co, co_align);
199 bUnit_AsString(ddr->name, sizeof(ddr->name),
201 4, unit->system, B_UNIT_LENGTH, do_split, false);
204 BLI_strncpy(ddr->name, "Nothing under cursor", sizeof(ddr->name));
210 CTX_wm_area_set(C, area_prev);
211 CTX_wm_region_set(C, ar_prev);
214 /* sets the sample depth RGB, maintaining A */
215 static void depthdropper_depth_set(bContext *C, DepthDropper *ddr, const float depth)
217 RNA_property_float_set(&ddr->ptr, ddr->prop, depth);
218 RNA_property_update(C, &ddr->ptr, ddr->prop);
221 /* set sample from accumulated values */
222 static void depthdropper_depth_set_accum(bContext *C, DepthDropper *ddr)
224 float depth = ddr->accum_depth;
225 if (ddr->accum_tot) {
226 depth /= (float)ddr->accum_tot;
228 depthdropper_depth_set(C, ddr, depth);
231 /* single point sample & set */
232 static void depthdropper_depth_sample(bContext *C, DepthDropper *ddr, int mx, int my)
235 if (depth != -1.0f) {
236 depthdropper_depth_sample_pt(C, ddr, mx, my, &depth);
237 depthdropper_depth_set(C, ddr, depth);
241 static void depthdropper_depth_sample_accum(bContext *C, DepthDropper *ddr, int mx, int my)
244 depthdropper_depth_sample_pt(C, ddr, mx, my, &depth);
245 if (depth != -1.0f) {
246 ddr->accum_depth += depth;
251 static void depthdropper_cancel(bContext *C, wmOperator *op)
253 DepthDropper *ddr = op->customdata;
254 depthdropper_depth_set(C, ddr, ddr->init_depth);
255 depthdropper_exit(C, op);
258 /* main modal status check */
259 static int depthdropper_modal(bContext *C, wmOperator *op, const wmEvent *event)
261 DepthDropper *ddr = (DepthDropper *)op->customdata;
263 /* handle modal keymap */
264 if (event->type == EVT_MODAL_MAP) {
265 switch (event->val) {
266 case EYE_MODAL_CANCEL:
267 depthdropper_cancel(C, op);
268 return OPERATOR_CANCELLED;
269 case EYE_MODAL_SAMPLE_CONFIRM:
270 if (ddr->accum_tot == 0) {
271 depthdropper_depth_sample(C, ddr, event->x, event->y);
274 depthdropper_depth_set_accum(C, ddr);
276 depthdropper_exit(C, op);
277 return OPERATOR_FINISHED;
278 case EYE_MODAL_SAMPLE_BEGIN:
279 /* enable accum and make first sample */
280 ddr->accum_start = true;
281 depthdropper_depth_sample_accum(C, ddr, event->x, event->y);
283 case EYE_MODAL_SAMPLE_RESET:
285 ddr->accum_depth = 0.0f;
286 depthdropper_depth_sample_accum(C, ddr, event->x, event->y);
287 depthdropper_depth_set_accum(C, ddr);
291 else if (event->type == MOUSEMOVE) {
292 if (ddr->accum_start) {
293 /* button is pressed so keep sampling */
294 depthdropper_depth_sample_accum(C, ddr, event->x, event->y);
295 depthdropper_depth_set_accum(C, ddr);
299 return OPERATOR_RUNNING_MODAL;
302 /* Modal Operator init */
303 static int depthdropper_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
306 if (depthdropper_init(C, op)) {
307 WM_cursor_modal_set(CTX_wm_window(C), BC_EYEDROPPER_CURSOR);
309 /* add temp handler */
310 WM_event_add_modal_handler(C, op);
312 return OPERATOR_RUNNING_MODAL;
315 depthdropper_exit(C, op);
316 return OPERATOR_CANCELLED;
320 /* Repeat operator */
321 static int depthdropper_exec(bContext *C, wmOperator *op)
324 if (depthdropper_init(C, op)) {
326 depthdropper_exit(C, op);
328 return OPERATOR_FINISHED;
331 return OPERATOR_CANCELLED;
335 static int depthdropper_poll(bContext *C)
342 /* check if there's an active button taking depth value */
343 if ((CTX_wm_window(C) != NULL) &&
344 (but = UI_context_active_but_prop_get(C, &ptr, &prop, &index_dummy)) &&
345 (but->type == UI_BTYPE_NUM) &&
348 if ((RNA_property_type(prop) == PROP_FLOAT) &&
349 (RNA_property_subtype(prop) & PROP_UNIT_LENGTH) &&
350 (RNA_property_array_check(prop) == false))
356 RegionView3D *rv3d = CTX_wm_region_view3d(C);
357 if (rv3d && rv3d->persp == RV3D_CAMOB) {
358 View3D *v3d = CTX_wm_view3d(C);
359 if (v3d->camera && v3d->camera->data && !ID_IS_LINKED(v3d->camera->data)) {
368 void UI_OT_eyedropper_depth(wmOperatorType *ot)
371 ot->name = "Eyedropper Depth";
372 ot->idname = "UI_OT_eyedropper_depth";
373 ot->description = "Sample depth from the 3D view";
376 ot->invoke = depthdropper_invoke;
377 ot->modal = depthdropper_modal;
378 ot->cancel = depthdropper_cancel;
379 ot->exec = depthdropper_exec;
380 ot->poll = depthdropper_poll;
383 ot->flag = OPTYPE_BLOCKING | OPTYPE_INTERNAL;