Fix T57177: redo select random operator selects hidden objects.
[blender.git] / source / blender / editors / interface / interface_icons.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) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributors: Blender Foundation, full recode
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/interface/interface_icons.c
27  *  \ingroup edinterface
28  */
29
30 #include <math.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "GPU_draw.h"
37 #include "GPU_matrix.h"
38 #include "GPU_batch.h"
39 #include "GPU_immediate.h"
40 #include "GPU_state.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_utildefines.h"
44 #include "BLI_fileops_types.h"
45 #include "BLI_math_vector.h"
46
47 #include "DNA_brush_types.h"
48 #include "DNA_curve_types.h"
49 #include "DNA_dynamicpaint_types.h"
50 #include "DNA_gpencil_types.h"
51 #include "DNA_object_types.h"
52 #include "DNA_screen_types.h"
53 #include "DNA_space_types.h"
54 #include "DNA_workspace_types.h"
55
56 #include "RNA_access.h"
57 #include "RNA_enum_types.h"
58
59 #include "BKE_context.h"
60 #include "BKE_global.h"
61 #include "BKE_paint.h"
62 #include "BKE_icons.h"
63 #include "BKE_appdir.h"
64 #include "BKE_studiolight.h"
65
66 #include "IMB_imbuf.h"
67 #include "IMB_imbuf_types.h"
68 #include "IMB_thumbs.h"
69
70 #include "BIF_glutil.h"
71
72 #include "DEG_depsgraph.h"
73
74 #include "DRW_engine.h"
75
76 #include "ED_datafiles.h"
77 #include "ED_keyframes_draw.h"
78 #include "ED_render.h"
79
80 #include "UI_interface.h"
81 #include "UI_interface_icons.h"
82
83 #include "WM_api.h"
84 #include "WM_types.h"
85
86 #include "interface_intern.h"
87
88 #ifndef WITH_HEADLESS
89 #define ICON_GRID_COLS      26
90 #define ICON_GRID_ROWS      30
91
92 #define ICON_GRID_MARGIN    10
93 #define ICON_GRID_W         32
94 #define ICON_GRID_H         32
95 #endif  /* WITH_HEADLESS */
96
97 typedef struct IconImage {
98         int w;
99         int h;
100         unsigned int *rect;
101         unsigned char *datatoc_rect;
102         int datatoc_size;
103 } IconImage;
104
105 typedef void (*VectorDrawFunc)(int x, int y, int w, int h, float alpha);
106
107 #define ICON_TYPE_PREVIEW        0
108 #define ICON_TYPE_COLOR_TEXTURE  1
109 #define ICON_TYPE_MONO_TEXTURE   2
110 #define ICON_TYPE_BUFFER         3
111 #define ICON_TYPE_VECTOR         4
112 #define ICON_TYPE_GEOM           5
113 #define ICON_TYPE_EVENT          6  /* draw keymap entries using custom renderer. */
114 #define ICON_TYPE_GPLAYER        7
115 #define ICON_TYPE_BLANK          8
116
117 typedef struct DrawInfo {
118         int type;
119
120         union {
121                 /* type specific data */
122                 struct {
123                         VectorDrawFunc func;
124                 } vector;
125                 struct {
126                         ImBuf *image_cache;
127                 } geom;
128                 struct {
129                         IconImage *image;
130                 } buffer;
131                 struct {
132                         int x, y, w, h;
133                         int theme_color;
134                 } texture;
135                 struct {
136                         /* Can be packed into a single int. */
137                         short event_type;
138                         short event_value;
139                         int icon;
140                         /* Allow lookups. */
141                         struct DrawInfo *next;
142                 } input;
143         } data;
144 } DrawInfo;
145
146 typedef struct IconTexture {
147         GLuint id;
148         int w;
149         int h;
150         float invw;
151         float invh;
152 } IconTexture;
153
154 typedef struct IconType {
155         int type;
156         int theme_color;
157 } IconType;
158
159 /* ******************* STATIC LOCAL VARS ******************* */
160 /* static here to cache results of icon directory scan, so it's not
161  * scanning the filesystem each time the menu is drawn */
162 static struct ListBase iconfilelist = {NULL, NULL};
163 static IconTexture icongltex = {0, 0, 0, 0.0f, 0.0f};
164
165 #ifndef WITH_HEADLESS
166
167 static const IconType icontypes[] = {
168 #define DEF_ICON(name) {ICON_TYPE_MONO_TEXTURE, 0},
169 #define DEF_ICON_COLLECTION(name) {ICON_TYPE_MONO_TEXTURE, TH_ICON_COLLECTION},
170 #define DEF_ICON_OBJECT(name) {ICON_TYPE_MONO_TEXTURE, TH_ICON_OBJECT},
171 #define DEF_ICON_OBJECT_DATA(name) {ICON_TYPE_MONO_TEXTURE, TH_ICON_OBJECT_DATA},
172 #define DEF_ICON_MODIFIER(name) {ICON_TYPE_MONO_TEXTURE, TH_ICON_MODIFIER},
173 #define DEF_ICON_SHADING(name) {ICON_TYPE_MONO_TEXTURE, TH_ICON_SHADING},
174 #define DEF_ICON_VECTOR(name) {ICON_TYPE_VECTOR, 0},
175 #define DEF_ICON_COLOR(name) {ICON_TYPE_COLOR_TEXTURE, 0},
176 #define DEF_ICON_BLANK(name) {ICON_TYPE_BLANK, 0},
177 #include "UI_icons.h"
178 };
179
180 /* **************************************************** */
181
182 static DrawInfo *def_internal_icon(ImBuf *bbuf, int icon_id, int xofs, int yofs, int size, int type, int theme_color)
183 {
184         Icon *new_icon = NULL;
185         IconImage *iimg = NULL;
186         DrawInfo *di;
187
188         new_icon = MEM_callocN(sizeof(Icon), "texicon");
189
190         new_icon->obj = NULL; /* icon is not for library object */
191         new_icon->id_type = 0;
192
193         di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
194         di->type = type;
195
196         if (ELEM(type, ICON_TYPE_COLOR_TEXTURE, ICON_TYPE_MONO_TEXTURE)) {
197                 di->data.texture.theme_color = theme_color;
198                 di->data.texture.x = xofs;
199                 di->data.texture.y = yofs;
200                 di->data.texture.w = size;
201                 di->data.texture.h = size;
202         }
203         else if (type == ICON_TYPE_BUFFER) {
204                 iimg = MEM_callocN(sizeof(IconImage), "icon_img");
205                 iimg->w = size;
206                 iimg->h = size;
207
208                 /* icon buffers can get initialized runtime now, via datatoc */
209                 if (bbuf) {
210                         int y, imgsize;
211
212                         iimg->rect = MEM_mallocN(size * size * sizeof(unsigned int), "icon_rect");
213
214                         /* Here we store the rect in the icon - same as before */
215                         if (size == bbuf->x && size == bbuf->y && xofs == 0 && yofs == 0)
216                                 memcpy(iimg->rect, bbuf->rect, size * size * sizeof(int));
217                         else {
218                                 /* this code assumes square images */
219                                 imgsize = bbuf->x;
220                                 for (y = 0; y < size; y++) {
221                                         memcpy(&iimg->rect[y * size], &bbuf->rect[(y + yofs) * imgsize + xofs], size * sizeof(int));
222                                 }
223                         }
224                 }
225                 di->data.buffer.image = iimg;
226         }
227
228         new_icon->drawinfo_free = UI_icons_free_drawinfo;
229         new_icon->drawinfo = di;
230
231         BKE_icon_set(icon_id, new_icon);
232
233         return di;
234 }
235
236 static void def_internal_vicon(int icon_id, VectorDrawFunc drawFunc)
237 {
238         Icon *new_icon = NULL;
239         DrawInfo *di;
240
241         new_icon = MEM_callocN(sizeof(Icon), "texicon");
242
243         new_icon->obj = NULL; /* icon is not for library object */
244         new_icon->id_type = 0;
245
246         di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
247         di->type = ICON_TYPE_VECTOR;
248         di->data.vector.func = drawFunc;
249
250         new_icon->drawinfo_free = NULL;
251         new_icon->drawinfo = di;
252
253         BKE_icon_set(icon_id, new_icon);
254 }
255
256 /* Vector Icon Drawing Routines */
257
258 /* Utilities */
259
260 static void viconutil_set_point(GLint pt[2], int x, int y)
261 {
262         pt[0] = x;
263         pt[1] = y;
264 }
265
266 static void vicon_small_tri_right_draw(int x, int y, int w, int UNUSED(h), float alpha)
267 {
268         GLint pts[3][2];
269         int cx = x + w / 2 - 4;
270         int cy = y + w / 2;
271         int d = w / 5, d2 = w / 7;
272
273         viconutil_set_point(pts[0], cx - d2, cy + d);
274         viconutil_set_point(pts[1], cx - d2, cy - d);
275         viconutil_set_point(pts[2], cx + d2, cy);
276
277         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
278         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
279         immUniformColor4f(0.2f, 0.2f, 0.2f, alpha);
280
281         immBegin(GPU_PRIM_TRIS, 3);
282         immVertex2iv(pos, pts[0]);
283         immVertex2iv(pos, pts[1]);
284         immVertex2iv(pos, pts[2]);
285         immEnd();
286
287         immUnbindProgram();
288 }
289
290 static void vicon_keytype_draw_wrapper(int x, int y, int w, int h, float alpha, short key_type, short handle_type)
291 {
292         /* init dummy theme state for Action Editor - where these colors are defined
293          * (since we're doing this offscreen, free from any particular space_id)
294          */
295         struct bThemeState theme_state;
296
297         UI_Theme_Store(&theme_state);
298         UI_SetTheme(SPACE_ACTION, RGN_TYPE_WINDOW);
299
300         /* the "x" and "y" given are the bottom-left coordinates of the icon,
301          * while the draw_keyframe_shape() function needs the midpoint for
302          * the keyframe
303          */
304         float xco = x + w / 2 + 0.5f;
305         float yco = y + h / 2 + 0.5f;
306
307         GPUVertFormat *format = immVertexFormat();
308         uint pos_id = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
309         uint size_id = GPU_vertformat_attr_add(format, "size", GPU_COMP_F32, 1, GPU_FETCH_FLOAT);
310         uint color_id = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
311         uint outline_color_id = GPU_vertformat_attr_add(format, "outlineColor", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
312         uint flags_id = GPU_vertformat_attr_add(format, "flags", GPU_COMP_U32, 1, GPU_FETCH_INT);
313
314         immBindBuiltinProgram(GPU_SHADER_KEYFRAME_DIAMOND);
315         GPU_enable_program_point_size();
316         immUniform2f("ViewportSize", -1.0f, -1.0f);
317         immBegin(GPU_PRIM_POINTS, 1);
318
319         /* draw keyframe
320          * - size: (default icon size == 16, default dopesheet icon size == 10)
321          * - sel: true unless in handletype icons (so that "keyframe" state shows the iconic yellow icon)
322          */
323         bool sel = (handle_type == KEYFRAME_HANDLE_NONE);
324
325         draw_keyframe_shape(xco, yco, (10.0f / 16.0f) * h, sel, key_type, KEYFRAME_SHAPE_BOTH, alpha,
326                             pos_id, size_id, color_id, outline_color_id,
327                             flags_id, handle_type, KEYFRAME_EXTREME_NONE);
328
329         immEnd();
330         GPU_disable_program_point_size();
331         immUnbindProgram();
332
333         UI_Theme_Restore(&theme_state);
334 }
335
336 static void vicon_keytype_keyframe_draw(int x, int y, int w, int h, float alpha)
337 {
338         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_KEYFRAME, KEYFRAME_HANDLE_NONE);
339 }
340
341 static void vicon_keytype_breakdown_draw(int x, int y, int w, int h, float alpha)
342 {
343         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_BREAKDOWN, KEYFRAME_HANDLE_NONE);
344 }
345
346 static void vicon_keytype_extreme_draw(int x, int y, int w, int h, float alpha)
347 {
348         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_EXTREME, KEYFRAME_HANDLE_NONE);
349 }
350
351 static void vicon_keytype_jitter_draw(int x, int y, int w, int h, float alpha)
352 {
353         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_JITTER, KEYFRAME_HANDLE_NONE);
354 }
355
356 static void vicon_keytype_moving_hold_draw(int x, int y, int w, int h, float alpha)
357 {
358         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_MOVEHOLD, KEYFRAME_HANDLE_NONE);
359 }
360
361 static void vicon_handletype_free_draw(int x, int y, int w, int h, float alpha)
362 {
363         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_KEYFRAME, KEYFRAME_HANDLE_FREE);
364 }
365
366 static void vicon_handletype_aligned_draw(int x, int y, int w, int h, float alpha)
367 {
368         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_KEYFRAME, KEYFRAME_HANDLE_ALIGNED);
369 }
370
371 static void vicon_handletype_vector_draw(int x, int y, int w, int h, float alpha)
372 {
373         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_KEYFRAME, KEYFRAME_HANDLE_VECTOR);
374 }
375
376 static void vicon_handletype_auto_draw(int x, int y, int w, int h, float alpha)
377 {
378         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_KEYFRAME, KEYFRAME_HANDLE_AUTO);
379 }
380
381 static void vicon_handletype_auto_clamp_draw(int x, int y, int w, int h, float alpha)
382 {
383         vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_KEYFRAME, KEYFRAME_HANDLE_AUTO_CLAMP);
384 }
385
386 static void vicon_colorset_draw(int index, int x, int y, int w, int h, float UNUSED(alpha))
387 {
388         bTheme *btheme = UI_GetTheme();
389         ThemeWireColor *cs = &btheme->tarm[index];
390
391         /* Draw three bands of color: One per color
392          *    x-----a-----b-----c
393          *    |  N  |  S  |  A  |
394          *    x-----a-----b-----c
395          */
396         const int a = x + w / 3;
397         const int b = x + w / 3 * 2;
398         const int c = x + w;
399
400         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
401         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
402
403         /* XXX: Include alpha into this... */
404         /* normal */
405         immUniformColor3ubv((unsigned char *)cs->solid);
406         immRecti(pos, x, y, a, y + h);
407
408         /* selected */
409         immUniformColor3ubv((unsigned char *)cs->select);
410         immRecti(pos, a, y, b, y + h);
411
412         /* active */
413         immUniformColor3ubv((unsigned char *)cs->active);
414         immRecti(pos, b, y, c, y + h);
415
416         immUnbindProgram();
417 }
418
419 #define DEF_ICON_VECTOR_COLORSET_DRAW_NTH(prefix, index)                              \
420         static void vicon_colorset_draw_##prefix(int x, int y, int w, int h, float alpha) \
421         {                                                                                 \
422                 vicon_colorset_draw(index, x, y, w, h, alpha);                                \
423         }
424
425 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(01, 0)
426 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(02, 1)
427 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(03, 2)
428 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(04, 3)
429 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(05, 4)
430 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(06, 5)
431 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(07, 6)
432 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(08, 7)
433 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(09, 8)
434 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(10, 9)
435 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(11, 10)
436 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(12, 11)
437 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(13, 12)
438 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(14, 13)
439 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(15, 14)
440 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(16, 15)
441 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(17, 16)
442 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(18, 17)
443 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(19, 18)
444 DEF_ICON_VECTOR_COLORSET_DRAW_NTH(20, 19)
445
446 #undef DEF_ICON_VECTOR_COLORSET_DRAW_NTH
447
448 /* Dynamically render icon instead of rendering a plain color to a texture/buffer
449  * This is mot strictly a "vicon", as it needs access to icon->obj to get the color info,
450  * but it works in a very similar way.
451  */
452 static void vicon_gplayer_color_draw(Icon *icon, int x, int y, int w, int h)
453 {
454         bGPDlayer *gpl = (bGPDlayer *)icon->obj;
455
456         /* Just draw a colored rect - Like for vicon_colorset_draw() */
457         /* TODO: Make this have rounded corners, and maybe be a bit smaller.
458          * However, UI_draw_roundbox_aa() draws the colors too dark, so can't be used.
459          */
460         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
461         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
462
463         immUniformColor3fv(gpl->color);
464         immRecti(pos, x, y, x + w - 1, y + h - 1);
465
466         immUnbindProgram();
467 }
468
469
470 #ifndef WITH_HEADLESS
471
472 static void init_brush_icons(void)
473 {
474
475 #define INIT_BRUSH_ICON(icon_id, name)                                          \
476         {                                                                           \
477                 unsigned char *rect = (unsigned char *)datatoc_ ##name## _png;          \
478                 int size = datatoc_ ##name## _png_size;                                 \
479                 DrawInfo *di;                                                           \
480                 \
481                 di = def_internal_icon(NULL, icon_id, 0, 0, w, ICON_TYPE_BUFFER, 0);    \
482                 di->data.buffer.image->datatoc_rect = rect;                             \
483                 di->data.buffer.image->datatoc_size = size;                             \
484         }
485         /* end INIT_BRUSH_ICON */
486
487         const int w = 96; /* warning, brush size hardcoded in C, but it gets scaled */
488
489         INIT_BRUSH_ICON(ICON_BRUSH_BLOB, blob);
490         INIT_BRUSH_ICON(ICON_BRUSH_BLUR, blur);
491         INIT_BRUSH_ICON(ICON_BRUSH_CLAY, clay);
492         INIT_BRUSH_ICON(ICON_BRUSH_CLAY_STRIPS, claystrips);
493         INIT_BRUSH_ICON(ICON_BRUSH_CLONE, clone);
494         INIT_BRUSH_ICON(ICON_BRUSH_CREASE, crease);
495         INIT_BRUSH_ICON(ICON_BRUSH_SCULPT_DRAW, draw);
496         INIT_BRUSH_ICON(ICON_BRUSH_FILL, fill);
497         INIT_BRUSH_ICON(ICON_BRUSH_FLATTEN, flatten);
498         INIT_BRUSH_ICON(ICON_BRUSH_GRAB, grab);
499         INIT_BRUSH_ICON(ICON_BRUSH_INFLATE, inflate);
500         INIT_BRUSH_ICON(ICON_BRUSH_LAYER, layer);
501         INIT_BRUSH_ICON(ICON_BRUSH_MASK, mask);
502         INIT_BRUSH_ICON(ICON_BRUSH_MIX, mix);
503         INIT_BRUSH_ICON(ICON_BRUSH_NUDGE, nudge);
504         INIT_BRUSH_ICON(ICON_BRUSH_PINCH, pinch);
505         INIT_BRUSH_ICON(ICON_BRUSH_SCRAPE, scrape);
506         INIT_BRUSH_ICON(ICON_BRUSH_SMEAR, smear);
507         INIT_BRUSH_ICON(ICON_BRUSH_SMOOTH, smooth);
508         INIT_BRUSH_ICON(ICON_BRUSH_SNAKE_HOOK, snake_hook);
509         INIT_BRUSH_ICON(ICON_BRUSH_SOFTEN, soften);
510         INIT_BRUSH_ICON(ICON_BRUSH_TEXDRAW, texdraw);
511         INIT_BRUSH_ICON(ICON_BRUSH_TEXFILL, texfill);
512         INIT_BRUSH_ICON(ICON_BRUSH_TEXMASK, texmask);
513         INIT_BRUSH_ICON(ICON_BRUSH_THUMB, thumb);
514         INIT_BRUSH_ICON(ICON_BRUSH_ROTATE, twist);
515
516         /* grease pencil sculpt */
517         INIT_BRUSH_ICON(ICON_GPBRUSH_SMOOTH, gp_brush_smooth);
518         INIT_BRUSH_ICON(ICON_GPBRUSH_THICKNESS, gp_brush_thickness);
519         INIT_BRUSH_ICON(ICON_GPBRUSH_STRENGTH, gp_brush_strength);
520         INIT_BRUSH_ICON(ICON_GPBRUSH_GRAB, gp_brush_grab);
521         INIT_BRUSH_ICON(ICON_GPBRUSH_PUSH, gp_brush_push);
522         INIT_BRUSH_ICON(ICON_GPBRUSH_TWIST, gp_brush_twist);
523         INIT_BRUSH_ICON(ICON_GPBRUSH_PINCH, gp_brush_pinch);
524         INIT_BRUSH_ICON(ICON_GPBRUSH_RANDOMIZE, gp_brush_randomize);
525         INIT_BRUSH_ICON(ICON_GPBRUSH_CLONE, gp_brush_clone);
526         INIT_BRUSH_ICON(ICON_GPBRUSH_WEIGHT, gp_brush_weight);
527
528         /* grease pencil drawing brushes */
529         INIT_BRUSH_ICON(ICON_GPBRUSH_PENCIL, gp_brush_pencil);
530         INIT_BRUSH_ICON(ICON_GPBRUSH_PEN, gp_brush_pen);
531         INIT_BRUSH_ICON(ICON_GPBRUSH_INK, gp_brush_ink);
532         INIT_BRUSH_ICON(ICON_GPBRUSH_INKNOISE, gp_brush_inknoise);
533         INIT_BRUSH_ICON(ICON_GPBRUSH_BLOCK, gp_brush_block);
534         INIT_BRUSH_ICON(ICON_GPBRUSH_MARKER, gp_brush_marker);
535         INIT_BRUSH_ICON(ICON_GPBRUSH_FILL, gp_brush_fill);
536         INIT_BRUSH_ICON(ICON_GPBRUSH_ERASE_SOFT, gp_brush_erase_soft);
537         INIT_BRUSH_ICON(ICON_GPBRUSH_ERASE_HARD, gp_brush_erase_hard);
538         INIT_BRUSH_ICON(ICON_GPBRUSH_ERASE_STROKE, gp_brush_erase_stroke);
539
540 #undef INIT_BRUSH_ICON
541 }
542
543 static DrawInfo *g_di_event_list = NULL;
544
545 int UI_icon_from_event_type(short event_type, short event_value)
546 {
547         if (event_type == RIGHTSHIFTKEY) {
548                 event_type = LEFTSHIFTKEY;
549         }
550         else if (event_type == RIGHTCTRLKEY) {
551                 event_type = LEFTCTRLKEY;
552         }
553         else if (event_type == RIGHTALTKEY) {
554                 event_type = LEFTALTKEY;
555         }
556         else if (event_type == EVT_TWEAK_L) {
557                 event_type = LEFTMOUSE;
558                 event_value = KM_CLICK_DRAG;
559         }
560         else if (event_type == EVT_TWEAK_M) {
561                 event_type = MIDDLEMOUSE;
562                 event_value = KM_CLICK_DRAG;
563         }
564         else if (event_type == EVT_TWEAK_R) {
565                 event_type = RIGHTMOUSE;
566                 event_value = KM_CLICK_DRAG;
567         }
568
569         DrawInfo *di = g_di_event_list;
570         do {
571                 if (di->data.input.event_type == event_type) {
572                         return di->data.input.icon;
573                 }
574         } while ((di = di->data.input.next));
575
576         if (event_type == LEFTMOUSE) {
577                 return ELEM(event_value, KM_CLICK, KM_PRESS) ? ICON_MOUSE_LMB : ICON_MOUSE_LMB_DRAG;
578         }
579         else if (event_type == MIDDLEMOUSE) {
580                 return ELEM(event_value, KM_CLICK, KM_PRESS) ? ICON_MOUSE_MMB : ICON_MOUSE_MMB_DRAG;
581         }
582         else if (event_type == RIGHTMOUSE) {
583                 return ELEM(event_value, KM_CLICK, KM_PRESS) ? ICON_MOUSE_RMB : ICON_MOUSE_RMB_DRAG;
584         }
585
586         return ICON_NONE;
587 }
588
589 int UI_icon_from_keymap_item(const wmKeyMapItem *kmi, int r_icon_mod[4])
590 {
591         if (r_icon_mod) {
592                 memset(r_icon_mod, 0x0, sizeof(int[4]));
593                 int i = 0;
594                 if (!ELEM(kmi->ctrl, KM_NOTHING, KM_ANY)) {
595                         r_icon_mod[i++] = ICON_EVENT_CTRL;
596                 }
597                 if (!ELEM(kmi->alt, KM_NOTHING, KM_ANY)) {
598                         r_icon_mod[i++] = ICON_EVENT_ALT;
599                 }
600                 if (!ELEM(kmi->shift, KM_NOTHING, KM_ANY)) {
601                         r_icon_mod[i++] = ICON_EVENT_SHIFT;
602                 }
603                 if (!ELEM(kmi->oskey, KM_NOTHING, KM_ANY)) {
604                         r_icon_mod[i++] = ICON_EVENT_OS;
605                 }
606         }
607         return UI_icon_from_event_type(kmi->type, kmi->val);
608 }
609
610 static void init_event_icons(void)
611 {
612         DrawInfo *di_next = NULL;
613
614 #define INIT_EVENT_ICON(icon_id, type, value) \
615         { \
616                 DrawInfo *di = def_internal_icon(NULL, icon_id, 0, 0, w, ICON_TYPE_EVENT, 0); \
617                 di->data.input.event_type = type; \
618                 di->data.input.event_value = value; \
619                 di->data.input.icon = icon_id; \
620                 di->data.input.next = di_next; \
621                 di_next = di; \
622         }
623         /* end INIT_EVENT_ICON */
624
625         const int w = 16; /* DUMMY */
626
627         INIT_EVENT_ICON(ICON_EVENT_A, AKEY, KM_ANY);
628         INIT_EVENT_ICON(ICON_EVENT_B, BKEY, KM_ANY);
629         INIT_EVENT_ICON(ICON_EVENT_C, CKEY, KM_ANY);
630         INIT_EVENT_ICON(ICON_EVENT_D, DKEY, KM_ANY);
631         INIT_EVENT_ICON(ICON_EVENT_E, EKEY, KM_ANY);
632         INIT_EVENT_ICON(ICON_EVENT_F, FKEY, KM_ANY);
633         INIT_EVENT_ICON(ICON_EVENT_G, GKEY, KM_ANY);
634         INIT_EVENT_ICON(ICON_EVENT_H, HKEY, KM_ANY);
635         INIT_EVENT_ICON(ICON_EVENT_I, IKEY, KM_ANY);
636         INIT_EVENT_ICON(ICON_EVENT_J, JKEY, KM_ANY);
637         INIT_EVENT_ICON(ICON_EVENT_K, KKEY, KM_ANY);
638         INIT_EVENT_ICON(ICON_EVENT_L, LKEY, KM_ANY);
639         INIT_EVENT_ICON(ICON_EVENT_M, MKEY, KM_ANY);
640         INIT_EVENT_ICON(ICON_EVENT_N, NKEY, KM_ANY);
641         INIT_EVENT_ICON(ICON_EVENT_O, OKEY, KM_ANY);
642         INIT_EVENT_ICON(ICON_EVENT_P, PKEY, KM_ANY);
643         INIT_EVENT_ICON(ICON_EVENT_Q, QKEY, KM_ANY);
644         INIT_EVENT_ICON(ICON_EVENT_R, RKEY, KM_ANY);
645         INIT_EVENT_ICON(ICON_EVENT_S, SKEY, KM_ANY);
646         INIT_EVENT_ICON(ICON_EVENT_T, TKEY, KM_ANY);
647         INIT_EVENT_ICON(ICON_EVENT_U, UKEY, KM_ANY);
648         INIT_EVENT_ICON(ICON_EVENT_V, VKEY, KM_ANY);
649         INIT_EVENT_ICON(ICON_EVENT_W, WKEY, KM_ANY);
650         INIT_EVENT_ICON(ICON_EVENT_X, XKEY, KM_ANY);
651         INIT_EVENT_ICON(ICON_EVENT_Y, YKEY, KM_ANY);
652         INIT_EVENT_ICON(ICON_EVENT_Z, ZKEY, KM_ANY);
653         INIT_EVENT_ICON(ICON_EVENT_SHIFT, LEFTSHIFTKEY, KM_ANY);
654         INIT_EVENT_ICON(ICON_EVENT_CTRL, LEFTCTRLKEY, KM_ANY);
655         INIT_EVENT_ICON(ICON_EVENT_ALT, LEFTALTKEY, KM_ANY);
656         INIT_EVENT_ICON(ICON_EVENT_OS, OSKEY, KM_ANY);
657         INIT_EVENT_ICON(ICON_EVENT_F1,  F1KEY, KM_ANY);
658         INIT_EVENT_ICON(ICON_EVENT_F2,  F2KEY, KM_ANY);
659         INIT_EVENT_ICON(ICON_EVENT_F3,  F3KEY, KM_ANY);
660         INIT_EVENT_ICON(ICON_EVENT_F4,  F4KEY, KM_ANY);
661         INIT_EVENT_ICON(ICON_EVENT_F5,  F5KEY, KM_ANY);
662         INIT_EVENT_ICON(ICON_EVENT_F6,  F6KEY, KM_ANY);
663         INIT_EVENT_ICON(ICON_EVENT_F7,  F7KEY, KM_ANY);
664         INIT_EVENT_ICON(ICON_EVENT_F8,  F8KEY, KM_ANY);
665         INIT_EVENT_ICON(ICON_EVENT_F9,  F9KEY, KM_ANY);
666         INIT_EVENT_ICON(ICON_EVENT_F10, F10KEY, KM_ANY);
667         INIT_EVENT_ICON(ICON_EVENT_F11, F11KEY, KM_ANY);
668         INIT_EVENT_ICON(ICON_EVENT_F12, F12KEY, KM_ANY);
669         INIT_EVENT_ICON(ICON_EVENT_ESC, ESCKEY, KM_ANY);
670         INIT_EVENT_ICON(ICON_EVENT_TAB, TABKEY, KM_ANY);
671         INIT_EVENT_ICON(ICON_EVENT_PAGEUP, PAGEUPKEY, KM_ANY);
672         INIT_EVENT_ICON(ICON_EVENT_PAGEDOWN, PAGEDOWNKEY, KM_ANY);
673         INIT_EVENT_ICON(ICON_EVENT_RETURN, RETKEY, KM_ANY);
674
675         g_di_event_list = di_next;
676
677 #undef INIT_EVENT_ICON
678 }
679
680 static void icon_verify_datatoc(IconImage *iimg)
681 {
682         /* if it has own rect, things are all OK */
683         if (iimg->rect)
684                 return;
685
686         if (iimg->datatoc_rect) {
687                 ImBuf *bbuf = IMB_ibImageFromMemory(iimg->datatoc_rect,
688                                                     iimg->datatoc_size, IB_rect, NULL, "<matcap icon>");
689                 /* w and h were set on initialize */
690                 if (bbuf->x != iimg->h && bbuf->y != iimg->w)
691                         IMB_scaleImBuf(bbuf, iimg->w, iimg->h);
692
693                 iimg->rect = bbuf->rect;
694                 bbuf->rect = NULL;
695                 IMB_freeImBuf(bbuf);
696         }
697 }
698
699 static void init_internal_icons(void)
700 {
701 //      bTheme *btheme = UI_GetTheme();
702         ImBuf *b16buf = NULL, *b32buf = NULL;
703         int x, y;
704
705 #if 0 // temp disabled
706         if ((btheme != NULL) && btheme->tui.iconfile[0]) {
707                 char *icondir = BKE_appdir_folder_id(BLENDER_DATAFILES, "icons");
708                 char iconfilestr[FILE_MAX];
709
710                 if (icondir) {
711                         BLI_join_dirfile(iconfilestr, sizeof(iconfilestr), icondir, btheme->tui.iconfile);
712                         bbuf = IMB_loadiffname(iconfilestr, IB_rect, NULL); /* if the image is missing bbuf will just be NULL */
713                         if (bbuf && (bbuf->x < ICON_IMAGE_W || bbuf->y < ICON_IMAGE_H)) {
714                                 printf("\n***WARNING***\nIcons file %s too small.\nUsing built-in Icons instead\n", iconfilestr);
715                                 IMB_freeImBuf(bbuf);
716                                 bbuf = NULL;
717                         }
718                 }
719                 else {
720                         printf("%s: 'icons' data path not found, continuing\n", __func__);
721                 }
722         }
723 #endif
724         if (b16buf == NULL)
725                 b16buf = IMB_ibImageFromMemory((unsigned char *)datatoc_blender_icons16_png,
726                                                datatoc_blender_icons16_png_size, IB_rect, NULL, "<blender icons>");
727         if (b16buf)
728                 IMB_premultiply_alpha(b16buf);
729
730         if (b32buf == NULL)
731                 b32buf = IMB_ibImageFromMemory((unsigned char *)datatoc_blender_icons32_png,
732                                                datatoc_blender_icons32_png_size, IB_rect, NULL, "<blender icons>");
733         if (b32buf)
734                 IMB_premultiply_alpha(b32buf);
735
736         if (b16buf && b32buf) {
737                 /* Free existing texture if any. */
738                 if (icongltex.id) {
739                         glDeleteTextures(1, &icongltex.id);
740                         icongltex.id = 0;
741                 }
742
743                 /* Allocate OpenGL texture. */
744                 glGenTextures(1, &icongltex.id);
745
746                 if (icongltex.id) {
747                         int level = 2;
748
749                         icongltex.w = b32buf->x;
750                         icongltex.h = b32buf->y;
751                         icongltex.invw = 1.0f / b32buf->x;
752                         icongltex.invh = 1.0f / b32buf->y;
753
754                         glBindTexture(GL_TEXTURE_2D, icongltex.id);
755
756                         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, b32buf->x, b32buf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, b32buf->rect);
757                         glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA8, b16buf->x, b16buf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, b16buf->rect);
758
759                         while (b16buf->x > 1) {
760                                 ImBuf *nbuf = IMB_onehalf(b16buf);
761                                 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA8, nbuf->x, nbuf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, nbuf->rect);
762                                 level++;
763                                 IMB_freeImBuf(b16buf);
764                                 b16buf = nbuf;
765                         }
766
767                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
768                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
769
770                         glBindTexture(GL_TEXTURE_2D, 0);
771                 }
772
773                 /* Define icons. */
774                 for (y = 0; y < ICON_GRID_ROWS; y++) {
775                         /* Row W has monochrome icons. */
776                         for (x = 0; x < ICON_GRID_COLS; x++) {
777                                 IconType icontype = icontypes[y * ICON_GRID_COLS + x];
778                                 if (!ELEM(icontype.type, ICON_TYPE_COLOR_TEXTURE, ICON_TYPE_MONO_TEXTURE)) {
779                                         continue;
780                                 }
781
782                                 def_internal_icon(b32buf, BIFICONID_FIRST + y * ICON_GRID_COLS + x,
783                                                   x * (ICON_GRID_W + ICON_GRID_MARGIN) + ICON_GRID_MARGIN,
784                                                   y * (ICON_GRID_H + ICON_GRID_MARGIN) + ICON_GRID_MARGIN, ICON_GRID_W,
785                                                   icontype.type, icontype.theme_color);
786                         }
787                 }
788         }
789
790         def_internal_vicon(ICON_SMALL_TRI_RIGHT_VEC, vicon_small_tri_right_draw);
791
792         def_internal_vicon(ICON_KEYTYPE_KEYFRAME_VEC, vicon_keytype_keyframe_draw);
793         def_internal_vicon(ICON_KEYTYPE_BREAKDOWN_VEC, vicon_keytype_breakdown_draw);
794         def_internal_vicon(ICON_KEYTYPE_EXTREME_VEC, vicon_keytype_extreme_draw);
795         def_internal_vicon(ICON_KEYTYPE_JITTER_VEC, vicon_keytype_jitter_draw);
796         def_internal_vicon(ICON_KEYTYPE_MOVING_HOLD_VEC, vicon_keytype_moving_hold_draw);
797
798         def_internal_vicon(ICON_HANDLETYPE_FREE_VEC, vicon_handletype_free_draw);
799         def_internal_vicon(ICON_HANDLETYPE_ALIGNED_VEC, vicon_handletype_aligned_draw);
800         def_internal_vicon(ICON_HANDLETYPE_VECTOR_VEC, vicon_handletype_vector_draw);
801         def_internal_vicon(ICON_HANDLETYPE_AUTO_VEC, vicon_handletype_auto_draw);
802         def_internal_vicon(ICON_HANDLETYPE_AUTO_CLAMP_VEC, vicon_handletype_auto_clamp_draw);
803
804         def_internal_vicon(ICON_COLORSET_01_VEC, vicon_colorset_draw_01);
805         def_internal_vicon(ICON_COLORSET_02_VEC, vicon_colorset_draw_02);
806         def_internal_vicon(ICON_COLORSET_03_VEC, vicon_colorset_draw_03);
807         def_internal_vicon(ICON_COLORSET_04_VEC, vicon_colorset_draw_04);
808         def_internal_vicon(ICON_COLORSET_05_VEC, vicon_colorset_draw_05);
809         def_internal_vicon(ICON_COLORSET_06_VEC, vicon_colorset_draw_06);
810         def_internal_vicon(ICON_COLORSET_07_VEC, vicon_colorset_draw_07);
811         def_internal_vicon(ICON_COLORSET_08_VEC, vicon_colorset_draw_08);
812         def_internal_vicon(ICON_COLORSET_09_VEC, vicon_colorset_draw_09);
813         def_internal_vicon(ICON_COLORSET_10_VEC, vicon_colorset_draw_10);
814         def_internal_vicon(ICON_COLORSET_11_VEC, vicon_colorset_draw_11);
815         def_internal_vicon(ICON_COLORSET_12_VEC, vicon_colorset_draw_12);
816         def_internal_vicon(ICON_COLORSET_13_VEC, vicon_colorset_draw_13);
817         def_internal_vicon(ICON_COLORSET_14_VEC, vicon_colorset_draw_14);
818         def_internal_vicon(ICON_COLORSET_15_VEC, vicon_colorset_draw_15);
819         def_internal_vicon(ICON_COLORSET_16_VEC, vicon_colorset_draw_16);
820         def_internal_vicon(ICON_COLORSET_17_VEC, vicon_colorset_draw_17);
821         def_internal_vicon(ICON_COLORSET_18_VEC, vicon_colorset_draw_18);
822         def_internal_vicon(ICON_COLORSET_19_VEC, vicon_colorset_draw_19);
823         def_internal_vicon(ICON_COLORSET_20_VEC, vicon_colorset_draw_20);
824
825         IMB_freeImBuf(b16buf);
826         IMB_freeImBuf(b32buf);
827
828 }
829 #endif  /* WITH_HEADLESS */
830
831 static void init_iconfile_list(struct ListBase *list)
832 {
833         IconFile *ifile;
834         struct direntry *dir;
835         int totfile, i, index = 1;
836         const char *icondir;
837
838         BLI_listbase_clear(list);
839         icondir = BKE_appdir_folder_id(BLENDER_DATAFILES, "icons");
840
841         if (icondir == NULL)
842                 return;
843
844         totfile = BLI_filelist_dir_contents(icondir, &dir);
845
846         for (i = 0; i < totfile; i++) {
847                 if ((dir[i].type & S_IFREG)) {
848                         const char *filename = dir[i].relname;
849
850                         if (BLI_path_extension_check(filename, ".png")) {
851                                 /* loading all icons on file start is overkill & slows startup
852                                  * its possible they change size after blender load anyway. */
853 #if 0
854                                 int ifilex, ifiley;
855                                 char iconfilestr[FILE_MAX + 16]; /* allow 256 chars for file+dir */
856                                 ImBuf *bbuf = NULL;
857                                 /* check to see if the image is the right size, continue if not */
858                                 /* copying strings here should go ok, assuming that we never get back
859                                  * a complete path to file longer than 256 chars */
860                                 BLI_join_dirfile(iconfilestr, sizeof(iconfilestr), icondir, filename);
861                                 bbuf = IMB_loadiffname(iconfilestr, IB_rect);
862
863                                 if (bbuf) {
864                                         ifilex = bbuf->x;
865                                         ifiley = bbuf->y;
866                                         IMB_freeImBuf(bbuf);
867                                 }
868                                 else {
869                                         ifilex = ifiley = 0;
870                                 }
871
872                                 /* bad size or failed to load */
873                                 if ((ifilex != ICON_IMAGE_W) || (ifiley != ICON_IMAGE_H)) {
874                                         printf("icon '%s' is wrong size %dx%d\n", iconfilestr, ifilex, ifiley);
875                                         continue;
876                                 }
877 #endif          /* removed */
878
879                                 /* found a potential icon file, so make an entry for it in the cache list */
880                                 ifile = MEM_callocN(sizeof(IconFile), "IconFile");
881
882                                 BLI_strncpy(ifile->filename, filename, sizeof(ifile->filename));
883                                 ifile->index = index;
884
885                                 BLI_addtail(list, ifile);
886
887                                 index++;
888                         }
889                 }
890         }
891
892         BLI_filelist_free(dir, totfile);
893         dir = NULL;
894 }
895
896 static void free_iconfile_list(struct ListBase *list)
897 {
898         IconFile *ifile = NULL, *next_ifile = NULL;
899
900         for (ifile = list->first; ifile; ifile = next_ifile) {
901                 next_ifile = ifile->next;
902                 BLI_freelinkN(list, ifile);
903         }
904 }
905
906 #endif  /* WITH_HEADLESS */
907
908 int UI_iconfile_get_index(const char *filename)
909 {
910         IconFile *ifile;
911         ListBase *list = &(iconfilelist);
912
913         for (ifile = list->first; ifile; ifile = ifile->next) {
914                 if (BLI_path_cmp(filename, ifile->filename) == 0) {
915                         return ifile->index;
916                 }
917         }
918
919         return 0;
920 }
921
922 ListBase *UI_iconfile_list(void)
923 {
924         ListBase *list = &(iconfilelist);
925
926         return list;
927 }
928
929
930 void UI_icons_free(void)
931 {
932 #ifndef WITH_HEADLESS
933         if (icongltex.id) {
934                 glDeleteTextures(1, &icongltex.id);
935                 icongltex.id = 0;
936         }
937
938         free_iconfile_list(&iconfilelist);
939         BKE_icons_free();
940 #endif
941 }
942
943 void UI_icons_free_drawinfo(void *drawinfo)
944 {
945         DrawInfo *di = drawinfo;
946
947         if (di) {
948                 if (di->type == ICON_TYPE_BUFFER) {
949                         if (di->data.buffer.image) {
950                                 if (di->data.buffer.image->rect)
951                                         MEM_freeN(di->data.buffer.image->rect);
952                                 MEM_freeN(di->data.buffer.image);
953                         }
954                 }
955                 else if (di->type == ICON_TYPE_GEOM) {
956                         if (di->data.geom.image_cache) {
957                                 IMB_freeImBuf(di->data.geom.image_cache);
958                         }
959                 }
960
961                 MEM_freeN(di);
962         }
963 }
964
965 /**
966  * #Icon.data_type and #Icon.obj
967  */
968 static DrawInfo *icon_create_drawinfo(Icon *icon)
969 {
970         int  icon_data_type = icon->obj_type;
971         DrawInfo *di = NULL;
972
973         di = MEM_callocN(sizeof(DrawInfo), "di_icon");
974
975         if (ELEM(icon_data_type, ICON_DATA_ID, ICON_DATA_PREVIEW)) {
976                 di->type = ICON_TYPE_PREVIEW;
977         }
978         else if (icon_data_type == ICON_DATA_GEOM) {
979                 di->type = ICON_TYPE_GEOM;
980         }
981         else if (icon_data_type == ICON_DATA_STUDIOLIGHT) {
982                 di->type = ICON_TYPE_BUFFER;
983         }
984         else if (icon_data_type == ICON_DATA_GPLAYER) {
985                 di->type = ICON_TYPE_GPLAYER;
986         }
987         else {
988                 BLI_assert(0);
989         }
990
991         return di;
992 }
993
994 static DrawInfo *icon_ensure_drawinfo(Icon *icon)
995 {
996         if (icon->drawinfo) {
997                 return icon->drawinfo;
998         }
999         DrawInfo *di = icon_create_drawinfo(icon);
1000         icon->drawinfo = di;
1001         icon->drawinfo_free = UI_icons_free_drawinfo;
1002         return di;
1003 }
1004
1005 /* note!, returns unscaled by DPI */
1006 int UI_icon_get_width(int icon_id)
1007 {
1008         Icon *icon = NULL;
1009         DrawInfo *di = NULL;
1010
1011         icon = BKE_icon_get(icon_id);
1012
1013         if (icon == NULL) {
1014                 if (G.debug & G_DEBUG)
1015                         printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
1016                 return 0;
1017         }
1018
1019         di = icon_ensure_drawinfo(icon);
1020         if (di) {
1021                 return ICON_DEFAULT_WIDTH;
1022         }
1023
1024         return 0;
1025 }
1026
1027 int UI_icon_get_height(int icon_id)
1028 {
1029         Icon *icon = BKE_icon_get(icon_id);
1030         if (icon == NULL) {
1031                 if (G.debug & G_DEBUG)
1032                         printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
1033                 return 0;
1034         }
1035
1036         DrawInfo *di = icon_ensure_drawinfo(icon);
1037         if (di) {
1038                 return ICON_DEFAULT_HEIGHT;
1039         }
1040
1041         return 0;
1042 }
1043
1044 void UI_icons_init()
1045 {
1046 #ifndef WITH_HEADLESS
1047         init_iconfile_list(&iconfilelist);
1048         init_internal_icons();
1049         init_brush_icons();
1050         init_event_icons();
1051 #endif
1052 }
1053
1054 /* Render size for preview images and icons
1055  */
1056 int UI_preview_render_size(enum eIconSizes size)
1057 {
1058         switch (size) {
1059                 case ICON_SIZE_ICON:
1060                         return ICON_RENDER_DEFAULT_HEIGHT;
1061                 case ICON_SIZE_PREVIEW:
1062                         return PREVIEW_RENDER_DEFAULT_HEIGHT;
1063                 default:
1064                         return 0;
1065         }
1066 }
1067
1068 /* Create rect for the icon
1069  */
1070 static void icon_create_rect(struct PreviewImage *prv_img, enum eIconSizes size)
1071 {
1072         unsigned int render_size = UI_preview_render_size(size);
1073
1074         if (!prv_img) {
1075                 if (G.debug & G_DEBUG)
1076                         printf("%s, error: requested preview image does not exist", __func__);
1077         }
1078         else if (!prv_img->rect[size]) {
1079                 prv_img->w[size] = render_size;
1080                 prv_img->h[size] = render_size;
1081                 prv_img->flag[size] |= PRV_CHANGED;
1082                 prv_img->changed_timestamp[size] = 0;
1083                 prv_img->rect[size] = MEM_callocN(render_size * render_size * sizeof(unsigned int), "prv_rect");
1084         }
1085 }
1086
1087 static void ui_id_preview_image_render_size(
1088         const bContext *C, Scene *scene, ID *id, PreviewImage *pi, int size, const bool use_job);
1089
1090 static void ui_studiolight_icon_job_exec(void *customdata, short *UNUSED(stop), short *UNUSED(do_update), float *UNUSED(progress))
1091 {
1092         Icon **tmp = (Icon **)customdata;
1093         Icon *icon = *tmp;
1094         DrawInfo *di = icon_ensure_drawinfo(icon);
1095         StudioLight *sl = icon->obj;
1096         BKE_studiolight_preview(di->data.buffer.image->rect, sl, icon->id_type);
1097 }
1098
1099 static void ui_studiolight_kill_icon_preview_job(wmWindowManager *wm, int icon_id)
1100 {
1101         Icon *icon = BKE_icon_get(icon_id);
1102         WM_jobs_kill_type(wm, icon, WM_JOB_TYPE_STUDIOLIGHT);
1103         icon->obj = NULL;
1104 }
1105
1106 static void ui_studiolight_free_function(StudioLight *sl, void *data)
1107 {
1108         wmWindowManager *wm = data;
1109
1110         /* Happens if job was canceled or already finished. */
1111         if (wm == NULL)
1112                 return;
1113
1114         // get icons_id, get icons and kill wm jobs
1115         if (sl->icon_id_radiance) {
1116                 ui_studiolight_kill_icon_preview_job(wm, sl->icon_id_radiance);
1117         }
1118         if (sl->icon_id_irradiance) {
1119                 ui_studiolight_kill_icon_preview_job(wm, sl->icon_id_irradiance);
1120         }
1121         if (sl->icon_id_matcap) {
1122                 ui_studiolight_kill_icon_preview_job(wm, sl->icon_id_matcap);
1123         }
1124         if (sl->icon_id_matcap_flipped) {
1125                 ui_studiolight_kill_icon_preview_job(wm, sl->icon_id_matcap_flipped);
1126         }
1127 }
1128
1129 static void ui_studiolight_icon_job_end(void *customdata)
1130 {
1131         Icon **tmp = (Icon **)customdata;
1132         Icon *icon = *tmp;
1133         StudioLight *sl = icon->obj;
1134         BKE_studiolight_set_free_function(sl, &ui_studiolight_free_function, NULL);
1135 }
1136
1137 void ui_icon_ensure_deferred(const bContext *C, const int icon_id, const bool big)
1138 {
1139         Icon *icon = BKE_icon_get(icon_id);
1140
1141         if (icon) {
1142                 DrawInfo *di = icon_ensure_drawinfo(icon);
1143
1144                 if (di) {
1145                         switch (di->type) {
1146                                 case ICON_TYPE_PREVIEW:
1147                                 {
1148                                         ID *id = (icon->id_type != 0) ? icon->obj : NULL;
1149                                         PreviewImage *prv = id ? BKE_previewimg_id_ensure(id) : icon->obj;
1150                                         /* Using jobs for screen previews crashes due to offscreen rendering.
1151                                          * XXX would be nicer if PreviewImage could store if it supports jobs */
1152                                         const bool use_jobs = !id || (GS(id->name) != ID_SCR);
1153
1154                                         if (prv) {
1155                                                 const int size = big ? ICON_SIZE_PREVIEW : ICON_SIZE_ICON;
1156
1157                                                 if (id || (prv->tag & PRV_TAG_DEFFERED) != 0) {
1158                                                         ui_id_preview_image_render_size(C, NULL, id, prv, size, use_jobs);
1159                                                 }
1160                                         }
1161                                         break;
1162                                 }
1163                                 case ICON_TYPE_BUFFER:
1164                                 {
1165                                         if (icon->obj_type == ICON_DATA_STUDIOLIGHT) {
1166                                                 if (di->data.buffer.image == NULL) {
1167                                                         wmWindowManager *wm = CTX_wm_manager(C);
1168                                                         StudioLight *sl = icon->obj;
1169                                                         BKE_studiolight_set_free_function(sl, &ui_studiolight_free_function, wm);
1170                                                         IconImage *img = MEM_mallocN(sizeof(IconImage), __func__);
1171
1172                                                         img->w = STUDIOLIGHT_ICON_SIZE;
1173                                                         img->h = STUDIOLIGHT_ICON_SIZE;
1174                                                         size_t size = STUDIOLIGHT_ICON_SIZE * STUDIOLIGHT_ICON_SIZE * sizeof(uint);
1175                                                         img->rect = MEM_mallocN(size, __func__);
1176                                                         memset(img->rect, 0, size);
1177                                                         di->data.buffer.image = img;
1178
1179                                                         wmJob *wm_job = WM_jobs_get(wm, CTX_wm_window(C), icon, "StudioLight Icon", 0, WM_JOB_TYPE_STUDIOLIGHT);
1180                                                         Icon **tmp = MEM_callocN(sizeof(Icon *), __func__);
1181                                                         *tmp = icon;
1182                                                         WM_jobs_customdata_set(wm_job, tmp, MEM_freeN);
1183                                                         WM_jobs_timer(wm_job, 0.01, 0, NC_WINDOW);
1184                                                         WM_jobs_callbacks(wm_job, ui_studiolight_icon_job_exec, NULL, NULL, ui_studiolight_icon_job_end);
1185                                                         WM_jobs_start(CTX_wm_manager(C), wm_job);
1186                                                 }
1187                                         }
1188                                         break;
1189                                 }
1190                         }
1191                 }
1192         }
1193 }
1194
1195 /* only called when icon has changed */
1196 /* only call with valid pointer from UI_icon_draw */
1197 static void icon_set_image(
1198         const bContext *C, Scene *scene, ID *id, PreviewImage *prv_img, enum eIconSizes size, const bool use_job)
1199 {
1200         if (!prv_img) {
1201                 if (G.debug & G_DEBUG)
1202                         printf("%s: no preview image for this ID: %s\n", __func__, id->name);
1203                 return;
1204         }
1205
1206         if (prv_img->flag[size] & PRV_USER_EDITED) {
1207                 /* user-edited preview, do not auto-update! */
1208                 return;
1209         }
1210
1211         icon_create_rect(prv_img, size);
1212
1213         if (use_job) {
1214                 /* Job (background) version */
1215                 ED_preview_icon_job(C, prv_img, id, prv_img->rect[size], prv_img->w[size], prv_img->h[size]);
1216         }
1217         else {
1218                 if (!scene) {
1219                         scene = CTX_data_scene(C);
1220                 }
1221                 /* Immediate version */
1222                 ED_preview_icon_render(CTX_data_main(C), scene, id, prv_img->rect[size], prv_img->w[size], prv_img->h[size]);
1223         }
1224 }
1225
1226 PreviewImage *UI_icon_to_preview(int icon_id)
1227 {
1228         Icon *icon = BKE_icon_get(icon_id);
1229
1230         if (icon) {
1231                 DrawInfo *di = (DrawInfo *)icon->drawinfo;
1232                 if (di) {
1233                         if (di->type == ICON_TYPE_PREVIEW) {
1234                                 PreviewImage *prv = (icon->id_type != 0) ? BKE_previewimg_id_ensure((ID *)icon->obj) : icon->obj;
1235
1236                                 if (prv) {
1237                                         return BKE_previewimg_copy(prv);
1238                                 }
1239                         }
1240                         else if (di->data.buffer.image) {
1241                                 ImBuf *bbuf;
1242
1243                                 bbuf = IMB_ibImageFromMemory(di->data.buffer.image->datatoc_rect, di->data.buffer.image->datatoc_size,
1244                                                              IB_rect, NULL, __func__);
1245                                 if (bbuf) {
1246                                         PreviewImage *prv = BKE_previewimg_create();
1247
1248                                         prv->rect[0] = bbuf->rect;
1249
1250                                         prv->w[0] = bbuf->x;
1251                                         prv->h[0] = bbuf->y;
1252
1253                                         bbuf->rect = NULL;
1254                                         IMB_freeImBuf(bbuf);
1255
1256                                         return prv;
1257                                 }
1258                         }
1259                 }
1260         }
1261         return NULL;
1262 }
1263
1264 static void icon_draw_rect(float x, float y, int w, int h, float UNUSED(aspect), int rw, int rh,
1265                            unsigned int *rect, float alpha, const float rgb[3], const float desaturate)
1266 {
1267         ImBuf *ima = NULL;
1268         int draw_w = w;
1269         int draw_h = h;
1270         int draw_x = x;
1271         int draw_y = y;
1272
1273         /* sanity check */
1274         if (w <= 0 || h <= 0 || w > 2000 || h > 2000) {
1275                 printf("%s: icons are %i x %i pixels?\n", __func__, w, h);
1276                 BLI_assert(!"invalid icon size");
1277                 return;
1278         }
1279         /* modulate color */
1280         float col[4] = {1.0f, 1.0f, 1.0f, alpha};
1281
1282         if (rgb) {
1283                 col[0] = rgb[0];
1284                 col[1] = rgb[1];
1285                 col[2] = rgb[2];
1286         }
1287
1288         /* rect contains image in 'rendersize', we only scale if needed */
1289         if (rw != w || rh != h) {
1290                 /* preserve aspect ratio and center */
1291                 if (rw > rh) {
1292                         draw_w = w;
1293                         draw_h = (int)(((float)rh / (float)rw) * (float)w);
1294                         draw_y += (h - draw_h) / 2;
1295                 }
1296                 else if (rw < rh) {
1297                         draw_w = (int)(((float)rw / (float)rh) * (float)h);
1298                         draw_h = h;
1299                         draw_x += (w - draw_w) / 2;
1300                 }
1301                 /* if the image is squared, the draw_ initialization values are good */
1302
1303                 /* first allocate imbuf for scaling and copy preview into it */
1304                 ima = IMB_allocImBuf(rw, rh, 32, IB_rect);
1305                 memcpy(ima->rect, rect, rw * rh * sizeof(unsigned int));
1306                 IMB_scaleImBuf(ima, draw_w, draw_h); /* scale it */
1307                 rect = ima->rect;
1308         }
1309
1310         /* We need to flush widget base first to ensure correct ordering. */
1311         UI_widgetbase_draw_cache_flush();
1312
1313         /* draw */
1314         GPUBuiltinShader shader;
1315         if (desaturate != 0.0f) {
1316                 shader = GPU_SHADER_2D_IMAGE_DESATURATE_COLOR;
1317         }
1318         else {
1319                 shader = GPU_SHADER_2D_IMAGE_COLOR;
1320         }
1321         IMMDrawPixelsTexState state = immDrawPixelsTexSetup(shader);
1322
1323         if (shader == GPU_SHADER_2D_IMAGE_DESATURATE_COLOR) {
1324                 immUniform1f("factor", desaturate);
1325         }
1326
1327         immDrawPixelsTex(&state, draw_x, draw_y, draw_w, draw_h, GL_RGBA, GL_UNSIGNED_BYTE, GL_NEAREST, rect,
1328                          1.0f, 1.0f, col);
1329
1330         if (ima)
1331                 IMB_freeImBuf(ima);
1332 }
1333
1334 /* High enough to make a difference, low enough so that
1335  * small draws are still efficient with the use of glUniform.
1336  * NOTE TODO: We could use UBO but we would need some triple
1337  * buffer system + persistent mapping for this to be more
1338  * efficient than simple glUniform calls. */
1339 #define ICON_DRAW_CACHE_SIZE 16
1340
1341 typedef struct IconDrawCall {
1342         rctf pos;
1343         rctf tex;
1344         float color[4];
1345 } IconDrawCall;
1346
1347 static struct {
1348         IconDrawCall drawcall_cache[ICON_DRAW_CACHE_SIZE];
1349         int calls; /* Number of calls batched together */
1350         bool enabled;
1351         float mat[4][4];
1352 } g_icon_draw_cache = {{{{0}}}};
1353
1354 void UI_icon_draw_cache_begin(void)
1355 {
1356         BLI_assert(g_icon_draw_cache.enabled == false);
1357         g_icon_draw_cache.enabled = true;
1358 }
1359
1360 static void icon_draw_cache_flush_ex(void)
1361 {
1362         if (g_icon_draw_cache.calls == 0)
1363                 return;
1364
1365         /* We need to flush widget base first to ensure correct ordering. */
1366         UI_widgetbase_draw_cache_flush();
1367
1368         GPU_blend_set_func(GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
1369
1370         glActiveTexture(GL_TEXTURE0);
1371         glBindTexture(GL_TEXTURE_2D, icongltex.id);
1372
1373         GPUShader *shader = GPU_shader_get_builtin_shader(GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR);
1374         GPU_shader_bind(shader);
1375
1376         int img_loc = GPU_shader_get_uniform(shader, "image");
1377         int data_loc = GPU_shader_get_uniform(shader, "calls_data[0]");
1378
1379         glUniform1i(img_loc, 0);
1380         glUniform4fv(data_loc, ICON_DRAW_CACHE_SIZE * 3, (float *)g_icon_draw_cache.drawcall_cache);
1381
1382         GPU_draw_primitive(GPU_PRIM_TRIS, 6 * g_icon_draw_cache.calls);
1383
1384         glBindTexture(GL_TEXTURE_2D, 0);
1385
1386         g_icon_draw_cache.calls = 0;
1387
1388         GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
1389 }
1390
1391 void UI_icon_draw_cache_end(void)
1392 {
1393         BLI_assert(g_icon_draw_cache.enabled == true);
1394         g_icon_draw_cache.enabled = false;
1395
1396         /* Don't change blend state if it's not needed. */
1397         if (g_icon_draw_cache.calls == 0)
1398                 return;
1399
1400         GPU_blend(true);
1401         icon_draw_cache_flush_ex();
1402         GPU_blend(false);
1403 }
1404
1405 static void icon_draw_texture_cached(
1406         float x, float y, float w, float h, int ix, int iy,
1407         int UNUSED(iw), int ih, float alpha, const float rgb[3])
1408 {
1409
1410         float mvp[4][4];
1411         GPU_matrix_model_view_projection_get(mvp);
1412
1413         IconDrawCall *call = &g_icon_draw_cache.drawcall_cache[g_icon_draw_cache.calls];
1414         g_icon_draw_cache.calls++;
1415
1416         /* Manual mat4*vec2 */
1417         call->pos.xmin = x * mvp[0][0] + y * mvp[1][0] + mvp[3][0];
1418         call->pos.ymin = x * mvp[0][1] + y * mvp[1][1] + mvp[3][1];
1419         call->pos.xmax = call->pos.xmin + w * mvp[0][0] + h * mvp[1][0];
1420         call->pos.ymax = call->pos.ymin + w * mvp[0][1] + h * mvp[1][1];
1421
1422         call->tex.xmin = ix * icongltex.invw;
1423         call->tex.xmax = (ix + ih) * icongltex.invw;
1424         call->tex.ymin = iy * icongltex.invh;
1425         call->tex.ymax = (iy + ih) * icongltex.invh;
1426
1427         if (rgb) copy_v4_fl4(call->color, rgb[0], rgb[1], rgb[2], alpha);
1428         else     copy_v4_fl(call->color, alpha);
1429
1430         if (g_icon_draw_cache.calls == ICON_DRAW_CACHE_SIZE) {
1431                 icon_draw_cache_flush_ex();
1432         }
1433 }
1434
1435 static void icon_draw_texture(
1436         float x, float y, float w, float h, int ix, int iy,
1437         int iw, int ih, float alpha, const float rgb[3])
1438 {
1439         if (g_icon_draw_cache.enabled) {
1440                 icon_draw_texture_cached(x, y, w, h, ix, iy, iw, ih, alpha, rgb);
1441                 return;
1442         }
1443
1444         /* We need to flush widget base first to ensure correct ordering. */
1445         UI_widgetbase_draw_cache_flush();
1446
1447         GPU_blend_set_func(GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
1448
1449         float x1, x2, y1, y2;
1450
1451         x1 = ix * icongltex.invw;
1452         x2 = (ix + ih) * icongltex.invw;
1453         y1 = iy * icongltex.invh;
1454         y2 = (iy + ih) * icongltex.invh;
1455
1456         glActiveTexture(GL_TEXTURE0);
1457         glBindTexture(GL_TEXTURE_2D, icongltex.id);
1458
1459         GPUShader *shader = GPU_shader_get_builtin_shader(GPU_SHADER_2D_IMAGE_RECT_COLOR);
1460         GPU_shader_bind(shader);
1461
1462         if (rgb) glUniform4f(GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_COLOR), rgb[0], rgb[1], rgb[2], alpha);
1463         else     glUniform4f(GPU_shader_get_builtin_uniform(shader, GPU_UNIFORM_COLOR), alpha, alpha, alpha, alpha);
1464
1465         glUniform1i(GPU_shader_get_uniform(shader, "image"), 0);
1466         glUniform4f(GPU_shader_get_uniform(shader, "rect_icon"), x1, y1, x2, y2);
1467         glUniform4f(GPU_shader_get_uniform(shader, "rect_geom"), x, y, x + w, y + h);
1468
1469         GPU_draw_primitive(GPU_PRIM_TRI_STRIP, 4);
1470
1471         glBindTexture(GL_TEXTURE_2D, 0);
1472
1473         GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
1474 }
1475
1476 /* Drawing size for preview images */
1477 static int get_draw_size(enum eIconSizes size)
1478 {
1479         switch (size) {
1480                 case ICON_SIZE_ICON:
1481                         return ICON_DEFAULT_HEIGHT;
1482                 case ICON_SIZE_PREVIEW:
1483                         return PREVIEW_DEFAULT_HEIGHT;
1484                 default:
1485                         return 0;
1486         }
1487 }
1488
1489
1490
1491 static void icon_draw_size(
1492         float x, float y, int icon_id, float aspect, float alpha, const float rgb[3],
1493         enum eIconSizes size, int draw_size, const float desaturate, const char mono_rgba[4])
1494 {
1495         bTheme *btheme = UI_GetTheme();
1496         Icon *icon = NULL;
1497         IconImage *iimg;
1498         const float fdraw_size = (float)draw_size;
1499         int w, h;
1500
1501         icon = BKE_icon_get(icon_id);
1502         alpha *= btheme->tui.icon_alpha;
1503
1504         if (icon == NULL) {
1505                 if (G.debug & G_DEBUG)
1506                         printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
1507                 return;
1508         }
1509
1510         /* scale width and height according to aspect */
1511         w = (int)(fdraw_size / aspect + 0.5f);
1512         h = (int)(fdraw_size / aspect + 0.5f);
1513
1514         DrawInfo *di = icon_ensure_drawinfo(icon);
1515
1516         if (di->type == ICON_TYPE_VECTOR) {
1517                 /* We need to flush widget base first to ensure correct ordering. */
1518                 UI_widgetbase_draw_cache_flush();
1519                 /* vector icons use the uiBlock transformation, they are not drawn
1520                  * with untransformed coordinates like the other icons */
1521                 di->data.vector.func((int)x, (int)y, w, h, 1.0f);
1522         }
1523         else if (di->type == ICON_TYPE_GEOM) {
1524                 /* We need to flush widget base first to ensure correct ordering. */
1525                 UI_widgetbase_draw_cache_flush();
1526
1527 #ifdef USE_UI_TOOLBAR_HACK
1528                 /* TODO(campbell): scale icons up for toolbar, we need a way to detect larger buttons and do this automatic. */
1529                 {
1530                         float scale = (float)ICON_DEFAULT_HEIGHT_TOOLBAR / (float)ICON_DEFAULT_HEIGHT;
1531                         y = (y + (h / 2)) - ((h * scale) / 2);
1532                         w *= scale;
1533                         h *= scale;
1534                 }
1535 #endif
1536
1537                 /* This could re-generate often if rendered at different sizes in the one interface.
1538                  * TODO(campbell): support caching multiple sizes. */
1539                 ImBuf *ibuf = di->data.geom.image_cache;
1540                 if ((ibuf == NULL) ||
1541                     (ibuf->x != w) ||
1542                     (ibuf->y != h))
1543                 {
1544                         if (ibuf) {
1545                                 IMB_freeImBuf(ibuf);
1546                         }
1547                         ibuf = BKE_icon_geom_rasterize(icon->obj, w, h);
1548                         di->data.geom.image_cache = ibuf;
1549                 }
1550                 glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1551                 icon_draw_rect(x, y, w, h, aspect, w, h, ibuf->rect, alpha, rgb, desaturate);
1552                 GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
1553         }
1554         else if (di->type == ICON_TYPE_EVENT) {
1555                 const short event_type = di->data.input.event_type;
1556                 const short event_value = di->data.input.event_value;
1557                 icon_draw_rect_input(x, y, w, h, alpha, event_type, event_value);
1558         }
1559         else if (di->type == ICON_TYPE_COLOR_TEXTURE) {
1560                 /* texture image use premul alpha for correct scaling */
1561                 icon_draw_texture(x, y, (float)w, (float)h, di->data.texture.x, di->data.texture.y,
1562                                   di->data.texture.w, di->data.texture.h, alpha, rgb);
1563         }
1564         else if (di->type == ICON_TYPE_MONO_TEXTURE) {
1565                 /* icon that matches text color, assumed to be white */
1566                 float color[4];
1567                 if (!UI_GetIconThemeColor4fv(di->data.texture.theme_color, color)) {
1568                         if (mono_rgba) {
1569                                 rgba_uchar_to_float(color, (const uchar *)mono_rgba);
1570                         }
1571                         else {
1572                                 UI_GetThemeColor4fv(TH_TEXT, color);
1573                         }
1574                 }
1575
1576                 if (rgb) {
1577                         mul_v3_v3(color, rgb);
1578                 }
1579
1580                 mul_v4_fl(color, alpha);
1581
1582                 icon_draw_texture(x, y, (float)w, (float)h, di->data.texture.x, di->data.texture.y,
1583                                   di->data.texture.w, di->data.texture.h, color[3], color);
1584         }
1585
1586         else if (di->type == ICON_TYPE_BUFFER) {
1587                 /* it is a builtin icon */
1588                 iimg = di->data.buffer.image;
1589 #ifndef WITH_HEADLESS
1590                 icon_verify_datatoc(iimg);
1591 #endif
1592                 if (!iimg->rect) return;  /* something has gone wrong! */
1593
1594                 GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
1595                 icon_draw_rect(x, y, w, h, aspect, iimg->w, iimg->h, iimg->rect, alpha, rgb, desaturate);
1596                 GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
1597         }
1598         else if (di->type == ICON_TYPE_PREVIEW) {
1599                 PreviewImage *pi = (icon->id_type != 0) ? BKE_previewimg_id_ensure((ID *)icon->obj) : icon->obj;
1600
1601                 if (pi) {
1602                         /* no create icon on this level in code */
1603                         if (!pi->rect[size]) return;  /* something has gone wrong! */
1604
1605                         /* Preview images use premultiplied alpha. */
1606                         GPU_blend_set_func_separate(GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
1607                         icon_draw_rect(x, y, w, h, aspect, pi->w[size], pi->h[size], pi->rect[size], alpha, rgb, desaturate);
1608                         GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
1609                 }
1610         }
1611         else if (di->type == ICON_TYPE_GPLAYER) {
1612                 BLI_assert(icon->obj != NULL);
1613
1614                 /* We need to flush widget base first to ensure correct ordering. */
1615                 UI_widgetbase_draw_cache_flush();
1616
1617                 /* Just draw a colored rect - Like for vicon_colorset_draw() */
1618 #ifndef WITH_HEADLESS
1619                 vicon_gplayer_color_draw(icon, (int)x, (int)y,  w, h);
1620 #endif
1621         }
1622 }
1623
1624 static void ui_id_preview_image_render_size(
1625         const bContext *C, Scene *scene, ID *id, PreviewImage *pi, int size, const bool use_job)
1626 {
1627         if (((pi->flag[size] & PRV_CHANGED) || !pi->rect[size])) { /* changed only ever set by dynamic icons */
1628                 /* create the rect if necessary */
1629                 icon_set_image(C, scene, id, pi, size, use_job);
1630
1631                 pi->flag[size] &= ~PRV_CHANGED;
1632         }
1633 }
1634
1635 void UI_id_icon_render(const bContext *C, Scene *scene, ID *id, const bool big, const bool use_job)
1636 {
1637         PreviewImage *pi = BKE_previewimg_id_ensure(id);
1638
1639         if (pi) {
1640                 if (big)
1641                         ui_id_preview_image_render_size(C, scene, id, pi, ICON_SIZE_PREVIEW, use_job);  /* bigger preview size */
1642                 else
1643                         ui_id_preview_image_render_size(C, scene, id, pi, ICON_SIZE_ICON, use_job);     /* icon size */
1644         }
1645 }
1646
1647 static void ui_id_icon_render(const bContext *C, ID *id, bool use_jobs)
1648 {
1649         PreviewImage *pi = BKE_previewimg_id_ensure(id);
1650         enum eIconSizes i;
1651
1652         if (!pi)
1653                 return;
1654
1655         for (i = 0; i < NUM_ICON_SIZES; i++) {
1656                 /* check if rect needs to be created; changed
1657                  * only set by dynamic icons */
1658                 if (((pi->flag[i] & PRV_CHANGED) || !pi->rect[i])) {
1659                         icon_set_image(C, NULL, id, pi, i, use_jobs);
1660                         pi->flag[i] &= ~PRV_CHANGED;
1661                 }
1662         }
1663 }
1664
1665
1666 static int ui_id_brush_get_icon(const bContext *C, ID *id)
1667 {
1668         Brush *br = (Brush *)id;
1669
1670         if (br->flag & BRUSH_CUSTOM_ICON) {
1671                 BKE_icon_id_ensure(id);
1672                 ui_id_icon_render(C, id, true);
1673         }
1674         else {
1675                 WorkSpace *workspace = CTX_wm_workspace(C);
1676                 Object *ob = CTX_data_active_object(C);
1677                 const EnumPropertyItem *items = NULL;
1678                 ePaintMode paint_mode = PAINT_MODE_INVALID;
1679                 ScrArea *sa = CTX_wm_area(C);
1680                 char space_type = sa->spacetype;
1681                 /* When in an unsupported space. */
1682                 if (!ELEM(space_type, SPACE_VIEW3D, SPACE_IMAGE)) {
1683                         space_type = workspace->tools_space_type;
1684                 }
1685
1686                 /* XXX: this is not nice, should probably make brushes
1687                  * be strictly in one paint mode only to avoid
1688                  * checking various context stuff here */
1689
1690                 if ((space_type == SPACE_VIEW3D) && ob) {
1691                         if (ob->mode & OB_MODE_SCULPT) {
1692                                 paint_mode = PAINT_MODE_SCULPT;
1693                         }
1694                         else if (ob->mode & OB_MODE_VERTEX_PAINT) {
1695                                 paint_mode = PAINT_MODE_VERTEX;
1696                         }
1697                         else if (ob->mode & OB_MODE_WEIGHT_PAINT) {
1698                                 paint_mode = PAINT_MODE_WEIGHT;
1699                         }
1700                         else if (ob->mode & OB_MODE_TEXTURE_PAINT) {
1701                                 paint_mode = PAINT_MODE_TEXTURE_3D;
1702                         }
1703                 }
1704                 else if (space_type == SPACE_IMAGE) {
1705                         int sima_mode;
1706                         if (sa->spacetype == space_type) {
1707                                 SpaceImage *sima = sa->spacedata.first;
1708                                 sima_mode = sima->mode;
1709                         }
1710                         else {
1711                                 sima_mode = workspace->tools_mode;
1712                         }
1713
1714                         if (sima_mode == SI_MODE_PAINT) {
1715                                 paint_mode = PAINT_MODE_TEXTURE_2D;
1716                         }
1717                 }
1718
1719                 /* reset the icon */
1720                 if ((ob != NULL) &&
1721                     (ob->mode & OB_MODE_PAINT_GPENCIL) &&
1722                     (br->gpencil_settings != NULL))
1723                 {
1724                         switch (br->gpencil_settings->icon_id) {
1725                                 case GP_BRUSH_ICON_PENCIL:
1726                                         br->id.icon_id = ICON_GPBRUSH_PENCIL;
1727                                         break;
1728                                 case GP_BRUSH_ICON_PEN:
1729                                         br->id.icon_id = ICON_GPBRUSH_PEN;
1730                                         break;
1731                                 case GP_BRUSH_ICON_INK:
1732                                         br->id.icon_id = ICON_GPBRUSH_INK;
1733                                         break;
1734                                 case GP_BRUSH_ICON_INKNOISE:
1735                                         br->id.icon_id = ICON_GPBRUSH_INKNOISE;
1736                                         break;
1737                                 case GP_BRUSH_ICON_BLOCK:
1738                                         br->id.icon_id = ICON_GPBRUSH_BLOCK;
1739                                         break;
1740                                 case GP_BRUSH_ICON_MARKER:
1741                                         br->id.icon_id = ICON_GPBRUSH_MARKER;
1742                                         break;
1743                                 case GP_BRUSH_ICON_FILL:
1744                                         br->id.icon_id = ICON_GPBRUSH_FILL;
1745                                         break;
1746                                 case GP_BRUSH_ICON_ERASE_SOFT:
1747                                         br->id.icon_id = ICON_GPBRUSH_ERASE_SOFT;
1748                                         break;
1749                                 case GP_BRUSH_ICON_ERASE_HARD:
1750                                         br->id.icon_id = ICON_GPBRUSH_ERASE_HARD;
1751                                         break;
1752                                 case GP_BRUSH_ICON_ERASE_STROKE:
1753                                         br->id.icon_id = ICON_GPBRUSH_ERASE_STROKE;
1754                                         break;
1755                                 default:
1756                                         br->id.icon_id = ICON_GPBRUSH_PEN;
1757                                         break;
1758                         }
1759                         return id->icon_id;
1760                 }
1761                 else if (paint_mode != PAINT_MODE_INVALID) {
1762                         items = BKE_paint_get_tool_enum_from_paintmode(paint_mode);
1763                         const uint tool_offset = BKE_paint_get_brush_tool_offset_from_paintmode(paint_mode);
1764                         const int tool_type = *(char *)POINTER_OFFSET(br, tool_offset);
1765                         if (!items || !RNA_enum_icon_from_value(items, tool_type, &id->icon_id)) {
1766                                 id->icon_id = 0;
1767                         }
1768                 }
1769                 else {
1770                         id->icon_id = 0;
1771                 }
1772         }
1773
1774         return id->icon_id;
1775 }
1776
1777 static int ui_id_screen_get_icon(const bContext *C, ID *id)
1778 {
1779         BKE_icon_id_ensure(id);
1780         /* Don't use jobs here, offscreen rendering doesn't like this and crashes. */
1781         ui_id_icon_render(C, id, false);
1782
1783         return id->icon_id;
1784 }
1785
1786 int ui_id_icon_get(const bContext *C, ID *id, const bool big)
1787 {
1788         int iconid = 0;
1789
1790         /* icon */
1791         switch (GS(id->name)) {
1792                 case ID_BR:
1793                         iconid = ui_id_brush_get_icon(C, id);
1794                         break;
1795                 case ID_MA: /* fall through */
1796                 case ID_TE: /* fall through */
1797                 case ID_IM: /* fall through */
1798                 case ID_WO: /* fall through */
1799                 case ID_LA: /* fall through */
1800                         iconid = BKE_icon_id_ensure(id);
1801                         /* checks if not exists, or changed */
1802                         UI_id_icon_render(C, NULL, id, big, true);
1803                         break;
1804                 case ID_SCR:
1805                         iconid = ui_id_screen_get_icon(C, id);
1806                         break;
1807                 default:
1808                         break;
1809         }
1810
1811         return iconid;
1812 }
1813
1814 int UI_rnaptr_icon_get(bContext *C, PointerRNA *ptr, int rnaicon, const bool big)
1815 {
1816         ID *id = NULL;
1817
1818         if (!ptr->data)
1819                 return rnaicon;
1820
1821         /* try ID, material, texture or dynapaint slot */
1822         if (RNA_struct_is_ID(ptr->type)) {
1823                 id = ptr->id.data;
1824         }
1825         else if (RNA_struct_is_a(ptr->type, &RNA_MaterialSlot)) {
1826                 id = RNA_pointer_get(ptr, "material").data;
1827         }
1828         else if (RNA_struct_is_a(ptr->type, &RNA_TextureSlot)) {
1829                 id = RNA_pointer_get(ptr, "texture").data;
1830         }
1831         else if (RNA_struct_is_a(ptr->type, &RNA_DynamicPaintSurface)) {
1832                 DynamicPaintSurface *surface = ptr->data;
1833
1834                 if (surface->format == MOD_DPAINT_SURFACE_F_PTEX)
1835                         return ICON_SHADING_TEXTURE;
1836                 else if (surface->format == MOD_DPAINT_SURFACE_F_VERTEX)
1837                         return ICON_OUTLINER_DATA_MESH;
1838                 else if (surface->format == MOD_DPAINT_SURFACE_F_IMAGESEQ)
1839                         return ICON_FILE_IMAGE;
1840         }
1841         else if (RNA_struct_is_a(ptr->type, &RNA_StudioLight)) {
1842                 StudioLight *sl = ptr->data;
1843                 switch (sl->flag & STUDIOLIGHT_FLAG_ORIENTATIONS) {
1844                         case STUDIOLIGHT_TYPE_STUDIO:
1845                                 return sl->icon_id_irradiance;
1846                         case STUDIOLIGHT_TYPE_WORLD:
1847                         default:
1848                                 return sl->icon_id_radiance;
1849                         case STUDIOLIGHT_TYPE_MATCAP:
1850                                 return sl->icon_id_matcap;
1851                 }
1852         }
1853
1854         /* get icon from ID */
1855         if (id) {
1856                 int icon = ui_id_icon_get(C, id, big);
1857
1858                 return icon ? icon : rnaicon;
1859         }
1860
1861         return rnaicon;
1862 }
1863
1864 int UI_idcode_icon_get(const int idcode)
1865 {
1866         switch (idcode) {
1867                 case ID_AC:
1868                         return ICON_ACTION;
1869                 case ID_AR:
1870                         return ICON_ARMATURE_DATA;
1871                 case ID_BR:
1872                         return ICON_BRUSH_DATA;
1873                 case ID_CA:
1874                         return ICON_CAMERA_DATA;
1875                 case ID_CF:
1876                         return ICON_FILE;
1877                 case ID_CU:
1878                         return ICON_CURVE_DATA;
1879                 case ID_GD:
1880                         return ICON_GREASEPENCIL;
1881                 case ID_GR:
1882                         return ICON_GROUP;
1883                 case ID_IM:
1884                         return ICON_IMAGE_DATA;
1885                 case ID_LA:
1886                         return ICON_LIGHT_DATA;
1887                 case ID_LS:
1888                         return ICON_LINE_DATA;
1889                 case ID_LT:
1890                         return ICON_LATTICE_DATA;
1891                 case ID_MA:
1892                         return ICON_MATERIAL_DATA;
1893                 case ID_MB:
1894                         return ICON_META_DATA;
1895                 case ID_MC:
1896                         return ICON_TRACKER;
1897                 case ID_ME:
1898                         return ICON_MESH_DATA;
1899                 case ID_MSK:
1900                         return ICON_MOD_MASK;  /* TODO! this would need its own icon! */
1901                 case ID_NT:
1902                         return ICON_NODETREE;
1903                 case ID_OB:
1904                         return ICON_OBJECT_DATA;
1905                 case ID_PA:
1906                         return ICON_PARTICLE_DATA;
1907                 case ID_PAL:
1908                         return ICON_COLOR;  /* TODO! this would need its own icon! */
1909                 case ID_PC:
1910                         return ICON_CURVE_BEZCURVE;  /* TODO! this would need its own icon! */
1911                 case ID_LP:
1912                         return ICON_LIGHTPROBE_CUBEMAP;
1913                 case ID_SCE:
1914                         return ICON_SCENE_DATA;
1915                 case ID_SPK:
1916                         return ICON_SPEAKER;
1917                 case ID_SO:
1918                         return ICON_SOUND;
1919                 case ID_TE:
1920                         return ICON_TEXTURE_DATA;
1921                 case ID_TXT:
1922                         return ICON_TEXT;
1923                 case ID_VF:
1924                         return ICON_FONT_DATA;
1925                 case ID_WO:
1926                         return ICON_WORLD_DATA;
1927                 default:
1928                         return ICON_NONE;
1929         }
1930 }
1931
1932 static void icon_draw_at_size(
1933         float x, float y, int icon_id, float aspect, float alpha,
1934         enum eIconSizes size, const float desaturate, const char mono_color[4])
1935 {
1936         int draw_size = get_draw_size(size);
1937         icon_draw_size(x, y, icon_id, aspect, alpha, NULL, size, draw_size, desaturate, mono_color);
1938 }
1939
1940 void UI_icon_draw_aspect(float x, float y, int icon_id, float aspect, float alpha, const char mono_color[4])
1941 {
1942         icon_draw_at_size(x, y, icon_id, aspect, alpha, ICON_SIZE_ICON, 0.0f, mono_color);
1943 }
1944
1945 void UI_icon_draw_aspect_color(float x, float y, int icon_id, float aspect, const float rgb[3], const char mono_color[4])
1946 {
1947         int draw_size = get_draw_size(ICON_SIZE_ICON);
1948         icon_draw_size(x, y, icon_id, aspect, 1.0f, rgb, ICON_SIZE_ICON, draw_size, false, mono_color);
1949 }
1950
1951 void UI_icon_draw_desaturate(float x, float y, int icon_id, float aspect, float alpha, float desaturate, const char mono_color[4])
1952 {
1953         icon_draw_at_size(x, y, icon_id, aspect, alpha, ICON_SIZE_ICON, desaturate, mono_color);
1954 }
1955
1956 /* draws icon with dpi scale factor */
1957 void UI_icon_draw(float x, float y, int icon_id)
1958 {
1959         UI_icon_draw_aspect(x, y, icon_id, 1.0f / UI_DPI_FAC, 1.0f, NULL);
1960 }
1961
1962 void UI_icon_draw_alpha(float x, float y, int icon_id, float alpha)
1963 {
1964         UI_icon_draw_aspect(x, y, icon_id, 1.0f / UI_DPI_FAC, alpha, NULL);
1965 }
1966
1967 void UI_icon_draw_size(float x, float y, int size, int icon_id, float alpha)
1968 {
1969         icon_draw_size(x, y, icon_id, 1.0f, alpha, NULL, ICON_SIZE_ICON, size, false, NULL);
1970 }
1971
1972 void UI_icon_draw_preview(float x, float y, int icon_id)
1973 {
1974         icon_draw_at_size(x, y, icon_id, 1.0f, 1.0f, ICON_SIZE_PREVIEW, false, NULL);
1975 }
1976
1977 void UI_icon_draw_preview_aspect(float x, float y, int icon_id, float aspect)
1978 {
1979         icon_draw_at_size(x, y, icon_id, aspect, 1.0f, ICON_SIZE_PREVIEW, false, NULL);
1980 }
1981
1982 void UI_icon_draw_preview_aspect_size(float x, float y, int icon_id, float aspect, float alpha, int size)
1983 {
1984         icon_draw_size(x, y, icon_id, aspect, alpha, NULL, ICON_SIZE_PREVIEW, size, false, NULL);
1985 }