Cleanup: split eyedropper into separate files
[blender.git] / source / blender / editors / interface / interface_eyedropper.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  * Contributor(s): Blender Foundation, Joshua Leung
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/interface/interface_eyedropper.c
27  *  \ingroup edinterface
28  */
29
30 #include "DNA_space_types.h"
31 #include "DNA_screen_types.h"
32
33 #include "BLI_blenlib.h"
34 #include "BLI_math_vector.h"
35
36 #include "BKE_context.h"
37 #include "BKE_screen.h"
38
39 #include "UI_interface.h"
40
41 #include "WM_api.h"
42 #include "WM_types.h"
43
44 #include "interface_intern.h"
45
46 #include "interface_eyedropper_intern.h"  /* own include */
47
48 /* -------------------------------------------------------------------- */
49 /* Keymap
50  */
51 /** \name Modal Keymap
52  * \{ */
53
54
55 wmKeyMap *eyedropper_modal_keymap(wmKeyConfig *keyconf)
56 {
57         static const EnumPropertyItem modal_items[] = {
58                 {EYE_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
59                 {EYE_MODAL_SAMPLE_CONFIRM, "SAMPLE_CONFIRM", 0, "Confirm Sampling", ""},
60                 {EYE_MODAL_SAMPLE_BEGIN, "SAMPLE_BEGIN", 0, "Start Sampling", ""},
61                 {EYE_MODAL_SAMPLE_RESET, "SAMPLE_RESET", 0, "Reset Sampling", ""},
62                 {0, NULL, 0, NULL, NULL}
63         };
64
65         wmKeyMap *keymap = WM_modalkeymap_get(keyconf, "Eyedropper Modal Map");
66
67         /* this function is called for each spacetype, only needs to add map once */
68         if (keymap && keymap->modal_items)
69                 return NULL;
70
71         keymap = WM_modalkeymap_add(keyconf, "Eyedropper Modal Map", modal_items);
72
73         /* items for modal map */
74         WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, EYE_MODAL_CANCEL);
75         WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_PRESS, KM_ANY, 0, EYE_MODAL_CANCEL);
76         WM_modalkeymap_add_item(keymap, RETKEY, KM_RELEASE, KM_ANY, 0, EYE_MODAL_SAMPLE_CONFIRM);
77         WM_modalkeymap_add_item(keymap, PADENTER, KM_RELEASE, KM_ANY, 0, EYE_MODAL_SAMPLE_CONFIRM);
78         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_ANY, 0, EYE_MODAL_SAMPLE_CONFIRM);
79         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_ANY, 0, EYE_MODAL_SAMPLE_BEGIN);
80         WM_modalkeymap_add_item(keymap, SPACEKEY, KM_RELEASE, KM_ANY, 0, EYE_MODAL_SAMPLE_RESET);
81
82         /* assign to operators */
83         WM_modalkeymap_assign(keymap, "UI_OT_eyedropper_colorband");
84         WM_modalkeymap_assign(keymap, "UI_OT_eyedropper_color");
85         WM_modalkeymap_assign(keymap, "UI_OT_eyedropper_id");
86         WM_modalkeymap_assign(keymap, "UI_OT_eyedropper_depth");
87         WM_modalkeymap_assign(keymap, "UI_OT_eyedropper_driver");
88
89         return keymap;
90 }
91
92 wmKeyMap *eyedropper_colorband_modal_keymap(wmKeyConfig *keyconf)
93 {
94         static const EnumPropertyItem modal_items_point[] = {
95                 {EYE_MODAL_POINT_CANCEL, "CANCEL", 0, "Cancel", ""},
96                 {EYE_MODAL_POINT_SAMPLE, "SAMPLE_SAMPLE", 0, "Sample a point", ""},
97                 {EYE_MODAL_POINT_CONFIRM, "SAMPLE_CONFIRM", 0, "Confirm Sampling", ""},
98                 {EYE_MODAL_POINT_RESET, "SAMPLE_RESET", 0, "Reset Sampling", ""},
99                 {0, NULL, 0, NULL, NULL}
100         };
101
102         wmKeyMap *keymap = WM_modalkeymap_get(keyconf, "Eyedropper ColorBand PointSampling Map");
103         if (keymap && keymap->modal_items)
104                 return keymap;
105
106         keymap = WM_modalkeymap_add(keyconf, "Eyedropper ColorBand PointSampling Map", modal_items_point);
107
108         /* items for modal map */
109         WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, EYE_MODAL_CANCEL);
110         WM_modalkeymap_add_item(keymap, BACKSPACEKEY, KM_PRESS, KM_ANY, 0, EYE_MODAL_POINT_REMOVE_LAST);
111         WM_modalkeymap_add_item(keymap, RIGHTMOUSE, KM_PRESS, KM_ANY, 0, EYE_MODAL_POINT_CONFIRM);
112         WM_modalkeymap_add_item(keymap, RETKEY, KM_RELEASE, KM_ANY, 0, EYE_MODAL_POINT_CONFIRM);
113         WM_modalkeymap_add_item(keymap, PADENTER, KM_RELEASE, KM_ANY, 0, EYE_MODAL_POINT_CONFIRM);
114         WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_ANY, 0, EYE_MODAL_POINT_SAMPLE);
115         WM_modalkeymap_add_item(keymap, SPACEKEY, KM_RELEASE, KM_ANY, 0, EYE_MODAL_POINT_RESET);
116
117         /* assign to operators */
118         WM_modalkeymap_assign(keymap, "UI_OT_eyedropper_colorband_point");
119
120         return keymap;
121 }
122
123 /** \} */
124
125
126 /* -------------------------------------------------------------------- */
127 /* Utility Functions
128  */
129 /** \name Generic Shared Functions
130  * \{ */
131
132 void eyedropper_draw_cursor_text(const struct bContext *C, const ARegion *ar, const char *name)
133 {
134         const uiFontStyle *fstyle = UI_FSTYLE_WIDGET;
135         wmWindow *win = CTX_wm_window(C);
136         int x = win->eventstate->x;
137         int y = win->eventstate->y;
138         const unsigned char fg[4] = {255, 255, 255, 255};
139         const unsigned char bg[4] = {0, 0, 0, 50};
140
141
142         if ((name[0] == '\0') ||
143             (BLI_rcti_isect_pt(&ar->winrct, x, y) == false))
144         {
145                 return;
146         }
147
148         x = x - ar->winrct.xmin;
149         y = y - ar->winrct.ymin;
150
151         y += U.widget_unit;
152
153         UI_fontstyle_draw_simple_backdrop(fstyle, x, y, name, fg, bg);
154 }
155
156
157 /**
158  * Utility to retrieve a button representing a RNA property that is currently under the cursor.
159  *
160  * This is to be used by any eyedroppers which fetch properties (e.g. UI_OT_eyedropper_driver).
161  * Especially during modal operations (e.g. as with the eyedroppers), context cannot be relied
162  * upon to provide this information, as it is not updated until the operator finishes.
163  *
164  * \return A button under the mouse which relates to some RNA Property, or NULL
165  */
166 uiBut *eyedropper_get_property_button_under_mouse(bContext *C, const wmEvent *event)
167 {
168         wmWindow *win = CTX_wm_window(C);
169         ScrArea *sa = BKE_screen_find_area_xy(win->screen, SPACE_TYPE_ANY, event->x, event->y);
170         ARegion *ar = BKE_area_find_region_xy(sa, RGN_TYPE_ANY, event->x, event->y);
171
172         uiBut *but = ui_but_find_mouse_over(ar, event);
173
174         if (ELEM(NULL, but, but->rnapoin.data, but->rnaprop)) {
175                 return NULL;
176         }
177         else {
178                 return but;
179         }
180 }
181
182 /** \} */
183