4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * Contributors: Blender Foundation, full recode
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/editors/interface/interface_icons.c
29 * \ingroup edinterface
42 #include "BLI_winstuff.h"
44 #include "MEM_guardedalloc.h"
46 #include "GPU_extensions.h"
49 #include "BLI_blenlib.h"
50 #include "BLI_storage_types.h"
51 #include "BLI_utildefines.h"
53 #include "DNA_brush_types.h"
54 #include "DNA_object_types.h"
55 #include "DNA_screen_types.h"
56 #include "DNA_space_types.h"
58 #include "RNA_access.h"
59 #include "RNA_enum_types.h"
61 #include "BKE_context.h"
62 #include "BKE_global.h"
63 #include "BKE_icons.h"
64 #include "BKE_utildefines.h"
66 #include "IMB_imbuf.h"
67 #include "IMB_imbuf_types.h"
70 #include "BIF_glutil.h"
72 #include "ED_datafiles.h"
73 #include "ED_render.h"
75 #include "UI_interface.h"
76 #include "UI_interface_icons.h"
78 #include "interface_intern.h"
81 #define ICON_IMAGE_W 600
82 #define ICON_IMAGE_H 640
84 #define ICON_GRID_COLS 26
85 #define ICON_GRID_ROWS 30
87 #define ICON_GRID_MARGIN 5
88 #define ICON_GRID_W 16
89 #define ICON_GRID_H 16
91 typedef struct IconImage {
97 typedef void (*VectorDrawFunc)(int x, int y, int w, int h, float alpha);
99 #define ICON_TYPE_PREVIEW 0
100 #define ICON_TYPE_TEXTURE 1
101 #define ICON_TYPE_BUFFER 2
102 #define ICON_TYPE_VECTOR 3
104 typedef struct DrawInfo {
108 /* type specific data */
121 typedef struct IconTexture {
129 /* ******************* STATIC LOCAL VARS ******************* */
130 /* static here to cache results of icon directory scan, so it's not
131 * scanning the filesystem each time the menu is drawn */
132 static struct ListBase iconfilelist = {NULL, NULL};
133 static IconTexture icongltex = {0, 0, 0, 0.0f, 0.0f};
135 /* **************************************************** */
137 static void def_internal_icon(ImBuf *bbuf, int icon_id, int xofs, int yofs, int size, int type)
139 Icon *new_icon = NULL;
140 IconImage *iimg = NULL;
145 new_icon = MEM_callocN(sizeof(Icon), "texicon");
147 new_icon->obj = NULL; /* icon is not for library object */
150 di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
153 if(type == ICON_TYPE_TEXTURE) {
154 di->data.texture.x= xofs;
155 di->data.texture.y= yofs;
156 di->data.texture.w= size;
157 di->data.texture.h= size;
159 else if(type == ICON_TYPE_BUFFER) {
160 iimg = MEM_mallocN(sizeof(IconImage), "icon_img");
161 iimg->rect = MEM_mallocN(size*size*sizeof(unsigned int), "icon_rect");
165 /* Here we store the rect in the icon - same as before */
167 for (y=0; y<size; y++) {
168 memcpy(&iimg->rect[y*size], &bbuf->rect[(y+yofs)*imgsize+xofs], size*sizeof(int));
171 di->data.buffer.image = iimg;
174 new_icon->drawinfo_free = UI_icons_free_drawinfo;
175 new_icon->drawinfo = di;
177 BKE_icon_set(icon_id, new_icon);
180 static void def_internal_vicon( int icon_id, VectorDrawFunc drawFunc)
182 Icon *new_icon = NULL;
185 new_icon = MEM_callocN(sizeof(Icon), "texicon");
187 new_icon->obj = NULL; /* icon is not for library object */
190 di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
191 di->type= ICON_TYPE_VECTOR;
192 di->data.vector.func =drawFunc;
194 new_icon->drawinfo_free = NULL;
195 new_icon->drawinfo = di;
197 BKE_icon_set(icon_id, new_icon);
200 /* Vector Icon Drawing Routines */
204 static void viconutil_set_point(GLint pt[2], int x, int y)
210 static void viconutil_draw_tri(GLint (*pts)[2])
212 glBegin(GL_TRIANGLES);
219 static void viconutil_draw_lineloop(GLint (*pts)[2], int numPoints)
223 glBegin(GL_LINE_LOOP);
224 for (i=0; i<numPoints; i++) {
230 static void viconutil_draw_lineloop_smooth(GLint (*pts)[2], int numPoints)
232 glEnable(GL_LINE_SMOOTH);
233 viconutil_draw_lineloop(pts, numPoints);
234 glDisable(GL_LINE_SMOOTH);
237 static void viconutil_draw_points(GLint (*pts)[2], int numPoints, int pointSize)
242 for (i=0; i<numPoints; i++) {
243 int x = pts[i][0], y = pts[i][1];
245 glVertex2i(x-pointSize,y-pointSize);
246 glVertex2i(x+pointSize,y-pointSize);
247 glVertex2i(x+pointSize,y+pointSize);
248 glVertex2i(x-pointSize,y+pointSize);
253 /* Drawing functions */
255 static void vicon_x_draw(int x, int y, int w, int h, float alpha)
262 glEnable( GL_LINE_SMOOTH );
266 glColor4f(0.0, 0.0, 0.0, alpha);
276 glDisable( GL_LINE_SMOOTH );
279 static void vicon_view3d_draw(int x, int y, int w, int h, float alpha)
283 int d = MAX2(2, h/3);
285 glColor4f(0.5, 0.5, 0.5, alpha);
287 glVertex2i(x , cy-d);
288 glVertex2i(x+w, cy-d);
289 glVertex2i(x , cy+d);
290 glVertex2i(x+w, cy+d);
292 glVertex2i(cx-d, y );
293 glVertex2i(cx-d, y+h);
294 glVertex2i(cx+d, y );
295 glVertex2i(cx+d, y+h);
298 glColor4f(0.0, 0.0, 0.0, alpha);
307 static void vicon_edit_draw(int x, int y, int w, int h, float alpha)
311 viconutil_set_point(pts[0], x+3 , y+3 );
312 viconutil_set_point(pts[1], x+w-3, y+3 );
313 viconutil_set_point(pts[2], x+w-3, y+h-3);
314 viconutil_set_point(pts[3], x+3 , y+h-3);
316 glColor4f(0.0, 0.0, 0.0, alpha);
317 viconutil_draw_lineloop(pts, 4);
319 glColor3f(1, 1, 0.0);
320 viconutil_draw_points(pts, 4, 1);
323 static void vicon_editmode_hlt_draw(int x, int y, int w, int h, float alpha)
327 viconutil_set_point(pts[0], x+w/2, y+h-2);
328 viconutil_set_point(pts[1], x+3, y+4);
329 viconutil_set_point(pts[2], x+w-3, y+4);
331 glColor4f(0.5, 0.5, 0.5, alpha);
332 viconutil_draw_tri(pts);
334 glColor4f(0.0, 0.0, 0.0, 1);
335 viconutil_draw_lineloop_smooth(pts, 3);
337 glColor3f(1, 1, 0.0);
338 viconutil_draw_points(pts, 3, 1);
341 static void vicon_editmode_dehlt_draw(int x, int y, int w, int h, float UNUSED(alpha))
345 viconutil_set_point(pts[0], x+w/2, y+h-2);
346 viconutil_set_point(pts[1], x+3, y+4);
347 viconutil_set_point(pts[2], x+w-3, y+4);
349 glColor4f(0.0f, 0.0f, 0.0f, 1);
350 viconutil_draw_lineloop_smooth(pts, 3);
352 glColor3f(.9f, .9f, .9f);
353 viconutil_draw_points(pts, 3, 1);
356 static void vicon_disclosure_tri_right_draw(int x, int y, int w, int UNUSED(h), float alpha)
361 int d = w/3, d2 = w/5;
363 viconutil_set_point(pts[0], cx-d2, cy+d);
364 viconutil_set_point(pts[1], cx-d2, cy-d);
365 viconutil_set_point(pts[2], cx+d2, cy);
367 glShadeModel(GL_SMOOTH);
368 glBegin(GL_TRIANGLES);
369 glColor4f(0.8f, 0.8f, 0.8f, alpha);
372 glColor4f(0.3f, 0.3f, 0.3f, alpha);
375 glShadeModel(GL_FLAT);
377 glColor4f(0.0f, 0.0f, 0.0f, 1);
378 viconutil_draw_lineloop_smooth(pts, 3);
381 static void vicon_small_tri_right_draw(int x, int y, int w, int UNUSED(h), float alpha)
386 int d = w/5, d2 = w/7;
388 viconutil_set_point(pts[0], cx-d2, cy+d);
389 viconutil_set_point(pts[1], cx-d2, cy-d);
390 viconutil_set_point(pts[2], cx+d2, cy);
392 glColor4f(0.2f, 0.2f, 0.2f, alpha);
394 glShadeModel(GL_SMOOTH);
395 glBegin(GL_TRIANGLES);
400 glShadeModel(GL_FLAT);
403 static void vicon_disclosure_tri_down_draw(int x, int y, int w, int UNUSED(h), float alpha)
408 int d = w/3, d2 = w/5;
410 viconutil_set_point(pts[0], cx+d, cy+d2);
411 viconutil_set_point(pts[1], cx-d, cy+d2);
412 viconutil_set_point(pts[2], cx, cy-d2);
414 glShadeModel(GL_SMOOTH);
415 glBegin(GL_TRIANGLES);
416 glColor4f(0.8f, 0.8f, 0.8f, alpha);
419 glColor4f(0.3f, 0.3f, 0.3f, alpha);
422 glShadeModel(GL_FLAT);
424 glColor4f(0.0f, 0.0f, 0.0f, 1);
425 viconutil_draw_lineloop_smooth(pts, 3);
428 static void vicon_move_up_draw(int x, int y, int w, int h, float UNUSED(alpha))
432 glEnable(GL_LINE_SMOOTH);
434 glColor3f(0.0, 0.0, 0.0);
436 glBegin(GL_LINE_STRIP);
437 glVertex2i(x+w/2-d*2, y+h/2+d);
438 glVertex2i(x+w/2, y+h/2-d + 1);
439 glVertex2i(x+w/2+d*2, y+h/2+d);
443 glDisable(GL_LINE_SMOOTH);
446 static void vicon_move_down_draw(int x, int y, int w, int h, float UNUSED(alpha))
450 glEnable(GL_LINE_SMOOTH);
452 glColor3f(0.0, 0.0, 0.0);
454 glBegin(GL_LINE_STRIP);
455 glVertex2i(x+w/2-d*2, y+h/2+d);
456 glVertex2i(x+w/2, y+h/2-d - 1);
457 glVertex2i(x+w/2+d*2, y+h/2+d);
461 glDisable(GL_LINE_SMOOTH);
464 #ifndef WITH_HEADLESS
465 static void init_brush_icons(void)
468 #define INIT_BRUSH_ICON(icon_id, name) \
469 bbuf = IMB_ibImageFromMemory((unsigned char*)datatoc_ ##name## _png, \
470 datatoc_ ##name## _png_size, IB_rect); \
471 def_internal_icon(bbuf, icon_id, 0, 0, w, ICON_TYPE_BUFFER); \
473 // end INIT_BRUSH_ICON
478 INIT_BRUSH_ICON(ICON_BRUSH_ADD, add);
479 INIT_BRUSH_ICON(ICON_BRUSH_BLOB, blob);
480 INIT_BRUSH_ICON(ICON_BRUSH_BLUR, blur);
481 INIT_BRUSH_ICON(ICON_BRUSH_CLAY, clay);
482 INIT_BRUSH_ICON(ICON_BRUSH_CLONE, clone);
483 INIT_BRUSH_ICON(ICON_BRUSH_CREASE, crease);
484 INIT_BRUSH_ICON(ICON_BRUSH_DARKEN, darken);
485 INIT_BRUSH_ICON(ICON_BRUSH_SCULPT_DRAW, draw);
486 INIT_BRUSH_ICON(ICON_BRUSH_FILL, fill);
487 INIT_BRUSH_ICON(ICON_BRUSH_FLATTEN, flatten);
488 INIT_BRUSH_ICON(ICON_BRUSH_GRAB, grab);
489 INIT_BRUSH_ICON(ICON_BRUSH_INFLATE, inflate);
490 INIT_BRUSH_ICON(ICON_BRUSH_LAYER, layer);
491 INIT_BRUSH_ICON(ICON_BRUSH_LIGHTEN, lighten);
492 INIT_BRUSH_ICON(ICON_BRUSH_MIX, mix);
493 INIT_BRUSH_ICON(ICON_BRUSH_MULTIPLY, multiply);
494 INIT_BRUSH_ICON(ICON_BRUSH_NUDGE, nudge);
495 INIT_BRUSH_ICON(ICON_BRUSH_PINCH, pinch);
496 INIT_BRUSH_ICON(ICON_BRUSH_SCRAPE, scrape);
497 INIT_BRUSH_ICON(ICON_BRUSH_SMEAR, smear);
498 INIT_BRUSH_ICON(ICON_BRUSH_SMOOTH, smooth);
499 INIT_BRUSH_ICON(ICON_BRUSH_SNAKE_HOOK, snake_hook);
500 INIT_BRUSH_ICON(ICON_BRUSH_SOFTEN, soften);
501 INIT_BRUSH_ICON(ICON_BRUSH_SUBTRACT, subtract);
502 INIT_BRUSH_ICON(ICON_BRUSH_TEXDRAW, texdraw);
503 INIT_BRUSH_ICON(ICON_BRUSH_THUMB, thumb);
504 INIT_BRUSH_ICON(ICON_BRUSH_ROTATE, twist);
505 INIT_BRUSH_ICON(ICON_BRUSH_VERTEXDRAW, vertexdraw);
507 #undef INIT_BRUSH_ICON
510 static void init_internal_icons(void)
512 bTheme *btheme= U.themes.first;
515 char iconfilestr[FILE_MAXDIR+FILE_MAXFILE];
517 if ((btheme!=NULL) && btheme->tui.iconfile[0]) {
518 char *icondir= BLI_get_folder(BLENDER_DATAFILES, "icons");
520 BLI_join_dirfile(iconfilestr, sizeof(iconfilestr), icondir, btheme->tui.iconfile);
521 bbuf = IMB_loadiffname(iconfilestr, IB_rect); /* if the image is missing bbuf will just be NULL */
522 if(bbuf && (bbuf->x < ICON_IMAGE_W || bbuf->y < ICON_IMAGE_H)) {
523 printf("\n***WARNING***\nIcons file %s too small.\nUsing built-in Icons instead\n", iconfilestr);
529 printf("%s: 'icons' data path not found, continuing\n", __func__);
533 bbuf = IMB_ibImageFromMemory((unsigned char*)datatoc_blenderbuttons, datatoc_blenderbuttons_size, IB_rect);
536 /* free existing texture if any */
538 glDeleteTextures(1, &icongltex.id);
542 /* we only use a texture for cards with non-power of two */
543 if(GPU_non_power_of_two_support()) {
544 glGenTextures(1, &icongltex.id);
547 icongltex.w = bbuf->x;
548 icongltex.h = bbuf->y;
549 icongltex.invw = 1.0f/bbuf->x;
550 icongltex.invh = 1.0f/bbuf->y;
552 glBindTexture(GL_TEXTURE_2D, icongltex.id);
553 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bbuf->x, bbuf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, bbuf->rect);
554 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
555 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
556 glBindTexture(GL_TEXTURE_2D, 0);
558 if(glGetError() == GL_OUT_OF_MEMORY) {
559 glDeleteTextures(1, &icongltex.id);
567 icontype= ICON_TYPE_TEXTURE;
569 icontype= ICON_TYPE_BUFFER;
572 for (y=0; y<ICON_GRID_ROWS; y++) {
573 for (x=0; x<ICON_GRID_COLS; x++) {
574 def_internal_icon(bbuf, BIFICONID_FIRST + y*ICON_GRID_COLS + x,
575 x*(ICON_GRID_W+ICON_GRID_MARGIN)+ICON_GRID_MARGIN,
576 y*(ICON_GRID_H+ICON_GRID_MARGIN)+ICON_GRID_MARGIN, ICON_GRID_W,
582 def_internal_vicon(VICO_VIEW3D_VEC, vicon_view3d_draw);
583 def_internal_vicon(VICO_EDIT_VEC, vicon_edit_draw);
584 def_internal_vicon(VICO_EDITMODE_DEHLT, vicon_editmode_dehlt_draw);
585 def_internal_vicon(VICO_EDITMODE_HLT, vicon_editmode_hlt_draw);
586 def_internal_vicon(VICO_DISCLOSURE_TRI_RIGHT_VEC, vicon_disclosure_tri_right_draw);
587 def_internal_vicon(VICO_DISCLOSURE_TRI_DOWN_VEC, vicon_disclosure_tri_down_draw);
588 def_internal_vicon(VICO_MOVE_UP_VEC, vicon_move_up_draw);
589 def_internal_vicon(VICO_MOVE_DOWN_VEC, vicon_move_down_draw);
590 def_internal_vicon(VICO_X_VEC, vicon_x_draw);
591 def_internal_vicon(VICO_SMALL_TRI_RIGHT_VEC, vicon_small_tri_right_draw);
595 #endif // WITH_HEADLESS
597 static void init_iconfile_list(struct ListBase *list)
600 struct direntry *dir;
601 int restoredir = 1; /* restore to current directory */
602 int totfile, i, index=1;
604 char olddir[FILE_MAX];
606 list->first = list->last = NULL;
607 icondir = BLI_get_folder(BLENDER_DATAFILES, "icons");
612 /* since BLI_getdir changes the current working directory, restore it
613 back to old value afterwards */
614 if(!BLI_getwdN(olddir, sizeof(olddir)))
616 totfile = BLI_getdir(icondir, &dir);
617 if (restoredir && !chdir(olddir)) {} /* fix warning about checking return value */
619 for(i=0; i<totfile; i++) {
620 if( (dir[i].type & S_IFREG) ) {
621 char *filename = dir[i].relname;
623 if(BLI_testextensie(filename, ".png")) {
624 /* loading all icons on file start is overkill & slows startup
625 * its possible they change size after blender load anyway. */
628 char iconfilestr[FILE_MAX+16]; /* allow 256 chars for file+dir */
630 /* check to see if the image is the right size, continue if not */
631 /* copying strings here should go ok, assuming that we never get back
632 a complete path to file longer than 256 chars */
633 BLI_join_dirfile(iconfilestr, sizeof(iconfilestr), icondir, filename);
634 bbuf= IMB_loadiffname(iconfilestr, IB_rect);
645 /* bad size or failed to load */
646 if ((ifilex != ICON_IMAGE_W) || (ifiley != ICON_IMAGE_H)) {
647 printf("icon '%s' is wrong size %dx%d\n", iconfilestr, ifilex, ifiley);
652 /* found a potential icon file, so make an entry for it in the cache list */
653 ifile = MEM_callocN(sizeof(IconFile), "IconFile");
655 BLI_strncpy(ifile->filename, filename, sizeof(ifile->filename));
656 ifile->index = index;
658 BLI_addtail(list, ifile);
665 /* free temporary direntry structure that's been created by BLI_getdir() */
669 MEM_freeN(dir[i].relname);
670 MEM_freeN(dir[i].path);
671 if (dir[i].string) MEM_freeN(dir[i].string);
677 static void free_iconfile_list(struct ListBase *list)
679 IconFile *ifile=NULL, *next_ifile=NULL;
681 for(ifile=list->first; ifile; ifile=next_ifile) {
682 next_ifile = ifile->next;
683 BLI_freelinkN(list, ifile);
687 int UI_iconfile_get_index(const char *filename)
690 ListBase *list=&(iconfilelist);
692 for(ifile=list->first; ifile; ifile=ifile->next) {
693 if (BLI_path_cmp(filename, ifile->filename) == 0) {
701 ListBase *UI_iconfile_list(void)
703 ListBase *list=&(iconfilelist);
709 void UI_icons_free(void)
711 #ifndef WITH_HEADLESS
713 glDeleteTextures(1, &icongltex.id);
717 free_iconfile_list(&iconfilelist);
722 void UI_icons_free_drawinfo(void *drawinfo)
724 DrawInfo *di = drawinfo;
727 if(di->type == ICON_TYPE_BUFFER) {
728 if(di->data.buffer.image) {
729 MEM_freeN(di->data.buffer.image->rect);
730 MEM_freeN(di->data.buffer.image);
738 static DrawInfo *icon_create_drawinfo(void)
742 di = MEM_callocN(sizeof(DrawInfo), "di_icon");
743 di->type= ICON_TYPE_PREVIEW;
748 /* note!, returns unscaled by DPI, may need to multiply result by UI_DPI_ICON_FAC */
749 int UI_icon_get_width(int icon_id)
754 icon = BKE_icon_get(icon_id);
758 printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
762 di = (DrawInfo *)icon->drawinfo;
764 di = icon_create_drawinfo();
769 return ICON_DEFAULT_WIDTH;
774 int UI_icon_get_height(int icon_id)
779 icon = BKE_icon_get(icon_id);
783 printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
787 di = (DrawInfo*)icon->drawinfo;
790 di = icon_create_drawinfo();
795 return ICON_DEFAULT_HEIGHT;
800 void UI_icons_init(int first_dyn_id)
805 init_iconfile_list(&iconfilelist);
806 BKE_icons_init(first_dyn_id);
807 init_internal_icons();
812 /* Render size for preview images and icons
814 static int preview_render_size(enum eIconSizes size)
817 case ICON_SIZE_ICON: return 32;
818 case ICON_SIZE_PREVIEW: return PREVIEW_DEFAULT_HEIGHT;
823 /* Create rect for the icon
825 static void icon_create_rect(struct PreviewImage* prv_img, enum eIconSizes size)
827 unsigned int render_size = preview_render_size(size);
831 printf("%s, error: requested preview image does not exist", __func__);
833 if (!prv_img->rect[size]) {
834 prv_img->w[size] = render_size;
835 prv_img->h[size] = render_size;
836 prv_img->changed[size] = 1;
837 prv_img->changed_timestamp[size] = 0;
838 prv_img->rect[size] = MEM_callocN(render_size*render_size*sizeof(unsigned int), "prv_rect");
842 /* only called when icon has changed */
843 /* only call with valid pointer from UI_icon_draw */
844 static void icon_set_image(bContext *C, ID *id, PreviewImage* prv_img, enum eIconSizes size)
848 printf("%s: no preview image for this ID: %s\n", __func__, id->name);
852 icon_create_rect(prv_img, size);
854 ED_preview_icon_job(C, prv_img, id, prv_img->rect[size],
855 prv_img->w[size], prv_img->h[size]);
858 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, float *rgb, short is_preview)
863 if(w<=0 || h<=0 || w>2000 || h>2000) {
864 printf("%s: icons are %i x %i pixels?\n", __func__, w, h);
865 BLI_assert(!"invalid icon size");
871 glPixelTransferf(GL_ALPHA_SCALE, alpha);
874 glPixelTransferf(GL_RED_SCALE, rgb[0]);
875 glPixelTransferf(GL_GREEN_SCALE, rgb[1]);
876 glPixelTransferf(GL_BLUE_SCALE, rgb[2]);
879 /* rect contains image in 'rendersize', we only scale if needed */
881 /* first allocate imbuf for scaling and copy preview into it */
882 ima = IMB_allocImBuf(rw, rh, 32, IB_rect);
883 memcpy(ima->rect, rect, rw*rh*sizeof(unsigned int));
884 IMB_scaleImBuf(ima, w, h); /* scale it */
890 glaDrawPixelsSafe(x, y, w, h, w, GL_RGBA, GL_UNSIGNED_BYTE, rect);
894 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, rect);
902 glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
905 glPixelTransferf(GL_RED_SCALE, 1.0f);
906 glPixelTransferf(GL_GREEN_SCALE, 1.0f);
907 glPixelTransferf(GL_BLUE_SCALE, 1.0f);
911 static void icon_draw_texture(float x, float y, float w, float h, int ix, int iy, int UNUSED(iw), int ih, float alpha, float *rgb)
913 float x1, x2, y1, y2;
915 if(rgb) glColor4f(rgb[0], rgb[1], rgb[2], alpha);
916 else glColor4f(1.0f, 1.0f, 1.0f, alpha);
918 x1= ix*icongltex.invw;
919 x2= (ix + ih)*icongltex.invw;
920 y1= iy*icongltex.invh;
921 y2= (iy + ih)*icongltex.invh;
923 glEnable(GL_TEXTURE_2D);
924 glBindTexture(GL_TEXTURE_2D, icongltex.id);
927 glTexCoord2f(x1, y1);
930 glTexCoord2f(x2, y1);
933 glTexCoord2f(x2, y2);
934 glVertex2f(x+w, y+h);
936 glTexCoord2f(x1, y2);
940 glBindTexture(GL_TEXTURE_2D, 0);
941 glDisable(GL_TEXTURE_2D);
944 /* Drawing size for preview images */
945 static int get_draw_size(enum eIconSizes size)
948 case ICON_SIZE_ICON: return ICON_DEFAULT_HEIGHT;
949 case ICON_SIZE_PREVIEW: return PREVIEW_DEFAULT_HEIGHT;
954 static void icon_draw_size(float x, float y, int icon_id, float aspect, float alpha, float *rgb, enum eIconSizes size, int draw_size, int UNUSED(nocreate), short is_preview)
959 float fdraw_size= is_preview ? draw_size : (draw_size * UI_DPI_ICON_FAC);
962 icon = BKE_icon_get(icon_id);
967 printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
971 di = (DrawInfo*)icon->drawinfo;
974 di = icon_create_drawinfo();
977 icon->drawinfo_free = UI_icons_free_drawinfo;
980 /* scale width and height according to aspect */
981 w = (int)(fdraw_size/aspect + 0.5f);
982 h = (int)(fdraw_size/aspect + 0.5f);
984 if(di->type == ICON_TYPE_VECTOR) {
985 /* vector icons use the uiBlock transformation, they are not drawn
986 with untransformed coordinates like the other icons */
987 di->data.vector.func((int)x, (int)y, ICON_DEFAULT_HEIGHT, ICON_DEFAULT_HEIGHT, 1.0f);
989 else if(di->type == ICON_TYPE_TEXTURE) {
990 icon_draw_texture(x, y, (float)w, (float)h, di->data.texture.x, di->data.texture.y,
991 di->data.texture.w, di->data.texture.h, alpha, rgb);
993 else if(di->type == ICON_TYPE_BUFFER) {
994 /* it is a builtin icon */
995 iimg= di->data.buffer.image;
997 if(!iimg->rect) return; /* something has gone wrong! */
999 icon_draw_rect(x, y, w, h, aspect, iimg->w, iimg->h, iimg->rect, alpha, rgb, is_preview);
1001 else if(di->type == ICON_TYPE_PREVIEW) {
1002 PreviewImage* pi = BKE_previewimg_get((ID*)icon->obj);
1005 /* no create icon on this level in code */
1006 if(!pi->rect[size]) return; /* something has gone wrong! */
1008 /* preview images use premul alpha ... */
1009 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1010 icon_draw_rect(x, y, w, h, aspect, pi->w[size], pi->h[size], pi->rect[size], 1.0f, NULL, is_preview);
1011 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1016 static void ui_id_icon_render(bContext *C, ID *id, int big)
1018 PreviewImage *pi = BKE_previewimg_get(id);
1021 if ((pi->changed[0] ||!pi->rect[0])) /* changed only ever set by dynamic icons */
1023 /* create the rect if necessary */
1025 icon_set_image(C, id, pi, ICON_SIZE_ICON); /* icon size */
1027 icon_set_image(C, id, pi, ICON_SIZE_PREVIEW); /* bigger preview size */
1034 static void ui_id_brush_render(bContext *C, ID *id)
1036 PreviewImage *pi = BKE_previewimg_get(id);
1042 for(i = 0; i < NUM_ICON_SIZES; i++) {
1043 /* check if rect needs to be created; changed
1044 only set by dynamic icons */
1045 if((pi->changed[i] || !pi->rect[i])) {
1046 icon_set_image(C, id, pi, i);
1053 static int ui_id_brush_get_icon(bContext *C, ID *id)
1055 Brush *br = (Brush*)id;
1057 if(br->flag & BRUSH_CUSTOM_ICON) {
1059 ui_id_brush_render(C, id);
1062 Object *ob = CTX_data_active_object(C);
1064 EnumPropertyItem *items = NULL;
1067 /* XXX: this is not nice, should probably make brushes
1068 be strictly in one paint mode only to avoid
1069 checking various context stuff here */
1071 if(CTX_wm_view3d(C) && ob) {
1072 if(ob->mode & OB_MODE_SCULPT)
1073 mode = OB_MODE_SCULPT;
1074 else if(ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT))
1075 mode = OB_MODE_VERTEX_PAINT;
1076 else if(ob->mode & OB_MODE_TEXTURE_PAINT)
1077 mode = OB_MODE_TEXTURE_PAINT;
1079 else if((sima = CTX_wm_space_image(C)) &&
1080 (sima->flag & SI_DRAWTOOL)) {
1081 mode = OB_MODE_TEXTURE_PAINT;
1084 /* reset the icon */
1085 if(mode == OB_MODE_SCULPT) {
1086 items = brush_sculpt_tool_items;
1087 tool = br->sculpt_tool;
1089 else if(mode == OB_MODE_VERTEX_PAINT) {
1090 items = brush_vertex_tool_items;
1091 tool = br->vertexpaint_tool;
1093 else if(mode == OB_MODE_TEXTURE_PAINT) {
1094 items = brush_image_tool_items;
1095 tool = br->imagepaint_tool;
1098 if(!items || !RNA_enum_icon_from_value(items, tool, &id->icon_id))
1105 int ui_id_icon_get(bContext *C, ID *id, int big)
1110 switch(GS(id->name)) {
1112 iconid= ui_id_brush_get_icon(C, id);
1114 case ID_MA: /* fall through */
1115 case ID_TE: /* fall through */
1116 case ID_IM: /* fall through */
1117 case ID_WO: /* fall through */
1118 case ID_LA: /* fall through */
1119 iconid= BKE_icon_getid(id);
1120 /* checks if not exists, or changed */
1121 ui_id_icon_render(C, id, big);
1130 static void icon_draw_at_size(float x, float y, int icon_id, float aspect, float alpha, enum eIconSizes size, int nocreate)
1132 int draw_size = get_draw_size(size);
1133 icon_draw_size(x, y, icon_id, aspect, alpha, NULL, size, draw_size, nocreate, FALSE);
1136 void UI_icon_draw_aspect(float x, float y, int icon_id, float aspect, float alpha)
1138 icon_draw_at_size(x, y, icon_id, aspect, alpha, ICON_SIZE_ICON, 0);
1141 void UI_icon_draw_aspect_color(float x, float y, int icon_id, float aspect, float *rgb)
1143 int draw_size = get_draw_size(ICON_SIZE_ICON);
1144 icon_draw_size(x, y, icon_id, aspect, 1.0f, rgb, ICON_SIZE_ICON, draw_size, FALSE, FALSE);
1147 void UI_icon_draw(float x, float y, int icon_id)
1149 UI_icon_draw_aspect(x, y, icon_id, 1.0f, 1.0f);
1152 void UI_icon_draw_size(float x, float y, int size, int icon_id, float alpha)
1154 icon_draw_size(x, y, icon_id, 1.0f, alpha, NULL, ICON_SIZE_ICON, size, TRUE, FALSE);
1157 void UI_icon_draw_preview(float x, float y, int icon_id)
1159 icon_draw_at_size(x, y, icon_id, 1.0f, 1.0f, ICON_SIZE_PREVIEW, 0);
1162 void UI_icon_draw_preview_aspect(float x, float y, int icon_id, float aspect)
1164 icon_draw_at_size(x, y, icon_id, aspect, 1.0f, ICON_SIZE_PREVIEW, 0);
1167 void UI_icon_draw_preview_aspect_size(float x, float y, int icon_id, float aspect, int size)
1169 icon_draw_size(x, y, icon_id, aspect, 1.0f, NULL, ICON_SIZE_PREVIEW, size, FALSE, TRUE);