Skip to content

Commit

Permalink
Move more Ui functions to Ui library (OpenRCT2#22444)
Browse files Browse the repository at this point in the history
* Move various methods into the ui library

* Move various zoom functions to ui
  • Loading branch information
duncanspumpkin authored Aug 1, 2024
1 parent b1e14c6 commit 144fa13
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 185 deletions.
2 changes: 1 addition & 1 deletion src/openrct2-ui/UiContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ class UiContext final : public IUiContext
if (abs(gesturePixels) > tolerance)
{
_gestureRadius = 0;
MainWindowZoom(gesturePixels > 0, true);
Windows::MainWindowZoom(gesturePixels > 0, true);
}
}
break;
Expand Down
220 changes: 174 additions & 46 deletions src/openrct2-ui/interface/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <algorithm>
#include <openrct2-ui/windows/Window.h>
#include <openrct2/Context.h>
#include <openrct2/GameState.h>
#include <openrct2/Input.h>
#include <openrct2/OpenRCT2.h>
#include <openrct2/audio/audio.h>
Expand Down Expand Up @@ -318,9 +319,9 @@ static void WindowViewportWheelInput(WindowBase& w, int32_t wheel)
return;

if (wheel < 0)
WindowZoomIn(w, true);
Windows::WindowZoomIn(w, true);
else if (wheel > 0)
WindowZoomOut(w, true);
Windows::WindowZoomOut(w, true);
}

static bool isSpinnerGroup(WindowBase& w, WidgetIndex index, WindowWidgetType buttonType)
Expand Down Expand Up @@ -481,48 +482,6 @@ void ApplyScreenSaverLockSetting()
Config::Get().general.DisableScreensaver ? SDL_DisableScreenSaver() : SDL_EnableScreenSaver();
}

/**
*
* rct2: 0x006EB15C
*/
void WindowDrawWidgets(WindowBase& w, DrawPixelInfo& dpi)
{
Widget* widget;
WidgetIndex widgetIndex;

if ((w.flags & WF_TRANSPARENT) && !(w.flags & WF_NO_BACKGROUND))
GfxFilterRect(
dpi, { w.windowPos, w.windowPos + ScreenCoordsXY{ w.width - 1, w.height - 1 } }, FilterPaletteID::Palette51);

// todo: some code missing here? Between 006EB18C and 006EB260

widgetIndex = 0;
for (widget = w.widgets; widget->type != WindowWidgetType::Last; widget++)
{
if (widget->IsVisible())
{
// Check if widget is outside the draw region
if (w.windowPos.x + widget->left < dpi.x + dpi.width && w.windowPos.x + widget->right >= dpi.x)
{
if (w.windowPos.y + widget->top < dpi.y + dpi.height && w.windowPos.y + widget->bottom >= dpi.y)
{
w.OnDrawWidget(widgetIndex, dpi);
}
}
}
widgetIndex++;
}

// todo: something missing here too? Between 006EC32B and 006EC369

if (w.flags & WF_WHITE_BORDER_MASK)
{
GfxFillRectInset(
dpi, { w.windowPos, w.windowPos + ScreenCoordsXY{ w.width - 1, w.height - 1 } }, { COLOUR_WHITE },
INSET_RECT_FLAG_FILL_NONE);
}
}

/**
*
* rct2: 0x006EA776
Expand All @@ -543,9 +502,21 @@ static void WindowInvalidatePressedImageButton(const WindowBase& w)
}
}

void Window::ScrollToViewport()
{
if (viewport == nullptr || !focus.has_value())
return;

CoordsXYZ newCoords = focus.value().GetPos();

auto mainWindow = WindowGetMain();
if (mainWindow != nullptr)
WindowScrollToLocation(*mainWindow, newCoords);
}

void Window::OnDraw(DrawPixelInfo& dpi)
{
WindowDrawWidgets(*this, dpi);
Windows::WindowDrawWidgets(*this, dpi);
}

void Window::OnDrawWidget(WidgetIndex widgetIndex, DrawPixelInfo& dpi)
Expand Down Expand Up @@ -605,7 +576,7 @@ void Window::SetCheckboxValue(WidgetIndex widgetIndex, bool value)

void Window::DrawWidgets(DrawPixelInfo& dpi)
{
WindowDrawWidgets(*this, dpi);
Windows::WindowDrawWidgets(*this, dpi);
}

void Window::Close()
Expand Down Expand Up @@ -658,6 +629,69 @@ void Window::TextInputOpen(
this, callWidget, title, description, descriptionArgs, existingText, existingArgs, maxLength);
}

void Window::ResizeFrame()
{
// Frame
widgets[0].right = width - 1;
widgets[0].bottom = height - 1;
// Title
widgets[1].right = width - 2;
// Close button
if (Config::Get().interface.WindowButtonsOnTheLeft)
{
widgets[2].left = 2;
widgets[2].right = 2 + kCloseButtonWidth;
}
else
{
widgets[2].left = width - 3 - kCloseButtonWidth;
widgets[2].right = width - 3;
}
}

void Window::ResizeFrameWithPage()
{
ResizeFrame();
// Page background
widgets[3].right = width - 1;
widgets[3].bottom = height - 1;
}

void Window::ResizeSpinner(WidgetIndex widgetIndex, const ScreenCoordsXY& origin, const ScreenSize& size)
{
auto right = origin.x + size.width - 1;
auto bottom = origin.y + size.height - 1;
widgets[widgetIndex].left = origin.x;
widgets[widgetIndex].top = origin.y;
widgets[widgetIndex].right = right;
widgets[widgetIndex].bottom = bottom;

widgets[widgetIndex + 1].left = right - size.height; // subtract height to maintain aspect ratio
widgets[widgetIndex + 1].top = origin.y + 1;
widgets[widgetIndex + 1].right = right - 1;
widgets[widgetIndex + 1].bottom = bottom - 1;

widgets[widgetIndex + 2].left = right - size.height * 2;
widgets[widgetIndex + 2].top = origin.y + 1;
widgets[widgetIndex + 2].right = right - size.height - 1;
widgets[widgetIndex + 2].bottom = bottom - 1;
}

void Window::ResizeDropdown(WidgetIndex widgetIndex, const ScreenCoordsXY& origin, const ScreenSize& size)
{
auto right = origin.x + size.width - 1;
auto bottom = origin.y + size.height - 1;
widgets[widgetIndex].left = origin.x;
widgets[widgetIndex].top = origin.y;
widgets[widgetIndex].right = right;
widgets[widgetIndex].bottom = bottom;

widgets[widgetIndex + 1].left = right - size.height + 1; // subtract height to maintain aspect ratio
widgets[widgetIndex + 1].top = origin.y + 1;
widgets[widgetIndex + 1].right = right - 1;
widgets[widgetIndex + 1].bottom = bottom - 1;
}

void WindowAlignTabs(WindowBase* w, WidgetIndex start_tab_id, WidgetIndex end_tab_id)
{
int32_t i, x = w->widgets[start_tab_id].left;
Expand Down Expand Up @@ -1255,4 +1289,98 @@ namespace OpenRCT2::Ui::Windows
w->OnResize();
});
}

/**
*
* rct2: 0x00685BE1
*
* @param dpi (edi)
* @param w (esi)
*/
void WindowDrawViewport(DrawPixelInfo& dpi, WindowBase& w)
{
ViewportRender(dpi, w.viewport, { { dpi.x, dpi.y }, { dpi.x + dpi.width, dpi.y + dpi.height } });
}

/**
*
* rct2: 0x006EB15C
*/
void WindowDrawWidgets(WindowBase& w, DrawPixelInfo& dpi)
{
Widget* widget;
WidgetIndex widgetIndex;

if ((w.flags & WF_TRANSPARENT) && !(w.flags & WF_NO_BACKGROUND))
GfxFilterRect(
dpi, { w.windowPos, w.windowPos + ScreenCoordsXY{ w.width - 1, w.height - 1 } }, FilterPaletteID::Palette51);

// todo: some code missing here? Between 006EB18C and 006EB260

widgetIndex = 0;
for (widget = w.widgets; widget->type != WindowWidgetType::Last; widget++)
{
if (widget->IsVisible())
{
// Check if widget is outside the draw region
if (w.windowPos.x + widget->left < dpi.x + dpi.width && w.windowPos.x + widget->right >= dpi.x)
{
if (w.windowPos.y + widget->top < dpi.y + dpi.height && w.windowPos.y + widget->bottom >= dpi.y)
{
w.OnDrawWidget(widgetIndex, dpi);
}
}
}
widgetIndex++;
}

// todo: something missing here too? Between 006EC32B and 006EC369

if (w.flags & WF_WHITE_BORDER_MASK)
{
GfxFillRectInset(
dpi, { w.windowPos, w.windowPos + ScreenCoordsXY{ w.width - 1, w.height - 1 } }, { COLOUR_WHITE },
INSET_RECT_FLAG_FILL_NONE);
}
}

/**
*
* rct2: 0x006887A6
*/
void WindowZoomIn(WindowBase& w, bool atCursor)
{
WindowZoomSet(w, w.viewport->zoom - 1, atCursor);
}

/**
*
* rct2: 0x006887E0
*/
void WindowZoomOut(WindowBase& w, bool atCursor)
{
WindowZoomSet(w, w.viewport->zoom + 1, atCursor);
}

void MainWindowZoom(bool zoomIn, bool atCursor)
{
auto* mainWindow = WindowGetMain();
if (mainWindow == nullptr)
return;

if (gScreenFlags & SCREEN_FLAGS_TITLE_DEMO)
return;

if (gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR && GetGameState().EditorStep != EditorStep::LandscapeEditor)
return;

if (gScreenFlags & SCREEN_FLAGS_TRACK_MANAGER)
return;

if (zoomIn)
WindowZoomIn(*mainWindow, atCursor);
else
WindowZoomOut(*mainWindow, atCursor);
}

} // namespace OpenRCT2::Ui::Windows
14 changes: 14 additions & 0 deletions src/openrct2-ui/interface/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct Window : WindowBase
virtual void OnDraw(DrawPixelInfo& dpi) override;
virtual void OnDrawWidget(WidgetIndex widgetIndex, DrawPixelInfo& dpi) override;

void ScrollToViewport();
void InitScrollWidgets();
void InvalidateWidget(WidgetIndex widgetIndex);
bool IsWidgetDisabled(WidgetIndex widgetIndex) const;
Expand All @@ -36,6 +37,12 @@ struct Window : WindowBase
void TextInputOpen(
WidgetIndex callWidget, StringId title, StringId description, const Formatter& descriptionArgs, StringId existingText,
uintptr_t existingArgs, int32_t maxLength);

void ResizeFrame();
void ResizeFrameWithPage();

void ResizeSpinner(WidgetIndex widgetIndex, const ScreenCoordsXY& origin, const ScreenSize& size);
void ResizeDropdown(WidgetIndex widgetIndex, const ScreenCoordsXY& origin, const ScreenSize& size);
};

void WindowAllWheelInput();
Expand Down Expand Up @@ -136,4 +143,11 @@ namespace OpenRCT2::Ui::Windows
bool WindowCanResize(const WindowBase& w);

void InvalidateAllWindowsAfterInput();

void WindowDrawWidgets(WindowBase& w, DrawPixelInfo& dpi);
void WindowDrawViewport(DrawPixelInfo& dpi, WindowBase& w);

void WindowZoomIn(WindowBase& w, bool atCursor);
void WindowZoomOut(WindowBase& w, bool atCursor);
void MainWindowZoom(bool zoomIn, bool atCursor);
} // namespace OpenRCT2::Ui::Windows
Loading

0 comments on commit 144fa13

Please sign in to comment.