1 /*****************************************************************************
2  * Copyright (c) 2014-2020 OpenRCT2 developers
3  *
4  * For a complete list of all authors, please refer to contributors.md
5  * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
6  *
7  * OpenRCT2 is licensed under the GNU General Public License version 3.
8  *****************************************************************************/
9 
10 #include <openrct2-ui/interface/Dropdown.h>
11 #include <openrct2-ui/interface/LandTool.h>
12 #include <openrct2-ui/interface/Widget.h>
13 #include <openrct2-ui/interface/Window.h>
14 #include <openrct2/Context.h>
15 #include <openrct2/Input.h>
16 #include <openrct2/drawing/Drawing.h>
17 #include <openrct2/object/ObjectManager.h>
18 #include <openrct2/object/TerrainEdgeObject.h>
19 #include <openrct2/object/TerrainSurfaceObject.h>
20 #include <openrct2/world/Map.h>
21 #include <openrct2/world/Surface.h>
22 
23 using namespace OpenRCT2;
24 
25 // clang-format off
26 static uint16_t toolSizeSpriteIndices[] =
27 {
28     SPR_LAND_TOOL_SIZE_0,
29     SPR_LAND_TOOL_SIZE_1,
30     SPR_LAND_TOOL_SIZE_2,
31     SPR_LAND_TOOL_SIZE_3,
32     SPR_LAND_TOOL_SIZE_4,
33     SPR_LAND_TOOL_SIZE_5,
34     SPR_G2_LAND_TOOL_SIZE_6,
35     SPR_LAND_TOOL_SIZE_7,
36 };
37 // clang-format on
38 
39 uint16_t gLandToolSize;
40 money64 gLandToolRaiseCost;
41 money64 gLandToolLowerCost;
42 ObjectEntryIndex gLandToolTerrainSurface;
43 ObjectEntryIndex gLandToolTerrainEdge;
44 money64 gWaterToolRaiseCost;
45 money64 gWaterToolLowerCost;
46 
SizeToSpriteIndex(uint16_t size)47 uint32_t LandTool::SizeToSpriteIndex(uint16_t size)
48 {
49     if (size <= MAX_TOOL_SIZE_WITH_SPRITE)
50     {
51         return toolSizeSpriteIndices[size];
52     }
53 
54     return 0xFFFFFFFF;
55 }
56 
ShowSurfaceStyleDropdown(rct_window * w,rct_widget * widget,ObjectEntryIndex currentSurfaceType)57 void LandTool::ShowSurfaceStyleDropdown(rct_window* w, rct_widget* widget, ObjectEntryIndex currentSurfaceType)
58 {
59     auto& objManager = GetContext()->GetObjectManager();
60 
61     auto defaultIndex = 0;
62     auto itemIndex = 0;
63     for (size_t i = 0; i < MAX_TERRAIN_SURFACE_OBJECTS; i++)
64     {
65         const auto surfaceObj = static_cast<TerrainSurfaceObject*>(objManager.GetLoadedObject(ObjectType::TerrainSurface, i));
66         // NumImagesLoaded can be 1 for RCT1 surfaces if the user does not have RCT1 linked.
67         if (surfaceObj != nullptr && surfaceObj->NumImagesLoaded > 1)
68         {
69             gDropdownItemsFormat[itemIndex] = Dropdown::FormatLandPicker;
70             gDropdownItemsArgs[itemIndex] = surfaceObj->IconImageId;
71             if (surfaceObj->Colour != 255)
72             {
73                 gDropdownItemsArgs[itemIndex] |= SPRITE_ID_PALETTE_COLOUR_1(surfaceObj->Colour);
74             }
75             if (i == currentSurfaceType)
76             {
77                 defaultIndex = itemIndex;
78             }
79             itemIndex++;
80         }
81     }
82     uint32_t surfaceCount = itemIndex;
83 
84     WindowDropdownShowImage(
85         w->windowPos.x + widget->left, w->windowPos.y + widget->top, widget->height(), w->colours[2], 0, surfaceCount, 47, 36,
86         DropdownGetAppropriateImageDropdownItemsPerRow(surfaceCount));
87 
88     gDropdownDefaultIndex = defaultIndex;
89 }
90 
ShowEdgeStyleDropdown(rct_window * w,rct_widget * widget,ObjectEntryIndex currentEdgeType)91 void LandTool::ShowEdgeStyleDropdown(rct_window* w, rct_widget* widget, ObjectEntryIndex currentEdgeType)
92 {
93     auto& objManager = GetContext()->GetObjectManager();
94 
95     auto defaultIndex = 0;
96     auto itemIndex = 0;
97     for (size_t i = 0; i < MAX_TERRAIN_EDGE_OBJECTS; i++)
98     {
99         const auto edgeObj = static_cast<TerrainEdgeObject*>(objManager.GetLoadedObject(ObjectType::TerrainEdge, i));
100         if (edgeObj != nullptr && edgeObj->NumImagesLoaded > 1)
101         {
102             gDropdownItemsFormat[itemIndex] = Dropdown::FormatLandPicker;
103             gDropdownItemsArgs[itemIndex] = edgeObj->IconImageId;
104             if (i == currentEdgeType)
105             {
106                 defaultIndex = itemIndex;
107             }
108             itemIndex++;
109         }
110     }
111     uint32_t edgeCount = itemIndex;
112     auto itemsPerRow = DropdownGetAppropriateImageDropdownItemsPerRow(edgeCount);
113 
114     WindowDropdownShowImage(
115         w->windowPos.x + widget->left, w->windowPos.y + widget->top, widget->height(), w->colours[2], 0, edgeCount, 47, 36,
116         itemsPerRow);
117 
118     gDropdownDefaultIndex = defaultIndex;
119 }
120