zoomedit: Abstracted room vertices.
This commit is contained in:
parent
db5647f286
commit
efb78cdf51
4 changed files with 106 additions and 24 deletions
|
@ -55,6 +55,14 @@ class LevelEdge : public LevelObject {
|
||||||
provider->rotateEdge(id, m, a);
|
provider->rotateEdge(id, m, a);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const EdgeProvider* getProvider() const {
|
||||||
|
return provider;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t getId() const {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
const Edge* operator->() const {return provider->getEdge(id);}
|
const Edge* operator->() const {return provider->getEdge(id);}
|
||||||
const Edge& operator*() const {return *provider->getEdge(id);}
|
const Edge& operator*() const {return *provider->getEdge(id);}
|
||||||
};
|
};
|
||||||
|
|
|
@ -37,6 +37,14 @@ class LevelVertex : public LevelObject {
|
||||||
virtual bool canConnect() const {return provider->canConnectVertex(id);}
|
virtual bool canConnect() const {return provider->canConnectVertex(id);}
|
||||||
virtual void connect() {provider->connectVertex(id);}
|
virtual void connect() {provider->connectVertex(id);}
|
||||||
|
|
||||||
|
const VertexProvider* getProvider() const {
|
||||||
|
return provider;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t getId() const {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
const Vertex* operator->() const {return provider->getVertex(id);}
|
const Vertex* operator->() const {return provider->getVertex(id);}
|
||||||
const Vertex& operator*() const {return *provider->getVertex(id);}
|
const Vertex& operator*() const {return *provider->getVertex(id);}
|
||||||
};
|
};
|
||||||
|
|
29
Room.cpp
29
Room.cpp
|
@ -3,25 +3,30 @@
|
||||||
|
|
||||||
|
|
||||||
const Room& Room::operator=(const Room &room) {
|
const Room& Room::operator=(const Room &room) {
|
||||||
polygon.clear();
|
vertices.clear();
|
||||||
edges.clear();
|
edges.clear();
|
||||||
|
|
||||||
name = room.name;
|
name = room.name;
|
||||||
height = room.height;
|
height = room.height;
|
||||||
|
|
||||||
for(Polygon::const_iterator v = room.polygon.begin(); v != room.polygon.end(); v++)
|
vertices = room.vertices;
|
||||||
addVertex(*v);
|
|
||||||
|
for(std::vector<Edge>::const_iterator edge = room.edges.begin(); edge != room.edges.end(); edge++) {
|
||||||
|
LevelVertex v1(this, edge->getVertex1().getId(), this);
|
||||||
|
LevelVertex v2(this, edge->getVertex2().getId(), this);
|
||||||
|
edges.push_back(Edge(v1, v2));
|
||||||
|
}
|
||||||
|
|
||||||
return room;
|
return room;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Room::addVertex(Vertex v) {
|
void Room::addVertex(Vertex v) {
|
||||||
polygon.push_back(v);
|
vertices.push_back(SharedPtr<RoomVertex>(new RoomVertexDirect(v)));
|
||||||
|
|
||||||
if(polygon.size() < 2)
|
if(vertices.size() < 2)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if(polygon.size() == 2) {
|
if(vertices.size() == 2) {
|
||||||
LevelVertex v1(this, 0, this), v2(this, 1, this);
|
LevelVertex v1(this, 0, this), v2(this, 1, this);
|
||||||
|
|
||||||
edges.push_back(Edge(v1, v2));
|
edges.push_back(Edge(v1, v2));
|
||||||
|
@ -32,7 +37,7 @@ void Room::addVertex(Vertex v) {
|
||||||
|
|
||||||
edges.pop_back();
|
edges.pop_back();
|
||||||
LevelVertex v1(edges.back().getVertex2());
|
LevelVertex v1(edges.back().getVertex2());
|
||||||
LevelVertex v2(this, polygon.size()-1, this);
|
LevelVertex v2(this, vertices.size()-1, this);
|
||||||
LevelVertex v3(edges.front().getVertex1());
|
LevelVertex v3(edges.front().getVertex1());
|
||||||
|
|
||||||
edges.push_back(Edge(v1, v2));
|
edges.push_back(Edge(v1, v2));
|
||||||
|
@ -42,7 +47,7 @@ void Room::addVertex(Vertex v) {
|
||||||
std::vector<SharedPtr<LevelObject> > Room::getChildren() {
|
std::vector<SharedPtr<LevelObject> > Room::getChildren() {
|
||||||
std::vector<SharedPtr<LevelObject> > children;
|
std::vector<SharedPtr<LevelObject> > children;
|
||||||
|
|
||||||
for(size_t i = 0; i < polygon.size(); i++)
|
for(size_t i = 0; i < vertices.size(); i++)
|
||||||
children.push_back(SharedPtr<LevelObject>(new LevelVertex(this, i, this)));
|
children.push_back(SharedPtr<LevelObject>(new LevelVertex(this, i, this)));
|
||||||
|
|
||||||
for(size_t i = 0; i < edges.size(); i++)
|
for(size_t i = 0; i < edges.size(); i++)
|
||||||
|
@ -55,9 +60,9 @@ void Room::rotate(Vertex m, float a) {
|
||||||
float s = sinf(a);
|
float s = sinf(a);
|
||||||
float c = cosf(a);
|
float c = cosf(a);
|
||||||
|
|
||||||
for(Polygon::iterator v = polygon.begin(); v != polygon.end(); v++) {
|
for(std::vector<SharedPtr<RoomVertex> >::iterator v = vertices.begin(); v != vertices.end(); v++) {
|
||||||
*v -= m;
|
**v -= m;
|
||||||
v->setLocation(c*v->getX() - s*v->getY(), c*v->getY() + s*v->getX());
|
(*v)->setLocation(c*(**v)->getX() - s*(**v)->getY(), c*(**v)->getY() + s*(**v)->getX());
|
||||||
*v += m;
|
**v += m;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
85
Room.h
85
Room.h
|
@ -11,19 +11,73 @@
|
||||||
|
|
||||||
class Room : public LevelObject, private VertexProvider, private EdgeProvider {
|
class Room : public LevelObject, private VertexProvider, private EdgeProvider {
|
||||||
private:
|
private:
|
||||||
Polygon polygon;
|
class RoomVertex {
|
||||||
|
public:
|
||||||
|
virtual const Vertex& operator*() const = 0;
|
||||||
|
const Vertex* operator->() {return &**this;}
|
||||||
|
|
||||||
|
virtual ~RoomVertex() {}
|
||||||
|
|
||||||
|
virtual const Vertex& operator+=(const Vertex &v) {return **this;}
|
||||||
|
virtual const Vertex& operator-=(const Vertex &v) {return **this;}
|
||||||
|
|
||||||
|
virtual void setLocation(float x, float y) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class RoomVertexDirect : public RoomVertex {
|
||||||
|
private:
|
||||||
|
Vertex vertex;
|
||||||
|
|
||||||
|
public:
|
||||||
|
RoomVertexDirect(const Vertex &v) : vertex(v) {}
|
||||||
|
|
||||||
|
virtual const Vertex& operator*() const {
|
||||||
|
return vertex;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual const Vertex& operator+=(const Vertex &v) {
|
||||||
|
vertex += v;
|
||||||
|
return vertex;
|
||||||
|
}
|
||||||
|
virtual const Vertex& operator-=(const Vertex &v) {
|
||||||
|
vertex -= v;
|
||||||
|
return vertex;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void setLocation(float x, float y) {
|
||||||
|
vertex.setLocation(x, y);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class RoomVertexIndirect : public RoomVertex {
|
||||||
|
private:
|
||||||
|
LevelVertex vertex;
|
||||||
|
|
||||||
|
public:
|
||||||
|
RoomVertexIndirect(const LevelVertex &v) : vertex(v) {}
|
||||||
|
|
||||||
|
virtual const Vertex& operator*() const {
|
||||||
|
return *vertex;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
std::string name;
|
std::string name;
|
||||||
float height;
|
float height;
|
||||||
|
|
||||||
|
std::vector<SharedPtr<RoomVertex> > vertices;
|
||||||
std::vector<Edge> edges;
|
std::vector<Edge> edges;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Room() {height = 10;}
|
Room() {height = 10;}
|
||||||
Room(std::string name) {this->name = name; height = 10;}
|
Room(std::string name) {this->name = name; height = 10;}
|
||||||
|
|
||||||
Room(const Room &room) : name(room.name), height(room.height) {
|
Room(const Room &room) : name(room.name), height(room.height), vertices(room.vertices) {
|
||||||
for(Polygon::const_iterator v = room.polygon.begin(); v != room.polygon.end(); v++)
|
for(std::vector<Edge>::const_iterator edge = room.edges.begin(); edge != room.edges.end(); edge++) {
|
||||||
addVertex(*v);
|
LevelVertex v1(this, edge->getVertex1().getId(), this);
|
||||||
|
LevelVertex v2(this, edge->getVertex2().getId(), this);
|
||||||
|
edges.push_back(Edge(v1, v2));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const Room& operator=(const Room &room);
|
const Room& operator=(const Room &room);
|
||||||
|
@ -37,9 +91,16 @@ class Room : public LevelObject, private VertexProvider, private EdgeProvider {
|
||||||
|
|
||||||
void addVertex(Vertex v);
|
void addVertex(Vertex v);
|
||||||
|
|
||||||
const Polygon& getPolygon() const {return polygon;}
|
Polygon getPolygon() const {
|
||||||
|
Polygon polygon;
|
||||||
|
|
||||||
virtual bool hit(const Vertex &v) const {return polygon.contains(v);}
|
for(std::vector<SharedPtr<RoomVertex> >::const_iterator v = vertices.begin(); v != vertices.end(); v++)
|
||||||
|
polygon.push_back(***v);
|
||||||
|
|
||||||
|
return polygon;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual bool hit(const Vertex &v) const {return getPolygon().contains(v);}
|
||||||
virtual int getPriority() const {return 0;}
|
virtual int getPriority() const {return 0;}
|
||||||
|
|
||||||
virtual std::vector<SharedPtr<LevelObject> > getChildren();
|
virtual std::vector<SharedPtr<LevelObject> > getChildren();
|
||||||
|
@ -53,8 +114,8 @@ class Room : public LevelObject, private VertexProvider, private EdgeProvider {
|
||||||
virtual void move(float x, float y) {
|
virtual void move(float x, float y) {
|
||||||
Vertex m(x, y);
|
Vertex m(x, y);
|
||||||
|
|
||||||
for(Polygon::iterator v = polygon.begin(); v != polygon.end(); v++)
|
for(std::vector<SharedPtr<RoomVertex> >::iterator v = vertices.begin(); v != vertices.end(); v++)
|
||||||
*v += m;
|
**v += m;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual bool canRotate() const {return true;}
|
virtual bool canRotate() const {return true;}
|
||||||
|
@ -62,15 +123,15 @@ class Room : public LevelObject, private VertexProvider, private EdgeProvider {
|
||||||
virtual void rotate(Vertex m, float a);
|
virtual void rotate(Vertex m, float a);
|
||||||
|
|
||||||
virtual const Vertex* getVertex(size_t id) const {
|
virtual const Vertex* getVertex(size_t id) const {
|
||||||
return &polygon[id];
|
return &**vertices[id];
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual size_t getVertexCount() const {
|
virtual size_t getVertexCount() const {
|
||||||
return polygon.size();
|
return vertices.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void moveVertex(size_t id, float x, float y) {
|
virtual void moveVertex(size_t id, float x, float y) {
|
||||||
polygon[id] += Vertex(x, y);
|
*vertices[id] += Vertex(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void rotateVertex(size_t id, Vertex m, float a) {
|
virtual void rotateVertex(size_t id, Vertex m, float a) {
|
||||||
|
@ -87,7 +148,7 @@ class Room : public LevelObject, private VertexProvider, private EdgeProvider {
|
||||||
|
|
||||||
virtual void moveEdge(size_t id, float x, float y) {
|
virtual void moveEdge(size_t id, float x, float y) {
|
||||||
moveVertex(id, x, y);
|
moveVertex(id, x, y);
|
||||||
moveVertex((id+1)%polygon.size(), x, y);
|
moveVertex((id+1)%vertices.size(), x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void rotateEdge(size_t id, Vertex m, float a) {
|
virtual void rotateEdge(size_t id, Vertex m, float a) {
|
||||||
|
|
Reference in a new issue