#include "EditManager.h" #include "Window.h" bool EditManager::lineOk(const Room &newRoom, const Line &l) const { Line l2; for(size_t i = 0; i+2 < newRoom.size(); i++) { l2.setVertex1(newRoom.at(i)); l2.setVertex2(newRoom.at(i+1)); if(l.intersects(l2, NULL) == INTERSECTION_SEGMENT_SEGMENT) return false; } if(newRoom.size() > 1) { l2.setVertex1(newRoom.at(newRoom.size()-2)); l2.setVertex2(newRoom.back()); if(l2.contains(l.getVertex2())) return false; } for(Level::iterator room = window->getLevel().begin(); room != window->getLevel().end(); room++) { if((*room)->isOfType("Room") && ((Room*)&**room)->intersects(l)) return false; } return true; } EditManager::EditManager(Window *window) { this->window = window; hoveredObject = NULL; selectedObject = NULL; hasHoveredVertex = false; } void EditManager::redraw() { window->redraw(); } bool EditManager::addRoom(const Room &newRoom) { if(newRoom.size() > 2 && polygonOk(newRoom)) { Room *room = new Room(newRoom); room->setName(idManager.generate("room")); window->getLevel().push_back(SharedPtr(room)); selectedObject = room; window->resetTool(); return true; } return false; } Vertex* EditManager::getHoveredVertex() { if(hasHoveredVertex) return &hoveredVertex; else return NULL; } void EditManager::setHoveredVertex(Vertex *v) { if(v) { hasHoveredVertex = true; hoveredVertex = *v; hoveredObject = NULL; for(Level::iterator object = window->getLevel().begin(); object != window->getLevel().end(); object++) { if((*object)->hit(*v)) { if(hoveredObject && (*object)->getPriority() < hoveredObject->getPriority()) continue; hoveredObject = &**object; } } } else { hasHoveredVertex = false; hoveredObject = NULL; } window->update(); } bool EditManager::vertexOk(const Vertex &v, const Room *newRoom) const { Line l; for(Level::iterator room = window->getLevel().begin(); room != window->getLevel().end(); room++) { if((*room)->isOfType("Room") && ((Room*)&**room)->contains(v)) return false; } if(!newRoom) return true; if(newRoom->empty()) return true; l.setVertex1(newRoom->back()); l.setVertex2(v); return lineOk(*newRoom, l); } bool EditManager::polygonOk(const Polygon& polygon) const { Line l, l2; if(polygon.empty()) return false; for(Level::iterator object = window->getLevel().begin(); object != window->getLevel().end(); object++) { if(!(*object)->isOfType("Room")) continue; Room *room = (Room*)&**object; 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)->isOfType("Room") && ((Room*)&**room)->intersects(l)) return false; } } return true; }