diff --git a/src/gui/game/GameModel.cpp b/src/gui/game/GameModel.cpp index b49b6c9bb..83094d418 100644 --- a/src/gui/game/GameModel.cpp +++ b/src/gui/game/GameModel.cpp @@ -1692,7 +1692,9 @@ std::optional GameModel::GetToolIndex(Tool *tool) void GameModel::AllocCustomGolTool(const CustomGOLData &gd) { - AllocTool(std::make_unique(PMAP(gd.rule, PT_LIFE), gd.nameString, "Custom GOL type: " + SerialiseGOLRule(gd.rule), gd.colour1, "DEFAULT_PT_LIFECUST_" + gd.nameString.ToAscii(), nullptr)); + auto tool = std::make_unique(PMAP(gd.rule, PT_LIFE), gd.nameString, "Custom GOL type: " + SerialiseGOLRule(gd.rule), gd.colour1, "DEFAULT_PT_LIFECUST_" + gd.nameString.ToAscii(), nullptr); + tool->MenuSection = SC_LIFE; + AllocTool(std::move(tool)); } void GameModel::UpdateElementTool(int element) @@ -1705,6 +1707,8 @@ void GameModel::UpdateElementTool(int element) tool->Description = elem.Description; tool->Colour = elem.Colour; tool->textureGen = elem.IconGenerator; + tool->MenuSection = elem.MenuSection; + tool->MenuVisible = elem.MenuVisible; } void GameModel::AllocElementTool(int element) @@ -1749,11 +1753,15 @@ void GameModel::InitTools() } for (int i = 0; i < NGOL; ++i) { - AllocTool(std::make_unique(PMAP(i, PT_LIFE), builtinGol[i].name, builtinGol[i].description, builtinGol[i].colour, "DEFAULT_PT_LIFE_" + builtinGol[i].name.ToAscii())); + auto tool = std::make_unique(PMAP(i, PT_LIFE), builtinGol[i].name, builtinGol[i].description, builtinGol[i].colour, "DEFAULT_PT_LIFE_" + builtinGol[i].name.ToAscii()); + tool->MenuSection = SC_LIFE; + AllocTool(std::move(tool)); } for (int i = 0; i < UI_WALLCOUNT; ++i) { - AllocTool(std::make_unique(i, sd.wtypes[i].descs, sd.wtypes[i].colour, sd.wtypes[i].identifier, sd.wtypes[i].textureGen)); + auto tool = std::make_unique(i, sd.wtypes[i].descs, sd.wtypes[i].colour, sd.wtypes[i].identifier, sd.wtypes[i].textureGen); + tool->MenuSection = SC_WALL; + AllocTool(std::move(tool)); } for (auto &tool : ::GetTools()) { @@ -1780,7 +1788,6 @@ void GameModel::InitTools() void GameModel::BuildMenus() { auto &sd = SimulationData::Ref(); - auto &elements = sd.elements; menuList.clear(); for (auto §ion : sd.msections) @@ -1788,39 +1795,15 @@ void GameModel::BuildMenus() menuList.push_back(std::make_unique(section.icon, section.name, section.doshow)); } - for (auto &elem : elements) + for (auto &tool : tools) { - if (elem.Enabled) - { - if (elem.MenuSection >= 0 && elem.MenuSection < int(sd.msections.size()) && elem.MenuVisible) - { - menuList[elem.MenuSection]->AddTool(GetToolFromIdentifier(elem.Identifier)); - } - } - } - - for (auto &ptr : tools) - { - if (!ptr) + if (!tool) { continue; } - if (ptr->Identifier.BeginsWith("DEFAULT_PT_LIFE_") || - ptr->Identifier.BeginsWith("DEFAULT_PT_LIFECUST_")) + if (tool->MenuSection >= 0 && tool->MenuSection < int(sd.msections.size()) && tool->MenuVisible) { - menuList[SC_LIFE]->AddTool(ptr.get()); - } - if (ptr->Identifier.BeginsWith("DEFAULT_WL_")) - { - menuList[SC_WALL]->AddTool(ptr.get()); - } - if (ptr->Identifier.Contains("_TOOL_")) - { - menuList[SC_TOOL]->AddTool(ptr.get()); - } - if (ptr->Identifier.BeginsWith("DEFAULT_DECOR_")) - { - menuList[SC_DECO]->AddTool(ptr.get()); + menuList[tool->MenuSection]->AddTool(tool.get()); } } @@ -1832,11 +1815,6 @@ void GameModel::BuildMenus() } } - menuList[SC_TOOL]->AddTool(GetToolFromIdentifier("DEFAULT_UI_PROPERTY")); - menuList[SC_TOOL]->AddTool(GetToolFromIdentifier("DEFAULT_UI_SIGN")); - menuList[SC_TOOL]->AddTool(GetToolFromIdentifier("DEFAULT_UI_SAMPLE")); - menuList[SC_LIFE]->AddTool(GetToolFromIdentifier("DEFAULT_UI_ADDLIFE")); - notifyMenuListChanged(); notifyActiveMenuToolListChanged(); notifyActiveToolsChanged(); diff --git a/src/gui/game/tool/DecorationTool.h b/src/gui/game/tool/DecorationTool.h index 7e789ef07..13abd29ec 100644 --- a/src/gui/game/tool/DecorationTool.h +++ b/src/gui/game/tool/DecorationTool.h @@ -18,7 +18,9 @@ public: Tool(decoMode, name, description, colour, identifier), Colour(0x000000_rgb .WithAlpha(0x00)), gameView(newGameView) - {} + { + MenuSection = SC_DECO; + } void Draw(Simulation * sim, Brush const &brush, ui::Point position) override; void DrawLine(Simulation * sim, Brush const &brush, ui::Point position1, ui::Point position2, bool dragging) override; diff --git a/src/gui/game/tool/GOLTool.h b/src/gui/game/tool/GOLTool.h index 5401ce64c..d1a5f135c 100644 --- a/src/gui/game/tool/GOLTool.h +++ b/src/gui/game/tool/GOLTool.h @@ -12,7 +12,9 @@ public: 0xFEA900_rgb, "DEFAULT_UI_ADDLIFE", NULL ), gameModel(gameModel) - {} + { + MenuSection = SC_LIFE; + } void OpenWindow(Simulation *sim, int toolSelection, int rule = 0, RGB colour1 = 0x000000_rgb, RGB colour2 = 0x000000_rgb); void Click(Simulation * sim, Brush const &brush, ui::Point position) override { } diff --git a/src/gui/game/tool/Tool.h b/src/gui/game/tool/Tool.h index f0e6cda4d..adb09f131 100644 --- a/src/gui/game/tool/Tool.h +++ b/src/gui/game/tool/Tool.h @@ -5,6 +5,7 @@ #include "graphics/VideoBuffer.h" #include "gui/interface/Point.h" #include "simulation/StructProperty.h" +#include "simulation/MenuSection.h" #include #include @@ -27,6 +28,8 @@ public: bool shiftBehaviour = false; bool ctrlBehaviour = false; bool altBehaviour = false; + int MenuSection = SC_TOOL; + bool MenuVisible = true; Tool() = default; diff --git a/src/lua/LuaTools.cpp b/src/lua/LuaTools.cpp index d3af27890..68880bb5b 100644 --- a/src/lua/LuaTools.cpp +++ b/src/lua/LuaTools.cpp @@ -306,7 +306,7 @@ static int property(lua_State *L) return 0; } int returnValueCount = 0; - auto handleProperty = [L, tool, &propertyName, &returnValueCount](auto simToolMember, const char *luaPropertyName) { + auto handleProperty = [L, lsi, tool, &propertyName, &returnValueCount](auto simToolMember, const char *luaPropertyName, bool buildMenusIfChanged) { if (propertyName == luaPropertyName) { auto &thing = tool->*simToolMember; @@ -314,12 +314,20 @@ static int property(lua_State *L) if (lua_gettop(L) > 2) { if constexpr (std::is_same_v) thing = tpt_lua_checkString(L, 3); + else if constexpr (std::is_same_v) thing = lua_toboolean(L, 3); + else if constexpr (std::is_same_v) thing = luaL_checkinteger(L, 3); else if constexpr (std::is_same_v>) thing = RGB::Unpack(luaL_checkinteger(L, 3)); else static_assert(DependentFalse::value); + if (buildMenusIfChanged) + { + lsi->gameModel->BuildMenus(); + } } else { if constexpr (std::is_same_v) tpt_lua_pushString(L, thing); + else if constexpr (std::is_same_v) lua_pushboolean(L, thing); + else if constexpr (std::is_same_v) lua_pushinteger(L, thing); else if constexpr (std::is_same_v>) lua_pushinteger(L, thing.Pack()); else static_assert(DependentFalse::value); returnValueCount = 1; @@ -328,10 +336,12 @@ static int property(lua_State *L) } return false; }; - if (handleProperty(&SimTool::Name , "Name" ) || - handleProperty(&SimTool::Description, "Description") || - handleProperty(&SimTool::Colour , "Colour" ) || - handleProperty(&SimTool::Colour , "Color" )) + if (handleProperty(&SimTool::Name , "Name" , false) || + handleProperty(&SimTool::Description, "Description", false) || + handleProperty(&SimTool::Colour , "Colour" , false) || + handleProperty(&SimTool::Colour , "Color" , false) || + handleProperty(&SimTool::MenuSection, "MenuSection", true) || + handleProperty(&SimTool::MenuVisible, "MenuVisible", true)) { return returnValueCount; } diff --git a/src/simulation/MenuSection.h b/src/simulation/MenuSection.h index 03bef9844..1bc70bf33 100644 --- a/src/simulation/MenuSection.h +++ b/src/simulation/MenuSection.h @@ -8,3 +8,20 @@ struct menu_section int itemcount; int doshow; }; + +constexpr int SC_WALL = 0; +constexpr int SC_ELEC = 1; +constexpr int SC_POWERED = 2; +constexpr int SC_SENSOR = 3; +constexpr int SC_FORCE = 4; +constexpr int SC_EXPLOSIVE = 5; +constexpr int SC_GAS = 6; +constexpr int SC_LIQUID = 7; +constexpr int SC_POWDERS = 8; +constexpr int SC_SOLIDS = 9; +constexpr int SC_NUCLEAR = 10; +constexpr int SC_SPECIAL = 11; +constexpr int SC_LIFE = 12; +constexpr int SC_TOOL = 13; +constexpr int SC_FAVORITES = 14; +constexpr int SC_DECO = 15; diff --git a/src/simulation/SimulationData.h b/src/simulation/SimulationData.h index 3e9f0129a..adabe17a7 100644 --- a/src/simulation/SimulationData.h +++ b/src/simulation/SimulationData.h @@ -15,23 +15,6 @@ #include #include -constexpr int SC_WALL = 0; -constexpr int SC_ELEC = 1; -constexpr int SC_POWERED = 2; -constexpr int SC_SENSOR = 3; -constexpr int SC_FORCE = 4; -constexpr int SC_EXPLOSIVE = 5; -constexpr int SC_GAS = 6; -constexpr int SC_LIQUID = 7; -constexpr int SC_POWDERS = 8; -constexpr int SC_SOLIDS = 9; -constexpr int SC_NUCLEAR = 10; -constexpr int SC_SPECIAL = 11; -constexpr int SC_LIFE = 12; -constexpr int SC_TOOL = 13; -constexpr int SC_FAVORITES = 14; -constexpr int SC_DECO = 15; - constexpr int O_WL_WALLELEC = 122; constexpr int O_WL_EWALL = 123; constexpr int O_WL_DETECT = 124;