#include "ToolAddRect.h" #include "GL/gl.h" Room ToolAddRect::createRoom() { const Vertex *v2 = editManager->getHoveredVertex(); Room room; room.push_back(v1); room.push_back(Vertex(v1.getX(), v2->getY())); room.push_back(*v2); room.push_back(Vertex(v2->getX(), v1.getY())); return room; } ToolAddRect::ToolAddRect(EditManager *editManager) : Renderer(editManager), sidebar(editManager) { this->editManager = editManager; pressed = false; image = gtk_image_new_from_stock(GTK_STOCK_MISSING_IMAGE, GTK_ICON_SIZE_BUTTON); g_object_ref_sink(G_OBJECT(image)); } ToolAddRect::~ToolAddRect() { g_object_unref(G_OBJECT(image)); } void ToolAddRect::activate() { editManager->setHighlightedObject(NULL); editManager->setSelectedObject(NULL); } void ToolAddRect::render(const Level &level, const Rectangle &rect, float scale) { if(!pressed || !editManager->getHoveredVertex()) return; Room room = createRoom(); if(editManager->polygonOk(room)) glColor4f(0.0f, 0.7f, 1.0f, 0.2f); else glColor4f(1.0f, 0.3f, 0.3f, 0.2f); fillPolygon(room); glLineWidth(2.0f); glColor4f(0.0f, 0.7f, 1.0f, 0.7f); drawPolygon(room); } bool ToolAddRect::buttonPress(unsigned int button) { if(button != 1) return false; if(!editManager->getHoveredVertex()) return false; if(!editManager->vertexOk(*editManager->getHoveredVertex())) return false; pressed = true; v1 = *editManager->getHoveredVertex(); editManager->redraw(); sidebar.update(); return true; } bool ToolAddRect::buttonRelease(unsigned int button) { if(button != 1 || !pressed) return false; pressed = false; if(editManager->getHoveredVertex()) { Room room = createRoom(); if(editManager->polygonOk(room)) { editManager->addRoom(room); editManager->redraw(); sidebar.update(); return true; } } editManager->redraw(); sidebar.update(); return false; }