forked from endless-sky/endless-sky-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGalaxyView.cpp
369 lines (300 loc) · 10.2 KB
/
GalaxyView.cpp
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* GalaxyView.cpp
Copyright (c) 2015 by Michael Zahniser
Endless Sky is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Endless Sky is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
*/
#include "GalaxyView.h"
#include "Map.h"
#include "SpriteSet.h"
#include "SystemView.h"
#include <QInputDialog>
#include <QMessageBox>
#include <QPainter>
#include <QPalette>
#include <QMouseEvent>
#include <QTabWidget>
#include <algorithm>
#include <cmath>
using namespace std;
namespace {
// Map a value between -1 and 1 to a color.
QColor MapColor(double value)
{
value = min(1., max(-1., value));
if(value < 0.)
return QColor(
(.12 + .12 * value) * 255.9,
(.48 + .36 * value) * 255.9,
(.48 - .12 * value) * 255.9);
else
return QColor(
(.12 + .48 * value) * 255.9,
(.48 + .00 * value) * 255.9,
(.48 - .48 * value) * 255.9);
}
QColor MapGrey(double value)
{
value = max(0., min(1., (value + 1.) / 2.));
return QColor(
200. * value + 55.9,
200. * value + 55.9,
200. * value + 55.9);
}
}
GalaxyView::GalaxyView(Map &mapData, QTabWidget *tabs, QWidget *parent) :
QWidget(parent), mapData(mapData), tabs(tabs)
{
setAutoFillBackground(true);
QPalette p = palette();
p.setColor(backgroundRole(), QColor(0, 0, 0));
setPalette(p);
setToolTip("Left click to select a system. Drag to move a system or pan the view.\n"
"Right click to create a new system or to toggle links between systems.\n"
"Use the scroll wheel to zoom in and out.");
Center();
}
void GalaxyView::Center()
{
if(mapData.Systems().empty())
return;
offset = QVector2D();
for(const auto &it : mapData.Systems())
offset -= it.second.Position();
offset /= mapData.Systems().size();
}
void GalaxyView::SetSystemView(SystemView *view)
{
systemView = view;
}
void GalaxyView::SetCommodity(const QString &name)
{
if(commodity != name)
{
commodity = name;
government.clear();
update();
}
}
void GalaxyView::SetGovernment(const QString &name)
{
if(government != name)
{
government = name;
commodity.clear();
update();
}
}
void GalaxyView::KeyPress(QKeyEvent *event)
{
if(event->key() == Qt::Key_Delete || event->key() == Qt::Key_Backspace)
DeleteSystem();
}
void GalaxyView::DeleteSystem()
{
if(!systemView || !systemView->Selected())
return;
System *system = systemView->Selected();
QMessageBox::StandardButton button = QMessageBox::question(this, "Delete system",
"Are you sure you want to delete \"" + system->Name() + "\"?");
if(button == QMessageBox::Yes)
{
systemView->Select(nullptr);
while(!system->Links().empty())
system->ToggleLink(&mapData.Systems().find(system->Links().front())->second);
auto it = mapData.Systems().find(system->Name());
mapData.Systems().erase(it);
mapData.SetChanged();
}
}
void GalaxyView::mousePressEvent(QMouseEvent *event)
{
clickOff = QVector2D(event->pos()) - offset;
dragSystem = nullptr;
QVector2D origin = MapPoint(event->pos());
for(auto &it : mapData.Systems())
if(origin.distanceToPoint(it.second.Position()) < 10.)
{
dragSystem = &it.second;
break;
}
if(!dragSystem)
{
if(event->button() == Qt::RightButton)
{
QString text = QInputDialog::getText(this, "New system", "Name:");
if(!text.isEmpty())
{
auto it = mapData.Systems().find(text);
if(it != mapData.Systems().end())
QMessageBox::warning(this, "Duplicate name",
"A system named \"" + text + "\" already exists.");
else
{
System &system = mapData.Systems()[text];
system.Init(text, origin);
if(systemView && systemView->Selected())
{
System &previous = *systemView->Selected();
for(const Map::Commodity &commodity : mapData.Commodities())
system.SetTrade(commodity.name, previous.Trade(commodity.name));
system.SetGovernment(previous.Government());
}
else
for(const Map::Commodity &commodity : mapData.Commodities())
system.SetTrade(commodity.name, (commodity.low + commodity.high) / 2);
if(systemView)
systemView->Select(&system);
mapData.SetChanged();
update();
}
}
}
return;
}
if(event->button() == Qt::LeftButton)
{
dragTime.start();
clickOff = QVector2D(event->pos());
if(systemView)
{
systemView->Select(dragSystem);
update();
}
}
else if(event->button() == Qt::RightButton)
{
if(systemView && systemView->Selected())
{
systemView->Selected()->ToggleLink(dragSystem);
mapData.SetChanged();
update();
}
dragSystem = nullptr;
}
}
void GalaxyView::mouseDoubleClickEvent(QMouseEvent *event)
{
if(event->button() != Qt::LeftButton)
return;
clickOff = QVector2D(event->pos()) - offset;
if(!tabs || !systemView)
return;
QVector2D origin = MapPoint(event->pos());
for(auto &it : mapData.Systems())
if(origin.distanceToPoint(it.second.Position()) < 5.)
{
systemView->Select(&it.second);
tabs->setCurrentWidget(systemView);
return;
}
}
void GalaxyView::mouseMoveEvent(QMouseEvent *event)
{
if(!(event->buttons() & Qt::LeftButton))
return;
QVector2D distance = QVector2D(event->pos()) - clickOff;
if(!dragSystem)
offset = distance;
else
{
if(dragTime.elapsed() < 1000 && distance.length() < 5.)
return;
dragSystem->SetPosition(dragSystem->Position() + distance / scale);
mapData.SetChanged();
clickOff = QVector2D(event->pos());
}
update();
}
void GalaxyView::wheelEvent(QWheelEvent *event)
{
QVector2D point(event->pos());
QVector2D center(.5 * width(), .5 * height());
// point = origin * scale + offset + center.
QVector2D origin = (point - offset - center) / scale;
scale = max(.0625, min(1., scale * exp(event->delta() * .001)));
// We want: point = origin * scale + offset + center.
offset = point - origin * scale - center;
update();
}
void GalaxyView::paintEvent(QPaintEvent * /*event*/)
{
QPen blackPen;
QPen mediumPen(QColor(120, 120, 120));
QPen brightPen(QColor(180, 180, 180));
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
painter.translate(.5 * width(), .5 * height());
painter.translate(offset.x(), offset.y());
painter.scale(scale, scale);
// Draw the "galaxy" images.
for(const Galaxy &it : mapData.Galaxies())
{
QPixmap sprite = SpriteSet::Get(it.Sprite());
QPointF pos = (it.Position() - QVector2D(.5 * sprite.width(), .5 * sprite.height())).toPointF();
painter.drawPixmap(pos, sprite);
}
painter.setBrush(Qt::NoBrush);
for(const auto &it : mapData.Systems())
{
QPointF pos = it.second.Position().toPointF();
for(const QString &link : it.second.Links())
{
auto lit = mapData.Systems().find(link);
if(lit == mapData.Systems().end())
continue;
double value = 0.;
if(!commodity.isEmpty())
{
int difference = abs(it.second.Trade(commodity) - lit->second.Trade(commodity));
value = (difference - 60) / 60.;
}
else if(!government.isEmpty())
value = (it.second.Government() != lit->second.Government());
// Set the link color based on the "value".
QPen pen(value < 1. ? MapGrey(value) : QColor(255, 0, 0));
painter.setPen(pen);
painter.drawLine(pos, lit->second.Position().toPointF());
}
}
for(const auto &it : mapData.Systems())
{
QPointF pos = it.second.Position().toPointF();
bool isSelected = (systemView && &it.second == systemView->Selected());
double value = 0.;
if(!commodity.isEmpty())
value = mapData.MapPrice(commodity, it.second.Trade(commodity)) * 2. - 1.;
else if(!government.isEmpty())
value = (it.second.Government() == government);
// Set the link color based on the "value".
QColor color = MapColor(value);
if(isSelected)
color.setRgbF(color.redF() * 1.5, color.greenF() * 1.5, color.blueF() * 1.5);
QBrush brush(color);
painter.setBrush(brush);
painter.setPen(blackPen);
painter.drawEllipse(pos, 5, 5);
painter.drawText(pos + QPointF(6, 6), it.first);
painter.setPen(brightPen);
painter.drawText(pos + QPointF(5, 5), it.first);
}
painter.setPen(mediumPen);
painter.setBrush(Qt::NoBrush);
if(systemView && systemView->Selected())
{
QPointF pos = systemView->Selected()->Position().toPointF();
painter.drawEllipse(pos, 10, 10);
}
}
// Figure out where in the 100% scale image the click occurred.
QVector2D GalaxyView::MapPoint(QPoint pos) const
{
QVector2D point(pos);
QVector2D center(.5 * width(), .5 * height());
// point = origin * scale + offset + center.
return (point - offset - center) / scale;
}