mirror of
https://github.com/OpenRCT2/OpenRCT2.git
synced 2025-01-23 02:41:58 -05:00
Refactored water window code into WaterWindow class
Created a new WaterWindow class that inherits from Window class and overrides On methods. Added a new generic method for WindowFocusOnCreate that takes position as an argument.
This commit is contained in:
parent
4218766729
commit
83fcfa6e68
2 changed files with 150 additions and 174 deletions
|
@ -37,182 +37,148 @@ static rct_widget window_water_widgets[] = {
|
|||
MakeRemapWidget({43, 32}, {16, 16}, WindowWidgetType::TrnBtn, WindowColour::Tertiary, SPR_LAND_TOOL_INCREASE, STR_ADJUST_LARGER_WATER_TIP), // increment size
|
||||
{ WIDGETS_END },
|
||||
};
|
||||
|
||||
static void window_water_close(rct_window *w);
|
||||
static void window_water_mouseup(rct_window *w, rct_widgetindex widgetIndex);
|
||||
static void window_water_mousedown(rct_window *w, rct_widgetindex widgetIndex, rct_widget *widget);
|
||||
static void window_water_update(rct_window *w);
|
||||
static void window_water_invalidate(rct_window *w);
|
||||
static void window_water_paint(rct_window *w, rct_drawpixelinfo *dpi);
|
||||
static void window_water_textinput(rct_window *w, rct_widgetindex widgetIndex, char *text);
|
||||
static void window_water_inputsize(rct_window *w);
|
||||
|
||||
static rct_window_event_list window_water_events([](auto& events)
|
||||
{
|
||||
events.close = &window_water_close;
|
||||
events.mouse_up = &window_water_mouseup;
|
||||
events.mouse_down = &window_water_mousedown;
|
||||
events.update = &window_water_update;
|
||||
events.text_input = &window_water_textinput;
|
||||
events.invalidate = &window_water_invalidate;
|
||||
events.paint = &window_water_paint;
|
||||
});
|
||||
// clang-format on
|
||||
|
||||
/**
|
||||
*
|
||||
* rct2: 0x006E6A40
|
||||
*/
|
||||
class WaterWindow final : public Window
|
||||
{
|
||||
public:
|
||||
void OnOpen() override
|
||||
{
|
||||
widgets = window_water_widgets;
|
||||
enabled_widgets = (1 << WIDX_CLOSE) | (1 << WIDX_DECREMENT) | (1 << WIDX_INCREMENT) | (1 << WIDX_PREVIEW);
|
||||
hold_down_widgets = (1 << WIDX_INCREMENT) | (1 << WIDX_DECREMENT);
|
||||
WindowInitScrollWidgets(this);
|
||||
window_push_others_below(this);
|
||||
|
||||
gLandToolSize = 1;
|
||||
gWaterToolRaiseCost = MONEY32_UNDEFINED;
|
||||
gWaterToolLowerCost = MONEY32_UNDEFINED;
|
||||
}
|
||||
|
||||
void OnClose() override
|
||||
{
|
||||
// If the tool wasn't changed, turn tool off
|
||||
if (water_tool_is_active())
|
||||
{
|
||||
tool_cancel();
|
||||
}
|
||||
}
|
||||
|
||||
void OnMouseUp(rct_widgetindex widgetIndex) override
|
||||
{
|
||||
switch (widgetIndex)
|
||||
{
|
||||
case WIDX_CLOSE:
|
||||
Close();
|
||||
break;
|
||||
case WIDX_PREVIEW:
|
||||
InputSize();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void OnMouseDown(rct_widgetindex widgetIndex) override
|
||||
{
|
||||
switch (widgetIndex)
|
||||
{
|
||||
case WIDX_DECREMENT:
|
||||
// Decrement land tool size
|
||||
gLandToolSize = std::max(MINIMUM_TOOL_SIZE, gLandToolSize - 1);
|
||||
|
||||
// Invalidate the window
|
||||
Invalidate();
|
||||
break;
|
||||
case WIDX_INCREMENT:
|
||||
// Increment land tool size
|
||||
gLandToolSize = std::min(MAXIMUM_TOOL_SIZE, gLandToolSize + 1);
|
||||
|
||||
// Invalidate the window
|
||||
Invalidate();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void OnUpdate() override
|
||||
{
|
||||
// Close window if another tool is open
|
||||
if (!water_tool_is_active())
|
||||
{
|
||||
Close();
|
||||
}
|
||||
}
|
||||
|
||||
void OnTextInput(rct_widgetindex widgetIndex, std::string_view text) override
|
||||
{
|
||||
int32_t size;
|
||||
char* end;
|
||||
|
||||
if (widgetIndex != WIDX_PREVIEW)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
size = strtol(std::string(text).c_str(), &end, 10);
|
||||
if (*end == '\0')
|
||||
{
|
||||
size = std::max(MINIMUM_TOOL_SIZE, size);
|
||||
size = std::min(MAXIMUM_TOOL_SIZE, size);
|
||||
gLandToolSize = size;
|
||||
|
||||
Invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
void OnPrepareDraw() override
|
||||
{
|
||||
// Set the preview image button to be pressed down
|
||||
SetWidgetPressed(WIDX_PREVIEW, true);
|
||||
|
||||
// Update the preview image
|
||||
widgets[WIDX_PREVIEW].image = LandTool::SizeToSpriteIndex(gLandToolSize);
|
||||
}
|
||||
|
||||
void OnDraw(rct_drawpixelinfo& dpi) override
|
||||
{
|
||||
auto screenCoords = ScreenCoordsXY{ windowPos.x + window_water_widgets[WIDX_PREVIEW].midX(),
|
||||
windowPos.y + window_water_widgets[WIDX_PREVIEW].midY() };
|
||||
|
||||
DrawWidgets(dpi);
|
||||
// Draw number for tool sizes bigger than 7
|
||||
if (gLandToolSize > MAX_TOOL_SIZE_WITH_SPRITE)
|
||||
{
|
||||
gfx_draw_string_centred(
|
||||
&dpi, STR_LAND_TOOL_SIZE_VALUE, screenCoords - ScreenCoordsXY{ 0, 2 }, COLOUR_BLACK, &gLandToolSize);
|
||||
}
|
||||
|
||||
if (!(gParkFlags & PARK_FLAGS_NO_MONEY))
|
||||
{
|
||||
// Draw raise cost amount
|
||||
screenCoords = { window_water_widgets[WIDX_PREVIEW].midX() + windowPos.x,
|
||||
window_water_widgets[WIDX_PREVIEW].bottom + windowPos.y + 5 };
|
||||
if (gWaterToolRaiseCost != MONEY32_UNDEFINED && gWaterToolRaiseCost != 0)
|
||||
{
|
||||
gfx_draw_string_centred(&dpi, STR_RAISE_COST_AMOUNT, screenCoords, COLOUR_BLACK, &gWaterToolRaiseCost);
|
||||
}
|
||||
screenCoords.y += 10;
|
||||
|
||||
// Draw lower cost amount
|
||||
if (gWaterToolLowerCost != MONEY32_UNDEFINED && gWaterToolLowerCost != 0)
|
||||
{
|
||||
gfx_draw_string_centred(&dpi, STR_LOWER_COST_AMOUNT, screenCoords, COLOUR_BLACK, &gWaterToolLowerCost);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
void InputSize()
|
||||
{
|
||||
TextInputDescriptionArgs[0] = MINIMUM_TOOL_SIZE;
|
||||
TextInputDescriptionArgs[1] = MAXIMUM_TOOL_SIZE;
|
||||
window_text_input_open(this, WIDX_PREVIEW, STR_SELECTION_SIZE, STR_ENTER_SELECTION_SIZE, STR_NONE, STR_NONE, 3);
|
||||
}
|
||||
};
|
||||
|
||||
rct_window* window_water_open()
|
||||
{
|
||||
rct_window* window;
|
||||
|
||||
// Check if window is already open
|
||||
window = window_find_by_class(WC_WATER);
|
||||
if (window != nullptr)
|
||||
return window;
|
||||
|
||||
window = WindowCreate(ScreenCoordsXY(context_get_width() - 76, 29), 76, 77, &window_water_events, WC_WATER, 0);
|
||||
window->widgets = window_water_widgets;
|
||||
window->enabled_widgets = (1 << WIDX_CLOSE) | (1 << WIDX_DECREMENT) | (1 << WIDX_INCREMENT) | (1 << WIDX_PREVIEW);
|
||||
window->hold_down_widgets = (1 << WIDX_INCREMENT) | (1 << WIDX_DECREMENT);
|
||||
WindowInitScrollWidgets(window);
|
||||
window_push_others_below(window);
|
||||
|
||||
gLandToolSize = 1;
|
||||
gWaterToolRaiseCost = MONEY32_UNDEFINED;
|
||||
gWaterToolLowerCost = MONEY32_UNDEFINED;
|
||||
|
||||
return window;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* rct2: 0x006E6B65
|
||||
*/
|
||||
static void window_water_close(rct_window* w)
|
||||
{
|
||||
// If the tool wasn't changed, turn tool off
|
||||
if (water_tool_is_active())
|
||||
tool_cancel();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* rct2: 0x006E6B4E
|
||||
*/
|
||||
static void window_water_mouseup(rct_window* w, rct_widgetindex widgetIndex)
|
||||
{
|
||||
switch (widgetIndex)
|
||||
{
|
||||
case WIDX_CLOSE:
|
||||
window_close(w);
|
||||
break;
|
||||
case WIDX_PREVIEW:
|
||||
window_water_inputsize(w);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void window_water_mousedown(rct_window* w, rct_widgetindex widgetIndex, rct_widget* widget)
|
||||
{
|
||||
switch (widgetIndex)
|
||||
{
|
||||
case WIDX_DECREMENT:
|
||||
// Decrement land tool size
|
||||
gLandToolSize = std::max(MINIMUM_TOOL_SIZE, gLandToolSize - 1);
|
||||
|
||||
// Invalidate the window
|
||||
w->Invalidate();
|
||||
break;
|
||||
case WIDX_INCREMENT:
|
||||
// Increment land tool size
|
||||
gLandToolSize = std::min(MAXIMUM_TOOL_SIZE, gLandToolSize + 1);
|
||||
|
||||
// Invalidate the window
|
||||
w->Invalidate();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void window_water_textinput(rct_window* w, rct_widgetindex widgetIndex, char* text)
|
||||
{
|
||||
int32_t size;
|
||||
char* end;
|
||||
|
||||
if (widgetIndex != WIDX_PREVIEW || text == nullptr)
|
||||
return;
|
||||
|
||||
size = strtol(text, &end, 10);
|
||||
if (*end == '\0')
|
||||
{
|
||||
size = std::max(MINIMUM_TOOL_SIZE, size);
|
||||
size = std::min(MAXIMUM_TOOL_SIZE, size);
|
||||
gLandToolSize = size;
|
||||
|
||||
w->Invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
static void window_water_inputsize(rct_window* w)
|
||||
{
|
||||
TextInputDescriptionArgs[0] = MINIMUM_TOOL_SIZE;
|
||||
TextInputDescriptionArgs[1] = MAXIMUM_TOOL_SIZE;
|
||||
window_text_input_open(w, WIDX_PREVIEW, STR_SELECTION_SIZE, STR_ENTER_SELECTION_SIZE, STR_NONE, STR_NONE, 3);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* rct2: 0x006E6BCE
|
||||
*/
|
||||
static void window_water_update(rct_window* w)
|
||||
{
|
||||
// Close window if another tool is open
|
||||
if (!water_tool_is_active())
|
||||
window_close(w);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* rct2: 0x006E6AB8
|
||||
*/
|
||||
static void window_water_invalidate(rct_window* w)
|
||||
{
|
||||
// Set the preview image button to be pressed down
|
||||
w->pressed_widgets |= (1 << WIDX_PREVIEW);
|
||||
|
||||
// Update the preview image
|
||||
window_water_widgets[WIDX_PREVIEW].image = LandTool::SizeToSpriteIndex(gLandToolSize);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* rct2: 0x006E6ACF
|
||||
*/
|
||||
static void window_water_paint(rct_window* w, rct_drawpixelinfo* dpi)
|
||||
{
|
||||
auto screenCoords = ScreenCoordsXY{ w->windowPos.x + window_water_widgets[WIDX_PREVIEW].midX(),
|
||||
w->windowPos.y + window_water_widgets[WIDX_PREVIEW].midY() };
|
||||
|
||||
WindowDrawWidgets(w, dpi);
|
||||
// Draw number for tool sizes bigger than 7
|
||||
if (gLandToolSize > MAX_TOOL_SIZE_WITH_SPRITE)
|
||||
{
|
||||
gfx_draw_string_centred(
|
||||
dpi, STR_LAND_TOOL_SIZE_VALUE, screenCoords - ScreenCoordsXY{ 0, 2 }, COLOUR_BLACK, &gLandToolSize);
|
||||
}
|
||||
|
||||
if (!(gParkFlags & PARK_FLAGS_NO_MONEY))
|
||||
{
|
||||
// Draw raise cost amount
|
||||
screenCoords = { window_water_widgets[WIDX_PREVIEW].midX() + w->windowPos.x,
|
||||
window_water_widgets[WIDX_PREVIEW].bottom + w->windowPos.y + 5 };
|
||||
if (gWaterToolRaiseCost != MONEY32_UNDEFINED && gWaterToolRaiseCost != 0)
|
||||
gfx_draw_string_centred(dpi, STR_RAISE_COST_AMOUNT, screenCoords, COLOUR_BLACK, &gWaterToolRaiseCost);
|
||||
screenCoords.y += 10;
|
||||
|
||||
// Draw lower cost amount
|
||||
if (gWaterToolLowerCost != MONEY32_UNDEFINED && gWaterToolLowerCost != 0)
|
||||
gfx_draw_string_centred(dpi, STR_LOWER_COST_AMOUNT, screenCoords, COLOUR_BLACK, &gWaterToolLowerCost);
|
||||
}
|
||||
return WindowFocusOrCreate<WaterWindow>(WC_WATER, ScreenCoordsXY(context_get_width() - WW, 29), WW, WH, 0);
|
||||
}
|
||||
|
|
|
@ -708,6 +708,16 @@ T* WindowCreate(rct_windowclass cls, int32_t width, int32_t height, uint32_t fla
|
|||
return static_cast<T*>(WindowCreate(std::make_unique<T>(), cls, {}, width, height, flags | WF_AUTO_POSITION));
|
||||
}
|
||||
template<typename T, typename std::enable_if<std::is_base_of<rct_window, T>::value>::type* = nullptr>
|
||||
T* WindowFocusOrCreate(rct_windowclass cls, const ScreenCoordsXY& pos, int32_t width, int32_t height, uint32_t flags = 0)
|
||||
{
|
||||
auto* w = window_bring_to_front_by_class(cls);
|
||||
if (w == nullptr)
|
||||
{
|
||||
w = WindowCreate<T>(cls, pos, width, height, flags);
|
||||
}
|
||||
return static_cast<T*>(w);
|
||||
}
|
||||
template<typename T, typename std::enable_if<std::is_base_of<rct_window, T>::value>::type* = nullptr>
|
||||
T* WindowFocusOrCreate(rct_windowclass cls, int32_t width, int32_t height, uint32_t flags = 0)
|
||||
{
|
||||
auto* w = window_bring_to_front_by_class(cls);
|
||||
|
|
Loading…
Reference in a new issue