1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
/*
mapwidget.cc
This file is part of the Project::OSiRiON world editor
and is distributed under the terms and conditions of
the GNU General Public License version 2
*/
#include "entitywidget.h"
#include "manipulator.h"
#include "mapwidget.h"
#include <QPainter>
#include <QMouseEvent>
#include <QWheelEvent>
#include <QDebug>
#include <QtGui>
namespace editor
{
MapWidget::MapWidget(QWidget *parent) : QWidget(parent)
{
//setMinimumSize(256,256);
mapwidget_manipulator = new Manipulator(this);
// zoom sucks but it works
mapwidget_zoom = 16;
// center of the map (used for dragging)
center_x = 0;
center_y = 0;
dragstart_x = 0;
dragstart_y = 0;
is_dragging = false;
setFocusPolicy(Qt::ClickFocus);
}
void MapWidget::wheelEvent(QWheelEvent *event)
{
if (event->orientation() == Qt::Vertical) {
int oldzoom = mapwidget_zoom;
mapwidget_zoom -= event->delta() / 120;
if (mapwidget_zoom < 1)
mapwidget_zoom = 1;
else if (mapwidget_zoom > 64) {
mapwidget_zoom = 64;
}
center_x = center_x * oldzoom / mapwidget_zoom;
center_y = center_y * oldzoom / mapwidget_zoom;
resizeChildren();
update();
event->accept();
}
}
void MapWidget::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::RightButton) {
dragstart_x = event->pos().x();
dragstart_y = event->pos().y();
event->accept();
is_dragging = true;
} else {
is_dragging = false;
}
}
void MapWidget::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::RightButton) {
is_dragging = false;
}
}
void MapWidget::mouseMoveEvent(QMouseEvent *event)
{
if (is_dragging) {
int delta_x = event->pos().x() - dragstart_x;
int delta_y = event->pos().y() - dragstart_y;
center_x += delta_x;
center_y += delta_y;
//qDebug() << "dragging " << delta_x << " " << delta_y;
dragstart_x = event->pos().x();
dragstart_y = event->pos().y();
event->accept();
resizeChildren();
update();
}
}
void MapWidget::resizeChildren()
{
if ((width() < 16) || (height() < 16))
return;
const float scale = (float) width() / (float) (mapwidget_zoom * 256);
for (int i = 0; i < mapwidget_enties.size(); ++i) {
EntityWidget * entity = mapwidget_enties.at(i);
int radius = (int) (entity->properties()->radius() * scale);
if (radius < 6)
radius = 6;
int x = width() / 2 - (int) (entity->properties()->location()[1] * scale) - radius;
int y = height() / 2 - (int)(entity->properties()->location()[0] * scale) - radius;
entity->setGeometry(center_x + x, center_y + y, radius * 2 , radius * 2);
//qDebug() << "Moving entity to " << x << " " << y;
if (entity->selected()) {
mapwidget_manipulator->setSize(entity);
mapwidget_manipulator->setProperties(entity->properties());
mapwidget_manipulator->update();
}
}
}
void MapWidget::deselect()
{
for (int i = 0; i < mapwidget_enties.size(); ++i) {
EntityWidget *entitywidget = mapwidget_enties.at(i);
entitywidget->set_selected(false);
}
emit propertiesChanged(0);
mapwidget_manipulator->setMode(Manipulator::None);
}
void MapWidget::dragEntity(EntityWidget *entity, int x, int y)
{
const float scale = (float) (mapwidget_zoom * 256) / (float) width();
int radius = (int) (entity->properties()->radius() / scale);
if (radius < 6)
radius = 6;
entity->properties()->set_location(
entity->properties()->location()[0] - (int) ((float) (y - radius) * scale),
entity->properties()->location()[1] - (int) ((float) (x - radius) * scale),
entity->properties()->location()[2]
);
emit propertiesChanged(entity->properties());
resizeChildren();
mapwidget_manipulator->setMode(Manipulator::Move);
}
void MapWidget::select(EntityWidget *entity)
{
mapwidget_manipulator->setMode(Manipulator::None);
for (int i = 0; i < mapwidget_enties.size(); ++i) {
EntityWidget *entitywidget = mapwidget_enties.at(i);
if (entity == entitywidget) {
entity->set_selected(true);
//qDebug() << "selected entity " << entity->name();
emit propertiesChanged(entity->properties());
mapwidget_manipulator->setMode(Manipulator::Select);
mapwidget_manipulator->setProperties(entity->properties());
mapwidget_manipulator->setSize(entity);
} else {
entitywidget->set_selected(false);
}
}
update();
}
void MapWidget::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Escape) {
deselect();
event->accept();
} else {
event->ignore();
}
}
void MapWidget::resizeEvent(QResizeEvent *event)
{
resizeChildren();
}
void MapWidget::paintEvent(QPaintEvent *event)
{
if ((width() < 16) || (height() < 16))
return;
const QColor gridlinecolor(0, 0, 128);
const QColor axislinecolor(128, 0, 0);
QPen pen(gridlinecolor, 1, Qt::SolidLine);
QPainter painter(this);
painter.setPen(pen);
// mapwidget_zoom is used to scale from game units to screen coorinates
// 1 grid square is 256 game units
// gridsize is the size of one grid square in screen coordinates
int gridsize = width() / mapwidget_zoom;
int x = center_x + width() / 2;
pen.setColor(axislinecolor);
painter.setPen(pen);
painter.drawLine(x, 0, x, height());
x -= gridsize;
pen.setColor(gridlinecolor);
painter.setPen(pen);
while (x > 0) {
painter.drawLine(x, 0, x, height());
x -= gridsize;
}
x = center_x + width() / 2 + gridsize;
while (x <= width()) {
painter.drawLine(x, 0, x, height());
x += gridsize;
}
int y = center_y + height() / 2;
pen.setColor(axislinecolor);
painter.setPen(pen);
painter.drawLine(0, y, width(), y);
y -= gridsize;
pen.setColor(gridlinecolor);
painter.setPen(pen);
while (y > 0) {
painter.drawLine(0, y, width(), y);
y -= gridsize;
}
y = center_y + height() / 2 + gridsize;
while (y <= height()) {
painter.drawLine(0, y, width(), y);
y += gridsize;
}
}
EntityWidget *MapWidget::addEntity()
{
EntityWidget *entitywidget = new EntityWidget(this);
mapwidget_enties.append(entitywidget);
// connect the EntityWidget::clicked() signal to the MapWidget::selected() slot
connect(entitywidget, SIGNAL(clicked(EntityWidget *)), this, SLOT(select(EntityWidget *)));
connect(entitywidget, SIGNAL(dragMove(EntityWidget *, int, int)), this, SLOT(dragEntity(EntityWidget *, int, int)));
entitywidget->show();
resizeChildren();
update();
return entitywidget;
}
void MapWidget::save(QTextStream &textstream)
{
for (int i = 0; i < mapwidget_enties.size(); ++i) {
EntityWidget *entitywidget = mapwidget_enties.at(i);
entitywidget->properties()->save(textstream);
}
}
void MapWidget::duplicateSelected()
{
}
void MapWidget::deleteSelected()
{
}
}
|