Add MenuSection and MenuVisible properties to Tool

This makes the task of deciding which tool goes in which menu easier (GameModel::BuildMenus). Due to changes in the order tools are added to menus, the sign/sample/prop tool triplet now comes before custom tools, and the custom life creation tool comes before custom life element tools, which I think is fine.
This commit is contained in:
Tamás Bálint Misius 2024-10-04 12:30:25 +02:00
parent b26d057783
commit 9c3cd69cef
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
7 changed files with 56 additions and 61 deletions

View File

@ -1692,7 +1692,9 @@ std::optional<int> GameModel::GetToolIndex(Tool *tool)
void GameModel::AllocCustomGolTool(const CustomGOLData &gd)
{
AllocTool(std::make_unique<ElementTool>(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<ElementTool>(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<ElementTool>(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<ElementTool>(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<WallTool>(i, sd.wtypes[i].descs, sd.wtypes[i].colour, sd.wtypes[i].identifier, sd.wtypes[i].textureGen));
auto tool = std::make_unique<WallTool>(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 &section : sd.msections)
@ -1788,39 +1795,15 @@ void GameModel::BuildMenus()
menuList.push_back(std::make_unique<Menu>(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();

View File

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

View File

@ -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<uint8_t> colour1 = 0x000000_rgb, RGB<uint8_t> colour2 = 0x000000_rgb);
void Click(Simulation * sim, Brush const &brush, ui::Point position) override { }

View File

@ -5,6 +5,7 @@
#include "graphics/VideoBuffer.h"
#include "gui/interface/Point.h"
#include "simulation/StructProperty.h"
#include "simulation/MenuSection.h"
#include <memory>
#include <optional>
@ -27,6 +28,8 @@ public:
bool shiftBehaviour = false;
bool ctrlBehaviour = false;
bool altBehaviour = false;
int MenuSection = SC_TOOL;
bool MenuVisible = true;
Tool() = default;

View File

@ -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<PropertyType, String >) thing = tpt_lua_checkString(L, 3);
else if constexpr (std::is_same_v<PropertyType, bool >) thing = lua_toboolean(L, 3);
else if constexpr (std::is_same_v<PropertyType, int >) thing = luaL_checkinteger(L, 3);
else if constexpr (std::is_same_v<PropertyType, RGB<uint8_t>>) thing = RGB<uint8_t>::Unpack(luaL_checkinteger(L, 3));
else static_assert(DependentFalse<PropertyType>::value);
if (buildMenusIfChanged)
{
lsi->gameModel->BuildMenus();
}
}
else
{
if constexpr (std::is_same_v<PropertyType, String >) tpt_lua_pushString(L, thing);
else if constexpr (std::is_same_v<PropertyType, bool >) lua_pushboolean(L, thing);
else if constexpr (std::is_same_v<PropertyType, int >) lua_pushinteger(L, thing);
else if constexpr (std::is_same_v<PropertyType, RGB<uint8_t>>) lua_pushinteger(L, thing.Pack());
else static_assert(DependentFalse<PropertyType>::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;
}

View File

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

View File

@ -15,23 +15,6 @@
#include <array>
#include <shared_mutex>
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;