1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
|
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* Contributors: Blender Foundation, full recode
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/editors/interface/interface_icons.c
* \ingroup edinterface
*/
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "MEM_guardedalloc.h"
#include "GPU_draw.h"
#include "GPU_immediate.h"
#include "BLI_blenlib.h"
#include "BLI_utildefines.h"
#include "BLI_fileops_types.h"
#include "DNA_brush_types.h"
#include "DNA_curve_types.h"
#include "DNA_dynamicpaint_types.h"
#include "DNA_object_types.h"
#include "DNA_screen_types.h"
#include "DNA_space_types.h"
#include "RNA_access.h"
#include "RNA_enum_types.h"
#include "BKE_context.h"
#include "BKE_global.h"
#include "BKE_icons.h"
#include "BKE_appdir.h"
#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"
#include "IMB_thumbs.h"
#include "BIF_glutil.h"
#include "ED_datafiles.h"
#include "ED_keyframes_draw.h"
#include "ED_render.h"
#include "UI_interface.h"
#include "UI_interface_icons.h"
#include "interface_intern.h"
#ifndef WITH_HEADLESS
#define ICON_GRID_COLS 26
#define ICON_GRID_ROWS 30
#define ICON_GRID_MARGIN 10
#define ICON_GRID_W 32
#define ICON_GRID_H 32
#endif /* WITH_HEADLESS */
typedef struct IconImage {
int w;
int h;
unsigned int *rect;
unsigned char *datatoc_rect;
int datatoc_size;
} IconImage;
typedef void (*VectorDrawFunc)(int x, int y, int w, int h, float alpha);
#define ICON_TYPE_PREVIEW 0
#define ICON_TYPE_TEXTURE 1
#define ICON_TYPE_BUFFER 2
#define ICON_TYPE_VECTOR 3
typedef struct DrawInfo {
int type;
union {
/* type specific data */
struct {
VectorDrawFunc func;
} vector;
struct {
IconImage *image;
} buffer;
struct {
int x, y, w, h;
} texture;
} data;
} DrawInfo;
typedef struct IconTexture {
GLuint id;
int w;
int h;
float invw;
float invh;
} IconTexture;
/* ******************* STATIC LOCAL VARS ******************* */
/* static here to cache results of icon directory scan, so it's not
* scanning the filesystem each time the menu is drawn */
static struct ListBase iconfilelist = {NULL, NULL};
static IconTexture icongltex = {0, 0, 0, 0.0f, 0.0f};
/* **************************************************** */
#ifndef WITH_HEADLESS
static DrawInfo *def_internal_icon(ImBuf *bbuf, int icon_id, int xofs, int yofs, int size, int type)
{
Icon *new_icon = NULL;
IconImage *iimg = NULL;
DrawInfo *di;
new_icon = MEM_callocN(sizeof(Icon), "texicon");
new_icon->obj = NULL; /* icon is not for library object */
new_icon->type = 0;
di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
di->type = type;
if (type == ICON_TYPE_TEXTURE) {
di->data.texture.x = xofs;
di->data.texture.y = yofs;
di->data.texture.w = size;
di->data.texture.h = size;
}
else if (type == ICON_TYPE_BUFFER) {
iimg = MEM_callocN(sizeof(IconImage), "icon_img");
iimg->w = size;
iimg->h = size;
/* icon buffers can get initialized runtime now, via datatoc */
if (bbuf) {
int y, imgsize;
iimg->rect = MEM_mallocN(size * size * sizeof(unsigned int), "icon_rect");
/* Here we store the rect in the icon - same as before */
if (size == bbuf->x && size == bbuf->y && xofs == 0 && yofs == 0)
memcpy(iimg->rect, bbuf->rect, size * size * sizeof(int));
else {
/* this code assumes square images */
imgsize = bbuf->x;
for (y = 0; y < size; y++) {
memcpy(&iimg->rect[y * size], &bbuf->rect[(y + yofs) * imgsize + xofs], size * sizeof(int));
}
}
}
di->data.buffer.image = iimg;
}
new_icon->drawinfo_free = UI_icons_free_drawinfo;
new_icon->drawinfo = di;
BKE_icon_set(icon_id, new_icon);
return di;
}
static void def_internal_vicon(int icon_id, VectorDrawFunc drawFunc)
{
Icon *new_icon = NULL;
DrawInfo *di;
new_icon = MEM_callocN(sizeof(Icon), "texicon");
new_icon->obj = NULL; /* icon is not for library object */
new_icon->type = 0;
di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
di->type = ICON_TYPE_VECTOR;
di->data.vector.func = drawFunc;
new_icon->drawinfo_free = NULL;
new_icon->drawinfo = di;
BKE_icon_set(icon_id, new_icon);
}
/* Vector Icon Drawing Routines */
/* Utilities */
static void viconutil_set_point(GLint pt[2], int x, int y)
{
pt[0] = x;
pt[1] = y;
}
static void vicon_small_tri_right_draw(int x, int y, int w, int UNUSED(h), float alpha)
{
GLint pts[3][2];
int cx = x + w / 2 - 4;
int cy = y + w / 2;
int d = w / 5, d2 = w / 7;
viconutil_set_point(pts[0], cx - d2, cy + d);
viconutil_set_point(pts[1], cx - d2, cy - d);
viconutil_set_point(pts[2], cx + d2, cy);
unsigned int pos = VertexFormat_add_attrib(immVertexFormat(), "pos", COMP_I32, 2, CONVERT_INT_TO_FLOAT);
immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
immUniformColor4f(0.2f, 0.2f, 0.2f, alpha);
immBegin(PRIM_TRIANGLES, 3);
immVertex2iv(pos, pts[0]);
immVertex2iv(pos, pts[1]);
immVertex2iv(pos, pts[2]);
immEnd();
immUnbindProgram();
}
static void vicon_keytype_draw_wrapper(int x, int y, int w, int h, float alpha, short key_type)
{
/* init dummy theme state for Action Editor - where these colors are defined
* (since we're doing this offscreen, free from any particular space_id)
*/
struct bThemeState theme_state;
UI_Theme_Store(&theme_state);
UI_SetTheme(SPACE_ACTION, RGN_TYPE_WINDOW);
/* the "x" and "y" given are the bottom-left coordinates of the icon,
* while the draw_keyframe_shape() function needs the midpoint for
* the keyframe
*/
int xco = x + w / 2;
int yco = y + h / 2;
VertexFormat *format = immVertexFormat();
unsigned int pos_id = VertexFormat_add_attrib(format, "pos", COMP_F32, 2, KEEP_FLOAT);
unsigned int size_id = VertexFormat_add_attrib(format, "size", COMP_F32, 1, KEEP_FLOAT);
unsigned int color_id = VertexFormat_add_attrib(format, "color", COMP_U8, 4, NORMALIZE_INT_TO_FLOAT);
unsigned int outline_color_id = VertexFormat_add_attrib(format, "outlineColor", COMP_U8, 4, NORMALIZE_INT_TO_FLOAT);
immBindBuiltinProgram(GPU_SHADER_KEYFRAME_DIAMOND);
GPU_enable_program_point_size();
immBegin(PRIM_POINTS, 1);
/* draw keyframe
* - size: 0.6 * h (found out experimentally... dunno why!)
* - sel: true (so that "keyframe" state shows the iconic yellow icon)
*/
draw_keyframe_shape(xco, yco, 0.6f * h, true, key_type, KEYFRAME_SHAPE_BOTH, alpha,
pos_id, size_id, color_id, outline_color_id);
immEnd();
GPU_disable_program_point_size();
immUnbindProgram();
UI_Theme_Restore(&theme_state);
}
static void vicon_keytype_keyframe_draw(int x, int y, int w, int h, float alpha)
{
vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_KEYFRAME);
}
static void vicon_keytype_breakdown_draw(int x, int y, int w, int h, float alpha)
{
vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_BREAKDOWN);
}
static void vicon_keytype_extreme_draw(int x, int y, int w, int h, float alpha)
{
vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_EXTREME);
}
static void vicon_keytype_jitter_draw(int x, int y, int w, int h, float alpha)
{
vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_JITTER);
}
static void vicon_keytype_moving_hold_draw(int x, int y, int w, int h, float alpha)
{
vicon_keytype_draw_wrapper(x, y, w, h, alpha, BEZT_KEYTYPE_MOVEHOLD);
}
static void vicon_colorset_draw(int index, int x, int y, int w, int h, float UNUSED(alpha))
{
bTheme *btheme = UI_GetTheme();
ThemeWireColor *cs = &btheme->tarm[index];
/* Draw three bands of color: One per color
* x-----a-----b-----c
* | N | S | A |
* x-----a-----b-----c
*/
const int a = x + w / 3;
const int b = x + w / 3 * 2;
const int c = x + w;
unsigned int pos = VertexFormat_add_attrib(immVertexFormat(), "pos", COMP_I32, 2, CONVERT_INT_TO_FLOAT);
immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
/* XXX: Include alpha into this... */
/* normal */
immUniformColor3ubv((unsigned char *)cs->solid);
immRecti(pos, x, y, a, y + h);
/* selected */
immUniformColor3ubv((unsigned char *)cs->select);
immRecti(pos, a, y, b, y + h);
/* active */
immUniformColor3ubv((unsigned char *)cs->active);
immRecti(pos, b, y, c, y + h);
immUnbindProgram();
}
#define DEF_VICON_COLORSET_DRAW_NTH(prefix, index) \
static void vicon_colorset_draw_##prefix(int x, int y, int w, int h, float alpha) \
{ \
vicon_colorset_draw(index, x, y, w, h, alpha); \
}
DEF_VICON_COLORSET_DRAW_NTH(01, 0)
DEF_VICON_COLORSET_DRAW_NTH(02, 1)
DEF_VICON_COLORSET_DRAW_NTH(03, 2)
DEF_VICON_COLORSET_DRAW_NTH(04, 3)
DEF_VICON_COLORSET_DRAW_NTH(05, 4)
DEF_VICON_COLORSET_DRAW_NTH(06, 5)
DEF_VICON_COLORSET_DRAW_NTH(07, 6)
DEF_VICON_COLORSET_DRAW_NTH(08, 7)
DEF_VICON_COLORSET_DRAW_NTH(09, 8)
DEF_VICON_COLORSET_DRAW_NTH(10, 9)
DEF_VICON_COLORSET_DRAW_NTH(11, 10)
DEF_VICON_COLORSET_DRAW_NTH(12, 11)
DEF_VICON_COLORSET_DRAW_NTH(13, 12)
DEF_VICON_COLORSET_DRAW_NTH(14, 13)
DEF_VICON_COLORSET_DRAW_NTH(15, 14)
DEF_VICON_COLORSET_DRAW_NTH(16, 15)
DEF_VICON_COLORSET_DRAW_NTH(17, 16)
DEF_VICON_COLORSET_DRAW_NTH(18, 17)
DEF_VICON_COLORSET_DRAW_NTH(19, 18)
DEF_VICON_COLORSET_DRAW_NTH(20, 19)
#undef DEF_VICON_COLORSET_DRAW_NTH
#ifndef WITH_HEADLESS
static void init_brush_icons(void)
{
#define INIT_BRUSH_ICON(icon_id, name) \
{ \
unsigned char *rect = (unsigned char *)datatoc_ ##name## _png; \
int size = datatoc_ ##name## _png_size; \
DrawInfo *di; \
\
di = def_internal_icon(NULL, icon_id, 0, 0, w, ICON_TYPE_BUFFER); \
di->data.buffer.image->datatoc_rect = rect; \
di->data.buffer.image->datatoc_size = size; \
}
/* end INIT_BRUSH_ICON */
const int w = 96; /* warning, brush size hardcoded in C, but it gets scaled */
INIT_BRUSH_ICON(ICON_BRUSH_ADD, add);
INIT_BRUSH_ICON(ICON_BRUSH_BLOB, blob);
INIT_BRUSH_ICON(ICON_BRUSH_BLUR, blur);
INIT_BRUSH_ICON(ICON_BRUSH_CLAY, clay);
INIT_BRUSH_ICON(ICON_BRUSH_CLAY_STRIPS, claystrips);
INIT_BRUSH_ICON(ICON_BRUSH_CLONE, clone);
INIT_BRUSH_ICON(ICON_BRUSH_CREASE, crease);
INIT_BRUSH_ICON(ICON_BRUSH_DARKEN, darken);
INIT_BRUSH_ICON(ICON_BRUSH_SCULPT_DRAW, draw);
INIT_BRUSH_ICON(ICON_BRUSH_FILL, fill);
INIT_BRUSH_ICON(ICON_BRUSH_FLATTEN, flatten);
INIT_BRUSH_ICON(ICON_BRUSH_GRAB, grab);
INIT_BRUSH_ICON(ICON_BRUSH_INFLATE, inflate);
INIT_BRUSH_ICON(ICON_BRUSH_LAYER, layer);
INIT_BRUSH_ICON(ICON_BRUSH_LIGHTEN, lighten);
INIT_BRUSH_ICON(ICON_BRUSH_MASK, mask);
INIT_BRUSH_ICON(ICON_BRUSH_MIX, mix);
INIT_BRUSH_ICON(ICON_BRUSH_MULTIPLY, multiply);
INIT_BRUSH_ICON(ICON_BRUSH_NUDGE, nudge);
INIT_BRUSH_ICON(ICON_BRUSH_PINCH, pinch);
INIT_BRUSH_ICON(ICON_BRUSH_SCRAPE, scrape);
INIT_BRUSH_ICON(ICON_BRUSH_SMEAR, smear);
INIT_BRUSH_ICON(ICON_BRUSH_SMOOTH, smooth);
INIT_BRUSH_ICON(ICON_BRUSH_SNAKE_HOOK, snake_hook);
INIT_BRUSH_ICON(ICON_BRUSH_SOFTEN, soften);
INIT_BRUSH_ICON(ICON_BRUSH_SUBTRACT, subtract);
INIT_BRUSH_ICON(ICON_BRUSH_TEXDRAW, texdraw);
INIT_BRUSH_ICON(ICON_BRUSH_TEXFILL, texfill);
INIT_BRUSH_ICON(ICON_BRUSH_TEXMASK, texmask);
INIT_BRUSH_ICON(ICON_BRUSH_THUMB, thumb);
INIT_BRUSH_ICON(ICON_BRUSH_ROTATE, twist);
INIT_BRUSH_ICON(ICON_BRUSH_VERTEXDRAW, vertexdraw);
#undef INIT_BRUSH_ICON
}
static void icon_verify_datatoc(IconImage *iimg)
{
/* if it has own rect, things are all OK */
if (iimg->rect)
return;
if (iimg->datatoc_rect) {
ImBuf *bbuf = IMB_ibImageFromMemory(iimg->datatoc_rect,
iimg->datatoc_size, IB_rect, NULL, "<matcap icon>");
/* w and h were set on initialize */
if (bbuf->x != iimg->h && bbuf->y != iimg->w)
IMB_scaleImBuf(bbuf, iimg->w, iimg->h);
iimg->rect = bbuf->rect;
bbuf->rect = NULL;
IMB_freeImBuf(bbuf);
}
}
static void init_matcap_icons(void)
{
/* dynamic allocation now, tucking datatoc pointers in DrawInfo */
#define INIT_MATCAP_ICON(icon_id, name) \
{ \
unsigned char *rect = (unsigned char *)datatoc_ ##name## _jpg; \
int size = datatoc_ ##name## _jpg_size; \
DrawInfo *di; \
\
di = def_internal_icon(NULL, icon_id, 0, 0, 96, ICON_TYPE_BUFFER); \
di->data.buffer.image->datatoc_rect = rect; \
di->data.buffer.image->datatoc_size = size; \
} (void)0
INIT_MATCAP_ICON(ICON_MATCAP_01, mc01);
INIT_MATCAP_ICON(ICON_MATCAP_02, mc02);
INIT_MATCAP_ICON(ICON_MATCAP_03, mc03);
INIT_MATCAP_ICON(ICON_MATCAP_04, mc04);
INIT_MATCAP_ICON(ICON_MATCAP_05, mc05);
INIT_MATCAP_ICON(ICON_MATCAP_06, mc06);
INIT_MATCAP_ICON(ICON_MATCAP_07, mc07);
INIT_MATCAP_ICON(ICON_MATCAP_08, mc08);
INIT_MATCAP_ICON(ICON_MATCAP_09, mc09);
INIT_MATCAP_ICON(ICON_MATCAP_10, mc10);
INIT_MATCAP_ICON(ICON_MATCAP_11, mc11);
INIT_MATCAP_ICON(ICON_MATCAP_12, mc12);
INIT_MATCAP_ICON(ICON_MATCAP_13, mc13);
INIT_MATCAP_ICON(ICON_MATCAP_14, mc14);
INIT_MATCAP_ICON(ICON_MATCAP_15, mc15);
INIT_MATCAP_ICON(ICON_MATCAP_16, mc16);
INIT_MATCAP_ICON(ICON_MATCAP_17, mc17);
INIT_MATCAP_ICON(ICON_MATCAP_18, mc18);
INIT_MATCAP_ICON(ICON_MATCAP_19, mc19);
INIT_MATCAP_ICON(ICON_MATCAP_20, mc20);
INIT_MATCAP_ICON(ICON_MATCAP_21, mc21);
INIT_MATCAP_ICON(ICON_MATCAP_22, mc22);
INIT_MATCAP_ICON(ICON_MATCAP_23, mc23);
INIT_MATCAP_ICON(ICON_MATCAP_24, mc24);
#undef INIT_MATCAP_ICON
}
static void init_internal_icons(void)
{
// bTheme *btheme = UI_GetTheme();
ImBuf *b16buf = NULL, *b32buf = NULL;
int x, y, icontype;
#if 0 // temp disabled
if ((btheme != NULL) && btheme->tui.iconfile[0]) {
char *icondir = BKE_appdir_folder_id(BLENDER_DATAFILES, "icons");
char iconfilestr[FILE_MAX];
if (icondir) {
BLI_join_dirfile(iconfilestr, sizeof(iconfilestr), icondir, btheme->tui.iconfile);
bbuf = IMB_loadiffname(iconfilestr, IB_rect, NULL); /* if the image is missing bbuf will just be NULL */
if (bbuf && (bbuf->x < ICON_IMAGE_W || bbuf->y < ICON_IMAGE_H)) {
printf("\n***WARNING***\nIcons file %s too small.\nUsing built-in Icons instead\n", iconfilestr);
IMB_freeImBuf(bbuf);
bbuf = NULL;
}
}
else {
printf("%s: 'icons' data path not found, continuing\n", __func__);
}
}
#endif
if (b16buf == NULL)
b16buf = IMB_ibImageFromMemory((unsigned char *)datatoc_blender_icons16_png,
datatoc_blender_icons16_png_size, IB_rect, NULL, "<blender icons>");
if (b16buf)
IMB_premultiply_alpha(b16buf);
if (b32buf == NULL)
b32buf = IMB_ibImageFromMemory((unsigned char *)datatoc_blender_icons32_png,
datatoc_blender_icons32_png_size, IB_rect, NULL, "<blender icons>");
if (b32buf)
IMB_premultiply_alpha(b32buf);
if (b16buf && b32buf) {
/* free existing texture if any */
if (icongltex.id) {
glDeleteTextures(1, &icongltex.id);
icongltex.id = 0;
}
#if 0 /* should be a compile-time check (if needed at all) */
/* we only use a texture for cards with non-power of two */
if (GPU_full_non_power_of_two_support()) {
#else
{
#endif
glGenTextures(1, &icongltex.id);
if (icongltex.id) {
int level = 2;
icongltex.w = b32buf->x;
icongltex.h = b32buf->y;
icongltex.invw = 1.0f / b32buf->x;
icongltex.invh = 1.0f / b32buf->y;
glBindTexture(GL_TEXTURE_2D, icongltex.id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, b32buf->x, b32buf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, b32buf->rect);
glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA8, b16buf->x, b16buf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, b16buf->rect);
while (b16buf->x > 1) {
ImBuf *nbuf = IMB_onehalf(b16buf);
glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA8, nbuf->x, nbuf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, nbuf->rect);
level++;
IMB_freeImBuf(b16buf);
b16buf = nbuf;
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
}
}
}
if (icongltex.id)
icontype = ICON_TYPE_TEXTURE;
else
icontype = ICON_TYPE_BUFFER;
if (b32buf) {
for (y = 0; y < ICON_GRID_ROWS; y++) {
for (x = 0; x < ICON_GRID_COLS; x++) {
def_internal_icon(b32buf, BIFICONID_FIRST + y * ICON_GRID_COLS + x,
x * (ICON_GRID_W + ICON_GRID_MARGIN) + ICON_GRID_MARGIN,
y * (ICON_GRID_H + ICON_GRID_MARGIN) + ICON_GRID_MARGIN, ICON_GRID_W,
icontype);
}
}
}
def_internal_vicon(VICO_SMALL_TRI_RIGHT_VEC, vicon_small_tri_right_draw);
def_internal_vicon(VICO_KEYTYPE_KEYFRAME_VEC, vicon_keytype_keyframe_draw);
def_internal_vicon(VICO_KEYTYPE_BREAKDOWN_VEC, vicon_keytype_breakdown_draw);
def_internal_vicon(VICO_KEYTYPE_EXTREME_VEC, vicon_keytype_extreme_draw);
def_internal_vicon(VICO_KEYTYPE_JITTER_VEC, vicon_keytype_jitter_draw);
def_internal_vicon(VICO_KEYTYPE_MOVING_HOLD_VEC, vicon_keytype_moving_hold_draw);
def_internal_vicon(VICO_COLORSET_01_VEC, vicon_colorset_draw_01);
def_internal_vicon(VICO_COLORSET_02_VEC, vicon_colorset_draw_02);
def_internal_vicon(VICO_COLORSET_03_VEC, vicon_colorset_draw_03);
def_internal_vicon(VICO_COLORSET_04_VEC, vicon_colorset_draw_04);
def_internal_vicon(VICO_COLORSET_05_VEC, vicon_colorset_draw_05);
def_internal_vicon(VICO_COLORSET_06_VEC, vicon_colorset_draw_06);
def_internal_vicon(VICO_COLORSET_07_VEC, vicon_colorset_draw_07);
def_internal_vicon(VICO_COLORSET_08_VEC, vicon_colorset_draw_08);
def_internal_vicon(VICO_COLORSET_09_VEC, vicon_colorset_draw_09);
def_internal_vicon(VICO_COLORSET_10_VEC, vicon_colorset_draw_10);
def_internal_vicon(VICO_COLORSET_11_VEC, vicon_colorset_draw_11);
def_internal_vicon(VICO_COLORSET_12_VEC, vicon_colorset_draw_12);
def_internal_vicon(VICO_COLORSET_13_VEC, vicon_colorset_draw_13);
def_internal_vicon(VICO_COLORSET_14_VEC, vicon_colorset_draw_14);
def_internal_vicon(VICO_COLORSET_15_VEC, vicon_colorset_draw_15);
def_internal_vicon(VICO_COLORSET_16_VEC, vicon_colorset_draw_16);
def_internal_vicon(VICO_COLORSET_17_VEC, vicon_colorset_draw_17);
def_internal_vicon(VICO_COLORSET_18_VEC, vicon_colorset_draw_18);
def_internal_vicon(VICO_COLORSET_19_VEC, vicon_colorset_draw_19);
def_internal_vicon(VICO_COLORSET_20_VEC, vicon_colorset_draw_20);
IMB_freeImBuf(b16buf);
IMB_freeImBuf(b32buf);
}
#endif /* WITH_HEADLESS */
static void init_iconfile_list(struct ListBase *list)
{
IconFile *ifile;
struct direntry *dir;
int totfile, i, index = 1;
const char *icondir;
BLI_listbase_clear(list);
icondir = BKE_appdir_folder_id(BLENDER_DATAFILES, "icons");
if (icondir == NULL)
return;
totfile = BLI_filelist_dir_contents(icondir, &dir);
for (i = 0; i < totfile; i++) {
if ((dir[i].type & S_IFREG)) {
const char *filename = dir[i].relname;
if (BLI_testextensie(filename, ".png")) {
/* loading all icons on file start is overkill & slows startup
* its possible they change size after blender load anyway. */
#if 0
int ifilex, ifiley;
char iconfilestr[FILE_MAX + 16]; /* allow 256 chars for file+dir */
ImBuf *bbuf = NULL;
/* check to see if the image is the right size, continue if not */
/* copying strings here should go ok, assuming that we never get back
* a complete path to file longer than 256 chars */
BLI_join_dirfile(iconfilestr, sizeof(iconfilestr), icondir, filename);
bbuf = IMB_loadiffname(iconfilestr, IB_rect);
if (bbuf) {
ifilex = bbuf->x;
ifiley = bbuf->y;
IMB_freeImBuf(bbuf);
}
else {
ifilex = ifiley = 0;
}
/* bad size or failed to load */
if ((ifilex != ICON_IMAGE_W) || (ifiley != ICON_IMAGE_H)) {
printf("icon '%s' is wrong size %dx%d\n", iconfilestr, ifilex, ifiley);
continue;
}
#endif /* removed */
/* found a potential icon file, so make an entry for it in the cache list */
ifile = MEM_callocN(sizeof(IconFile), "IconFile");
BLI_strncpy(ifile->filename, filename, sizeof(ifile->filename));
ifile->index = index;
BLI_addtail(list, ifile);
index++;
}
}
}
BLI_filelist_free(dir, totfile);
dir = NULL;
}
static void free_iconfile_list(struct ListBase *list)
{
IconFile *ifile = NULL, *next_ifile = NULL;
for (ifile = list->first; ifile; ifile = next_ifile) {
next_ifile = ifile->next;
BLI_freelinkN(list, ifile);
}
}
#endif /* WITH_HEADLESS */
int UI_iconfile_get_index(const char *filename)
{
IconFile *ifile;
ListBase *list = &(iconfilelist);
for (ifile = list->first; ifile; ifile = ifile->next) {
if (BLI_path_cmp(filename, ifile->filename) == 0) {
return ifile->index;
}
}
return 0;
}
ListBase *UI_iconfile_list(void)
{
ListBase *list = &(iconfilelist);
return list;
}
void UI_icons_free(void)
{
#ifndef WITH_HEADLESS
if (icongltex.id) {
glDeleteTextures(1, &icongltex.id);
icongltex.id = 0;
}
free_iconfile_list(&iconfilelist);
BKE_icons_free();
#endif
}
void UI_icons_free_drawinfo(void *drawinfo)
{
DrawInfo *di = drawinfo;
if (di) {
if (di->type == ICON_TYPE_BUFFER) {
if (di->data.buffer.image) {
if (di->data.buffer.image->rect)
MEM_freeN(di->data.buffer.image->rect);
MEM_freeN(di->data.buffer.image);
}
}
MEM_freeN(di);
}
}
static DrawInfo *icon_create_drawinfo(void)
{
DrawInfo *di = NULL;
di = MEM_callocN(sizeof(DrawInfo), "di_icon");
di->type = ICON_TYPE_PREVIEW;
return di;
}
/* note!, returns unscaled by DPI */
int UI_icon_get_width(int icon_id)
{
Icon *icon = NULL;
DrawInfo *di = NULL;
icon = BKE_icon_get(icon_id);
if (icon == NULL) {
if (G.debug & G_DEBUG)
printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
return 0;
}
di = (DrawInfo *)icon->drawinfo;
if (!di) {
di = icon_create_drawinfo();
icon->drawinfo = di;
}
if (di)
return ICON_DEFAULT_WIDTH;
return 0;
}
int UI_icon_get_height(int icon_id)
{
Icon *icon = NULL;
DrawInfo *di = NULL;
icon = BKE_icon_get(icon_id);
if (icon == NULL) {
if (G.debug & G_DEBUG)
printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
return 0;
}
di = (DrawInfo *)icon->drawinfo;
if (!di) {
di = icon_create_drawinfo();
icon->drawinfo = di;
}
if (di)
return ICON_DEFAULT_HEIGHT;
return 0;
}
void UI_icons_init(int first_dyn_id)
{
BKE_icons_init(first_dyn_id);
#ifndef WITH_HEADLESS
init_iconfile_list(&iconfilelist);
init_internal_icons();
init_brush_icons();
init_matcap_icons();
#endif
}
/* Render size for preview images and icons
*/
int UI_preview_render_size(enum eIconSizes size)
{
switch (size) {
case ICON_SIZE_ICON:
return ICON_RENDER_DEFAULT_HEIGHT;
case ICON_SIZE_PREVIEW:
return PREVIEW_RENDER_DEFAULT_HEIGHT;
default:
return 0;
}
}
/* Create rect for the icon
*/
static void icon_create_rect(struct PreviewImage *prv_img, enum eIconSizes size)
{
unsigned int render_size = UI_preview_render_size(size);
if (!prv_img) {
if (G.debug & G_DEBUG)
printf("%s, error: requested preview image does not exist", __func__);
}
else if (!prv_img->rect[size]) {
prv_img->w[size] = render_size;
prv_img->h[size] = render_size;
prv_img->flag[size] |= PRV_CHANGED;
prv_img->changed_timestamp[size] = 0;
prv_img->rect[size] = MEM_callocN(render_size * render_size * sizeof(unsigned int), "prv_rect");
}
}
static void ui_id_preview_image_render_size(
const bContext *C, Scene *scene, ID *id, PreviewImage *pi, int size, const bool use_job);
void ui_icon_ensure_deferred(const bContext *C, const int icon_id, const bool big)
{
Icon *icon = BKE_icon_get(icon_id);
if (icon) {
DrawInfo *di = (DrawInfo *)icon->drawinfo;
if (!di) {
di = icon_create_drawinfo();
icon->drawinfo = di;
icon->drawinfo_free = UI_icons_free_drawinfo;
}
if (di) {
switch (di->type) {
case ICON_TYPE_PREVIEW:
{
ID *id = (icon->type != 0) ? icon->obj : NULL;
PreviewImage *prv = id ? BKE_previewimg_id_ensure(id) : icon->obj;
/* Using jobs for screen previews crashes due to offscreen rendering.
* XXX would be nicer if PreviewImage could store if it supports jobs */
const bool use_jobs = !id || (GS(id->name) != ID_SCR);
if (prv) {
const int size = big ? ICON_SIZE_PREVIEW : ICON_SIZE_ICON;
if (id || (prv->tag & PRV_TAG_DEFFERED) != 0) {
ui_id_preview_image_render_size(C, NULL, id, prv, size, use_jobs);
}
}
break;
}
}
}
}
}
/* only called when icon has changed */
/* only call with valid pointer from UI_icon_draw */
static void icon_set_image(
const bContext *C, Scene *scene, ID *id, PreviewImage *prv_img, enum eIconSizes size, const bool use_job)
{
if (!prv_img) {
if (G.debug & G_DEBUG)
printf("%s: no preview image for this ID: %s\n", __func__, id->name);
return;
}
if (prv_img->flag[size] & PRV_USER_EDITED) {
/* user-edited preview, do not auto-update! */
return;
}
icon_create_rect(prv_img, size);
if (use_job) {
/* Job (background) version */
ED_preview_icon_job(C, prv_img, id, prv_img->rect[size], prv_img->w[size], prv_img->h[size]);
}
else {
if (!scene) {
scene = CTX_data_scene(C);
}
/* Immediate version */
ED_preview_icon_render(CTX_data_main(C), scene, id, prv_img->rect[size], prv_img->w[size], prv_img->h[size]);
}
}
PreviewImage *UI_icon_to_preview(int icon_id)
{
Icon *icon = BKE_icon_get(icon_id);
if (icon) {
DrawInfo *di = (DrawInfo *)icon->drawinfo;
if (di) {
if (di->type == ICON_TYPE_PREVIEW) {
PreviewImage *prv = (icon->type != 0) ? BKE_previewimg_id_ensure((ID *)icon->obj) : icon->obj;
if (prv) {
return BKE_previewimg_copy(prv);
}
}
else if (di->data.buffer.image) {
ImBuf *bbuf;
bbuf = IMB_ibImageFromMemory(di->data.buffer.image->datatoc_rect, di->data.buffer.image->datatoc_size,
IB_rect, NULL, __func__);
if (bbuf) {
PreviewImage *prv = BKE_previewimg_create();
prv->rect[0] = bbuf->rect;
prv->w[0] = bbuf->x;
prv->h[0] = bbuf->y;
bbuf->rect = NULL;
IMB_freeImBuf(bbuf);
return prv;
}
}
}
}
return NULL;
}
static void icon_draw_rect(float x, float y, int w, int h, float UNUSED(aspect), int rw, int rh,
unsigned int *rect, float alpha, const float rgb[3], const bool UNUSED(is_preview))
{
ImBuf *ima = NULL;
int draw_w = w;
int draw_h = h;
int draw_x = x;
int draw_y = y;
/* sanity check */
if (w <= 0 || h <= 0 || w > 2000 || h > 2000) {
printf("%s: icons are %i x %i pixels?\n", __func__, w, h);
BLI_assert(!"invalid icon size");
return;
}
/* modulate color */
float col[4] = {1.0f, 1.0f, 1.0f, alpha};
if (rgb) {
col[0] = rgb[0];
col[1] = rgb[1];
col[2] = rgb[2];
}
/* rect contains image in 'rendersize', we only scale if needed */
if (rw != w || rh != h) {
/* preserve aspect ratio and center */
if (rw > rh) {
draw_w = w;
draw_h = (int)(((float)rh / (float)rw) * (float)w);
draw_y += (h - draw_h) / 2;
}
else if (rw < rh) {
draw_w = (int)(((float)rw / (float)rh) * (float)h);
draw_h = h;
draw_x += (w - draw_w) / 2;
}
/* if the image is squared, the draw_ initialization values are good */
/* first allocate imbuf for scaling and copy preview into it */
ima = IMB_allocImBuf(rw, rh, 32, IB_rect);
memcpy(ima->rect, rect, rw * rh * sizeof(unsigned int));
IMB_scaleImBuf(ima, draw_w, draw_h); /* scale it */
rect = ima->rect;
}
/* draw */
IMMDrawPixelsTexState state = immDrawPixelsTexSetup(GPU_SHADER_2D_IMAGE_COLOR);
immDrawPixelsTex(&state, draw_x, draw_y, draw_w, draw_h, GL_RGBA, GL_UNSIGNED_BYTE, GL_NEAREST, rect,
1.0f, 1.0f, col);
if (ima)
IMB_freeImBuf(ima);
}
static void icon_draw_texture(
float x, float y, float w, float h, int ix, int iy,
int UNUSED(iw), int ih, float alpha, const float rgb[3])
{
float x1, x2, y1, y2;
x1 = ix * icongltex.invw;
x2 = (ix + ih) * icongltex.invw;
y1 = iy * icongltex.invh;
y2 = (iy + ih) * icongltex.invh;
glBindTexture(GL_TEXTURE_2D, icongltex.id);
VertexFormat *format = immVertexFormat();
unsigned int pos = VertexFormat_add_attrib(format, "pos", COMP_F32, 2, KEEP_FLOAT);
unsigned int texCoord = VertexFormat_add_attrib(format, "texCoord", COMP_F32, 2, KEEP_FLOAT);
immBindBuiltinProgram(GPU_SHADER_2D_IMAGE_COLOR);
if (rgb) immUniformColor3fvAlpha(rgb, alpha);
else immUniformColor4f(alpha, alpha, alpha, alpha);
immUniform1i("image", 0);
immBegin(PRIM_TRIANGLE_STRIP, 4);
immAttrib2f(texCoord, x1, y2);
immVertex2f(pos, x, y + h);
immAttrib2f(texCoord, x1, y1);
immVertex2f(pos, x, y);
immAttrib2f(texCoord, x2, y2);
immVertex2f(pos, x + w, y + h);
immAttrib2f(texCoord, x2, y1);
immVertex2f(pos, x + w, y);
immEnd();
immUnbindProgram();
glBindTexture(GL_TEXTURE_2D, 0);
}
/* Drawing size for preview images */
static int get_draw_size(enum eIconSizes size)
{
switch (size) {
case ICON_SIZE_ICON:
return ICON_DEFAULT_HEIGHT;
case ICON_SIZE_PREVIEW:
return PREVIEW_DEFAULT_HEIGHT;
default:
return 0;
}
}
static void icon_draw_size(
float x, float y, int icon_id, float aspect, float alpha, const float rgb[3],
enum eIconSizes size, int draw_size, const bool UNUSED(nocreate), const bool is_preview)
{
bTheme *btheme = UI_GetTheme();
Icon *icon = NULL;
DrawInfo *di = NULL;
IconImage *iimg;
const float fdraw_size = (float)draw_size;
int w, h;
icon = BKE_icon_get(icon_id);
alpha *= btheme->tui.icon_alpha;
if (icon == NULL) {
if (G.debug & G_DEBUG)
printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
return;
}
di = (DrawInfo *)icon->drawinfo;
if (!di) {
di = icon_create_drawinfo();
icon->drawinfo = di;
icon->drawinfo_free = UI_icons_free_drawinfo;
}
/* scale width and height according to aspect */
w = (int)(fdraw_size / aspect + 0.5f);
h = (int)(fdraw_size / aspect + 0.5f);
if (di->type == ICON_TYPE_VECTOR) {
/* vector icons use the uiBlock transformation, they are not drawn
* with untransformed coordinates like the other icons */
di->data.vector.func((int)x, (int)y, w, h, 1.0f);
}
else if (di->type == ICON_TYPE_TEXTURE) {
/* texture image use premul alpha for correct scaling */
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
icon_draw_texture(x, y, (float)w, (float)h, di->data.texture.x, di->data.texture.y,
di->data.texture.w, di->data.texture.h, alpha, rgb);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
else if (di->type == ICON_TYPE_BUFFER) {
/* it is a builtin icon */
iimg = di->data.buffer.image;
#ifndef WITH_HEADLESS
icon_verify_datatoc(iimg);
#endif
if (!iimg->rect) return; /* something has gone wrong! */
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
icon_draw_rect(x, y, w, h, aspect, iimg->w, iimg->h, iimg->rect, alpha, rgb, is_preview);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
else if (di->type == ICON_TYPE_PREVIEW) {
PreviewImage *pi = (icon->type != 0) ? BKE_previewimg_id_ensure((ID *)icon->obj) : icon->obj;
if (pi) {
/* no create icon on this level in code */
if (!pi->rect[size]) return; /* something has gone wrong! */
/* preview images use premul alpha ... */
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
icon_draw_rect(x, y, w, h, aspect, pi->w[size], pi->h[size], pi->rect[size], alpha, rgb, is_preview);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
}
}
static void ui_id_preview_image_render_size(
const bContext *C, Scene *scene, ID *id, PreviewImage *pi, int size, const bool use_job)
{
if (((pi->flag[size] & PRV_CHANGED) || !pi->rect[size])) { /* changed only ever set by dynamic icons */
/* create the rect if necessary */
icon_set_image(C, scene, id, pi, size, use_job);
pi->flag[size] &= ~PRV_CHANGED;
}
}
void UI_id_icon_render(const bContext *C, Scene *scene, ID *id, const bool big, const bool use_job)
{
PreviewImage *pi = BKE_previewimg_id_ensure(id);
if (pi) {
if (big)
ui_id_preview_image_render_size(C, scene, id, pi, ICON_SIZE_PREVIEW, use_job); /* bigger preview size */
else
ui_id_preview_image_render_size(C, scene, id, pi, ICON_SIZE_ICON, use_job); /* icon size */
}
}
static void ui_id_icon_render(const bContext *C, ID *id, bool use_jobs)
{
PreviewImage *pi = BKE_previewimg_id_ensure(id);
enum eIconSizes i;
if (!pi)
return;
for (i = 0; i < NUM_ICON_SIZES; i++) {
/* check if rect needs to be created; changed
* only set by dynamic icons */
if (((pi->flag[i] & PRV_CHANGED) || !pi->rect[i])) {
icon_set_image(C, NULL, id, pi, i, use_jobs);
pi->flag[i] &= ~PRV_CHANGED;
}
}
}
static int ui_id_brush_get_icon(const bContext *C, ID *id)
{
Brush *br = (Brush *)id;
if (br->flag & BRUSH_CUSTOM_ICON) {
BKE_icon_id_ensure(id);
ui_id_icon_render(C, id, true);
}
else {
Object *ob = CTX_data_active_object(C);
SpaceImage *sima;
EnumPropertyItem *items = NULL;
int tool = PAINT_TOOL_DRAW, mode = 0;
/* XXX: this is not nice, should probably make brushes
* be strictly in one paint mode only to avoid
* checking various context stuff here */
if (CTX_wm_view3d(C) && ob) {
if (ob->mode & OB_MODE_SCULPT)
mode = OB_MODE_SCULPT;
else if (ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT))
mode = OB_MODE_VERTEX_PAINT;
else if (ob->mode & OB_MODE_TEXTURE_PAINT)
mode = OB_MODE_TEXTURE_PAINT;
}
else if ((sima = CTX_wm_space_image(C)) &&
(sima->mode == SI_MODE_PAINT))
{
mode = OB_MODE_TEXTURE_PAINT;
}
/* reset the icon */
if (mode == OB_MODE_SCULPT) {
items = rna_enum_brush_sculpt_tool_items;
tool = br->sculpt_tool;
}
else if (mode == OB_MODE_VERTEX_PAINT) {
items = rna_enum_brush_vertex_tool_items;
tool = br->vertexpaint_tool;
}
else if (mode == OB_MODE_TEXTURE_PAINT) {
items = rna_enum_brush_image_tool_items;
tool = br->imagepaint_tool;
}
if (!items || !RNA_enum_icon_from_value(items, tool, &id->icon_id))
id->icon_id = 0;
}
return id->icon_id;
}
static int ui_id_screen_get_icon(const bContext *C, ID *id)
{
BKE_icon_id_ensure(id);
/* Don't use jobs here, offscreen rendering doesn't like this and crashes. */
ui_id_icon_render(C, id, false);
return id->icon_id;
}
int ui_id_icon_get(const bContext *C, ID *id, const bool big)
{
int iconid = 0;
/* icon */
switch (GS(id->name)) {
case ID_BR:
iconid = ui_id_brush_get_icon(C, id);
break;
case ID_MA: /* fall through */
case ID_TE: /* fall through */
case ID_IM: /* fall through */
case ID_WO: /* fall through */
case ID_LA: /* fall through */
iconid = BKE_icon_id_ensure(id);
/* checks if not exists, or changed */
UI_id_icon_render(C, NULL, id, big, true);
break;
case ID_SCR:
iconid = ui_id_screen_get_icon(C, id);
break;
default:
break;
}
return iconid;
}
int UI_rnaptr_icon_get(bContext *C, PointerRNA *ptr, int rnaicon, const bool big)
{
ID *id = NULL;
if (!ptr->data)
return rnaicon;
/* try ID, material, texture or dynapaint slot */
if (RNA_struct_is_ID(ptr->type)) {
id = ptr->id.data;
}
else if (RNA_struct_is_a(ptr->type, &RNA_MaterialSlot)) {
id = RNA_pointer_get(ptr, "material").data;
}
else if (RNA_struct_is_a(ptr->type, &RNA_TextureSlot)) {
id = RNA_pointer_get(ptr, "texture").data;
}
else if (RNA_struct_is_a(ptr->type, &RNA_DynamicPaintSurface)) {
DynamicPaintSurface *surface = (DynamicPaintSurface *)ptr->data;
if (surface->format == MOD_DPAINT_SURFACE_F_PTEX)
return ICON_TEXTURE_SHADED;
else if (surface->format == MOD_DPAINT_SURFACE_F_VERTEX)
return ICON_OUTLINER_DATA_MESH;
else if (surface->format == MOD_DPAINT_SURFACE_F_IMAGESEQ)
return ICON_FILE_IMAGE;
}
/* get icon from ID */
if (id) {
int icon = ui_id_icon_get(C, id, big);
return icon ? icon : rnaicon;
}
return rnaicon;
}
int UI_idcode_icon_get(const int idcode)
{
switch (idcode) {
case ID_AC:
return ICON_ANIM_DATA;
case ID_AR:
return ICON_ARMATURE_DATA;
case ID_BR:
return ICON_BRUSH_DATA;
case ID_CA:
return ICON_CAMERA_DATA;
case ID_CF:
return ICON_FILE;
case ID_CU:
return ICON_CURVE_DATA;
case ID_GD:
return ICON_GREASEPENCIL;
case ID_GR:
return ICON_GROUP;
case ID_IM:
return ICON_IMAGE_DATA;
case ID_LA:
return ICON_LAMP_DATA;
case ID_LS:
return ICON_LINE_DATA;
case ID_LT:
return ICON_LATTICE_DATA;
case ID_MA:
return ICON_MATERIAL_DATA;
case ID_MB:
return ICON_META_DATA;
case ID_MC:
return ICON_CLIP;
case ID_ME:
return ICON_MESH_DATA;
case ID_MSK:
return ICON_MOD_MASK; /* TODO! this would need its own icon! */
case ID_NT:
return ICON_NODETREE;
case ID_OB:
return ICON_OBJECT_DATA;
case ID_PA:
return ICON_PARTICLE_DATA;
case ID_PAL:
return ICON_COLOR; /* TODO! this would need its own icon! */
case ID_PC:
return ICON_CURVE_BEZCURVE; /* TODO! this would need its own icon! */
case ID_LP:
return ICON_RADIO;
case ID_SCE:
return ICON_SCENE_DATA;
case ID_SPK:
return ICON_SPEAKER;
case ID_SO:
return ICON_SOUND;
case ID_TE:
return ICON_TEXTURE_DATA;
case ID_TXT:
return ICON_TEXT;
case ID_VF:
return ICON_FONT_DATA;
case ID_WO:
return ICON_WORLD_DATA;
default:
return ICON_NONE;
}
}
static void icon_draw_at_size(
float x, float y, int icon_id, float aspect, float alpha,
enum eIconSizes size, const bool nocreate)
{
int draw_size = get_draw_size(size);
icon_draw_size(x, y, icon_id, aspect, alpha, NULL, size, draw_size, nocreate, false);
}
void UI_icon_draw_aspect(float x, float y, int icon_id, float aspect, float alpha)
{
icon_draw_at_size(x, y, icon_id, aspect, alpha, ICON_SIZE_ICON, 0);
}
void UI_icon_draw_aspect_color(float x, float y, int icon_id, float aspect, const float rgb[3])
{
int draw_size = get_draw_size(ICON_SIZE_ICON);
icon_draw_size(x, y, icon_id, aspect, 1.0f, rgb, ICON_SIZE_ICON, draw_size, false, false);
}
/* draws icon with dpi scale factor */
void UI_icon_draw(float x, float y, int icon_id)
{
UI_icon_draw_aspect(x, y, icon_id, 1.0f / UI_DPI_FAC, 1.0f);
}
void UI_icon_draw_alpha(float x, float y, int icon_id, float alpha)
{
UI_icon_draw_aspect(x, y, icon_id, 1.0f / UI_DPI_FAC, alpha);
}
void UI_icon_draw_size(float x, float y, int size, int icon_id, float alpha)
{
icon_draw_size(x, y, icon_id, 1.0f, alpha, NULL, ICON_SIZE_ICON, size, true, false);
}
void UI_icon_draw_preview(float x, float y, int icon_id)
{
icon_draw_at_size(x, y, icon_id, 1.0f, 1.0f, ICON_SIZE_PREVIEW, 0);
}
void UI_icon_draw_preview_aspect(float x, float y, int icon_id, float aspect)
{
icon_draw_at_size(x, y, icon_id, aspect, 1.0f, ICON_SIZE_PREVIEW, 0);
}
void UI_icon_draw_preview_aspect_size(float x, float y, int icon_id, float aspect, float alpha, int size)
{
icon_draw_size(x, y, icon_id, aspect, alpha, NULL, ICON_SIZE_PREVIEW, size, false, true);
}
|