#include "draw.h" #include "Rectangle.h" #include #include #include #include static void drawGrid(const Rectangle &rect, float scale) { float depth = log10f(scale)-0.75f; float depth2 = floorf(depth); float step = powf(0.1f, depth2); float f; int i; //gchar *string; float x1 = rect.getVertex1().getX(), y1 = rect.getVertex1().getY(); float x2 = rect.getVertex2().getX(), y2 = rect.getVertex2().getY(); glBegin(GL_LINES); //cairo_set_font_size(cr, 10.0/scale); for(i = 0; 0.4f*(depth-depth2+i-1) < 0.5f; i++) { f = fminf(0.4f*(depth-depth2+i), 0.5f); glColor3f(f, f, f); for(f = x1 - fmodf(x1, step) - step; f <= x2; f+=step) { glVertex2f(f, y1); glVertex2f(f, y2); /*if(step > 0.005) { if(step > 0.5) string = g_strdup_printf("%i", (int)rint(d)); else string = g_strdup_printf("%.*f", -(int)floor(log10(step*1.1)), d+step/10); cairo_move_to(cr, d+1/scale, y1+11/scale); cairo_show_text(cr, string); g_free(string); }*/ } for(f = y1 - fmodf(y1, step) - step; f <= y2; f+=step) { glVertex2f(x1, f); glVertex2f(x2, f); /*if(step > 0.005) { if(step > 0.5) string = g_strdup_printf("%i", (int)rint(d)); else string = g_strdup_printf("%.*f", -(int)floor(log10(step*1.1)), d+step/10); cairo_move_to(cr, x1+3/scale, d+11/scale); cairo_show_text(cr, string); g_free(string); }*/ } step *= 10; } glEnd(); } static void fillPolygon(const Polygon &polygon) { std::vector triangles; polygon.triangulate(triangles); glBegin(GL_TRIANGLES); for(std::vector::iterator t = triangles.begin(); t != triangles.end(); t++) { glVertex2f(t->getVertexA().getX(), t->getVertexA().getY()); glVertex2f(t->getVertexB().getX(), t->getVertexB().getY()); glVertex2f(t->getVertexC().getX(), t->getVertexC().getY()); } glEnd(); } static void drawPolygon(const Polygon &polygon, bool close) { glBegin(GL_LINE_STRIP); for(Polygon::const_iterator vertex = polygon.begin(); vertex != polygon.end(); vertex++) glVertex2d(vertex->getX(), vertex->getY()); if(close) glVertex2d(polygon.front().getX(), polygon.front().getY()); glEnd(); } gboolean drawTopView(GtkWidget *widget, GdkEventExpose *event, Window *window, Drawer *drawer) { GdkGLContext *context = gtk_widget_get_gl_context(widget); GdkGLDrawable *drawable = gtk_widget_get_gl_drawable(widget); Vertex v1(0, 0), v2(widget->allocation.width, widget->allocation.height); Rectangle rect; drawer->viewToImage(&v1); drawer->viewToImage(&v2); rect.setVertex1(v1); rect.setVertex2(v2); if(!gdk_gl_drawable_gl_begin(drawable, context)) return FALSE; glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLineWidth(1.0f); glTranslatef(drawer->getImageWidth()/2-drawer->getXTranslate(), drawer->getImageHeight()/2-drawer->getYTranslate(), 0); glScalef(drawer->getScale(), drawer->getScale(), 1); drawGrid(rect, drawer->getScale()); for(Level::const_iterator room = window->getLevel().begin(); room != window->getLevel().end(); room++) { if(&*room == window->getEditManager().getActiveRoom() && window->getEditManager().getMode() == EditManager::ADD) continue; if(&*room == window->getEditManager().getActiveRoom()) glColor4f(0.0f, 0.7f, 1.0f, 0.2f); else glColor4f(0.0f, 0.7f, 1.0f, 0.3f); fillPolygon(*room); if(&*room == window->getEditManager().getActiveRoom()) { glColor4f(1.0f, 1.0f, 1.0f, 0.9f); glLineWidth(2.0f); } else if(&*room == window->getEditManager().getHoveredRoom() && window->getEditManager().getMode() == EditManager::VIEW) { glColor4f(0.0f, 0.7f, 1.0f, 0.7f); glLineWidth(2.0f); } else { glColor4f(0.0f, 0.7f, 1.0f, 0.7f); glLineWidth(1.0f); } drawPolygon(*room, true); } if(window->getEditManager().getMode() == EditManager::ADD) { if(window->getEditManager().polygonOk(*window->getEditManager().getActiveRoom())) glColor4f(0.0f, 0.7f, 1.0f, 0.2f); else glColor4f(1.0f, 0.3f, 0.3f, 0.2f); fillPolygon(*window->getEditManager().getActiveRoom()); glLineWidth(2.0f); glColor4f(0.0f, 0.7f, 1.0f, 0.7f); drawPolygon(*window->getEditManager().getActiveRoom(), false); if(!window->getEditManager().getActiveRoom()->empty() && window->getEditManager().getHoveredVertex()) { if(!window->getEditManager().vertexOk(*window->getEditManager().getHoveredVertex())) glColor4f(1.0f, 0.3f, 0.3f, 0.7f); glBegin(GL_LINES); glVertex2d(window->getEditManager().getActiveRoom()->back().getX(), window->getEditManager().getActiveRoom()->back().getY()); glVertex2d(window->getEditManager().getHoveredVertex()->getX(), window->getEditManager().getHoveredVertex()->getY()); glEnd(); } } glPopMatrix(); gdk_gl_drawable_swap_buffers(drawable); gdk_gl_drawable_gl_end(drawable); return TRUE; }