OpenRCT2/src/openrct2-ui/windows/Sign.cpp

521 lines
17 KiB
C++
Raw Normal View History

/*****************************************************************************
2020-07-21 15:04:34 +02:00
* 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.
*****************************************************************************/
2018-06-22 23:21:44 +02:00
#include <openrct2-ui/interface/Dropdown.h>
#include <openrct2-ui/interface/Viewport.h>
#include <openrct2-ui/interface/Widget.h>
2017-09-06 15:09:18 +02:00
#include <openrct2-ui/windows/Window.h>
2017-11-30 18:17:06 +01:00
#include <openrct2/Game.h>
2019-01-03 02:20:35 +01:00
#include <openrct2/actions/LargeSceneryRemoveAction.hpp>
2018-06-22 23:21:44 +02:00
#include <openrct2/actions/SignSetNameAction.hpp>
#include <openrct2/actions/SignSetStyleAction.hpp>
2018-06-22 23:21:44 +02:00
#include <openrct2/actions/WallRemoveAction.hpp>
#include <openrct2/config/Config.h>
2018-01-06 18:32:25 +01:00
#include <openrct2/localisation/Localisation.h>
#include <openrct2/localisation/StringIds.h>
2018-06-22 23:21:44 +02:00
#include <openrct2/sprites.h>
#include <openrct2/world/Banner.h>
2017-11-20 11:13:55 +01:00
#include <openrct2/world/LargeScenery.h>
2018-01-11 10:59:26 +01:00
#include <openrct2/world/Scenery.h>
2017-11-20 11:13:55 +01:00
#include <openrct2/world/Wall.h>
2020-05-05 22:26:14 +02:00
static constexpr const rct_string_id WINDOW_TITLE = STR_SIGN;
2020-05-09 17:05:01 +02:00
static constexpr const int32_t WW = 113;
static constexpr const int32_t WH = 96;
// clang-format off
enum WINDOW_SIGN_WIDGET_IDX {
WIDX_BACKGROUND,
WIDX_TITLE,
WIDX_CLOSE,
WIDX_VIEWPORT,
WIDX_SIGN_TEXT,
WIDX_SIGN_DEMOLISH,
WIDX_MAIN_COLOUR,
WIDX_TEXT_COLOUR
};
// 0x9AEE00
static rct_widget window_sign_widgets[] = {
2020-07-30 22:39:09 +02:00
WINDOW_SHIM(WINDOW_TITLE, WW, WH),
MakeWidget({ 3, 17}, {85, 60}, WWT_VIEWPORT, WindowColour::Secondary, STR_VIEWPORT ), // Viewport
MakeWidget({WW - 25, 19}, {24, 24}, WWT_FLATBTN, WindowColour::Secondary, SPR_RENAME, STR_CHANGE_SIGN_TEXT_TIP ), // change sign button
MakeWidget({WW - 25, 67}, {24, 24}, WWT_FLATBTN, WindowColour::Secondary, SPR_DEMOLISH, STR_DEMOLISH_SIGN_TIP ), // demolish button
MakeWidget({ 5, WH - 16}, {12, 12}, WWT_COLOURBTN, WindowColour::Secondary, 0xFFFFFFFF, STR_SELECT_MAIN_SIGN_COLOUR_TIP), // Main colour
MakeWidget({ 17, WH - 16}, {12, 12}, WWT_COLOURBTN, WindowColour::Secondary, 0xFFFFFFFF, STR_SELECT_TEXT_COLOUR_TIP ), // Text colour
2020-07-30 22:39:09 +02:00
{ WIDGETS_END },
};
2017-05-01 15:41:45 +02:00
static void window_sign_mouseup(rct_window *w, rct_widgetindex widgetIndex);
static void window_sign_mousedown(rct_window *w, rct_widgetindex widgetIndex, rct_widget* widget);
static void window_sign_dropdown(rct_window *w, rct_widgetindex widgetIndex, int32_t dropdownIndex);
2017-05-01 15:41:45 +02:00
static void window_sign_textinput(rct_window *w, rct_widgetindex widgetIndex, char *text);
static void window_sign_viewport_rotate(rct_window *w);
static void window_sign_invalidate(rct_window *w);
static void window_sign_paint(rct_window *w, rct_drawpixelinfo *dpi);
// 0x98E44C
static rct_window_event_list window_sign_events([](auto& events)
{
events.mouse_up = &window_sign_mouseup;
events.mouse_down = &window_sign_mousedown;
events.dropdown = &window_sign_dropdown;
events.text_input = &window_sign_textinput;
events.viewport_rotate = &window_sign_viewport_rotate;
events.invalidate = &window_sign_invalidate;
events.paint = &window_sign_paint;
});
2017-05-01 15:41:45 +02:00
static void window_sign_small_mouseup(rct_window *w, rct_widgetindex widgetIndex);
static void window_sign_small_dropdown(rct_window *w, rct_widgetindex widgetIndex, int32_t dropdownIndex);
static void window_sign_small_invalidate(rct_window *w);
2014-11-02 15:09:04 +01:00
// 0x9A410C
static rct_window_event_list window_sign_small_events([](auto& events)
{
events.mouse_up = &window_sign_small_mouseup;
events.mouse_down = &window_sign_mousedown;
events.dropdown = &window_sign_small_dropdown;
events.text_input = &window_sign_textinput;
events.viewport_rotate = &window_sign_viewport_rotate;
events.invalidate = &window_sign_small_invalidate;
events.paint = &window_sign_paint;
});
// clang-format on
2014-11-02 15:09:04 +01:00
2019-07-21 02:28:33 +02:00
static void window_sign_show_text_input(rct_window* w);
/**
2018-06-22 23:21:44 +02:00
*
* rct2: 0x006BA305
*/
rct_window* window_sign_open(rct_windownumber number)
{
rct_window* w;
2018-06-22 23:21:44 +02:00
rct_widget* viewportWidget;
// Check if window is already open
w = window_bring_to_front_by_number(WC_BANNER, number);
2017-08-15 10:07:44 +02:00
if (w != nullptr)
2017-09-06 15:09:18 +02:00
return w;
w = window_create_auto_pos(WW, WH, &window_sign_events, WC_BANNER, WF_NO_SCROLLING);
w->widgets = window_sign_widgets;
2018-06-22 23:21:44 +02:00
w->enabled_widgets = (1 << WIDX_CLOSE) | (1 << WIDX_SIGN_TEXT) | (1 << WIDX_SIGN_DEMOLISH) | (1 << WIDX_MAIN_COLOUR)
| (1 << WIDX_TEXT_COLOUR);
w->number = number;
window_init_scroll_widgets(w);
2019-07-26 18:24:19 +02:00
auto banner = GetBanner(w->number);
2020-03-27 13:04:45 +01:00
if (banner == nullptr)
return nullptr;
2020-03-27 13:04:45 +01:00
auto signViewPos = banner->position.ToCoordsXY().ToTileCentre();
TileElement* tile_element = map_get_first_element_at(signViewPos);
if (tile_element == nullptr)
return nullptr;
while (1)
{
if (tile_element->GetType() == TILE_ELEMENT_TYPE_LARGE_SCENERY)
{
2018-09-14 12:12:22 +02:00
rct_scenery_entry* scenery_entry = tile_element->AsLargeScenery()->GetEntry();
if (scenery_entry != nullptr && scenery_entry->large_scenery.scrolling_mode != SCROLLING_MODE_NONE)
{
2020-02-08 17:42:36 +01:00
auto bannerIndex = tile_element->AsLargeScenery()->GetBannerIndex();
if (bannerIndex == w->number)
break;
}
}
2017-10-31 12:57:40 +01:00
tile_element++;
if (tile_element >= &gTileElements[std::size(gTileElements)])
{
return nullptr;
}
}
int32_t view_z = tile_element->GetBaseZ();
w->frame_no = view_z;
2018-09-14 12:12:22 +02:00
w->list_information_type = tile_element->AsLargeScenery()->GetPrimaryColour();
w->var_492 = tile_element->AsLargeScenery()->GetSecondaryColour();
2020-03-01 21:51:29 +01:00
w->SceneryEntry = tile_element->AsLargeScenery()->GetEntryIndex();
// Create viewport
viewportWidget = &window_sign_widgets[WIDX_VIEWPORT];
viewport_create(
w, w->windowPos + ScreenCoordsXY{ viewportWidget->left + 1, viewportWidget->top + 1 }, viewportWidget->width() - 1,
viewportWidget->height() - 1, 0, { signViewPos, view_z }, 0, SPRITE_INDEX_NULL);
w->viewport->flags = gConfigGeneral.always_show_gridlines ? VIEWPORT_FLAG_GRIDLINES : 0;
w->Invalidate();
2017-09-06 15:09:18 +02:00
return w;
}
/**
*
* rct2: 0x6B9765
*/
2018-06-22 23:21:44 +02:00
static void window_sign_mouseup(rct_window* w, rct_widgetindex widgetIndex)
{
2018-06-22 23:21:44 +02:00
switch (widgetIndex)
{
case WIDX_CLOSE:
window_close(w);
break;
case WIDX_SIGN_DEMOLISH:
2018-12-22 19:46:59 +01:00
{
2019-07-21 02:28:33 +02:00
auto banner = GetBanner(w->number);
2020-05-01 17:19:20 +02:00
auto bannerCoords = banner->position.ToCoordsXY();
auto tile_element = map_get_first_element_at(bannerCoords);
if (tile_element == nullptr)
return;
2018-06-22 23:21:44 +02:00
while (1)
{
2018-06-22 23:21:44 +02:00
if (tile_element->GetType() == TILE_ELEMENT_TYPE_LARGE_SCENERY)
{
2018-09-14 12:12:22 +02:00
rct_scenery_entry* scenery_entry = tile_element->AsLargeScenery()->GetEntry();
if (scenery_entry->large_scenery.scrolling_mode != SCROLLING_MODE_NONE)
2018-06-22 23:21:44 +02:00
{
2020-02-08 17:42:36 +01:00
auto bannerIndex = tile_element->AsLargeScenery()->GetBannerIndex();
2018-06-22 23:21:44 +02:00
if (bannerIndex == w->number)
break;
}
}
2018-06-22 23:21:44 +02:00
tile_element++;
if (tile_element >= &gTileElements[std::size(gTileElements)])
{
return;
}
}
2018-12-22 19:46:59 +01:00
2019-01-03 02:20:35 +01:00
auto sceneryRemoveAction = LargeSceneryRemoveAction(
2020-05-01 17:19:20 +02:00
{ bannerCoords, tile_element->GetBaseZ(), tile_element->GetDirection() },
2018-12-22 19:46:59 +01:00
tile_element->AsLargeScenery()->GetSequenceIndex());
GameActions::Execute(&sceneryRemoveAction);
2018-06-22 23:21:44 +02:00
break;
2018-12-22 19:46:59 +01:00
}
2018-06-22 23:21:44 +02:00
case WIDX_SIGN_TEXT:
2019-07-21 02:28:33 +02:00
window_sign_show_text_input(w);
2018-06-22 23:21:44 +02:00
break;
}
}
/**
*
* rct2: 0x6B9784
& 0x6E6164 */
2018-06-22 23:21:44 +02:00
static void window_sign_mousedown(rct_window* w, rct_widgetindex widgetIndex, rct_widget* widget)
{
2018-06-22 23:21:44 +02:00
switch (widgetIndex)
{
case WIDX_MAIN_COLOUR:
window_dropdown_show_colour(w, widget, TRANSLUCENT(w->colours[1]), static_cast<uint8_t>(w->list_information_type));
2018-06-22 23:21:44 +02:00
break;
case WIDX_TEXT_COLOUR:
window_dropdown_show_colour(w, widget, TRANSLUCENT(w->colours[1]), static_cast<uint8_t>(w->var_492));
2018-06-22 23:21:44 +02:00
break;
}
}
/**
*
* rct2: 0x6B979C
*/
2018-06-22 23:21:44 +02:00
static void window_sign_dropdown(rct_window* w, rct_widgetindex widgetIndex, int32_t dropdownIndex)
{
2018-06-22 23:21:44 +02:00
switch (widgetIndex)
{
case WIDX_MAIN_COLOUR:
{
2018-06-22 23:21:44 +02:00
if (dropdownIndex == -1)
return;
w->list_information_type = dropdownIndex;
auto signSetStyleAction = SignSetStyleAction(w->number, dropdownIndex, w->var_492, true);
GameActions::Execute(&signSetStyleAction);
2018-06-22 23:21:44 +02:00
break;
}
2018-06-22 23:21:44 +02:00
case WIDX_TEXT_COLOUR:
{
2018-06-22 23:21:44 +02:00
if (dropdownIndex == -1)
return;
w->var_492 = dropdownIndex;
auto signSetStyleAction = SignSetStyleAction(w->number, w->list_information_type, dropdownIndex, true);
GameActions::Execute(&signSetStyleAction);
2018-06-22 23:21:44 +02:00
break;
}
2018-06-22 23:21:44 +02:00
default:
return;
}
w->Invalidate();
}
/**
*
* rct2: 0x6B9791, 0x6E6171
*/
2018-06-22 23:21:44 +02:00
static void window_sign_textinput(rct_window* w, rct_widgetindex widgetIndex, char* text)
{
2018-03-24 04:52:47 +01:00
if (widgetIndex == WIDX_SIGN_TEXT && text != nullptr)
{
auto signSetNameAction = SignSetNameAction(w->number, text);
GameActions::Execute(&signSetNameAction);
}
}
/**
*
* rct2: 0x006B96F5
*/
2018-06-22 23:21:44 +02:00
static void window_sign_invalidate(rct_window* w)
{
rct_widget* main_colour_btn = &window_sign_widgets[WIDX_MAIN_COLOUR];
rct_widget* text_colour_btn = &window_sign_widgets[WIDX_TEXT_COLOUR];
2020-03-01 21:51:29 +01:00
rct_scenery_entry* scenery_entry = get_large_scenery_entry(w->SceneryEntry);
main_colour_btn->type = WWT_EMPTY;
text_colour_btn->type = WWT_EMPTY;
2018-06-22 23:21:44 +02:00
if (scenery_entry->large_scenery.flags & LARGE_SCENERY_FLAG_HAS_PRIMARY_COLOUR)
{
main_colour_btn->type = WWT_COLOURBTN;
}
2018-06-22 23:21:44 +02:00
if (scenery_entry->large_scenery.flags & LARGE_SCENERY_FLAG_HAS_SECONDARY_COLOUR)
{
text_colour_btn->type = WWT_COLOURBTN;
}
main_colour_btn->image = SPRITE_ID_PALETTE_COLOUR_1(w->list_information_type) | IMAGE_TYPE_TRANSPARENT | SPR_PALETTE_BTN;
text_colour_btn->image = SPRITE_ID_PALETTE_COLOUR_1(w->var_492) | IMAGE_TYPE_TRANSPARENT | SPR_PALETTE_BTN;
}
/**
*
* rct2: 0x006B9754, 0x006E6134
*/
2018-06-22 23:21:44 +02:00
static void window_sign_paint(rct_window* w, rct_drawpixelinfo* dpi)
{
window_draw_widgets(w, dpi);
// Draw viewport
2018-06-22 23:21:44 +02:00
if (w->viewport != nullptr)
{
window_draw_viewport(dpi, w);
}
}
/**
*
* rct2: 0x6B9A6C, 0x6E6424
*/
2018-06-22 23:21:44 +02:00
static void window_sign_viewport_rotate(rct_window* w)
{
rct_viewport* view = w->viewport;
2018-01-04 06:58:44 +01:00
w->viewport = nullptr;
view->width = 0;
2019-07-26 18:24:19 +02:00
auto banner = GetBanner(w->number);
auto signViewPos = CoordsXYZ{ banner->position.ToCoordsXY().ToTileCentre(), w->frame_no };
// Create viewport
rct_widget* viewportWidget = &window_sign_widgets[WIDX_VIEWPORT];
viewport_create(
w, w->windowPos + ScreenCoordsXY{ viewportWidget->left + 1, viewportWidget->top + 1 }, viewportWidget->width() - 1,
viewportWidget->height() - 1, 0, signViewPos, 0, SPRITE_INDEX_NULL);
if (w->viewport != nullptr)
w->viewport->flags = gConfigGeneral.always_show_gridlines ? VIEWPORT_FLAG_GRIDLINES : 0;
w->Invalidate();
}
2014-11-02 15:09:04 +01:00
/**
*
* rct2: 0x6E5F52
*/
2018-06-22 23:21:44 +02:00
rct_window* window_sign_small_open(rct_windownumber number)
{
rct_window* w;
2018-06-22 23:21:44 +02:00
rct_widget* viewportWidget;
// Check if window is already open
w = window_bring_to_front_by_number(WC_BANNER, number);
2017-08-15 10:07:44 +02:00
if (w != nullptr)
2017-09-06 15:09:18 +02:00
return w;
w = window_create_auto_pos(WW, WH, &window_sign_small_events, WC_BANNER, 0);
w->widgets = window_sign_widgets;
2018-06-22 23:21:44 +02:00
w->enabled_widgets = (1 << WIDX_CLOSE) | (1 << WIDX_SIGN_TEXT) | (1 << WIDX_SIGN_DEMOLISH) | (1 << WIDX_MAIN_COLOUR)
| (1 << WIDX_TEXT_COLOUR);
w->number = number;
window_init_scroll_widgets(w);
w->colours[0] = COLOUR_DARK_BROWN;
w->colours[1] = COLOUR_DARK_BROWN;
w->colours[2] = COLOUR_DARK_BROWN;
2019-07-26 18:24:19 +02:00
auto banner = GetBanner(w->number);
auto signViewPos = banner->position.ToCoordsXY().ToTileCentre();
TileElement* tile_element = map_get_first_element_at(signViewPos);
if (tile_element == nullptr)
return nullptr;
2018-06-22 23:21:44 +02:00
while (1)
{
if (tile_element->GetType() == TILE_ELEMENT_TYPE_WALL)
{
rct_scenery_entry* scenery_entry = tile_element->AsWall()->GetEntry();
if (scenery_entry->wall.scrolling_mode != SCROLLING_MODE_NONE)
2018-06-22 23:21:44 +02:00
{
if (tile_element->AsWall()->GetBannerIndex() == w->number)
break;
}
}
2017-10-31 12:57:40 +01:00
tile_element++;
}
int32_t view_z = tile_element->GetBaseZ();
w->frame_no = view_z;
w->list_information_type = tile_element->AsWall()->GetPrimaryColour();
w->var_492 = tile_element->AsWall()->GetSecondaryColour();
2020-03-01 21:51:29 +01:00
w->SceneryEntry = tile_element->AsWall()->GetEntryIndex();
// Create viewport
viewportWidget = &window_sign_widgets[WIDX_VIEWPORT];
viewport_create(
w, w->windowPos + ScreenCoordsXY{ viewportWidget->left + 1, viewportWidget->top + 1 }, viewportWidget->width() - 1,
viewportWidget->height() - 1, 0, { signViewPos, view_z }, 0, SPRITE_INDEX_NULL);
w->viewport->flags = gConfigGeneral.always_show_gridlines ? VIEWPORT_FLAG_GRIDLINES : 0;
w->flags |= WF_NO_SCROLLING;
w->Invalidate();
2017-09-06 15:09:18 +02:00
return w;
2014-11-02 15:09:04 +01:00
}
/**
*
* rct2: 0x6E6145
*/
2018-06-22 23:21:44 +02:00
static void window_sign_small_mouseup(rct_window* w, rct_widgetindex widgetIndex)
2014-11-02 15:09:04 +01:00
{
2018-06-22 23:21:44 +02:00
switch (widgetIndex)
{
case WIDX_CLOSE:
window_close(w);
break;
case WIDX_SIGN_DEMOLISH:
{
2019-07-21 02:28:33 +02:00
auto banner = GetBanner(w->number);
2020-05-01 17:19:20 +02:00
auto bannerCoords = banner->position.ToCoordsXY();
auto tile_element = map_get_first_element_at(bannerCoords);
if (tile_element == nullptr)
return;
2018-05-14 12:36:45 +02:00
while (true)
{
2018-05-14 12:36:45 +02:00
if (tile_element->GetType() == TILE_ELEMENT_TYPE_WALL)
{
rct_scenery_entry* scenery_entry = tile_element->AsWall()->GetEntry();
if (scenery_entry->wall.scrolling_mode != SCROLLING_MODE_NONE)
{
if (tile_element->AsWall()->GetBannerIndex() == w->number)
break;
}
}
tile_element++;
}
2020-05-01 17:19:20 +02:00
CoordsXYZD wallLocation = { bannerCoords, tile_element->GetBaseZ(), tile_element->GetDirection() };
2018-05-14 12:36:45 +02:00
auto wallRemoveAction = WallRemoveAction(wallLocation);
GameActions::Execute(&wallRemoveAction);
2019-07-25 18:51:22 +02:00
break;
}
2018-06-22 23:21:44 +02:00
case WIDX_SIGN_TEXT:
2019-07-21 02:28:33 +02:00
window_sign_show_text_input(w);
2018-06-22 23:21:44 +02:00
break;
}
2014-11-02 15:09:04 +01:00
}
/**
*
* rct2: 0x6E617C
*/
2018-06-22 23:21:44 +02:00
static void window_sign_small_dropdown(rct_window* w, rct_widgetindex widgetIndex, int32_t dropdownIndex)
2014-11-02 15:09:04 +01:00
{
2018-06-22 23:21:44 +02:00
switch (widgetIndex)
{
case WIDX_MAIN_COLOUR:
{
2018-06-22 23:21:44 +02:00
if (dropdownIndex == -1)
return;
w->list_information_type = dropdownIndex;
auto signSetStyleAction = SignSetStyleAction(w->number, dropdownIndex, w->var_492, false);
GameActions::Execute(&signSetStyleAction);
2018-06-22 23:21:44 +02:00
break;
}
2018-06-22 23:21:44 +02:00
case WIDX_TEXT_COLOUR:
{
2018-06-22 23:21:44 +02:00
if (dropdownIndex == -1)
return;
w->var_492 = dropdownIndex;
auto signSetStyleAction = SignSetStyleAction(w->number, w->list_information_type, dropdownIndex, false);
GameActions::Execute(&signSetStyleAction);
2018-06-22 23:21:44 +02:00
break;
}
2018-06-22 23:21:44 +02:00
default:
return;
}
w->Invalidate();
2014-11-02 15:09:04 +01:00
}
/**
*
* rct2: 0x006E60D5
*/
2018-06-22 23:21:44 +02:00
static void window_sign_small_invalidate(rct_window* w)
2014-11-02 15:09:04 +01:00
{
rct_widget* main_colour_btn = &window_sign_widgets[WIDX_MAIN_COLOUR];
rct_widget* text_colour_btn = &window_sign_widgets[WIDX_TEXT_COLOUR];
2014-11-02 15:09:04 +01:00
2020-03-01 21:51:29 +01:00
rct_scenery_entry* scenery_entry = get_wall_entry(w->SceneryEntry);
2014-11-02 15:09:04 +01:00
main_colour_btn->type = WWT_EMPTY;
text_colour_btn->type = WWT_EMPTY;
2014-11-02 15:09:04 +01:00
2018-06-22 23:21:44 +02:00
if (scenery_entry->wall.flags & WALL_SCENERY_HAS_PRIMARY_COLOUR)
{
main_colour_btn->type = WWT_COLOURBTN;
}
2018-06-22 23:21:44 +02:00
if (scenery_entry->wall.flags & WALL_SCENERY_HAS_SECONDARY_COLOUR)
{
text_colour_btn->type = WWT_COLOURBTN;
}
2014-11-02 15:09:04 +01:00
main_colour_btn->image = SPRITE_ID_PALETTE_COLOUR_1(w->list_information_type) | IMAGE_TYPE_TRANSPARENT | SPR_PALETTE_BTN;
text_colour_btn->image = SPRITE_ID_PALETTE_COLOUR_1(w->var_492) | IMAGE_TYPE_TRANSPARENT | SPR_PALETTE_BTN;
}
2019-07-21 02:28:33 +02:00
static void window_sign_show_text_input(rct_window* w)
{
auto banner = GetBanner(w->number);
2019-07-21 18:40:52 +02:00
if (banner != nullptr)
2019-07-21 02:28:33 +02:00
{
2019-07-21 18:40:52 +02:00
auto bannerText = banner->GetText();
window_text_input_raw_open(w, WIDX_SIGN_TEXT, STR_SIGN_TEXT_TITLE, STR_SIGN_TEXT_PROMPT, bannerText.c_str(), 32);
2019-07-21 02:28:33 +02:00
}
}