2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * The Original Code is Copyright (C) 2009 Blender Foundation.
19 * All rights reserved.
21 * Contributor(s): Blender Foundation
23 * ***** END GPL LICENSE BLOCK *****
31 #include "MEM_guardedalloc.h"
34 #include "DNA_screen_types.h"
35 #include "DNA_userdef_types.h"
36 #include "DNA_windowmanager_types.h"
38 #include "BLI_arithb.h"
39 #include "BLI_listbase.h"
42 #include "BKE_context.h"
43 #include "BKE_curve.h"
44 #include "BKE_global.h"
45 #include "BKE_utildefines.h"
48 #include "BIF_glutil.h"
52 #include "UI_interface.h"
53 #include "UI_interface_icons.h"
54 #include "UI_resources.h"
55 #include "UI_view2d.h"
60 #include "interface_intern.h"
62 /* ************** widget base functions ************** */
64 - in: roundbox codes for corner types and radius
65 - return: array of [size][2][x,y] points, the edges of the roundbox, + UV coords
67 - draw black box with alpha 0 on exact button boundbox
69 - draw the inner part for a round filled box, with color blend codes or texture coords
70 - draw outline in outline color
71 - draw outer part, bottom half, extruded 1 pixel to bottom, for emboss shadow
72 - draw extra decorations
73 - draw background color box with alpha 1 on exact button boundbox
77 /* fill this struct with polygon info to draw AA'ed */
78 /* it has outline, back, and two optional tria meshes */
80 typedef struct uiWidgetTrias {
88 typedef struct uiWidgetBase {
90 int totvert, halfwayvert;
93 float inner_uv[64][2];
95 short inner, outline, emboss; /* set on/off */
103 /* uiWidgetType: for time being only for visual appearance,
104 later, a handling callback can be added too
106 typedef struct uiWidgetType {
108 /* pointer to theme color definition */
109 uiWidgetColors *wcol_theme;
110 uiWidgetStateColors *wcol_state;
112 /* converted colors for state */
115 void (*state)(struct uiWidgetType *, int state);
116 void (*draw)(uiWidgetColors *, rcti *, int state, int roundboxalign);
117 void (*custom)(uiBut *, uiWidgetColors *, rcti *, int state, int roundboxalign);
118 void (*text)(uiFontStyle *, uiWidgetColors *, uiBut *, rcti *);
123 /* *********************** draw data ************************** */
125 static float cornervec[9][2]= {{0.0, 0.0}, {0.195, 0.02}, {0.383, 0.067}, {0.55, 0.169},
126 {0.707, 0.293}, {0.831, 0.45}, {0.924, 0.617}, {0.98, 0.805}, {1.0, 1.0}};
128 static float jit[8][2]= {{0.468813 , -0.481430}, {-0.155755 , -0.352820},
129 {0.219306 , -0.238501}, {-0.393286 , -0.110949}, {-0.024699 , 0.013908},
130 {0.343805 , 0.147431}, {-0.272855 , 0.269918}, {0.095909 , 0.388710}};
132 static float num_tria_vert[19][2]= {
133 {0.382684, 0.923879}, {0.000001, 1.000000}, {-0.382683, 0.923880}, {-0.707107, 0.707107},
134 {-0.923879, 0.382684}, {-1.000000, 0.000000}, {-0.923880, -0.382684}, {-0.707107, -0.707107},
135 {-0.382683, -0.923880}, {0.000000, -1.000000}, {0.382684, -0.923880}, {0.707107, -0.707107},
136 {0.923880, -0.382684}, {1.000000, -0.000000}, {0.923880, 0.382683}, {0.707107, 0.707107},
137 {-0.352077, 0.532607}, {-0.352077, -0.549313}, {0.729843, -0.008353}};
139 static int num_tria_face[19][3]= {
140 {13, 14, 18}, {17, 5, 6}, {12, 13, 18}, {17, 6, 7}, {15, 18, 14}, {16, 4, 5}, {16, 5, 17}, {18, 11, 12},
141 {18, 17, 10}, {18, 10, 11}, {17, 9, 10}, {15, 0, 18}, {18, 0, 16}, {3, 4, 16}, {8, 9, 17}, {8, 17, 7},
142 {2, 3, 16}, {1, 2, 16}, {16, 0, 1}};
144 static float menu_tria_vert[6][2]= {
145 {-0.41, 0.16}, {0.41, 0.16}, {0, 0.82},
146 {0, -0.82}, {-0.41, -0.16}, {0.41, -0.16}};
148 static int menu_tria_face[2][3]= {{2, 0, 1}, {3, 5, 4}};
150 static float check_tria_vert[6][2]= {
151 {-0.578579, 0.253369}, {-0.392773, 0.412794}, {-0.004241, -0.328551},
152 {-0.003001, 0.034320}, {1.055313, 0.864744}, {0.866408, 1.026895}};
154 static int check_tria_face[4][3]= {
155 {3, 2, 4}, {3, 4, 5}, {1, 0, 3}, {0, 2, 3}};
157 /* ************************************************* */
159 void ui_draw_anti_tria(float x1, float y1, float x2, float y2, float x3, float y3)
165 glGetFloatv(GL_CURRENT_COLOR, color);
169 /* for each AA step */
171 glTranslatef(1.0*jit[j][0], 1.0*jit[j][1], 0.0f);
179 glTranslatef(-1.0*jit[j][0], -1.0*jit[j][1], 0.0f);
186 static void widget_init(uiWidgetBase *wtb)
188 wtb->totvert= wtb->halfwayvert= 0;
198 /* helper call, makes shadow rect, with 'sun' above menu, so only shadow to left/right/bottom */
200 static int round_box_shadow_edges(float (*vert)[2], rcti *rect, float rad, int roundboxalign, float step)
203 float minx, miny, maxx, maxy;
208 if(2.0f*rad > rect->ymax-rect->ymin)
209 rad= 0.5f*(rect->ymax-rect->ymin);
211 minx= rect->xmin-step;
212 miny= rect->ymin-step;
213 maxx= rect->xmax+step;
214 maxy= rect->ymax+step;
218 vec[a][0]= rad*cornervec[a][0];
219 vec[a][1]= rad*cornervec[a][1];
222 /* start with left-top, anti clockwise */
223 if(roundboxalign & 1) {
224 for(a=0; a<9; a++, tot++) {
225 vert[tot][0]= minx+rad-vec[a][0];
226 vert[tot][1]= maxy-vec[a][1];
230 for(a=0; a<9; a++, tot++) {
236 if(roundboxalign & 8) {
237 for(a=0; a<9; a++, tot++) {
238 vert[tot][0]= minx+vec[a][1];
239 vert[tot][1]= miny+rad-vec[a][0];
243 for(a=0; a<9; a++, tot++) {
249 if(roundboxalign & 4) {
250 for(a=0; a<9; a++, tot++) {
251 vert[tot][0]= maxx-rad+vec[a][0];
252 vert[tot][1]= miny+vec[a][1];
256 for(a=0; a<9; a++, tot++) {
262 if(roundboxalign & 2) {
263 for(a=0; a<9; a++, tot++) {
264 vert[tot][0]= maxx-vec[a][1];
265 vert[tot][1]= maxy-rad+vec[a][0];
269 for(a=0; a<9; a++, tot++) {
277 /* this call has 1 extra arg to allow mask outline */
278 static void round_box__edges(uiWidgetBase *wt, int roundboxalign, rcti *rect, float rad, float radi)
280 float vec[9][2], veci[9][2];
281 float minx= rect->xmin, miny= rect->ymin, maxx= rect->xmax, maxy= rect->ymax;
282 float minxi= minx + 1.0f; /* boundbox inner */
283 float maxxi= maxx - 1.0f;
284 float minyi= miny + 1.0f;
285 float maxyi= maxy - 1.0f;
286 float facxi= 1.0f/(maxxi-minxi); /* for uv */
287 float facyi= 1.0f/(maxyi-minyi);
288 int a, tot= 0, minsize;
290 minsize= MIN2(rect->xmax-rect->xmin, rect->ymax-rect->ymin);
292 if(2.0f*rad > minsize)
295 if(2.0f*(radi+1.0f) > minsize)
296 radi= 0.5f*minsize - 1.0f;
300 veci[a][0]= radi*cornervec[a][0];
301 veci[a][1]= radi*cornervec[a][1];
302 vec[a][0]= rad*cornervec[a][0];
303 vec[a][1]= rad*cornervec[a][1];
306 /* corner left-bottom */
307 if(roundboxalign & 8) {
309 for(a=0; a<9; a++, tot++) {
310 wt->inner_v[tot][0]= minxi+veci[a][1];
311 wt->inner_v[tot][1]= minyi+radi-veci[a][0];
313 wt->outer_v[tot][0]= minx+vec[a][1];
314 wt->outer_v[tot][1]= miny+rad-vec[a][0];
316 wt->inner_uv[tot][0]= facxi*(wt->inner_v[tot][0] - minxi);
317 wt->inner_uv[tot][1]= facyi*(wt->inner_v[tot][1] - minyi);
321 wt->inner_v[tot][0]= minxi;
322 wt->inner_v[tot][1]= minyi;
324 wt->outer_v[tot][0]= minx;
325 wt->outer_v[tot][1]= miny;
327 wt->inner_uv[tot][0]= 0.0f;
328 wt->inner_uv[tot][1]= 0.0f;
333 /* corner right-bottom */
334 if(roundboxalign & 4) {
336 for(a=0; a<9; a++, tot++) {
337 wt->inner_v[tot][0]= maxxi-radi+veci[a][0];
338 wt->inner_v[tot][1]= minyi+veci[a][1];
340 wt->outer_v[tot][0]= maxx-rad+vec[a][0];
341 wt->outer_v[tot][1]= miny+vec[a][1];
343 wt->inner_uv[tot][0]= facxi*(wt->inner_v[tot][0] - minxi);
344 wt->inner_uv[tot][1]= facyi*(wt->inner_v[tot][1] - minyi);
348 wt->inner_v[tot][0]= maxxi;
349 wt->inner_v[tot][1]= minyi;
351 wt->outer_v[tot][0]= maxx;
352 wt->outer_v[tot][1]= miny;
354 wt->inner_uv[tot][0]= 1.0f;
355 wt->inner_uv[tot][1]= 0.0f;
360 wt->halfwayvert= tot;
362 /* corner right-top */
363 if(roundboxalign & 2) {
365 for(a=0; a<9; a++, tot++) {
366 wt->inner_v[tot][0]= maxxi-veci[a][1];
367 wt->inner_v[tot][1]= maxyi-radi+veci[a][0];
369 wt->outer_v[tot][0]= maxx-vec[a][1];
370 wt->outer_v[tot][1]= maxy-rad+vec[a][0];
372 wt->inner_uv[tot][0]= facxi*(wt->inner_v[tot][0] - minxi);
373 wt->inner_uv[tot][1]= facyi*(wt->inner_v[tot][1] - minyi);
377 wt->inner_v[tot][0]= maxxi;
378 wt->inner_v[tot][1]= maxyi;
380 wt->outer_v[tot][0]= maxx;
381 wt->outer_v[tot][1]= maxy;
383 wt->inner_uv[tot][0]= 1.0f;
384 wt->inner_uv[tot][1]= 1.0f;
389 /* corner left-top */
390 if(roundboxalign & 1) {
392 for(a=0; a<9; a++, tot++) {
393 wt->inner_v[tot][0]= minxi+radi-veci[a][0];
394 wt->inner_v[tot][1]= maxyi-veci[a][1];
396 wt->outer_v[tot][0]= minx+rad-vec[a][0];
397 wt->outer_v[tot][1]= maxy-vec[a][1];
399 wt->inner_uv[tot][0]= facxi*(wt->inner_v[tot][0] - minxi);
400 wt->inner_uv[tot][1]= facyi*(wt->inner_v[tot][1] - minyi);
406 wt->inner_v[tot][0]= minxi;
407 wt->inner_v[tot][1]= maxyi;
409 wt->outer_v[tot][0]= minx;
410 wt->outer_v[tot][1]= maxy;
412 wt->inner_uv[tot][0]= 0.0f;
413 wt->inner_uv[tot][1]= 1.0f;
421 static void round_box_edges(uiWidgetBase *wt, int roundboxalign, rcti *rect, float rad)
423 round_box__edges(wt, roundboxalign, rect, rad, rad-1.0f);
427 /* based on button rect, return scaled array of triangles */
428 static void widget_num_tria(uiWidgetTrias *tria, rcti *rect, float triasize, char where)
430 float centx, centy, sizex, sizey, minsize;
433 minsize= MIN2(rect->xmax-rect->xmin, rect->ymax-rect->ymin);
435 /* center position and size */
436 centx= (float)rect->xmin + 0.5f*minsize;
437 centy= (float)rect->ymin + 0.5f*minsize;
438 sizex= sizey= -0.5f*triasize*minsize;
441 centx= (float)rect->xmax - 0.5f*minsize;
444 else if(where=='t') {
445 centy= (float)rect->ymax - 0.5f*minsize;
449 else if(where=='b') {
454 for(a=0; a<19; a++) {
455 tria->vec[a][0]= sizex*num_tria_vert[a][i1] + centx;
456 tria->vec[a][1]= sizey*num_tria_vert[a][i2] + centy;
460 tria->index= num_tria_face;
463 static void widget_trias_draw(uiWidgetTrias *tria)
467 glBegin(GL_TRIANGLES);
468 for(a=0; a<tria->tot; a++) {
469 glVertex2fv(tria->vec[ tria->index[a][0] ]);
470 glVertex2fv(tria->vec[ tria->index[a][1] ]);
471 glVertex2fv(tria->vec[ tria->index[a][2] ]);
477 static void widget_menu_trias(uiWidgetTrias *tria, rcti *rect)
479 float centx, centy, size, asp;
482 /* center position and size */
483 centx= rect->xmax - 0.5f*(rect->ymax-rect->ymin);
484 centy= rect->ymin + 0.5f*(rect->ymax-rect->ymin);
485 size= 0.4f*(rect->ymax-rect->ymin);
488 asp= ((float)rect->xmax-rect->xmin)/((float)rect->ymax-rect->ymin);
489 if(asp > 1.2f && asp < 2.6f)
490 centx= rect->xmax - 0.3f*(rect->ymax-rect->ymin);
493 tria->vec[a][0]= size*menu_tria_vert[a][0] + centx;
494 tria->vec[a][1]= size*menu_tria_vert[a][1] + centy;
498 tria->index= menu_tria_face;
501 static void widget_check_trias(uiWidgetTrias *tria, rcti *rect)
503 float centx, centy, size;
506 /* center position and size */
507 centx= rect->xmin + 0.5f*(rect->ymax-rect->ymin);
508 centy= rect->ymin + 0.5f*(rect->ymax-rect->ymin);
509 size= 0.5f*(rect->ymax-rect->ymin);
512 tria->vec[a][0]= size*check_tria_vert[a][0] + centx;
513 tria->vec[a][1]= size*check_tria_vert[a][1] + centy;
517 tria->index= check_tria_face;
521 /* prepares shade colors */
522 static void shadecolors4(char *coltop, char *coldown, char *color, short shadetop, short shadedown)
525 coltop[0]= CLAMPIS(color[0]+shadetop, 0, 255);
526 coltop[1]= CLAMPIS(color[1]+shadetop, 0, 255);
527 coltop[2]= CLAMPIS(color[2]+shadetop, 0, 255);
530 coldown[0]= CLAMPIS(color[0]+shadedown, 0, 255);
531 coldown[1]= CLAMPIS(color[1]+shadedown, 0, 255);
532 coldown[2]= CLAMPIS(color[2]+shadedown, 0, 255);
533 coldown[3]= color[3];
536 static void round_box_shade_col4(char *col1, char *col2, float fac)
541 faci= floor(255.1f*fac);
544 col[0]= (faci*col1[0] + facm*col2[0])>>8;
545 col[1]= (faci*col1[1] + facm*col2[1])>>8;
546 col[2]= (faci*col1[2] + facm*col2[2])>>8;
547 col[3]= (faci*col1[3] + facm*col2[3])>>8;
552 static void widgetbase_outline(uiWidgetBase *wtb)
557 glBegin(GL_QUAD_STRIP);
558 for(a=0; a<wtb->totvert; a++) {
559 glVertex2fv(wtb->outer_v[a]);
560 glVertex2fv(wtb->inner_v[a]);
562 glVertex2fv(wtb->outer_v[0]);
563 glVertex2fv(wtb->inner_v[0]);
567 static void widgetbase_draw(uiWidgetBase *wtb, uiWidgetColors *wcol)
573 /* backdrop non AA */
575 if(wcol->shaded==0) {
577 /* filled center, solid */
578 glColor4ubv(wcol->inner);
580 for(a=0; a<wtb->totvert; a++)
581 glVertex2fv(wtb->inner_v[a]);
586 char col1[4], col2[4];
588 shadecolors4(col1, col2, wcol->inner, wcol->shadetop, wcol->shadedown);
590 glShadeModel(GL_SMOOTH);
592 for(a=0; a<wtb->totvert; a++) {
593 round_box_shade_col4(col1, col2, wtb->inner_uv[a][wtb->shadedir]);
594 glVertex2fv(wtb->inner_v[a]);
597 glShadeModel(GL_FLAT);
601 /* for each AA step */
604 glTranslatef(1.0*jit[j][0], 1.0*jit[j][1], 0.0f);
607 glColor4ub(wcol->outline[0], wcol->outline[1], wcol->outline[2], 32);
608 glBegin(GL_QUAD_STRIP);
609 for(a=0; a<wtb->totvert; a++) {
610 glVertex2fv(wtb->outer_v[a]);
611 glVertex2fv(wtb->inner_v[a]);
613 glVertex2fv(wtb->outer_v[0]);
614 glVertex2fv(wtb->inner_v[0]);
617 /* emboss bottom shadow */
619 glColor4f(1.0f, 1.0f, 1.0f, 0.02f);
620 glBegin(GL_QUAD_STRIP);
621 for(a=0; a<wtb->halfwayvert; a++) {
622 glVertex2fv(wtb->outer_v[a]);
623 glVertex2f(wtb->outer_v[a][0], wtb->outer_v[a][1]-1.0f);
628 glTranslatef(-1.0*jit[j][0], -1.0*jit[j][1], 0.0f);
633 if(wtb->tria1.tot || wtb->tria2.tot) {
634 /* for each AA step */
636 glTranslatef(1.0*jit[j][0], 1.0*jit[j][1], 0.0f);
639 glColor4ub(wcol->item[0], wcol->item[1], wcol->item[2], 32);
640 widget_trias_draw(&wtb->tria1);
643 glColor4ub(wcol->item[0], wcol->item[1], wcol->item[2], 32);
644 widget_trias_draw(&wtb->tria2);
647 glTranslatef(-1.0*jit[j][0], -1.0*jit[j][1], 0.0f);
655 /* *********************** text/icon ************************************** */
658 /* icons have been standardized... and this call draws in untransformed coordinates */
659 #define ICON_HEIGHT 16.0f
661 static void widget_draw_icon(uiBut *but, BIFIconID icon, int blend, rcti *rect)
664 float aspect, height;
666 /* this icon doesn't need draw... */
667 if(icon==ICON_BLANK1 && (but->flag & UI_ICON_SUBMENU)==0) return;
669 /* we need aspect from block, for menus... these buttons are scaled in uiPositionBlock() */
670 aspect= but->block->aspect;
671 if(aspect != but->aspect) {
672 /* prevent scaling up icon in pupmenu */
679 height= ICON_HEIGHT/aspect;
684 /* calculate blend color */
685 if ELEM3(but->type, TOG, ROW, TOGN) {
686 if(but->flag & UI_SELECT);
687 else if(but->flag & UI_ACTIVE);
693 if(icon && icon!=ICON_BLANK1) {
694 if(but->flag & UI_ICON_LEFT) {
695 if (but->type==BUT_TOGDUAL) {
696 if (but->drawstr[0]) {
699 xs= (rect->xmin+rect->xmax- height)/2;
702 else if (but->block->flag & UI_BLOCK_LOOP) {
703 if(but->type==SEARCH_MENU)
708 else if ((but->type==ICONROW) || (but->type==ICONTEXTROW)) {
714 ys= (rect->ymin+rect->ymax- height)/2;
717 xs= (rect->xmin+rect->xmax- height)/2;
718 ys= (rect->ymin+rect->ymax- height)/2;
721 UI_icon_draw_aspect_blended(xs, ys, icon, aspect, blend);
724 if(but->flag & UI_ICON_SUBMENU) {
726 ys= (rect->ymin+rect->ymax- height)/2;
728 UI_icon_draw_aspect_blended(xs, ys, ICON_RIGHTARROW_THIN, aspect, blend);
734 /* sets but->ofs to make sure text is correctly visible */
735 static void ui_text_leftclip(uiFontStyle *fstyle, uiBut *but, rcti *rect)
737 int okwidth= rect->xmax-rect->xmin;
739 /* need to set this first */
740 uiStyleFontSet(fstyle);
742 but->strwidth= BLF_width(but->drawstr);
745 while(but->strwidth > okwidth ) {
748 but->strwidth= BLF_width(but->drawstr+but->ofs);
750 /* textbut exception */
751 if(but->editstr && but->pos != -1) {
752 int pos= but->pos+strlen(but->str);
754 if(pos-1 < but->ofs) {
761 but->drawstr[ strlen(but->drawstr)-pos ]= 0;
765 if(but->strwidth < 10) break;
769 static void widget_draw_text(uiFontStyle *fstyle, uiWidgetColors *wcol, uiBut *but, rcti *rect)
774 uiStyleFontSet(fstyle);
776 if(but->editstr || (but->flag & UI_TEXT_LEFT))
777 fstyle->align= UI_STYLE_TEXT_LEFT;
779 fstyle->align= UI_STYLE_TEXT_CENTER;
781 /* text button selection and cursor */
782 if(but->editstr && but->pos != -1) {
784 short selsta_tmp, selend_tmp, selsta_draw, selwidth_draw;
786 if ((but->selend - but->selsta) > 0) {
787 /* XXX weak, why is this? (ton) */
790 /* text button selection */
791 selsta_tmp = but->selsta + strlen(but->str);
792 selend_tmp = but->selend + strlen(but->str);
794 if(but->drawstr[0]!=0) {
795 ch= but->drawstr[selsta_tmp];
796 but->drawstr[selsta_tmp]= 0;
798 selsta_draw = BLF_width(but->drawstr+but->ofs) + t;
800 but->drawstr[selsta_tmp]= ch;
802 ch= but->drawstr[selend_tmp];
803 but->drawstr[selend_tmp]= 0;
805 selwidth_draw = BLF_width(but->drawstr+but->ofs) + t;
807 but->drawstr[selend_tmp]= ch;
809 glColor3ubv(wcol->item);
810 glRects(rect->xmin+selsta_draw+1, rect->ymin+2, rect->xmin+selwidth_draw+1, rect->ymax-2);
814 pos= but->pos+strlen(but->str);
815 if(pos >= but->ofs) {
816 if(but->drawstr[0]!=0) {
817 ch= but->drawstr[pos];
818 but->drawstr[pos]= 0;
820 t= BLF_width(but->drawstr+but->ofs) + 1;
822 but->drawstr[pos]= ch;
827 glRects(rect->xmin+t, rect->ymin+2, rect->xmin+t+2, rect->ymax-2);
831 // ui_rasterpos_safe(x, y, but->aspect);
832 // if(but->type==IDPOIN) transopts= 0; // no translation, of course!
833 // else transopts= ui_translate_buttons();
835 /* cut string in 2 parts - only for menu entries */
836 if(ELEM5(but->type, SLI, NUM, TEX, NUMSLI, NUMABS)==0) {
837 cpoin= strchr(but->drawstr, '|');
841 glColor3ubv(wcol->text);
842 uiStyleFontDraw(fstyle, rect, but->drawstr+but->ofs);
844 /* part text right aligned */
846 fstyle->align= UI_STYLE_TEXT_RIGHT;
848 uiStyleFontDraw(fstyle, rect, cpoin+1);
853 /* draws text and icons for buttons */
854 static void widget_draw_text_icon(uiFontStyle *fstyle, uiWidgetColors *wcol, uiBut *but, rcti *rect)
857 if(but==NULL) return;
859 /* cutting off from left part */
860 if ELEM3(but->type, NUM, NUMABS, TEX) {
861 ui_text_leftclip(fstyle, but, rect);
865 /* check for button text label */
866 if (but->type == ICONTEXTROW) {
867 widget_draw_icon(but, (BIFIconID) (but->icon+but->iconadd), 0, rect);
871 if(but->type==BUT_TOGDUAL) {
873 if(but->pointype==SHO)
874 dualset= BTST( *(((short *)but->poin)+1), but->bitnr);
875 else if(but->pointype==INT)
876 dualset= BTST( *(((int *)but->poin)+1), but->bitnr);
878 widget_draw_icon(but, ICON_DOT, dualset?0:-100, rect);
881 /* If there's an icon too (made with uiDefIconTextBut) then draw the icon
882 and offset the text label to accomodate it */
884 if (but->flag & UI_HAS_ICON) {
885 widget_draw_icon(but, but->icon+but->iconadd, 0, rect);
887 rect->xmin += UI_icon_get_width(but->icon+but->iconadd);
889 if(but->editstr || (but->flag & UI_TEXT_LEFT))
892 else if(but->flag & UI_TEXT_LEFT)
895 /* always draw text for textbutton cursor */
896 widget_draw_text(fstyle, wcol, but, rect);
903 /* *********************** widget types ************************************* */
906 /* uiWidgetStateColors
908 char inner_anim_sel[4];
910 char inner_key_sel[4];
911 char inner_driven[4];
912 char inner_driven_sel[4];
917 static struct uiWidgetStateColors wcol_state= {
920 {240, 235, 100, 255},
936 float shadetop, shadedown;
939 static struct uiWidgetColors wcol_num= {
941 {180, 180, 180, 255},
942 {153, 153, 153, 255},
946 {255, 255, 255, 255},
952 static struct uiWidgetColors wcol_numslider= {
954 {180, 180, 180, 255},
955 {153, 153, 153, 255},
956 {128, 128, 128, 255},
959 {255, 255, 255, 255},
965 static struct uiWidgetColors wcol_text= {
967 {153, 153, 153, 255},
968 {153, 153, 153, 255},
972 {255, 255, 255, 255},
978 static struct uiWidgetColors wcol_option= {
982 {255, 255, 255, 255},
985 {255, 255, 255, 255},
991 /* button that shows popup */
992 static struct uiWidgetColors wcol_menu= {
996 {255, 255, 255, 255},
998 {255, 255, 255, 255},
999 {204, 204, 204, 255},
1005 /* button that starts pulldown */
1006 static struct uiWidgetColors wcol_pulldown= {
1009 {86, 128, 194, 255},
1010 {255, 255, 255, 255},
1019 /* button inside menu */
1020 static struct uiWidgetColors wcol_menu_item= {
1023 {86, 128, 194, 255},
1024 {255, 255, 255, 255},
1026 {255, 255, 255, 255},
1033 /* backdrop menu + title text color */
1034 static struct uiWidgetColors wcol_menu_back= {
1038 {100, 100, 100, 255},
1040 {255, 255, 255, 255},
1041 {255, 255, 255, 255},
1048 static struct uiWidgetColors wcol_radio= {
1051 {86, 128, 194, 255},
1052 {255, 255, 255, 255},
1054 {255, 255, 255, 255},
1061 static struct uiWidgetColors wcol_regular= {
1063 {153, 153, 153, 255},
1064 {100, 100, 100, 255},
1068 {255, 255, 255, 255},
1074 static struct uiWidgetColors wcol_tool= {
1076 {153, 153, 153, 255},
1077 {100, 100, 100, 255},
1081 {255, 255, 255, 255},
1087 static struct uiWidgetColors wcol_box= {
1089 {128, 128, 128, 255},
1090 {100, 100, 100, 255},
1094 {255, 255, 255, 255},
1100 static struct uiWidgetColors wcol_toggle= {
1102 {153, 153, 153, 255},
1103 {100, 100, 100, 255},
1107 {255, 255, 255, 255},
1113 static struct uiWidgetColors wcol_scroll= {
1116 {100, 100, 100, 180},
1117 {180, 180, 180, 255},
1120 {255, 255, 255, 255},
1126 /* free wcol struct to play with */
1127 static struct uiWidgetColors wcol_tmp= {
1129 {128, 128, 128, 255},
1130 {100, 100, 100, 255},
1134 {255, 255, 255, 255},
1141 /* called for theme init (new theme) and versions */
1142 void ui_widget_color_init(ThemeUI *tui)
1144 tui->wcol_regular= wcol_regular;
1145 tui->wcol_tool= wcol_tool;
1146 tui->wcol_text= wcol_text;
1147 tui->wcol_radio= wcol_radio;
1148 tui->wcol_option= wcol_option;
1149 tui->wcol_toggle= wcol_toggle;
1150 tui->wcol_num= wcol_num;
1151 tui->wcol_numslider= wcol_numslider;
1152 tui->wcol_menu= wcol_menu;
1153 tui->wcol_pulldown= wcol_pulldown;
1154 tui->wcol_menu_back= wcol_menu_back;
1155 tui->wcol_menu_item= wcol_menu_item;
1156 tui->wcol_box= wcol_box;
1157 tui->wcol_scroll= wcol_scroll;
1159 tui->wcol_state= wcol_state;
1162 /* ************ button callbacks, state ***************** */
1164 static void widget_state_blend(char *cp, char *cpstate, float fac)
1167 cp[0]= (int)((1.0f-fac)*cp[0] + fac*cpstate[0]);
1168 cp[1]= (int)((1.0f-fac)*cp[1] + fac*cpstate[1]);
1169 cp[2]= (int)((1.0f-fac)*cp[2] + fac*cpstate[2]);
1173 /* copy colors from theme, and set changes in it based on state */
1174 static void widget_state(uiWidgetType *wt, int state)
1176 uiWidgetStateColors *wcol_state= wt->wcol_state;
1178 wt->wcol= *(wt->wcol_theme);
1180 if(state & UI_SELECT) {
1181 QUATCOPY(wt->wcol.inner, wt->wcol.inner_sel)
1183 if(state & UI_BUT_ANIMATED_KEY)
1184 widget_state_blend(wt->wcol.inner, wcol_state->inner_key_sel, wcol_state->blend);
1185 else if(state & UI_BUT_ANIMATED)
1186 widget_state_blend(wt->wcol.inner, wcol_state->inner_anim_sel, wcol_state->blend);
1187 else if(state & UI_BUT_DRIVEN)
1188 widget_state_blend(wt->wcol.inner, wcol_state->inner_driven_sel, wcol_state->blend);
1190 VECCOPY(wt->wcol.text, wt->wcol.text_sel);
1192 /* only flip shade if it's not "pushed in" already */
1193 if(wt->wcol.shaded && wt->wcol.shadetop>wt->wcol.shadedown) {
1194 SWAP(short, wt->wcol.shadetop, wt->wcol.shadedown);
1198 if(state & UI_BUT_ANIMATED_KEY)
1199 widget_state_blend(wt->wcol.inner, wcol_state->inner_key, wcol_state->blend);
1200 else if(state & UI_BUT_ANIMATED)
1201 widget_state_blend(wt->wcol.inner, wcol_state->inner_anim, wcol_state->blend);
1202 else if(state & UI_BUT_DRIVEN)
1203 widget_state_blend(wt->wcol.inner, wcol_state->inner_driven, wcol_state->blend);
1205 if(state & UI_ACTIVE) { /* mouse over? */
1206 wt->wcol.inner[0]= wt->wcol.inner[0]>=240? 255 : wt->wcol.inner[0]+15;
1207 wt->wcol.inner[1]= wt->wcol.inner[1]>=240? 255 : wt->wcol.inner[1]+15;
1208 wt->wcol.inner[2]= wt->wcol.inner[2]>=240? 255 : wt->wcol.inner[2]+15;
1213 /* labels use theme colors for text */
1214 static void widget_state_label(uiWidgetType *wt, int state)
1216 /* call this for option button */
1217 widget_state(wt, state);
1219 if(state & UI_SELECT)
1220 UI_GetThemeColor4ubv(TH_TEXT_HI, wt->wcol.text);
1222 UI_GetThemeColor4ubv(TH_TEXT, wt->wcol.text);
1226 static void widget_state_nothing(uiWidgetType *wt, int state)
1228 wt->wcol= *(wt->wcol_theme);
1231 /* special case, button that calls pulldown */
1232 static void widget_state_pulldown(uiWidgetType *wt, int state)
1234 wt->wcol= *(wt->wcol_theme);
1236 QUATCOPY(wt->wcol.inner, wt->wcol.inner_sel);
1237 VECCOPY(wt->wcol.outline, wt->wcol.inner);
1239 if(state & UI_ACTIVE)
1240 VECCOPY(wt->wcol.text, wt->wcol.text_sel);
1243 /* special case, menu items */
1244 static void widget_state_menu_item(uiWidgetType *wt, int state)
1246 wt->wcol= *(wt->wcol_theme);
1248 if(state & (UI_BUT_DISABLED|UI_BUT_INACTIVE)) {
1249 wt->wcol.text[0]= 0.5f*(wt->wcol.text[0]+wt->wcol.text_sel[0]);
1250 wt->wcol.text[1]= 0.5f*(wt->wcol.text[1]+wt->wcol.text_sel[1]);
1251 wt->wcol.text[2]= 0.5f*(wt->wcol.text[2]+wt->wcol.text_sel[2]);
1253 else if(state & UI_ACTIVE) {
1254 QUATCOPY(wt->wcol.inner, wt->wcol.inner_sel);
1255 VECCOPY(wt->wcol.text, wt->wcol.text_sel);
1262 /* ************ menu backdrop ************************* */
1264 /* outside of rect, rad to left/bottom/right */
1265 static void widget_softshadow(rcti *rect, int roundboxalign, float radin, float radout)
1269 float alpha, alphastep;
1272 /* prevent tooltips to not show round shadow */
1273 if( 2.0f*radout > 0.2f*(rect1.ymax-rect1.ymin) )
1274 rect1.ymax -= 0.2f*(rect1.ymax-rect1.ymin);
1276 rect1.ymax -= 2.0f*radout;
1279 tot= round_box_shadow_edges(wtb.inner_v, &rect1, radin, roundboxalign & 12, 0.0f);
1281 /* inverse linear shadow alpha */
1285 for(step= 1; step<=radout; step++, alpha*=alphastep) {
1286 round_box_shadow_edges(wtb.outer_v, &rect1, radin, 15, (float)step);
1288 glColor4f(0.0f, 0.0f, 0.0f, alpha);
1290 glBegin(GL_QUAD_STRIP);
1291 for(a=0; a<tot; a++) {
1292 glVertex2fv(wtb.outer_v[a]);
1293 glVertex2fv(wtb.inner_v[a]);
1300 static void widget_menu_back(uiWidgetColors *wcol, rcti *rect, int flag, int direction)
1303 int roundboxalign= 15;
1307 /* menu is 2nd level or deeper */
1308 if (flag & UI_BLOCK_POPUP) {
1309 //rect->ymin -= 4.0;
1310 //rect->ymax += 4.0;
1312 else if (direction == UI_DOWN) {
1316 else if (direction == UI_TOP) {
1322 widget_softshadow(rect, roundboxalign, 5.0f, 8.0f);
1324 round_box_edges(&wtb, roundboxalign, rect, 5.0f);
1326 widgetbase_draw(&wtb, wcol);
1328 glDisable(GL_BLEND);
1332 static void ui_hsv_cursor(float x, float y)
1336 glTranslatef(x, y, 0.0f);
1338 glColor3f(1.0f, 1.0f, 1.0f);
1339 glutil_draw_filled_arc(0.0f, M_PI*2.0, 3.0f, 8);
1342 glEnable(GL_LINE_SMOOTH );
1343 glColor3f(0.0f, 0.0f, 0.0f);
1344 glutil_draw_lined_arc(0.0f, M_PI*2.0, 3.0f, 12);
1345 glDisable(GL_BLEND);
1346 glDisable(GL_LINE_SMOOTH );
1352 void ui_hsvcircle_vals_from_pos(float *valrad, float *valdist, rcti *rect, float mx, float my)
1354 /* duplication of code... well, simple is better now */
1355 float centx= (float)(rect->xmin + rect->xmax)/2;
1356 float centy= (float)(rect->ymin + rect->ymax)/2;
1359 if( rect->xmax-rect->xmin > rect->ymax-rect->ymin )
1360 radius= (float)(rect->ymax - rect->ymin)/2;
1362 radius= (float)(rect->xmax - rect->xmin)/2;
1366 dist= sqrt( mx*mx + my*my);
1368 *valdist= dist/radius;
1372 *valrad= atan2(mx, my)/(2.0f*M_PI) + 0.5f;
1375 void ui_draw_but_HSVCIRCLE(uiBut *but, rcti *rect)
1377 /* gouraud triangle fan */
1378 float radstep, ang= 0.0f;
1379 float centx, centy, radius;
1380 float hsv[3], col[3], colcent[3];
1383 radstep= 2.0f*M_PI/(float)tot;
1384 centx= (float)(rect->xmin + rect->xmax)/2;
1385 centy= (float)(rect->ymin + rect->ymax)/2;
1387 if( rect->xmax-rect->xmin > rect->ymax-rect->ymin )
1388 radius= (float)(rect->ymax - rect->ymin)/2;
1390 radius= (float)(rect->xmax - rect->xmin)/2;
1393 VECCOPY(hsv, but->hsv);
1394 hsv[0]= hsv[1]= 0.0f;
1395 hsv_to_rgb(hsv[0], hsv[1], hsv[2], colcent, colcent+1, colcent+2);
1397 glShadeModel(GL_SMOOTH);
1399 glBegin(GL_TRIANGLE_FAN);
1400 glColor3fv(colcent);
1401 glVertex2f( centx, centy);
1403 for(a=0; a<=tot; a++, ang+=radstep) {
1407 ui_hsvcircle_vals_from_pos(hsv, hsv+1, rect, centx + co*radius, centy + si*radius);
1408 hsv_to_rgb(hsv[0], hsv[1], hsv[2], col, col+1, col+2);
1410 glVertex2f( centx + co*radius, centy + si*radius);
1414 glShadeModel(GL_FLAT);
1416 /* fully rounded outline */
1418 glTranslatef(centx, centy, 0.0f);
1420 glEnable(GL_LINE_SMOOTH );
1421 glColor3f(0.0f, 0.0f, 0.0f);
1422 glutil_draw_lined_arc(0.0f, M_PI*2.0, radius, tot);
1423 glDisable(GL_BLEND);
1424 glDisable(GL_LINE_SMOOTH );
1428 ang= 2.0f*M_PI*but->hsv[0] + 0.5f*M_PI;
1429 radius= but->hsv[1]*radius;
1430 ui_hsv_cursor(centx + cos(-ang)*radius, centy + sin(-ang)*radius);
1434 /* ************ custom buttons, old stuff ************** */
1436 /* draws in resolution of 20x4 colors */
1437 static void ui_draw_but_HSVCUBE(uiBut *but, rcti *rect)
1441 float dx, dy, sx1, sx2, sy, x, y;
1442 float col0[4][3]; // left half, rect bottom to top
1443 float col1[4][3]; // right half, rect bottom to top
1449 /* draw series of gouraud rects */
1450 glShadeModel(GL_SMOOTH);
1452 if(but->a1==0) { // H and V vary
1453 hsv_to_rgb(0.0, s, 0.0, &col1[0][0], &col1[0][1], &col1[0][2]);
1454 hsv_to_rgb(0.0, s, 0.333, &col1[1][0], &col1[1][1], &col1[1][2]);
1455 hsv_to_rgb(0.0, s, 0.666, &col1[2][0], &col1[2][1], &col1[2][2]);
1456 hsv_to_rgb(0.0, s, 1.0, &col1[3][0], &col1[3][1], &col1[3][2]);
1459 else if(but->a1==1) { // H and S vary
1460 hsv_to_rgb(0.0, 0.0, v, &col1[0][0], &col1[0][1], &col1[0][2]);
1461 hsv_to_rgb(0.0, 0.333, v, &col1[1][0], &col1[1][1], &col1[1][2]);
1462 hsv_to_rgb(0.0, 0.666, v, &col1[2][0], &col1[2][1], &col1[2][2]);
1463 hsv_to_rgb(0.0, 1.0, v, &col1[3][0], &col1[3][1], &col1[3][2]);
1466 else if(but->a1==2) { // S and V vary
1467 hsv_to_rgb(h, 0.0, 0.0, &col1[0][0], &col1[0][1], &col1[0][2]);
1468 hsv_to_rgb(h, 0.333, 0.0, &col1[1][0], &col1[1][1], &col1[1][2]);
1469 hsv_to_rgb(h, 0.666, 0.0, &col1[2][0], &col1[2][1], &col1[2][2]);
1470 hsv_to_rgb(h, 1.0, 0.0, &col1[3][0], &col1[3][1], &col1[3][2]);
1473 else if(but->a1==3) { // only hue slider
1474 hsv_to_rgb(0.0, 1.0, 1.0, &col1[0][0], &col1[0][1], &col1[0][2]);
1475 VECCOPY(col1[1], col1[0]);
1476 VECCOPY(col1[2], col1[0]);
1477 VECCOPY(col1[3], col1[0]);
1481 for(dx=0.0; dx<1.0; dx+= 0.05) {
1483 VECCOPY(col0[0], col1[0]);
1484 VECCOPY(col0[1], col1[1]);
1485 VECCOPY(col0[2], col1[2]);
1486 VECCOPY(col0[3], col1[3]);
1489 if(but->a1==0) { // H and V vary
1490 hsv_to_rgb(dx, s, 0.0, &col1[0][0], &col1[0][1], &col1[0][2]);
1491 hsv_to_rgb(dx, s, 0.333, &col1[1][0], &col1[1][1], &col1[1][2]);
1492 hsv_to_rgb(dx, s, 0.666, &col1[2][0], &col1[2][1], &col1[2][2]);
1493 hsv_to_rgb(dx, s, 1.0, &col1[3][0], &col1[3][1], &col1[3][2]);
1495 else if(but->a1==1) { // H and S vary
1496 hsv_to_rgb(dx, 0.0, v, &col1[0][0], &col1[0][1], &col1[0][2]);
1497 hsv_to_rgb(dx, 0.333, v, &col1[1][0], &col1[1][1], &col1[1][2]);
1498 hsv_to_rgb(dx, 0.666, v, &col1[2][0], &col1[2][1], &col1[2][2]);
1499 hsv_to_rgb(dx, 1.0, v, &col1[3][0], &col1[3][1], &col1[3][2]);
1501 else if(but->a1==2) { // S and V vary
1502 hsv_to_rgb(h, 0.0, dx, &col1[0][0], &col1[0][1], &col1[0][2]);
1503 hsv_to_rgb(h, 0.333, dx, &col1[1][0], &col1[1][1], &col1[1][2]);
1504 hsv_to_rgb(h, 0.666, dx, &col1[2][0], &col1[2][1], &col1[2][2]);
1505 hsv_to_rgb(h, 1.0, dx, &col1[3][0], &col1[3][1], &col1[3][2]);
1507 else if(but->a1==3) { // only H
1508 hsv_to_rgb(dx, 1.0, 1.0, &col1[0][0], &col1[0][1], &col1[0][2]);
1509 VECCOPY(col1[1], col1[0]);
1510 VECCOPY(col1[2], col1[0]);
1511 VECCOPY(col1[3], col1[0]);
1515 sx1= rect->xmin + dx*(rect->xmax-rect->xmin);
1516 sx2= rect->xmin + (dx+0.05)*(rect->xmax-rect->xmin);
1518 dy= (rect->ymax-rect->ymin)/3.0;
1521 for(a=0; a<3; a++, sy+=dy) {
1522 glColor3fv(col0[a]);
1523 glVertex2f(sx1, sy);
1525 glColor3fv(col1[a]);
1526 glVertex2f(sx2, sy);
1528 glColor3fv(col1[a+1]);
1529 glVertex2f(sx2, sy+dy);
1531 glColor3fv(col0[a+1]);
1532 glVertex2f(sx1, sy+dy);
1537 glShadeModel(GL_FLAT);
1540 x= rect->xmin + x*(rect->xmax-rect->xmin);
1541 y= rect->ymin + y*(rect->ymax-rect->ymin);
1542 CLAMP(x, rect->xmin+3.0, rect->xmax-3.0);
1543 CLAMP(y, rect->ymin+3.0, rect->ymax-3.0);
1545 ui_hsv_cursor(x, y);
1548 glColor3ub(0, 0, 0);
1549 fdrawbox((rect->xmin), (rect->ymin), (rect->xmax), (rect->ymax));
1552 /* vertical 'value' slider, using new widget code */
1553 static void ui_draw_but_HSV_v(uiBut *but, rcti *rect)
1556 float rad= 0.5f*(rect->xmax - rect->xmin);
1562 round_box_edges(&wtb, 15, rect, rad);
1564 /* setup temp colors */
1565 wcol_tmp.outline[0]= wcol_tmp.outline[1]= wcol_tmp.outline[2]= 0;
1566 wcol_tmp.inner[0]= wcol_tmp.inner[1]= wcol_tmp.inner[2]= 128;
1567 wcol_tmp.shadetop= 127;
1568 wcol_tmp.shadedown= -128;
1571 widgetbase_draw(&wtb, &wcol_tmp);
1574 x= rect->xmin + 0.5f*(rect->xmax-rect->xmin);
1575 y= rect->ymin + but->hsv[2]*(rect->ymax-rect->ymin);
1576 CLAMP(y, rect->ymin+3.0, rect->ymax-3.0);
1578 ui_hsv_cursor(x, y);
1582 /* ************ button callbacks, draw ***************** */
1584 static void widget_numbut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1587 float rad= 0.5f*(rect->ymax - rect->ymin);
1592 round_box_edges(&wtb, roundboxalign, rect, rad);
1595 if(!(state & UI_TEXTINPUT)) {
1596 widget_num_tria(&wtb.tria1, rect, 0.6f, 'l');
1597 widget_num_tria(&wtb.tria2, rect, 0.6f, 'r');
1599 widgetbase_draw(&wtb, wcol);
1602 rect->xmin += (rect->ymax-rect->ymin);
1603 rect->xmax -= (rect->ymax-rect->ymin);
1608 static int ui_link_bezier_points(rcti *rect, float coord_array[][2], int resol)
1610 float dist, vec[4][2];
1612 vec[0][0]= rect->xmin;
1613 vec[0][1]= rect->ymin;
1614 vec[3][0]= rect->xmax;
1615 vec[3][1]= rect->ymax;
1617 dist= 0.5f*ABS(vec[0][0] - vec[3][0]);
1619 vec[1][0]= vec[0][0]+dist;
1620 vec[1][1]= vec[0][1];
1622 vec[2][0]= vec[3][0]-dist;
1623 vec[2][1]= vec[3][1];
1625 forward_diff_bezier(vec[0][0], vec[1][0], vec[2][0], vec[3][0], coord_array[0], resol, 2);
1626 forward_diff_bezier(vec[0][1], vec[1][1], vec[2][1], vec[3][1], coord_array[0]+1, resol, 2);
1631 #define LINK_RESOL 24
1632 void ui_draw_link_bezier(rcti *rect)
1634 float coord_array[LINK_RESOL+1][2];
1636 if(ui_link_bezier_points(rect, coord_array, LINK_RESOL)) {
1640 /* we can reuse the dist variable here to increment the GL curve eval amount*/
1641 dist = 1.0f/(float)LINK_RESOL;
1644 glEnable(GL_LINE_SMOOTH);
1646 glBegin(GL_LINE_STRIP);
1647 for(i=0; i<=LINK_RESOL; i++) {
1648 glVertex2fv(coord_array[i]);
1652 glDisable(GL_BLEND);
1653 glDisable(GL_LINE_SMOOTH);
1658 /* function in use for buttons and for view2d sliders */
1659 void uiWidgetScrollDraw(uiWidgetColors *wcol, rcti *rect, rcti *slider, int state)
1667 /* determine horizontal/vertical */
1668 horizontal= (rect->xmax - rect->xmin > rect->ymax - rect->ymin);
1671 rad= 0.5f*(rect->ymax - rect->ymin);
1673 rad= 0.5f*(rect->xmax - rect->xmin);
1675 wtb.shadedir= (horizontal)? 1: 0;
1677 /* draw back part, colors swapped and shading inverted */
1679 SWAP(short, wcol->shadetop, wcol->shadedown);
1681 round_box_edges(&wtb, 15, rect, rad);
1682 widgetbase_draw(&wtb, wcol);
1685 if(slider->xmax-slider->xmin<2 || slider->ymax-slider->ymin<2);
1688 SWAP(short, wcol->shadetop, wcol->shadedown);
1690 QUATCOPY(wcol->inner, wcol->item);
1692 if(wcol->shadetop>wcol->shadedown)
1693 wcol->shadetop+= 20; /* XXX violates themes... */
1694 else wcol->shadedown+= 20;
1696 if(state & UI_SCROLL_PRESSED)
1697 SWAP(short, wcol->shadetop, wcol->shadedown);
1700 wtb.emboss= 0; /* only emboss once */
1702 round_box_edges(&wtb, 15, slider, rad);
1704 if(state & UI_SCROLL_ARROWS) {
1705 if(wcol->item[0] > 48) wcol->item[0]-= 48;
1706 if(wcol->item[1] > 48) wcol->item[1]-= 48;
1707 if(wcol->item[2] > 48) wcol->item[2]-= 48;
1711 widget_num_tria(&wtb.tria1, slider, 0.6f, 'l');
1712 widget_num_tria(&wtb.tria2, slider, 0.6f, 'r');
1715 widget_num_tria(&wtb.tria1, slider, 0.6f, 'b');
1716 widget_num_tria(&wtb.tria2, slider, 0.6f, 't');
1719 widgetbase_draw(&wtb, wcol);
1723 static void widget_scroll(uiBut *but, uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1730 /* calculate slider part */
1731 value= ui_get_but_val(but);
1733 size= (but->softmax + but->a1 - but->softmin);
1734 size= MAX2(size, 2);
1739 /* determine horizontal/vertical */
1740 horizontal= (rect->xmax - rect->xmin > rect->ymax - rect->ymin);
1743 fac= (rect->xmax - rect->xmin)/(size);
1744 rect1.xmin= rect1.xmin + ceil(fac*(value - but->softmin));
1745 rect1.xmax= rect1.xmin + ceil(fac*(but->a1 - but->softmin));
1748 fac= (rect->ymax - rect->ymin)/(size);
1749 rect1.ymax= rect1.ymax - ceil(fac*(value - but->softmin));
1750 rect1.ymin= rect1.ymax - ceil(fac*(but->a1 - but->softmin));
1753 if(state & UI_SELECT)
1754 state= UI_SCROLL_PRESSED;
1757 uiWidgetScrollDraw(wcol, rect, &rect1, state);
1761 static void widget_link(uiBut *but, uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1764 if(but->flag & UI_SELECT) {
1767 UI_ThemeColor(TH_TEXT_HI);
1769 rectlink.xmin= (rect->xmin+rect->xmax)/2;
1770 rectlink.ymin= (rect->ymin+rect->ymax)/2;
1771 rectlink.xmax= but->linkto[0];
1772 rectlink.ymax= but->linkto[1];
1774 ui_draw_link_bezier(&rectlink);
1779 static void widget_numslider(uiBut *but, uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1781 uiWidgetBase wtb, wtb1;
1790 /* backdrop first */
1793 offs= 0.5f*(rect->ymax - rect->ymin);
1794 round_box_edges(&wtb, roundboxalign, rect, offs);
1797 widgetbase_draw(&wtb, wcol);
1800 VECCOPY(outline, wcol->outline);
1801 VECCOPY(wcol->outline, wcol->item);
1802 VECCOPY(wcol->inner, wcol->item);
1803 SWAP(short, wcol->shadetop, wcol->shadedown);
1807 value= ui_get_but_val(but);
1808 fac= (value-but->softmin)*(rect1.xmax - rect1.xmin - offs)/(but->softmax - but->softmin);
1810 /* left part of slider, always rounded */
1811 rect1.xmax= rect1.xmin + ceil(offs+1.0f);
1812 round_box_edges(&wtb1, roundboxalign & ~6, &rect1, offs);
1814 widgetbase_draw(&wtb1, wcol);
1816 /* right part of slider, interpolate roundness */
1817 rect1.xmax= rect1.xmin + fac + offs;
1818 rect1.xmin+= floor(offs-1.0f);
1819 if(rect1.xmax + offs > rect->xmax)
1820 offs*= (rect1.xmax + offs - rect->xmax)/offs;
1823 round_box_edges(&wtb1, roundboxalign & ~9, &rect1, offs);
1825 widgetbase_draw(&wtb1, wcol);
1826 VECCOPY(wcol->outline, outline);
1827 SWAP(short, wcol->shadetop, wcol->shadedown);
1832 widgetbase_draw(&wtb, wcol);
1835 rect->xmin += (rect->ymax-rect->ymin);
1836 rect->xmax -= (rect->ymax-rect->ymin);
1840 static void widget_swatch(uiBut *but, uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1848 round_box_edges(&wtb, roundboxalign, rect, 4.0f);
1850 ui_get_but_vectorf(but, col);
1851 wcol->inner[0]= FTOCHAR(col[0]);
1852 wcol->inner[1]= FTOCHAR(col[1]);
1853 wcol->inner[2]= FTOCHAR(col[2]);
1855 widgetbase_draw(&wtb, wcol);
1860 static void widget_textbut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1867 round_box_edges(&wtb, roundboxalign, rect, 5.0f);
1869 widgetbase_draw(&wtb, wcol);
1874 static void widget_menubut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1881 round_box_edges(&wtb, roundboxalign, rect, 4.0f);
1884 widget_menu_trias(&wtb.tria1, rect);
1886 widgetbase_draw(&wtb, wcol);
1889 rect->xmax -= (rect->ymax-rect->ymin);
1893 static void widget_pulldownbut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1895 if(state & UI_ACTIVE) {
1897 float rad= 0.5f*(rect->ymax - rect->ymin);
1902 round_box_edges(&wtb, roundboxalign, rect, rad);
1904 widgetbase_draw(&wtb, wcol);
1908 static void widget_menu_itembut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1914 /* not rounded, no outline */
1916 round_box_edges(&wtb, 0, rect, 0.0f);
1918 widgetbase_draw(&wtb, wcol);
1922 static void widget_optionbut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1925 rcti recttemp= *rect;
1931 recttemp.xmax= recttemp.xmin + (recttemp.ymax-recttemp.ymin);
1934 delta= 1 + (recttemp.ymax-recttemp.ymin)/8;
1935 recttemp.xmin+= delta;
1936 recttemp.ymin+= delta;
1937 recttemp.xmax-= delta;
1938 recttemp.ymax-= delta;
1941 round_box_edges(&wtb, 15, &recttemp, 4.0f);
1944 if(state & UI_SELECT) {
1945 widget_check_trias(&wtb.tria1, &recttemp);
1948 widgetbase_draw(&wtb, wcol);
1951 rect->xmin += (rect->ymax-rect->ymin)*0.7 + delta;
1955 static void widget_radiobut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1962 round_box_edges(&wtb, roundboxalign, rect, 4.0f);
1964 widgetbase_draw(&wtb, wcol);
1968 static void widget_but(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1975 round_box_edges(&wtb, roundboxalign, rect, 4.0f);
1977 widgetbase_draw(&wtb, wcol);
1981 static void widget_roundbut(uiWidgetColors *wcol, rcti *rect, int state, int roundboxalign)
1984 float rad= 0.5f*(rect->ymax - rect->ymin);
1989 round_box_edges(&wtb, roundboxalign, rect, rad);
1991 widgetbase_draw(&wtb, wcol);
1994 static void widget_draw_extra_mask(const bContext *C, uiBut *but, uiWidgetType *wt, rcti *rect)
2000 wt->wcol= *(wt->wcol_theme);
2004 if(but->block->drawextra) {
2005 /* note: drawextra can change rect +1 or -1, to match round errors of existing previews */
2006 but->block->drawextra(C, but->poin, rect);
2008 /* make mask to draw over image */
2009 UI_GetThemeColor3ubv(TH_BACK, col);
2012 round_box__edges(&wtb, 15, rect, 0.0f, 4.0);
2013 widgetbase_outline(&wtb);
2017 round_box_edges(&wtb, 15, rect, 5.0f);
2020 widgetbase_draw(&wtb, &wt->wcol);
2025 static void widget_disabled(rcti *rect)
2031 /* can't use theme TH_BACK or TH_PANEL... undefined */
2032 glGetFloatv(GL_COLOR_CLEAR_VALUE, col);
2033 glColor4f(col[0], col[1], col[2], 0.5f);
2034 /* need -1 and +1 to make it work right for aligned buttons,
2035 * but problem may be somewhere else? */
2036 glRectf(rect->xmin-1, rect->ymin, rect->xmax, rect->ymax+1);
2038 glDisable(GL_BLEND);
2041 static uiWidgetType *widget_type(uiWidgetTypeEnum type)
2043 bTheme *btheme= U.themes.first;
2044 static uiWidgetType wt;
2047 wt.wcol_theme= &btheme->tui.wcol_regular;
2048 wt.wcol_state= &btheme->tui.wcol_state;
2049 wt.state= widget_state;
2050 wt.draw= widget_but;
2052 wt.text= widget_draw_text_icon;
2055 case UI_WTYPE_REGULAR:
2058 case UI_WTYPE_LABEL:
2060 wt.state= widget_state_label;
2063 case UI_WTYPE_TOGGLE:
2064 wt.wcol_theme= &btheme->tui.wcol_toggle;
2067 case UI_WTYPE_OPTION:
2068 wt.wcol_theme= &btheme->tui.wcol_option;
2069 wt.draw= widget_optionbut;
2070 wt.state= widget_state_label;
2073 case UI_WTYPE_RADIO:
2074 wt.wcol_theme= &btheme->tui.wcol_radio;
2075 wt.draw= widget_radiobut;
2078 case UI_WTYPE_NUMBER:
2079 wt.wcol_theme= &btheme->tui.wcol_num;
2080 wt.draw= widget_numbut;
2083 case UI_WTYPE_SLIDER:
2084 wt.wcol_theme= &btheme->tui.wcol_numslider;
2085 wt.custom= widget_numslider;
2089 wt.wcol_theme= &btheme->tui.wcol_tool;
2090 wt.draw= widget_roundbut;
2096 wt.wcol_theme= &btheme->tui.wcol_text;
2097 wt.draw= widget_textbut;
2100 case UI_WTYPE_NAME_LINK:
2103 case UI_WTYPE_POINTER_LINK:
2106 case UI_WTYPE_FILENAME:
2111 case UI_WTYPE_MENU_RADIO:
2112 wt.wcol_theme= &btheme->tui.wcol_menu;
2113 wt.draw= widget_menubut;
2116 case UI_WTYPE_MENU_POINTER_LINK:
2117 wt.wcol_theme= &btheme->tui.wcol_menu;
2118 wt.draw= widget_menubut;
2122 case UI_WTYPE_PULLDOWN:
2123 wt.wcol_theme= &btheme->tui.wcol_pulldown;
2124 wt.draw= widget_pulldownbut;
2125 wt.state= widget_state_pulldown;
2129 case UI_WTYPE_MENU_ITEM:
2130 wt.wcol_theme= &btheme->tui.wcol_menu_item;
2131 wt.draw= widget_menu_itembut;
2132 wt.state= widget_state_menu_item;
2135 case UI_WTYPE_MENU_BACK:
2136 wt.wcol_theme= &btheme->tui.wcol_menu_back;
2137 wt.draw= widget_menu_back;
2145 case UI_WTYPE_SWATCH:
2146 wt.custom= widget_swatch;
2150 wt.wcol_theme= &btheme->tui.wcol_box;
2153 case UI_WTYPE_RGB_PICKER:
2156 case UI_WTYPE_NORMAL:
2159 case UI_WTYPE_SCROLL:
2160 wt.wcol_theme= &btheme->tui.wcol_scroll;
2161 wt.state= widget_state_nothing;
2162 wt.custom= widget_scroll;
2170 static int widget_roundbox_set(uiBut *but, rcti *rect)
2173 if(but->flag & UI_BUT_ALIGN) {
2175 if(but->flag & UI_BUT_ALIGN_TOP)
2177 if(but->flag & UI_BUT_ALIGN_LEFT)
2180 switch(but->flag & UI_BUT_ALIGN) {
2181 case UI_BUT_ALIGN_TOP:
2184 case UI_BUT_ALIGN_DOWN:
2187 case UI_BUT_ALIGN_LEFT:
2190 case UI_BUT_ALIGN_RIGHT:
2194 case UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_RIGHT:
2197 case UI_BUT_ALIGN_DOWN|UI_BUT_ALIGN_LEFT:
2200 case UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_RIGHT:
2203 case UI_BUT_ALIGN_TOP|UI_BUT_ALIGN_LEFT:
2215 /* conversion from old to new buttons, so still messy */
2216 void ui_draw_but(const bContext *C, ARegion *ar, uiStyle *style, uiBut *but, rcti *rect)
2218 bTheme *btheme= U.themes.first;
2219 ThemeUI *tui= &btheme->tui;
2220 uiFontStyle *fstyle= &style->widget;
2221 uiWidgetType *wt= NULL;
2223 /* handle menus seperately */
2224 if(but->dt==UI_EMBOSSP) {
2225 switch (but->type) {
2227 widget_draw_text_icon(&style->widgetlabel, &tui->wcol_menu_back, but, rect);
2233 wt= widget_type(UI_WTYPE_MENU_ITEM);
2236 else if(but->dt==UI_EMBOSSN) {
2238 wt= widget_type(UI_WTYPE_ICON);
2242 switch (but->type) {
2244 if(but->block->flag & UI_BLOCK_LOOP)
2245 widget_draw_text_icon(&style->widgetlabel, &tui->wcol_menu_back, but, rect);
2247 wt= widget_type(UI_WTYPE_LABEL);
2248 fstyle= &style->widgetlabel;
2256 wt= widget_type(UI_WTYPE_EXEC);
2260 wt= widget_type(UI_WTYPE_NUMBER);
2265 wt= widget_type(UI_WTYPE_SLIDER);
2269 wt= widget_type(UI_WTYPE_RADIO);
2273 wt= widget_type(UI_WTYPE_NAME);
2277 wt= widget_type(UI_WTYPE_NAME);
2278 if(but->block->flag & UI_BLOCK_LOOP)
2279 wt->wcol_theme= &btheme->tui.wcol_menu_back;
2286 wt= widget_type(UI_WTYPE_TOGGLE);
2291 if (!(but->flag & UI_HAS_ICON)) {
2292 wt= widget_type(UI_WTYPE_OPTION);
2293 but->flag |= UI_TEXT_LEFT;
2296 wt= widget_type(UI_WTYPE_TOGGLE);
2302 wt= widget_type(UI_WTYPE_MENU_RADIO);
2306 wt= widget_type(UI_WTYPE_PULLDOWN);
2310 wt= widget_type(UI_WTYPE_MENU_ITEM);
2314 wt= widget_type(UI_WTYPE_SWATCH);
2318 wt= widget_type(UI_WTYPE_BOX);
2323 wt= widget_type(UI_WTYPE_ICON);
2324 wt->custom= widget_link;
2329 widget_draw_extra_mask(C, but, widget_type(UI_WTYPE_BOX), rect);
2333 if(but->a1==4) // vertical V slider, uses new widget draw now
2334 ui_draw_but_HSV_v(but, rect);
2335 else // other HSV pickers...
2336 ui_draw_but_HSVCUBE(but, rect);
2340 ui_draw_but_HSVCIRCLE(but, rect);
2344 ui_draw_but_COLORBAND(but, &tui->wcol_regular, rect);
2348 ui_draw_but_NORMAL(but, &tui->wcol_regular, rect);
2352 ui_draw_but_CURVE(ar, but, &tui->wcol_regular, rect);
2356 wt= widget_type(UI_WTYPE_SCROLL);
2360 wt= widget_type(UI_WTYPE_REGULAR);
2365 rcti disablerect= *rect; /* rect gets clipped smaller for text */
2366 int roundboxalign, state;
2368 roundboxalign= widget_roundbox_set(but, rect);
2371 if(but->editstr) state |= UI_TEXTINPUT;
2373 wt->state(wt, state);
2375 wt->custom(but, &wt->wcol, rect, state, roundboxalign);
2377 wt->draw(&wt->wcol, rect, state, roundboxalign);
2378 wt->text(fstyle, &wt->wcol, but, rect);
2380 if(state & (UI_BUT_DISABLED|UI_BUT_INACTIVE))
2381 if(but->dt!=UI_EMBOSSP)
2382 widget_disabled(&disablerect);
2386 void ui_draw_menu_back(uiStyle *style, uiBlock *block, rcti *rect)
2388 uiWidgetType *wt= widget_type(UI_WTYPE_MENU_BACK);
2392 wt->draw(&wt->wcol, rect, block->flag, block->direction);
2394 wt->draw(&wt->wcol, rect, 0, 0);
2398 void ui_draw_search_back(uiStyle *style, uiBlock *block, rcti *rect)
2400 uiWidgetType *wt= widget_type(UI_WTYPE_BOX);
2403 widget_softshadow(rect, 15, 5.0f, 8.0f);
2404 glDisable(GL_BLEND);
2408 wt->draw(&wt->wcol, rect, block->flag, 15);
2410 wt->draw(&wt->wcol, rect, 0, 15);
2415 /* helper call to draw a menu item without button */
2416 /* state: UI_ACTIVE or 0 */
2417 void ui_draw_menu_item(uiFontStyle *fstyle, rcti *rect, char *name, int iconid, int state)
2419 uiWidgetType *wt= widget_type(UI_WTYPE_MENU_ITEM);
2423 wt->state(wt, state);
2424 wt->draw(&wt->wcol, rect, 0, 0);
2426 uiStyleFontSet(fstyle);
2427 fstyle->align= UI_STYLE_TEXT_LEFT;
2429 /* text location offset */
2431 if(iconid) rect->xmin+= ICON_HEIGHT;
2433 /* cut string in 2 parts? */
2434 cpoin= strchr(name, '|');
2437 rect->xmax -= BLF_width(cpoin+1) + 10;
2440 glColor3ubv(wt->wcol.text);
2441 uiStyleFontDraw(fstyle, rect, name);
2443 /* part text right aligned */
2445 fstyle->align= UI_STYLE_TEXT_RIGHT;
2446 rect->xmax= _rect.xmax - 5;
2447 uiStyleFontDraw(fstyle, rect, cpoin+1);
2451 /* restore rect, was messed with */
2455 int xs= rect->xmin+4;
2456 int ys= 1 + (rect->ymin+rect->ymax- ICON_HEIGHT)/2;
2458 UI_icon_draw_aspect_blended(xs, ys, iconid, 1.2f, 0); /* XXX scale weak get from fstyle? */
2459 glDisable(GL_BLEND);