| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							|  |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The Original Code is Copyright (C) 2009 Blender Foundation. | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-18 08:08:12 +11:00
										 |  |  | /** \file
 | 
					
						
							|  |  |  |  * \ingroup edinterface | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Eyedropper (ID data-blocks) | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Defines: | 
					
						
							|  |  |  |  * - #UI_OT_eyedropper_id | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "MEM_guardedalloc.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "DNA_object_types.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-19 09:33:03 +01:00
										 |  |  | #include "DNA_screen_types.h"
 | 
					
						
							|  |  |  | #include "DNA_space_types.h"
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "BLI_string.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "BLT_translation.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "BKE_context.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-19 19:37:00 +01:00
										 |  |  | #include "BKE_idtype.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-19 09:33:03 +01:00
										 |  |  | #include "BKE_report.h"
 | 
					
						
							|  |  |  | #include "BKE_screen.h"
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "RNA_access.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "UI_interface.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "WM_api.h"
 | 
					
						
							|  |  |  | #include "WM_types.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 09:33:03 +01:00
										 |  |  | #include "ED_outliner.h"
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  | #include "ED_screen.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-19 09:33:03 +01:00
										 |  |  | #include "ED_space_api.h"
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  | #include "ED_view3d.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "interface_eyedropper_intern.h"
 | 
					
						
							| 
									
										
										
										
											2020-03-19 09:33:03 +01:00
										 |  |  | #include "interface_intern.h"
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * \note #DataDropper is only internal name to avoid confusion with other kinds of eye-droppers. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | typedef struct DataDropper { | 
					
						
							|  |  |  |   PointerRNA ptr; | 
					
						
							|  |  |  |   PropertyRNA *prop; | 
					
						
							|  |  |  |   short idcode; | 
					
						
							|  |  |  |   const char *idcode_name; | 
					
						
							| 
									
										
										
										
											2019-01-17 16:39:38 +11:00
										 |  |  |   bool is_undo; | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |   ID *init_id; /* for resetting on cancel */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-08 13:40:00 -06:00
										 |  |  |   ScrArea *cursor_area; /* Area under the cursor */ | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |   ARegionType *art; | 
					
						
							|  |  |  |   void *draw_handle_pixel; | 
					
						
							|  |  |  |   char name[200]; | 
					
						
							|  |  |  | } DataDropper; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-06 16:56:42 +01:00
										 |  |  | static void datadropper_draw_cb(const struct bContext *C, ARegion *region, void *arg) | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  | { | 
					
						
							|  |  |  |   DataDropper *ddr = arg; | 
					
						
							| 
									
										
										
										
											2021-03-17 08:48:05 +01:00
										 |  |  |   eyedropper_draw_cursor_text_region(C, region, ddr->name); | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int datadropper_init(bContext *C, wmOperator *op) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   int index_dummy; | 
					
						
							|  |  |  |   StructRNA *type; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   SpaceType *st; | 
					
						
							|  |  |  |   ARegionType *art; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   st = BKE_spacetype_from_id(SPACE_VIEW3D); | 
					
						
							|  |  |  |   art = BKE_regiontype_from_id(st, RGN_TYPE_WINDOW); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-17 15:43:00 +11:00
										 |  |  |   DataDropper *ddr = MEM_callocN(sizeof(DataDropper), __func__); | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-17 16:39:38 +11:00
										 |  |  |   uiBut *but = UI_context_active_but_prop_get(C, &ddr->ptr, &ddr->prop, &index_dummy); | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if ((ddr->ptr.data == NULL) || (ddr->prop == NULL) || | 
					
						
							|  |  |  |       (RNA_property_editable(&ddr->ptr, ddr->prop) == false) || | 
					
						
							|  |  |  |       (RNA_property_type(ddr->prop) != PROP_POINTER)) { | 
					
						
							| 
									
										
										
										
											2019-01-17 15:43:00 +11:00
										 |  |  |     MEM_freeN(ddr); | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-01-17 15:43:00 +11:00
										 |  |  |   op->customdata = ddr; | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-17 16:39:38 +11:00
										 |  |  |   ddr->is_undo = UI_but_flag_is_set(but, UI_BUT_UNDO); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-08 13:40:00 -06:00
										 |  |  |   ddr->cursor_area = CTX_wm_area(C); | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |   ddr->art = art; | 
					
						
							|  |  |  |   ddr->draw_handle_pixel = ED_region_draw_cb_activate( | 
					
						
							|  |  |  |       art, datadropper_draw_cb, ddr, REGION_DRAW_POST_PIXEL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   type = RNA_property_pointer_type(&ddr->ptr, ddr->prop); | 
					
						
							|  |  |  |   ddr->idcode = RNA_type_to_ID_code(type); | 
					
						
							|  |  |  |   BLI_assert(ddr->idcode != 0); | 
					
						
							| 
									
										
										
										
											2019-01-15 23:24:20 +11:00
										 |  |  |   /* Note we can translate here (instead of on draw time),
 | 
					
						
							|  |  |  |    * because this struct has very short lifetime. */ | 
					
						
							| 
									
										
										
										
											2020-03-19 19:37:00 +01:00
										 |  |  |   ddr->idcode_name = TIP_(BKE_idtype_idcode_to_name(ddr->idcode)); | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-26 10:11:13 +10:00
										 |  |  |   const PointerRNA ptr = RNA_property_pointer_get(&ddr->ptr, ddr->prop); | 
					
						
							| 
									
										
										
										
											2019-08-23 09:52:12 +02:00
										 |  |  |   ddr->init_id = ptr.owner_id; | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void datadropper_exit(bContext *C, wmOperator *op) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-06 16:22:28 +01:00
										 |  |  |   wmWindow *win = CTX_wm_window(C); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   WM_cursor_modal_restore(win); | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (op->customdata) { | 
					
						
							|  |  |  |     DataDropper *ddr = (DataDropper *)op->customdata; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ddr->art) { | 
					
						
							|  |  |  |       ED_region_draw_cb_exit(ddr->art, ddr->draw_handle_pixel); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     MEM_freeN(op->customdata); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     op->customdata = NULL; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-06 16:22:28 +01:00
										 |  |  |   WM_event_add_mousemove(win); | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* *** datadropper id helper functions *** */ | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2019-08-08 13:40:00 -06:00
										 |  |  |  * \brief get the ID from the 3D view or outliner. | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |  */ | 
					
						
							|  |  |  | static void datadropper_id_sample_pt(bContext *C, DataDropper *ddr, int mx, int my, ID **r_id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   /* we could use some clever */ | 
					
						
							| 
									
										
										
										
											2017-12-12 23:19:41 +11:00
										 |  |  |   bScreen *screen = CTX_wm_screen(C); | 
					
						
							| 
									
										
										
										
											2020-04-03 13:25:03 +02:00
										 |  |  |   ScrArea *area = BKE_screen_find_area_xy(screen, -1, mx, my); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |   ScrArea *area_prev = CTX_wm_area(C); | 
					
						
							| 
									
										
										
										
											2020-04-03 12:51:03 +02:00
										 |  |  |   ARegion *region_prev = CTX_wm_region(C); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |   ddr->name[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-03 13:25:03 +02:00
										 |  |  |   if (area) { | 
					
						
							|  |  |  |     if (ELEM(area->spacetype, SPACE_VIEW3D, SPACE_OUTLINER)) { | 
					
						
							|  |  |  |       ARegion *region = BKE_area_find_region_xy(area, RGN_TYPE_WINDOW, mx, my); | 
					
						
							| 
									
										
										
										
											2020-03-06 16:56:42 +01:00
										 |  |  |       if (region) { | 
					
						
							|  |  |  |         const int mval[2] = {mx - region->winrct.xmin, my - region->winrct.ymin}; | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |         Base *base; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-03 13:25:03 +02:00
										 |  |  |         CTX_wm_area_set(C, area); | 
					
						
							| 
									
										
										
										
											2020-03-06 16:56:42 +01:00
										 |  |  |         CTX_wm_region_set(C, region); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |         /* grr, always draw else we leave stale text */ | 
					
						
							| 
									
										
										
										
											2020-03-06 16:56:42 +01:00
										 |  |  |         ED_region_tag_redraw(region); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-03 13:25:03 +02:00
										 |  |  |         if (area->spacetype == SPACE_VIEW3D) { | 
					
						
							| 
									
										
										
										
											2019-08-08 13:40:00 -06:00
										 |  |  |           base = ED_view3d_give_base_under_cursor(C, mval); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |           base = ED_outliner_give_base_under_cursor(C, mval); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |         if (base) { | 
					
						
							|  |  |  |           Object *ob = base->object; | 
					
						
							|  |  |  |           ID *id = NULL; | 
					
						
							|  |  |  |           if (ddr->idcode == ID_OB) { | 
					
						
							|  |  |  |             id = (ID *)ob; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           else if (ob->data) { | 
					
						
							|  |  |  |             if (GS(((ID *)ob->data)->name) == ddr->idcode) { | 
					
						
							|  |  |  |               id = (ID *)ob->data; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |               BLI_snprintf( | 
					
						
							|  |  |  |                   ddr->name, sizeof(ddr->name), "Incompatible, expected a %s", ddr->idcode_name); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 12:16:27 +02:00
										 |  |  |           PointerRNA idptr; | 
					
						
							|  |  |  |           RNA_id_pointer_create(id, &idptr); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 12:16:27 +02:00
										 |  |  |           if (id && RNA_property_pointer_poll(&ddr->ptr, ddr->prop, &idptr)) { | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |             BLI_snprintf(ddr->name, sizeof(ddr->name), "%s: %s", ddr->idcode_name, id->name + 2); | 
					
						
							|  |  |  |             *r_id = id; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |   CTX_wm_area_set(C, area_prev); | 
					
						
							| 
									
										
										
										
											2020-04-03 12:51:03 +02:00
										 |  |  |   CTX_wm_region_set(C, region_prev); | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* sets the ID, returns success */ | 
					
						
							|  |  |  | static bool datadropper_id_set(bContext *C, DataDropper *ddr, ID *id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   PointerRNA ptr_value; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   RNA_id_pointer_create(id, &ptr_value); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-20 18:06:09 +10:00
										 |  |  |   RNA_property_pointer_set(&ddr->ptr, ddr->prop, ptr_value, NULL); | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |   RNA_property_update(C, &ddr->ptr, ddr->prop); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ptr_value = RNA_property_pointer_get(&ddr->ptr, ddr->prop); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-23 09:52:12 +02:00
										 |  |  |   return (ptr_value.owner_id == id); | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* single point sample & set */ | 
					
						
							|  |  |  | static bool datadropper_id_sample(bContext *C, DataDropper *ddr, int mx, int my) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   ID *id = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   datadropper_id_sample_pt(C, ddr, mx, my, &id); | 
					
						
							|  |  |  |   return datadropper_id_set(C, ddr, id); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void datadropper_cancel(bContext *C, wmOperator *op) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   DataDropper *ddr = op->customdata; | 
					
						
							|  |  |  |   datadropper_id_set(C, ddr, ddr->init_id); | 
					
						
							|  |  |  |   datadropper_exit(C, op); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-08 13:40:00 -06:00
										 |  |  | /* To switch the draw callback when region under mouse event changes */ | 
					
						
							|  |  |  | static void datadropper_set_draw_callback_region(bContext *C, | 
					
						
							|  |  |  |                                                  DataDropper *ddr, | 
					
						
							|  |  |  |                                                  const int mx, | 
					
						
							|  |  |  |                                                  const int my) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   bScreen *screen = CTX_wm_screen(C); | 
					
						
							| 
									
										
										
										
											2020-04-03 13:25:03 +02:00
										 |  |  |   ScrArea *area = BKE_screen_find_area_xy(screen, -1, mx, my); | 
					
						
							| 
									
										
										
										
											2019-08-08 13:40:00 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-03 13:25:03 +02:00
										 |  |  |   if (area) { | 
					
						
							| 
									
										
										
										
											2019-08-08 13:40:00 -06:00
										 |  |  |     /* If spacetype changed */ | 
					
						
							| 
									
										
										
										
											2020-04-03 13:25:03 +02:00
										 |  |  |     if (area->spacetype != ddr->cursor_area->spacetype) { | 
					
						
							| 
									
										
										
										
											2019-08-08 13:40:00 -06:00
										 |  |  |       /* Remove old callback */ | 
					
						
							|  |  |  |       ED_region_draw_cb_exit(ddr->art, ddr->draw_handle_pixel); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       /* Redraw old area */ | 
					
						
							| 
									
										
										
										
											2020-03-06 16:56:42 +01:00
										 |  |  |       ARegion *region = BKE_area_find_region_type(ddr->cursor_area, RGN_TYPE_WINDOW); | 
					
						
							|  |  |  |       ED_region_tag_redraw(region); | 
					
						
							| 
									
										
										
										
											2019-08-08 13:40:00 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |       /* Set draw callback in new region */ | 
					
						
							| 
									
										
										
										
											2020-04-03 13:25:03 +02:00
										 |  |  |       ARegionType *art = BKE_regiontype_from_id(area->type, RGN_TYPE_WINDOW); | 
					
						
							| 
									
										
										
										
											2019-08-08 13:40:00 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-03 13:25:03 +02:00
										 |  |  |       ddr->cursor_area = area; | 
					
						
							| 
									
										
										
										
											2019-08-08 13:40:00 -06:00
										 |  |  |       ddr->art = art; | 
					
						
							|  |  |  |       ddr->draw_handle_pixel = ED_region_draw_cb_activate( | 
					
						
							|  |  |  |           art, datadropper_draw_cb, ddr, REGION_DRAW_POST_PIXEL); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  | /* main modal status check */ | 
					
						
							|  |  |  | static int datadropper_modal(bContext *C, wmOperator *op, const wmEvent *event) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   DataDropper *ddr = (DataDropper *)op->customdata; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |   /* handle modal keymap */ | 
					
						
							|  |  |  |   if (event->type == EVT_MODAL_MAP) { | 
					
						
							|  |  |  |     switch (event->val) { | 
					
						
							|  |  |  |       case EYE_MODAL_CANCEL: | 
					
						
							|  |  |  |         datadropper_cancel(C, op); | 
					
						
							|  |  |  |         return OPERATOR_CANCELLED; | 
					
						
							|  |  |  |       case EYE_MODAL_SAMPLE_CONFIRM: { | 
					
						
							| 
									
										
										
										
											2019-01-17 16:39:38 +11:00
										 |  |  |         const bool is_undo = ddr->is_undo; | 
					
						
							|  |  |  |         const bool success = datadropper_id_sample(C, ddr, event->x, event->y); | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |         datadropper_exit(C, op); | 
					
						
							|  |  |  |         if (success) { | 
					
						
							| 
									
										
										
										
											2019-01-17 16:39:38 +11:00
										 |  |  |           /* Could support finished & undo-skip. */ | 
					
						
							|  |  |  |           return is_undo ? OPERATOR_FINISHED : OPERATOR_CANCELLED; | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-07-03 14:20:10 +02:00
										 |  |  |         BKE_report(op->reports, RPT_WARNING, "Failed to set value"); | 
					
						
							|  |  |  |         return OPERATOR_CANCELLED; | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else if (event->type == MOUSEMOVE) { | 
					
						
							|  |  |  |     ID *id = NULL; | 
					
						
							| 
									
										
										
										
											2019-08-08 13:40:00 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Set the region for eyedropper cursor text drawing */ | 
					
						
							|  |  |  |     datadropper_set_draw_callback_region(C, ddr, event->x, event->y); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |     datadropper_id_sample_pt(C, ddr, event->x, event->y, &id); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |   return OPERATOR_RUNNING_MODAL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Modal Operator init */ | 
					
						
							|  |  |  | static int datadropper_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event)) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   /* init */ | 
					
						
							|  |  |  |   if (datadropper_init(C, op)) { | 
					
						
							| 
									
										
										
										
											2020-05-20 22:04:59 +10:00
										 |  |  |     wmWindow *win = CTX_wm_window(C); | 
					
						
							|  |  |  |     /* Workaround for de-activating the button clearing the cursor, see T76794 */ | 
					
						
							|  |  |  |     UI_context_active_but_clear(C, win, CTX_wm_region(C)); | 
					
						
							|  |  |  |     WM_cursor_modal_set(win, WM_CURSOR_EYEDROPPER); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |     /* add temp handler */ | 
					
						
							|  |  |  |     WM_event_add_modal_handler(C, op); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |     return OPERATOR_RUNNING_MODAL; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-07-03 14:20:10 +02:00
										 |  |  |   return OPERATOR_CANCELLED; | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Repeat operator */ | 
					
						
							|  |  |  | static int datadropper_exec(bContext *C, wmOperator *op) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   /* init */ | 
					
						
							|  |  |  |   if (datadropper_init(C, op)) { | 
					
						
							|  |  |  |     /* cleanup */ | 
					
						
							|  |  |  |     datadropper_exit(C, op); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |     return OPERATOR_FINISHED; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-07-03 14:20:10 +02:00
										 |  |  |   return OPERATOR_CANCELLED; | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 11:47:00 +02:00
										 |  |  | static bool datadropper_poll(bContext *C) | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  | { | 
					
						
							|  |  |  |   PointerRNA ptr; | 
					
						
							|  |  |  |   PropertyRNA *prop; | 
					
						
							|  |  |  |   int index_dummy; | 
					
						
							|  |  |  |   uiBut *but; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |   /* data dropper only supports object data */ | 
					
						
							|  |  |  |   if ((CTX_wm_window(C) != NULL) && | 
					
						
							|  |  |  |       (but = UI_context_active_but_prop_get(C, &ptr, &prop, &index_dummy)) && | 
					
						
							|  |  |  |       (but->type == UI_BTYPE_SEARCH_MENU) && (but->flag & UI_BUT_VALUE_CLEAR)) { | 
					
						
							|  |  |  |     if (prop && RNA_property_type(prop) == PROP_POINTER) { | 
					
						
							|  |  |  |       StructRNA *type = RNA_property_pointer_type(&ptr, prop); | 
					
						
							|  |  |  |       const short idcode = RNA_type_to_ID_code(type); | 
					
						
							|  |  |  |       if ((idcode == ID_OB) || OB_DATA_SUPPORT_ID(idcode)) { | 
					
						
							| 
									
										
										
										
											2020-09-02 19:10:18 +02:00
										 |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-02 19:10:18 +02:00
										 |  |  |   return false; | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void UI_OT_eyedropper_id(wmOperatorType *ot) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   /* identifiers */ | 
					
						
							|  |  |  |   ot->name = "Eyedropper Data-Block"; | 
					
						
							|  |  |  |   ot->idname = "UI_OT_eyedropper_id"; | 
					
						
							|  |  |  |   ot->description = "Sample a data-block from the 3D View to store in a property"; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |   /* api callbacks */ | 
					
						
							|  |  |  |   ot->invoke = datadropper_invoke; | 
					
						
							|  |  |  |   ot->modal = datadropper_modal; | 
					
						
							|  |  |  |   ot->cancel = datadropper_cancel; | 
					
						
							|  |  |  |   ot->exec = datadropper_exec; | 
					
						
							|  |  |  |   ot->poll = datadropper_poll; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |   /* flags */ | 
					
						
							| 
									
										
										
										
											2019-01-17 16:39:38 +11:00
										 |  |  |   ot->flag = OPTYPE_UNDO | OPTYPE_BLOCKING | OPTYPE_INTERNAL; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-12 15:16:13 +11:00
										 |  |  |   /* properties */ | 
					
						
							|  |  |  | } |