-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCGalacticMapPainter.cpp
543 lines (404 loc) · 14.5 KB
/
CGalacticMapPainter.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// CGalacticMapPainter.cpp
//
// CGalacticMapPainter class
// Copyright (c) 2016 by Kronosaur Productions, LLC. All Rights Reserved.
#include "PreComp.h"
const int NODE_RADIUS = 6;
const int HIT_TEST_RADIUS = 8;
const int SELECTION_RADIUS = 24;
const int SELECTION_WIDTH = 4;
const int STARGATE_LINE_WIDTH = 1;
const int MIN_NODE_MARGIN = 64;
const int NAME_PADDING_X = 4;
const int NAME_SPACING_Y = 4;
const int CORNER_RADIUS = 2;
const Metric MIN_SYSTEM_THUMB_SCALE = 0.5;
const CG32bitPixel RGB_STARGATE = CG32bitPixel(178, 217, 255); // H:210 S:30 B:100
const CG32bitPixel RGB_SYSTEM_NAME = CG32bitPixel(128, 191, 255); // H:210 S:50 B:100
//const CG32bitPixel RGB_SYSTEM_NAME = CG32bitPixel(204, 184, 163); // H:30 S:20 B:80
//const CG32bitPixel RGB_SYSTEM_NAME = CG32bitPixel(143, 159, 179); // H:210 S:20 B:70
//const CG32bitPixel RGB_SYSTEM_NAME = CG32bitPixel(115, 121, 128); // H:210 S:10 B:50
const CG32bitPixel RGB_SYSTEM_NAME_BACK = CG32bitPixel(23, 24, 26, 96); // H:210 S:10 B:20
#define FONT_MEDIUM CONSTLIT("Medium")
#define COLOR_SELECTION CONSTLIT("AreaDialogHighlight")
CGalacticMapPainter::CGalacticMapPainter (const CUniverse &Universe, CSystemMap *pMap, CSystemMapThumbnails &SystemMapThumbnails) :
m_Universe(Universe),
m_MediumFont(m_Universe.GetFont(FONT_MEDIUM)),
m_rgbSelection(m_Universe.GetColor(COLOR_SELECTION)),
m_pMap(pMap),
m_SystemMapThumbnails(SystemMapThumbnails),
m_cxMap(-1),
m_cyMap(-1),
m_bFreeImage(false),
m_rImageScale(1.0),
m_pImage(NULL),
m_pSelected(NULL),
m_iSelectAngle(0),
m_iScale(100),
m_xCenter(0),
m_yCenter(0)
// CGalacticMapPainter constructor
{
RECT rcRect;
rcRect.left = 0;
rcRect.top = 0;
rcRect.right = 100;
rcRect.bottom = 100;
SetViewport(rcRect);
Init();
}
CGalacticMapPainter::~CGalacticMapPainter (void)
// CGalacticMapPainter destructor
{
if (m_pImage && m_bFreeImage)
delete m_pImage;
}
void CGalacticMapPainter::AdjustCenter (int xCenter, int yCenter, int iScale, int *retxCenter, int *retyCenter) const
// AdjustCenter
//
// Adjusts the center coordinates so that the map is always fully visible. The
// viewport must be set, but scale and position are inputs to this.
{
// Compute the dimensions of the map to paint
int cxWidth = m_cxMap;
int cyHeight = m_cyMap;
int cxMap = (100 * RectWidth(m_rcView) / iScale);
int cyMap = (100 * RectHeight(m_rcView) / iScale);
// Compute the given center in map image coordinates
int xMapCenter = Max(cxMap / 2, Min((cxWidth / 2) + xCenter, cxWidth - (cxMap / 2)));
int yMapCenter = Max(cyMap / 2, Min((cyHeight / 2) - yCenter, cyHeight - (cyMap / 2)));
// Convert back after adjustment
if ((iScale * cxWidth / 100) < RectWidth(m_rcView))
*retxCenter = 0;
else
*retxCenter = xMapCenter - (cxWidth / 2);
if ((iScale * cyHeight / 100) < RectHeight(m_rcView))
*retyCenter = 0;
else
*retyCenter = (cyHeight / 2) - yMapCenter;
}
void CGalacticMapPainter::DrawNode (CG32bitImage &Dest, CTopologyNode *pNode, int x, int y, Metric rScale, CG32bitPixel rgbColor) const
// DrawNode
//
// Draws a topology node
{
// Draw selection, if necessary
if (pNode == m_pSelected)
DrawSelection(Dest, x, y, m_rgbSelection);
// Draw the node (either as a full system thumbnail or just the stars)
#ifdef FULL_SYSTEM_THUMBNAILS
bool bFullSystem = ((pNode->GetLastVisitedTime() != 0xffffffff) && rScale > MIN_SYSTEM_THUMB_SCALE);
#else
bool bFullSystem = false;
#endif
m_SystemMapThumbnails.DrawThumbnail(pNode, Dest, x, y, bFullSystem, rScale);
// Draw the name label
int cxName = m_MediumFont.MeasureText(pNode->GetSystemName());
int cxNameBack = cxName + 2 * NAME_PADDING_X;
int xNameBack = x - (cxNameBack / 2);
int yName = y + NODE_RADIUS + NAME_SPACING_Y;
CGDraw::RoundedRect(Dest, xNameBack, yName, cxNameBack, m_MediumFont.GetHeight(), CORNER_RADIUS, RGB_SYSTEM_NAME_BACK);
m_MediumFont.DrawText(Dest,
xNameBack + NAME_PADDING_X, yName,
RGB_SYSTEM_NAME,
pNode->GetSystemName(),
0);
// Debug info
if (g_pUniverse->InDebugMode() && m_pMap->DebugShowAttributes())
{
CString sLine = pNode->GetAttributes();
#if 0
int iPos = strFind(sLine, CONSTLIT("level"));
if (iPos != -1)
sLine = strSubString(sLine, iPos, 7);
#endif
m_MediumFont.DrawText(Dest,
x,
y + NODE_RADIUS + 2 + m_MediumFont.GetHeight(),
CG32bitPixel(128, 128, 128),
sLine,
CG16bitFont::AlignCenter);
}
}
void CGalacticMapPainter::DrawNodeConnections (CG32bitImage &Dest, CTopologyNode *pNode, int x, int y) const
// DrawNodeConnections
//
// Draws gate connections from this node to all other known systems.
{
int i, j;
// Compute some visual attributes.
CG32bitPixel rgbStargateColor = (m_pMap ? m_pMap->GetStargateLineColor() : RGB_STARGATE);
int iStargateLine = Max(1, mathRound(STARGATE_LINE_WIDTH * (Metric)m_iScale / 100.0));
// Paint each gate line
for (i = 0; i < pNode->GetStargateCount(); i++)
{
CTopologyNode::SStargateRouteDesc RouteDesc;
pNode->GetStargateRouteDesc(i, &RouteDesc);
if (RouteDesc.pToNode
&& !RouteDesc.bUncharted
&& !RouteDesc.pToNode->IsMarked()
&& RouteDesc.pToNode->IsKnown())
{
// Figure out position of destination node.
SPoint End;
RouteDesc.pToNode->GetDisplayPos(&End.x, &End.y);
End = Xform(End);
// If this is a curved path, the draw it
if (RouteDesc.MidPoints.GetCount() > 0)
{
int iCurMid = 0;
while (iCurMid <= RouteDesc.MidPoints.GetCount())
{
// If we're at the end and we don't have enough points
// for a curve, then just draw a line.
//
// LATER: We try to do a curved line.
if (iCurMid == RouteDesc.MidPoints.GetCount())
{
SPoint ptFrom = Xform(RouteDesc.MidPoints[iCurMid - 1]);
Dest.DrawLine(ptFrom.x, ptFrom.y, End.x, End.y, iStargateLine, rgbStargateColor);
}
// Otherwise, we draw a curve
else
{
SPoint ptStart = {x, y};
SPoint ptMid = Xform(RouteDesc.MidPoints[iCurMid]);
SPoint ptFrom = (iCurMid > 0 ? Xform(RouteDesc.MidPoints[iCurMid - 1]) : ptStart);
SPoint ptTo = (iCurMid + 1 < RouteDesc.MidPoints.GetCount() ? Xform(RouteDesc.MidPoints[iCurMid + 1]) : End);
CGDraw::QuadCurve(Dest, ptFrom.x, ptFrom.y, ptTo.x, ptTo.y, ptMid.x, ptMid.y, iStargateLine, rgbStargateColor);
}
iCurMid += 2;
}
// If in debug mode, paint all the intermediate nodes
if (g_pUniverse->InDebugMode())
{
for (j = 0; j < RouteDesc.MidPoints.GetCount(); j++)
{
SPoint ptPos = Xform(RouteDesc.MidPoints[j]);
Dest.DrawDot(ptPos.x, ptPos.y, CG32bitPixel(255, 128, 0), markerMediumCross);
}
}
}
// Otherwise, straight line
else
Dest.DrawLine(x, y, End.x, End.y, iStargateLine, rgbStargateColor);
}
}
}
void CGalacticMapPainter::DrawSelection (CG32bitImage &Dest, int x, int y, CG32bitPixel rgbColor) const
// DrawSelection
//
// Draws a selection
{
int i;
const int ANGLE_INC = 90;
// Draw four arcs
int iAngle = m_iSelectAngle;
for (i = 0; i < 4; i++)
{
CGDraw::Arc(Dest, x, y, SELECTION_RADIUS, iAngle, iAngle + ANGLE_INC, SELECTION_WIDTH, rgbColor, CGDraw::blendNormal, SELECTION_WIDTH, 0);
iAngle += ANGLE_INC;
}
m_iSelectAngle = AngleMod(m_iSelectAngle + 1);
}
void CGalacticMapPainter::DrawUnknownNode (CG32bitImage &Dest, CTopologyNode *pNode, int x, int y, Metric rScale, CG32bitPixel rgbColor) const
// DrawUnknownNode
//
// Draws a topology node
{
// Draw selection, if necessary
if (pNode == m_pSelected)
DrawSelection(Dest, x, y, m_rgbSelection);
// Draw a star
int RADIUS = 24;
int INTENSITY = 25;
CG32bitPixel RGB_PRIMARY = CG32bitPixel(255, 255, 255);
CG32bitPixel RGB_SECONDARY = CG32bitPixel(255, 255, 255);
BYTE OPACITY = 255;
// Radius changes based on scale
int iRadius = mathRound(rScale * RADIUS);
// Create a color table
TArray<CG32bitPixel> ColorTable;
CPaintHelper::CalcSmoothColorTable(iRadius, INTENSITY, RGB_PRIMARY, RGB_SECONDARY, OPACITY, &ColorTable);
// Paint
CGDraw::Circle(Dest, x, y, iRadius, ColorTable, CGDraw::blendScreen);
}
void CGalacticMapPainter::GalacticToView (int x, int y, int xCenter, int yCenter, int iScale, int *retx, int *rety) const
// GalacticToView
//
// Converts from galactic (map) coordinates to view coordinates (relative to
// the entire session).
{
*retx = m_xViewCenter + iScale * (x - xCenter) / 100;
*rety = m_yViewCenter + iScale * (yCenter - y) / 100;
}
bool CGalacticMapPainter::HitTest (int x, int y, SSelectResult &Result) const
// HitTest
//
// Returns TRUE if we clicked on a node
{
int i;
// Convert to galactic coordinates
int xPos, yPos;
ViewToGalactic(x, y, m_xCenter, m_yCenter, m_iScale, &xPos, &yPos);
// See if we hit a node
for (i = 0; i < g_pUniverse->GetTopologyNodeCount(); i++)
{
CTopologyNode *pNode = g_pUniverse->GetTopologyNode(i);
int xNode, yNode;
if (!pNode->IsPositionKnown()
|| pNode->IsEndGame()
|| pNode->GetDisplayPos(&xNode, &yNode) != m_pMap)
continue;
int xDiff = Absolute(xNode - xPos);
int yDiff = Absolute(yNode - yPos);
if (xDiff < HIT_TEST_RADIUS && yDiff < HIT_TEST_RADIUS)
{
Result.pNode = pNode;
return true;
}
}
// If we get this far, we did not find a node.
return false;
}
void CGalacticMapPainter::Init (void)
// Init
//
// Initialize what we need to paint.
{
int i;
if (m_cxMap == -1)
{
// Allocate a new image and initialize it with the background
m_pImage = m_pMap->CreateBackgroundImage(&m_rImageScale);
m_bFreeImage = (m_pImage != NULL);
// Compute the size of the map based on whatever is greater, then background
// image or the position of the nodes
int xMin = 0;
int xMax = 0;
int yMin = 0;
int yMax = 0;
for (i = 0; i < g_pUniverse->GetTopologyNodeCount(); i++)
{
CTopologyNode *pNode = g_pUniverse->GetTopologyNode(i);
int xPos, yPos;
if (pNode->GetDisplayPos(&xPos, &yPos) == m_pMap
&& pNode->IsPositionKnown()
&& !pNode->IsEndGame())
{
if (xPos < xMin)
xMin = xPos;
if (xPos > xMax)
xMax = xPos;
if (yPos < yMin)
yMin = yPos;
if (yPos > yMax)
yMax = yPos;
}
}
int cxExtent = 2 * Max(xMax, -xMin) + MIN_NODE_MARGIN;
int cyExtent = 2 * Max(yMax, -yMin) + MIN_NODE_MARGIN;
m_cxMap = Max(cxExtent, GetImageGalacticWidth());
m_cyMap = Max(cyExtent, GetImageGalacticHeight());
}
}
void CGalacticMapPainter::Paint (CG32bitImage &Dest) const
// Paint
//
// Paints the galactic map to the given destination, centered at the
// given coordinates.
//
// iScale 100 = normal
// iScale 200 = x2
// iScale 400 = x4
{
int i;
ASSERT(m_iScale > 0);
// System thumbnails are full-res at x4
Metric rScale = m_iScale / 400.0;
// Compute the stargate line width based on scale
int iStargateLine = Max(1, mathRound(STARGATE_LINE_WIDTH * (Metric)m_iScale / 100.0));
// Paint the image, if we have it
if (m_cxMap > 0 && m_cyMap > 0)
{
// Compute some metrics
CG32bitPixel rgbNodeColor = CG32bitPixel(255, 200, 128);
CG32bitPixel rgbStargateColor = (m_pMap ? m_pMap->GetStargateLineColor() : RGB_STARGATE);
int cxWidth = (m_pImage ? m_pImage->GetWidth() : 0);
int cyHeight = (m_pImage ? m_pImage->GetHeight() : 0);
// Compute the dimensions of the map to paint
int cxMap = (int)((100 * RectWidth(m_rcView) / m_iScale) * m_rImageScale);
int cyMap = (int)((100 * RectHeight(m_rcView) / m_iScale) * m_rImageScale);
// Compute the given center in map image coordinates
int xMapCenter = (cxWidth / 2) + (int)(m_xCenter * m_rImageScale);
int yMapCenter = (cyHeight / 2) - (int)(m_yCenter * m_rImageScale);
// Compute the upper-left corner of the map
int xMap = xMapCenter - (cxMap / 2);
int yMap = yMapCenter - (cyMap / 2);
// Fill the borders, in case we the image won't fit
if (xMap < 0)
{
int cx = -(int)(xMap * m_iScale / m_rImageScale) / 100;
Dest.Fill(m_rcView.left, m_rcView.top, cx, RectHeight(m_rcView), 0);
}
if (yMap < 0)
{
int cy = -(int)(yMap * m_iScale / m_rImageScale) / 100;
Dest.Fill(m_rcView.left, m_rcView.top, RectWidth(m_rcView), cy, 0);
}
if (xMap + cxMap > cxWidth)
{
int cx = (int)((m_iScale * ((xMap + cxMap) - cxWidth)) / m_rImageScale) / 100;
Dest.Fill(m_rcView.right - cx, m_rcView.top, cx, RectHeight(m_rcView), 0);
}
if (yMap + cyMap > cyHeight)
{
int cy = (int)((m_iScale * ((yMap + cyMap) - cyHeight)) / m_rImageScale) / 100;
Dest.Fill(m_rcView.left, m_rcView.bottom - cy, RectWidth(m_rcView), cy, 0);
}
// Blt
if (m_pImage)
CGDraw::BltScaled(Dest, m_rcView.left, m_rcView.top, RectWidth(m_rcView), RectHeight(m_rcView), *m_pImage, xMap, yMap, cxMap, cyMap);
// Loop over all nodes and clear marks on the ones that we need to draw
for (i = 0; i < g_pUniverse->GetTopologyNodeCount(); i++)
{
CTopologyNode *pNode = g_pUniverse->GetTopologyNode(i);
int xPos, yPos;
pNode->SetMarked(pNode->GetDisplayPos(&xPos, &yPos) != m_pMap
|| !pNode->IsPositionKnown()
|| pNode->IsEndGame());
}
// Paint the nodes
for (i = 0; i < g_pUniverse->GetTopologyNodeCount(); i++)
{
CTopologyNode *pNode = g_pUniverse->GetTopologyNode(i);
if (pNode->IsMarked())
continue;
SPoint Start;
pNode->GetDisplayPos(&Start.x, &Start.y);
Start = Xform(Start);
// Draw gate connections
if (pNode->IsKnown())
DrawNodeConnections(Dest, pNode, Start.x, Start.y);
// Draw star system
if (Start.x >= m_rcView.left && Start.x < m_rcView.right && Start.y >= m_rcView.top && Start.y < m_rcView.bottom)
{
if (pNode->IsKnown())
DrawNode(Dest, pNode, Start.x, Start.y, rScale, rgbNodeColor);
else
DrawUnknownNode(Dest, pNode, Start.x, Start.y, rScale, rgbNodeColor);
}
pNode->SetMarked();
}
}
else
Dest.Fill(m_rcView.left, m_rcView.top, RectWidth(m_rcView), RectHeight(m_rcView), 0);
}
void CGalacticMapPainter::ViewToGalactic (int x, int y, int xCenter, int yCenter, int iScale, int *retx, int *rety) const
// ViewToGalactic
//
// Converts from session/screen coordinates to galactic (map) coordinates.
{
*retx = (100 * (x - m_xViewCenter) / iScale) + xCenter;
*rety = yCenter - (100 * (y - m_yViewCenter) / iScale);
}