2019-12-12 16:42:19 +01:00
/*****************************************************************************
2023-01-01 11:58:01 +01:00
* Copyright ( c ) 2014 - 2023 OpenRCT2 developers
2019-12-12 16:42:19 +01:00
*
* 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 <openrct2-ui/interface/LandTool.h>
# include <openrct2-ui/interface/Widget.h>
# include <openrct2-ui/windows/Window.h>
# include <openrct2/Context.h>
2021-11-23 23:17:41 +01:00
# include <openrct2/core/String.hpp>
2021-12-12 00:06:06 +01:00
# include <openrct2/localisation/Formatter.h>
2019-12-12 16:42:19 +01:00
# include <openrct2/localisation/Localisation.h>
# include <openrct2/world/Scenery.h>
2021-11-26 15:25:34 +01:00
enum WindowSceneryScatterWidgetIdx
2019-12-12 16:42:19 +01:00
{
WIDX_BACKGROUND ,
WIDX_TITLE ,
WIDX_CLOSE ,
WIDX_PREVIEW ,
WIDX_DECREMENT ,
WIDX_INCREMENT ,
2020-02-19 14:05:36 +01:00
WIDX_DENSITY ,
WIDX_DENSITY_LOW ,
WIDX_DENSITY_MEDIUM ,
WIDX_DENSITY_HIGH
2019-12-12 16:42:19 +01:00
} ;
2020-02-20 15:00:13 +01:00
bool gWindowSceneryScatterEnabled = false ;
uint16_t gWindowSceneryScatterSize ;
ScatterToolDensity gWindowSceneryScatterDensity ;
2019-12-12 16:42:19 +01:00
// clang-format off
2022-12-24 16:50:29 +01:00
static Widget window_scenery_scatter_widgets [ ] = {
2020-11-27 04:14:20 +01:00
MakeWidget ( { 0 , 0 } , { 86 , 100 } , WindowWidgetType : : Frame , WindowColour : : Secondary ) , // panel / background
MakeWidget ( { 1 , 1 } , { 84 , 14 } , WindowWidgetType : : Caption , WindowColour : : Primary , STR_SCENERY_SCATTER , STR_WINDOW_TITLE_TIP ) , // title bar
MakeWidget ( { 73 , 2 } , { 11 , 12 } , WindowWidgetType : : CloseBox , WindowColour : : Primary , STR_CLOSE_X , STR_CLOSE_WINDOW_TIP ) , // close x button
2020-02-19 14:05:36 +01:00
2022-12-24 17:47:22 +01:00
MakeWidget ( { 20 , 17 } , { 44 , 32 } , WindowWidgetType : : ImgBtn , WindowColour : : Secondary , ImageId ( SPR_LAND_TOOL_SIZE_0 ) ) , // preview box
2020-11-27 04:14:20 +01:00
MakeRemapWidget ( { 21 , 18 } , { 16 , 16 } , WindowWidgetType : : TrnBtn , WindowColour : : Secondary , SPR_LAND_TOOL_DECREASE , STR_ADJUST_SMALLER_LAND_TIP ) , // decrement size
MakeRemapWidget ( { 47 , 32 } , { 16 , 16 } , WindowWidgetType : : TrnBtn , WindowColour : : Secondary , SPR_LAND_TOOL_INCREASE , STR_ADJUST_LARGER_LAND_TIP ) , // increment size
2020-02-19 14:05:36 +01:00
2020-11-27 04:14:20 +01:00
MakeWidget ( { 3 , 55 } , { 80 , 42 } , WindowWidgetType : : Groupbox , WindowColour : : Secondary , STR_SCATTER_TOOL_DENSITY ) ,
MakeRemapWidget ( { 7 , 68 } , { 24 , 24 } , WindowWidgetType : : FlatBtn , WindowColour : : Secondary , SPR_G2_SCENERY_SCATTER_LOW , STR_SCATTER_TOOL_DENSITY_LOW ) , // low amount
MakeRemapWidget ( { 31 , 68 } , { 24 , 24 } , WindowWidgetType : : FlatBtn , WindowColour : : Secondary , SPR_G2_SCENERY_SCATTER_MEDIUM , STR_SCATTER_TOOL_DENSITY_MEDIUM ) , // medium amount
MakeRemapWidget ( { 55 , 68 } , { 24 , 24 } , WindowWidgetType : : FlatBtn , WindowColour : : Secondary , SPR_G2_SCENERY_SCATTER_HIGH , STR_SCATTER_TOOL_DENSITY_HIGH ) , // high amount
2021-09-26 11:11:42 +02:00
WIDGETS_END ,
2019-12-12 16:42:19 +01:00
} ;
// clang-format on
2023-01-21 20:57:17 +01:00
class SceneryScatterWindow final : public WindowBase
2019-12-12 16:42:19 +01:00
{
2021-11-23 23:17:41 +01:00
public :
void OnOpen ( ) override
{
widgets = window_scenery_scatter_widgets ;
2022-10-17 19:21:18 +02:00
hold_down_widgets = ( 1uLL < < WIDX_INCREMENT ) | ( 1uLL < < WIDX_DECREMENT ) ;
2022-07-30 21:59:47 +02:00
WindowInitScrollWidgets ( * this ) ;
2023-01-16 21:13:42 +01:00
WindowPushOthersBelow ( * this ) ;
2021-11-23 23:17:41 +01:00
gWindowSceneryScatterEnabled = true ;
gWindowSceneryScatterSize = 16 ;
gWindowSceneryScatterDensity = ScatterToolDensity : : MediumDensity ;
}
2019-12-12 16:42:19 +01:00
2021-11-23 23:17:41 +01:00
void OnClose ( ) override
{
gWindowSceneryScatterEnabled = false ;
}
2020-02-20 15:00:13 +01:00
2022-08-21 18:49:23 +02:00
void InputSize ( const WidgetIndex widgetIndex )
2021-11-23 23:17:41 +01:00
{
uint8_t maxLength = 0 ;
Formatter ft ;
2019-12-12 16:42:19 +01:00
2021-11-23 23:17:41 +01:00
switch ( widgetIndex )
{
case WIDX_PREVIEW :
ft . Add < int16_t > ( MINIMUM_TOOL_SIZE ) ;
ft . Add < int16_t > ( MAXIMUM_TOOL_SIZE ) ;
maxLength = 3 ;
break ;
}
WindowTextInputOpen ( this , widgetIndex , STR_SELECTION_SIZE , STR_ENTER_SELECTION_SIZE , ft , STR_NONE , STR_NONE , maxLength ) ;
}
2019-12-12 16:42:19 +01:00
2022-08-21 18:49:23 +02:00
void OnMouseUp ( WidgetIndex widgetIndex ) override
2019-12-12 16:42:19 +01:00
{
2021-11-23 23:17:41 +01:00
switch ( widgetIndex )
{
case WIDX_CLOSE :
2023-01-16 21:13:42 +01:00
WindowClose ( * this ) ;
2021-11-23 23:17:41 +01:00
break ;
2020-02-20 15:00:13 +01:00
2021-11-23 23:17:41 +01:00
case WIDX_PREVIEW :
InputSize ( widgetIndex ) ;
break ;
2020-02-19 14:05:36 +01:00
2021-11-23 23:17:41 +01:00
case WIDX_DENSITY_LOW :
gWindowSceneryScatterDensity = ScatterToolDensity : : LowDensity ;
break ;
2020-02-19 14:05:36 +01:00
2021-11-23 23:17:41 +01:00
case WIDX_DENSITY_MEDIUM :
gWindowSceneryScatterDensity = ScatterToolDensity : : MediumDensity ;
break ;
2020-02-19 14:05:36 +01:00
2021-11-23 23:17:41 +01:00
case WIDX_DENSITY_HIGH :
gWindowSceneryScatterDensity = ScatterToolDensity : : HighDensity ;
break ;
}
2019-12-12 16:42:19 +01:00
}
2022-08-21 18:49:23 +02:00
void OnMouseDown ( const WidgetIndex widgetIndex ) override
2019-12-12 16:42:19 +01:00
{
2021-11-23 23:17:41 +01:00
switch ( widgetIndex )
{
case WIDX_DECREMENT :
// Decrement land tool size, if it stays within the limit
gWindowSceneryScatterSize = std : : max ( MINIMUM_TOOL_SIZE , gWindowSceneryScatterSize - 1 ) ;
Invalidate ( ) ;
break ;
case WIDX_INCREMENT :
// Increment land tool size, if it stays within the limit
gWindowSceneryScatterSize = std : : min ( MAXIMUM_TOOL_SIZE , gWindowSceneryScatterSize + 1 ) ;
Invalidate ( ) ;
break ;
}
2019-12-12 16:42:19 +01:00
}
2022-08-21 18:49:23 +02:00
void OnTextInput ( const WidgetIndex widgetIndex , const std : : string_view text ) override
2021-11-23 23:17:41 +01:00
{
if ( widgetIndex ! = WIDX_PREVIEW | | text . empty ( ) )
return ;
2019-12-12 16:42:19 +01:00
2021-11-23 23:17:41 +01:00
const auto res = String : : Parse < int32_t > ( text ) ;
2019-12-12 16:42:19 +01:00
2021-11-23 23:17:41 +01:00
if ( res . has_value ( ) )
2019-12-12 16:42:19 +01:00
{
2021-11-23 23:17:41 +01:00
switch ( widgetIndex )
{
case WIDX_PREVIEW :
gWindowSceneryScatterSize = std : : clamp < int32_t > ( res . value ( ) , MINIMUM_TOOL_SIZE , MAXIMUM_TOOL_SIZE ) ;
break ;
}
Invalidate ( ) ;
2019-12-12 16:42:19 +01:00
}
}
2021-11-23 23:17:41 +01:00
void OnPrepareDraw ( ) override
2019-12-12 16:42:19 +01:00
{
2021-11-23 23:17:41 +01:00
// Set the preview image button to be pressed down
2022-10-17 19:21:18 +02:00
pressed_widgets = ( 1uLL < < WIDX_PREVIEW ) ;
2019-12-12 16:42:19 +01:00
2021-11-23 23:17:41 +01:00
// Set density buttons' pressed state.
switch ( gWindowSceneryScatterDensity )
{
case ScatterToolDensity : : LowDensity :
2022-10-17 19:21:18 +02:00
pressed_widgets | = ( 1uLL < < WIDX_DENSITY_LOW ) ;
2021-11-23 23:17:41 +01:00
break ;
2020-02-19 14:05:36 +01:00
2021-11-23 23:17:41 +01:00
case ScatterToolDensity : : MediumDensity :
2022-10-17 19:21:18 +02:00
pressed_widgets | = ( 1uLL < < WIDX_DENSITY_MEDIUM ) ;
2021-11-23 23:17:41 +01:00
break ;
2020-02-19 14:05:36 +01:00
2021-11-23 23:17:41 +01:00
case ScatterToolDensity : : HighDensity :
2022-10-17 19:21:18 +02:00
pressed_widgets | = ( 1uLL < < WIDX_DENSITY_HIGH ) ;
2021-11-23 23:17:41 +01:00
break ;
}
2020-02-19 14:05:36 +01:00
2021-11-23 23:17:41 +01:00
// Update the preview image (for tool sizes up to 7)
2022-12-24 17:47:22 +01:00
widgets [ WIDX_PREVIEW ] . image = ImageId ( LandTool : : SizeToSpriteIndex ( gWindowSceneryScatterSize ) ) ;
2020-02-19 14:05:36 +01:00
}
2023-01-19 09:16:44 +01:00
void OnDraw ( DrawPixelInfo & dpi ) override
2021-11-23 23:17:41 +01:00
{
2022-07-30 21:59:47 +02:00
WindowDrawWidgets ( * this , & dpi ) ;
2019-12-12 16:42:19 +01:00
2021-11-23 23:17:41 +01:00
// Draw area as a number for tool sizes bigger than 7
if ( gWindowSceneryScatterSize > MAX_TOOL_SIZE_WITH_SPRITE )
{
const auto & preview = widgets [ WIDX_PREVIEW ] ;
const auto screenCoords = ScreenCoordsXY { windowPos . x + preview . midX ( ) , windowPos . y + preview . midY ( ) } ;
auto ft = Formatter ( ) ;
ft . Add < uint16_t > ( gWindowSceneryScatterSize ) ;
2023-02-24 22:05:07 +01:00
DrawTextBasic ( dpi , screenCoords - ScreenCoordsXY { 0 , 2 } , STR_LAND_TOOL_SIZE_VALUE , ft , { TextAlignment : : CENTRE } ) ;
2021-11-23 23:17:41 +01:00
}
}
} ;
2019-12-12 16:42:19 +01:00
2023-01-21 20:57:17 +01:00
WindowBase * WindowSceneryScatterOpen ( )
2021-11-23 23:17:41 +01:00
{
// Check if window is already open
2023-01-16 21:13:42 +01:00
auto * window = WindowFindByClass ( WindowClass : : SceneryScatter ) ;
2021-11-23 23:17:41 +01:00
if ( window = = nullptr )
2019-12-12 16:42:19 +01:00
{
2022-08-21 18:38:25 +02:00
window = WindowCreate < SceneryScatterWindow > ( WindowClass : : SceneryScatter , 86 , 100 ) ;
2019-12-12 16:42:19 +01:00
}
2021-11-23 23:17:41 +01:00
return window ;
2019-12-12 16:42:19 +01:00
}