istereo2

view 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 source
1 #include <map>
2 #include <string>
3 #include "goatkit/goatkit.h"
4 #include "opengl.h"
5 #include "sanegl.h"
6 #include "drawtext.h"
7 #include "sdr.h"
9 #define BEVEL 1.0
10 #define VIS_THRES 0.005
12 using namespace goatkit;
14 struct Color {
15 float r, g, b, a;
17 Color() : r(1), g(1), b(1), a(1) {}
18 Color(float r, float g, float b, float a = 1.0f)
19 {
20 this->r = r;
21 this->g = g;
22 this->b = b;
23 this->a = a;
24 }
25 };
27 enum {
28 TEXT_COLOR,
29 TOP_COLOR,
30 BOTTOM_COLOR,
31 BEVEL_LIT_COLOR,
32 BEVEL_SHAD_COLOR,
33 CURSOR_COLOR,
34 SELECTION_COLOR,
35 CHECK_COLOR
36 };
38 static Color colors[] = {
39 Color(0.0, 0.0, 0.0, 1.0), /* text color */
40 Color(0.75, 0.75, 0.75, 1.0), /* top color */
41 Color(0.56, 0.56, 0.56, 1.0), /* bot color */
42 Color(0.8, 0.8, 0.8, 1.0), /* lit bevel */
43 Color(0.35, 0.35, 0.35, 1.0), /* shadowed bevel */
44 Color(0.8, 0.25, 0.18, 1.0), /* cursor color */
45 Color(0.68, 0.85, 1.3, 1.0), /* selection color */
46 Color(0.75, 0.1, 0.095, 1.0) /* check color */
47 };
50 #define JLEFT -1.0f
51 #define JCENTER 0.0f
52 #define JRIGHT 1.0f
54 enum { FRAME_INSET, FRAME_OUTSET };
56 extern int view_xsz, view_ysz;
57 extern float view_aspect;
58 extern unsigned int prog_ui, prog_font, prog_color;
59 extern struct dtx_font *font;
61 static void draw_label(const Widget *w);
62 static void draw_button(const Widget *w);
63 static void draw_rect(const Vec2 &pos, const Vec2 &sz, const Color &color);
64 static void draw_rect(const Vec2 &pos, const Vec2 &sz, const Color &ctop, const Color &cbot);
65 static void draw_text(float justify, float x, float y, const char *text);
66 static void draw_frame(const Vec2 &pos, const Vec2 &sz, float inset);
67 static Color lerp(const Color &a, const Color &b, float t);
69 static struct {
70 const char *name;
71 WidgetDrawFunc func;
72 } widget_funcs[] = {
73 { "label", draw_label },
74 { "button", draw_button },
75 {0, 0}
76 };
78 static std::map<std::string, WidgetDrawFunc> funcmap;
81 extern "C" __attribute__ ((used))
82 WidgetDrawFunc get_widget_func(const char *name)
83 {
84 static bool initialized;
86 if(!initialized) {
87 for(int i=0; widget_funcs[i].func; i++) {
88 funcmap[widget_funcs[i].name] = widget_funcs[i].func;
89 }
90 initialized = true;
91 }
92 return funcmap[name];
93 }
95 // register ourselves as a built-in theme
96 GOATKIT_BUILTIN_THEME("istereo", get_widget_func);
98 static void begin_drawing(const Widget *w)
99 {
100 //Vec2 pos = w->get_position();
102 glEnable(GL_BLEND);
103 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
105 gl_matrix_mode(GL_MODELVIEW);
106 gl_push_matrix();
107 gl_load_identity();
108 //gl_translatef(pos.x, pos.y, 0);
109 }
111 static void end_drawing(const Widget *w)
112 {
113 gl_matrix_mode(GL_MODELVIEW);
114 gl_pop_matrix();
115 }
117 static void draw_label(const Widget *w)
118 {
119 Vec2 pos = w->get_position();
120 float vis = w->get_visibility();
121 if(vis < VIS_THRES) return;
123 begin_drawing(w);
124 draw_text(JLEFT, pos.x, pos.y, w->get_text());
125 end_drawing(w);
126 }
128 static void draw_button(const Widget *w)
129 {
130 Vec2 pos = w->get_position();
131 Vec2 sz = w->get_size();
132 float vis = w->get_visibility();
133 float press = w->get_pressed();
134 if(vis < VIS_THRES) return;
136 Color tcol = lerp(colors[TOP_COLOR], colors[BOTTOM_COLOR], press);
137 Color bcol = lerp(colors[BOTTOM_COLOR], colors[TOP_COLOR], press);
139 begin_drawing(w);
141 draw_frame(pos, sz, press);
142 draw_rect(pos, sz, tcol, bcol);
143 //draw_rect(Vec2(pos.x + sz.x / 2.0 - 2.0, pos.y), Vec2(4.0, sz.y),
144 // Color(0.4, 0.5, 1.0));
145 draw_text(JCENTER, pos.x + sz.x / 2.0, pos.y, w->get_text());
147 end_drawing(w);
148 }
150 static void draw_rect(const Vec2 &pos, const Vec2 &sz, const Color &color)
151 {
152 draw_rect(pos, sz, color, color);
153 }
155 static void draw_rect(const Vec2 &pos, const Vec2 &sz, const Color &ctop, const Color &cbot)
156 {
157 float aspect = sz.x / sz.y;
159 bind_program(prog_color);
161 gl_begin(GL_QUADS);
162 gl_color4f(cbot.r, cbot.g, cbot.b, cbot.a);
163 gl_texcoord2f(0, 1);
164 gl_vertex3f(pos.x, pos.y, 0);
165 gl_texcoord2f(aspect, 1);
166 gl_vertex3f(pos.x + sz.x, pos.y, 0);
167 gl_color4f(ctop.r, ctop.g, ctop.b, ctop.a);
168 gl_texcoord2f(aspect, 0);
169 gl_vertex3f(pos.x + sz.x, pos.y + sz.y, 0);
170 gl_texcoord2f(0, 0);
171 gl_vertex3f(pos.x, pos.y + sz.y, 0);
172 gl_end();
173 }
175 static void draw_text(float justify, float x, float y, const char *text)
176 {
177 struct dtx_glyphmap *gmap = dtx_get_font_glyphmap_idx(font, 0);
178 dtx_use_font(font, dtx_get_glyphmap_ptsize(gmap));
180 float twidth = dtx_string_width(text);
181 float thalf = twidth / 2.0;
183 gl_matrix_mode(GL_MODELVIEW);
184 gl_push_matrix();
185 gl_load_identity();
186 gl_translatef(x - thalf - justify * thalf, y + 8, 0);
188 bind_program(prog_font);
189 set_uniform_float4(prog_font, "ucolor", 1.0, 1.0, 1.0, 1.0);
190 gl_apply_xform(prog_font);
192 dtx_string(text);
194 gl_matrix_mode(GL_MODELVIEW);
195 gl_pop_matrix();
196 }
198 static void draw_frame(const Vec2 &pos, const Vec2 &sz, float inset)
199 {
200 float x = pos.x - BEVEL;
201 float y = pos.y - BEVEL;
202 float w = sz.x + BEVEL * 2.0;
203 float h = sz.y + BEVEL * 2.0;
204 float b = BEVEL;
206 Color tcol = lerp(colors[BEVEL_LIT_COLOR], colors[BEVEL_SHAD_COLOR], inset);
207 Color bcol = lerp(colors[BEVEL_SHAD_COLOR], colors[BEVEL_LIT_COLOR], inset);
209 bind_program(prog_color);
211 gl_begin(GL_QUADS);
212 gl_color4f(tcol.r, tcol.g, tcol.b, tcol.a);
213 gl_vertex2f(x + b, y + h - b);
214 gl_vertex2f(x + w - b, y + h - b);
215 gl_vertex2f(x + w, y + h);
216 gl_vertex2f(x, y + h);
218 gl_vertex2f(x + b, y + b);
219 gl_vertex2f(x, y);
220 gl_vertex2f(x, y + h);
221 gl_vertex2f(x + b, y + h - b);
223 gl_color4f(bcol.r, bcol.g, bcol.b, bcol.a);
224 gl_vertex2f(x, y);
225 gl_vertex2f(x + b, y + b);
226 gl_vertex2f(x + w - b, y + b);
227 gl_vertex2f(x + w, y);
229 gl_vertex2f(x + w - b, y + b);
230 gl_vertex2f(x + w, y);
231 gl_vertex2f(x + w, y + h);
232 gl_vertex2f(x + w - b, y + h - b);
233 gl_end();
234 }
236 static Color lerp(const Color &a, const Color &b, float t)
237 {
238 Color res;
239 res.r = a.r + (b.r - a.r) * t;
240 res.g = a.g + (b.g - a.g) * t;
241 res.b = a.b + (b.b - a.b) * t;
242 return res;
243 }