diff options
Diffstat (limited to 'EditManager.cpp')
-rw-r--r-- | EditManager.cpp | 149 |
1 files changed, 139 insertions, 10 deletions
diff --git a/EditManager.cpp b/EditManager.cpp index 34d53be..d2e65ce 100644 --- a/EditManager.cpp +++ b/EditManager.cpp @@ -1,19 +1,50 @@ #include "EditManager.h" #include "Window.h" -#include "edit.h" + + +bool EditManager::lineOk(const Line& l) const { + Line l2; + + if(activeRoom) { + for(size_t i = 0; i+2 < activeRoom->size(); i++) { + l2.setVertex1(activeRoom->at(i)); + l2.setVertex2(activeRoom->at(i+1)); + + if(l.intersects(l2, NULL) == INTERSECTION_SEGMENT_SEGMENT) return false; + } + + if(activeRoom->size() > 1) { + l2.setVertex1(activeRoom->at(activeRoom->size()-2)); + l2.setVertex2(activeRoom->back()); + if(l2.contains(l.getVertex2())) return false; + } + } + + for(Level::iterator room = window->getLevel().begin(); room != window->getLevel().end(); room++) { + if(room->intersects(l)) + return false; + } + + return true; +} EditManager::EditManager(Window *window) { this->window = window; + activeRoom = NULL; mode = VIEW; + + hoveredRoom = NULL; + + hasHoveredVertex = false; } void EditManager::addRoom() { - //newRoom = Room(); + newRoom = Room(idManager.generate("room")); mode = ADD; - startAddMode(); + activeRoom = &newRoom; window->update(); } @@ -21,20 +52,118 @@ void EditManager::addRoom() { void EditManager::finishRoom() { mode = VIEW; - if(getActiveRoom() && getActiveRoom()->size() > 2 && isPolygonOk(getActiveRoom())) { - getLevel()->push_back(*getActiveRoom()); - setActiveRoom(&getLevel()->back()); - - endAddMode(); + if(newRoom.size() > 2 && polygonOk(newRoom)) { + window->getLevel().push_back(newRoom); + activeRoom = &window->getLevel().back(); } window->update(); } void EditManager::addVertex(const Vertex &v) { - if(mode != ADD || !getActiveRoom()) + if(mode != ADD || !activeRoom) return; - getActiveRoom()->push_back(v); + activeRoom->push_back(v); window->update(); } + +Vertex* EditManager::getHoveredVertex() { + if(hasHoveredVertex) return &hoveredVertex; + else return NULL; +} + +void EditManager::setHoveredVertex(Vertex *v) { + if(v) { + hasHoveredVertex = true; + hoveredVertex = *v; + + hoveredRoom = NULL; + + for(Level::iterator room = window->getLevel().begin(); room != window->getLevel().end(); room++) { + if(room->contains(*v)) { + hoveredRoom = &*room; + break; + } + } + } + else { + hasHoveredVertex = false; + hoveredRoom = NULL; + } + + window->update(); +} + +void EditManager::buttonPress(unsigned int button) { + switch(button) { + case 1: + if(!hasHoveredVertex) + break; + + switch(mode) { + case VIEW: + activeRoom = getHoveredRoom();; + break; + + case ADD: + if(vertexOk(hoveredVertex)) + addVertex(hoveredVertex); + } + + window->update(); + } +} + +bool EditManager::vertexOk(const Vertex& v) const { + Line l; + + for(Level::iterator room = window->getLevel().begin(); room != window->getLevel().end(); room++) { + if(room->contains(v)) return false; + } + + if(!(activeRoom && !activeRoom->empty())) + return true; + + l.setVertex1(activeRoom->back()); + l.setVertex2(v); + + return lineOk(l); +} + +bool EditManager::polygonOk(const Polygon& polygon) const { + Line l, l2; + + if(polygon.empty()) return false; + + for(Level::iterator room = window->getLevel().begin(); room != window->getLevel().end(); room++) { + if(room->empty()) continue; + + if(room->contains(polygon.front())) + return false; + + if(polygon.contains(room->front())) + return false; + } + + if(polygon.size() == 1) + return true; + + if(!polygon.isSimple()) + return false; + + for(Polygon::const_iterator it = polygon.begin(); it != polygon.end(); it++) { + Polygon::const_iterator it2 = it+1; + if(it2 == polygon.end()) it2 = polygon.begin(); + + l.setVertex1(*it); + l.setVertex2(*it2); + + for(Level::iterator room = window->getLevel().begin(); room != window->getLevel().end(); room++) { + if(room->intersects(l)) + return false; + } + } + + return true; +} |