From 70ce930b1f481815daa6bfeea28c8ee79279f7ff Mon Sep 17 00:00:00 2001 From: Jarod42 Date: Sun, 3 Sep 2023 20:40:49 +0200 Subject: [PATCH] `PopupByIdent` return by reference. --- src/include/ui.h | 2 +- src/ui/botpanel.cpp | 29 +++++++++++++---------------- src/ui/ui.cpp | 7 ++++--- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/include/ui.h b/src/include/ui.h index afac06024..0216d591e 100644 --- a/src/include/ui.h +++ b/src/include/ui.h @@ -563,7 +563,7 @@ extern void FreeButtonStyles(); extern void UserInterfaceCclRegister(); /// return popup by ident string -extern CPopup *PopupByIdent(const std::string &ident); +extern CPopup &PopupByIdent(std::string_view ident); /// Find a button style extern ButtonStyle *FindButtonStyle(const std::string &style); diff --git a/src/ui/botpanel.cpp b/src/ui/botpanel.cpp index 999de7c9f..71d12e193 100644 --- a/src/ui/botpanel.cpp +++ b/src/ui/botpanel.cpp @@ -163,11 +163,7 @@ int AddButton(int pos, int level, const std::string &icon_ident, ba->CommentSound.MapSound(); } if (!ba->Popup.empty()) { - CPopup *popup = PopupByIdent(ba->Popup); - if (!popup) { - fprintf(stderr, "Popup \"%s\" hasn't defined.\n ", ba->Popup.c_str()); - Exit(1); - } + PopupByIdent(ba->Popup); // Checking for error } ba->ButtonCursor = cursor; ba->Popup = popup; @@ -545,12 +541,13 @@ static struct PopupDrawCache { */ void DrawPopup(const ButtonAction &button, const CUIButton &uibutton, int x, int y) { - CPopup *popup = PopupByIdent(button.Popup); + if (button.Popup.empty()) { + return; + } + CPopup &popup = PopupByIdent(button.Popup); bool useCache = false; - if (!popup) { - return; - } else if (&button == LastDrawnButtonPopup) { + if (&button == LastDrawnButtonPopup) { useCache = true; } else { LastDrawnButtonPopup = const_cast(&button); @@ -583,9 +580,9 @@ void DrawPopup(const ButtonAction &button, const CUIButton &uibutton, int x, int popupWidth = popupCache.popupWidth; popupHeight = popupCache.popupHeight; } else { - GetPopupSize(*popup, button, popupWidth, popupHeight, Costs); - popupWidth = std::max(popupWidth, popup->MinWidth); - popupHeight = std::max(popupHeight, popup->MinHeight); + GetPopupSize(popup, button, popupWidth, popupHeight, Costs); + popupWidth = std::max(popupWidth, popup.MinWidth); + popupHeight = std::max(popupHeight, popup.MinHeight); popupCache.popupWidth = popupWidth; popupCache.popupHeight = popupHeight; } @@ -596,15 +593,15 @@ void DrawPopup(const ButtonAction &button, const CUIButton &uibutton, int x, int clamp(&y, 0, Video.Height - 1); // Background - Video.FillTransRectangle(popup->BackgroundColor, x, y, popupWidth, popupHeight, popup->BackgroundColor >> ASHIFT); - Video.DrawRectangle(popup->BorderColor, x, y, popupWidth, popupHeight); + Video.FillTransRectangle(popup.BackgroundColor, x, y, popupWidth, popupHeight, popup.BackgroundColor >> ASHIFT); + Video.DrawRectangle(popup.BorderColor, x, y, popupWidth, popupHeight); // Contents - for (CPopupContentType *contentPtr : popup->Contents) { + for (CPopupContentType *contentPtr : popup.Contents) { const CPopupContentType &content = *contentPtr; if (CanShowPopupContent(content.Condition, button, UnitTypes[button.Value])) { - content.Draw(x + content.pos.x, y + content.pos.y, *popup, popupWidth, button, Costs); + content.Draw(x + content.pos.x, y + content.pos.y, popup, popupWidth, button, Costs); } } diff --git a/src/ui/ui.cpp b/src/ui/ui.cpp index 250dfce22..9b396a62a 100644 --- a/src/ui/ui.cpp +++ b/src/ui/ui.cpp @@ -186,14 +186,15 @@ CUserInterface::CUserInterface() : ** ** @return popup class pointer. */ -CPopup *PopupByIdent(const std::string &ident) +CPopup &PopupByIdent(std::string_view ident) { const auto it = ranges::find_if(UI.ButtonPopups, [&](CPopup *popup) { return popup->Ident == ident; }); if (it != UI.ButtonPopups.end()) { - return *it; + return **it; } - return nullptr; + DebugPrint("Unknown popup '%s'\n" _C_ ident.data()); + ExitFatal(1); } /**