Merge pull request #483 from Wargus/reference

`PopupByIdent` return by reference.
This commit is contained in:
Joris Dauphin 2023-09-03 22:47:46 +02:00 committed by GitHub
commit 7ff1df4999
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 20 deletions

View file

@ -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);

View file

@ -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<ButtonAction *>(&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<int>(&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);
}
}

View file

@ -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);
}
/**