nasty bug in Texture::create
[laserbrain_demo] / src / ui_exhibit.cc
1 #include <drawtext.h>
2 #include "ui_exhibit.h"
3 #include "ui.h"
4 #include "app.h"
5 #include "snode.h"
6 #include "shader.h"
7 #include "rtarg.h"
8
9 struct Rect {
10         float x, y, w, h;
11 };
12
13 static void draw_frame(const Rect &rect);
14 static void draw_titlebar(const Rect &rect);
15 static void draw_tabs(const Rect &rect);
16 static void draw_text(const Rect &rect);
17 static void layout_text(const char *text);
18
19 static float aspect;
20 static int ui_width, ui_height;
21
22 static RenderTarget *rtarg;
23 static const SceneNode *parent;
24 static Vec3 pos;
25 static Vec2 size;
26 static int text_padding;
27 static float text_scale = 1.0f;//0.05f;
28 static Exhibit *ex;
29 static int vis_tab;
30 static float scroll;
31 static std::vector<const char*> text_lines;
32 static AudioStream *voice;
33
34 enum {COL_BG, COL_FG, COL_FRM};
35 static float color[][3] = {
36         {0.09, 0.14, 0.2},      // COL_BG
37         {0.31, 0.58, 0.9},      // COL_FG
38         {0.19, 0.23, 0.46}      // COL_FRM
39 };
40
41
42 bool exui_init()
43 {
44         size.x = 15;
45         size.y = 18;
46         text_padding = ui_width / 100;
47
48         aspect = size.x / size.y;
49         ui_height = 512;
50         ui_width = ui_height * aspect;
51
52         rtarg = new RenderTarget;
53         if(!rtarg->create(ui_width, ui_height, GL_RGB)) {
54                 error_log("failed to create exui render target\n");
55                 return false;
56         }
57
58         return true;
59 }
60
61 void exui_shutdown()
62 {
63         delete rtarg;
64 }
65
66 void exui_setnode(const SceneNode *node)
67 {
68         parent = node;
69 }
70
71 void exui_change_tab(int dir)
72 {
73 }
74
75 void exui_scroll(float delta)
76 {
77 }
78
79 bool exui_raycast(const Ray &ray)
80 {
81         return false;
82 }
83
84 void exui_update(float dt)
85 {
86         if(exsel_active.ex != ex) {
87                 ex = exsel_active.ex;
88                 scroll = 0.0f;
89                 vis_tab = 0;
90                 text_lines.clear();
91                 if(voice) voice->stop();
92
93                 if(ex) {
94                         int num_data = ex->data.size();
95                         for(int i=0; i<num_data; i++) {
96                                 if(ex->data[i].type == EXDATA_INFO) {
97                                         layout_text(ex->data[i].text.c_str());
98                                         voice = ex->data[i].voice;
99                                 }
100                         }
101
102                         if(voice) {
103                                 voice->play(AUDIO_PLAYMODE_ONCE);
104                         }
105                 } else {
106                         voice = 0;
107                 }
108         }
109 }
110
111 static void draw_2d_ui()
112 {
113         dtx_use_font(ui_font, ui_font_size);
114         float rowspc = dtx_line_height() * text_scale;
115
116         glMatrixMode(GL_PROJECTION);
117         glPushMatrix();
118         glLoadIdentity();
119         glOrtho(0, ui_width, 0, ui_height, -1, 1);
120
121         glMatrixMode(GL_MODELVIEW);
122         glPushMatrix();
123         glLoadIdentity();
124
125         bind_shader(0);
126
127         glPushAttrib(GL_ENABLE_BIT);
128         glDisable(GL_TEXTURE_2D);
129         glDisable(GL_LIGHTING);
130         glDisable(GL_DEPTH_TEST);
131
132         Rect rect = {0, 0, (float)ui_width, (float)ui_height};
133         draw_frame(rect);
134         Rect tbar_rect = {rect.x, rect.y, rect.w, rowspc};
135         draw_titlebar(tbar_rect);
136         Rect tabs_rect = {tbar_rect.x, tbar_rect.y + rowspc, tbar_rect.w, tbar_rect.h};
137         draw_tabs(tabs_rect);
138         Rect text_rect = {rect.x, tabs_rect.y + rowspc, rect.w, rect.h - tabs_rect.y - rowspc};
139         draw_text(text_rect);
140
141         glPopAttrib();
142
143         glMatrixMode(GL_PROJECTION);
144         glPopMatrix();
145         glMatrixMode(GL_MODELVIEW);
146         glPopMatrix();
147 }
148
149 void exui_draw()
150 {
151         if(!exsel_active) return;
152         if(!ui_font) return;
153
154         // render the 2D UI in a texture
155         push_render_target(rtarg);
156         glClearColor(0, 1, 0, 0);
157         glClear(GL_COLOR_BUFFER_BIT);
158         draw_2d_ui();
159         pop_render_target();
160
161         // place UI image into the scene
162         glMatrixMode(GL_MODELVIEW);
163         glPushMatrix();
164         if(parent) {
165                 glMultMatrixf(parent->get_matrix()[0]);
166         }
167         glTranslatef(pos.x, pos.y, pos.z);
168
169         glPushAttrib(GL_ENABLE_BIT);
170         glEnable(GL_BLEND);
171         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
172         glEnable(GL_TEXTURE_2D);
173         glDepthMask(0);
174
175         bind_shader(0);
176         bind_texture(rtarg->texture());
177
178         glMatrixMode(GL_TEXTURE);
179         glLoadMatrixf(rtarg->texture_matrix()[0]);
180
181         glBegin(GL_QUADS);
182         glColor3f(1, 1, 1);
183         glTexCoord2f(0, 0); glVertex2f(-size.x / 2, -size.y / 2);
184         glTexCoord2f(1, 0); glVertex2f(size.x / 2, -size.y / 2);
185         glTexCoord2f(1, 1); glVertex2f(size.x / 2, size.y / 2);
186         glTexCoord2f(0, 1); glVertex2f(-size.x / 2, size.y / 2);
187         glEnd();
188
189         glLoadIdentity();
190
191         glDepthMask(1);
192         glPopAttrib();
193
194         glMatrixMode(GL_MODELVIEW);
195         glPopMatrix();
196 }
197
198 static inline float *vrect(const Rect &rect, int i)
199 {
200         static float v[2];
201         v[0] = ((i + 1) & 2) ? rect.x + rect.w : rect.x;
202         v[1] = (i & 2) ? rect.y : rect.y + rect.h;
203         return v;
204 }
205
206 static inline void draw_rect(const Rect &rect, int col)
207 {
208         glBegin(GL_QUADS);
209         glColor3fv(color[col]);
210         for(int i=0; i<4; i++)
211                 glVertex2fv(vrect(rect, i));
212         glEnd();
213 }
214
215 static void draw_frame(const Rect &rect)
216 {
217         draw_rect(rect, COL_BG);
218         glLineWidth(2.0);
219         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
220         draw_rect(rect, COL_FRM);
221         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
222 }
223
224 static void draw_titlebar(const Rect &rect)
225 {
226         draw_rect(rect, COL_FRM);
227
228         const char *title = ex->get_name();
229         if(title) {
230                 glPushMatrix();
231                 glTranslatef(rect.x + text_padding, rect.y + rect.h - text_padding, 0);
232                 glScalef(text_scale, -text_scale, text_scale);
233
234                 glColor3fv(color[COL_BG]);
235                 dtx_string(ex->get_name());
236                 glPopMatrix();
237         }
238 }
239
240 static void draw_tabs(const Rect &rect)
241 {
242 }
243
244 static void draw_text(const Rect &rect)
245 {
246 }
247
248 static void layout_text(const char *text)
249 {
250         text_lines.clear();
251         if(!text) return;
252         if(!ui_font) return;
253
254         dtx_use_font(ui_font, ui_font_size);
255
256         float pos = text_padding;
257         text_lines.push_back(text);
258         const char *last_break = 0;
259
260         while(*text) {
261                 if(*text == '\n') {     // paragraph break
262                         text_lines.push_back(text);
263                         text_lines.push_back(++text);
264                         pos = text_padding;
265                         last_break = 0;
266                         continue;
267                 }
268
269                 int code = dtx_utf8_char_code(text);
270                 const char *next = dtx_utf8_next_char((char*)text);
271                 pos += dtx_glyph_width(code) * text_scale;
272
273                 if(code < 256 && isspace(code)) {
274                         last_break = text;
275                 }
276
277                 if(pos >= ui_width - text_padding) {
278                         if(text == text_lines.back()) {
279                                 // not even a single character fits on a line... abort
280                                 warning_log("text layout failed. glyph %d doesn't fit in line (%d)\n", code, ui_width - 2 * text_padding);
281                                 text_lines.clear();
282                                 return;
283                         }
284                         if(last_break) {
285                                 text_lines.push_back(last_break + 1);
286                                 last_break = 0;
287                         } else {
288                                 // no good point to break, just break here
289                                 text_lines.push_back(text);
290                         }
291                         pos = text_padding;
292                 }
293                 text = next;
294         }
295         text_lines.push_back(0);
296
297         /*
298         debug_log("text layout:\n");
299         for(size_t i=0; i<text_lines.size() - 1; i++) {
300                 const char *p = text_lines[i];
301                 while(*p && p != text_lines[i + 1]) {
302                         putchar(*p);
303                         ++p;
304                 }
305                 putchar('\n');
306         }
307         debug_log("---\n");
308         */
309 }