/
mapindicator.cpp
236 lines (191 loc) · 7.48 KB
/
mapindicator.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
#include "mapindicator.h"
#include <iostream>
#include <QTimer>
#include <cmath>
#include <QFont>
#include "instrument_panel.h"
using namespace std;
MapIndicator::MapIndicator(QJsonObject params, int nr, InstrumentPanel *parent, bool edit, bool overlay) :
GenericIndicator(params, nr, parent, edit, overlay)
{
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(got_image(QNetworkReply*)));
disconnect(parent, SIGNAL(panel_update(Gamestate*)), this, SLOT(update_ind(Gamestate*)));
connect(parent, SIGNAL(map_update(Gamestate*)), this, SLOT(update_ind(Gamestate*)));
text.setText("");
text.resize(params["w"].toDouble(), params["h"].toDouble());
mapObjects = new QWidget(this);
mapObjects->resize(text.size());
mapObjects->move(0,0);
this->resize(text.size());
QPixmap p(size());
p.fill(QColor(100, 100,100, 100));
text.setPixmap(p);
this->mapValid = false;
}
void MapIndicator::update_ind(Gamestate* actual)
{
QJsonArray mapObj = actual->mapObj.array();
int player = -1;
bool playerTank = 0;
float mapW = actual->mapInfo.object()["map_max"].toArray()[0].toDouble() - actual->mapInfo.object()["map_min"].toArray()[0].toDouble();
float mapH = actual->mapInfo.object()["map_max"].toArray()[1].toDouble() - actual->mapInfo.object()["map_min"].toArray()[1].toDouble();
for (int i =0; i<mapObj.size(); ++i)
{
if(mapObj[i].toObject()["icon"].toString() == QString("Player"))
{
player = i;
if(mapObj[i].toObject()["type"].toString()==QString("ground_model"))
{
playerTank = 1;
}
break;
}
}
if(mapObj.isEmpty() or player==-1 or (!playerTank and overlay))
{
mapValid = false;
this->hide();
}
else if ((!overlay) or playerTank)
{
if(mapValid==false)
{
if(this->overlay)
{
QPixmap n(text.size());
n.fill(QColor(0,0,0,0));
text.setPixmap(n);
cout << "Setting empty pixmap"<<endl;
}
else
{
QUrl url(dynamic_cast<InstrumentPanel*>(this->parent())->settings["url"].toString()+QString("/map.img"));
QNetworkRequest request(url);
cout << "Downloading image " << request.url().toString().toStdString()<<endl;
manager->get(request);
}
}
//cout << "Painting map Object!"<<endl;
double playerX = mapObj[player].toObject()["x"].toDouble();
double playerY = mapObj[player].toObject()["y"].toDouble();
qDeleteAll(mapObjects->findChildren<QLabel*>());
for (int i =0; i<mapObj.size(); ++i)
{
QLabel* obj; ;
double x = mapObj[i].toObject()["x"].toDouble();
double y = mapObj[i].toObject()["y"].toDouble();
int mapX = x*this->size().width();
int mapY = y*this->size().height();
int dist = sqrt((x-playerX)*mapW*(x-playerX)*mapW+(y-playerY)*mapH*(y-playerY)*mapW);
//cout << dist<<endl;
QWidget* obscure = mapObjects->childAt(mapX, mapY);
int obscureX = text.fontMetrics().size(Qt::TextSingleLine, "00").width();
int obscureY = text.fontMetrics().size(Qt::TextSingleLine, "00").height();
QWidget* obscure2 = mapObjects->childAt(mapX+obscureX, mapY+obscureY);
QWidget* obscure3 = mapObjects->childAt(mapX-obscureX, mapY-obscureY);
QWidget* obscure4 = mapObjects->childAt(mapX-obscureX, mapY+obscureY);
QWidget* obscure5 = mapObjects->childAt(mapX+obscureX, mapY-obscureY);
if(!(obscure or obscure2 or obscure3 or obscure4 or obscure5))
{
obj = new QLabel(mapObjects);
obj->setText( QString::number(dist));
if(this->size().width()>400 and params["scaling_text"].toBool())
{
QFont ff = obj->font();
ff.setPixelSize(this->size().width()/26);
obj->setFont(ff);
}
obj->move(mapX, mapY);
obj->show();
}
if(!overlay)
{
obj = new QLabel(mapObjects);
if( mapObj[i].toObject()["icon_bg"].toString().indexOf("Target")>-1)
{
obj->setStyleSheet(QString("QLabel { color: gold}"));
}
else
{
obj->setStyleSheet(QString("QLabel { color: ") + mapObj[i].toObject()["color"].toString()+QString("}"));
}
if(mapObj[i].toObject()["type"].toString() == QString("aircraft"))
{
obj->setText("^");
}
else if(mapObj[i].toObject()["type"].toString() == QString("ground_model"))
{
obj->setText("=");
}
if(mapObj[i].toObject()["icon"].toString() == QString("Player"))
{
obj->setText("o");
obj->setStyleSheet(QString("QLabel { font: bold; color: green}"));
}
if(this->size().width()>400 and params["scaling_text"].toBool())
{
QFont ff = obj->font();
ff.setPixelSize(this->size().width()/26);
obj->setFont(ff);
}
//obj->setStyleSheet(QString("QLabel { color: ") + mapObj[i].toObject()["color"].toString()+QString("}"));
obj->move(mapX-obj->fontMetrics().size(Qt::TextSingleLine, "0").height()/2, mapY-obj->fontMetrics().size(Qt::TextSingleLine, "0").height()/2);
obj->show();
}
/*if( mapObj[i].toObject()["icon_bg"].toString().indexOf("Target")>-1)
{
obj = new QLabel(mapObjects);
obj->setText( QString("Target distance: ")+QString::number(dist));
// obj->setStyleSheet(QString("QLabel { color: ") + mapObj[i].toObject()["color"].toString()+QString("}"));
obj->move(0,0);
obj->show();
obj->setStyleSheet(QString("QLabel { font: bold 15px; color: red}"));
obj->resize(obj->sizeHint());
}*/
}
mapObjects->raise();
this->show();
mapValid = true;
}
}
void MapIndicator::hide_later()
{
if(!mapValid)
{
this->hide();
}
}
void MapIndicator::wheelEvent(QWheelEvent* e)
{
if(editMode)
{
//std::cout <<"Resizing " <<text.size().width()<<std::endl;
float diff = e->angleDelta().y()/120;
QSize d(diff, diff);
std::cout << diff <<std::endl;
QSize old = this->size();
text.resize(old+=d);
QPixmap p(size());
p.fill(QColor(100, 100,100, 100));
text.setPixmap(p);
mapObjects->resize(text.size());
this->resize(text.size());
}
}
void MapIndicator::got_image(QNetworkReply* reply)
{
if (reply->error() == QNetworkReply::NoError)
{
cout << "getting IMAGE"<<endl;
QByteArray Data = reply->readAll();
cout <<Data.size()<<"size"<<endl;
QPixmap pixmap;
cout << "Succes? " << pixmap.loadFromData(Data,0,Qt::ColorOnly )<< endl;
text.setPixmap(pixmap.scaled(text.size(), Qt::KeepAspectRatio));
text.repaint();
cout <<text.size().width()<<endl;
}
else
this->mapValid = false;
}