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_utildefines.h"
52 #include "DNA_brush_types.h"
53 #include "DNA_object_types.h"
54 #include "DNA_screen_types.h"
55 #include "DNA_space_types.h"
57 #include "RNA_access.h"
58 #include "RNA_enum_types.h"
60 #include "BKE_context.h"
61 #include "BKE_global.h"
62 #include "BKE_icons.h"
63 #include "BKE_utildefines.h"
65 #include "IMB_imbuf.h"
66 #include "IMB_imbuf_types.h"
69 #include "BIF_glutil.h"
71 #include "ED_datafiles.h"
72 #include "ED_render.h"
74 #include "UI_interface.h"
75 #include "UI_interface_icons.h"
77 #include "interface_intern.h"
80 #define ICON_IMAGE_W 600
81 #define ICON_IMAGE_H 640
83 #define ICON_GRID_COLS 26
84 #define ICON_GRID_ROWS 30
86 #define ICON_GRID_MARGIN 5
87 #define ICON_GRID_W 16
88 #define ICON_GRID_H 16
90 typedef struct IconImage {
96 typedef void (*VectorDrawFunc)(int x, int y, int w, int h, float alpha);
98 #define ICON_TYPE_PREVIEW 0
99 #define ICON_TYPE_TEXTURE 1
100 #define ICON_TYPE_BUFFER 2
101 #define ICON_TYPE_VECTOR 3
103 typedef struct DrawInfo {
107 /* type specific data */
120 typedef struct IconTexture {
128 /* ******************* STATIC LOCAL VARS ******************* */
129 /* static here to cache results of icon directory scan, so it's not
130 * scanning the filesystem each time the menu is drawn */
131 static struct ListBase iconfilelist = {NULL, NULL};
132 static IconTexture icongltex = {0, 0, 0, 0.0f, 0.0f};
134 /* **************************************************** */
136 static void def_internal_icon(ImBuf *bbuf, int icon_id, int xofs, int yofs, int size, int type)
138 Icon *new_icon = NULL;
139 IconImage *iimg = NULL;
144 new_icon = MEM_callocN(sizeof(Icon), "texicon");
146 new_icon->obj = NULL; /* icon is not for library object */
149 di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
152 if(type == ICON_TYPE_TEXTURE) {
153 di->data.texture.x= xofs;
154 di->data.texture.y= yofs;
155 di->data.texture.w= size;
156 di->data.texture.h= size;
158 else if(type == ICON_TYPE_BUFFER) {
159 iimg = MEM_mallocN(sizeof(IconImage), "icon_img");
160 iimg->rect = MEM_mallocN(size*size*sizeof(unsigned int), "icon_rect");
164 /* Here we store the rect in the icon - same as before */
166 for (y=0; y<size; y++) {
167 memcpy(&iimg->rect[y*size], &bbuf->rect[(y+yofs)*imgsize+xofs], size*sizeof(int));
170 di->data.buffer.image = iimg;
173 new_icon->drawinfo_free = UI_icons_free_drawinfo;
174 new_icon->drawinfo = di;
176 BKE_icon_set(icon_id, new_icon);
179 static void def_internal_vicon( int icon_id, VectorDrawFunc drawFunc)
181 Icon *new_icon = NULL;
184 new_icon = MEM_callocN(sizeof(Icon), "texicon");
186 new_icon->obj = NULL; /* icon is not for library object */
189 di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
190 di->type= ICON_TYPE_VECTOR;
191 di->data.vector.func =drawFunc;
193 new_icon->drawinfo_free = NULL;
194 new_icon->drawinfo = di;
196 BKE_icon_set(icon_id, new_icon);
199 /* Vector Icon Drawing Routines */
203 static void viconutil_set_point(GLint pt[2], int x, int y)
209 static void viconutil_draw_tri(GLint (*pts)[2])
211 glBegin(GL_TRIANGLES);
218 static void viconutil_draw_lineloop(GLint (*pts)[2], int numPoints)
222 glBegin(GL_LINE_LOOP);
223 for (i=0; i<numPoints; i++) {
229 static void viconutil_draw_lineloop_smooth(GLint (*pts)[2], int numPoints)
231 glEnable(GL_LINE_SMOOTH);
232 viconutil_draw_lineloop(pts, numPoints);
233 glDisable(GL_LINE_SMOOTH);
236 static void viconutil_draw_points(GLint (*pts)[2], int numPoints, int pointSize)
241 for (i=0; i<numPoints; i++) {
242 int x = pts[i][0], y = pts[i][1];
244 glVertex2i(x-pointSize,y-pointSize);
245 glVertex2i(x+pointSize,y-pointSize);
246 glVertex2i(x+pointSize,y+pointSize);
247 glVertex2i(x-pointSize,y+pointSize);
252 /* Drawing functions */
254 static void vicon_x_draw(int x, int y, int w, int h, float alpha)
261 glEnable( GL_LINE_SMOOTH );
265 glColor4f(0.0, 0.0, 0.0, alpha);
275 glDisable( GL_LINE_SMOOTH );
278 static void vicon_view3d_draw(int x, int y, int w, int h, float alpha)
282 int d = MAX2(2, h/3);
284 glColor4f(0.5, 0.5, 0.5, alpha);
286 glVertex2i(x , cy-d);
287 glVertex2i(x+w, cy-d);
288 glVertex2i(x , cy+d);
289 glVertex2i(x+w, cy+d);
291 glVertex2i(cx-d, y );
292 glVertex2i(cx-d, y+h);
293 glVertex2i(cx+d, y );
294 glVertex2i(cx+d, y+h);
297 glColor4f(0.0, 0.0, 0.0, alpha);
306 static void vicon_edit_draw(int x, int y, int w, int h, float alpha)
310 viconutil_set_point(pts[0], x+3 , y+3 );
311 viconutil_set_point(pts[1], x+w-3, y+3 );
312 viconutil_set_point(pts[2], x+w-3, y+h-3);
313 viconutil_set_point(pts[3], x+3 , y+h-3);
315 glColor4f(0.0, 0.0, 0.0, alpha);
316 viconutil_draw_lineloop(pts, 4);
318 glColor3f(1, 1, 0.0);
319 viconutil_draw_points(pts, 4, 1);
322 static void vicon_editmode_hlt_draw(int x, int y, int w, int h, float alpha)
326 viconutil_set_point(pts[0], x+w/2, y+h-2);
327 viconutil_set_point(pts[1], x+3, y+4);
328 viconutil_set_point(pts[2], x+w-3, y+4);
330 glColor4f(0.5, 0.5, 0.5, alpha);
331 viconutil_draw_tri(pts);
333 glColor4f(0.0, 0.0, 0.0, 1);
334 viconutil_draw_lineloop_smooth(pts, 3);
336 glColor3f(1, 1, 0.0);
337 viconutil_draw_points(pts, 3, 1);
340 static void vicon_editmode_dehlt_draw(int x, int y, int w, int h, float UNUSED(alpha))
344 viconutil_set_point(pts[0], x+w/2, y+h-2);
345 viconutil_set_point(pts[1], x+3, y+4);
346 viconutil_set_point(pts[2], x+w-3, y+4);
348 glColor4f(0.0f, 0.0f, 0.0f, 1);
349 viconutil_draw_lineloop_smooth(pts, 3);
351 glColor3f(.9f, .9f, .9f);
352 viconutil_draw_points(pts, 3, 1);
355 static void vicon_disclosure_tri_right_draw(int x, int y, int w, int UNUSED(h), float alpha)
360 int d = w/3, d2 = w/5;
362 viconutil_set_point(pts[0], cx-d2, cy+d);
363 viconutil_set_point(pts[1], cx-d2, cy-d);
364 viconutil_set_point(pts[2], cx+d2, cy);
366 glShadeModel(GL_SMOOTH);
367 glBegin(GL_TRIANGLES);
368 glColor4f(0.8f, 0.8f, 0.8f, alpha);
371 glColor4f(0.3f, 0.3f, 0.3f, alpha);
374 glShadeModel(GL_FLAT);
376 glColor4f(0.0f, 0.0f, 0.0f, 1);
377 viconutil_draw_lineloop_smooth(pts, 3);
380 static void vicon_small_tri_right_draw(int x, int y, int w, int UNUSED(h), float alpha)
385 int d = w/5, d2 = w/7;
387 viconutil_set_point(pts[0], cx-d2, cy+d);
388 viconutil_set_point(pts[1], cx-d2, cy-d);
389 viconutil_set_point(pts[2], cx+d2, cy);
391 glColor4f(0.2f, 0.2f, 0.2f, alpha);
393 glShadeModel(GL_SMOOTH);
394 glBegin(GL_TRIANGLES);
399 glShadeModel(GL_FLAT);
402 static void vicon_disclosure_tri_down_draw(int x, int y, int w, int UNUSED(h), float alpha)
407 int d = w/3, d2 = w/5;
409 viconutil_set_point(pts[0], cx+d, cy+d2);
410 viconutil_set_point(pts[1], cx-d, cy+d2);
411 viconutil_set_point(pts[2], cx, cy-d2);
413 glShadeModel(GL_SMOOTH);
414 glBegin(GL_TRIANGLES);
415 glColor4f(0.8f, 0.8f, 0.8f, alpha);
418 glColor4f(0.3f, 0.3f, 0.3f, alpha);
421 glShadeModel(GL_FLAT);
423 glColor4f(0.0f, 0.0f, 0.0f, 1);
424 viconutil_draw_lineloop_smooth(pts, 3);
427 static void vicon_move_up_draw(int x, int y, int w, int h, float UNUSED(alpha))
431 glEnable(GL_LINE_SMOOTH);
433 glColor3f(0.0, 0.0, 0.0);
435 glBegin(GL_LINE_STRIP);
436 glVertex2i(x+w/2-d*2, y+h/2+d);
437 glVertex2i(x+w/2, y+h/2-d + 1);
438 glVertex2i(x+w/2+d*2, y+h/2+d);
442 glDisable(GL_LINE_SMOOTH);
445 static void vicon_move_down_draw(int x, int y, int w, int h, float UNUSED(alpha))
449 glEnable(GL_LINE_SMOOTH);
451 glColor3f(0.0, 0.0, 0.0);
453 glBegin(GL_LINE_STRIP);
454 glVertex2i(x+w/2-d*2, y+h/2+d);
455 glVertex2i(x+w/2, y+h/2-d - 1);
456 glVertex2i(x+w/2+d*2, y+h/2+d);
460 glDisable(GL_LINE_SMOOTH);
463 #ifndef WITH_HEADLESS
464 static void init_brush_icons(void)
467 #define INIT_BRUSH_ICON(icon_id, name) \
468 bbuf = IMB_ibImageFromMemory((unsigned char*)datatoc_ ##name## _png, \
469 datatoc_ ##name## _png_size, IB_rect); \
470 def_internal_icon(bbuf, icon_id, 0, 0, w, ICON_TYPE_BUFFER); \
472 // end INIT_BRUSH_ICON
477 INIT_BRUSH_ICON(ICON_BRUSH_ADD, add);
478 INIT_BRUSH_ICON(ICON_BRUSH_BLOB, blob);
479 INIT_BRUSH_ICON(ICON_BRUSH_BLUR, blur);
480 INIT_BRUSH_ICON(ICON_BRUSH_CLAY, clay);
481 INIT_BRUSH_ICON(ICON_BRUSH_CLONE, clone);
482 INIT_BRUSH_ICON(ICON_BRUSH_CREASE, crease);
483 INIT_BRUSH_ICON(ICON_BRUSH_DARKEN, darken);
484 INIT_BRUSH_ICON(ICON_BRUSH_SCULPT_DRAW, draw);
485 INIT_BRUSH_ICON(ICON_BRUSH_FILL, fill);
486 INIT_BRUSH_ICON(ICON_BRUSH_FLATTEN, flatten);
487 INIT_BRUSH_ICON(ICON_BRUSH_GRAB, grab);
488 INIT_BRUSH_ICON(ICON_BRUSH_INFLATE, inflate);
489 INIT_BRUSH_ICON(ICON_BRUSH_LAYER, layer);
490 INIT_BRUSH_ICON(ICON_BRUSH_LIGHTEN, lighten);
491 INIT_BRUSH_ICON(ICON_BRUSH_MIX, mix);
492 INIT_BRUSH_ICON(ICON_BRUSH_MULTIPLY, multiply);
493 INIT_BRUSH_ICON(ICON_BRUSH_NUDGE, nudge);
494 INIT_BRUSH_ICON(ICON_BRUSH_PINCH, pinch);
495 INIT_BRUSH_ICON(ICON_BRUSH_SCRAPE, scrape);
496 INIT_BRUSH_ICON(ICON_BRUSH_SMEAR, smear);
497 INIT_BRUSH_ICON(ICON_BRUSH_SMOOTH, smooth);
498 INIT_BRUSH_ICON(ICON_BRUSH_SNAKE_HOOK, snake_hook);
499 INIT_BRUSH_ICON(ICON_BRUSH_SOFTEN, soften);
500 INIT_BRUSH_ICON(ICON_BRUSH_SUBTRACT, subtract);
501 INIT_BRUSH_ICON(ICON_BRUSH_TEXDRAW, texdraw);
502 INIT_BRUSH_ICON(ICON_BRUSH_THUMB, thumb);
503 INIT_BRUSH_ICON(ICON_BRUSH_ROTATE, twist);
504 INIT_BRUSH_ICON(ICON_BRUSH_VERTEXDRAW, vertexdraw);
506 #undef INIT_BRUSH_ICON
509 static void init_internal_icons(void)
511 bTheme *btheme= U.themes.first;
514 char iconfilestr[FILE_MAXDIR+FILE_MAXFILE];
516 if ((btheme!=NULL) && btheme->tui.iconfile[0]) {
517 char *icondir= BLI_get_folder(BLENDER_DATAFILES, "icons");
519 BLI_join_dirfile(iconfilestr, sizeof(iconfilestr), icondir, btheme->tui.iconfile);
520 bbuf = IMB_loadiffname(iconfilestr, IB_rect); /* if the image is missing bbuf will just be NULL */
521 if(bbuf && (bbuf->x < ICON_IMAGE_W || bbuf->y < ICON_IMAGE_H)) {
522 printf("\n***WARNING***\nIcons file %s too small.\nUsing built-in Icons instead\n", iconfilestr);
528 printf("%s: 'icons' data path not found, continuing\n", __func__);
532 bbuf = IMB_ibImageFromMemory((unsigned char*)datatoc_blenderbuttons, datatoc_blenderbuttons_size, IB_rect);
535 /* free existing texture if any */
537 glDeleteTextures(1, &icongltex.id);
541 /* we only use a texture for cards with non-power of two */
542 if(GPU_non_power_of_two_support()) {
543 glGenTextures(1, &icongltex.id);
546 icongltex.w = bbuf->x;
547 icongltex.h = bbuf->y;
548 icongltex.invw = 1.0f/bbuf->x;
549 icongltex.invh = 1.0f/bbuf->y;
551 glBindTexture(GL_TEXTURE_2D, icongltex.id);
552 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bbuf->x, bbuf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, bbuf->rect);
553 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
554 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
555 glBindTexture(GL_TEXTURE_2D, 0);
557 if(glGetError() == GL_OUT_OF_MEMORY) {
558 glDeleteTextures(1, &icongltex.id);
566 icontype= ICON_TYPE_TEXTURE;
568 icontype= ICON_TYPE_BUFFER;
571 for (y=0; y<ICON_GRID_ROWS; y++) {
572 for (x=0; x<ICON_GRID_COLS; x++) {
573 def_internal_icon(bbuf, BIFICONID_FIRST + y*ICON_GRID_COLS + x,
574 x*(ICON_GRID_W+ICON_GRID_MARGIN)+ICON_GRID_MARGIN,
575 y*(ICON_GRID_H+ICON_GRID_MARGIN)+ICON_GRID_MARGIN, ICON_GRID_W,
581 def_internal_vicon(VICO_VIEW3D_VEC, vicon_view3d_draw);
582 def_internal_vicon(VICO_EDIT_VEC, vicon_edit_draw);
583 def_internal_vicon(VICO_EDITMODE_DEHLT, vicon_editmode_dehlt_draw);
584 def_internal_vicon(VICO_EDITMODE_HLT, vicon_editmode_hlt_draw);
585 def_internal_vicon(VICO_DISCLOSURE_TRI_RIGHT_VEC, vicon_disclosure_tri_right_draw);
586 def_internal_vicon(VICO_DISCLOSURE_TRI_DOWN_VEC, vicon_disclosure_tri_down_draw);
587 def_internal_vicon(VICO_MOVE_UP_VEC, vicon_move_up_draw);
588 def_internal_vicon(VICO_MOVE_DOWN_VEC, vicon_move_down_draw);
589 def_internal_vicon(VICO_X_VEC, vicon_x_draw);
590 def_internal_vicon(VICO_SMALL_TRI_RIGHT_VEC, vicon_small_tri_right_draw);
594 #endif // WITH_HEADLESS
596 static void init_iconfile_list(struct ListBase *list)
599 struct direntry *dir;
600 int restoredir = 1; /* restore to current directory */
601 int totfile, i, index=1;
603 char olddir[FILE_MAX];
605 list->first = list->last = NULL;
606 icondir = BLI_get_folder(BLENDER_DATAFILES, "icons");
611 /* since BLI_dir_contents changes the current working directory, restore it
612 back to old value afterwards */
613 if(!BLI_current_working_dir(olddir, sizeof(olddir)))
615 totfile = BLI_dir_contents(icondir, &dir);
616 if (restoredir && !chdir(olddir)) {} /* fix warning about checking return value */
618 for(i=0; i<totfile; i++) {
619 if( (dir[i].type & S_IFREG) ) {
620 char *filename = dir[i].relname;
622 if(BLI_testextensie(filename, ".png")) {
623 /* loading all icons on file start is overkill & slows startup
624 * its possible they change size after blender load anyway. */
627 char iconfilestr[FILE_MAX+16]; /* allow 256 chars for file+dir */
629 /* check to see if the image is the right size, continue if not */
630 /* copying strings here should go ok, assuming that we never get back
631 a complete path to file longer than 256 chars */
632 BLI_join_dirfile(iconfilestr, sizeof(iconfilestr), icondir, filename);
633 bbuf= IMB_loadiffname(iconfilestr, IB_rect);
644 /* bad size or failed to load */
645 if ((ifilex != ICON_IMAGE_W) || (ifiley != ICON_IMAGE_H)) {
646 printf("icon '%s' is wrong size %dx%d\n", iconfilestr, ifilex, ifiley);
651 /* found a potential icon file, so make an entry for it in the cache list */
652 ifile = MEM_callocN(sizeof(IconFile), "IconFile");
654 BLI_strncpy(ifile->filename, filename, sizeof(ifile->filename));
655 ifile->index = index;
657 BLI_addtail(list, ifile);
664 /* free temporary direntry structure that's been created by BLI_dir_contents() */
668 MEM_freeN(dir[i].relname);
669 MEM_freeN(dir[i].path);
670 if (dir[i].string) MEM_freeN(dir[i].string);
676 static void free_iconfile_list(struct ListBase *list)
678 IconFile *ifile=NULL, *next_ifile=NULL;
680 for(ifile=list->first; ifile; ifile=next_ifile) {
681 next_ifile = ifile->next;
682 BLI_freelinkN(list, ifile);
686 int UI_iconfile_get_index(const char *filename)
689 ListBase *list=&(iconfilelist);
691 for(ifile=list->first; ifile; ifile=ifile->next) {
692 if (BLI_path_cmp(filename, ifile->filename) == 0) {
700 ListBase *UI_iconfile_list(void)
702 ListBase *list=&(iconfilelist);
708 void UI_icons_free(void)
710 #ifndef WITH_HEADLESS
712 glDeleteTextures(1, &icongltex.id);
716 free_iconfile_list(&iconfilelist);
721 void UI_icons_free_drawinfo(void *drawinfo)
723 DrawInfo *di = drawinfo;
726 if(di->type == ICON_TYPE_BUFFER) {
727 if(di->data.buffer.image) {
728 MEM_freeN(di->data.buffer.image->rect);
729 MEM_freeN(di->data.buffer.image);
737 static DrawInfo *icon_create_drawinfo(void)
741 di = MEM_callocN(sizeof(DrawInfo), "di_icon");
742 di->type= ICON_TYPE_PREVIEW;
747 /* note!, returns unscaled by DPI, may need to multiply result by UI_DPI_ICON_FAC */
748 int UI_icon_get_width(int icon_id)
753 icon = BKE_icon_get(icon_id);
757 printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
761 di = (DrawInfo *)icon->drawinfo;
763 di = icon_create_drawinfo();
768 return ICON_DEFAULT_WIDTH;
773 int UI_icon_get_height(int icon_id)
778 icon = BKE_icon_get(icon_id);
782 printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
786 di = (DrawInfo*)icon->drawinfo;
789 di = icon_create_drawinfo();
794 return ICON_DEFAULT_HEIGHT;
799 void UI_icons_init(int first_dyn_id)
804 init_iconfile_list(&iconfilelist);
805 BKE_icons_init(first_dyn_id);
806 init_internal_icons();
811 /* Render size for preview images and icons
813 static int preview_render_size(enum eIconSizes size)
816 case ICON_SIZE_ICON: return 32;
817 case ICON_SIZE_PREVIEW: return PREVIEW_DEFAULT_HEIGHT;
822 /* Create rect for the icon
824 static void icon_create_rect(struct PreviewImage* prv_img, enum eIconSizes size)
826 unsigned int render_size = preview_render_size(size);
830 printf("%s, error: requested preview image does not exist", __func__);
832 if (!prv_img->rect[size]) {
833 prv_img->w[size] = render_size;
834 prv_img->h[size] = render_size;
835 prv_img->changed[size] = 1;
836 prv_img->changed_timestamp[size] = 0;
837 prv_img->rect[size] = MEM_callocN(render_size*render_size*sizeof(unsigned int), "prv_rect");
841 /* only called when icon has changed */
842 /* only call with valid pointer from UI_icon_draw */
843 static void icon_set_image(bContext *C, ID *id, PreviewImage* prv_img, enum eIconSizes size)
847 printf("%s: no preview image for this ID: %s\n", __func__, id->name);
851 icon_create_rect(prv_img, size);
853 ED_preview_icon_job(C, prv_img, id, prv_img->rect[size],
854 prv_img->w[size], prv_img->h[size]);
857 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)
862 if(w<=0 || h<=0 || w>2000 || h>2000) {
863 printf("%s: icons are %i x %i pixels?\n", __func__, w, h);
864 BLI_assert(!"invalid icon size");
870 glPixelTransferf(GL_ALPHA_SCALE, alpha);
873 glPixelTransferf(GL_RED_SCALE, rgb[0]);
874 glPixelTransferf(GL_GREEN_SCALE, rgb[1]);
875 glPixelTransferf(GL_BLUE_SCALE, rgb[2]);
878 /* rect contains image in 'rendersize', we only scale if needed */
880 /* first allocate imbuf for scaling and copy preview into it */
881 ima = IMB_allocImBuf(rw, rh, 32, IB_rect);
882 memcpy(ima->rect, rect, rw*rh*sizeof(unsigned int));
883 IMB_scaleImBuf(ima, w, h); /* scale it */
889 glaDrawPixelsSafe(x, y, w, h, w, GL_RGBA, GL_UNSIGNED_BYTE, rect);
893 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, rect);
901 glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
904 glPixelTransferf(GL_RED_SCALE, 1.0f);
905 glPixelTransferf(GL_GREEN_SCALE, 1.0f);
906 glPixelTransferf(GL_BLUE_SCALE, 1.0f);
910 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)
912 float x1, x2, y1, y2;
914 if(rgb) glColor4f(rgb[0], rgb[1], rgb[2], alpha);
915 else glColor4f(1.0f, 1.0f, 1.0f, alpha);
917 x1= ix*icongltex.invw;
918 x2= (ix + ih)*icongltex.invw;
919 y1= iy*icongltex.invh;
920 y2= (iy + ih)*icongltex.invh;
922 glEnable(GL_TEXTURE_2D);
923 glBindTexture(GL_TEXTURE_2D, icongltex.id);
926 glTexCoord2f(x1, y1);
929 glTexCoord2f(x2, y1);
932 glTexCoord2f(x2, y2);
933 glVertex2f(x+w, y+h);
935 glTexCoord2f(x1, y2);
939 glBindTexture(GL_TEXTURE_2D, 0);
940 glDisable(GL_TEXTURE_2D);
943 /* Drawing size for preview images */
944 static int get_draw_size(enum eIconSizes size)
947 case ICON_SIZE_ICON: return ICON_DEFAULT_HEIGHT;
948 case ICON_SIZE_PREVIEW: return PREVIEW_DEFAULT_HEIGHT;
953 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)
958 float fdraw_size= is_preview ? draw_size : (draw_size * UI_DPI_ICON_FAC);
961 icon = BKE_icon_get(icon_id);
966 printf("%s: Internal error, no icon for icon ID: %d\n", __func__, icon_id);
970 di = (DrawInfo*)icon->drawinfo;
973 di = icon_create_drawinfo();
976 icon->drawinfo_free = UI_icons_free_drawinfo;
979 /* scale width and height according to aspect */
980 w = (int)(fdraw_size/aspect + 0.5f);
981 h = (int)(fdraw_size/aspect + 0.5f);
983 if(di->type == ICON_TYPE_VECTOR) {
984 /* vector icons use the uiBlock transformation, they are not drawn
985 with untransformed coordinates like the other icons */
986 di->data.vector.func((int)x, (int)y, ICON_DEFAULT_HEIGHT, ICON_DEFAULT_HEIGHT, 1.0f);
988 else if(di->type == ICON_TYPE_TEXTURE) {
989 icon_draw_texture(x, y, (float)w, (float)h, di->data.texture.x, di->data.texture.y,
990 di->data.texture.w, di->data.texture.h, alpha, rgb);
992 else if(di->type == ICON_TYPE_BUFFER) {
993 /* it is a builtin icon */
994 iimg= di->data.buffer.image;
996 if(!iimg->rect) return; /* something has gone wrong! */
998 icon_draw_rect(x, y, w, h, aspect, iimg->w, iimg->h, iimg->rect, alpha, rgb, is_preview);
1000 else if(di->type == ICON_TYPE_PREVIEW) {
1001 PreviewImage* pi = BKE_previewimg_get((ID*)icon->obj);
1004 /* no create icon on this level in code */
1005 if(!pi->rect[size]) return; /* something has gone wrong! */
1007 /* preview images use premul alpha ... */
1008 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1009 icon_draw_rect(x, y, w, h, aspect, pi->w[size], pi->h[size], pi->rect[size], 1.0f, NULL, is_preview);
1010 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1015 static void ui_id_icon_render(bContext *C, ID *id, int big)
1017 PreviewImage *pi = BKE_previewimg_get(id);
1020 if ((pi->changed[0] ||!pi->rect[0])) /* changed only ever set by dynamic icons */
1022 /* create the rect if necessary */
1024 icon_set_image(C, id, pi, ICON_SIZE_ICON); /* icon size */
1026 icon_set_image(C, id, pi, ICON_SIZE_PREVIEW); /* bigger preview size */
1033 static void ui_id_brush_render(bContext *C, ID *id)
1035 PreviewImage *pi = BKE_previewimg_get(id);
1041 for(i = 0; i < NUM_ICON_SIZES; i++) {
1042 /* check if rect needs to be created; changed
1043 only set by dynamic icons */
1044 if((pi->changed[i] || !pi->rect[i])) {
1045 icon_set_image(C, id, pi, i);
1052 static int ui_id_brush_get_icon(bContext *C, ID *id)
1054 Brush *br = (Brush*)id;
1056 if(br->flag & BRUSH_CUSTOM_ICON) {
1058 ui_id_brush_render(C, id);
1061 Object *ob = CTX_data_active_object(C);
1063 EnumPropertyItem *items = NULL;
1066 /* XXX: this is not nice, should probably make brushes
1067 be strictly in one paint mode only to avoid
1068 checking various context stuff here */
1070 if(CTX_wm_view3d(C) && ob) {
1071 if(ob->mode & OB_MODE_SCULPT)
1072 mode = OB_MODE_SCULPT;
1073 else if(ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT))
1074 mode = OB_MODE_VERTEX_PAINT;
1075 else if(ob->mode & OB_MODE_TEXTURE_PAINT)
1076 mode = OB_MODE_TEXTURE_PAINT;
1078 else if((sima = CTX_wm_space_image(C)) &&
1079 (sima->flag & SI_DRAWTOOL)) {
1080 mode = OB_MODE_TEXTURE_PAINT;
1083 /* reset the icon */
1084 if(mode == OB_MODE_SCULPT) {
1085 items = brush_sculpt_tool_items;
1086 tool = br->sculpt_tool;
1088 else if(mode == OB_MODE_VERTEX_PAINT) {
1089 items = brush_vertex_tool_items;
1090 tool = br->vertexpaint_tool;
1092 else if(mode == OB_MODE_TEXTURE_PAINT) {
1093 items = brush_image_tool_items;
1094 tool = br->imagepaint_tool;
1097 if(!items || !RNA_enum_icon_from_value(items, tool, &id->icon_id))
1104 int ui_id_icon_get(bContext *C, ID *id, int big)
1109 switch(GS(id->name)) {
1111 iconid= ui_id_brush_get_icon(C, id);
1113 case ID_MA: /* fall through */
1114 case ID_TE: /* fall through */
1115 case ID_IM: /* fall through */
1116 case ID_WO: /* fall through */
1117 case ID_LA: /* fall through */
1118 iconid= BKE_icon_getid(id);
1119 /* checks if not exists, or changed */
1120 ui_id_icon_render(C, id, big);
1129 static void icon_draw_at_size(float x, float y, int icon_id, float aspect, float alpha, enum eIconSizes size, int nocreate)
1131 int draw_size = get_draw_size(size);
1132 icon_draw_size(x, y, icon_id, aspect, alpha, NULL, size, draw_size, nocreate, FALSE);
1135 void UI_icon_draw_aspect(float x, float y, int icon_id, float aspect, float alpha)
1137 icon_draw_at_size(x, y, icon_id, aspect, alpha, ICON_SIZE_ICON, 0);
1140 void UI_icon_draw_aspect_color(float x, float y, int icon_id, float aspect, float *rgb)
1142 int draw_size = get_draw_size(ICON_SIZE_ICON);
1143 icon_draw_size(x, y, icon_id, aspect, 1.0f, rgb, ICON_SIZE_ICON, draw_size, FALSE, FALSE);
1146 void UI_icon_draw(float x, float y, int icon_id)
1148 UI_icon_draw_aspect(x, y, icon_id, 1.0f, 1.0f);
1151 void UI_icon_draw_size(float x, float y, int size, int icon_id, float alpha)
1153 icon_draw_size(x, y, icon_id, 1.0f, alpha, NULL, ICON_SIZE_ICON, size, TRUE, FALSE);
1156 void UI_icon_draw_preview(float x, float y, int icon_id)
1158 icon_draw_at_size(x, y, icon_id, 1.0f, 1.0f, ICON_SIZE_PREVIEW, 0);
1161 void UI_icon_draw_preview_aspect(float x, float y, int icon_id, float aspect)
1163 icon_draw_at_size(x, y, icon_id, aspect, 1.0f, ICON_SIZE_PREVIEW, 0);
1166 void UI_icon_draw_preview_aspect_size(float x, float y, int icon_id, float aspect, int size)
1168 icon_draw_size(x, y, icon_id, aspect, 1.0f, NULL, ICON_SIZE_PREVIEW, size, FALSE, TRUE);