doxygen: add newline after \file
[blender.git] / source / blender / editors / interface / interface_icons_event.c
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
17 /** \file
18  * \ingroup edinterface
19  *
20  * A special set of icons to represent input devices,
21  * this is a mix of text (via fonts) and a handful of custom glyphs for special keys.
22  *
23  * Event codes are used as identifiers.
24  */
25
26 #include <math.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "MEM_guardedalloc.h"
31
32 #include "GPU_draw.h"
33 #include "GPU_matrix.h"
34 #include "GPU_batch.h"
35 #include "GPU_immediate.h"
36 #include "GPU_state.h"
37
38 #include "BLI_blenlib.h"
39 #include "BLI_utildefines.h"
40 #include "BLI_math_vector.h"
41
42 #include "DNA_brush_types.h"
43 #include "DNA_curve_types.h"
44 #include "DNA_dynamicpaint_types.h"
45 #include "DNA_object_types.h"
46 #include "DNA_screen_types.h"
47 #include "DNA_space_types.h"
48 #include "DNA_workspace_types.h"
49
50 #include "RNA_access.h"
51 #include "RNA_enum_types.h"
52
53 #include "BKE_icons.h"
54 #include "BKE_appdir.h"
55 #include "BKE_studiolight.h"
56
57 #include "IMB_imbuf.h"
58 #include "IMB_imbuf_types.h"
59 #include "IMB_thumbs.h"
60
61 #include "BLF_api.h"
62
63 #include "DEG_depsgraph.h"
64
65 #include "DRW_engine.h"
66
67 #include "ED_datafiles.h"
68 #include "ED_keyframes_draw.h"
69 #include "ED_render.h"
70
71 #include "UI_interface.h"
72 #include "UI_interface_icons.h"
73
74 #include "WM_api.h"
75 #include "WM_types.h"
76
77 #include "interface_intern.h"
78
79 static void icon_draw_rect_input_small_text_ex(
80         const rctf *rect, const float color[4], const float margin[2], const char *str,
81         int font_size)
82 {
83         BLF_batch_draw_flush();
84         const int font_id = BLF_default();
85         BLF_color4fv(font_id, color);
86         BLF_size(font_id, font_size * U.pixelsize, U.dpi);
87         BLF_position(font_id, rect->xmin + margin[0] * 2, rect->ymin + margin[1] * 5, 0.0f);
88         BLF_draw(font_id, str, BLF_DRAW_STR_DUMMY_MAX);
89         BLF_batch_draw_flush();
90 }
91
92 static void icon_draw_rect_input_small_text(
93         const rctf *rect, const float color[4], const float margin[2], const char *str)
94 {
95         icon_draw_rect_input_small_text_ex(rect, color, margin, str, 8);
96 }
97
98 static void icon_draw_rect_input_default_text(
99         const rctf *rect,
100         const float color[4], const float margin[2], const char *str)
101 {
102         BLF_batch_draw_flush();
103         const int font_id = BLF_default();
104         BLF_color4fv(font_id, color);
105         BLF_position(font_id, (int)(rect->xmin + margin[0] * 5), (int)(rect->ymin + margin[1] * 5), 0.0f);
106         BLF_draw(font_id, str, BLF_DRAW_STR_DUMMY_MAX);
107         BLF_batch_draw_flush();
108 }
109
110 static void icon_draw_rect_input_mono_text(
111         const rctf *rect,
112         const float color[4], const float margin[2], const char *str)
113 {
114         BLF_batch_draw_flush();
115         const int font_id = blf_mono_font;
116         BLF_color4fv(font_id, color);
117         BLF_size(font_id, 20 * U.pixelsize, U.dpi);
118         BLF_position(font_id, (int)(rect->xmin + margin[0] * 5), (int)(rect->ymin + margin[1] * 5), 0.0f);
119         BLF_draw(font_id, str, BLF_DRAW_STR_DUMMY_MAX);
120         BLF_batch_draw_flush();
121 }
122
123 static void icon_draw_rect_input_line_prim(
124         const rctf *rect,
125         const float color[4],
126         const int prim,
127         const char lines[][2], int lines_len)
128 {
129         glEnable(GL_LINE_SMOOTH);
130         glEnable(GL_BLEND);
131         BLI_assert(ELEM(prim, GPU_PRIM_LINE_LOOP, GPU_PRIM_LINE_STRIP));
132         const uint pos_id = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
133         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
134         immUniformColor4fv(color);
135         immBegin(prim, lines_len);
136         float w_inv = BLI_rctf_size_x(rect) / 255.0f;
137         float h_inv = BLI_rctf_size_y(rect) / 255.0f;
138         for (int i = 0; i < lines_len; i++) {
139                 immVertex2f(
140                         pos_id,
141                         round_fl_to_int(rect->xmin + ((float)lines[i][0] * w_inv)),
142                         round_fl_to_int(rect->ymin + ((float)lines[i][1] * h_inv))
143                 );
144         }
145         immEnd();
146         immUnbindProgram();
147         glDisable(GL_LINE_SMOOTH);
148         glDisable(GL_BLEND);
149 }
150
151 void icon_draw_rect_input(
152         float x, float y, int w, int h, float UNUSED(alpha),
153         short event_type, short UNUSED(event_value))
154 {
155         float color[4];
156         const float margin[2] = {w / 20.0f, h / 20.0f};
157         GPU_line_width(1.0f);
158         UI_GetThemeColor4fv(TH_TEXT, color);
159         UI_draw_roundbox_corner_set(UI_CNR_ALL);
160         UI_draw_roundbox_aa(
161                 false,
162                 (int)x,
163                 (int)y,
164                 (int)(x + w),
165                 (int)(y + h), 4.0f, color
166         );
167
168         const rctf rect = {
169                 .xmin = x,
170                 .ymin = y,
171                 .xmax = x + w,
172                 .ymax = y + h,
173         };
174
175         const bool simple_text = false;
176
177         if ((event_type >= AKEY) || (ZKEY <= event_type)) {
178                 char str[2] = {'A' + (event_type - AKEY), '\0'};
179                 icon_draw_rect_input_default_text(&rect, color, margin, str);
180         }
181         if ((event_type >= F1KEY) || (F12KEY <= event_type)) {
182                 char str[3] = {'F', '1' + (event_type - F1KEY), '\0'};
183                 icon_draw_rect_input_default_text(&rect, color, margin, str);
184         }
185         else if (event_type == LEFTSHIFTKEY) {
186                 if (simple_text) {
187                         icon_draw_rect_input_small_text(&rect, color, margin, "Shift");
188                 }
189                 else {
190                         rctf rect_ofs = rect;
191                         BLI_rctf_translate(&rect_ofs, (w / -14.0f), (w / -14.0f));
192                         icon_draw_rect_input_mono_text(&rect_ofs, color, margin, (const char[]){0xe2, 0x87, 0xa7, 0x0});
193                 }
194         }
195         else if (event_type == LEFTCTRLKEY) {
196                 if (simple_text) {
197                         icon_draw_rect_input_small_text(&rect, color, margin, "Ctrl");
198                 }
199                 else {
200                         rctf rect_ofs = rect;
201                         BLI_rctf_translate(&rect_ofs, (w / -16.0f), 0.0f);
202                         icon_draw_rect_input_default_text(&rect_ofs, color, margin, "^");
203                 }
204         }
205         else if (event_type == LEFTALTKEY) {
206                 if (simple_text) {
207                         icon_draw_rect_input_small_text(&rect, color, margin, "Alt");
208                 }
209                 else {
210                         rctf rect_ofs = rect;
211                         BLI_rctf_translate(&rect_ofs, (w / -8.0f), 0.0f);
212                         icon_draw_rect_input_default_text(&rect_ofs, color, margin, (const char[]){0xe2, 0x8c, 0xa5, 0x0});
213                 }
214         }
215         else if (event_type == OSKEY) {
216                 icon_draw_rect_input_small_text(&rect, color, margin, "OS");
217         }
218         else if (event_type == DELKEY) {
219                 icon_draw_rect_input_small_text(&rect, color, margin, "Del");
220         }
221         else if (event_type == TABKEY) {
222                 if (simple_text) {
223                         icon_draw_rect_input_small_text(&rect, color, margin, "Tab");
224                 }
225                 else {
226                         rctf rect_ofs = rect;
227                         BLI_rctf_translate(&rect_ofs, (w / -12.0f), (w / -12.0f));
228                         icon_draw_rect_input_mono_text(&rect_ofs, color, margin, (const char[]){0xe2, 0x86, 0xb9, 0x0});
229                 }
230         }
231         else if (event_type == HOMEKEY) {
232                 if (simple_text) {
233                         icon_draw_rect_input_small_text(&rect, color, margin, "Home");
234                 }
235                 else {
236                         rctf rect_ofs = rect;
237                         BLI_rctf_translate(&rect_ofs, (w / -12.0f), (w / -12.0f));
238                         icon_draw_rect_input_mono_text(&rect_ofs, color, margin, (const char[]){0xe2, 0x87, 0xa4, 0x0});
239                 }
240         }
241         else if (event_type == ENDKEY) {
242                 if (simple_text) {
243                         icon_draw_rect_input_small_text(&rect, color, margin, "End");
244                 }
245                 else {
246                         rctf rect_ofs = rect;
247                         BLI_rctf_translate(&rect_ofs, (w / -12.0f), (w / -12.0f));
248                         icon_draw_rect_input_mono_text(&rect_ofs, color, margin, (const char[]){0xe2, 0x87, 0xa5, 0x0});
249                 }
250         }
251         else if (event_type == RETKEY) {
252                 if (simple_text) {
253                         icon_draw_rect_input_small_text(&rect, color, margin, "Ret");
254                 }
255                 else {
256                         rctf rect_ofs = rect;
257                         BLI_rctf_translate(&rect_ofs, (w / -8.0f), (w / -6.0f));
258                         icon_draw_rect_input_mono_text(&rect_ofs, color, margin, (const char[]){0xe2, 0x8f, 0x8e, 0x0});
259                 }
260         }
261         else if (event_type == ESCKEY) {
262                 icon_draw_rect_input_small_text(&rect, color, margin, "Esc");
263         }
264         else if (event_type == PAGEUPKEY) {
265                 icon_draw_rect_input_small_text_ex(&rect, color, margin, (const char[]){'P', 0xe2, 0x86, 0x91, 0x0}, 10);
266         }
267         else if (event_type == PAGEDOWNKEY) {
268                 icon_draw_rect_input_small_text_ex(&rect, color, margin, (const char[]){'P', 0xe2, 0x86, 0x93, 0x0}, 10);
269         }
270         else if (event_type == LEFTARROWKEY) {
271                 icon_draw_rect_input_default_text(&rect, color, margin, (const char[]){0xe2, 0x86, 0x90, 0x0});
272         }
273         else if (event_type == UPARROWKEY) {
274                 icon_draw_rect_input_default_text(&rect, color, margin, (const char[]){0xe2, 0x86, 0x91, 0x0});
275         }
276         else if (event_type == RIGHTARROWKEY) {
277                 icon_draw_rect_input_default_text(&rect, color, margin, (const char[]){0xe2, 0x86, 0x92, 0x0});
278         }
279         else if (event_type == DOWNARROWKEY) {
280                 icon_draw_rect_input_default_text(&rect, color, margin, (const char[]){0xe2, 0x86, 0x93, 0x0});
281         }
282         else if (event_type == SPACEKEY) {
283                 const uchar lines[] = {60, 118, 60, 60, 195, 60, 195, 118};
284                 icon_draw_rect_input_line_prim(
285                         &rect, color, GPU_PRIM_LINE_STRIP,
286                         (const void *)lines, ARRAY_SIZE(lines) / 2);
287         }
288 }