Files
OpenRCT2-Web/src/openrct2-ui/interface/Widget.cpp
T

1154 lines
39 KiB
C++
Raw Normal View History

2014-04-03 19:49:00 +01:00
/*****************************************************************************
2020-07-21 15:04:34 +02:00
* Copyright (c) 2014-2020 OpenRCT2 developers
2014-04-03 19:49:00 +01:00
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2014-04-03 19:49:00 +01:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2014-04-03 19:49:00 +01:00
*****************************************************************************/
2018-06-22 23:20:47 +02:00
#include "Widget.h"
#include "Window.h"
2018-01-05 23:56:37 +00:00
#include <algorithm>
#include <cmath>
2018-06-11 00:04:41 +01:00
#include <openrct2/Context.h>
#include <openrct2/Input.h>
2018-06-22 23:20:47 +02:00
#include <openrct2/drawing/Drawing.h>
2018-06-11 00:04:41 +01:00
#include <openrct2/localisation/Localisation.h>
#include <openrct2/sprites.h>
#include <openrct2/util/Util.h>
2020-11-03 14:29:22 -07:00
static void WidgetFrameDraw(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex);
static void WidgetResizeDraw(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex);
static void WidgetButtonDraw(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex);
static void WidgetTabDraw(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex);
static void WidgetFlatButtonDraw(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex);
static void WidgetTextButton(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex);
static void WidgetTextCentred(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex);
static void WidgetText(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex);
static void WidgetTextInset(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex);
static void WidgetTextBoxDraw(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex);
static void WidgetGroupboxDraw(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex);
static void WidgetCaptionDraw(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex);
static void WidgetCheckboxDraw(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex);
static void WidgetCloseboxDraw(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex);
static void WidgetScrollDraw(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex);
static void WidgetHScrollbarDraw(
rct_drawpixelinfo* dpi, const rct_scroll& scroll, int32_t l, int32_t t, int32_t r, int32_t b, int32_t colour);
2020-11-03 14:29:22 -07:00
static void WidgetVScrollbarDraw(
rct_drawpixelinfo* dpi, const rct_scroll& scroll, int32_t l, int32_t t, int32_t r, int32_t b, int32_t colour);
2020-11-03 14:29:22 -07:00
static void WidgetDrawImage(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex);
2014-04-09 22:35:29 +01:00
2014-04-28 17:35:12 +01:00
/**
2015-10-20 20:16:30 +02:00
*
2014-04-28 17:35:12 +01:00
* rct2: 0x006EB2A8
*/
2020-11-03 14:29:22 -07:00
void WidgetDraw(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex)
2014-04-09 22:35:29 +01:00
{
const auto& widget = w->widgets[widgetIndex];
switch (widget.type)
2018-06-22 23:20:47 +02:00
{
case WindowWidgetType::Frame:
2020-11-03 14:29:22 -07:00
WidgetFrameDraw(dpi, w, widgetIndex);
2018-06-22 23:20:47 +02:00
break;
case WindowWidgetType::Resize:
2020-11-03 14:29:22 -07:00
WidgetResizeDraw(dpi, w, widgetIndex);
2018-06-22 23:20:47 +02:00
break;
case WindowWidgetType::ImgBtn:
2020-11-03 14:29:22 -07:00
WidgetButtonDraw(dpi, w, widgetIndex);
2018-06-22 23:20:47 +02:00
break;
case WindowWidgetType::ColourBtn:
case WindowWidgetType::TrnBtn:
case WindowWidgetType::Tab:
2020-11-03 14:29:22 -07:00
WidgetTabDraw(dpi, w, widgetIndex);
2018-06-22 23:20:47 +02:00
break;
case WindowWidgetType::FlatBtn:
2020-11-03 14:29:22 -07:00
WidgetFlatButtonDraw(dpi, w, widgetIndex);
2018-06-22 23:20:47 +02:00
break;
case WindowWidgetType::Button:
case WindowWidgetType::TableHeader:
2020-11-03 14:29:22 -07:00
WidgetTextButton(dpi, w, widgetIndex);
2018-06-22 23:20:47 +02:00
break;
case WindowWidgetType::LabelCentred:
2020-11-03 14:29:22 -07:00
WidgetTextCentred(dpi, w, widgetIndex);
2018-06-22 23:20:47 +02:00
break;
case WindowWidgetType::Label:
2020-11-03 14:29:22 -07:00
WidgetText(dpi, w, widgetIndex);
2018-06-22 23:20:47 +02:00
break;
case WindowWidgetType::Spinner:
case WindowWidgetType::DropdownMenu:
case WindowWidgetType::Viewport:
2020-11-03 14:29:22 -07:00
WidgetTextInset(dpi, w, widgetIndex);
2018-06-22 23:20:47 +02:00
break;
case WindowWidgetType::Groupbox:
2020-11-03 14:29:22 -07:00
WidgetGroupboxDraw(dpi, w, widgetIndex);
2018-06-22 23:20:47 +02:00
break;
case WindowWidgetType::Caption:
2020-11-03 14:29:22 -07:00
WidgetCaptionDraw(dpi, w, widgetIndex);
2018-06-22 23:20:47 +02:00
break;
case WindowWidgetType::CloseBox:
2020-11-03 14:29:22 -07:00
WidgetCloseboxDraw(dpi, w, widgetIndex);
2018-06-22 23:20:47 +02:00
break;
case WindowWidgetType::Scroll:
2020-11-03 14:29:22 -07:00
WidgetScrollDraw(dpi, w, widgetIndex);
2018-06-22 23:20:47 +02:00
break;
case WindowWidgetType::Checkbox:
2020-11-03 14:29:22 -07:00
WidgetCheckboxDraw(dpi, w, widgetIndex);
2018-06-22 23:20:47 +02:00
break;
case WindowWidgetType::TextBox:
2020-11-03 14:29:22 -07:00
WidgetTextBoxDraw(dpi, w, widgetIndex);
2018-06-22 23:20:47 +02:00
break;
2021-02-24 22:54:53 +00:00
default:
break;
2017-06-06 23:24:18 +02:00
}
2014-04-09 22:35:29 +01:00
}
/**
2015-10-20 20:16:30 +02:00
*
2014-04-09 22:35:29 +01:00
* rct2: 0x006EB6CE
*/
2020-11-03 14:29:22 -07:00
static void WidgetFrameDraw(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex)
2014-04-09 22:35:29 +01:00
{
2017-06-06 23:24:18 +02:00
// Get the widget
const auto& widget = w->widgets[widgetIndex];
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Resolve the absolute ltrb
auto leftTop = w->windowPos + ScreenCoordsXY{ widget.left, widget.top };
int32_t r = w->windowPos.x + widget.right;
int32_t b = w->windowPos.y + widget.bottom;
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
//
uint8_t press = ((w->flags & WF_10) ? INSET_RECT_FLAG_FILL_MID_LIGHT : 0);
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Get the colour
uint8_t colour = w->colours[widget.colour];
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Draw the frame
gfx_fill_rect_inset(dpi, { leftTop, { r, b } }, colour, press);
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Check if the window can be resized
if (!(w->flags & WF_RESIZABLE))
return;
if (w->min_width == w->max_width && w->min_height == w->max_height)
return;
2017-06-06 23:24:18 +02:00
// Draw the resize sprite at the bottom right corner
leftTop = w->windowPos + ScreenCoordsXY{ widget.right - 18, widget.bottom - 18 };
gfx_draw_sprite(dpi, ImageId(SPR_RESIZE, colour & 0x7F), leftTop);
}
/**
2015-10-20 20:16:30 +02:00
*
* rct2: 0x006EB765
*/
2020-11-03 14:29:22 -07:00
static void WidgetResizeDraw(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex)
{
2017-06-06 23:24:18 +02:00
// Get the widget
const auto& widget = w->widgets[widgetIndex];
2017-06-06 23:24:18 +02:00
// Resolve the absolute ltrb
auto leftTop = w->windowPos + ScreenCoordsXY{ widget.left, widget.top };
int32_t r = w->windowPos.x + widget.right;
int32_t b = w->windowPos.y + widget.bottom;
2017-06-06 23:24:18 +02:00
// Get the colour
uint8_t colour = w->colours[widget.colour];
2017-06-06 23:24:18 +02:00
// Draw the panel
gfx_fill_rect_inset(dpi, { leftTop, { r, b } }, colour, 0);
2017-06-06 23:24:18 +02:00
// Check if the window can be resized
if (!(w->flags & WF_RESIZABLE))
return;
if (w->min_width == w->max_width && w->min_height == w->max_height)
return;
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Draw the resize sprite at the bottom right corner
leftTop = w->windowPos + ScreenCoordsXY{ widget.right - 18, widget.bottom - 18 };
gfx_draw_sprite(dpi, ImageId(SPR_RESIZE, colour & 0x7F), leftTop);
2014-04-09 22:35:29 +01:00
}
/**
2015-10-20 20:16:30 +02:00
*
2014-04-09 22:35:29 +01:00
* rct2: 0x006EB8E5
*/
2020-11-03 14:29:22 -07:00
static void WidgetButtonDraw(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex)
2014-04-09 22:35:29 +01:00
{
2017-06-06 23:24:18 +02:00
// Get the widget
const auto& widget = w->widgets[widgetIndex];
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Resolve the absolute ltrb
ScreenRect rect{ w->windowPos + ScreenCoordsXY{ widget.left, widget.top },
w->windowPos + ScreenCoordsXY{ widget.right, widget.bottom } };
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Check if the button is pressed down
2020-11-03 14:29:22 -07:00
uint8_t press = WidgetIsPressed(w, widgetIndex) || WidgetIsActiveTool(w, widgetIndex) ? INSET_RECT_FLAG_BORDER_INSET : 0;
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Get the colour
uint8_t colour = w->colours[widget.colour];
2014-04-09 22:35:29 +01:00
if (static_cast<int32_t>(widget.image) == -2)
2018-06-22 23:20:47 +02:00
{
2017-06-06 23:24:18 +02:00
// Draw border with no fill
gfx_fill_rect_inset(dpi, rect, colour, press | INSET_RECT_FLAG_FILL_NONE);
2017-06-06 23:24:18 +02:00
return;
}
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Draw the border with fill
gfx_fill_rect_inset(dpi, rect, colour, press);
2014-04-09 22:35:29 +01:00
2020-11-03 14:29:22 -07:00
WidgetDrawImage(dpi, w, widgetIndex);
2014-04-09 22:35:29 +01:00
}
/**
*
* rct2: 0x006EB806
*/
2020-11-03 14:29:22 -07:00
static void WidgetTabDraw(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex)
2014-04-09 22:35:29 +01:00
{
2017-06-06 23:24:18 +02:00
// Get the widget
auto& widget = w->widgets[widgetIndex];
2014-04-09 22:35:29 +01:00
if (widget.type != WindowWidgetType::Tab && static_cast<int32_t>(widget.image) == -1)
2017-06-06 23:24:18 +02:00
return;
2014-04-09 22:35:29 +01:00
if (widget.type == WindowWidgetType::Tab)
2020-08-13 16:41:13 +02:00
{
2020-11-03 14:29:22 -07:00
if (WidgetIsDisabled(w, widgetIndex))
2020-08-13 16:41:13 +02:00
return;
if (widget.image == static_cast<uint32_t>(SPR_NONE))
2020-08-13 16:41:13 +02:00
{
// Set standard tab sprite to use.
widget.image = IMAGE_TYPE_REMAP | SPR_TAB;
2020-08-13 16:41:13 +02:00
}
}
2017-06-06 23:24:18 +02:00
// Draw widgets that aren't explicitly disabled.
2020-11-03 14:29:22 -07:00
if (!WidgetIsDisabled(w, widgetIndex))
2018-06-22 23:20:47 +02:00
{
2020-11-03 14:29:22 -07:00
WidgetDrawImage(dpi, w, widgetIndex);
2017-06-06 23:24:18 +02:00
return;
}
if (widget.type != WindowWidgetType::TrnBtn)
2018-06-22 23:20:47 +02:00
{
2020-11-03 14:29:22 -07:00
WidgetDrawImage(dpi, w, widgetIndex);
2017-06-06 23:24:18 +02:00
return;
}
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Resolve the absolute ltrb
auto leftTop = w->windowPos + ScreenCoordsXY{ widget.left, widget.top };
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Get the colour and disabled image
uint8_t colour = w->colours[widget.colour] & 0x7F;
uint32_t image = widget.image + 2;
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Draw disabled image
gfx_draw_sprite(dpi, ImageId(image, colour), leftTop);
2014-04-09 22:35:29 +01:00
}
/**
2015-10-20 20:16:30 +02:00
*
2014-04-09 22:35:29 +01:00
* rct2: 0x006EB861
*/
2020-11-03 14:29:22 -07:00
static void WidgetFlatButtonDraw(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex)
2014-04-09 22:35:29 +01:00
{
2020-11-03 14:29:22 -07:00
if (!WidgetIsDisabled(w, widgetIndex) && WidgetIsHighlighted(w, widgetIndex))
2018-06-22 23:20:47 +02:00
{
2020-11-03 14:29:22 -07:00
WidgetButtonDraw(dpi, w, widgetIndex);
2017-06-06 23:24:18 +02:00
return;
}
2014-04-11 17:38:15 +01:00
2017-06-06 23:24:18 +02:00
// Get the widget
const auto& widget = w->widgets[widgetIndex];
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Resolve the absolute ltrb
ScreenRect rect{ w->windowPos + ScreenCoordsXY{ widget.left, widget.top },
w->windowPos + ScreenCoordsXY{ widget.right, widget.bottom } };
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Get the colour
uint8_t colour = w->colours[widget.colour];
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Check if the button is pressed down
2020-11-03 14:29:22 -07:00
if (WidgetIsPressed(w, widgetIndex) || WidgetIsActiveTool(w, widgetIndex))
2018-06-22 23:20:47 +02:00
{
if (static_cast<int32_t>(widget.image) == -2)
2018-06-22 23:20:47 +02:00
{
2017-06-06 23:24:18 +02:00
// Draw border with no fill
gfx_fill_rect_inset(dpi, rect, colour, INSET_RECT_FLAG_BORDER_INSET | INSET_RECT_FLAG_FILL_NONE);
2017-06-06 23:24:18 +02:00
return;
}
2014-04-11 17:38:15 +01:00
2017-06-06 23:24:18 +02:00
// Draw the border with fill
gfx_fill_rect_inset(dpi, rect, colour, INSET_RECT_FLAG_BORDER_INSET);
2017-06-06 23:24:18 +02:00
}
2014-04-11 17:38:15 +01:00
2017-06-06 23:24:18 +02:00
// Draw image
2020-11-03 14:29:22 -07:00
WidgetDrawImage(dpi, w, widgetIndex);
2014-04-09 22:35:29 +01:00
}
/**
2015-10-20 20:16:30 +02:00
*
* rct2: 0x006EBBEB
*/
2020-11-03 14:29:22 -07:00
static void WidgetTextButton(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex)
{
2017-06-06 23:24:18 +02:00
// Get the widget
const auto& widget = w->widgets[widgetIndex];
2017-06-06 23:24:18 +02:00
// Resolve the absolute ltrb
ScreenRect rect{ w->windowPos + ScreenCoordsXY{ widget.left, widget.top },
w->windowPos + ScreenCoordsXY{ widget.right, widget.bottom } };
2017-06-06 23:24:18 +02:00
// Get the colour
uint8_t colour = w->colours[widget.colour];
2017-06-06 23:24:18 +02:00
// Border
2020-11-03 14:29:22 -07:00
uint8_t press = WidgetIsPressed(w, widgetIndex) || WidgetIsActiveTool(w, widgetIndex) ? INSET_RECT_FLAG_BORDER_INSET : 0;
gfx_fill_rect_inset(dpi, rect, colour, press);
// Button caption
if (widget.type != WindowWidgetType::TableHeader)
{
2020-11-03 14:29:22 -07:00
WidgetTextCentred(dpi, w, widgetIndex);
}
else
{
2020-11-03 14:29:22 -07:00
WidgetText(dpi, w, widgetIndex);
}
}
/**
2015-10-20 20:16:30 +02:00
*
* rct2: 0x006EBC41
*/
2020-11-03 14:29:22 -07:00
static void WidgetTextCentred(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex)
{
2017-06-06 23:24:18 +02:00
// Get the widget
const auto& widget = w->widgets[widgetIndex];
if (widget.text == STR_NONE)
return;
// Get the colour
colour_t colour = w->colours[widget.colour];
colour &= ~(COLOUR_FLAG_TRANSLUCENT);
2020-11-03 14:29:22 -07:00
if (WidgetIsDisabled(w, widgetIndex))
colour |= COLOUR_FLAG_INSET;
2017-06-06 23:24:18 +02:00
// Resolve the absolute ltrb
auto topLeft = w->windowPos + ScreenCoordsXY{ widget.left, 0 };
int32_t r = w->windowPos.x + widget.right;
if (widget.type == WindowWidgetType::Button || widget.type == WindowWidgetType::TableHeader)
topLeft.y += widget.textTop();
else
topLeft.y += widget.top;
auto stringId = widget.text;
auto ft = Formatter::Common();
if (widget.flags & WIDGET_FLAGS::TEXT_IS_STRING)
2018-03-25 22:04:36 +01:00
{
stringId = STR_STRING;
ft.Add<utf8*>(widget.string);
2018-03-25 22:04:36 +01:00
}
2021-01-28 23:51:56 +00:00
ScreenCoordsXY coords = { (topLeft.x + r + 1) / 2 - 1, topLeft.y };
if (widget.type == WindowWidgetType::LabelCentred)
2021-01-28 23:51:56 +00:00
{
DrawTextWrapped(dpi, coords, widget.width() - 2, stringId, ft, { colour, TextAlignment::CENTRE });
2021-01-28 23:51:56 +00:00
}
else
{
DrawTextEllipsised(dpi, coords, widget.width() - 2, stringId, ft, { colour, TextAlignment::CENTRE });
2021-01-28 23:51:56 +00:00
}
}
/**
2015-10-20 20:16:30 +02:00
*
* rct2: 0x006EBD52
*/
2020-11-03 14:29:22 -07:00
static void WidgetText(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex)
{
2017-06-06 23:24:18 +02:00
// Get the widget
const auto& widget = w->widgets[widgetIndex];
if (widget.text == STR_NONE || widget.text == STR_VIEWPORT)
2017-06-06 23:24:18 +02:00
return;
// Get the colour
uint8_t colour = w->colours[widget.colour];
2020-11-03 14:29:22 -07:00
if (WidgetIsDisabled(w, widgetIndex))
2017-06-06 23:24:18 +02:00
colour |= COLOUR_FLAG_INSET;
// Resolve the absolute ltrb
int32_t l = w->windowPos.x + widget.left;
int32_t r = w->windowPos.x + widget.right;
int32_t t;
if (widget.type == WindowWidgetType::Button || widget.type == WindowWidgetType::DropdownMenu
|| widget.type == WindowWidgetType::Spinner || widget.type == WindowWidgetType::TableHeader)
{
t = w->windowPos.y + widget.textTop();
}
else
t = w->windowPos.y + widget.top;
auto stringId = widget.text;
auto ft = Formatter::Common();
if (widget.flags & WIDGET_FLAGS::TEXT_IS_STRING)
2018-03-25 22:04:36 +01:00
{
stringId = STR_STRING;
ft.Add<utf8*>(widget.string);
2018-03-25 22:04:36 +01:00
}
2021-01-28 23:51:56 +00:00
ScreenCoordsXY coords = { l + 1, t };
if (widget.type == WindowWidgetType::LabelCentred)
2021-01-28 23:51:56 +00:00
{
DrawTextWrapped(dpi, coords, r - l, stringId, ft, { colour, TextAlignment::CENTRE });
2021-01-28 23:51:56 +00:00
}
else
{
DrawTextEllipsised(dpi, coords, r - l, stringId, ft, colour);
}
}
2014-04-17 03:19:30 +01:00
/**
2015-10-20 20:16:30 +02:00
*
2014-04-17 03:19:30 +01:00
* rct2: 0x006EBD1F
*/
2020-11-03 14:29:22 -07:00
static void WidgetTextInset(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex)
2014-04-17 03:19:30 +01:00
{
2017-06-06 23:24:18 +02:00
// Get the widget
const auto& widget = w->widgets[widgetIndex];
2014-04-17 03:19:30 +01:00
2017-06-06 23:24:18 +02:00
// Resolve the absolute ltrb
ScreenRect rect{ w->windowPos + ScreenCoordsXY{ widget.left, widget.top },
w->windowPos + ScreenCoordsXY{ widget.right, widget.bottom } };
2014-04-17 03:19:30 +01:00
2017-06-06 23:24:18 +02:00
// Get the colour
uint8_t colour = w->colours[widget.colour];
2014-04-17 03:19:30 +01:00
gfx_fill_rect_inset(dpi, rect, colour, INSET_RECT_F_60);
2020-11-03 14:29:22 -07:00
WidgetText(dpi, w, widgetIndex);
2014-04-17 03:19:30 +01:00
}
static std::pair<rct_string_id, void*> widget_get_stringid_and_args(const rct_widget& widget)
2020-02-15 15:07:10 +00:00
{
auto stringId = widget.text;
2020-02-15 15:07:10 +00:00
void* formatArgs = gCommonFormatArgs;
if (widget.flags & WIDGET_FLAGS::TEXT_IS_STRING)
2020-02-15 15:07:10 +00:00
{
if (widget.string == nullptr || widget.string[0] == '\0')
2020-02-15 15:07:10 +00:00
{
stringId = STR_NONE;
formatArgs = nullptr;
}
else
{
stringId = STR_STRING;
formatArgs = const_cast<void*>(reinterpret_cast<const void*>(&widget.string));
2020-02-15 15:07:10 +00:00
}
}
return std::make_pair(stringId, formatArgs);
}
2014-04-28 17:35:12 +01:00
/**
2015-10-20 20:16:30 +02:00
*
2014-04-28 17:35:12 +01:00
* rct2: 0x006EB535
*/
2020-11-03 14:29:22 -07:00
static void WidgetGroupboxDraw(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex)
2014-04-28 17:35:12 +01:00
{
2017-06-06 23:24:18 +02:00
// Get the widget
const auto& widget = w->widgets[widgetIndex];
2014-04-28 17:35:12 +01:00
2017-06-06 23:24:18 +02:00
// Resolve the absolute ltrb
auto l = w->windowPos.x + widget.left + 5;
auto t = w->windowPos.y + widget.top;
2021-09-24 23:21:35 +02:00
auto textRight = l;
2014-04-28 17:35:12 +01:00
2017-06-06 23:24:18 +02:00
// Text
2020-02-15 15:07:10 +00:00
auto [stringId, formatArgs] = widget_get_stringid_and_args(widget);
2020-02-15 14:19:11 +00:00
if (stringId != STR_NONE)
2018-06-22 23:20:47 +02:00
{
uint8_t colour = w->colours[widget.colour] & 0x7F;
2020-11-03 14:29:22 -07:00
if (WidgetIsDisabled(w, widgetIndex))
2018-01-03 19:12:44 +01:00
colour |= COLOUR_FLAG_INSET;
2017-09-03 23:13:55 +01:00
utf8 buffer[512] = { 0 };
2020-02-15 14:19:11 +00:00
format_string(buffer, sizeof(buffer), stringId, formatArgs);
auto ft = Formatter();
ft.Add<utf8*>(buffer);
DrawTextBasic(dpi, { l, t }, STR_STRING, ft, { colour });
textRight = l + gfx_get_string_width(buffer, FontSpriteBase::MEDIUM) + 1;
2017-06-06 23:24:18 +02:00
}
2014-04-28 17:35:12 +01:00
2017-06-06 23:24:18 +02:00
// Border
// Resolve the absolute ltrb
l = w->windowPos.x + widget.left;
t = w->windowPos.y + widget.top + 4;
const auto r = w->windowPos.x + widget.right;
const auto b = w->windowPos.y + widget.bottom;
2014-04-28 17:35:12 +01:00
2017-06-06 23:24:18 +02:00
// Get the colour
uint8_t colour = w->colours[widget.colour] & 0x7F;
2014-04-28 17:35:12 +01:00
2017-06-06 23:24:18 +02:00
// Border left of text
gfx_fill_rect(dpi, { { l, t }, { l + 4, t } }, ColourMapA[colour].mid_dark);
gfx_fill_rect(dpi, { { l + 1, t + 1 }, { l + 4, t + 1 } }, ColourMapA[colour].lighter);
2014-04-28 17:35:12 +01:00
2017-06-06 23:24:18 +02:00
// Border right of text
gfx_fill_rect(dpi, { { textRight, t }, { r - 1, t } }, ColourMapA[colour].mid_dark);
gfx_fill_rect(dpi, { { textRight, t + 1 }, { r - 2, t + 1 } }, ColourMapA[colour].lighter);
2014-04-28 17:35:12 +01:00
2017-06-06 23:24:18 +02:00
// Border right
gfx_fill_rect(dpi, { { r - 1, t + 1 }, { r - 1, b - 1 } }, ColourMapA[colour].mid_dark);
gfx_fill_rect(dpi, { { r, t }, { r, b } }, ColourMapA[colour].lighter);
2014-04-28 17:35:12 +01:00
2017-06-06 23:24:18 +02:00
// Border bottom
gfx_fill_rect(dpi, { { l, b - 1 }, { r - 2, b - 1 } }, ColourMapA[colour].mid_dark);
gfx_fill_rect(dpi, { { l, b }, { r - 1, b } }, ColourMapA[colour].lighter);
2014-04-28 17:35:12 +01:00
2017-06-06 23:24:18 +02:00
// Border left
gfx_fill_rect(dpi, { { l, t + 1 }, { l, b - 2 } }, ColourMapA[colour].mid_dark);
gfx_fill_rect(dpi, { { l + 1, t + 2 }, { l + 1, b - 2 } }, ColourMapA[colour].lighter);
2014-04-28 17:35:12 +01:00
}
2014-04-09 22:35:29 +01:00
/**
2015-10-20 20:16:30 +02:00
*
2014-04-09 22:35:29 +01:00
* rct2: 0x006EB2F9
*/
2020-11-03 14:29:22 -07:00
static void WidgetCaptionDraw(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex)
2014-04-09 22:35:29 +01:00
{
2017-06-06 23:24:18 +02:00
// Get the widget
const auto* widget = &w->widgets[widgetIndex];
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Resolve the absolute ltrb
auto topLeft = w->windowPos + ScreenCoordsXY{ widget->left, widget->top };
auto bottomRight = w->windowPos + ScreenCoordsXY{ widget->right, widget->bottom };
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Get the colour
uint8_t colour = w->colours[widget->colour];
2014-04-09 22:35:29 +01:00
uint8_t press = INSET_RECT_F_60;
2017-06-06 23:24:18 +02:00
if (w->flags & WF_10)
press |= INSET_RECT_FLAG_FILL_MID_LIGHT;
2014-04-09 22:35:29 +01:00
gfx_fill_rect_inset(dpi, { topLeft, bottomRight }, colour, press);
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Black caption bars look slightly green, this fixes that
if (colour == 0)
gfx_fill_rect(
dpi, { { topLeft + ScreenCoordsXY{ 1, 1 } }, { bottomRight - ScreenCoordsXY{ 1, 1 } } }, ColourMapA[colour].dark);
2017-06-06 23:24:18 +02:00
else
gfx_filter_rect(
dpi, { { topLeft + ScreenCoordsXY{ 1, 1 } }, { bottomRight - ScreenCoordsXY{ 1, 1 } } },
FilterPaletteID::PaletteDarken3);
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Draw text
if (widget->text == STR_NONE)
return;
2014-04-19 03:16:20 +01:00
topLeft = w->windowPos + ScreenCoordsXY{ widget->left + 2, widget->top + 1 };
int32_t width = widget->width() - 4;
if ((widget + 1)->type == WindowWidgetType::CloseBox)
2018-06-22 23:20:47 +02:00
{
2017-06-06 23:24:18 +02:00
width -= 10;
if ((widget + 2)->type == WindowWidgetType::CloseBox)
2017-06-06 23:24:18 +02:00
width -= 10;
}
topLeft.x += width / 2;
DrawTextEllipsised(
dpi, topLeft, width, widget->text, Formatter::Common(), { COLOUR_WHITE | COLOUR_FLAG_OUTLINE, TextAlignment::CENTRE });
2014-04-09 22:35:29 +01:00
}
/**
2015-10-20 20:16:30 +02:00
*
2014-04-09 22:35:29 +01:00
* rct2: 0x006EBB85
*/
2020-11-03 14:29:22 -07:00
static void WidgetCloseboxDraw(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex)
2014-04-09 22:35:29 +01:00
{
2017-06-06 23:24:18 +02:00
// Get the widget
const auto& widget = w->widgets[widgetIndex];
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Resolve the absolute ltrb
auto topLeft = w->windowPos + ScreenCoordsXY{ widget.left, widget.top };
auto bottomRight = w->windowPos + ScreenCoordsXY{ widget.right, widget.bottom };
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Check if the button is pressed down
uint8_t press = 0;
2017-06-06 23:24:18 +02:00
if (w->flags & WF_10)
press |= INSET_RECT_FLAG_FILL_MID_LIGHT;
2020-11-03 14:29:22 -07:00
if (WidgetIsPressed(w, widgetIndex) || WidgetIsActiveTool(w, widgetIndex))
2017-06-06 23:24:18 +02:00
press |= INSET_RECT_FLAG_BORDER_INSET;
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Get the colour
uint8_t colour = w->colours[widget.colour];
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Draw the button
gfx_fill_rect_inset(dpi, { topLeft, bottomRight }, colour, press);
2014-04-09 22:35:29 +01:00
if (widget.text == STR_NONE)
2017-06-06 23:24:18 +02:00
return;
2014-04-09 22:35:29 +01:00
topLeft = w->windowPos + ScreenCoordsXY{ widget.midX() - 1, std::max<int32_t>(widget.top, widget.midY() - 5) };
2014-04-09 22:35:29 +01:00
2020-11-03 14:29:22 -07:00
if (WidgetIsDisabled(w, widgetIndex))
2017-06-06 23:24:18 +02:00
colour |= COLOUR_FLAG_INSET;
2014-04-09 22:35:29 +01:00
DrawTextEllipsised(dpi, topLeft, widget.width() - 2, widget.text, Formatter::Common(), { colour, TextAlignment::CENTRE });
2014-04-09 22:35:29 +01:00
}
2014-05-11 10:51:05 +02:00
/**
2015-10-20 20:16:30 +02:00
*
2018-06-22 23:20:47 +02:00
* rct2: 0x006EBAD9
*/
2020-11-03 14:29:22 -07:00
static void WidgetCheckboxDraw(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex)
2014-05-11 10:51:05 +02:00
{
2017-06-06 23:24:18 +02:00
// Get the widget
const auto& widget = w->widgets[widgetIndex];
2014-05-11 10:51:05 +02:00
2017-06-06 23:24:18 +02:00
// Resolve the absolute ltb
ScreenCoordsXY topLeft = w->windowPos + ScreenCoordsXY{ widget.left, widget.top };
ScreenCoordsXY bottomRight = w->windowPos + ScreenCoordsXY{ widget.right, widget.bottom };
ScreenCoordsXY midLeft = { topLeft.x, (topLeft.y + bottomRight.y) / 2 };
2014-05-11 10:51:05 +02:00
2017-06-06 23:24:18 +02:00
// Get the colour
colour_t colour = w->colours[widget.colour];
2014-05-11 10:51:05 +02:00
2018-01-03 21:31:25 +01:00
// checkbox
gfx_fill_rect_inset(dpi, { midLeft - ScreenCoordsXY{ 0, 5 }, midLeft + ScreenCoordsXY{ 9, 4 } }, colour, INSET_RECT_F_60);
2014-05-11 10:51:05 +02:00
2020-11-03 14:29:22 -07:00
if (WidgetIsDisabled(w, widgetIndex))
2018-08-17 21:57:30 +02:00
{
colour |= COLOUR_FLAG_INSET;
}
2018-01-03 21:31:25 +01:00
// fill it when checkbox is pressed
2020-11-03 14:29:22 -07:00
if (WidgetIsPressed(w, widgetIndex))
2018-06-22 23:20:47 +02:00
{
gfx_draw_string(
dpi, { midLeft - ScreenCoordsXY{ 0, 5 } }, static_cast<const char*>(CheckBoxMarkString),
{ static_cast<colour_t>(NOT_TRANSLUCENT(colour)) });
2017-06-06 23:24:18 +02:00
}
2014-05-11 10:51:05 +02:00
2017-06-06 23:24:18 +02:00
// draw the text
if (widget.text == STR_NONE)
2017-06-06 23:24:18 +02:00
return;
2014-05-11 10:51:05 +02:00
2020-02-15 15:07:10 +00:00
auto [stringId, formatArgs] = widget_get_stringid_and_args(widget);
gfx_draw_string_left_centred(dpi, stringId, formatArgs, colour, { midLeft + ScreenCoordsXY{ 14, 0 } });
2014-05-11 10:51:05 +02:00
}
2014-04-09 22:35:29 +01:00
/**
2015-10-20 20:16:30 +02:00
*
2014-04-09 22:35:29 +01:00
* rct2: 0x006EBD96
*/
2020-11-03 14:29:22 -07:00
static void WidgetScrollDraw(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex)
2014-04-09 22:35:29 +01:00
{
2017-06-06 23:24:18 +02:00
// Get the widget
int32_t scrollIndex = window_get_scroll_data_index(w, widgetIndex);
const auto& widget = w->widgets[widgetIndex];
const auto& scroll = w->scrolls[scrollIndex];
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Resolve the absolute ltrb
ScreenCoordsXY topLeft = w->windowPos + ScreenCoordsXY{ widget.left, widget.top };
ScreenCoordsXY bottomRight = w->windowPos + ScreenCoordsXY{ widget.right, widget.bottom };
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Get the colour
uint8_t colour = w->colours[widget.colour];
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Draw the border
gfx_fill_rect_inset(dpi, { topLeft, bottomRight }, colour, INSET_RECT_F_60);
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Inflate by -1
topLeft.x++;
topLeft.y++;
bottomRight.x--;
bottomRight.y--;
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Horizontal scrollbar
if (scroll.flags & HSCROLLBAR_VISIBLE)
2020-11-03 14:29:22 -07:00
WidgetHScrollbarDraw(
dpi, scroll, topLeft.x, bottomRight.y - SCROLLBAR_WIDTH,
((scroll.flags & VSCROLLBAR_VISIBLE) ? bottomRight.x - (SCROLLBAR_WIDTH + 1) : bottomRight.x), bottomRight.y,
colour);
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Vertical scrollbar
if (scroll.flags & VSCROLLBAR_VISIBLE)
2020-11-03 14:29:22 -07:00
WidgetVScrollbarDraw(
dpi, scroll, bottomRight.x - SCROLLBAR_WIDTH, topLeft.y, bottomRight.x,
((scroll.flags & HSCROLLBAR_VISIBLE) ? bottomRight.y - (SCROLLBAR_WIDTH + 1) : bottomRight.y), colour);
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Contents
if (scroll.flags & HSCROLLBAR_VISIBLE)
bottomRight.y -= (SCROLLBAR_WIDTH + 1);
if (scroll.flags & VSCROLLBAR_VISIBLE)
bottomRight.x -= (SCROLLBAR_WIDTH + 1);
2014-04-09 22:35:29 +01:00
bottomRight.y++;
bottomRight.x++;
2014-09-21 09:21:36 +01:00
2017-06-06 23:24:18 +02:00
// Create a new inner scroll dpi
rct_drawpixelinfo scroll_dpi = *dpi;
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Clip the scroll dpi against the outer dpi
int32_t cl = std::max<int32_t>(dpi->x, topLeft.x);
int32_t ct = std::max<int32_t>(dpi->y, topLeft.y);
int32_t cr = std::min<int32_t>(dpi->x + dpi->width, bottomRight.x);
int32_t cb = std::min<int32_t>(dpi->y + dpi->height, bottomRight.y);
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Set the respective dpi attributes
scroll_dpi.x = cl - topLeft.x + scroll.h_left;
scroll_dpi.y = ct - topLeft.y + scroll.v_top;
2017-06-06 23:24:18 +02:00
scroll_dpi.width = cr - cl;
scroll_dpi.height = cb - ct;
scroll_dpi.bits += cl - dpi->x;
scroll_dpi.bits += (ct - dpi->y) * (dpi->width + dpi->pitch);
scroll_dpi.pitch = (dpi->width + dpi->pitch) - scroll_dpi.width;
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Draw the scroll contents
if (scroll_dpi.width > 0 && scroll_dpi.height > 0)
window_event_scroll_paint_call(w, &scroll_dpi, scrollIndex);
2014-04-09 22:35:29 +01:00
}
2020-11-03 14:29:22 -07:00
static void WidgetHScrollbarDraw(
rct_drawpixelinfo* dpi, const rct_scroll& scroll, int32_t l, int32_t t, int32_t r, int32_t b, int32_t colour)
2014-04-09 22:35:29 +01:00
{
2017-06-06 23:24:18 +02:00
colour &= 0x7F;
// Trough
gfx_fill_rect(dpi, { { l + SCROLLBAR_WIDTH, t }, { r - SCROLLBAR_WIDTH, b } }, ColourMapA[colour].lighter);
gfx_fill_rect(dpi, { { l + SCROLLBAR_WIDTH, t }, { r - SCROLLBAR_WIDTH, b } }, 0x1000000 | ColourMapA[colour].mid_dark);
gfx_fill_rect(dpi, { { l + SCROLLBAR_WIDTH, t + 2 }, { r - SCROLLBAR_WIDTH, t + 2 } }, ColourMapA[colour].mid_dark);
gfx_fill_rect(dpi, { { l + SCROLLBAR_WIDTH, t + 3 }, { r - SCROLLBAR_WIDTH, t + 3 } }, ColourMapA[colour].lighter);
gfx_fill_rect(dpi, { { l + SCROLLBAR_WIDTH, t + 7 }, { r - SCROLLBAR_WIDTH, t + 7 } }, ColourMapA[colour].mid_dark);
gfx_fill_rect(dpi, { { l + SCROLLBAR_WIDTH, t + 8 }, { r - SCROLLBAR_WIDTH, t + 8 } }, ColourMapA[colour].lighter);
2015-10-20 20:16:30 +02:00
2017-06-06 23:24:18 +02:00
// Left button
{
uint8_t flags = (scroll.flags & HSCROLLBAR_LEFT_PRESSED) ? INSET_RECT_FLAG_BORDER_INSET : 0;
gfx_fill_rect_inset(dpi, { { l, t }, { l + (SCROLLBAR_WIDTH - 1), b } }, colour, flags);
gfx_draw_string(dpi, { l + 1, t }, static_cast<const char*>(BlackLeftArrowString), {});
}
2015-10-20 20:16:30 +02:00
2017-06-06 23:24:18 +02:00
// Thumb
{
int16_t left = std::max(l + SCROLLBAR_WIDTH, l + scroll.h_thumb_left - 1);
int16_t right = std::min(r - SCROLLBAR_WIDTH, l + scroll.h_thumb_right - 1);
uint8_t flags = (scroll.flags & HSCROLLBAR_THUMB_PRESSED) ? INSET_RECT_FLAG_BORDER_INSET : 0;
gfx_fill_rect_inset(dpi, { { left, t }, { right, b } }, colour, flags);
}
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Right button
{
uint8_t flags = (scroll.flags & HSCROLLBAR_RIGHT_PRESSED) ? INSET_RECT_FLAG_BORDER_INSET : 0;
gfx_fill_rect_inset(dpi, { { r - (SCROLLBAR_WIDTH - 1), t }, { r, b } }, colour, flags);
gfx_draw_string(dpi, { r - 6, t }, static_cast<const char*>(BlackRightArrowString), {});
}
2014-04-09 22:35:29 +01:00
}
2020-11-03 14:29:22 -07:00
static void WidgetVScrollbarDraw(
rct_drawpixelinfo* dpi, const rct_scroll& scroll, int32_t l, int32_t t, int32_t r, int32_t b, int32_t colour)
2014-04-09 22:35:29 +01:00
{
2017-06-06 23:24:18 +02:00
colour &= 0x7F;
// Trough
gfx_fill_rect(dpi, { { l, t + SCROLLBAR_WIDTH }, { r, b - SCROLLBAR_WIDTH } }, ColourMapA[colour].lighter);
gfx_fill_rect(dpi, { { l, t + SCROLLBAR_WIDTH }, { r, b - SCROLLBAR_WIDTH } }, 0x1000000 | ColourMapA[colour].mid_dark);
gfx_fill_rect(dpi, { { l + 2, t + SCROLLBAR_WIDTH }, { l + 2, b - SCROLLBAR_WIDTH } }, ColourMapA[colour].mid_dark);
gfx_fill_rect(dpi, { { l + 3, t + SCROLLBAR_WIDTH }, { l + 3, b - SCROLLBAR_WIDTH } }, ColourMapA[colour].lighter);
gfx_fill_rect(dpi, { { l + 7, t + SCROLLBAR_WIDTH }, { l + 7, b - SCROLLBAR_WIDTH } }, ColourMapA[colour].mid_dark);
gfx_fill_rect(dpi, { { l + 8, t + SCROLLBAR_WIDTH }, { l + 8, b - SCROLLBAR_WIDTH } }, ColourMapA[colour].lighter);
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Up button
2018-06-22 23:20:47 +02:00
gfx_fill_rect_inset(
dpi, { { l, t }, { r, t + (SCROLLBAR_WIDTH - 1) } }, colour,
((scroll.flags & VSCROLLBAR_UP_PRESSED) ? INSET_RECT_FLAG_BORDER_INSET : 0));
gfx_draw_string(dpi, { l + 1, t - 1 }, static_cast<const char*>(BlackUpArrowString), {});
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Thumb
2018-06-22 23:20:47 +02:00
gfx_fill_rect_inset(
dpi,
{ { l, std::max(t + SCROLLBAR_WIDTH, t + scroll.v_thumb_top - 1) },
{ r, std::min(b - SCROLLBAR_WIDTH, t + scroll.v_thumb_bottom - 1) } },
colour, ((scroll.flags & VSCROLLBAR_THUMB_PRESSED) ? INSET_RECT_FLAG_BORDER_INSET : 0));
2014-04-09 22:35:29 +01:00
2017-06-06 23:24:18 +02:00
// Down button
2018-06-22 23:20:47 +02:00
gfx_fill_rect_inset(
dpi, { { l, b - (SCROLLBAR_WIDTH - 1) }, { r, b } }, colour,
((scroll.flags & VSCROLLBAR_DOWN_PRESSED) ? INSET_RECT_FLAG_BORDER_INSET : 0));
gfx_draw_string(dpi, { l + 1, b - (SCROLLBAR_WIDTH - 1) }, static_cast<const char*>(BlackDownArrowString), {});
2014-04-09 22:35:29 +01:00
}
2014-04-29 22:25:37 +01:00
/**
2015-10-20 20:16:30 +02:00
*
2014-04-29 22:25:37 +01:00
* rct2: 0x006EB951
*/
2020-11-03 14:29:22 -07:00
static void WidgetDrawImage(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex)
2014-04-11 17:38:15 +01:00
{
2017-06-06 23:24:18 +02:00
// Get the widget
const auto& widget = w->widgets[widgetIndex];
2014-04-11 17:38:15 +01:00
2017-06-06 23:24:18 +02:00
// Get the image
int32_t image = widget.image;
2017-06-06 23:24:18 +02:00
if (image == SPR_NONE)
return;
2014-04-11 17:38:15 +01:00
2017-06-06 23:24:18 +02:00
// Resolve the absolute ltrb
auto screenCoords = w->windowPos + ScreenCoordsXY{ widget.left, widget.top };
2014-04-11 17:38:15 +01:00
2017-06-06 23:24:18 +02:00
// Get the colour
uint8_t colour = NOT_TRANSLUCENT(w->colours[widget.colour]);
2014-04-11 17:38:15 +01:00
if (widget.type == WindowWidgetType::ColourBtn || widget.type == WindowWidgetType::TrnBtn
|| widget.type == WindowWidgetType::Tab)
2020-11-03 14:29:22 -07:00
if (WidgetIsPressed(w, widgetIndex) || WidgetIsActiveTool(w, widgetIndex))
2017-06-06 23:24:18 +02:00
image++;
2014-04-11 17:38:15 +01:00
2020-11-03 14:29:22 -07:00
if (WidgetIsDisabled(w, widgetIndex))
2018-06-22 23:20:47 +02:00
{
2017-06-06 23:24:18 +02:00
// Draw greyed out (light border bottom right shadow)
colour = w->colours[widget.colour];
2017-06-06 23:24:18 +02:00
colour = ColourMapA[NOT_TRANSLUCENT(colour)].lighter;
gfx_draw_sprite_solid(dpi, image, screenCoords + ScreenCoordsXY{ 1, 1 }, colour);
2014-04-29 22:25:37 +01:00
2017-06-06 23:24:18 +02:00
// Draw greyed out (dark)
colour = w->colours[widget.colour];
2017-06-06 23:24:18 +02:00
colour = ColourMapA[NOT_TRANSLUCENT(colour)].mid_light;
gfx_draw_sprite_solid(dpi, image, screenCoords, colour);
2018-06-22 23:20:47 +02:00
}
else
{
if (image & IMAGE_TYPE_REMAP_2_PLUS)
{
2017-06-06 23:24:18 +02:00
// ?
}
2014-04-11 17:38:15 +01:00
if (image & IMAGE_TYPE_TRANSPARENT)
image &= ~IMAGE_TYPE_TRANSPARENT;
2017-06-06 23:24:18 +02:00
else
image |= colour << 19;
2014-04-11 17:38:15 +01:00
gfx_draw_sprite(dpi, ImageId::FromUInt32(image), screenCoords);
2017-06-06 23:24:18 +02:00
}
2014-04-11 17:38:15 +01:00
}
2020-11-03 14:29:22 -07:00
bool WidgetIsEnabled(rct_window* w, rct_widgetindex widgetIndex)
2014-04-24 18:53:42 +01:00
{
2021-01-23 12:21:18 +00:00
if (!WidgetIsVisible(w, widgetIndex))
return false;
return (w->enabled_widgets & (1LL << widgetIndex)) != 0;
2014-04-24 18:53:42 +01:00
}
2020-11-03 14:29:22 -07:00
bool WidgetIsDisabled(rct_window* w, rct_widgetindex widgetIndex)
2014-04-09 22:35:29 +01:00
{
return (w->disabled_widgets & (1LL << widgetIndex)) != 0;
2014-04-09 22:35:29 +01:00
}
2021-02-04 00:25:34 +00:00
bool WidgetIsHoldable(rct_window* w, rct_widgetindex widgetIndex)
{
return (w->hold_down_widgets & (1LL << widgetIndex)) != 0;
}
2021-01-23 12:21:18 +00:00
bool WidgetIsVisible(rct_window* w, rct_widgetindex widgetIndex)
{
return w->widgets[widgetIndex].IsVisible();
}
2020-11-03 14:29:22 -07:00
bool WidgetIsPressed(rct_window* w, rct_widgetindex widgetIndex)
2014-04-09 22:35:29 +01:00
{
2018-06-22 23:20:47 +02:00
if (w->pressed_widgets & (1LL << widgetIndex))
{
return true;
2017-06-06 23:24:18 +02:00
}
if (input_get_state() == InputState::WidgetPressed || input_get_state() == InputState::DropdownActive)
2018-06-22 23:20:47 +02:00
{
if (!(input_test_flag(INPUT_FLAG_WIDGET_PRESSED)))
return false;
if (gPressedWidget.window_classification != w->classification)
return false;
if (gPressedWidget.window_number != w->number)
return false;
if (gPressedWidget.widget_index != widgetIndex)
return false;
return true;
2017-06-06 23:24:18 +02:00
}
return false;
2014-04-09 22:35:29 +01:00
}
2020-11-03 14:29:22 -07:00
bool WidgetIsHighlighted(rct_window* w, rct_widgetindex widgetIndex)
2014-04-09 22:35:29 +01:00
{
2018-06-22 23:20:47 +02:00
if (gHoverWidget.window_classification != w->classification)
return false;
if (gHoverWidget.window_number != w->number)
return false;
if (gHoverWidget.widget_index != widgetIndex)
return false;
return true;
2014-04-17 15:12:41 +01:00
}
2020-11-03 14:29:22 -07:00
bool WidgetIsActiveTool(rct_window* w, rct_widgetindex widgetIndex)
2014-04-17 15:12:41 +01:00
{
2017-06-06 23:24:18 +02:00
if (!(input_test_flag(INPUT_FLAG_TOOL_ACTIVE)))
return false;
2017-06-06 23:24:18 +02:00
if (gCurrentToolWidget.window_classification != w->classification)
return false;
2017-06-06 23:24:18 +02:00
if (gCurrentToolWidget.window_number != w->number)
return false;
2017-06-06 23:24:18 +02:00
if (gCurrentToolWidget.widget_index != widgetIndex)
return false;
2014-04-17 15:12:41 +01:00
return true;
}
2014-06-13 22:40:15 +02:00
/**
2015-12-11 09:38:37 -06:00
*
* rct2: 0x006E9F92
2017-06-06 23:24:18 +02:00
* eax: x / output_x
* ebx: y / output_y
* ecx: output_scroll_area
* edx: scroll_id
2017-06-06 23:24:18 +02:00
* esi: w
* edi: widget
*/
2020-11-03 14:29:22 -07:00
void WidgetScrollGetPart(
rct_window* w, const rct_widget* widget, const ScreenCoordsXY& screenCoords, ScreenCoordsXY& retScreenCoords,
int32_t* output_scroll_area, int32_t* scroll_id)
2014-06-13 22:40:15 +02:00
{
2017-06-06 23:24:18 +02:00
*scroll_id = 0;
for (rct_widget* iterator = w->widgets; iterator != widget; iterator++)
2017-06-06 23:24:18 +02:00
{
if (iterator->type == WindowWidgetType::Scroll)
2017-06-06 23:24:18 +02:00
{
*scroll_id += 1;
2017-06-06 23:24:18 +02:00
}
}
2014-06-13 22:40:15 +02:00
const auto& scroll = w->scrolls[*scroll_id];
if ((scroll.flags & HSCROLLBAR_VISIBLE) && screenCoords.y >= (w->windowPos.y + widget->bottom - (SCROLLBAR_WIDTH + 1)))
2017-06-06 23:24:18 +02:00
{
// horizontal scrollbar
int32_t rightOffset = 0;
int32_t iteratorLeft = widget->left + w->windowPos.x + SCROLLBAR_WIDTH;
int32_t iteratorRight = widget->right + w->windowPos.x - SCROLLBAR_WIDTH;
if (!(scroll.flags & VSCROLLBAR_VISIBLE))
2017-06-06 23:24:18 +02:00
{
rightOffset = SCROLLBAR_WIDTH + 1;
2017-06-06 23:24:18 +02:00
}
if (screenCoords.x <= iteratorLeft)
2017-06-06 23:24:18 +02:00
{
*output_scroll_area = SCROLL_PART_HSCROLLBAR_LEFT;
}
else if (screenCoords.x >= iteratorRight + rightOffset)
2017-06-06 23:24:18 +02:00
{
*output_scroll_area = SCROLL_PART_NONE;
}
else if (screenCoords.x >= iteratorRight + rightOffset - SCROLLBAR_WIDTH)
2017-06-06 23:24:18 +02:00
{
*output_scroll_area = SCROLL_PART_HSCROLLBAR_RIGHT;
}
else if (screenCoords.x < (widget->left + w->windowPos.x + scroll.h_thumb_left))
2017-06-06 23:24:18 +02:00
{
*output_scroll_area = SCROLL_PART_HSCROLLBAR_LEFT_TROUGH;
}
else if (screenCoords.x > (widget->left + w->windowPos.x + scroll.h_thumb_right))
2017-06-06 23:24:18 +02:00
{
*output_scroll_area = SCROLL_PART_HSCROLLBAR_RIGHT_TROUGH;
}
else
{
*output_scroll_area = SCROLL_PART_HSCROLLBAR_THUMB;
}
}
else if ((scroll.flags & VSCROLLBAR_VISIBLE) && (screenCoords.x >= w->windowPos.x + widget->right - (SCROLLBAR_WIDTH + 1)))
2017-06-06 23:24:18 +02:00
{
// vertical scrollbar
int32_t bottomOffset = 0;
int32_t iteratorTop = widget->top + w->windowPos.y + SCROLLBAR_WIDTH;
2020-03-01 16:32:35 -03:00
int32_t iteratorBottom = widget->bottom + w->windowPos.y;
if (scroll.flags & HSCROLLBAR_VISIBLE)
2017-06-06 23:24:18 +02:00
{
bottomOffset = (SCROLLBAR_WIDTH + 1);
2017-06-06 23:24:18 +02:00
}
if (screenCoords.y <= iteratorTop)
2017-06-06 23:24:18 +02:00
{
*output_scroll_area = SCROLL_PART_VSCROLLBAR_TOP;
}
else if (screenCoords.y >= (iteratorBottom - bottomOffset))
2017-06-06 23:24:18 +02:00
{
*output_scroll_area = SCROLL_PART_NONE;
}
else if (screenCoords.y >= (iteratorBottom - bottomOffset - SCROLLBAR_WIDTH))
2017-06-06 23:24:18 +02:00
{
*output_scroll_area = SCROLL_PART_VSCROLLBAR_BOTTOM;
}
else if (screenCoords.y < (widget->top + w->windowPos.y + scroll.v_thumb_top))
2017-06-06 23:24:18 +02:00
{
*output_scroll_area = SCROLL_PART_VSCROLLBAR_TOP_TROUGH;
}
else if (screenCoords.y > (widget->top + w->windowPos.y + scroll.v_thumb_bottom))
2017-06-06 23:24:18 +02:00
{
*output_scroll_area = SCROLL_PART_VSCROLLBAR_BOTTOM_TROUGH;
}
else
{
*output_scroll_area = SCROLL_PART_VSCROLLBAR_THUMB;
}
}
else
{
2018-06-22 23:20:47 +02:00
// view
2017-06-06 23:24:18 +02:00
*output_scroll_area = SCROLL_PART_VIEW;
retScreenCoords.x = screenCoords.x - widget->left;
retScreenCoords.y = screenCoords.y - widget->top;
2020-03-01 16:32:35 -03:00
retScreenCoords -= w->windowPos;
if (retScreenCoords.x <= 0 || retScreenCoords.y <= 0)
2017-06-06 23:24:18 +02:00
{
*output_scroll_area = SCROLL_PART_NONE;
}
else
{
retScreenCoords.x += scroll.h_left - 1;
retScreenCoords.y += scroll.v_top - 1;
2017-06-06 23:24:18 +02:00
}
}
2014-06-13 22:40:15 +02:00
}
2020-11-03 14:29:22 -07:00
void WidgetSetEnabled(rct_window* w, rct_widgetindex widgetIndex, bool enabled)
2016-06-05 00:45:13 +02:00
{
2018-06-22 23:20:47 +02:00
if (enabled)
{
2017-06-06 23:24:18 +02:00
w->enabled_widgets |= (1ULL << widgetIndex);
w->disabled_widgets &= ~(1ULL << widgetIndex);
}
2018-06-22 23:20:47 +02:00
else
{
2017-06-06 23:24:18 +02:00
w->enabled_widgets &= ~(1ULL << widgetIndex);
w->disabled_widgets |= (1ULL << widgetIndex);
}
2016-06-05 00:45:13 +02:00
}
void WidgetSetDisabled(rct_window* w, rct_widgetindex widgetIndex, bool value)
{
if (value)
{
w->disabled_widgets |= (1ULL << widgetIndex);
}
else
{
w->disabled_widgets &= ~(1ULL << widgetIndex);
}
}
2021-02-04 00:25:34 +00:00
void WidgetSetHoldable(rct_window* w, rct_widgetindex widgetIndex, bool value)
{
if (value)
{
w->hold_down_widgets |= (1ULL << widgetIndex);
}
else
{
w->hold_down_widgets &= ~(1ULL << widgetIndex);
}
}
2021-01-23 12:21:18 +00:00
void WidgetSetVisible(rct_window* w, rct_widgetindex widgetIndex, bool value)
{
if (value)
{
w->widgets[widgetIndex].flags &= ~WIDGET_FLAGS::IS_HIDDEN;
}
else
{
w->widgets[widgetIndex].flags |= WIDGET_FLAGS::IS_HIDDEN;
}
}
2020-11-03 14:29:22 -07:00
void WidgetSetCheckboxValue(rct_window* w, rct_widgetindex widgetIndex, int32_t value)
{
2017-06-06 23:24:18 +02:00
if (value)
w->pressed_widgets |= (1ULL << widgetIndex);
else
w->pressed_widgets &= ~(1ULL << widgetIndex);
2015-06-06 17:04:53 -04:00
}
2020-11-03 14:29:22 -07:00
static void WidgetTextBoxDraw(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex)
2015-06-06 17:04:53 -04:00
{
int32_t no_lines = 0;
2017-06-06 23:24:18 +02:00
char wrapped_string[TEXT_INPUT_SIZE];
2015-06-06 17:04:53 -04:00
2017-06-06 23:24:18 +02:00
// Get the widget
const auto& widget = w->widgets[widgetIndex];
2015-06-06 17:04:53 -04:00
2017-06-06 23:24:18 +02:00
// Resolve the absolute ltrb
ScreenCoordsXY topLeft{ w->windowPos + ScreenCoordsXY{ widget.left, widget.top } };
ScreenCoordsXY bottomRight{ w->windowPos + ScreenCoordsXY{ widget.right, widget.bottom } };
2015-06-06 17:04:53 -04:00
2017-06-06 23:24:18 +02:00
// Get the colour
uint8_t colour = w->colours[widget.colour];
2015-06-06 17:04:53 -04:00
2018-06-22 23:20:47 +02:00
bool active = w->classification == gCurrentTextBox.window.classification && w->number == gCurrentTextBox.window.number
&& widgetIndex == gCurrentTextBox.widget_index;
2015-06-06 17:04:53 -04:00
2018-06-22 23:20:47 +02:00
// gfx_fill_rect_inset(dpi, l, t, r, b, colour, 0x20 | (!active ? 0x40 : 0x00));
gfx_fill_rect_inset(dpi, { topLeft, bottomRight }, colour, INSET_RECT_F_60);
2015-06-06 17:04:53 -04:00
// Figure out where the text should be positioned vertically.
topLeft.y = w->windowPos.y + widget.textTop();
2018-06-22 23:20:47 +02:00
if (!active || gTextInput == nullptr)
{
if (widget.text != 0)
2018-06-22 23:20:47 +02:00
{
safe_strcpy(wrapped_string, widget.string, 512);
gfx_wrap_string(wrapped_string, bottomRight.x - topLeft.x - 5, FontSpriteBase::MEDIUM, &no_lines);
gfx_draw_string_no_formatting(
dpi, { topLeft.x + 2, topLeft.y }, wrapped_string, { w->colours[1], FontSpriteBase::MEDIUM });
2017-06-06 23:24:18 +02:00
}
return;
}
2015-06-06 17:04:53 -04:00
2017-06-06 23:24:18 +02:00
safe_strcpy(wrapped_string, gTextBoxInput, TEXT_INPUT_SIZE);
2015-06-06 17:04:53 -04:00
2017-06-06 23:24:18 +02:00
// String length needs to add 12 either side of box
// +13 for cursor when max length.
gfx_wrap_string(wrapped_string, bottomRight.x - topLeft.x - 5 - 6, FontSpriteBase::MEDIUM, &no_lines);
2015-06-06 17:04:53 -04:00
gfx_draw_string_no_formatting(dpi, { topLeft.x + 2, topLeft.y }, wrapped_string, { w->colours[1], FontSpriteBase::MEDIUM });
2015-06-06 17:04:53 -04:00
2017-06-06 23:24:18 +02:00
size_t string_length = get_string_size(wrapped_string) - 1;
2015-06-06 17:04:53 -04:00
2017-06-06 23:24:18 +02:00
// Make a copy of the string for measuring the width.
char temp_string[TEXT_INPUT_SIZE] = { 0 };
std::memcpy(temp_string, wrapped_string, std::min(string_length, gTextInput->SelectionStart));
int32_t cur_x = topLeft.x + gfx_get_string_width_no_formatting(temp_string, FontSpriteBase::MEDIUM) + 3;
2015-06-06 17:04:53 -04:00
int32_t width = 6;
2020-03-28 16:36:51 -03:00
if (static_cast<uint32_t>(gTextInput->SelectionStart) < strlen(gTextBoxInput))
2018-06-22 23:20:47 +02:00
{
2017-06-06 23:24:18 +02:00
// Make a new 1 character wide string for measuring the width
// of the character that the cursor is under.
temp_string[1] = '\0';
temp_string[0] = gTextBoxInput[gTextInput->SelectionStart];
width = std::max(gfx_get_string_width_no_formatting(temp_string, FontSpriteBase::MEDIUM) - 2, 4);
2017-06-06 23:24:18 +02:00
}
2015-06-06 17:04:53 -04:00
2018-06-22 23:20:47 +02:00
if (gTextBoxFrameNo <= 15)
{
2017-06-06 23:24:18 +02:00
colour = ColourMapA[w->colours[1]].mid_light;
auto y = topLeft.y + (widget.height() - 1);
gfx_fill_rect(dpi, { { cur_x, y }, { cur_x + width, y } }, colour + 5);
2017-06-06 23:24:18 +02:00
}
2015-06-06 17:04:53 -04:00
}
uint32_t GetColourButtonImage(colour_t colour)
{
return SPRITE_ID_PALETTE_COLOUR_1(colour) | IMAGE_TYPE_TRANSPARENT | SPR_PALETTE_BTN;
}