2 #if defined(WIN32) || defined(__WIN32__)
8 #include "ui_exhibit.h"
19 static void draw_frame(const Rect &rect);
20 static void draw_titlebar(const Rect &rect);
21 static void draw_tabs(const Rect &rect);
22 static void draw_text(const Rect &rect);
23 static void layout_text(const char *text);
25 static struct dtx_font *font;
27 static unsigned int fontsdr;
30 static int ui_width, ui_height;
32 static RenderTarget *rtarg;
33 static const SceneNode *parent;
36 static int text_padding;
37 static float text_scale = 0.65f;
39 static int vis_tab, num_tabs;
40 static std::vector<std::string> tab_names;
42 static std::vector<const char*> text_lines;
43 static int max_line_size;
44 static AudioStream *voice;
46 enum {COL_BG, COL_FG, COL_FRM};
47 static float color[][3] = {
48 {0.014, 0.016, 0.04}, // COL_BG
49 {0.31, 0.58, 0.9}, // COL_FG
50 {0.19, 0.23, 0.46} // COL_FRM
56 if(!(font = dtx_open_font_glyphmap("data/ui_en.glyphmap"))) {
57 error_log("failed to open exhibit ui font\n");
60 font_size = dtx_get_glyphmap_ptsize(dtx_get_glyphmap(font, 0));
62 if(!(fontsdr = create_program_load("sdr/dfont.v.glsl", "sdr/dfont.p.glsl"))) {
63 error_log("failed to load font shader\n");
71 aspect = size.x / size.y;
73 ui_width = ui_height * aspect;
75 rtarg = new RenderTarget;
76 if(!rtarg->create(ui_width, ui_height, GL_RGBA)) {
77 error_log("failed to create exui render target\n");
90 void exui_setnode(const SceneNode *node)
95 void exui_change_tab(int dir)
97 vis_tab = (vis_tab + dir) % num_tabs;
100 void exui_scroll(float delta)
104 bool exui_raycast(const Ray &ray)
109 void exui_update(float dt)
111 if(exsel_active.ex != ex) {
112 ex = exsel_active.ex;
118 if(voice) voice->stop();
121 int num_data = ex->data.size();
122 for(int i=0; i<num_data; i++) {
123 if(ex->data[i].type == EXDATA_INFO) {
124 layout_text(ex->data[i].text.c_str());
125 voice = ex->data[i].voice;
127 tab_names.push_back("info");
132 voice->play(AUDIO_PLAYMODE_ONCE);
140 static void draw_2d_ui()
142 dtx_use_font(font, font_size);
143 float rowspc = dtx_line_height() * text_scale;
145 glMatrixMode(GL_PROJECTION);
148 glTranslatef(-1, 1, 0);
149 glScalef(2.0 / ui_width, -2.0 / ui_height, 1);
151 glMatrixMode(GL_MODELVIEW);
157 glPushAttrib(GL_ENABLE_BIT);
158 glDisable(GL_TEXTURE_2D);
159 glDisable(GL_LIGHTING);
160 glDisable(GL_DEPTH_TEST);
161 glEnable(GL_SCISSOR_TEST);
163 Rect rect = {0, 0, (float)ui_width, (float)ui_height};
165 Rect tbar_rect = {rect.x, rect.y, rect.w, rowspc + text_padding}; // half the padding
166 draw_titlebar(tbar_rect);
167 Rect tabs_rect = {tbar_rect.x, tbar_rect.y + tbar_rect.h, tbar_rect.w, tbar_rect.h};
168 draw_tabs(tabs_rect);
171 switch(ex->data[vis_tab].type) {
174 Rect text_rect = {rect.x, tabs_rect.y + tabs_rect.h, rect.w, rect.h - tabs_rect.y - tabs_rect.h};
175 draw_text(text_rect);
186 glMatrixMode(GL_PROJECTION);
188 glMatrixMode(GL_MODELVIEW);
194 if(!exsel_active) return;
197 // render the 2D UI in a texture
198 push_render_target(rtarg);
199 glClearColor(0, 1, 0, 0);
200 glClear(GL_COLOR_BUFFER_BIT);
204 // place UI image into the scene
205 glMatrixMode(GL_MODELVIEW);
209 glGetFloatv(GL_MODELVIEW_MATRIX, mvmat[0]);
211 mvmat = parent->get_matrix() * mvmat;
213 mvmat.translate(pos.x, pos.y, pos.z);
215 mvmat[0][0] = mvmat[1][1] = mvmat[2][2] = 1.0f;
216 mvmat[0][1] = mvmat[0][2] = mvmat[1][0] = mvmat[2][0] = mvmat[1][2] = mvmat[2][1] = 0.0f;
217 glLoadMatrixf(mvmat[0]);
219 glPushAttrib(GL_ENABLE_BIT);
221 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
222 glEnable(GL_TEXTURE_2D);
226 bind_texture(rtarg->texture());
228 glMatrixMode(GL_TEXTURE);
229 glLoadMatrixf(rtarg->texture_matrix()[0]);
233 glTexCoord2f(0, 0); glVertex2f(-size.x / 2, -size.y / 2);
234 glTexCoord2f(1, 0); glVertex2f(size.x / 2, -size.y / 2);
235 glTexCoord2f(1, 1); glVertex2f(size.x / 2, size.y / 2);
236 glTexCoord2f(0, 1); glVertex2f(-size.x / 2, size.y / 2);
244 glMatrixMode(GL_MODELVIEW);
248 static inline float *vrect(const Rect &rect, int i)
251 v[0] = ((i + 1) & 2) ? rect.x + rect.w : rect.x;
252 v[1] = (i & 2) ? rect.y : rect.y + rect.h;
256 static inline void draw_rect(const Rect &rect, int col)
259 glColor3fv(color[col]);
260 for(int i=0; i<4; i++)
261 glVertex2fv(vrect(rect, i));
265 static void clip_rect(const Rect &rect)
267 glScissor(rect.x, ui_height - rect.y - rect.h, rect.w, rect.h);
270 static void draw_frame(const Rect &rect)
274 draw_rect(rect, COL_BG);
276 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
277 draw_rect(rect, COL_FRM);
278 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
281 static void draw_titlebar(const Rect &rect)
285 draw_rect(rect, COL_FRM);
287 const char *title = ex->get_name();
289 glUseProgram(fontsdr);
292 glTranslatef(rect.x + text_padding, rect.y + rect.h - text_padding, 0);
293 glScalef(text_scale, -text_scale, text_scale);
295 glColor3fv(color[COL_BG]);
296 dtx_string(ex->get_name());
303 static void draw_tabs(const Rect &rect)
305 if(!num_tabs) return;
311 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
312 draw_rect(rect, COL_FRM);
313 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
316 int max_tab_size = ui_width / 2;
317 int tab_size = std::min(max_tab_size, ui_width / num_tabs);
319 for(int i=0; i<num_tabs; i++) {
320 Rect tr = {rect.x + i * tab_size, rect.y, (float)tab_size, rect.h};
325 draw_rect(tr, COL_FRM);
327 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
328 draw_rect(tr, COL_FRM);
329 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
333 glTranslatef(tr.x + text_padding, tr.y + tr.h - text_padding, 0);
334 glScalef(text_scale, -text_scale, text_scale);
336 glUseProgram(fontsdr);
337 glColor3fv(color[vis_tab == i ? COL_BG : COL_FRM]);
338 dtx_string(tab_names[i].c_str());
345 static void draw_text(const Rect &rect)
349 char *buf = (char*)alloca(max_line_size + 1);
351 float dy = dtx_line_height();
353 glUseProgram(fontsdr);
355 glMatrixMode(GL_MODELVIEW);
357 glTranslatef(rect.x + text_padding, rect.y + dy + text_padding, 0);
358 glScalef(text_scale, -text_scale, text_scale);
360 glColor3fv(color[COL_FG]);
362 int nlines = text_lines.size() - 1;
363 for(int i=0; i<nlines; i++) {
365 int sz = text_lines[i + 1] - text_lines[i];
366 assert(sz <= max_line_size);
367 memcpy(buf, text_lines[i], sz);
370 buf = (char*)text_lines[i];
373 dtx_position(0, -dy * i);
382 static void layout_text(const char *text)
388 dtx_use_font(font, font_size);
390 int left_margin = text_padding;
391 int right_margin = ui_width - text_padding;
393 text_lines.push_back(text);
394 const char *last_break = 0;
398 if(*text == '\n') { // paragraph break
399 text_lines.push_back(text);
400 text_lines.push_back(++text);
405 int code = dtx_utf8_char_code(text);
406 const char *next = dtx_utf8_next_char((char*)text);
409 dtx_substring_box(text_lines.back(), 0, text - text_lines.back(), &box);
410 float pos = left_margin + (box.width + box.x) * text_scale;
412 if(code < 256 && isspace(code)) {
416 if(pos > right_margin) {
417 if(text == text_lines.back()) {
418 // not even a single character fits on a line... abort
419 warning_log("text layout failed. glyph %d doesn't fit in line (%d)\n", code, right_margin - left_margin);
424 text_lines.push_back(last_break + 1);
427 // no good point to break, just break here
428 text_lines.push_back(text);
431 int d = text_lines.back() - (text_lines[text_lines.size() - 2]);
432 if(d > max_line_size) max_line_size = d;
436 text_lines.push_back(0);
439 debug_log("text layout:\n");
440 for(size_t i=0; i<text_lines.size() - 1; i++) {
441 const char *p = text_lines[i];
442 while(*p && p != text_lines[i + 1]) {