-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCMapLabelArranger.cpp
212 lines (168 loc) · 4.79 KB
/
CMapLabelArranger.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
// CMapLabelArranger.cpp
//
// CMapLabelArranger class
// Copyright (c) 2018 Kronosaur Productions, LLC. All Rights Reserved.
#include "PreComp.h"
static constexpr int LABEL_SPACING_X = 8;
static constexpr int LABEL_SPACING_Y = 4;
static constexpr int LABEL_OVERLAP_Y = 1;
static constexpr Metric MAP_VERTICAL_ADJUST = 1.4;
void CMapLabelArranger::Arrange (CSystem *pSystem)
// Arrange
//
// Arranges labels for objects in the system.
{
int i;
const int MAX_LABELS = 100;
int iLabelCount = 0;
SLabelEntry Labels[MAX_LABELS];
// Compute some font metrics
int cxChar = g_pUniverse->GetNamedFont(CUniverse::fontMapLabel).GetAverageWidth();
int cyChar = g_pUniverse->GetNamedFont(CUniverse::fontMapLabel).GetHeight();
// Compute a transform for map coordinate
ViewportTransform Trans(CVector(),
g_MapKlicksPerPixel,
g_MapKlicksPerPixel * MAP_VERTICAL_ADJUST,
0,
0);
// Loop over all objects and see if they have a map label
for (i = 0; i < pSystem->GetObjectCount() && iLabelCount < MAX_LABELS; i++)
{
CSpaceObject *pObj = pSystem->GetObject(i);
if (pObj && pObj->HasMapLabel())
{
Labels[iLabelCount].pObj = pObj;
Trans.Transform(pObj->GetPos(), &Labels[iLabelCount].x, &Labels[iLabelCount].y);
Labels[iLabelCount].cxLabel = g_pUniverse->GetNamedFont(CUniverse::fontMapLabel).MeasureText(pObj->GetNounPhrase(nounTitleCapitalize));
SetLabelLeft(Labels[iLabelCount], cyChar);
iLabelCount++;
}
}
// Keep looping until we minimize overlap
bool bOverlap;
int iIteration = 0;
do
{
bOverlap = CalcOverlap(Labels, iLabelCount);
if (bOverlap)
{
// Modify the label location of any overlapping labels
for (i = 0; i < iLabelCount; i++)
{
switch (Labels[i].iNewPosition)
{
case posRight:
{
SetLabelRight(Labels[i], cyChar);
break;
}
case posLeft:
{
SetLabelLeft(Labels[i], cyChar);
break;
}
case posBottom:
{
SetLabelBelow(Labels[i], cyChar);
break;
}
}
}
iIteration++;
}
}
while (bOverlap && iIteration < 10);
// Set the label position for all the objects
for (i = 0; i < iLabelCount; i++)
Labels[i].pObj->SetMapLabelPos(Labels[i].iPosition);
}
void CMapLabelArranger::CalcLabelPos (const CString &sLabel, EPositions iPos, int &xMapLabel, int &yMapLabel)
// CalcLabelPos
//
// Calculate the position of the label relative to the object center.
{
const CG16bitFont &Font = g_pUniverse->GetNamedFont(CUniverse::fontMapLabel);
int cyLabel = Font.GetHeight();
switch (iPos)
{
case posLeft:
{
int cxLabel = Font.MeasureText(sLabel);
xMapLabel = -(LABEL_SPACING_X + cxLabel);
yMapLabel = -(cyLabel / 2);
break;
}
case posBottom:
{
int cxLabel = Font.MeasureText(sLabel);
xMapLabel = -(cxLabel / 2);
yMapLabel = LABEL_SPACING_Y;
break;
}
// Defaults to posRight
default:
xMapLabel = LABEL_SPACING_X;
yMapLabel = -(cyLabel / 2);
break;
}
}
bool CMapLabelArranger::CalcOverlap (SLabelEntry *pEntries, int iCount)
{
bool bOverlap = false;
int i, j;
for (i = 0; i < iCount; i++)
{
pEntries[i].iNewPosition = posNone;
for (j = 0; j < iCount; j++)
if (i != j)
{
if (RectsIntersect(pEntries[i].rcLabel, pEntries[j].rcLabel))
{
int xDelta = pEntries[j].x - pEntries[i].x;
int yDelta = pEntries[j].y - pEntries[i].y;
switch (pEntries[i].iPosition)
{
case posRight:
{
if (xDelta > 0)
pEntries[i].iNewPosition = posLeft;
break;
}
case posLeft:
{
if (xDelta < 0)
pEntries[i].iNewPosition = posBottom;
break;
}
}
bOverlap = true;
break;
}
}
}
return bOverlap;
}
void CMapLabelArranger::SetLabelBelow (SLabelEntry &Entry, int cyChar)
{
Entry.rcLabel.top = Entry.y + LABEL_SPACING_Y + LABEL_OVERLAP_Y;
Entry.rcLabel.bottom = Entry.rcLabel.top + cyChar - (2 * LABEL_OVERLAP_Y);
Entry.rcLabel.left = Entry.x - (Entry.cxLabel / 2);
Entry.rcLabel.right = Entry.rcLabel.left + Entry.cxLabel;
Entry.iPosition = posBottom;
}
void CMapLabelArranger::SetLabelLeft (SLabelEntry &Entry, int cyChar)
{
Entry.rcLabel.left = Entry.x - (LABEL_SPACING_X + Entry.cxLabel);
Entry.rcLabel.top = Entry.y - (cyChar / 2) + LABEL_OVERLAP_Y;
Entry.rcLabel.right = Entry.rcLabel.left + Entry.cxLabel;
Entry.rcLabel.bottom = Entry.rcLabel.top + cyChar - (2 * LABEL_OVERLAP_Y);
Entry.iPosition = posLeft;
}
void CMapLabelArranger::SetLabelRight (SLabelEntry &Entry, int cyChar)
{
Entry.rcLabel.left = Entry.x + LABEL_SPACING_X;
Entry.rcLabel.top = Entry.y - (cyChar / 2) + LABEL_OVERLAP_Y;
Entry.rcLabel.right = Entry.rcLabel.left + Entry.cxLabel;
Entry.rcLabel.bottom = Entry.rcLabel.top + cyChar - (2 * LABEL_OVERLAP_Y);
Entry.iPosition = posRight;
}