diff options
author | neoraider <devnull@localhost> | 2008-02-15 12:01:04 +0100 |
---|---|---|
committer | neoraider <devnull@localhost> | 2008-02-15 12:01:04 +0100 |
commit | 80b4f14530426d21c7ea26b7976a728f5b36c294 (patch) | |
tree | a3d54b26f072f1204c7a1146bd1dc96a8a3850d3 | |
parent | c4aadd98230f3b9ab50398b715164ff874ea8a61 (diff) | |
download | zoomedit-80b4f14530426d21c7ea26b7976a728f5b36c294.tar zoomedit-80b4f14530426d21c7ea26b7976a728f5b36c294.zip |
Added edges to rooms.
-rw-r--r-- | Drawer.cpp | 8 | ||||
-rw-r--r-- | Edge.h | 24 | ||||
-rw-r--r-- | EdgeProvider.h | 18 | ||||
-rw-r--r-- | EditManager.cpp | 30 | ||||
-rw-r--r-- | FileManager.cpp | 6 | ||||
-rw-r--r-- | LevelEdge.h | 68 | ||||
-rw-r--r-- | LevelVertex.h | 22 | ||||
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | Makefile.in | 20 | ||||
-rw-r--r-- | Portal.h | 2 | ||||
-rw-r--r-- | Renderer.cpp | 26 | ||||
-rw-r--r-- | Renderer.h | 2 | ||||
-rw-r--r-- | Room.cpp | 63 | ||||
-rw-r--r-- | Room.h | 80 | ||||
-rw-r--r-- | SidebarView.cpp | 4 | ||||
-rw-r--r-- | ToolAddPolygon.cpp | 12 | ||||
-rw-r--r-- | ToolAddRect.cpp | 16 |
17 files changed, 305 insertions, 98 deletions
@@ -262,9 +262,9 @@ float Drawer::getImageWidth() const { for(Level::iterator object = window->getLevel().begin(); object != window->getLevel().end(); object++) { if((*object)->isOfType("Room")) { - Room *room = (Room*)&**object; + const Polygon &polygon = ((Room*)&**object)->getPolygon(); - for(Room::iterator v = room->begin(); v != room->end(); v++) { + for(Polygon::const_iterator v = polygon.begin(); v != polygon.end(); v++) { max = fmaxf(max, fabsf(v->getX())); } } @@ -278,9 +278,9 @@ float Drawer::getImageHeight() const { for(Level::iterator object = window->getLevel().begin(); object != window->getLevel().end(); object++) { if((*object)->isOfType("Room")) { - Room *room = (Room*)&**object; + const Polygon &polygon = ((Room*)&**object)->getPolygon(); - for(Room::iterator v = room->begin(); v != room->end(); v++) { + for(Polygon::const_iterator v = polygon.begin(); v != polygon.end(); v++) { max = fmaxf(max, fabsf(v->getY())); } } @@ -0,0 +1,24 @@ +#ifndef EDGE_H_ +#define EDGE_H_ + +#include "LevelVertex.h" +#include "Line.h" + + +class Edge { + private: + LevelVertex vertex1, vertex2; + + public: + Edge(const LevelVertex &v1, const LevelVertex &v2) + : vertex1(v1), vertex2(v2) {} + + const LevelVertex& getVertex1() const {return vertex1;} + const LevelVertex& getVertex2() const {return vertex2;} + + Line toLine() const { + return Line(*vertex1, *vertex2); + } +}; + +#endif /*EDGE_H_*/ diff --git a/EdgeProvider.h b/EdgeProvider.h new file mode 100644 index 0000000..3a08829 --- /dev/null +++ b/EdgeProvider.h @@ -0,0 +1,18 @@ +#ifndef EDGEPROVIDER_H_ +#define EDGEPROVIDER_H_ + +#include "Edge.h" + + +class EdgeProvider { + public: + virtual ~EdgeProvider() {} + + virtual const Edge* getEdge(size_t id) const = 0; + virtual size_t getEdgeCount() const = 0; + + virtual void moveEdge(size_t id, float x, float y) {} + virtual void rotateEdge(size_t id, float a) {} +}; + +#endif /*EDGEPROVIDER_H_*/ diff --git a/EditManager.cpp b/EditManager.cpp index 9ca3dfb..2697612 100644 --- a/EditManager.cpp +++ b/EditManager.cpp @@ -5,21 +5,21 @@ 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)); + for(size_t i = 0; i+2 < newRoom.getPolygon().size(); i++) { + l2.setVertex1(newRoom.getPolygon()[i]); + l2.setVertex2(newRoom.getPolygon()[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(newRoom.getPolygon().size() > 1) { + l2.setVertex1(newRoom.getPolygon()[newRoom.getPolygon().size()-2]); + l2.setVertex2(newRoom.getPolygon().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)) + if((*room)->isOfType("Room") && ((Room*)&**room)->getPolygon().intersects(l)) return false; } @@ -42,7 +42,7 @@ void EditManager::redraw() { } bool EditManager::addRoom(const Room &newRoom) { - if(newRoom.size() > 2 && polygonOk(newRoom)) { + if(newRoom.getPolygon().size() > 2 && polygonOk(newRoom.getPolygon())) { Room *room = new Room(newRoom); room->setName(idManager.generate("room")); @@ -90,16 +90,16 @@ 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((*room)->isOfType("Room") && ((Room*)&**room)->getPolygon().contains(v)) return false; } if(!newRoom) return true; - if(newRoom->empty()) + if(newRoom->getPolygon().empty()) return true; - l.setVertex1(newRoom->back()); + l.setVertex1(newRoom->getPolygon().back()); l.setVertex2(v); return lineOk(*newRoom, l); @@ -116,12 +116,12 @@ bool EditManager::polygonOk(const Polygon& polygon) const { Room *room = (Room*)&**object; - if(room->empty()) continue; + if(room->getPolygon().empty()) continue; - if(room->contains(polygon.front())) + if(room->getPolygon().contains(polygon.front())) return false; - if(polygon.contains(room->front())) + if(polygon.contains(room->getPolygon().front())) return false; } @@ -139,7 +139,7 @@ bool EditManager::polygonOk(const Polygon& polygon) const { l.setVertex2(*it2); for(Level::iterator room = window->getLevel().begin(); room != window->getLevel().end(); room++) { - if((*room)->isOfType("Room") && ((Room*)&**room)->intersects(l)) + if((*room)->isOfType("Room") && ((Room*)&**room)->getPolygon().intersects(l)) return false; } } diff --git a/FileManager.cpp b/FileManager.cpp index 90ef12b..9f1ed30 100644 --- a/FileManager.cpp +++ b/FileManager.cpp @@ -328,7 +328,7 @@ bool FileManager::write() { } std::vector<Triangle> triangles; - room->triangulate(triangles); + room->getPolygon().triangulate(triangles); for(std::vector<Triangle>::iterator triangle = triangles.begin(); triangle != triangles.end(); triangle++) { if(!writeTriangle(writer, *triangle, room->getHeight())) { @@ -337,9 +337,9 @@ bool FileManager::write() { } } - std::vector<Vertex> vertices = *room; + std::vector<Vertex> vertices = room->getPolygon(); - if(room->getDirection() == Triangle::CCW) { + if(room->getPolygon().getDirection() == Triangle::CCW) { vertices = std::vector<Vertex>(vertices.rbegin(), vertices.rend()); } diff --git a/LevelEdge.h b/LevelEdge.h new file mode 100644 index 0000000..7fb4e1a --- /dev/null +++ b/LevelEdge.h @@ -0,0 +1,68 @@ +#ifndef LEVELEDGE_H_ +#define LEVELEDGE_H_ + +#include "LevelObject.h" +#include "EdgeProvider.h" +#include <math.h> + + +class LevelEdge : public LevelObject { + private: + EdgeProvider *provider; + size_t id; + + static const float width = 1; + + public: + LevelEdge(EdgeProvider *p, size_t i, LevelObject *parent) + : LevelObject(parent), provider(p), id(i) {} + + virtual bool hit(const Vertex &v, float scale) const { + const Edge &edge = **this; + const Vertex &v1 = *edge.getVertex1(), &v2 = *edge.getVertex2(); + + float width = this->width/scale; + + if(v.getX() < fminf(v1.getX(), v2.getX())-width) + return false; + + if(v.getX() > fmaxf(v1.getX(), v2.getX())+width) + return false; + + if(v.getY() < fminf(v1.getY(), v2.getY())-width) + return false; + + if(v.getY() > fmaxf(v1.getY(), v2.getY())+width) + return false; + + Vertex r = v2 - v1, p = v - v1; + r *= (r.getX()*p.getX() + r.getY()*p.getY())/v1.distanceSq(v2); + + return (r.distanceSq(p) <= width*width); + } + + virtual int getPriority() const {return 500;} + + virtual const char* getType() const { + return "LevelEdge"; + } + + virtual void move(float x, float y) { + provider->moveEdge(id, x, y); + } + + virtual void rotate(float a) { + provider->rotateEdge(id, a); + } + + virtual Vertex getCenter() const { + Line l = provider->getEdge(id)->toLine(); + + return (l.getVertex1()+l.getVertex2()) / 2; + } + + const Edge* operator->() const {return provider->getEdge(id);} + const Edge& operator*() const {return *provider->getEdge(id);} +}; + +#endif /*LEVELEDGE_H_*/ diff --git a/LevelVertex.h b/LevelVertex.h index 998756f..448d021 100644 --- a/LevelVertex.h +++ b/LevelVertex.h @@ -5,14 +5,14 @@ #include "VertexProvider.h" -class LevelVertex : public LevelObject, public VertexProvider { +class LevelVertex : public LevelObject { private: VertexProvider *provider; size_t id; public: - LevelVertex(VertexProvider *p, size_t i) - : provider(p), id(i) {} + LevelVertex(VertexProvider *p, size_t i, LevelObject *parent) + : LevelObject(parent), provider(p), id(i) {} virtual const char* getType() const { return "LevelVertex"; @@ -38,22 +38,6 @@ class LevelVertex : public LevelObject, public VertexProvider { return *provider->getVertex(id); } - virtual const Vertex* getVertex(size_t id) const { - return provider->getVertex(this->id); - } - - virtual size_t getVertexCount() const { - return 1; - } - - virtual void moveVertex(size_t id, float x, float y) { - provider->moveVertex(this->id, x, y); - } - - virtual void rotateVertex(size_t id, float a) { - provider->rotateVertex(this->id, a); - } - const Vertex* operator->() const {return provider->getVertex(id);} const Vertex& operator*() const {return *provider->getVertex(id);} }; diff --git a/Makefile.am b/Makefile.am index 946d987..cdb09be 100644 --- a/Makefile.am +++ b/Makefile.am @@ -5,6 +5,6 @@ zoomedit_SOURCES = zoomedit.cpp UIManager.cpp Renderer.cpp Vertex.cpp \ Window.cpp SidebarView.cpp SidebarAdd.cpp Drawer.cpp \ EditManager.cpp FileManager.cpp SidebarToolbox.cpp \ ToolSelector.cpp ToolAddPolygon.cpp ToolAddRect.cpp \ - ToolGrab.cpp ToolRotate.cpp Vertex3d.cpp + ToolGrab.cpp ToolRotate.cpp Vertex3d.cpp Room.cpp zoomedit_CPPFLAGS = @GTK_CFLAGS@ @GTKGLEXT_CFLAGS@ @libxml2_CFLAGS@ zoomedit_LDADD = @GTK_LIBS@ @GTKGLEXT_LIBS@ @libxml2_LIBS@
\ No newline at end of file diff --git a/Makefile.in b/Makefile.in index 3c2feed..d37144b 100644 --- a/Makefile.in +++ b/Makefile.in @@ -62,7 +62,8 @@ am_zoomedit_OBJECTS = zoomedit-zoomedit.$(OBJEXT) \ zoomedit-ToolSelector.$(OBJEXT) \ zoomedit-ToolAddPolygon.$(OBJEXT) \ zoomedit-ToolAddRect.$(OBJEXT) zoomedit-ToolGrab.$(OBJEXT) \ - zoomedit-ToolRotate.$(OBJEXT) zoomedit-Vertex3d.$(OBJEXT) + zoomedit-ToolRotate.$(OBJEXT) zoomedit-Vertex3d.$(OBJEXT) \ + zoomedit-Room.$(OBJEXT) zoomedit_OBJECTS = $(am_zoomedit_OBJECTS) zoomedit_DEPENDENCIES = DEFAULT_INCLUDES = -I.@am__isrc@ @@ -190,7 +191,7 @@ zoomedit_SOURCES = zoomedit.cpp UIManager.cpp Renderer.cpp Vertex.cpp \ Window.cpp SidebarView.cpp SidebarAdd.cpp Drawer.cpp \ EditManager.cpp FileManager.cpp SidebarToolbox.cpp \ ToolSelector.cpp ToolAddPolygon.cpp ToolAddRect.cpp \ - ToolGrab.cpp ToolRotate.cpp Vertex3d.cpp + ToolGrab.cpp ToolRotate.cpp Vertex3d.cpp Room.cpp zoomedit_CPPFLAGS = @GTK_CFLAGS@ @GTKGLEXT_CFLAGS@ @libxml2_CFLAGS@ zoomedit_LDADD = @GTK_LIBS@ @GTKGLEXT_LIBS@ @libxml2_LIBS@ @@ -290,6 +291,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/zoomedit-Polygon.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/zoomedit-Rectangle.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/zoomedit-Renderer.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/zoomedit-Room.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/zoomedit-SidebarAdd.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/zoomedit-SidebarManager.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/zoomedit-SidebarToolbox.Po@am__quote@ @@ -657,6 +659,20 @@ zoomedit-Vertex3d.obj: Vertex3d.cpp @AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(zoomedit_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o zoomedit-Vertex3d.obj `if test -f 'Vertex3d.cpp'; then $(CYGPATH_W) 'Vertex3d.cpp'; else $(CYGPATH_W) '$(srcdir)/Vertex3d.cpp'; fi` +zoomedit-Room.o: Room.cpp +@am__fastdepCXX_TRUE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(zoomedit_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT zoomedit-Room.o -MD -MP -MF $(DEPDIR)/zoomedit-Room.Tpo -c -o zoomedit-Room.o `test -f 'Room.cpp' || echo '$(srcdir)/'`Room.cpp +@am__fastdepCXX_TRUE@ mv -f $(DEPDIR)/zoomedit-Room.Tpo $(DEPDIR)/zoomedit-Room.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='Room.cpp' object='zoomedit-Room.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(zoomedit_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o zoomedit-Room.o `test -f 'Room.cpp' || echo '$(srcdir)/'`Room.cpp + +zoomedit-Room.obj: Room.cpp +@am__fastdepCXX_TRUE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(zoomedit_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT zoomedit-Room.obj -MD -MP -MF $(DEPDIR)/zoomedit-Room.Tpo -c -o zoomedit-Room.obj `if test -f 'Room.cpp'; then $(CYGPATH_W) 'Room.cpp'; else $(CYGPATH_W) '$(srcdir)/Room.cpp'; fi` +@am__fastdepCXX_TRUE@ mv -f $(DEPDIR)/zoomedit-Room.Tpo $(DEPDIR)/zoomedit-Room.Po +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='Room.cpp' object='zoomedit-Room.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(zoomedit_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o zoomedit-Room.obj `if test -f 'Room.cpp'; then $(CYGPATH_W) 'Room.cpp'; else $(CYGPATH_W) '$(srcdir)/Room.cpp'; fi` + ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES) list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ unique=`for i in $$list; do \ @@ -71,7 +71,7 @@ class Portal : public LevelObject, public VertexProvider { std::vector<SharedPtr<LevelObject> > children; for(size_t i = 0; i < 4; i++) - children.push_back(SharedPtr<LevelObject>(new LevelVertex(this, i))); + children.push_back(SharedPtr<LevelObject>(new LevelVertex(this, i, this))); return children; } diff --git a/Renderer.cpp b/Renderer.cpp index 42614a7..0a4f403 100644 --- a/Renderer.cpp +++ b/Renderer.cpp @@ -93,6 +93,8 @@ void Renderer::drawCross(const Vertex &m, float r) { void Renderer::renderObject(const LevelObject &object, bool selected, bool highlighted, float scale) { if(object.isOfType("LevelVertex")) renderLevelVertex(*(LevelVertex*)&object, selected, highlighted, scale); + else if(object.isOfType("LevelEdge")) + renderLevelEdge(*(LevelEdge*)&object, selected, highlighted, scale); else if(object.isOfType("Room")) renderRoom(*(Room*)&object, selected, highlighted, scale); else if(object.isOfType("PlayerStart")) @@ -115,13 +117,33 @@ void Renderer::renderLevelVertex(const LevelVertex &vertex, bool selected, bool drawCircle(*vertex, 3.5f/scale, 16); } +void Renderer::renderLevelEdge(const LevelEdge &edge, bool selected, bool highlighted, float scale) { + if(selected) { + glColor4f(1.0f, 1.0f, 1.0f, 0.9f); + glLineWidth(2.0f); + } + else if(highlighted) { + glColor4f(0.0f, 0.7f, 1.0f, 0.7f); + glLineWidth(2.0f); + } + else + return; + + glBegin(GL_LINES); + + glVertex2f(edge->getVertex1()->getX(), edge->getVertex1()->getY()); + glVertex2f(edge->getVertex2()->getX(), edge->getVertex2()->getY()); + + glEnd(); +} + void Renderer::renderRoom(const Room &room, bool selected, bool highlighted, float scale) { if(selected) glColor4f(0.0f, 0.7f, 1.0f, 0.2f); else glColor4f(0.0f, 0.7f, 1.0f, 0.3f); - fillPolygon(room); + fillPolygon(room.getPolygon()); if(selected) { glColor4f(1.0f, 1.0f, 1.0f, 0.9f); @@ -136,7 +158,7 @@ void Renderer::renderRoom(const Room &room, bool selected, bool highlighted, flo glLineWidth(1.0f); } - drawPolygon(room); + drawPolygon(room.getPolygon()); } void Renderer::renderPlayerStart(const PlayerStart &start, bool selected, bool highlighted, float scale) { @@ -8,6 +8,7 @@ #include "Room.h" #include "PlayerStart.h" #include "Portal.h" +#include "LevelEdge.h" class Renderer { @@ -26,6 +27,7 @@ class Renderer { void renderObject(const LevelObject &object, bool selected, bool highlighted, float scale); void renderLevelVertex(const LevelVertex &vertex, bool selected, bool highlighted, float scale); + void renderLevelEdge(const LevelEdge &edge, bool selected, bool highlighted, float scale); void renderRoom(const Room &room, bool selected, bool highlighted, float scale); void renderPlayerStart(const PlayerStart &start, bool selected, bool highlighted, float scale); diff --git a/Room.cpp b/Room.cpp new file mode 100644 index 0000000..1980026 --- /dev/null +++ b/Room.cpp @@ -0,0 +1,63 @@ +#include "Room.h" +#include "LevelEdge.h" + + +void Room::rotateAround(Vertex z, float a) { + float s = sinf(a); + float c = cosf(a); + + for(Polygon::iterator v = polygon.begin(); v != polygon.end(); v++) { + *v -= z; + v->setLocation(c*v->getX() - s*v->getY(), c*v->getY() + s*v->getX()); + *v += z; + } +} + +const Room& Room::operator=(const Room &room) { + polygon.clear(); + edges.clear(); + + name = room.name; + height = room.height; + + for(Polygon::const_iterator v = room.polygon.begin(); v != room.polygon.end(); v++) + addVertex(*v); + + return room; +} + +void Room::addVertex(Vertex v) { + polygon.push_back(v); + + if(polygon.size() < 2) + return; + + if(polygon.size() == 2) { + LevelVertex v1(this, 0, this), v2(this, 1, this); + + edges.push_back(Edge(v1, v2)); + edges.push_back(Edge(v2, v1)); + + return; + } + + edges.pop_back(); + LevelVertex v1(edges.back().getVertex2()); + LevelVertex v2(this, polygon.size()-1, this); + LevelVertex v3(edges.front().getVertex1()); + + edges.push_back(Edge(v1, v2)); + edges.push_back(Edge(v2, v3)); +} + +std::vector<SharedPtr<LevelObject> > Room::getChildren() { + std::vector<SharedPtr<LevelObject> > children; + + for(size_t i = 0; i < polygon.size(); i++) + children.push_back(SharedPtr<LevelObject>(new LevelVertex(this, i, this))); + + for(size_t i = 0; i < edges.size(); i++) + children.push_back(SharedPtr<LevelObject>(new LevelEdge(this, i, this))); + + return children; +} @@ -2,21 +2,34 @@ #define ROOM_H_ #include "Polygon.h" +#include "SharedPtr.h" #include "LevelObject.h" #include "VertexProvider.h" -#include "LevelVertex.h" +#include "EdgeProvider.h" #include <string> -class Room : public Polygon, public LevelObject, public VertexProvider { +class Room : public LevelObject, private VertexProvider, private EdgeProvider { private: + Polygon polygon; std::string name; float height; + std::vector<Edge> edges; + + void rotateAround(Vertex z, float a); + public: Room() {height = 10;} Room(std::string name) {this->name = name; height = 10;} + Room(const Room &room) : name(room.name), height(room.height) { + for(Polygon::const_iterator v = room.polygon.begin(); v != room.polygon.end(); v++) + addVertex(*v); + } + + const Room& operator=(const Room &room); + std::string &getName() {return name;} const std::string &getName() const {return name;} void setName(const std::string &name) {this->name = name;} @@ -24,17 +37,14 @@ class Room : public Polygon, public LevelObject, public VertexProvider { float getHeight() const {return height;} void setHeight(float height) {this->height = height;} - virtual bool hit(const Vertex &v, float scale) const {return contains(v);} + void addVertex(Vertex v); + + const Polygon& getPolygon() const {return polygon;} + + virtual bool hit(const Vertex &v, float scale) const {return polygon.contains(v);} virtual int getPriority() const {return 0;} - virtual std::vector<SharedPtr<LevelObject> > getChildren() { - std::vector<SharedPtr<LevelObject> > children; - - for(size_t i = 0; i < size(); i++) - children.push_back(SharedPtr<LevelObject>(new LevelVertex(this, i))); - - return children; - } + virtual std::vector<SharedPtr<LevelObject> > getChildren(); virtual const char* getType() const { return "Room"; @@ -43,55 +53,55 @@ class Room : public Polygon, public LevelObject, public VertexProvider { virtual void move(float x, float y) { Vertex m(x, y); - for(iterator v = begin(); v != end(); v++) + for(Polygon::iterator v = polygon.begin(); v != polygon.end(); v++) *v += m; } virtual void rotate(float a) { - Vertex z = getCenter(); - float s = sinf(a); - float c = cosf(a); - - for(iterator v = begin(); v != end(); v++) { - *v -= z; - v->setLocation(c*v->getX() - s*v->getY(), c*v->getY() + s*v->getX()); - *v += z; - } + rotateAround(getCenter(), a); } virtual Vertex getCenter() const { Vertex ret; - for(const_iterator v = begin(); v != end(); v++) + for(Polygon::const_iterator v = polygon.begin(); v != polygon.end(); v++) ret += *v; - return ret / size(); + return ret / polygon.size(); } virtual const Vertex* getVertex(size_t id) const { - return &at(id); + return &polygon[id]; } virtual size_t getVertexCount() const { - return size(); + return polygon.size(); } virtual void moveVertex(size_t id, float x, float y) { - at(id) += Vertex(x, y); + polygon[id] += Vertex(x, y); } virtual void rotateVertex(size_t id, float a) { - Vertex z = at(id); - float s = sinf(a); - float c = cosf(a); - - for(iterator v = begin(); v != end(); v++) { - *v -= z; - v->setLocation(c*v->getX() - s*v->getY(), c*v->getY() + s*v->getX()); - *v += z; - } + rotateAround(polygon[id], a); } + virtual const Edge* getEdge(size_t id) const { + return &edges[id]; + } + + virtual size_t getEdgeCount() const { + return edges.size(); + } + + virtual void moveEdge(size_t id, float x, float y) { + moveVertex(id, x, y); + moveVertex((id+1)%polygon.size(), x, y); + } + + virtual void rotateEdge(size_t id, float a) { + rotateAround((polygon[id]+polygon[(id+1)%polygon.size()])/2, a); + } }; #endif /*ROOM_H_*/ diff --git a/SidebarView.cpp b/SidebarView.cpp index 1246126..db470be 100644 --- a/SidebarView.cpp +++ b/SidebarView.cpp @@ -166,11 +166,11 @@ void SidebarView::update() { gtk_entry_set_text(GTK_ENTRY(entryName), room->getName().c_str()); gtk_widget_show(entryName); - gchar *string = g_strdup_printf("%.2f", room->area()); + gchar *string = g_strdup_printf("%.2f", room->getPolygon().area()); gtk_label_set_text(GTK_LABEL(labelArea), string); g_free(string); - string = g_strdup_printf("%.2f", room->perimeter()); + string = g_strdup_printf("%.2f", room->getPolygon().perimeter()); gtk_label_set_text(GTK_LABEL(labelPerimeter), string); g_free(string); diff --git a/ToolAddPolygon.cpp b/ToolAddPolygon.cpp index 0ac8fea..4ede1a3 100644 --- a/ToolAddPolygon.cpp +++ b/ToolAddPolygon.cpp @@ -25,24 +25,24 @@ void ToolAddPolygon::deactivate() { } void ToolAddPolygon::render(const Level &level, const Rectangle &rect, float scale) { - if(editManager->polygonOk(newRoom)) + if(editManager->polygonOk(newRoom.getPolygon())) glColor4f(0.0f, 0.7f, 1.0f, 0.2f); else glColor4f(1.0f, 0.3f, 0.3f, 0.2f); - fillPolygon(newRoom); + fillPolygon(newRoom.getPolygon()); glLineWidth(2.0f); glColor4f(0.0f, 0.7f, 1.0f, 0.7f); - drawPolygon(newRoom, false); + drawPolygon(newRoom.getPolygon(), false); - if(!newRoom.empty() && editManager->getHoveredVertex()) { + if(!newRoom.getPolygon().empty() && editManager->getHoveredVertex()) { if(!editManager->vertexOk(*editManager->getHoveredVertex(), &newRoom)) glColor4f(1.0f, 0.3f, 0.3f, 0.7f); glBegin(GL_LINES); - glVertex2f(newRoom.back().getX(), newRoom.back().getY()); + glVertex2f(newRoom.getPolygon().back().getX(), newRoom.getPolygon().back().getY()); glVertex2f(editManager->getHoveredVertex()->getX(), editManager->getHoveredVertex()->getY()); glEnd(); @@ -59,7 +59,7 @@ bool ToolAddPolygon::buttonPress(unsigned int button) { if(!editManager->vertexOk(*editManager->getHoveredVertex(), &newRoom)) return false; - newRoom.push_back(*editManager->getHoveredVertex()); + newRoom.addVertex(*editManager->getHoveredVertex()); editManager->redraw(); sidebar.update(); diff --git a/ToolAddRect.cpp b/ToolAddRect.cpp index a749c69..08367ff 100644 --- a/ToolAddRect.cpp +++ b/ToolAddRect.cpp @@ -6,10 +6,10 @@ 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())); + room.addVertex(v1); + room.addVertex(Vertex(v1.getX(), v2->getY())); + room.addVertex(*v2); + room.addVertex(Vertex(v2->getX(), v1.getY())); return room; } @@ -38,16 +38,16 @@ void ToolAddRect::render(const Level &level, const Rectangle &rect, float scale) Room room = createRoom(); - if(editManager->polygonOk(room)) + if(editManager->polygonOk(room.getPolygon())) glColor4f(0.0f, 0.7f, 1.0f, 0.2f); else glColor4f(1.0f, 0.3f, 0.3f, 0.2f); - fillPolygon(room); + fillPolygon(room.getPolygon()); glLineWidth(2.0f); glColor4f(0.0f, 0.7f, 1.0f, 0.7f); - drawPolygon(room); + drawPolygon(room.getPolygon()); } bool ToolAddRect::buttonPress(unsigned int button) { @@ -78,7 +78,7 @@ bool ToolAddRect::buttonRelease(unsigned int button) { if(editManager->getHoveredVertex()) { Room room = createRoom(); - if(editManager->polygonOk(room)) { + if(editManager->polygonOk(room.getPolygon())) { editManager->addRoom(room); editManager->redraw(); |