1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2009 Blender Foundation.
17  * All rights reserved.
18  */
19 
20 /** \file
21  * \ingroup edinterface
22  */
23 
24 #include "DNA_screen_types.h"
25 #include "DNA_space_types.h"
26 
27 #include "BLI_blenlib.h"
28 
29 #include "BKE_context.h"
30 #include "BKE_screen.h"
31 
32 #include "UI_interface.h"
33 
34 #include "WM_api.h"
35 #include "WM_types.h"
36 
37 #include "interface_intern.h"
38 
39 #include "interface_eyedropper_intern.h" /* own include */
40 
41 /* -------------------------------------------------------------------- */
42 /* Keymap
43  */
44 /** \name Modal Keymap
45  * \{ */
46 
eyedropper_modal_keymap(wmKeyConfig * keyconf)47 wmKeyMap *eyedropper_modal_keymap(wmKeyConfig *keyconf)
48 {
49   static const EnumPropertyItem modal_items[] = {
50       {EYE_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
51       {EYE_MODAL_SAMPLE_CONFIRM, "SAMPLE_CONFIRM", 0, "Confirm Sampling", ""},
52       {EYE_MODAL_SAMPLE_BEGIN, "SAMPLE_BEGIN", 0, "Start Sampling", ""},
53       {EYE_MODAL_SAMPLE_RESET, "SAMPLE_RESET", 0, "Reset Sampling", ""},
54       {0, NULL, 0, NULL, NULL},
55   };
56 
57   wmKeyMap *keymap = WM_modalkeymap_find(keyconf, "Eyedropper Modal Map");
58 
59   /* this function is called for each spacetype, only needs to add map once */
60   if (keymap && keymap->modal_items) {
61     return NULL;
62   }
63 
64   keymap = WM_modalkeymap_ensure(keyconf, "Eyedropper Modal Map", modal_items);
65 
66   /* assign to operators */
67   WM_modalkeymap_assign(keymap, "UI_OT_eyedropper_colorramp");
68   WM_modalkeymap_assign(keymap, "UI_OT_eyedropper_color");
69   WM_modalkeymap_assign(keymap, "UI_OT_eyedropper_id");
70   WM_modalkeymap_assign(keymap, "UI_OT_eyedropper_depth");
71   WM_modalkeymap_assign(keymap, "UI_OT_eyedropper_driver");
72   WM_modalkeymap_assign(keymap, "UI_OT_eyedropper_gpencil_color");
73 
74   return keymap;
75 }
76 
eyedropper_colorband_modal_keymap(wmKeyConfig * keyconf)77 wmKeyMap *eyedropper_colorband_modal_keymap(wmKeyConfig *keyconf)
78 {
79   static const EnumPropertyItem modal_items_point[] = {
80       {EYE_MODAL_POINT_CANCEL, "CANCEL", 0, "Cancel", ""},
81       {EYE_MODAL_POINT_SAMPLE, "SAMPLE_SAMPLE", 0, "Sample a point", ""},
82       {EYE_MODAL_POINT_CONFIRM, "SAMPLE_CONFIRM", 0, "Confirm Sampling", ""},
83       {EYE_MODAL_POINT_RESET, "SAMPLE_RESET", 0, "Reset Sampling", ""},
84       {0, NULL, 0, NULL, NULL},
85   };
86 
87   wmKeyMap *keymap = WM_modalkeymap_find(keyconf, "Eyedropper ColorRamp PointSampling Map");
88   if (keymap && keymap->modal_items) {
89     return keymap;
90   }
91 
92   keymap = WM_modalkeymap_ensure(
93       keyconf, "Eyedropper ColorRamp PointSampling Map", modal_items_point);
94 
95   /* assign to operators */
96   WM_modalkeymap_assign(keymap, "UI_OT_eyedropper_colorramp_point");
97 
98   return keymap;
99 }
100 
101 /** \} */
102 
103 /* -------------------------------------------------------------------- */
104 /* Utility Functions
105  */
106 /** \name Generic Shared Functions
107  * \{ */
108 
eyedropper_draw_cursor_text(const struct bContext * C,const ARegion * region,const char * name)109 void eyedropper_draw_cursor_text(const struct bContext *C, const ARegion *region, const char *name)
110 {
111   const uiFontStyle *fstyle = UI_FSTYLE_WIDGET;
112   wmWindow *win = CTX_wm_window(C);
113   int x = win->eventstate->x;
114   int y = win->eventstate->y;
115   const float col_fg[4] = {1.0f, 1.0f, 1.0f, 1.0f};
116   const float col_bg[4] = {0.0f, 0.0f, 0.0f, 0.2f};
117 
118   if ((name[0] == '\0') || (BLI_rcti_isect_pt(&region->winrct, x, y) == false)) {
119     return;
120   }
121 
122   x = x - region->winrct.xmin;
123   y = y - region->winrct.ymin;
124 
125   y += U.widget_unit;
126 
127   UI_fontstyle_draw_simple_backdrop(fstyle, x, y, name, col_fg, col_bg);
128 }
129 
130 /**
131  * Utility to retrieve a button representing a RNA property that is currently under the cursor.
132  *
133  * This is to be used by any eyedroppers which fetch properties (e.g. UI_OT_eyedropper_driver).
134  * Especially during modal operations (e.g. as with the eyedroppers), context cannot be relied
135  * upon to provide this information, as it is not updated until the operator finishes.
136  *
137  * \return A button under the mouse which relates to some RNA Property, or NULL
138  */
eyedropper_get_property_button_under_mouse(bContext * C,const wmEvent * event)139 uiBut *eyedropper_get_property_button_under_mouse(bContext *C, const wmEvent *event)
140 {
141   bScreen *screen = CTX_wm_screen(C);
142   ScrArea *area = BKE_screen_find_area_xy(screen, SPACE_TYPE_ANY, event->x, event->y);
143   ARegion *region = BKE_area_find_region_xy(area, RGN_TYPE_ANY, event->x, event->y);
144 
145   uiBut *but = ui_but_find_mouse_over(region, event);
146 
147   if (ELEM(NULL, but, but->rnapoin.data, but->rnaprop)) {
148     return NULL;
149   }
150   return but;
151 }
152 
153 /** \} */
154