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 *****
37 #include "BLI_winstuff.h"
39 #include "MEM_guardedalloc.h"
41 #include "GPU_extensions.h"
44 #include "BLI_blenlib.h"
45 #include "BLI_storage_types.h"
46 #include "BLI_utildefines.h"
48 #include "DNA_brush_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_space_types.h"
53 #include "RNA_access.h"
54 #include "RNA_enum_types.h"
56 #include "BKE_context.h"
57 #include "BKE_global.h"
58 #include "BKE_icons.h"
59 #include "BKE_utildefines.h"
61 #include "IMB_imbuf.h"
62 #include "IMB_imbuf_types.h"
65 #include "BIF_glutil.h"
67 #include "ED_datafiles.h"
68 #include "ED_render.h"
70 #include "UI_interface.h"
71 #include "UI_interface_icons.h"
73 #include "interface_intern.h"
76 #define ICON_IMAGE_W 600
77 #define ICON_IMAGE_H 640
79 #define ICON_GRID_COLS 26
80 #define ICON_GRID_ROWS 30
82 #define ICON_GRID_MARGIN 5
83 #define ICON_GRID_W 16
84 #define ICON_GRID_H 16
86 typedef struct IconImage {
92 typedef void (*VectorDrawFunc)(int x, int y, int w, int h, float alpha);
94 #define ICON_TYPE_PREVIEW 0
95 #define ICON_TYPE_TEXTURE 1
96 #define ICON_TYPE_BUFFER 2
97 #define ICON_TYPE_VECTOR 3
99 typedef struct DrawInfo {
103 /* type specific data */
116 typedef struct IconTexture {
124 /* ******************* STATIC LOCAL VARS ******************* */
125 /* static here to cache results of icon directory scan, so it's not
126 * scanning the filesystem each time the menu is drawn */
127 static struct ListBase iconfilelist = {0, 0};
128 static IconTexture icongltex = {0, 0, 0, 0.0f, 0.0f};
130 /* **************************************************** */
132 static void def_internal_icon(ImBuf *bbuf, int icon_id, int xofs, int yofs, int size, int type)
134 Icon *new_icon = NULL;
135 IconImage *iimg = NULL;
140 new_icon = MEM_callocN(sizeof(Icon), "texicon");
142 new_icon->obj = 0; /* icon is not for library object */
145 di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
148 if(type == ICON_TYPE_TEXTURE) {
149 di->data.texture.x= xofs;
150 di->data.texture.y= yofs;
151 di->data.texture.w= size;
152 di->data.texture.h= size;
154 else if(type == ICON_TYPE_BUFFER) {
155 iimg = MEM_mallocN(sizeof(IconImage), "icon_img");
156 iimg->rect = MEM_mallocN(size*size*sizeof(unsigned int), "icon_rect");
160 /* Here we store the rect in the icon - same as before */
162 for (y=0; y<size; y++) {
163 memcpy(&iimg->rect[y*size], &bbuf->rect[(y+yofs)*imgsize+xofs], size*sizeof(int));
166 di->data.buffer.image = iimg;
169 new_icon->drawinfo_free = UI_icons_free_drawinfo;
170 new_icon->drawinfo = di;
172 BKE_icon_set(icon_id, new_icon);
175 static void def_internal_vicon( int icon_id, VectorDrawFunc drawFunc)
177 Icon *new_icon = NULL;
180 new_icon = MEM_callocN(sizeof(Icon), "texicon");
182 new_icon->obj = 0; /* icon is not for library object */
185 di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
186 di->type= ICON_TYPE_VECTOR;
187 di->data.vector.func =drawFunc;
189 new_icon->drawinfo_free = 0;
190 new_icon->drawinfo = di;
192 BKE_icon_set(icon_id, new_icon);
195 /* Vector Icon Drawing Routines */
199 static void viconutil_set_point(GLint pt[2], int x, int y)
205 static void viconutil_draw_tri(GLint (*pts)[2])
207 glBegin(GL_TRIANGLES);
214 static void viconutil_draw_lineloop(GLint (*pts)[2], int numPoints)
218 glBegin(GL_LINE_LOOP);
219 for (i=0; i<numPoints; i++) {
225 static void viconutil_draw_lineloop_smooth(GLint (*pts)[2], int numPoints)
227 glEnable(GL_LINE_SMOOTH);
228 viconutil_draw_lineloop(pts, numPoints);
229 glDisable(GL_LINE_SMOOTH);
232 static void viconutil_draw_points(GLint (*pts)[2], int numPoints, int pointSize)
237 for (i=0; i<numPoints; i++) {
238 int x = pts[i][0], y = pts[i][1];
240 glVertex2i(x-pointSize,y-pointSize);
241 glVertex2i(x+pointSize,y-pointSize);
242 glVertex2i(x+pointSize,y+pointSize);
243 glVertex2i(x-pointSize,y+pointSize);
248 /* Drawing functions */
250 static void vicon_x_draw(int x, int y, int w, int h, float alpha)
257 glEnable( GL_LINE_SMOOTH );
261 glColor4f(0.0, 0.0, 0.0, alpha);
271 glDisable( GL_LINE_SMOOTH );
274 static void vicon_view3d_draw(int x, int y, int w, int h, float alpha)
278 int d = MAX2(2, h/3);
280 glColor4f(0.5, 0.5, 0.5, alpha);
282 glVertex2i(x , cy-d);
283 glVertex2i(x+w, cy-d);
284 glVertex2i(x , cy+d);
285 glVertex2i(x+w, cy+d);
287 glVertex2i(cx-d, y );
288 glVertex2i(cx-d, y+h);
289 glVertex2i(cx+d, y );
290 glVertex2i(cx+d, y+h);
293 glColor4f(0.0, 0.0, 0.0, alpha);
302 static void vicon_edit_draw(int x, int y, int w, int h, float alpha)
306 viconutil_set_point(pts[0], x+3 , y+3 );
307 viconutil_set_point(pts[1], x+w-3, y+3 );
308 viconutil_set_point(pts[2], x+w-3, y+h-3);
309 viconutil_set_point(pts[3], x+3 , y+h-3);
311 glColor4f(0.0, 0.0, 0.0, alpha);
312 viconutil_draw_lineloop(pts, 4);
314 glColor3f(1, 1, 0.0);
315 viconutil_draw_points(pts, 4, 1);
318 static void vicon_editmode_hlt_draw(int x, int y, int w, int h, float alpha)
322 viconutil_set_point(pts[0], x+w/2, y+h-2);
323 viconutil_set_point(pts[1], x+3, y+4);
324 viconutil_set_point(pts[2], x+w-3, y+4);
326 glColor4f(0.5, 0.5, 0.5, alpha);
327 viconutil_draw_tri(pts);
329 glColor4f(0.0, 0.0, 0.0, 1);
330 viconutil_draw_lineloop_smooth(pts, 3);
332 glColor3f(1, 1, 0.0);
333 viconutil_draw_points(pts, 3, 1);
336 static void vicon_editmode_dehlt_draw(int x, int y, int w, int h, float UNUSED(alpha))
340 viconutil_set_point(pts[0], x+w/2, y+h-2);
341 viconutil_set_point(pts[1], x+3, y+4);
342 viconutil_set_point(pts[2], x+w-3, y+4);
344 glColor4f(0.0f, 0.0f, 0.0f, 1);
345 viconutil_draw_lineloop_smooth(pts, 3);
347 glColor3f(.9f, .9f, .9f);
348 viconutil_draw_points(pts, 3, 1);
351 static void vicon_disclosure_tri_right_draw(int x, int y, int w, int UNUSED(h), float alpha)
356 int d = w/3, d2 = w/5;
358 viconutil_set_point(pts[0], cx-d2, cy+d);
359 viconutil_set_point(pts[1], cx-d2, cy-d);
360 viconutil_set_point(pts[2], cx+d2, cy);
362 glShadeModel(GL_SMOOTH);
363 glBegin(GL_TRIANGLES);
364 glColor4f(0.8f, 0.8f, 0.8f, alpha);
367 glColor4f(0.3f, 0.3f, 0.3f, alpha);
370 glShadeModel(GL_FLAT);
372 glColor4f(0.0f, 0.0f, 0.0f, 1);
373 viconutil_draw_lineloop_smooth(pts, 3);
376 static void vicon_small_tri_right_draw(int x, int y, int w, int UNUSED(h), float alpha)
381 int d = w/5, d2 = w/7;
383 viconutil_set_point(pts[0], cx-d2, cy+d);
384 viconutil_set_point(pts[1], cx-d2, cy-d);
385 viconutil_set_point(pts[2], cx+d2, cy);
387 glColor4f(0.2f, 0.2f, 0.2f, alpha);
389 glShadeModel(GL_SMOOTH);
390 glBegin(GL_TRIANGLES);
395 glShadeModel(GL_FLAT);
398 static void vicon_disclosure_tri_down_draw(int x, int y, int w, int UNUSED(h), float alpha)
403 int d = w/3, d2 = w/5;
405 viconutil_set_point(pts[0], cx+d, cy+d2);
406 viconutil_set_point(pts[1], cx-d, cy+d2);
407 viconutil_set_point(pts[2], cx, cy-d2);
409 glShadeModel(GL_SMOOTH);
410 glBegin(GL_TRIANGLES);
411 glColor4f(0.8f, 0.8f, 0.8f, alpha);
414 glColor4f(0.3f, 0.3f, 0.3f, alpha);
417 glShadeModel(GL_FLAT);
419 glColor4f(0.0f, 0.0f, 0.0f, 1);
420 viconutil_draw_lineloop_smooth(pts, 3);
423 static void vicon_move_up_draw(int x, int y, int w, int h, float UNUSED(alpha))
427 glEnable(GL_LINE_SMOOTH);
429 glColor3f(0.0, 0.0, 0.0);
431 glBegin(GL_LINE_STRIP);
432 glVertex2i(x+w/2-d*2, y+h/2+d);
433 glVertex2i(x+w/2, y+h/2-d + 1);
434 glVertex2i(x+w/2+d*2, y+h/2+d);
438 glDisable(GL_LINE_SMOOTH);
441 static void vicon_move_down_draw(int x, int y, int w, int h, float UNUSED(alpha))
445 glEnable(GL_LINE_SMOOTH);
447 glColor3f(0.0, 0.0, 0.0);
449 glBegin(GL_LINE_STRIP);
450 glVertex2i(x+w/2-d*2, y+h/2+d);
451 glVertex2i(x+w/2, y+h/2-d - 1);
452 glVertex2i(x+w/2+d*2, y+h/2+d);
456 glDisable(GL_LINE_SMOOTH);
459 static void init_brush_icons(void)
462 #define INIT_BRUSH_ICON(icon_id, name) \
463 bbuf = IMB_ibImageFromMemory((unsigned char*)datatoc_ ##name## _png, \
464 datatoc_ ##name## _png_size, IB_rect); \
465 def_internal_icon(bbuf, icon_id, 0, 0, w, ICON_TYPE_BUFFER); \
467 // end INIT_BRUSH_ICON
472 INIT_BRUSH_ICON(ICON_BRUSH_ADD, add);
473 INIT_BRUSH_ICON(ICON_BRUSH_BLOB, blob);
474 INIT_BRUSH_ICON(ICON_BRUSH_BLUR, blur);
475 INIT_BRUSH_ICON(ICON_BRUSH_CLAY, clay);
476 INIT_BRUSH_ICON(ICON_BRUSH_CLONE, clone);
477 INIT_BRUSH_ICON(ICON_BRUSH_CREASE, crease);
478 INIT_BRUSH_ICON(ICON_BRUSH_DARKEN, darken);
479 INIT_BRUSH_ICON(ICON_BRUSH_SCULPT_DRAW, draw);
480 INIT_BRUSH_ICON(ICON_BRUSH_FILL, fill);
481 INIT_BRUSH_ICON(ICON_BRUSH_FLATTEN, flatten);
482 INIT_BRUSH_ICON(ICON_BRUSH_GRAB, grab);
483 INIT_BRUSH_ICON(ICON_BRUSH_INFLATE, inflate);
484 INIT_BRUSH_ICON(ICON_BRUSH_LAYER, layer);
485 INIT_BRUSH_ICON(ICON_BRUSH_LIGHTEN, lighten);
486 INIT_BRUSH_ICON(ICON_BRUSH_MIX, mix);
487 INIT_BRUSH_ICON(ICON_BRUSH_MULTIPLY, multiply);
488 INIT_BRUSH_ICON(ICON_BRUSH_NUDGE, nudge);
489 INIT_BRUSH_ICON(ICON_BRUSH_PINCH, pinch);
490 INIT_BRUSH_ICON(ICON_BRUSH_SCRAPE, scrape);
491 INIT_BRUSH_ICON(ICON_BRUSH_SMEAR, smear);
492 INIT_BRUSH_ICON(ICON_BRUSH_SMOOTH, smooth);
493 INIT_BRUSH_ICON(ICON_BRUSH_SNAKE_HOOK, snake_hook);
494 INIT_BRUSH_ICON(ICON_BRUSH_SOFTEN, soften);
495 INIT_BRUSH_ICON(ICON_BRUSH_SUBTRACT, subtract);
496 INIT_BRUSH_ICON(ICON_BRUSH_TEXDRAW, texdraw);
497 INIT_BRUSH_ICON(ICON_BRUSH_THUMB, thumb);
498 INIT_BRUSH_ICON(ICON_BRUSH_ROTATE, twist);
499 INIT_BRUSH_ICON(ICON_BRUSH_VERTEXDRAW, vertexdraw);
501 #undef INIT_BRUSH_ICON
504 static void init_internal_icons(void)
506 bTheme *btheme= U.themes.first;
509 char iconfilestr[FILE_MAXDIR+FILE_MAXFILE];
511 if ((btheme!=NULL) && btheme->tui.iconfile[0]) {
512 char *datadir= BLI_get_folder(BLENDER_DATAFILES, NULL);
514 BLI_make_file_string("/", iconfilestr, datadir, btheme->tui.iconfile);
516 if (BLI_exists(iconfilestr)) {
517 bbuf = IMB_loadiffname(iconfilestr, IB_rect);
518 if(bbuf->x < ICON_IMAGE_W || bbuf->y < ICON_IMAGE_H) {
520 printf("\n***WARNING***\nIcons file %s too small.\nUsing built-in Icons instead\n", iconfilestr);
528 bbuf = IMB_ibImageFromMemory((unsigned char*)datatoc_blenderbuttons, datatoc_blenderbuttons_size, IB_rect);
531 /* free existing texture if any */
533 glDeleteTextures(1, &icongltex.id);
537 /* we only use a texture for cards with non-power of two */
538 if(GPU_non_power_of_two_support()) {
539 glGenTextures(1, &icongltex.id);
542 icongltex.w = bbuf->x;
543 icongltex.h = bbuf->y;
544 icongltex.invw = 1.0f/bbuf->x;
545 icongltex.invh = 1.0f/bbuf->y;
547 glBindTexture(GL_TEXTURE_2D, icongltex.id);
548 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bbuf->x, bbuf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, bbuf->rect);
549 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
550 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
551 glBindTexture(GL_TEXTURE_2D, 0);
553 if(glGetError() == GL_OUT_OF_MEMORY) {
554 glDeleteTextures(1, &icongltex.id);
562 icontype= ICON_TYPE_TEXTURE;
564 icontype= ICON_TYPE_BUFFER;
567 for (y=0; y<ICON_GRID_ROWS; y++) {
568 for (x=0; x<ICON_GRID_COLS; x++) {
569 def_internal_icon(bbuf, BIFICONID_FIRST + y*ICON_GRID_COLS + x,
570 x*(ICON_GRID_W+ICON_GRID_MARGIN)+ICON_GRID_MARGIN,
571 y*(ICON_GRID_H+ICON_GRID_MARGIN)+ICON_GRID_MARGIN, ICON_GRID_W,
577 def_internal_vicon(VICO_VIEW3D_VEC, vicon_view3d_draw);
578 def_internal_vicon(VICO_EDIT_VEC, vicon_edit_draw);
579 def_internal_vicon(VICO_EDITMODE_DEHLT, vicon_editmode_dehlt_draw);
580 def_internal_vicon(VICO_EDITMODE_HLT, vicon_editmode_hlt_draw);
581 def_internal_vicon(VICO_DISCLOSURE_TRI_RIGHT_VEC, vicon_disclosure_tri_right_draw);
582 def_internal_vicon(VICO_DISCLOSURE_TRI_DOWN_VEC, vicon_disclosure_tri_down_draw);
583 def_internal_vicon(VICO_MOVE_UP_VEC, vicon_move_up_draw);
584 def_internal_vicon(VICO_MOVE_DOWN_VEC, vicon_move_down_draw);
585 def_internal_vicon(VICO_X_VEC, vicon_x_draw);
586 def_internal_vicon(VICO_SMALL_TRI_RIGHT_VEC, vicon_small_tri_right_draw);
592 static void init_iconfile_list(struct ListBase *list)
596 struct direntry *dir;
597 int restoredir = 1; /* restore to current directory */
598 int totfile, i, index=1;
600 char icondirstr[FILE_MAX];
601 char iconfilestr[FILE_MAX+16]; /* allow 256 chars for file+dir */
602 char olddir[FILE_MAX];
605 list->first = list->last = NULL;
606 datadir = BLI_get_folder(BLENDER_DATAFILES, NULL);
608 if (!datadir) return;
610 BLI_make_file_string("/", icondirstr, datadir, "");
612 if(BLI_exists(icondirstr)==0)
615 /* since BLI_getdir changes the current working directory, restore it
616 back to old value afterwards */
617 if(!BLI_getwdN(olddir, sizeof(olddir)))
619 totfile = BLI_getdir(icondirstr, &dir);
620 if (restoredir && !chdir(olddir)) {} /* fix warning about checking return value */
622 for(i=0; i<totfile; i++) {
623 if( (dir[i].type & S_IFREG) ) {
624 char *filename = dir[i].relname;
626 if(BLI_testextensie(filename, ".png")) {
628 /* check to see if the image is the right size, continue if not */
629 /* copying strings here should go ok, assuming that we never get back
630 a complete path to file longer than 256 chars */
631 sprintf(iconfilestr, "%s/%s", icondirstr, filename);
632 if(BLI_exists(iconfilestr))
633 bbuf= IMB_loadiffname(iconfilestr, IB_rect);
647 /* bad size or failed to load */
648 if ((ifilex != ICON_IMAGE_W) || (ifiley != ICON_IMAGE_H))
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_getdir() */
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_streq(filename, ifile->filename)) {
700 ListBase *UI_iconfile_list(void)
702 ListBase *list=&(iconfilelist);
708 void UI_icons_free(void)
711 glDeleteTextures(1, &icongltex.id);
715 free_iconfile_list(&iconfilelist);
719 void UI_icons_free_drawinfo(void *drawinfo)
721 DrawInfo *di = drawinfo;
724 if(di->type == ICON_TYPE_BUFFER) {
725 if(di->data.buffer.image) {
726 MEM_freeN(di->data.buffer.image->rect);
727 MEM_freeN(di->data.buffer.image);
735 static DrawInfo *icon_create_drawinfo(void)
739 di = MEM_callocN(sizeof(DrawInfo), "di_icon");
740 di->type= ICON_TYPE_PREVIEW;
745 int UI_icon_get_width(int icon_id)
750 icon = BKE_icon_get(icon_id);
752 if (icon==ICON_NULL) {
754 printf("UI_icon_get_width: Internal error, no icon for icon ID: %d\n", icon_id);
758 di = (DrawInfo *)icon->drawinfo;
760 di = icon_create_drawinfo();
765 return ICON_DEFAULT_WIDTH;
770 int UI_icon_get_height(int icon_id)
775 icon = BKE_icon_get(icon_id);
777 if (icon==ICON_NULL) {
779 printf("UI_icon_get_height: Internal error, no icon for icon ID: %d\n", icon_id);
783 di = (DrawInfo*)icon->drawinfo;
786 di = icon_create_drawinfo();
791 return ICON_DEFAULT_HEIGHT;
796 void UI_icons_init(int first_dyn_id)
798 init_iconfile_list(&iconfilelist);
799 BKE_icons_init(first_dyn_id);
800 init_internal_icons();
804 /* Render size for preview images at level miplevel */
805 static int preview_render_size(int miplevel)
809 case 1: return PREVIEW_DEFAULT_HEIGHT;
814 static void icon_create_mipmap(struct PreviewImage* prv_img, int miplevel)
816 unsigned int size = preview_render_size(miplevel);
820 printf("Error: requested preview image does not exist");
822 if (!prv_img->rect[miplevel]) {
823 prv_img->w[miplevel] = size;
824 prv_img->h[miplevel] = size;
825 prv_img->changed[miplevel] = 1;
826 prv_img->changed_timestamp[miplevel] = 0;
827 prv_img->rect[miplevel] = MEM_callocN(size*size*sizeof(unsigned int), "prv_rect");
831 /* only called when icon has changed */
832 /* only call with valid pointer from UI_icon_draw */
833 static void icon_set_image(bContext *C, ID *id, PreviewImage* prv_img, int miplevel)
837 printf("No preview image for this ID: %s\n", id->name);
841 /* create the preview rect */
842 icon_create_mipmap(prv_img, miplevel);
844 ED_preview_icon_job(C, prv_img, id, prv_img->rect[miplevel],
845 prv_img->w[miplevel], prv_img->h[miplevel]);
848 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)
853 if(w<=0 || h<=0 || w>2000 || h>2000) {
854 printf("icon_draw_rect: icons are %i x %i pixels?\n", w, h);
855 BLI_assert(!"invalid icon size");
861 glPixelTransferf(GL_ALPHA_SCALE, alpha);
864 glPixelTransferf(GL_RED_SCALE, rgb[0]);
865 glPixelTransferf(GL_GREEN_SCALE, rgb[1]);
866 glPixelTransferf(GL_BLUE_SCALE, rgb[2]);
869 /* rect contains image in 'rendersize', we only scale if needed */
871 /* first allocate imbuf for scaling and copy preview into it */
872 ima = IMB_allocImBuf(rw, rh, 32, IB_rect);
873 memcpy(ima->rect, rect, rw*rh*sizeof(unsigned int));
874 IMB_scaleImBuf(ima, w, h); /* scale it */
880 glaDrawPixelsSafe(x, y, w, h, w, GL_RGBA, GL_UNSIGNED_BYTE, rect);
884 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, rect);
892 glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
895 glPixelTransferf(GL_RED_SCALE, 1.0f);
896 glPixelTransferf(GL_GREEN_SCALE, 1.0f);
897 glPixelTransferf(GL_BLUE_SCALE, 1.0f);
901 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)
903 float x1, x2, y1, y2;
905 if(rgb) glColor4f(rgb[0], rgb[1], rgb[2], alpha);
906 else glColor4f(1.0f, 1.0f, 1.0f, alpha);
908 x1= ix*icongltex.invw;
909 x2= (ix + ih)*icongltex.invw;
910 y1= iy*icongltex.invh;
911 y2= (iy + ih)*icongltex.invh;
913 glEnable(GL_TEXTURE_2D);
914 glBindTexture(GL_TEXTURE_2D, icongltex.id);
917 glTexCoord2f(x1, y1);
920 glTexCoord2f(x2, y1);
923 glTexCoord2f(x2, y2);
924 glVertex2f(x+w, y+h);
926 glTexCoord2f(x1, y2);
930 glBindTexture(GL_TEXTURE_2D, 0);
931 glDisable(GL_TEXTURE_2D);
934 /* Drawing size for preview images at level miplevel */
935 static int preview_size(int miplevel)
938 case 0: return ICON_DEFAULT_HEIGHT;
939 case 1: return PREVIEW_DEFAULT_HEIGHT;
944 static void icon_draw_size(float x, float y, int icon_id, float aspect, float alpha, float *rgb, int miplevel, int draw_size, int UNUSED(nocreate), int is_preview)
951 icon = BKE_icon_get(icon_id);
953 if (icon==ICON_NULL) {
955 printf("icon_draw_mipmap: Internal error, no icon for icon ID: %d\n", icon_id);
959 di = (DrawInfo*)icon->drawinfo;
962 di = icon_create_drawinfo();
965 icon->drawinfo_free = UI_icons_free_drawinfo;
968 /* scale width and height according to aspect */
969 w = (int)(draw_size/aspect + 0.5f);
970 h = (int)(draw_size/aspect + 0.5f);
972 if(di->type == ICON_TYPE_VECTOR) {
973 /* vector icons use the uiBlock transformation, they are not drawn
974 with untransformed coordinates like the other icons */
975 di->data.vector.func((int)x, (int)y, ICON_DEFAULT_HEIGHT, ICON_DEFAULT_HEIGHT, 1.0f);
977 else if(di->type == ICON_TYPE_TEXTURE) {
978 icon_draw_texture(x, y, w, h, di->data.texture.x, di->data.texture.y,
979 di->data.texture.w, di->data.texture.h, alpha, rgb);
981 else if(di->type == ICON_TYPE_BUFFER) {
982 /* it is a builtin icon */
983 iimg= di->data.buffer.image;
985 if(!iimg->rect) return; /* something has gone wrong! */
987 icon_draw_rect(x, y, w, h, aspect, iimg->w, iimg->h, iimg->rect, alpha, rgb, is_preview);
989 else if(di->type == ICON_TYPE_PREVIEW) {
990 PreviewImage* pi = BKE_previewimg_get((ID*)icon->obj);
993 /* no create icon on this level in code */
994 if(!pi->rect[miplevel]) return; /* something has gone wrong! */
996 /* preview images use premul alpha ... */
997 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
998 icon_draw_rect(x, y, w, h, aspect, pi->w[miplevel], pi->h[miplevel], pi->rect[miplevel], 1.0f, NULL, is_preview);
999 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1004 static void ui_id_icon_render(bContext *C, ID *id, int big)
1006 PreviewImage *pi = BKE_previewimg_get(id);
1009 if ((pi->changed[0] ||!pi->rect[0])) /* changed only ever set by dynamic icons */
1011 /* create the rect if necessary */
1013 icon_set_image(C, id, pi, 0); /* icon size */
1015 icon_set_image(C, id, pi, 1); /* bigger preview size */
1022 static void ui_id_brush_render(bContext *C, ID *id)
1024 PreviewImage *pi = BKE_previewimg_get(id);
1030 for(i = 0; i < PREVIEW_MIPMAPS; i++) {
1031 /* check if rect needs to be created; changed
1032 only set by dynamic icons */
1033 if((pi->changed[i] || !pi->rect[i])) {
1034 icon_set_image(C, id, pi, i);
1041 static int ui_id_brush_get_icon(bContext *C, ID *id)
1043 Brush *br = (Brush*)id;
1045 if(br->flag & BRUSH_CUSTOM_ICON) {
1047 ui_id_brush_render(C, id);
1050 Object *ob = CTX_data_active_object(C);
1052 EnumPropertyItem *items = NULL;
1055 /* XXX: this is not nice, should probably make brushes
1056 be strictly in one paint mode only to avoid
1057 checking various context stuff here */
1059 if(CTX_wm_view3d(C) && ob) {
1060 if(ob->mode & OB_MODE_SCULPT)
1061 mode = OB_MODE_SCULPT;
1062 else if(ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT))
1063 mode = OB_MODE_VERTEX_PAINT;
1064 else if(ob->mode & OB_MODE_TEXTURE_PAINT)
1065 mode = OB_MODE_TEXTURE_PAINT;
1067 else if((sima = CTX_wm_space_image(C)) &&
1068 (sima->flag & SI_DRAWTOOL)) {
1069 mode = OB_MODE_TEXTURE_PAINT;
1072 /* reset the icon */
1073 if(mode == OB_MODE_SCULPT) {
1074 items = brush_sculpt_tool_items;
1075 tool = br->sculpt_tool;
1077 else if(mode == OB_MODE_VERTEX_PAINT) {
1078 items = brush_vertexpaint_tool_items;
1079 tool = br->vertexpaint_tool;
1081 else if(mode == OB_MODE_TEXTURE_PAINT) {
1082 items = brush_imagepaint_tool_items;
1083 tool = br->imagepaint_tool;
1086 if(!items || !RNA_enum_icon_from_value(items, tool, &id->icon_id))
1093 int ui_id_icon_get(bContext *C, ID *id, int big)
1098 switch(GS(id->name))
1101 iconid= ui_id_brush_get_icon(C, id);
1103 case ID_MA: /* fall through */
1104 case ID_TE: /* fall through */
1105 case ID_IM: /* fall through */
1106 case ID_WO: /* fall through */
1107 case ID_LA: /* fall through */
1108 iconid= BKE_icon_getid(id);
1109 /* checks if not exists, or changed */
1110 ui_id_icon_render(C, id, big);
1119 static void icon_draw_mipmap(float x, float y, int icon_id, float aspect, float alpha, int miplevel, int nocreate)
1121 int draw_size = preview_size(miplevel);
1122 icon_draw_size(x, y, icon_id, aspect, alpha, NULL, miplevel, draw_size, nocreate, FALSE);
1125 void UI_icon_draw_aspect(float x, float y, int icon_id, float aspect, float alpha)
1127 icon_draw_mipmap(x, y, icon_id, aspect, alpha, PREVIEW_MIPMAP_ZERO, 0);
1130 void UI_icon_draw_aspect_color(float x, float y, int icon_id, float aspect, float *rgb)
1132 int draw_size = preview_size(PREVIEW_MIPMAP_ZERO);
1133 icon_draw_size(x, y, icon_id, aspect, 1.0f, rgb, PREVIEW_MIPMAP_ZERO, draw_size, FALSE, FALSE);
1136 void UI_icon_draw(float x, float y, int icon_id)
1138 UI_icon_draw_aspect(x, y, icon_id, 1.0f, 1.0f);
1141 void UI_icon_draw_size(float x, float y, int size, int icon_id, float alpha)
1143 icon_draw_size(x, y, icon_id, 1.0f, alpha, NULL, PREVIEW_MIPMAP_ZERO, size, TRUE, FALSE);
1146 void UI_icon_draw_preview(float x, float y, int icon_id)
1148 icon_draw_mipmap(x, y, icon_id, 1.0f, 1.0f, PREVIEW_MIPMAP_LARGE, 0);
1151 void UI_icon_draw_preview_aspect(float x, float y, int icon_id, float aspect)
1153 icon_draw_mipmap(x, y, icon_id, aspect, 1.0f, PREVIEW_MIPMAP_LARGE, 0);
1156 void UI_icon_draw_preview_aspect_size(float x, float y, int icon_id, float aspect, int size)
1158 icon_draw_size(x, y, icon_id, aspect, 1.0f, NULL, PREVIEW_MIPMAP_LARGE, size, FALSE, TRUE);