istereo2
diff src/uitheme.cc @ 14:018f997dc646
button done
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 29 Sep 2015 01:11:54 +0300 |
parents | ea928c313344 |
children | 7bd4264bf74a |
line diff
1.1 --- a/src/uitheme.cc Mon Sep 28 19:04:50 2015 +0300 1.2 +++ b/src/uitheme.cc Tue Sep 29 01:11:54 2015 +0300 1.3 @@ -6,10 +6,53 @@ 1.4 #include "drawtext.h" 1.5 #include "sdr.h" 1.6 1.7 +#define BEVEL 1.0 1.8 #define VIS_THRES 0.005 1.9 1.10 using namespace goatkit; 1.11 1.12 +struct Color { 1.13 + float r, g, b, a; 1.14 + 1.15 + Color() : r(1), g(1), b(1), a(1) {} 1.16 + Color(float r, float g, float b, float a = 1.0f) 1.17 + { 1.18 + this->r = r; 1.19 + this->g = g; 1.20 + this->b = b; 1.21 + this->a = a; 1.22 + } 1.23 +}; 1.24 + 1.25 +enum { 1.26 + TEXT_COLOR, 1.27 + TOP_COLOR, 1.28 + BOTTOM_COLOR, 1.29 + BEVEL_LIT_COLOR, 1.30 + BEVEL_SHAD_COLOR, 1.31 + CURSOR_COLOR, 1.32 + SELECTION_COLOR, 1.33 + CHECK_COLOR 1.34 +}; 1.35 + 1.36 +static Color colors[] = { 1.37 + Color(0.0, 0.0, 0.0, 1.0), /* text color */ 1.38 + Color(0.75, 0.75, 0.75, 1.0), /* top color */ 1.39 + Color(0.56, 0.56, 0.56, 1.0), /* bot color */ 1.40 + Color(0.8, 0.8, 0.8, 1.0), /* lit bevel */ 1.41 + Color(0.35, 0.35, 0.35, 1.0), /* shadowed bevel */ 1.42 + Color(0.8, 0.25, 0.18, 1.0), /* cursor color */ 1.43 + Color(0.68, 0.85, 1.3, 1.0), /* selection color */ 1.44 + Color(0.75, 0.1, 0.095, 1.0) /* check color */ 1.45 +}; 1.46 + 1.47 + 1.48 +#define JLEFT -1.0f 1.49 +#define JCENTER 0.0f 1.50 +#define JRIGHT 1.0f 1.51 + 1.52 +enum { FRAME_INSET, FRAME_OUTSET }; 1.53 + 1.54 extern int view_xsz, view_ysz; 1.55 extern float view_aspect; 1.56 extern unsigned int prog_ui, prog_font, prog_color; 1.57 @@ -17,8 +60,11 @@ 1.58 1.59 static void draw_label(const Widget *w); 1.60 static void draw_button(const Widget *w); 1.61 -static void draw_rect(const Vec2 &pos, const Vec2 &sz, float r, float g, float b, float a = 1.0f); 1.62 -static void draw_text(float x, float y, const char *text); 1.63 +static void draw_rect(const Vec2 &pos, const Vec2 &sz, const Color &color); 1.64 +static void draw_rect(const Vec2 &pos, const Vec2 &sz, const Color &ctop, const Color &cbot); 1.65 +static void draw_text(float justify, float x, float y, const char *text); 1.66 +static void draw_frame(const Vec2 &pos, const Vec2 &sz, float inset); 1.67 +static Color lerp(const Color &a, const Color &b, float t); 1.68 1.69 static struct { 1.70 const char *name; 1.71 @@ -51,7 +97,7 @@ 1.72 1.73 static void begin_drawing(const Widget *w) 1.74 { 1.75 - Vec2 pos = w->get_position(); 1.76 + //Vec2 pos = w->get_position(); 1.77 1.78 glEnable(GL_BLEND); 1.79 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 1.80 @@ -59,7 +105,7 @@ 1.81 gl_matrix_mode(GL_MODELVIEW); 1.82 gl_push_matrix(); 1.83 gl_load_identity(); 1.84 - gl_translatef(pos.x, pos.y, 0); 1.85 + //gl_translatef(pos.x, pos.y, 0); 1.86 } 1.87 1.88 static void end_drawing(const Widget *w) 1.89 @@ -75,7 +121,7 @@ 1.90 if(vis < VIS_THRES) return; 1.91 1.92 begin_drawing(w); 1.93 - draw_text(pos.x, pos.y, w->get_text()); 1.94 + draw_text(JLEFT, pos.x, pos.y, w->get_text()); 1.95 end_drawing(w); 1.96 } 1.97 1.98 @@ -84,62 +130,114 @@ 1.99 Vec2 pos = w->get_position(); 1.100 Vec2 sz = w->get_size(); 1.101 float vis = w->get_visibility(); 1.102 + float press = w->get_pressed(); 1.103 if(vis < VIS_THRES) return; 1.104 1.105 + Color tcol = lerp(colors[TOP_COLOR], colors[BOTTOM_COLOR], press); 1.106 + Color bcol = lerp(colors[BOTTOM_COLOR], colors[TOP_COLOR], press); 1.107 + 1.108 begin_drawing(w); 1.109 1.110 - draw_rect(pos, sz, 1.0, 0.3, 0.2); 1.111 - draw_text(pos.x, pos.y, w->get_text()); 1.112 + draw_frame(pos, sz, press); 1.113 + draw_rect(pos, sz, tcol, bcol); 1.114 + //draw_rect(Vec2(pos.x + sz.x / 2.0 - 2.0, pos.y), Vec2(4.0, sz.y), 1.115 + // Color(0.4, 0.5, 1.0)); 1.116 + draw_text(JCENTER, pos.x + sz.x / 2.0, pos.y, w->get_text()); 1.117 1.118 end_drawing(w); 1.119 } 1.120 1.121 -static void draw_rect(const Vec2 &pos, const Vec2 &sz, float r, float g, float b, float a) 1.122 +static void draw_rect(const Vec2 &pos, const Vec2 &sz, const Color &color) 1.123 +{ 1.124 + draw_rect(pos, sz, color, color); 1.125 +} 1.126 + 1.127 +static void draw_rect(const Vec2 &pos, const Vec2 &sz, const Color &ctop, const Color &cbot) 1.128 { 1.129 float aspect = sz.x / sz.y; 1.130 1.131 bind_program(prog_color); 1.132 - gl_apply_xform(prog_color); 1.133 1.134 gl_begin(GL_QUADS); 1.135 - gl_color4f(r, g, b, a); 1.136 + gl_color4f(cbot.r, cbot.g, cbot.b, cbot.a); 1.137 gl_texcoord2f(0, 1); 1.138 - gl_vertex2f(pos.x, pos.y); 1.139 + gl_vertex3f(pos.x, pos.y, 0); 1.140 gl_texcoord2f(aspect, 1); 1.141 - gl_vertex2f(pos.x + sz.x, pos.y); 1.142 + gl_vertex3f(pos.x + sz.x, pos.y, 0); 1.143 + gl_color4f(ctop.r, ctop.g, ctop.b, ctop.a); 1.144 gl_texcoord2f(aspect, 0); 1.145 - gl_vertex2f(pos.x + sz.x, pos.y + sz.y); 1.146 + gl_vertex3f(pos.x + sz.x, pos.y + sz.y, 0); 1.147 gl_texcoord2f(0, 0); 1.148 - gl_vertex2f(pos.x, pos.y + sz.y); 1.149 + gl_vertex3f(pos.x, pos.y + sz.y, 0); 1.150 gl_end(); 1.151 } 1.152 1.153 -static void draw_text(float x, float y, const char *text) 1.154 +static void draw_text(float justify, float x, float y, const char *text) 1.155 { 1.156 struct dtx_glyphmap *gmap = dtx_get_font_glyphmap_idx(font, 0); 1.157 dtx_use_font(font, dtx_get_glyphmap_ptsize(gmap)); 1.158 1.159 - float virt_xsz = 420.0 * view_aspect; 1.160 - float virt_ysz = 420.0; 1.161 - 1.162 - gl_matrix_mode(GL_PROJECTION); 1.163 - gl_push_matrix(); 1.164 - gl_load_identity(); 1.165 - gl_ortho(0, virt_xsz, 0, virt_ysz, -1, 1); 1.166 + float twidth = dtx_string_width(text); 1.167 + float thalf = twidth / 2.0; 1.168 1.169 gl_matrix_mode(GL_MODELVIEW); 1.170 gl_push_matrix(); 1.171 gl_load_identity(); 1.172 - gl_translatef(x * virt_xsz, y * virt_ysz, 0); 1.173 + gl_translatef(x - thalf - justify * thalf, y + 8, 0); 1.174 1.175 bind_program(prog_font); 1.176 set_uniform_float4(prog_font, "ucolor", 1.0, 1.0, 1.0, 1.0); 1.177 - gl_apply_xform(prog_ui); 1.178 + gl_apply_xform(prog_font); 1.179 1.180 dtx_string(text); 1.181 1.182 - gl_matrix_mode(GL_PROJECTION); 1.183 - gl_pop_matrix(); 1.184 gl_matrix_mode(GL_MODELVIEW); 1.185 gl_pop_matrix(); 1.186 } 1.187 + 1.188 +static void draw_frame(const Vec2 &pos, const Vec2 &sz, float inset) 1.189 +{ 1.190 + float x = pos.x - BEVEL; 1.191 + float y = pos.y - BEVEL; 1.192 + float w = sz.x + BEVEL * 2.0; 1.193 + float h = sz.y + BEVEL * 2.0; 1.194 + float b = BEVEL; 1.195 + 1.196 + Color tcol = lerp(colors[BEVEL_LIT_COLOR], colors[BEVEL_SHAD_COLOR], inset); 1.197 + Color bcol = lerp(colors[BEVEL_SHAD_COLOR], colors[BEVEL_LIT_COLOR], inset); 1.198 + 1.199 + bind_program(prog_color); 1.200 + 1.201 + gl_begin(GL_QUADS); 1.202 + gl_color4f(tcol.r, tcol.g, tcol.b, tcol.a); 1.203 + gl_vertex2f(x + b, y + h - b); 1.204 + gl_vertex2f(x + w - b, y + h - b); 1.205 + gl_vertex2f(x + w, y + h); 1.206 + gl_vertex2f(x, y + h); 1.207 + 1.208 + gl_vertex2f(x + b, y + b); 1.209 + gl_vertex2f(x, y); 1.210 + gl_vertex2f(x, y + h); 1.211 + gl_vertex2f(x + b, y + h - b); 1.212 + 1.213 + gl_color4f(bcol.r, bcol.g, bcol.b, bcol.a); 1.214 + gl_vertex2f(x, y); 1.215 + gl_vertex2f(x + b, y + b); 1.216 + gl_vertex2f(x + w - b, y + b); 1.217 + gl_vertex2f(x + w, y); 1.218 + 1.219 + gl_vertex2f(x + w - b, y + b); 1.220 + gl_vertex2f(x + w, y); 1.221 + gl_vertex2f(x + w, y + h); 1.222 + gl_vertex2f(x + w - b, y + h - b); 1.223 + gl_end(); 1.224 +} 1.225 + 1.226 +static Color lerp(const Color &a, const Color &b, float t) 1.227 +{ 1.228 + Color res; 1.229 + res.r = a.r + (b.r - a.r) * t; 1.230 + res.g = a.g + (b.g - a.g) * t; 1.231 + res.b = a.b + (b.b - a.b) * t; 1.232 + return res; 1.233 +}