-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Scenery.cpp
1406 lines (1258 loc) 路 52.2 KB
/
Scenery.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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
* Copyright (c) 2014-2020 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 <algorithm>
#include <openrct2-ui/interface/Dropdown.h>
#include <openrct2-ui/interface/Viewport.h>
#include <openrct2-ui/interface/Widget.h>
#include <openrct2-ui/windows/Window.h>
#include <openrct2/Context.h>
#include <openrct2/Input.h>
#include <openrct2/audio/audio.h>
#include <openrct2/core/Guard.hpp>
#include <openrct2/localisation/Formatter.h>
#include <openrct2/localisation/Localisation.h>
#include <openrct2/management/Research.h>
#include <openrct2/network/network.h>
#include <openrct2/object/ObjectList.h>
#include <openrct2/sprites.h>
#include <openrct2/world/LargeScenery.h>
#include <openrct2/world/Park.h>
#include <openrct2/world/Scenery.h>
#include <openrct2/world/SmallScenery.h>
static constexpr const rct_string_id WINDOW_TITLE = STR_NONE;
constexpr int32_t WINDOW_SCENERY_WIDTH = 634;
constexpr int32_t WINDOW_SCENERY_HEIGHT = 180;
constexpr int32_t SCENERY_BUTTON_WIDTH = 66;
constexpr int32_t SCENERY_BUTTON_HEIGHT = 80;
constexpr uint8_t SceneryContentScrollIndex = 0;
enum WindowSceneryListWidgetIdx
{
WIDX_SCENERY_BACKGROUND,
WIDX_SCENERY_TITLE,
WIDX_SCENERY_CLOSE,
WIDX_SCENERY_TAB_CONTENT_PANEL,
WIDX_SCENERY_LIST,
WIDX_SCENERY_ROTATE_OBJECTS_BUTTON,
WIDX_SCENERY_REPAINT_SCENERY_BUTTON,
WIDX_SCENERY_PRIMARY_COLOUR_BUTTON,
WIDX_SCENERY_SECONDARY_COLOUR_BUTTON,
WIDX_SCENERY_TERTIARY_COLOUR_BUTTON,
WIDX_SCENERY_EYEDROPPER_BUTTON,
WIDX_SCENERY_BUILD_CLUSTER_BUTTON,
WIDX_SCENERY_TAB_1,
};
validate_global_widx(WC_SCENERY, WIDX_SCENERY_TAB_1);
validate_global_widx(WC_SCENERY, WIDX_SCENERY_ROTATE_OBJECTS_BUTTON);
validate_global_widx(WC_SCENERY, WIDX_SCENERY_EYEDROPPER_BUTTON);
// clang-format off
static rct_widget WindowSceneryBaseWidgets[] = {
WINDOW_SHIM(WINDOW_TITLE, WINDOW_SCENERY_WIDTH, WINDOW_SCENERY_HEIGHT),
MakeWidget ({ 0, 43}, {634, 99}, WindowWidgetType::Resize, WindowColour::Secondary ), // 8 0x009DE2C8
MakeWidget ({ 2, 47}, {607, 80}, WindowWidgetType::Scroll, WindowColour::Secondary, SCROLL_VERTICAL ), // 1000000 0x009DE418
MakeWidget ({609, 44}, { 24, 24}, WindowWidgetType::FlatBtn, WindowColour::Secondary, SPR_ROTATE_ARROW, STR_ROTATE_OBJECTS_90 ), // 2000000 0x009DE428
MakeWidget ({609, 68}, { 24, 24}, WindowWidgetType::FlatBtn, WindowColour::Secondary, SPR_PAINTBRUSH, STR_SCENERY_PAINTBRUSH_TIP ), // 4000000 0x009DE438
MakeWidget ({615, 93}, { 12, 12}, WindowWidgetType::ColourBtn, WindowColour::Secondary, 0xFFFFFFFF, STR_SELECT_COLOUR ), // 8000000 0x009DE448
MakeWidget ({615, 105}, { 12, 12}, WindowWidgetType::ColourBtn, WindowColour::Secondary, 0xFFFFFFFF, STR_SELECT_SECONDARY_COLOUR), // 10000000 0x009DE458
MakeWidget ({615, 117}, { 12, 12}, WindowWidgetType::ColourBtn, WindowColour::Secondary, 0xFFFFFFFF, STR_SELECT_TERNARY_COLOUR ), // 20000000 0x009DE468
MakeWidget ({609, 130}, { 24, 24}, WindowWidgetType::FlatBtn, WindowColour::Secondary, SPR_G2_EYEDROPPER, STR_SCENERY_EYEDROPPER_TIP ), // 40000000 0x009DE478
MakeWidget ({609, 154}, { 24, 24}, WindowWidgetType::FlatBtn, WindowColour::Secondary, SPR_SCENERY_CLUSTER, STR_SCENERY_CLUSTER_TIP ), // 40000000 0x009DE478
WIDGETS_END,
};
// clang-format on
std::vector<ScenerySelection> gWindowSceneryTabSelections;
size_t gWindowSceneryActiveTabIndex;
uint8_t gWindowSceneryPaintEnabled;
uint8_t gWindowSceneryRotation;
colour_t gWindowSceneryPrimaryColour;
colour_t gWindowScenerySecondaryColour;
colour_t gWindowSceneryTertiaryColour;
bool gWindowSceneryEyedropperEnabled;
class SceneryWindow final : public Window
{
private:
struct SceneryItem
{
int32_t allRows;
int32_t selected_item;
ScenerySelection scenerySelection;
};
struct SceneryTabInfo
{
ObjectEntryIndex SceneryGroupIndex = OBJECT_ENTRY_INDEX_NULL;
std::vector<ScenerySelection> Entries;
bool IsMisc() const
{
return SceneryGroupIndex == OBJECT_ENTRY_INDEX_NULL;
}
bool Contains(const ScenerySelection& entry) const
{
return std::find(std::begin(Entries), std::end(Entries), entry) != std::end(Entries);
}
void AddEntry(const ScenerySelection& entry)
{
if (!Contains(entry))
{
Entries.push_back(entry);
}
}
const rct_scenery_group_entry* GetSceneryGroupEntry() const
{
return get_scenery_group_entry(SceneryGroupIndex);
}
};
std::vector<SceneryTabInfo> _tabEntries;
std::vector<rct_widget> _widgets;
ScenerySelection _selectedScenery;
int16_t _hoverCounter;
public:
void OnOpen() override
{
Init();
InitScrollWidgets();
ContentUpdateScroll();
show_gridlines();
gWindowSceneryRotation = 3;
gSceneryCtrlPressed = false;
gSceneryShiftPressed = false;
_selectedScenery = {};
_hoverCounter = 0;
window_push_others_below(this);
gSceneryGhostType = 0;
gSceneryPlaceCost = MONEY32_UNDEFINED;
gSceneryPlaceRotation = 0;
gWindowSceneryPaintEnabled = 0; // repaint coloured scenery tool state
gWindowSceneryEyedropperEnabled = false;
min_width = WINDOW_SCENERY_WIDTH;
max_width = WINDOW_SCENERY_WIDTH;
min_height = WINDOW_SCENERY_HEIGHT;
max_height = WINDOW_SCENERY_HEIGHT;
}
void OnClose() override
{
scenery_remove_ghost_tool_placement();
hide_gridlines();
viewport_set_visibility(0);
if (gWindowSceneryScatterEnabled)
window_close_by_class(WC_SCENERY_SCATTER);
if (scenery_tool_is_active())
tool_cancel();
}
void OnMouseUp(rct_widgetindex widgetIndex) override
{
switch (widgetIndex)
{
case WIDX_SCENERY_CLOSE:
if (gWindowSceneryScatterEnabled)
window_close_by_class(WC_SCENERY_SCATTER);
Close();
break;
case WIDX_SCENERY_ROTATE_OBJECTS_BUTTON:
gWindowSceneryRotation++;
gWindowSceneryRotation = gWindowSceneryRotation % 4;
scenery_remove_ghost_tool_placement();
Invalidate();
break;
case WIDX_SCENERY_REPAINT_SCENERY_BUTTON:
gWindowSceneryPaintEnabled ^= 1;
gWindowSceneryEyedropperEnabled = false;
if (gWindowSceneryScatterEnabled)
window_close_by_class(WC_SCENERY_SCATTER);
Invalidate();
break;
case WIDX_SCENERY_EYEDROPPER_BUTTON:
gWindowSceneryPaintEnabled = 0;
gWindowSceneryEyedropperEnabled = !gWindowSceneryEyedropperEnabled;
if (gWindowSceneryScatterEnabled)
window_close_by_class(WC_SCENERY_SCATTER);
scenery_remove_ghost_tool_placement();
Invalidate();
break;
case WIDX_SCENERY_BUILD_CLUSTER_BUTTON:
gWindowSceneryPaintEnabled = 0;
gWindowSceneryEyedropperEnabled = false;
if (gWindowSceneryScatterEnabled)
window_close_by_class(WC_SCENERY_SCATTER);
else if (
network_get_mode() != NETWORK_MODE_CLIENT
|| network_can_perform_command(network_get_current_player_group_index(), -2))
{
WindowSceneryScatterOpen();
}
else
{
context_show_error(STR_CANT_DO_THIS, STR_PERMISSION_DENIED, {});
}
Invalidate();
break;
}
}
void OnResize() override
{
if (width < min_width)
{
Invalidate();
width = min_width;
Invalidate();
}
if (width > max_width)
{
Invalidate();
width = max_width;
Invalidate();
}
if (height < min_height)
{
Invalidate();
height = min_height;
Invalidate();
// HACK: For some reason invalidate has not been called
window_event_invalidate_call(this);
ContentUpdateScroll();
}
if (height > max_height)
{
Invalidate();
height = max_height;
Invalidate();
// HACK: For some reason invalidate has not been called
window_event_invalidate_call(this);
ContentUpdateScroll();
}
}
void OnMouseDown(rct_widgetindex widgetIndex) override
{
switch (widgetIndex)
{
case WIDX_SCENERY_PRIMARY_COLOUR_BUTTON:
WindowDropdownShowColour(this, &widgets[widgetIndex], colours[1], gWindowSceneryPrimaryColour);
break;
case WIDX_SCENERY_SECONDARY_COLOUR_BUTTON:
WindowDropdownShowColour(this, &widgets[widgetIndex], colours[1], gWindowScenerySecondaryColour);
break;
case WIDX_SCENERY_TERTIARY_COLOUR_BUTTON:
WindowDropdownShowColour(this, &widgets[widgetIndex], colours[1], gWindowSceneryTertiaryColour);
break;
}
if (widgetIndex >= WIDX_SCENERY_TAB_1)
{
gWindowSceneryActiveTabIndex = widgetIndex - WIDX_SCENERY_TAB_1;
Invalidate();
gSceneryPlaceCost = MONEY32_UNDEFINED;
ContentUpdateScroll();
}
}
void OnDropdown(rct_widgetindex widgetIndex, int32_t dropdownIndex) override
{
if (dropdownIndex == -1)
return;
if (widgetIndex == WIDX_SCENERY_PRIMARY_COLOUR_BUTTON)
{
gWindowSceneryPrimaryColour = static_cast<colour_t>(dropdownIndex);
}
else if (widgetIndex == WIDX_SCENERY_SECONDARY_COLOUR_BUTTON)
{
gWindowScenerySecondaryColour = static_cast<colour_t>(dropdownIndex);
}
else if (widgetIndex == WIDX_SCENERY_TERTIARY_COLOUR_BUTTON)
{
gWindowSceneryTertiaryColour = static_cast<colour_t>(dropdownIndex);
}
Invalidate();
}
void OnPeriodicUpdate() override
{
if (!_selectedScenery.IsUndefined())
{
// Find out what scenery the cursor is over
const CursorState* state = context_get_cursor_state();
rct_widgetindex widgetIndex = window_find_widget_from_point(this, state->position);
if (widgetIndex == WIDX_SCENERY_LIST)
{
ScreenCoordsXY scrollPos = {};
int32_t scrollArea = 0;
int32_t scrollId = 0;
WidgetScrollGetPart(this, &widgets[WIDX_SCENERY_LIST], state->position, scrollPos, &scrollArea, &scrollId);
if (scrollArea == SCROLL_PART_VIEW)
{
const ScenerySelection scenery = GetSceneryIdByCursorPos(scrollPos);
if (scenery == _selectedScenery)
{
return;
}
}
}
// Cursor was not over the currently hover selected scenery so reset hover selection.
// This will happen when the mouse leaves the scroll window and is required so that the cost and description switch
// to the tool scenery selection.
_selectedScenery = {};
}
}
void OnUpdate() override
{
const CursorState* state = context_get_cursor_state();
rct_window* other = window_find_from_point(state->position);
if (other == this)
{
ScreenCoordsXY window = state->position - ScreenCoordsXY{ windowPos.x - 26, windowPos.y };
if (window.y < 44 || window.x <= width)
{
rct_widgetindex widgetIndex = window_find_widget_from_point(this, state->position);
if (widgetIndex >= WIDX_SCENERY_TAB_CONTENT_PANEL)
{
_hoverCounter++;
if (_hoverCounter < 8)
{
if (input_get_state() != InputState::ScrollLeft)
{
min_width = WINDOW_SCENERY_WIDTH;
max_width = WINDOW_SCENERY_WIDTH;
min_height = WINDOW_SCENERY_HEIGHT;
max_height = WINDOW_SCENERY_HEIGHT;
}
}
else
{
const auto& listWidget = widgets[WIDX_SCENERY_LIST];
const auto nonListHeight = height - listWidget.height() + 2;
const auto numRows = static_cast<int32_t>(CountRows());
const auto maxContentHeight = numRows * SCENERY_BUTTON_HEIGHT;
const auto maxWindowHeight = maxContentHeight + nonListHeight;
const auto windowHeight = std::clamp(maxWindowHeight, WINDOW_SCENERY_HEIGHT, 463);
min_width = WINDOW_SCENERY_WIDTH;
max_width = WINDOW_SCENERY_WIDTH;
min_height = windowHeight;
max_height = windowHeight;
}
}
}
}
else
{
_hoverCounter = 0;
if (input_get_state() != InputState::ScrollLeft)
{
min_width = WINDOW_SCENERY_WIDTH;
max_width = WINDOW_SCENERY_WIDTH;
min_height = WINDOW_SCENERY_HEIGHT;
max_height = WINDOW_SCENERY_HEIGHT;
}
}
Invalidate();
if (!scenery_tool_is_active())
{
Close();
return;
}
if (gWindowSceneryEyedropperEnabled)
{
gCurrentToolId = Tool::Crosshair;
}
else if (gWindowSceneryPaintEnabled == 1)
{
gCurrentToolId = Tool::PaintDown;
}
else
{
const auto tabIndex = gWindowSceneryActiveTabIndex;
const auto tabSelectedScenery = GetSelectedScenery(tabIndex);
if (!tabSelectedScenery.IsUndefined())
{
if (tabSelectedScenery.SceneryType == SCENERY_TYPE_BANNER)
{
gCurrentToolId = Tool::EntranceDown;
}
else if (tabSelectedScenery.SceneryType == SCENERY_TYPE_LARGE)
{
gCurrentToolId = static_cast<Tool>(get_large_scenery_entry(tabSelectedScenery.EntryIndex)->tool_id);
}
else if (tabSelectedScenery.SceneryType == SCENERY_TYPE_WALL)
{
gCurrentToolId = static_cast<Tool>(get_wall_entry(tabSelectedScenery.EntryIndex)->tool_id);
}
else if (tabSelectedScenery.SceneryType == SCENERY_TYPE_PATH_ITEM)
{ // path bit
gCurrentToolId = static_cast<Tool>(get_footpath_item_entry(tabSelectedScenery.EntryIndex)->tool_id);
}
else
{ // small scenery
gCurrentToolId = static_cast<Tool>(get_small_scenery_entry(tabSelectedScenery.EntryIndex)->tool_id);
}
}
}
}
ScreenSize OnScrollGetSize(int32_t scrollIndex) override
{
if (scrollIndex == SceneryContentScrollIndex)
{
return ContentScrollGetSize();
}
return {};
}
void OnScrollMouseDown(int32_t scrollIndex, const ScreenCoordsXY& screenCoords) override
{
if (scrollIndex == SceneryContentScrollIndex)
{
ContentScrollMouseDown(screenCoords);
}
}
void OnScrollMouseOver(int32_t scrollIndex, const ScreenCoordsXY& screenCoords) override
{
if (scrollIndex == SceneryContentScrollIndex)
{
ContentScrollMouseOver(screenCoords);
}
}
OpenRCT2String OnTooltip(const rct_widgetindex widgetIndex, const rct_string_id fallback) override
{
if (widgetIndex >= WIDX_SCENERY_TAB_1)
{
const auto tabIndex = static_cast<size_t>(widgetIndex - WIDX_SCENERY_TAB_1);
if (_tabEntries.size() > tabIndex)
{
const auto& tabInfo = _tabEntries[tabIndex];
if (tabInfo.IsMisc())
{
auto ft = Formatter();
ft.Add<rct_string_id>(STR_MISCELLANEOUS);
return { fallback, ft };
}
const auto* sceneryEntry = tabInfo.GetSceneryGroupEntry();
if (sceneryEntry != nullptr)
{
auto ft = Formatter();
ft.Add<rct_string_id>(sceneryEntry->name);
return { fallback, ft };
}
}
}
return { STR_NONE, Formatter() };
}
void OnPrepareDraw() override
{
// Set the window title
rct_string_id titleStringId = STR_MISCELLANEOUS;
const auto tabIndex = gWindowSceneryActiveTabIndex;
if (tabIndex < _tabEntries.size())
{
const auto& tabInfo = _tabEntries[tabIndex];
const auto* sgEntry = tabInfo.GetSceneryGroupEntry();
if (sgEntry != nullptr)
{
titleStringId = sgEntry->name;
}
}
widgets[WIDX_SCENERY_TITLE].text = titleStringId;
pressed_widgets = 0;
pressed_widgets |= 1ULL << (tabIndex + WIDX_SCENERY_TAB_1);
if (gWindowSceneryPaintEnabled == 1)
pressed_widgets |= (1ULL << WIDX_SCENERY_REPAINT_SCENERY_BUTTON);
if (gWindowSceneryEyedropperEnabled)
pressed_widgets |= (1ULL << WIDX_SCENERY_EYEDROPPER_BUTTON);
if (gWindowSceneryScatterEnabled)
pressed_widgets |= (1ULL << WIDX_SCENERY_BUILD_CLUSTER_BUTTON);
widgets[WIDX_SCENERY_ROTATE_OBJECTS_BUTTON].type = WindowWidgetType::Empty;
widgets[WIDX_SCENERY_EYEDROPPER_BUTTON].type = WindowWidgetType::Empty;
widgets[WIDX_SCENERY_BUILD_CLUSTER_BUTTON].type = WindowWidgetType::Empty;
if (!(gWindowSceneryPaintEnabled & 1))
{
widgets[WIDX_SCENERY_EYEDROPPER_BUTTON].type = WindowWidgetType::FlatBtn;
}
const auto tabSelectedScenery = GetSelectedScenery(tabIndex);
if (!tabSelectedScenery.IsUndefined())
{
if (tabSelectedScenery.SceneryType == SCENERY_TYPE_SMALL)
{
if (!(gWindowSceneryPaintEnabled & 1))
{
widgets[WIDX_SCENERY_BUILD_CLUSTER_BUTTON].type = WindowWidgetType::FlatBtn;
}
auto* sceneryEntry = get_small_scenery_entry(tabSelectedScenery.EntryIndex);
if (sceneryEntry->HasFlag(SMALL_SCENERY_FLAG_ROTATABLE))
{
widgets[WIDX_SCENERY_ROTATE_OBJECTS_BUTTON].type = WindowWidgetType::FlatBtn;
}
}
else if (tabSelectedScenery.SceneryType >= SCENERY_TYPE_LARGE)
{
widgets[WIDX_SCENERY_ROTATE_OBJECTS_BUTTON].type = WindowWidgetType::FlatBtn;
}
}
widgets[WIDX_SCENERY_PRIMARY_COLOUR_BUTTON].image = SPRITE_ID_PALETTE_COLOUR_1(gWindowSceneryPrimaryColour)
| IMAGE_TYPE_TRANSPARENT | SPR_PALETTE_BTN;
widgets[WIDX_SCENERY_SECONDARY_COLOUR_BUTTON].image = SPRITE_ID_PALETTE_COLOUR_1(gWindowScenerySecondaryColour)
| IMAGE_TYPE_TRANSPARENT | SPR_PALETTE_BTN;
widgets[WIDX_SCENERY_TERTIARY_COLOUR_BUTTON].image = SPRITE_ID_PALETTE_COLOUR_1(gWindowSceneryTertiaryColour)
| IMAGE_TYPE_TRANSPARENT | SPR_PALETTE_BTN;
widgets[WIDX_SCENERY_PRIMARY_COLOUR_BUTTON].type = WindowWidgetType::Empty;
widgets[WIDX_SCENERY_SECONDARY_COLOUR_BUTTON].type = WindowWidgetType::Empty;
widgets[WIDX_SCENERY_TERTIARY_COLOUR_BUTTON].type = WindowWidgetType::Empty;
if (gWindowSceneryPaintEnabled & 1)
{ // repaint coloured scenery tool is on
widgets[WIDX_SCENERY_PRIMARY_COLOUR_BUTTON].type = WindowWidgetType::ColourBtn;
widgets[WIDX_SCENERY_SECONDARY_COLOUR_BUTTON].type = WindowWidgetType::ColourBtn;
widgets[WIDX_SCENERY_TERTIARY_COLOUR_BUTTON].type = WindowWidgetType::ColourBtn;
widgets[WIDX_SCENERY_ROTATE_OBJECTS_BUTTON].type = WindowWidgetType::Empty;
}
else if (!tabSelectedScenery.IsUndefined())
{
if (tabSelectedScenery.SceneryType == SCENERY_TYPE_BANNER)
{
auto* bannerEntry = get_banner_entry(tabSelectedScenery.EntryIndex);
if (bannerEntry->flags & BANNER_ENTRY_FLAG_HAS_PRIMARY_COLOUR)
{
widgets[WIDX_SCENERY_PRIMARY_COLOUR_BUTTON].type = WindowWidgetType::ColourBtn;
}
}
else if (tabSelectedScenery.SceneryType == SCENERY_TYPE_LARGE)
{
auto* sceneryEntry = get_large_scenery_entry(tabSelectedScenery.EntryIndex);
if (sceneryEntry->flags & LARGE_SCENERY_FLAG_HAS_PRIMARY_COLOUR)
widgets[WIDX_SCENERY_PRIMARY_COLOUR_BUTTON].type = WindowWidgetType::ColourBtn;
if (sceneryEntry->flags & LARGE_SCENERY_FLAG_HAS_SECONDARY_COLOUR)
widgets[WIDX_SCENERY_SECONDARY_COLOUR_BUTTON].type = WindowWidgetType::ColourBtn;
if (sceneryEntry->flags & LARGE_SCENERY_FLAG_HAS_TERTIARY_COLOUR)
widgets[WIDX_SCENERY_TERTIARY_COLOUR_BUTTON].type = WindowWidgetType::ColourBtn;
}
else if (tabSelectedScenery.SceneryType == SCENERY_TYPE_WALL)
{
auto* wallEntry = get_wall_entry(tabSelectedScenery.EntryIndex);
if (wallEntry->flags & (WALL_SCENERY_HAS_PRIMARY_COLOUR | WALL_SCENERY_HAS_GLASS))
{
widgets[WIDX_SCENERY_PRIMARY_COLOUR_BUTTON].type = WindowWidgetType::ColourBtn;
if (wallEntry->flags & WALL_SCENERY_HAS_SECONDARY_COLOUR)
{
widgets[WIDX_SCENERY_SECONDARY_COLOUR_BUTTON].type = WindowWidgetType::ColourBtn;
if (wallEntry->flags2 & WALL_SCENERY_2_NO_SELECT_PRIMARY_COLOUR)
widgets[WIDX_SCENERY_PRIMARY_COLOUR_BUTTON].type = WindowWidgetType::Empty;
if (wallEntry->flags & WALL_SCENERY_HAS_TERNARY_COLOUR)
widgets[WIDX_SCENERY_TERTIARY_COLOUR_BUTTON].type = WindowWidgetType::ColourBtn;
}
}
}
else if (tabSelectedScenery.SceneryType == SCENERY_TYPE_SMALL)
{
auto* sceneryEntry = get_small_scenery_entry(tabSelectedScenery.EntryIndex);
if (sceneryEntry->HasFlag(SMALL_SCENERY_FLAG_HAS_PRIMARY_COLOUR | SMALL_SCENERY_FLAG_HAS_GLASS))
{
widgets[WIDX_SCENERY_PRIMARY_COLOUR_BUTTON].type = WindowWidgetType::ColourBtn;
if (sceneryEntry->HasFlag(SMALL_SCENERY_FLAG_HAS_SECONDARY_COLOUR))
widgets[WIDX_SCENERY_SECONDARY_COLOUR_BUTTON].type = WindowWidgetType::ColourBtn;
}
}
}
auto windowWidth = width;
if (_tabEntries.size() > 0)
{
const auto lastTabIndex = _tabEntries.size() - 1;
const auto lastTabWidget = &widgets[WIDX_SCENERY_TAB_1 + lastTabIndex];
windowWidth = std::max<int32_t>(windowWidth, lastTabWidget->right + 3);
}
min_width = windowWidth;
max_width = windowWidth;
widgets[WIDX_SCENERY_BACKGROUND].right = windowWidth - 1;
widgets[WIDX_SCENERY_BACKGROUND].bottom = height - 1;
widgets[WIDX_SCENERY_TAB_CONTENT_PANEL].right = windowWidth - 1;
widgets[WIDX_SCENERY_TAB_CONTENT_PANEL].bottom = height - 1;
widgets[WIDX_SCENERY_TITLE].right = windowWidth - 2;
widgets[WIDX_SCENERY_CLOSE].left = windowWidth - 13;
widgets[WIDX_SCENERY_CLOSE].right = widgets[WIDX_SCENERY_CLOSE].left + 10;
widgets[WIDX_SCENERY_LIST].right = windowWidth - 26;
widgets[WIDX_SCENERY_LIST].bottom = height - 14;
widgets[WIDX_SCENERY_ROTATE_OBJECTS_BUTTON].left = windowWidth - 25;
widgets[WIDX_SCENERY_REPAINT_SCENERY_BUTTON].left = windowWidth - 25;
widgets[WIDX_SCENERY_EYEDROPPER_BUTTON].left = windowWidth - 25;
widgets[WIDX_SCENERY_BUILD_CLUSTER_BUTTON].left = windowWidth - 25;
widgets[WIDX_SCENERY_ROTATE_OBJECTS_BUTTON].right = windowWidth - 2;
widgets[WIDX_SCENERY_REPAINT_SCENERY_BUTTON].right = windowWidth - 2;
widgets[WIDX_SCENERY_EYEDROPPER_BUTTON].right = windowWidth - 2;
widgets[WIDX_SCENERY_BUILD_CLUSTER_BUTTON].right = windowWidth - 2;
widgets[WIDX_SCENERY_PRIMARY_COLOUR_BUTTON].left = windowWidth - 19;
widgets[WIDX_SCENERY_SECONDARY_COLOUR_BUTTON].left = windowWidth - 19;
widgets[WIDX_SCENERY_TERTIARY_COLOUR_BUTTON].left = windowWidth - 19;
widgets[WIDX_SCENERY_PRIMARY_COLOUR_BUTTON].right = windowWidth - 8;
widgets[WIDX_SCENERY_SECONDARY_COLOUR_BUTTON].right = windowWidth - 8;
widgets[WIDX_SCENERY_TERTIARY_COLOUR_BUTTON].right = windowWidth - 8;
}
void OnDraw(rct_drawpixelinfo& dpi) override
{
DrawWidgets(dpi);
DrawTabs(dpi, windowPos);
auto selectedSceneryEntry = _selectedScenery;
if (selectedSceneryEntry.IsUndefined())
{
if (gWindowSceneryPaintEnabled & 1) // repaint coloured scenery tool is on
return;
if (gWindowSceneryEyedropperEnabled)
return;
selectedSceneryEntry = GetSelectedScenery(gWindowSceneryActiveTabIndex);
if (selectedSceneryEntry.IsUndefined())
return;
}
auto [name, price] = GetNameAndPrice(selectedSceneryEntry);
if (price != MONEY32_UNDEFINED && !(gParkFlags & PARK_FLAGS_NO_MONEY))
{
auto ft = Formatter();
ft.Add<money64>(price);
// -14
DrawTextBasic(
&dpi, windowPos + ScreenCoordsXY{ width - 0x1A, height - 13 }, STR_COST_LABEL, ft, { TextAlignment::RIGHT });
}
auto ft = Formatter();
ft.Add<rct_string_id>(name);
DrawTextEllipsised(&dpi, { windowPos.x + 3, windowPos.y + height - 13 }, width - 19, STR_BLACK_STRING, ft);
}
void OnScrollDraw(int32_t scrollIndex, rct_drawpixelinfo& dpi) override
{
if (scrollIndex == SceneryContentScrollIndex)
{
ContentScrollDraw(dpi);
}
}
void SetSelectedItem(
const ScenerySelection& scenery, const std::optional<colour_t> primary, const std::optional<colour_t> secondary,
const std::optional<colour_t> tertiary, const std::optional<colour_t> rotation)
{
auto tabIndex = FindTabWithScenery(scenery);
if (!tabIndex.has_value())
{
return;
}
gWindowSceneryActiveTabIndex = tabIndex.value();
SetSelectedScenery(tabIndex.value(), scenery);
if (primary.has_value())
{
gWindowSceneryPrimaryColour = primary.value();
}
if (secondary.has_value())
{
gWindowScenerySecondaryColour = secondary.value();
}
if (tertiary.has_value())
{
gWindowSceneryTertiaryColour = tertiary.value();
}
if (rotation.has_value())
{
gWindowSceneryRotation = rotation.value();
}
gWindowSceneryEyedropperEnabled = false;
OpenRCT2::Audio::Play(OpenRCT2::Audio::SoundId::Click1, 0, context_get_width() / 2);
_hoverCounter = -16;
gSceneryPlaceCost = MONEY32_UNDEFINED;
Invalidate();
}
void SetSelectedTab(const ObjectEntryIndex sceneryGroupIndex)
{
const auto* tabInfo = GetSceneryTabInfoForGroup(sceneryGroupIndex);
if (tabInfo == nullptr)
{
tabInfo = &_tabEntries.back();
}
const auto tabId = std::distance(&*_tabEntries.cbegin(), tabInfo);
OnMouseDown(WIDX_SCENERY_TAB_1 + tabId);
}
// Used after removing objects, in order to avoid crashes.
void ResetSelectedSceneryItems()
{
gWindowSceneryTabSelections.clear();
}
void Init()
{
_tabEntries.clear();
auto maxTabs = 32;
for (ObjectEntryIndex scenerySetIndex = 0; scenerySetIndex < maxTabs - 1; scenerySetIndex++)
{
const auto* sceneryGroupEntry = get_scenery_group_entry(scenerySetIndex);
if (sceneryGroupEntry != nullptr && scenery_group_is_invented(scenerySetIndex))
{
SceneryTabInfo tabInfo;
tabInfo.SceneryGroupIndex = scenerySetIndex;
for (size_t i = 0; i < sceneryGroupEntry->entry_count; i++)
{
const auto& sceneryEntry = sceneryGroupEntry->scenery_entries[i];
if (IsSceneryAvailableToBuild(sceneryEntry))
{
tabInfo.Entries.push_back(sceneryEntry);
}
}
if (tabInfo.Entries.size() > 0)
{
_tabEntries.push_back(std::move(tabInfo));
}
}
}
// Add misc tab
_tabEntries.emplace_back();
// small scenery
for (ObjectEntryIndex sceneryId = 0; sceneryId < MAX_SMALL_SCENERY_OBJECTS; sceneryId++)
{
const auto* sceneryEntry = get_small_scenery_entry(sceneryId);
if (sceneryEntry != nullptr)
{
InitSceneryEntry({ SCENERY_TYPE_SMALL, sceneryId }, sceneryEntry->scenery_tab_id);
}
}
// large scenery
for (ObjectEntryIndex sceneryId = 0; sceneryId < MAX_LARGE_SCENERY_OBJECTS; sceneryId++)
{
const auto* sceneryEntry = get_large_scenery_entry(sceneryId);
if (sceneryEntry != nullptr)
{
InitSceneryEntry({ SCENERY_TYPE_LARGE, sceneryId }, sceneryEntry->scenery_tab_id);
}
}
// walls
for (ObjectEntryIndex sceneryId = 0; sceneryId < MAX_WALL_SCENERY_OBJECTS; sceneryId++)
{
const auto* sceneryEntry = get_wall_entry(sceneryId);
if (sceneryEntry != nullptr)
{
InitSceneryEntry({ SCENERY_TYPE_WALL, sceneryId }, sceneryEntry->scenery_tab_id);
}
}
// banners
for (ObjectEntryIndex sceneryId = 0; sceneryId < MAX_BANNER_OBJECTS; sceneryId++)
{
const auto* sceneryEntry = get_banner_entry(sceneryId);
if (sceneryEntry != nullptr)
{
InitSceneryEntry({ SCENERY_TYPE_BANNER, sceneryId }, sceneryEntry->scenery_tab_id);
}
}
// path bits
for (ObjectEntryIndex sceneryId = 0; sceneryId < MAX_PATH_ADDITION_OBJECTS; sceneryId++)
{
const auto* sceneryEntry = get_footpath_item_entry(sceneryId);
if (sceneryEntry != nullptr)
{
InitSceneryEntry({ SCENERY_TYPE_PATH_ITEM, sceneryId }, sceneryEntry->scenery_tab_id);
}
}
// Remove misc tab if empty
if (_tabEntries.back().Entries.size() == 0)
{
_tabEntries.pop_back();
}
SortTabs();
PrepareWidgets();
window_invalidate_by_class(WC_SCENERY);
}
void SetDefaultPlacementConfiguration()
{
gWindowSceneryRotation = 3;
gWindowSceneryPrimaryColour = COLOUR_BORDEAUX_RED;
gWindowScenerySecondaryColour = COLOUR_YELLOW;
gWindowSceneryTertiaryColour = COLOUR_DARK_BROWN;
Init();
gWindowSceneryTabSelections.clear();
gWindowSceneryActiveTabIndex = 0;
}
private:
int32_t GetNumColumns() const
{
const auto& listWidget = widgets[WIDX_SCENERY_LIST];
const auto contentWidth = listWidget.width() - SCROLLBAR_WIDTH;
return contentWidth / SCENERY_BUTTON_WIDTH;
}
template<typename T> T CountRows(T items) const
{
const auto rows = items / GetNumColumns();
return rows;
}
size_t CountRows() const
{
const auto tabIndex = gWindowSceneryActiveTabIndex;
if (tabIndex >= _tabEntries.size())
{
return 0;
}
const auto totalItems = _tabEntries[tabIndex].Entries.size();
const auto numColumns = GetNumColumns();
const auto rows = CountRows(totalItems + numColumns - 1);
return rows;
}
int32_t constexpr ContentRowsHeight(const size_t rows) const
{
return static_cast<int32_t>(rows * SCENERY_BUTTON_HEIGHT);
}
void ContentUpdateScroll()
{
const auto tabIndex = gWindowSceneryActiveTabIndex;
if (tabIndex >= _tabEntries.size())
{
return;
}
const int32_t listHeight = height - 14 - widgets[WIDX_SCENERY_LIST].top - 1;
const auto sceneryItem = ContentCountRowsWithSelectedItem(tabIndex);
scrolls[SceneryContentScrollIndex].v_bottom = ContentRowsHeight(sceneryItem.allRows) + 1;
const int32_t maxTop = std::max(0, scrolls[SceneryContentScrollIndex].v_bottom - listHeight);
auto rowSelected = CountRows(sceneryItem.selected_item);
if (sceneryItem.scenerySelection.IsUndefined())
{
rowSelected = 0;
const auto& scenery = _tabEntries[tabIndex].Entries[0];
if (!scenery.IsUndefined())
{
SetSelectedScenery(tabIndex, scenery);
}
}
scrolls[SceneryContentScrollIndex].v_top = ContentRowsHeight(rowSelected);
scrolls[SceneryContentScrollIndex].v_top = std::min<int32_t>(maxTop, scrolls[SceneryContentScrollIndex].v_top);
WidgetScrollUpdateThumbs(this, WIDX_SCENERY_LIST);
}
SceneryItem ContentCountRowsWithSelectedItem(const size_t tabIndex)
{
SceneryItem sceneryItem = { 0, 0, ScenerySelection() };
const auto scenerySelection = GetSelectedScenery(tabIndex);
const auto& tabInfo = _tabEntries[tabIndex];
for (size_t i = 0; i < tabInfo.Entries.size(); i++)
{
const auto& currentEntry = tabInfo.Entries[i];
if (currentEntry == scenerySelection)
{
sceneryItem.selected_item = static_cast<int32_t>(i);
sceneryItem.scenerySelection = scenerySelection;
}
}
sceneryItem.allRows = static_cast<int32_t>(CountRows(tabInfo.Entries.size() + 8));
return sceneryItem;
}
const ScenerySelection GetSelectedScenery(const size_t tabIndex)
{
if (gWindowSceneryTabSelections.size() > tabIndex)
{
return gWindowSceneryTabSelections[tabIndex];
}
return {};
}
void SetSelectedScenery(const size_t tabIndex, const ScenerySelection& value)
{
if (gWindowSceneryTabSelections.size() <= tabIndex)
{
gWindowSceneryTabSelections.resize(tabIndex + 1);
}
gWindowSceneryTabSelections[tabIndex] = value;
}
SceneryTabInfo* GetSceneryTabInfoForGroup(const ObjectEntryIndex sceneryGroupIndex)
{
if (sceneryGroupIndex == OBJECT_ENTRY_INDEX_NULL)
{
return &_tabEntries[_tabEntries.size() - 1];
}
for (auto& tabEntry : _tabEntries)
{
if (tabEntry.SceneryGroupIndex == sceneryGroupIndex)
return &tabEntry;
}
return nullptr;
}
std::optional<size_t> FindTabWithScenery(const ScenerySelection& scenery)
{
for (size_t i = 0; i < _tabEntries.size(); i++)
{
const auto& tabInfo = _tabEntries[i];
if (tabInfo.Contains(scenery))
{
return i;
}
}
return std::nullopt;
}
void InitSceneryEntry(const ScenerySelection& selection, const ObjectEntryIndex sceneryGroupIndex)
{
Guard::ArgumentInRange<int32_t>(selection.EntryIndex, 0, OBJECT_ENTRY_INDEX_NULL);
if (IsSceneryAvailableToBuild(selection))
{
// Get current tab
const auto tabIndex = FindTabWithScenery(selection);
// Add scenery to primary group (usually trees or path additions)
if (sceneryGroupIndex != OBJECT_ENTRY_INDEX_NULL)
{
auto* tabInfo = GetSceneryTabInfoForGroup(sceneryGroupIndex);
if (tabInfo != nullptr)
{
tabInfo->AddEntry(selection);
return;
}
}
// If scenery is no tab, add it to misc
if (!tabIndex.has_value())
{
auto* tabInfo = GetSceneryTabInfoForGroup(OBJECT_ENTRY_INDEX_NULL);
if (tabInfo != nullptr)
{
tabInfo->AddEntry(selection);