2 #include "ui_exhibit.h"
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);
20 static int ui_width, ui_height;
22 static RenderTarget *rtarg;
23 static const SceneNode *parent;
26 static int text_padding;
27 static float text_scale = 1.0f;//0.05f;
31 static std::vector<const char*> text_lines;
32 static AudioStream *voice;
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
46 text_padding = ui_width / 100;
48 aspect = size.x / size.y;
50 ui_width = ui_height * aspect;
52 rtarg = new RenderTarget;
53 if(!rtarg->create(ui_width, ui_height, GL_RGB)) {
54 error_log("failed to create exui render target\n");
66 void exui_setnode(const SceneNode *node)
71 void exui_change_tab(int dir)
75 void exui_scroll(float delta)
79 bool exui_raycast(const Ray &ray)
84 void exui_update(float dt)
86 if(exsel_active.ex != ex) {
91 if(voice) voice->stop();
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;
103 voice->play(AUDIO_PLAYMODE_ONCE);
111 static void draw_2d_ui()
113 dtx_use_font(ui_font, ui_font_size);
114 float rowspc = dtx_line_height() * text_scale;
116 glMatrixMode(GL_PROJECTION);
119 glOrtho(0, ui_width, 0, ui_height, -1, 1);
121 glMatrixMode(GL_MODELVIEW);
127 glPushAttrib(GL_ENABLE_BIT);
128 glDisable(GL_TEXTURE_2D);
129 glDisable(GL_LIGHTING);
130 glDisable(GL_DEPTH_TEST);
132 Rect rect = {0, 0, (float)ui_width, (float)ui_height};
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);
143 glMatrixMode(GL_PROJECTION);
145 glMatrixMode(GL_MODELVIEW);
151 if(!exsel_active) return;
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);
161 // place UI image into the scene
162 glMatrixMode(GL_MODELVIEW);
165 glMultMatrixf(parent->get_matrix()[0]);
167 glTranslatef(pos.x, pos.y, pos.z);
169 glPushAttrib(GL_ENABLE_BIT);
171 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
172 glEnable(GL_TEXTURE_2D);
176 bind_texture(rtarg->texture());
178 glMatrixMode(GL_TEXTURE);
179 glLoadMatrixf(rtarg->texture_matrix()[0]);
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);
194 glMatrixMode(GL_MODELVIEW);
198 static inline float *vrect(const Rect &rect, int i)
201 v[0] = ((i + 1) & 2) ? rect.x + rect.w : rect.x;
202 v[1] = (i & 2) ? rect.y : rect.y + rect.h;
206 static inline void draw_rect(const Rect &rect, int col)
209 glColor3fv(color[col]);
210 for(int i=0; i<4; i++)
211 glVertex2fv(vrect(rect, i));
215 static void draw_frame(const Rect &rect)
217 draw_rect(rect, COL_BG);
219 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
220 draw_rect(rect, COL_FRM);
221 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
224 static void draw_titlebar(const Rect &rect)
226 draw_rect(rect, COL_FRM);
228 const char *title = ex->get_name();
231 glTranslatef(rect.x + text_padding, rect.y + rect.h - text_padding, 0);
232 glScalef(text_scale, -text_scale, text_scale);
234 glColor3fv(color[COL_BG]);
235 dtx_string(ex->get_name());
240 static void draw_tabs(const Rect &rect)
244 static void draw_text(const Rect &rect)
248 static void layout_text(const char *text)
254 dtx_use_font(ui_font, ui_font_size);
256 float pos = text_padding;
257 text_lines.push_back(text);
258 const char *last_break = 0;
261 if(*text == '\n') { // paragraph break
262 text_lines.push_back(text);
263 text_lines.push_back(++text);
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;
273 if(code < 256 && isspace(code)) {
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);
285 text_lines.push_back(last_break + 1);
288 // no good point to break, just break here
289 text_lines.push_back(text);
295 text_lines.push_back(0);
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]) {