1
0
Fork 0

Codechange: Manage window viewport via unique_ptr.

pull/13985/head
Peter Nelson 2025-03-27 18:48:15 +00:00 committed by Peter Nelson
parent 8275bbfb87
commit c982816c0e
4 changed files with 5 additions and 13 deletions

View File

@ -205,12 +205,6 @@ static Point MapXYZToViewport(const Viewport &vp, int x, int y, int z)
return p;
}
void DeleteWindowViewport(Window *w)
{
delete w->viewport;
w->viewport = nullptr;
}
/**
* Initialize viewport of the window for use.
* @param w Window to use/display the viewport in
@ -226,7 +220,7 @@ void InitializeWindowViewport(Window *w, int x, int y,
{
assert(w->viewport == nullptr);
ViewportData *vp = new ViewportData();
auto vp = std::make_unique<ViewportData>();
vp->left = x + w->left;
vp->top = y + w->top;
@ -272,9 +266,10 @@ void InitializeWindowViewport(Window *w, int x, int y,
vp->overlay = nullptr;
w->viewport = vp;
vp->virtual_left = 0;
vp->virtual_top = 0;
w->viewport = std::move(vp);
}
static Point _vp_move_offs;
@ -415,7 +410,7 @@ Viewport *IsPtInWindowViewport(const Window *w, int x, int y)
if (w->viewport == nullptr) return nullptr;
const Viewport &vp = *w->viewport;
if (IsInsideMM(x, vp.left, vp.left + vp.width) && IsInsideMM(y, vp.top, vp.top + vp.height)) return w->viewport;
if (IsInsideMM(x, vp.left, vp.left + vp.width) && IsInsideMM(y, vp.top, vp.top + vp.height)) return w->viewport.get();
return nullptr;
}

View File

@ -21,7 +21,6 @@ static const int TILE_HEIGHT_STEP = 50; ///< One Z unit tile height difference i
void SetSelectionRed(bool);
void DeleteWindowViewport(Window *w);
void InitializeWindowViewport(Window *w, int x, int y, int width, int height, std::variant<TileIndex, VehicleID> focus, ZoomLevel zoom);
Viewport *IsPtInWindowViewport(const Window *w, int x, int y);
Point TranslateXYToTileCoord(const Viewport &vp, int x, int y, bool clamp_to_map = true);

View File

@ -1090,8 +1090,6 @@ Window::~Window()
{
/* Make sure the window is closed, deletion is allowed only in Window::DeleteClosedWindows(). */
assert(*this->z_position == nullptr);
if (this->viewport != nullptr) DeleteWindowViewport(this);
}
/**

View File

@ -316,7 +316,7 @@ public:
Owner owner = INVALID_OWNER; ///< The owner of the content shown in this window. Company colour is acquired from this variable.
ViewportData *viewport = nullptr; ///< Pointer to viewport data, if present.
std::unique_ptr<ViewportData> viewport; ///< Pointer to viewport data, if present.
const NWidgetCore *nested_focus = nullptr; ///< Currently focused nested widget, or \c nullptr if no nested widget has focus.
std::map<WidgetID, QueryString*> querystrings{}; ///< QueryString associated to WWT_EDITBOX widgets.
std::unique_ptr<NWidgetBase> nested_root{}; ///< Root of the nested tree.