Safely look up widget for drawing

This commit is contained in:
Michael Steenbeek 2022-03-10 01:01:59 +01:00 committed by GitHub
parent 9b1c915f4d
commit 0f2cf23318
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 9 deletions

View File

@ -48,8 +48,14 @@ static void WidgetDrawImage(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetind
*/
void WidgetDraw(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex widgetIndex)
{
const auto& widget = w->widgets[widgetIndex];
switch (widget.type)
const auto* widget = GetWidgetByIndex(*w, widgetIndex);
if (widget == nullptr)
{
log_error("Tried drawing an out-of-bounds widget index!");
return;
}
switch (widget->type)
{
case WindowWidgetType::Frame:
WidgetFrameDraw(dpi, w, widgetIndex);
@ -1030,22 +1036,36 @@ void WidgetScrollGetPart(
}
}
static void SafeSetWidgetFlag(rct_window* w, rct_widgetindex widgetIndex, WidgetFlags mask, bool value)
rct_widget* GetWidgetByIndex(const rct_window& w, rct_widgetindex widgetIndex)
{
// Make sure we don't go out of bounds if we are given a bad widget index
rct_widgetindex index = 0;
for (auto* widget = w->widgets; widget->type != WindowWidgetType::Last; widget++)
for (auto* widget = w.widgets; widget->type != WindowWidgetType::Last; widget++)
{
if (index == widgetIndex)
{
if (value)
widget->flags |= mask;
else
widget->flags &= ~mask;
break;
return widget;
}
index++;
}
log_error("Widget index %i out of bounds for window class %u", widgetIndex, w.classification);
return nullptr;
}
static void SafeSetWidgetFlag(rct_window* w, rct_widgetindex widgetIndex, WidgetFlags mask, bool value)
{
rct_widget* widget = GetWidgetByIndex(*w, widgetIndex);
if (widget == nullptr)
{
return;
}
if (value)
widget->flags |= mask;
else
widget->flags &= ~mask;
}
void WidgetSetEnabled(rct_window* w, rct_widgetindex widgetIndex, bool enabled)

View File

@ -12,3 +12,4 @@
#include <openrct2/interface/Widget.h>
uint32_t GetColourButtonImage(colour_t colour);
rct_widget* GetWidgetByIndex(const rct_window& w, rct_widgetindex widgetIndex);