From d04d765034bc495a4c471054f68fd9261f606bac Mon Sep 17 00:00:00 2001 From: Jarod42 Date: Sat, 7 Oct 2023 01:15:05 +0200 Subject: [PATCH 1/3] Clean up game/trigger.cpp --- src/game/trigger.cpp | 40 +++++++++++++++----------------------- src/include/util.h | 46 +++++++++++++++++++++++--------------------- 2 files changed, 39 insertions(+), 47 deletions(-) diff --git a/src/game/trigger.cpp b/src/game/trigger.cpp index 9330558179..81de501423 100644 --- a/src/game/trigger.cpp +++ b/src/game/trigger.cpp @@ -47,13 +47,15 @@ #include "unit_find.h" #include "unittype.h" +#include + /*---------------------------------------------------------------------------- -- Variables ----------------------------------------------------------------------------*/ CTimer GameTimer; /// The game timer static int Trigger; -static bool *ActiveTriggers; +static std::vector ActiveTriggers; /// Some data accessible for script during the game. TriggerDataType TriggerData; @@ -300,22 +302,15 @@ static int CclIfRescuedNearUnit(lua_State *l) */ int GetNumOpponents(int player) { - int n = 0; - - // Check the player opponents - for (int i = 0; i < PlayerMax; ++i) { + return ranges::count_if(Players, [&](const CPlayer &p) { + // Check the player opponents // This player is our enemy and has units left. - if ((Players[player].IsEnemy(Players[i])) || (Players[i].IsEnemy(Players[player]))) { - // Don't count walls - for (CUnit *unit : Players[i].GetUnits()) { - if (unit->Type->BoolFlag[WALL_INDEX].value == false) { - ++n; - break; - } - } - } - } - return n; + // Don't count walls + return (p.IsEnemy(Players[player]) || Players[player].IsEnemy(p)) + && ranges::any_of(p.GetUnits(), [](const CUnit *unit) { + return unit->Type->BoolFlag[WALL_INDEX].value == false; + }); + }); } /** @@ -432,7 +427,7 @@ static int CclAddTrigger(lua_State *l) } const int i = lua_rawlen(l, -1); - if (ActiveTriggers && !ActiveTriggers[i / 2]) { + if (!ActiveTriggers.empty() && !ActiveTriggers[i / 2]) { lua_pushnil(l); lua_rawseti(l, -2, i + 1); lua_pushnil(l); @@ -465,7 +460,7 @@ static int CclSetActiveTriggers(lua_State *l) { const int args = lua_gettop(l); - ActiveTriggers = new bool[args]; + ActiveTriggers.resize(args); for (int j = 0; j < args; ++j) { ActiveTriggers[j] = LuaToBoolean(l, j + 1); } @@ -489,11 +484,7 @@ static bool TriggerExecuteAction(int script) for (int j = 0; j < args; ++j) { lua_rawgeti(Lua, -1, j + 1); LuaCall(0, 0); - if (lua_gettop(Lua) > base + 1 && lua_toboolean(Lua, -1)) { - ret = true; - } else { - ret = false; - } + ret = lua_gettop(Lua) > base + 1 && lua_toboolean(Lua, -1); lua_settop(Lua, base + 1); } lua_pop(Lua, 1); @@ -646,8 +637,7 @@ void CleanTriggers() Trigger = 0; - delete[] ActiveTriggers; - ActiveTriggers = nullptr; + ActiveTriggers.clear(); GameTimer.Reset(); } diff --git a/src/include/util.h b/src/include/util.h index 58f6b3221d..c827ff2a8d 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -182,110 +182,112 @@ namespace ranges template auto all_of(const Range &range, Predicate &&predicate) { - return std::all_of(begin(range), end(range), std::forward(predicate)); + return std::all_of(std::begin(range), std::end(range), std::forward(predicate)); } template auto any_of(const Range &range, Predicate &&predicate) { - return std::any_of(begin(range), end(range), std::forward(predicate)); + return std::any_of(std::begin(range), std::end(range), std::forward(predicate)); } template auto none_of(const Range &range, Predicate &&predicate) { - return std::none_of(begin(range), end(range), std::forward(predicate)); + return std::none_of(std::begin(range), std::end(range), std::forward(predicate)); } template std::size_t count_if(const Range &range, Predicate &&predicate) { - return std::count_if(begin(range), end(range), predicate); + return std::count_if(std::begin(range), std::end(range), predicate); } template void fill(Range &range, const Value &value) { - std::fill(begin(range), end(range), value); + std::fill(std::begin(range), std::end(range), value); } template void iota(Range &range, const Value startValue) { - std::iota(begin(range), end(range), startValue); + std::iota(std::begin(range), std::end(range), startValue); } template auto find(Range &range, const Value &value, Proj proj = {}) { - return std::find_if( - begin(range), end(range), [&](const auto &elem) { return std::invoke(proj, elem) == value; }); + return std::find_if(std::begin(range), std::end(range), [&](const auto &elem) { + return std::invoke(proj, elem) == value; + }); } template auto find_if(Range &range, Predicate &&predicate) { - return std::find_if(begin(range), end(range), std::forward(predicate)); + return std::find_if(std::begin(range), std::end(range), std::forward(predicate)); } template auto partition(Range &range, Pred pred) { - return std::partition(begin(range), end(range), pred); + return std::partition(std::begin(range), std::end(range), pred); } template bool contains(const Range &range, const Value &value) { - return std::find(begin(range), end(range), value) != end(range); + return std::find(std::begin(range), std::end(range), value) != std::end(range); } template void reverse(Range &range) { - std::reverse(begin(range), end(range)); + std::reverse(std::begin(range), std::end(range)); } template void erase(std::vector &v, const U &value) { - v.erase(std::remove(begin(v), end(v), value), end(v)); + v.erase(std::remove(std::begin(v), std::end(v), value), std::end(v)); } template auto min_element(Range &range) { - return std::min_element(begin(range), end(range)); + return std::min_element(std::begin(range), std::end(range)); } template auto min_element(Range &range, CmpFunction cmp) { - return std::min_element(begin(range), end(range), cmp); + return std::min_element(std::begin(range), std::end(range), cmp); } template auto lower_bound(Range &range, const Value &value) { - return std::lower_bound(begin(range), end(range), value); + return std::lower_bound(std::begin(range), std::end(range), value); } template > auto upper_bound(Range &range, const Value &value, Comparer &&comparer = {}) { - return std::upper_bound(begin(range), end(range), value, std::forward(comparer)); + return std::upper_bound( + std::begin(range), std::end(range), value, std::forward(comparer)); } template auto copy(const Range &range, OutputIt copy_to) { - return std::copy(begin(range), end(range), copy_to); + return std::copy(std::begin(range), std::end(range), copy_to); } template auto copy_if(const Range &range, OutputIt copy_to, UnaryPredicate pred) { - return std::copy_if(begin(range), end(range), copy_to, pred); + return std::copy_if(std::begin(range), std::end(range), copy_to, pred); } template @@ -293,11 +295,11 @@ namespace ranges { if (shift >= 0) { for (int i = 0; i < shift; i++) { - std::rotate(rbegin(range), rbegin(range) + 1, rend(range)); + std::rotate(std::rbegin(range), std::rbegin(range) + 1, std::rend(range)); } } else { for (int i = 0; i > shift; i--) { - std::rotate(begin(range), begin(range) + 1, end(range)); + std::rotate(std::begin(range), std::begin(range) + 1, std::end(range)); } } } @@ -305,7 +307,7 @@ namespace ranges template > void sort(Range &range, Comparer &&comparer = {}) { - std::sort(begin(range), end(range), std::forward(comparer)); + std::sort(std::begin(range), std::end(range), std::forward(comparer)); } } From cddc2a34724567fb811ad4c190b469357bf2c17c Mon Sep 17 00:00:00 2001 From: Jarod42 Date: Sat, 7 Oct 2023 01:46:31 +0200 Subject: [PATCH 2/3] Clean up stratagus/title.cpp --- src/include/title.h | 37 ++++++-------- src/stratagus/title.cpp | 106 +++++++++------------------------------- src/ui/script_ui.cpp | 18 ++----- src/ui/ui.cpp | 8 +-- 4 files changed, 44 insertions(+), 125 deletions(-) diff --git a/src/include/title.h b/src/include/title.h index 56c679ee5d..0d4094ec36 100644 --- a/src/include/title.h +++ b/src/include/title.h @@ -33,8 +33,9 @@ #include "filesystem.h" +#include #include - +#include class CFont; @@ -45,28 +46,20 @@ enum { class TitleScreenLabel { public: - TitleScreenLabel() : Font(0), Xofs(0), Yofs(0), Flags(0) {} + TitleScreenLabel() = default; std::string Text; - CFont *Font; - int Xofs; - int Yofs; - int Flags; + CFont *Font = nullptr; + int Xofs = 0; + int Yofs = 0; + int Flags = 0; }; class TitleScreen { public: - TitleScreen() : StretchImage(true), Timeout(0), Iterations(0), Editor(0), Labels(nullptr) {} - ~TitleScreen() - { - if (this->Labels) { - for (int i = 0; this->Labels[i]; ++i) { - delete this->Labels[i]; - } - delete[] this->Labels; - } - } + TitleScreen() = default; + ~TitleScreen() = default; void ShowTitleImage(); @@ -75,14 +68,14 @@ class TitleScreen public: fs::path File; std::string Music; - bool StretchImage; - int Timeout; - int Iterations; - int Editor; - TitleScreenLabel **Labels; + bool StretchImage = true; + int Timeout = 0; + int Iterations = 0; + int Editor = 0; + std::vector> Labels; }; -extern TitleScreen **TitleScreens; /// File for title screen +extern std::vector> TitleScreens; /// File for title screen extern void ShowTitleScreens(); diff --git a/src/stratagus/title.cpp b/src/stratagus/title.cpp index c75973c437..a76ba1905d 100644 --- a/src/stratagus/title.cpp +++ b/src/stratagus/title.cpp @@ -38,89 +38,29 @@ #include "editor.h" -TitleScreen **TitleScreens; /// Title screens to show at startup +std::vector> TitleScreens; /// Title screens to show at startup static bool WaitNoEvent; /// Flag got an event extern std::string CliMapName; - -/** -** Callback for input. -*/ -static void WaitCallbackButtonPressed(unsigned) -{ - WaitNoEvent = false; -} - -/** -** Callback for input. -*/ -static void WaitCallbackButtonReleased(unsigned) -{ -} - -/** -** Callback for input. -*/ -static void WaitCallbackKeyPressed(unsigned, unsigned) -{ - WaitNoEvent = false; -} - -/** -** Callback for input. -*/ -static void WaitCallbackKeyReleased(unsigned, unsigned) -{ -} - -/** -** Callback for input. -*/ -static void WaitCallbackKeyRepeated(unsigned, unsigned) -{ -} - -/** -** Callback for input. -*/ -static void WaitCallbackMouse(const PixelPos &) -{ -} - -/** -** Callback for exit. -*/ -static void WaitCallbackExit() -{ -} - - void TitleScreen::ShowLabels() { - TitleScreenLabel **labels = this->Labels; - - if (!labels) { - return ; - } - - for (int i = 0; labels[i]; ++i) { - if (!labels[i]->Font) { + for (auto &titleScreenlabel : this->Labels) { + if (!titleScreenlabel->Font) { continue; } // offsets are for 640x480, scale up to actual resolution - const int x = labels[i]->Xofs * Video.Width / 640; - const int y = labels[i]->Yofs * Video.Height / 480; - CLabel label(*labels[i]->Font); + const int x = titleScreenlabel->Xofs * Video.Width / 640; + const int y = titleScreenlabel->Yofs * Video.Height / 480; + CLabel label(*titleScreenlabel->Font); - if (labels[i]->Flags & TitleFlagCenter) { - label.DrawCentered(x, y, labels[i]->Text); + if (titleScreenlabel->Flags & TitleFlagCenter) { + label.DrawCentered(x, y, titleScreenlabel->Text); } else { - label.Draw(x, y, labels[i]->Text); + label.Draw(x, y, titleScreenlabel->Text); } } } - /** ** Show a title image */ @@ -131,13 +71,13 @@ void TitleScreen::ShowTitleImage() WaitNoEvent = true; - callbacks.ButtonPressed = WaitCallbackButtonPressed; - callbacks.ButtonReleased = WaitCallbackButtonReleased; - callbacks.MouseMoved = WaitCallbackMouse; - callbacks.MouseExit = WaitCallbackExit; - callbacks.KeyPressed = WaitCallbackKeyPressed; - callbacks.KeyReleased = WaitCallbackKeyReleased; - callbacks.KeyRepeated = WaitCallbackKeyRepeated; + callbacks.ButtonPressed = +[](unsigned) { WaitNoEvent = false; }; + callbacks.ButtonReleased = +[](unsigned) {}; + callbacks.MouseMoved = +[](const PixelPos &) {}; + callbacks.MouseExit = +[]() {}; + callbacks.KeyPressed = +[](unsigned, unsigned) { WaitNoEvent = false; }; + callbacks.KeyReleased = +[](unsigned, unsigned) {}; + callbacks.KeyRepeated = +[](unsigned, unsigned) {}; //callbacks.NetworkEvent = NetworkEvent; callbacks.NetworkEvent = nullptr; @@ -169,25 +109,25 @@ void TitleScreen::ShowTitleImage() */ void ShowTitleScreens() { - if (!TitleScreens || !CliMapName.empty()) { + if (TitleScreens.empty() || !CliMapName.empty()) { return; } SetVideoSync(); - for (int i = 0; TitleScreens[i]; ++i) { - if ((Editor.Running && !TitleScreens[i]->Editor) || (!Editor.Running && TitleScreens[i]->Editor)) { + for (const auto& titleScreen : TitleScreens) { + if ((Editor.Running && !titleScreen->Editor) || (!Editor.Running && titleScreen->Editor)) { continue; } - if (!TitleScreens[i]->Music.empty()) { - if (TitleScreens[i]->Music == "none" || PlayMusic(TitleScreens[i]->Music) == -1) { + if (!titleScreen->Music.empty()) { + if (titleScreen->Music == "none" || PlayMusic(titleScreen->Music) == -1) { StopMusic(); } } - if (!TitleScreens[i]->File.empty() && PlayMovie(TitleScreens[i]->File.string())) { - TitleScreens[i]->ShowTitleImage(); + if (!titleScreen->File.empty() && PlayMovie(titleScreen->File.string())) { + titleScreen->ShowTitleImage(); } Video.ClearScreen(); diff --git a/src/ui/script_ui.cpp b/src/ui/script_ui.cpp index 5317b7eb3f..4dd3688475 100644 --- a/src/ui/script_ui.cpp +++ b/src/ui/script_ui.cpp @@ -362,23 +362,16 @@ static int CclShowTitleScreens(lua_State *l) */ static int CclSetTitleScreens(lua_State *l) { - if (TitleScreens) { - for (int i = 0; TitleScreens[i]; ++i) { - delete TitleScreens[i]; - } - delete[] TitleScreens; - TitleScreens = nullptr; - } + TitleScreens.clear(); const int args = lua_gettop(l); - TitleScreens = new TitleScreen *[args + 1]; - memset(TitleScreens, 0, (args + 1) * sizeof(TitleScreen *)); + TitleScreens.resize(args); for (int j = 0; j < args; ++j) { if (!lua_istable(l, j + 1)) { LuaError(l, "incorrect argument"); } - TitleScreens[j] = new TitleScreen; + TitleScreens[j] = std::make_unique(); TitleScreens[j]->Iterations = 1; lua_pushnil(l); while (lua_next(l, j + 1)) { @@ -398,14 +391,13 @@ static int CclSetTitleScreens(lua_State *l) LuaError(l, "incorrect argument"); } const int subargs = lua_rawlen(l, -1); - TitleScreens[j]->Labels = new TitleScreenLabel *[subargs + 1]; - memset(TitleScreens[j]->Labels, 0, (subargs + 1) * sizeof(TitleScreenLabel *)); + TitleScreens[j]->Labels.resize(subargs); for (int k = 0; k < subargs; ++k) { lua_rawgeti(l, -1, k + 1); if (!lua_istable(l, -1)) { LuaError(l, "incorrect argument"); } - TitleScreens[j]->Labels[k] = new TitleScreenLabel; + TitleScreens[j]->Labels[k] = std::make_unique(); lua_pushnil(l); while (lua_next(l, -2)) { const std::string_view value = LuaToString(l, -2); diff --git a/src/ui/ui.cpp b/src/ui/ui.cpp index 7fe221bf9e..3ca91a4573 100644 --- a/src/ui/ui.cpp +++ b/src/ui/ui.cpp @@ -378,13 +378,7 @@ void CleanUserInterface() CGraphic::Free(UI.DefeatBackgroundG); // Title Screens - if (TitleScreens) { - for (int i = 0; TitleScreens[i]; ++i) { - delete TitleScreens[i]; - } - delete[] TitleScreens; - TitleScreens = nullptr; - } + TitleScreens.clear(); } void FreeButtonStyles() From 16894ce0ea8b1f3d70a75c878bd9554fd1163382 Mon Sep 17 00:00:00 2001 From: Jarod42 Date: Sat, 7 Oct 2023 11:03:23 +0200 Subject: [PATCH 3/3] Clean up ui/popup.h --- src/include/ui/popup.h | 102 +++++++++++++++++++---------------------- src/include/unittype.h | 2 +- src/spell/spells.cpp | 2 +- src/ui/botpanel.cpp | 12 ++--- src/ui/mainscr.cpp | 2 +- src/ui/popup.cpp | 49 ++++++-------------- src/unit/unittype.cpp | 7 ++- 7 files changed, 77 insertions(+), 99 deletions(-) diff --git a/src/include/ui/popup.h b/src/include/ui/popup.h index eba32fd0e1..0eae7de8f8 100644 --- a/src/include/ui/popup.h +++ b/src/include/ui/popup.h @@ -35,6 +35,7 @@ #include "color.h" #include "script.h" #include "vec2i.h" +#include "video.h" #include #include @@ -55,31 +56,24 @@ enum class ButtonCmd; class PopupConditionPanel { public: - PopupConditionPanel() : HasHint(false), HasDescription(false), HasDependencies(false), - ButtonAction(std::nullopt), BoolFlags(nullptr), Variables(nullptr) {} - ~PopupConditionPanel() - { - delete[] BoolFlags; - delete[] Variables; - } - - bool HasHint; /// check if button has hint. - bool HasDescription; /// check if button has description. - bool HasDependencies; /// check if button has dependencies or restrictions. - std::optional ButtonAction; /// action type of button + PopupConditionPanel() = default; + ~PopupConditionPanel() = default; + + bool HasHint = false; /// check if button has hint. + bool HasDescription = false; /// check if button has description. + bool HasDependencies = false; /// check if button has dependencies or restrictions. + std::optional ButtonAction; /// action type of button std::string ButtonValue; /// value used in ValueStr field of button - char *BoolFlags; /// array of condition about user flags. - char *Variables; /// array of variable to verify (enable and max > 0) + std::vector BoolFlags; /// array of condition about user flags. + std::vector Variables; /// array of variable to verify (enable and max > 0) }; class CPopupContentType { public: - CPopupContentType() : pos(0, 0), - MarginX(MARGIN_X), MarginY(MARGIN_Y), minSize(0, 0), - Wrap(true), Condition(nullptr) {} - virtual ~CPopupContentType() { delete Condition; } + CPopupContentType() = default; + virtual ~CPopupContentType() = default; /// Tell how show the variable Index. virtual void Draw(int x, int y, const CPopup &popup, const unsigned int popupWidth, const ButtonAction &button, int *Costs) const = 0; @@ -90,20 +84,20 @@ class CPopupContentType virtual void Parse(lua_State *l) = 0; - static CPopupContentType *ParsePopupContent(lua_State *l); + static std::unique_ptr ParsePopupContent(lua_State *l); public: - PixelPos pos; /// position to draw. + PixelPos pos{0, 0}; /// position to draw. - int MarginX; /// Left and right margin width. - int MarginY; /// Upper and lower margin height. - PixelSize minSize; /// Minimal size covered by content type. - bool Wrap; /// If true, the next content will be placed on the next "line". + int MarginX = MARGIN_X; /// Left and right margin width. + int MarginY = MARGIN_Y; /// Upper and lower margin height. + PixelSize minSize{0, 0}; /// Minimal size covered by content type. + bool Wrap = true; /// If true, the next content will be placed on the next "line". protected: std::string TextColor; /// Color used for plain text in content. std::string HighlightColor; /// Color used for highlighted letters. public: - PopupConditionPanel *Condition; /// Condition to show the content; if nullptr, no condition. + std::unique_ptr Condition; /// Condition to show the content; if nullptr, no condition. }; enum PopupButtonInfo_Types { @@ -115,8 +109,8 @@ enum PopupButtonInfo_Types { class CPopupContentTypeButtonInfo : public CPopupContentType { public: - CPopupContentTypeButtonInfo() : InfoType(0), MaxWidth(0), Font(nullptr) {} - virtual ~CPopupContentTypeButtonInfo() {} + CPopupContentTypeButtonInfo() = default; + virtual ~CPopupContentTypeButtonInfo() = default; virtual void Draw(int x, int y, const CPopup &popup, const unsigned int popupWidth, const ButtonAction &button, int *Costs) const; @@ -126,16 +120,16 @@ class CPopupContentTypeButtonInfo : public CPopupContentType virtual void Parse(lua_State *l); private: - int InfoType; /// Type of information to show. - unsigned int MaxWidth; /// Maximum width of multilined information. - CFont *Font; /// Font to use. + int InfoType = 0; /// Type of information to show. + unsigned int MaxWidth = 0; /// Maximum width of multilined information. + CFont *Font = nullptr; /// Font to use. }; class CPopupContentTypeText : public CPopupContentType { public: - CPopupContentTypeText() : MaxWidth(0), Font(nullptr) {} - virtual ~CPopupContentTypeText() {} + CPopupContentTypeText() = default; + virtual ~CPopupContentTypeText() = default; virtual void Draw(int x, int y, const CPopup &popup, const unsigned int popupWidth, const ButtonAction &button, int *Costs) const; @@ -145,16 +139,16 @@ class CPopupContentTypeText : public CPopupContentType virtual void Parse(lua_State *l); private: - std::string Text; /// Text to display - unsigned int MaxWidth; /// Maximum width of multilined text. - CFont *Font; /// Font to use. + std::string Text; /// Text to display + unsigned int MaxWidth = 0; /// Maximum width of multilined text. + CFont *Font = nullptr; /// Font to use. }; class CPopupContentTypeCosts : public CPopupContentType { public: - CPopupContentTypeCosts() : Font(nullptr), Centered(0) {} - virtual ~CPopupContentTypeCosts() {} + CPopupContentTypeCosts() = default; + virtual ~CPopupContentTypeCosts() = default; virtual void Draw(int x, int y, const CPopup &popup, const unsigned int popupWidth, const ButtonAction &button, int *Costs) const; @@ -164,15 +158,15 @@ class CPopupContentTypeCosts : public CPopupContentType virtual void Parse(lua_State *l); private: - CFont *Font; /// Font to use. - char Centered; /// if true, center the display. + CFont *Font = nullptr; /// Font to use. + bool Centered = false; /// if true, center the display. }; class CPopupContentTypeLine : public CPopupContentType { public: - CPopupContentTypeLine(); - virtual ~CPopupContentTypeLine() {} + CPopupContentTypeLine() = default; + virtual ~CPopupContentTypeLine() = default; virtual void Draw(int x, int y, const CPopup &popup, const unsigned int popupWidth, const ButtonAction &button, int *Costs) const; @@ -182,9 +176,9 @@ class CPopupContentTypeLine : public CPopupContentType virtual void Parse(lua_State *l); private: - IntColor Color; /// Color used for line. - unsigned int Width; /// line height - unsigned int Height; /// line height + IntColor Color = ColorWhite; /// Color used for line. + unsigned int Width = 0; /// line height + unsigned int Height = 1; /// line height }; class CPopupContentTypeVariable : public CPopupContentType @@ -209,18 +203,18 @@ class CPopupContentTypeVariable : public CPopupContentType class CPopup { public: - CPopup(); - ~CPopup(); + CPopup() = default; + ~CPopup() = default; - std::vector Contents; /// Array of contents to display. + std::vector> Contents; /// Array of contents to display. std::string Ident; /// Ident of the popup. - int MarginX; /// Left and right margin width. - int MarginY; /// Upper and lower margin height. - int MinWidth; /// Minimal width covered by popup. - int MinHeight; /// Minimal height covered by popup. - CFont *DefaultFont; /// Default font for content. - IntColor BackgroundColor; /// Color used for popup's background. - IntColor BorderColor; /// Color used for popup's borders. + int MarginX = MARGIN_X; /// Left and right margin width. + int MarginY = MARGIN_Y; /// Upper and lower margin height. + int MinWidth = 0; /// Minimal width covered by popup. + int MinHeight = 0; /// Minimal height covered by popup. + CFont *DefaultFont = nullptr; /// Default font for content. + IntColor BackgroundColor = ColorBlue; /// Color used for popup's background. + IntColor BorderColor = ColorWhite; /// Color used for popup's borders. }; diff --git a/src/include/unittype.h b/src/include/unittype.h index d30814a0a8..ddc56eb38f 100644 --- a/src/include/unittype.h +++ b/src/include/unittype.h @@ -562,7 +562,7 @@ class CUnitType Vec2i GetHalfTileSize() const { return Vec2i(TileWidth / 2, TileHeight / 2); } PixelSize GetPixelSize() const; - bool CheckUserBoolFlags(const char *BoolFlags) const; + bool CheckUserBoolFlags(const std::vector &BoolFlags) const; bool CanTransport() const { return MaxOnBoard > 0 && !GivesResource; } bool CanMove() const; diff --git a/src/spell/spells.cpp b/src/spell/spells.cpp index ae3ed301fe..c337cf0ddb 100644 --- a/src/spell/spells.cpp +++ b/src/spell/spells.cpp @@ -170,7 +170,7 @@ static bool PassCondition(const CUnit &caster, const SpellType &spell, const CUn return false; } } - if (target && !target->Type->CheckUserBoolFlags(condition->BoolFlag.data())) { + if (target && !target->Type->CheckUserBoolFlags(condition->BoolFlag)) { return false; } diff --git a/src/ui/botpanel.cpp b/src/ui/botpanel.cpp index becdb722e5..9e710c3196 100644 --- a/src/ui/botpanel.cpp +++ b/src/ui/botpanel.cpp @@ -338,11 +338,11 @@ static bool CanShowPopupContent(const PopupConditionPanel *condition, return false; } - if (type && condition->BoolFlags && !type->CheckUserBoolFlags(condition->BoolFlags)) { + if (type && !type->CheckUserBoolFlags(condition->BoolFlags)) { return false; } - if (condition->Variables && type) { + if (!condition->Variables.empty() && type) { for (unsigned int i = 0; i < UnitTypeVar.GetNumberVariable(); ++i) { if (condition->Variables[i] != CONDITION_TRUE) { if ((condition->Variables[i] == CONDITION_ONLY) ^ type->Stats[ThisPlayer->Index].Variables[i].Enable) { @@ -364,10 +364,10 @@ static void GetPopupSize(const CPopup &popup, const ButtonAction &button, popupWidth = popup.MarginX; popupHeight = popup.MarginY; - for (CPopupContentType *contentPtr : popup.Contents) { + for (auto &contentPtr : popup.Contents) { CPopupContentType &content = *contentPtr; - if (CanShowPopupContent(content.Condition, button, UnitTypes[button.Value])) { + if (CanShowPopupContent(content.Condition.get(), button, UnitTypes[button.Value])) { // Automatically write the calculated coordinates. content.pos.x = contentWidth + content.MarginX; content.pos.y = popupHeight + content.MarginY; @@ -576,10 +576,10 @@ void DrawPopup(const ButtonAction &button, const CUIButton &uibutton, int x, int Video.DrawRectangle(popup.BorderColor, x, y, popupWidth, popupHeight); // Contents - for (CPopupContentType *contentPtr : popup.Contents) { + for (auto &contentPtr : popup.Contents) { const CPopupContentType &content = *contentPtr; - if (CanShowPopupContent(content.Condition, button, UnitTypes[button.Value])) { + if (CanShowPopupContent(content.Condition.get(), button, UnitTypes[button.Value])) { content.Draw(x + content.pos.x, y + content.pos.y, popup, popupWidth, button, Costs); } } diff --git a/src/ui/mainscr.cpp b/src/ui/mainscr.cpp index b09f06c1f5..18832512ec 100644 --- a/src/ui/mainscr.cpp +++ b/src/ui/mainscr.cpp @@ -225,7 +225,7 @@ static bool CanShowContent(const ConditionPanel *condition, const CUnit &unit) || ((ThisPlayer->IsAllied(unit) || unit.Player == ThisPlayer) && condition->HideAllied)) { return false; } - if (!condition->BoolFlags.empty() && !unit.Type->CheckUserBoolFlags(condition->BoolFlags.data())) { + if (!unit.Type->CheckUserBoolFlags(condition->BoolFlags)) { return false; } if (!condition->Variables.empty()) { diff --git a/src/ui/popup.cpp b/src/ui/popup.cpp index 729369ed3c..6d32cd0754 100644 --- a/src/ui/popup.cpp +++ b/src/ui/popup.cpp @@ -330,11 +330,6 @@ } } -CPopupContentTypeLine::CPopupContentTypeLine() : Color(ColorWhite), Width(0), Height(1) -{ - -} - /* virtual */ int CPopupContentTypeLine::GetWidth(const ButtonAction &button, int *Costs) const { return this->Width; @@ -456,11 +451,11 @@ CPopupContentTypeLine::CPopupContentTypeLine() : Color(ColorWhite), Width(0), He ** ** @param l Lua State. */ -static PopupConditionPanel *ParsePopupConditions(lua_State *l) +static std::unique_ptr ParsePopupConditions(lua_State *l) { Assert(lua_istable(l, -1)); - PopupConditionPanel *condition = new PopupConditionPanel; + std::unique_ptr condition = std::make_unique(); for (lua_pushnil(l); lua_next(l, -2); lua_pop(l, 1)) { const std::string_view key = LuaToString(l, -2); @@ -522,20 +517,18 @@ static PopupConditionPanel *ParsePopupConditions(lua_State *l) } else { int index = UnitTypeVar.BoolFlagNameLookup[key]; if (index != -1) { - if (!condition->BoolFlags) { + if (condition->BoolFlags.empty()) { size_t new_bool_size = UnitTypeVar.GetNumberBoolFlag(); - condition->BoolFlags = new char[new_bool_size]; - memset(condition->BoolFlags, 0, new_bool_size * sizeof(char)); + condition->BoolFlags.resize(new_bool_size); } condition->BoolFlags[index] = Ccl2Condition(l, LuaToString(l, -1)); continue; } index = UnitTypeVar.VariableNameLookup[key]; if (index != -1) { - if (!condition->Variables) { + if (condition->Variables.empty()) { size_t new_variables_size = UnitTypeVar.GetNumberVariable(); - condition->Variables = new char[new_variables_size]; - memset(condition->Variables, 0, new_variables_size * sizeof(char)); + condition->Variables.resize(new_variables_size); } condition->Variables[index] = Ccl2Condition(l, LuaToString(l, -1)); continue; @@ -546,7 +539,7 @@ static PopupConditionPanel *ParsePopupConditions(lua_State *l) return condition; } -/* static */ CPopupContentType *CPopupContentType::ParsePopupContent(lua_State *l) +/* static */ std::unique_ptr CPopupContentType::ParsePopupContent(lua_State *l) { Assert(lua_istable(l, -1)); @@ -557,8 +550,8 @@ static PopupConditionPanel *ParsePopupConditions(lua_State *l) int minHeight = 0; std::string textColor("white"); std::string highColor("red"); - CPopupContentType *content = nullptr; - PopupConditionPanel *condition = nullptr; + std::unique_ptr content; + std::unique_ptr condition; for (lua_pushnil(l); lua_next(l, -2); lua_pop(l, 1)) { std::string_view key = LuaToString(l, -2); @@ -580,15 +573,15 @@ static PopupConditionPanel *ParsePopupConditions(lua_State *l) key = LuaToString(l, -1, 1); // Method name lua_rawgeti(l, -1, 2); // Method data if (key == "ButtonInfo") { - content = new CPopupContentTypeButtonInfo; + content = std::make_unique(); } else if (key == "Text") { - content = new CPopupContentTypeText; + content = std::make_unique(); } else if (key == "Costs") { - content = new CPopupContentTypeCosts; + content = std::make_unique(); } else if (key == "Line") { - content = new CPopupContentTypeLine; + content = std::make_unique(); } else if (key == "Variable") { - content = new CPopupContentTypeVariable; + content = std::make_unique(); } else { LuaError(l, "Invalid drawing method '%s' in DefinePopups", key.data()); } @@ -605,22 +598,10 @@ static PopupConditionPanel *ParsePopupConditions(lua_State *l) content->MarginY = marginY; content->minSize.x = minWidth; content->minSize.y = minHeight; - content->Condition = condition; + content->Condition = std::move(condition); content->TextColor = textColor; content->HighlightColor = highColor; return content; } -CPopup::CPopup() : - Contents(), MarginX(MARGIN_X), MarginY(MARGIN_Y), MinWidth(0), MinHeight(0), - DefaultFont(nullptr), BackgroundColor(ColorBlue), BorderColor(ColorWhite) -{} - -CPopup::~CPopup() -{ - for (CPopupContentType *content : Contents) { - delete content; - } -} - //@} diff --git a/src/unit/unittype.cpp b/src/unit/unittype.cpp index 3cf9a3b535..5beb1f686e 100644 --- a/src/unit/unittype.cpp +++ b/src/unit/unittype.cpp @@ -559,11 +559,14 @@ PixelSize CUnitType::GetPixelSize() const return PixelSize(TileWidth * PixelTileSize.x, TileHeight * PixelTileSize.y); } -bool CUnitType::CheckUserBoolFlags(const char *BoolFlags) const +bool CUnitType::CheckUserBoolFlags(const std::vector &BoolFlags) const { + if (BoolFlags.empty()) { + return true; + } for (unsigned int i = 0; i < UnitTypeVar.GetNumberBoolFlag(); ++i) { // User defined flags if (BoolFlags[i] != CONDITION_TRUE && - ((BoolFlags[i] == CONDITION_ONLY) ^ (BoolFlag[i].value))) { + ((BoolFlags[i] == CONDITION_ONLY) ^ (this->BoolFlag[i].value))) { return false; } }