-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Paint.LargeScenery.cpp
422 lines (374 loc) 路 14.3 KB
/
Paint.LargeScenery.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
/*****************************************************************************
* Copyright (c) 2014-2024 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "../Paint.h"
#include "../../Game.h"
#include "../../GameState.h"
#include "../../config/Config.h"
#include "../../core/Numerics.hpp"
#include "../../core/String.hpp"
#include "../../interface/Viewport.h"
#include "../../localisation/Formatter.h"
#include "../../localisation/Formatting.h"
#include "../../localisation/Localisation.h"
#include "../../object/LargeSceneryObject.h"
#include "../../profiling/Profiling.h"
#include "../../ride/Ride.h"
#include "../../ride/TrackDesign.h"
#include "../../util/Util.h"
#include "../../world/Banner.h"
#include "../../world/Map.h"
#include "../../world/Scenery.h"
#include "../../world/TileInspector.h"
#include "../Boundbox.h"
#include "../support/WoodenSupports.h"
#include "Paint.TileElement.h"
using namespace OpenRCT2;
// clang-format off
static constexpr BoundBoxXY LargeSceneryBoundBoxes[] = {
{ { 3, 3 }, { 26, 26 } },
{ { 17, 17 }, { 12, 12 } },
{ { 17, 3 }, { 12, 12 } },
{ { 17, 3 }, { 12, 26 } },
{ { 3, 3 }, { 12, 12 } },
{ { 3, 3 }, { 26, 26 } },
{ { 3, 3 }, { 28, 12 } },
{ { 3, 3 }, { 26, 26 } },
{ { 3, 17 }, { 12, 12 } },
{ { 3, 17 }, { 26, 12 } },
{ { 3, 3 }, { 26, 26 } },
{ { 3, 3 }, { 26, 26 } },
{ { 3, 3 }, { 12, 28 } },
{ { 3, 3 }, { 26, 26 } },
{ { 3, 3 }, { 26, 26 } },
{ { 3, 3 }, { 26, 26 } },
{ { 1, 1 }, { 30, 30 } },
};
// clang-format on
static void PaintLargeScenerySupports(
PaintSession& session, uint8_t direction, uint16_t height, const LargeSceneryElement& tileElement, ImageId imageTemplate,
const LargeSceneryTile& tile)
{
PROFILED_FUNCTION();
if (tile.flags & LARGE_SCENERY_TILE_FLAG_NO_SUPPORTS)
return;
auto transitionType = WoodenSupportTransitionType::None;
auto supportHeight = height;
if (supportHeight & 0xF)
{
supportHeight &= ~0xF;
transitionType = WoodenSupportTransitionType::Scenery;
}
WoodenBSupportsPaintSetupRotated(
session, WoodenSupportType::Truss, WoodenSupportSubType::NeSw, direction, supportHeight, imageTemplate, transitionType);
int32_t clearanceHeight = Ceil2(tileElement.GetClearanceZ() + 15, 16);
if (tile.flags & LARGE_SCENERY_TILE_FLAG_ALLOW_SUPPORTS_ABOVE)
{
PaintUtilSetSegmentSupportHeight(session, kSegmentsAll, clearanceHeight, 0x20);
}
else
{
PaintUtilSetSegmentSupportHeight(session, kSegmentsAll, 0xFFFF, 0);
}
PaintUtilSetGeneralSupportHeight(session, clearanceHeight, 0x20);
}
static std::string_view LargeSceneryCalculateDisplayText(const LargeSceneryText& text, std::string_view s, bool height)
{
size_t totalSize = 0;
CodepointView view(s);
auto it = view.begin();
while (it != view.end() && totalSize <= text.max_width)
{
auto glyph = text.GetGlyph(*it, ' ');
totalSize += height ? glyph.height : glyph.width;
it++;
}
auto totalLength = it.GetIndex();
return s.substr(0, totalLength);
}
static int32_t DivToMinusInfinity(int32_t a, int32_t b)
{
return (a / b) - (a % b < 0);
}
static void PaintLargeScenery3DTextLine(
PaintSession& session, const LargeSceneryEntry& sceneryEntry, const LargeSceneryText& text, std::string_view line,
ImageId imageTemplate, Direction direction, int32_t offsetY)
{
PROFILED_FUNCTION();
line = LargeSceneryCalculateDisplayText(text, line, false);
auto width = text.MeasureWidth(line);
auto offsetX = text.offset[(direction & 1)].x;
auto acc = offsetY * ((direction & 1) ? -1 : 1);
if (!(text.flags & LARGE_SCENERY_TEXT_FLAG_VERTICAL))
{
// sign is horizontal, centre text:
offsetX -= (width / 2);
acc -= (width / 2);
}
for (auto codepoint : CodepointView(line))
{
auto glyph = text.GetGlyph(codepoint, ' ');
// Upcasting from uint8_t to uint32_t to avoid an overflow.
uint32_t glyphOffset = glyph.image_offset;
auto glyphType = direction & 1;
if (text.flags & LARGE_SCENERY_TEXT_FLAG_VERTICAL)
{
glyphOffset *= 2;
}
else
{
glyphOffset *= 4;
// set slightly different glyph on horizontal sign, which was rendered 1/2 pixel lower to deal with aliasing:
if (direction & 1)
{
if (!(acc & 1))
{
glyphType += 2;
}
}
else
{
if ((acc & 1))
{
glyphType += 2;
}
}
}
auto imageIndex = sceneryEntry.text_image + glyphOffset + glyphType;
auto imageId = imageTemplate.WithIndex(imageIndex);
if (direction == 3)
{
PaintAttachToPreviousPS(session, imageId, offsetX, -DivToMinusInfinity(acc, 2));
}
else if (text.flags & LARGE_SCENERY_TEXT_FLAG_VERTICAL)
{
PaintAttachToPreviousPS(session, imageId, offsetX, DivToMinusInfinity(acc, 2));
}
else
{
PaintAttachToPreviousAttach(session, imageId, offsetX, DivToMinusInfinity(acc, 2));
}
offsetX += glyph.width;
acc += glyph.width;
}
}
static bool Is3DTextSingleLine(const LargeSceneryText& text, std::string_view s)
{
if (text.flags & LARGE_SCENERY_TEXT_FLAG_TWO_LINE)
{
auto width = text.MeasureWidth(s);
return width <= text.max_width;
}
return true;
}
static void PaintLargeScenery3DText(
PaintSession& session, const LargeSceneryEntry& sceneryEntry, const LargeSceneryTile& tile,
const LargeSceneryElement& tileElement, uint8_t direction, uint16_t height, bool isGhost)
{
PROFILED_FUNCTION();
if (sceneryEntry.tiles[1].x_offset != -1)
{
auto sequenceDirection = (tileElement.GetSequenceIndex() - 1) & 3;
if (sequenceDirection != direction)
{
return;
}
}
if (session.DPI.zoom_level > ZoomLevel{ 1 })
return;
auto banner = tileElement.GetBanner();
if (banner == nullptr)
return;
const auto* text = sceneryEntry.text;
if (text == nullptr)
return;
auto textColour = isGhost ? static_cast<colour_t>(COLOUR_GREY) : tileElement.GetSecondaryColour();
auto imageTemplate = ImageId().WithPrimary(textColour);
char signString[256];
auto ft = Formatter();
banner->FormatTextTo(ft);
OpenRCT2::FormatStringLegacy(signString, sizeof(signString), STR_STRINGID, ft.Data());
auto offsetY = text->offset[(direction & 1)].y * 2;
if (text->flags & LARGE_SCENERY_TEXT_FLAG_VERTICAL)
{
// Vertical sign
offsetY++;
auto displayText = LargeSceneryCalculateDisplayText(*text, signString, true);
auto displayTextHeight = text->MeasureHeight(displayText);
for (auto codepoint : CodepointView(displayText))
{
char line[8]{};
UTF8WriteCodepoint(line, codepoint);
PaintLargeScenery3DTextLine(
session, sceneryEntry, *text, line, imageTemplate, direction, offsetY - displayTextHeight);
auto glyph = text->GetGlyph(codepoint, ' ');
offsetY += glyph.height * 2;
}
}
else
{
// Horizontal sign
offsetY -= (direction & 1);
if (Is3DTextSingleLine(*text, signString))
{
PaintLargeScenery3DTextLine(session, sceneryEntry, *text, signString, imageTemplate, direction, offsetY);
}
else
{
auto lineHeight = text->GetGlyph('A')->height + 1;
offsetY -= lineHeight;
// Split the string into two lines at best position
auto current = std::string_view(signString);
std::string_view next;
for (int32_t lineIndex = 0; lineIndex < 2; lineIndex++)
{
std::string_view best;
CodepointView view(current);
auto lineWidth = 0;
auto it = view.begin();
while (it != view.end() && lineWidth < text->max_width)
{
// Trim any leading spaces
auto codepoint = *it;
if (codepoint != ' ' || lineWidth != 0)
{
// Determine if this is a good place to split
if (codepoint == ' ' || codepoint == '\n')
{
auto index = it.GetIndex();
best = current.substr(0, index);
next = current.substr(index + 1);
if (codepoint == '\n')
break;
}
auto glyph = text->GetGlyph(*it, ' ');
lineWidth += glyph.width;
}
it++;
}
if (best.empty())
{
// No good split found, or reached end of string
auto index = it.GetIndex();
best = current.substr(0, index);
next = current.substr(index);
}
PaintLargeScenery3DTextLine(session, sceneryEntry, *text, best, imageTemplate, direction, offsetY);
current = next;
offsetY += lineHeight * 2;
}
}
}
}
static void PaintLargeSceneryScrollingText(
PaintSession& session, const LargeSceneryEntry& sceneryEntry, const LargeSceneryElement& tileElement, uint8_t direction,
uint16_t height, const CoordsXYZ& bbOffset, bool isGhost)
{
PROFILED_FUNCTION();
auto textColour = isGhost ? static_cast<colour_t>(COLOUR_GREY) : tileElement.GetSecondaryColour();
auto textPaletteIndex = direction == 0 ? ColourMapA[textColour].mid_dark : ColourMapA[textColour].light;
auto banner = tileElement.GetBanner();
if (banner == nullptr)
return;
auto ft = Formatter();
banner->FormatTextTo(ft);
char text[256];
if (gConfigGeneral.UpperCaseBanners)
{
FormatStringToUpper(text, sizeof(text), STR_SCROLLING_SIGN_TEXT, ft.Data());
}
else
{
OpenRCT2::FormatStringLegacy(text, sizeof(text), STR_SCROLLING_SIGN_TEXT, ft.Data());
}
auto scrollMode = sceneryEntry.scrolling_mode + ((direction + 1) & 3);
auto stringWidth = GfxGetStringWidth(text, FontStyle::Tiny);
auto scroll = stringWidth > 0 ? (GetGameState().CurrentTicks / 2) % stringWidth : 0;
auto imageId = ScrollingTextSetup(session, STR_SCROLLING_SIGN_TEXT, ft, scroll, scrollMode, textPaletteIndex);
PaintAddImageAsChild(session, imageId, { 0, 0, height + 25 }, { bbOffset, { 1, 1, 21 } });
}
void PaintLargeScenery(PaintSession& session, uint8_t direction, uint16_t height, const LargeSceneryElement& tileElement)
{
PROFILED_FUNCTION();
if (session.ViewFlags & VIEWPORT_FLAG_HIGHLIGHT_PATH_ISSUES)
return;
auto sequenceNum = tileElement.GetSequenceIndex();
const auto* object = tileElement.GetObject();
if (object == nullptr)
return;
const auto* sceneryEntry = tileElement.GetEntry();
if (sceneryEntry == nullptr)
return;
const auto* tile = object->GetTileForSequence(sequenceNum);
if (tile == nullptr)
return;
session.InteractionType = ViewportInteractionItem::LargeScenery;
auto isGhost = false;
ImageId imageTemplate;
if (gTrackDesignSaveMode && !TrackDesignSaveContainsTileElement(reinterpret_cast<const TileElement*>(&tileElement)))
{
imageTemplate = ImageId().WithRemap(FilterPaletteID::Palette46);
isGhost = true;
}
else if (tileElement.IsGhost())
{
session.InteractionType = ViewportInteractionItem::None;
imageTemplate = ImageId().WithRemap(FilterPaletteID::PaletteGhost);
isGhost = true;
}
else if (session.SelectedElement == reinterpret_cast<const TileElement*>(&tileElement))
{
imageTemplate = ImageId().WithRemap(FilterPaletteID::PaletteGhost);
isGhost = true;
}
else
{
if (sceneryEntry->flags & LARGE_SCENERY_FLAG_HAS_PRIMARY_COLOUR)
{
imageTemplate = imageTemplate.WithPrimary(tileElement.GetPrimaryColour());
}
if (sceneryEntry->flags & LARGE_SCENERY_FLAG_HAS_SECONDARY_COLOUR)
{
imageTemplate = imageTemplate.WithSecondary(tileElement.GetSecondaryColour());
}
if (sceneryEntry->flags & LARGE_SCENERY_FLAG_HAS_TERTIARY_COLOUR)
{
imageTemplate = imageTemplate.WithTertiary(tileElement.GetTertiaryColour());
}
}
auto boxlengthZ = std::min<uint8_t>(tile->z_clearance, 128) - 3;
auto flags = tile->flags;
auto bbIndex = 16;
if (flags & 0xF00)
{
flags &= 0xF000;
flags = Numerics::rol16(flags, direction);
bbIndex = (flags & 0xF) | (flags >> 12);
}
const CoordsXYZ& bbOffset = { LargeSceneryBoundBoxes[bbIndex].offset, height };
const CoordsXYZ& bbLength = { LargeSceneryBoundBoxes[bbIndex].length, boxlengthZ };
auto imageIndex = sceneryEntry->image + 4 + (sequenceNum << 2) + direction;
PaintAddImageAsParent(session, imageTemplate.WithIndex(imageIndex), { 0, 0, height }, { bbOffset, bbLength });
if (sceneryEntry->scrolling_mode != SCROLLING_MODE_NONE && direction != 1 && direction != 2)
{
if (sceneryEntry->flags & LARGE_SCENERY_FLAG_3D_TEXT)
{
PaintLargeScenery3DText(session, *sceneryEntry, *tile, tileElement, direction, height, isGhost);
}
else if (session.DPI.zoom_level <= ZoomLevel{ 0 })
{
auto sequenceDirection2 = (tileElement.GetSequenceIndex() - 1) & 3;
if (sequenceDirection2 == direction)
{
PaintLargeSceneryScrollingText(session, *sceneryEntry, tileElement, direction, height, bbOffset, isGhost);
}
}
}
PaintLargeScenerySupports(
session, direction, height, tileElement, isGhost ? imageTemplate : ImageId(0, COLOUR_BLACK), *tile);
}