istereo2

view src/uitheme.cc @ 13:ea928c313344

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 28 Sep 2015 19:04:50 +0300
parents 57188f7d9304
children 018f997dc646
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 VIS_THRES 0.005
11 using namespace goatkit;
13 extern int view_xsz, view_ysz;
14 extern float view_aspect;
15 extern unsigned int prog_ui, prog_font, prog_color;
16 extern struct dtx_font *font;
18 static void draw_label(const Widget *w);
19 static void draw_button(const Widget *w);
20 static void draw_rect(const Vec2 &pos, const Vec2 &sz, float r, float g, float b, float a = 1.0f);
21 static void draw_text(float x, float y, const char *text);
23 static struct {
24 const char *name;
25 WidgetDrawFunc func;
26 } widget_funcs[] = {
27 { "label", draw_label },
28 { "button", draw_button },
29 {0, 0}
30 };
32 static std::map<std::string, WidgetDrawFunc> funcmap;
35 extern "C" __attribute__ ((used))
36 WidgetDrawFunc get_widget_func(const char *name)
37 {
38 static bool initialized;
40 if(!initialized) {
41 for(int i=0; widget_funcs[i].func; i++) {
42 funcmap[widget_funcs[i].name] = widget_funcs[i].func;
43 }
44 initialized = true;
45 }
46 return funcmap[name];
47 }
49 // register ourselves as a built-in theme
50 GOATKIT_BUILTIN_THEME("istereo", get_widget_func);
52 static void begin_drawing(const Widget *w)
53 {
54 Vec2 pos = w->get_position();
56 glEnable(GL_BLEND);
57 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
59 gl_matrix_mode(GL_MODELVIEW);
60 gl_push_matrix();
61 gl_load_identity();
62 gl_translatef(pos.x, pos.y, 0);
63 }
65 static void end_drawing(const Widget *w)
66 {
67 gl_matrix_mode(GL_MODELVIEW);
68 gl_pop_matrix();
69 }
71 static void draw_label(const Widget *w)
72 {
73 Vec2 pos = w->get_position();
74 float vis = w->get_visibility();
75 if(vis < VIS_THRES) return;
77 begin_drawing(w);
78 draw_text(pos.x, pos.y, w->get_text());
79 end_drawing(w);
80 }
82 static void draw_button(const Widget *w)
83 {
84 Vec2 pos = w->get_position();
85 Vec2 sz = w->get_size();
86 float vis = w->get_visibility();
87 if(vis < VIS_THRES) return;
89 begin_drawing(w);
91 draw_rect(pos, sz, 1.0, 0.3, 0.2);
92 draw_text(pos.x, pos.y, w->get_text());
94 end_drawing(w);
95 }
97 static void draw_rect(const Vec2 &pos, const Vec2 &sz, float r, float g, float b, float a)
98 {
99 float aspect = sz.x / sz.y;
101 bind_program(prog_color);
102 gl_apply_xform(prog_color);
104 gl_begin(GL_QUADS);
105 gl_color4f(r, g, b, a);
106 gl_texcoord2f(0, 1);
107 gl_vertex2f(pos.x, pos.y);
108 gl_texcoord2f(aspect, 1);
109 gl_vertex2f(pos.x + sz.x, pos.y);
110 gl_texcoord2f(aspect, 0);
111 gl_vertex2f(pos.x + sz.x, pos.y + sz.y);
112 gl_texcoord2f(0, 0);
113 gl_vertex2f(pos.x, pos.y + sz.y);
114 gl_end();
115 }
117 static void draw_text(float x, float y, const char *text)
118 {
119 struct dtx_glyphmap *gmap = dtx_get_font_glyphmap_idx(font, 0);
120 dtx_use_font(font, dtx_get_glyphmap_ptsize(gmap));
122 float virt_xsz = 420.0 * view_aspect;
123 float virt_ysz = 420.0;
125 gl_matrix_mode(GL_PROJECTION);
126 gl_push_matrix();
127 gl_load_identity();
128 gl_ortho(0, virt_xsz, 0, virt_ysz, -1, 1);
130 gl_matrix_mode(GL_MODELVIEW);
131 gl_push_matrix();
132 gl_load_identity();
133 gl_translatef(x * virt_xsz, y * virt_ysz, 0);
135 bind_program(prog_font);
136 set_uniform_float4(prog_font, "ucolor", 1.0, 1.0, 1.0, 1.0);
137 gl_apply_xform(prog_ui);
139 dtx_string(text);
141 gl_matrix_mode(GL_PROJECTION);
142 gl_pop_matrix();
143 gl_matrix_mode(GL_MODELVIEW);
144 gl_pop_matrix();
145 }