+ if(!font) return;
+
+ // render the 2D UI in a texture
+ push_render_target(rtarg);
+ glClearColor(0, 1, 0, 0);
+ glClear(GL_COLOR_BUFFER_BIT);
+ draw_2d_ui();
+ pop_render_target();
+
+ // place UI image into the scene
+ glMatrixMode(GL_MODELVIEW);
+ glPushMatrix();
+
+ Mat4 mvmat;
+ glGetFloatv(GL_MODELVIEW_MATRIX, mvmat[0]);
+ if(parent) {
+ mvmat = parent->get_matrix() * mvmat;
+ }
+ mvmat.translate(pos.x, pos.y, pos.z);
+
+ mvmat[0][0] = mvmat[1][1] = mvmat[2][2] = 1.0f;
+ mvmat[0][1] = mvmat[0][2] = mvmat[1][0] = mvmat[2][0] = mvmat[1][2] = mvmat[2][1] = 0.0f;
+ glLoadMatrixf(mvmat[0]);
+
+ glPushAttrib(GL_ENABLE_BIT);
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE);
+ glEnable(GL_TEXTURE_2D);
+ glDepthMask(0);
+
+ glUseProgram(0);
+ bind_texture(rtarg->texture());
+
+ glMatrixMode(GL_TEXTURE);
+ glLoadMatrixf(rtarg->texture_matrix()[0]);
+
+ glBegin(GL_QUADS);
+ glColor3f(1, 1, 1);
+ glTexCoord2f(0, 0); glVertex2f(-size.x / 2, -size.y / 2);
+ glTexCoord2f(1, 0); glVertex2f(size.x / 2, -size.y / 2);
+ glTexCoord2f(1, 1); glVertex2f(size.x / 2, size.y / 2);
+ glTexCoord2f(0, 1); glVertex2f(-size.x / 2, size.y / 2);
+ glEnd();
+
+ glLoadIdentity();
+
+ glDepthMask(1);
+ glPopAttrib();
+
+ glMatrixMode(GL_MODELVIEW);
+ glPopMatrix();
+}
+
+static inline float *vrect(const Rect &rect, int i)
+{
+ static float v[2];
+ v[0] = ((i + 1) & 2) ? rect.x + rect.w : rect.x;
+ v[1] = (i & 2) ? rect.y : rect.y + rect.h;
+ return v;
+}
+
+static inline void draw_rect(const Rect &rect, int col)
+{
+ glBegin(GL_QUADS);
+ glColor3fv(color[col]);
+ for(int i=0; i<4; i++)
+ glVertex2fv(vrect(rect, i));
+ glEnd();
+}
+
+static void clip_rect(const Rect &rect)
+{
+ glScissor(rect.x, ui_height - rect.y - rect.h, rect.w, rect.h);
+}
+
+static void draw_frame(const Rect &rect)
+{
+ clip_rect(rect);
+
+ draw_rect(rect, COL_BG);
+ glLineWidth(3.0);
+ glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+ draw_rect(rect, COL_FRM);
+ glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
+}
+
+static void draw_titlebar(const Rect &rect)
+{
+ clip_rect(rect);
+
+ draw_rect(rect, COL_FRM);
+
+ const char *title = ex->get_name();
+ if(title) {
+ glUseProgram(fontsdr);
+
+ glPushMatrix();
+ glTranslatef(rect.x + text_padding, rect.y + rect.h - text_padding, 0);
+ glScalef(text_scale, -text_scale, text_scale);
+
+ glColor3fv(color[COL_BG]);
+ dtx_string(ex->get_name());
+ glPopMatrix();
+
+ glUseProgram(0);
+ }
+}
+
+static void draw_tabs(const Rect &rect)
+{
+ if(!num_tabs) return;
+
+ clip_rect(rect);
+
+ glLineWidth(1);
+ if(num_tabs == 1) {
+ glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+ draw_rect(rect, COL_FRM);
+ glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
+ }
+
+ int max_tab_size = ui_width / 2;
+ int tab_size = std::min(max_tab_size, ui_width / num_tabs);
+
+ for(int i=0; i<num_tabs; i++) {
+ Rect tr = {rect.x + i * tab_size, rect.y, (float)tab_size, rect.h};
+
+ clip_rect(tr);
+
+ if(vis_tab == i) {
+ draw_rect(tr, COL_FRM);
+ } else {
+ glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+ draw_rect(tr, COL_FRM);
+ glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
+ }
+
+ glPushMatrix();
+ glTranslatef(tr.x + text_padding, tr.y + tr.h - text_padding, 0);
+ glScalef(text_scale, -text_scale, text_scale);
+
+ glUseProgram(fontsdr);
+ glColor3fv(color[vis_tab == i ? COL_BG : COL_FRM]);
+ dtx_string(tab_names[i].c_str());
+ glUseProgram(0);
+
+ glPopMatrix();
+ }
+}
+
+static void draw_text(const Rect &rect)
+{
+ clip_rect(rect);
+
+ char *buf = (char*)alloca(max_line_size + 1);
+
+ float dy = dtx_line_height();
+
+ glUseProgram(fontsdr);
+
+ glMatrixMode(GL_MODELVIEW);
+ glPushMatrix();
+ glTranslatef(rect.x + text_padding, rect.y + dy + text_padding, 0);
+ glScalef(text_scale, -text_scale, text_scale);
+
+ glColor3fv(color[COL_FG]);
+
+ int nlines = text_lines.size() - 1;
+ for(int i=0; i<nlines; i++) {
+ if(i < nlines - 1) {
+ int sz = text_lines[i + 1] - text_lines[i];
+ assert(sz <= max_line_size);
+ memcpy(buf, text_lines[i], sz);
+ buf[sz] = 0;
+ } else {
+ buf = (char*)text_lines[i];
+ }
+
+ dtx_position(0, -dy * i);
+ dtx_string(buf);
+ }
+ dtx_position(0, 0);
+
+ glPopMatrix();
+ glUseProgram(0);
+}
+
+static void layout_text(const char *text)
+{
+ text_lines.clear();
+ if(!text) return;
+ if(!font) return;
+
+ dtx_use_font(font, font_size);
+
+ int left_margin = text_padding;
+ int right_margin = ui_width - text_padding;
+
+ text_lines.push_back(text);
+ const char *last_break = 0;
+ max_line_size = 1;
+
+ while(*text) {
+ if(*text == '\n') { // paragraph break
+ text_lines.push_back(text);
+ text_lines.push_back(++text);
+ last_break = 0;
+ continue;
+ }
+
+ int code = dtx_utf8_char_code(text);
+ const char *next = dtx_utf8_next_char((char*)text);
+
+ struct dtx_box box;
+ dtx_substring_box(text_lines.back(), 0, text - text_lines.back(), &box);
+ float pos = left_margin + (box.width + box.x) * text_scale;
+
+ if(code < 256 && isspace(code)) {
+ last_break = text;
+ }
+
+ if(pos > right_margin) {
+ if(text == text_lines.back()) {
+ // not even a single character fits on a line... abort
+ warning_log("text layout failed. glyph %d doesn't fit in line (%d)\n", code, right_margin - left_margin);
+ text_lines.clear();
+ return;
+ }
+ if(last_break) {
+ text_lines.push_back(last_break + 1);
+ last_break = 0;
+ } else {
+ // no good point to break, just break here
+ text_lines.push_back(text);
+ }
+
+ int d = text_lines.back() - (text_lines[text_lines.size() - 2]);
+ if(d > max_line_size) max_line_size = d;
+ }
+ text = next;
+ }
+ text_lines.push_back(0);