Newer
Older
#include "simul/TableGraphics.h"
using namespace std;
Point2d::Point2d(int mx, int my)
{
x = mx;
y = my;
}
void Point2d::addXOffset(int xOffset)
{
x += xOffset;
}
void Point2d::addYOffset(int yOffset)
{
y += yOffset;
}
void Point2d::readSelf(XMLTag* tree)
{
std::vector<XMLTag*> children = tree->getChildren();
int size = children.size();
for (int i = 0; i < size; i++)
{
QString type = children[i]->getType();
if (type == "x" || type == "X" || type == "width")
x = children[i]->getValue().toInt();
else if (type == "y" || type == "Y" || type == "height")
y = children[i]->getValue().toInt();
}
}
Shape::Shape()
{
p_z = 0;
}
Shape::~Shape()
{
}
void Shape::setSolid(bool solid)
{
this->solid = solid;
}
bool Shape::getSolid() const
{
return solid;
}
void Shape::readSelf(XMLTag* tree)
{
std::vector<XMLTag*> children = tree->getChildren();
int size = children.size();
for (int i = 0; i < size; i++)
{
QString type = children[i]->getType();
if (type == "z" || type == "Z")
p_z = children[i]->getValue().toInt();
else if (type == "Color" || type == "color")
p_color = QColor(children[i]->getValue());
}
}
void Shape::drawSelf(QPainter* painter)
{
painter->setBrush(p_color);
painter->setPen(p_color);
}
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
Rect::Rect()
{
p_start = Point2d();
p_size = Size2d();
}
Rect::~Rect()
{
}
void Rect::addXOffset(int xOffset)
{
p_start.addXOffset(xOffset);
}
void Rect::addYOffset(int yOffset)
{
p_start.addYOffset(yOffset);
}
void Rect::readSelf(XMLTag* tree)
{
Shape::readSelf(tree); // reads shapes properties
std::vector<XMLTag*> children = tree->getChildren();
int size = children.size();
for (int i = 0; i < size; i++)
{
QString type = children[i]->getType();
if (type == "Position" || type == "position")
p_start.readSelf(children[i]);
else if (type == "Size" || type == "size")
p_size.readSelf(children[i]);
}
}
void Rect::drawSelf(QPainter* painter)
{
//Shape::drawSelf(painter);
painter->fillRect(p_start.x, p_start.y, p_size.x, p_size.y, p_color);
}
void Rect::createSolid(b2Body* body)
{
#ifdef BOX2D_2_0_1
b2PolygonDef box;
b2PolygonDef &fixture = box;
#else
b2PolygonShape box;
b2FixtureDef fixture;
fixture.shape = &box;
#endif
fixture.friction = 0.5;
fixture.density = 0;
#ifdef BOX2D_2_0_1
#define CreateFixture CreateShape
#endif
box.SetAsBox(p_size.x/200.,p_size.y/200., b2Vec2((p_start.x + p_size.x/2.)/100.,(p_start.y + p_size.y/2.)/100.),0);
body->CreateFixture(&fixture);
}
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
RoundedRect::RoundedRect() : Rect()
{
p_xRadius = 0;
p_yRadius = 0;
}
void RoundedRect::addXOffset(int xOffset)
{
Rect::addXOffset(xOffset);
}
void RoundedRect::addYOffset(int yOffset)
{
Rect::addYOffset(yOffset);
}
void RoundedRect::readSelf(XMLTag* tree)
{
Rect::readSelf(tree); // reads rect properties
std::vector<XMLTag*> children = tree->getChildren();
int size = children.size();
for (int i = 0; i < size; i++)
{
QString type = children[i]->getType();
if (type == "xradius" || type == "xRadius" || type == "XRadius")
p_xRadius = children[i]->getValue().toInt();
else if (type == "yradius" || type == "yRadius" || type == "YRadius")
p_xRadius = children[i]->getValue().toInt();
else if (type == "radius" || type == "Radius")
{
p_xRadius = children[i]->getValue().toInt();
p_yRadius = children[i]->getValue().toInt();
}
}
}
void RoundedRect::drawSelf(QPainter* painter)
{
Shape::drawSelf(painter);
painter->drawRoundedRect(p_start.x, p_start.y, p_size.x, p_size.y, p_xRadius, p_yRadius);
}
Ellipse::Ellipse()
{
p_center = Point2d();
p_xRadius = 0;
p_yRadius = 0;
}
void Ellipse::addXOffset(int xOffset)
{
p_center.addXOffset(xOffset);
}
void Ellipse::addYOffset(int yOffset)
{
p_center.addYOffset(yOffset);
}
void Ellipse::readSelf(XMLTag* tree)
{
Shape::readSelf(tree); // reads shapes properties
std::vector<XMLTag*> children = tree->getChildren();
int size = children.size();
for (int i = 0; i < size; i++)
{
QString type = children[i]->getType();
if (type == "Center" || type == "center")
p_center.readSelf(children[i]);
else if (type == "xradius" || type == "xRadius" || type == "XRadius")
p_xRadius = children[i]->getValue().toInt();
else if (type == "yradius" || type == "yRadius" || type == "YRadius")
p_xRadius = children[i]->getValue().toInt();
else if (type == "radius" || type == "Radius")
{
p_xRadius = children[i]->getValue().toInt();
p_yRadius = children[i]->getValue().toInt();
}
}
}
void Ellipse::drawSelf(QPainter* painter)
{
Shape::drawSelf(painter);
painter->drawEllipse(QPoint(p_center.x, p_center.y), p_xRadius, p_yRadius);
}
TableGraphics::TableGraphics()
{
shapes.clear();
}
void TableGraphics::addXOffset(int xOffset)
{
int size = shapes.size();
for (int i = 0; i < size; i++)
shapes[i]->addXOffset(xOffset);
}
void TableGraphics::addYOffset(int yOffset)
{
int size = shapes.size();
for (int i = 0; i < size; i++)
shapes[i]->addYOffset(yOffset);
}
void TableGraphics::load(QString filename)
{
QFile file(filename);
if (!file.open(QIODevice::ReadOnly))
{
cout << "Error while opening " << filename.toStdString() << endl;
return;
}
QString text = file.readAll();
file.close();
/// NOW PROCESS ALL DATA
XMLTag* tag = XMLReader::getTree(text); // get the root
std::vector<XMLTag*> children = tag->getChildren();
int size = children.size();
for (int i = 0; i < size; i++)
{
QString type = children[i]->getType();
if (type == "Rect" || type == "rect")
{
shape->readSelf(children[i]);
shapes.push_back(shape);
}
else if (type == "RoundedRect" || type == "roundedrect")
{
shape->readSelf(children[i]);
shapes.push_back(shape);
}
else if (type == "Ellipse" || type == "ellipse")
{
shape->readSelf(children[i]);
shapes.push_back(shape);
}
}
}
void TableGraphics::draw(QPainter* painter)
{
int size = shapes.size();
for (int i = 0; i < size; i++)
shapes[i]->drawSelf(painter);
}
void TableGraphics::createSolids(b2Body* body)
{
int size = shapes.size();
for (int i = 0; i < size; i++)
if (shapes[i]->getSolid())
shapes[i]->createSolid(body);
}