Add Select callback to Lua tools

This is intended to support selection-time configuration the way PROP and the custom life tools do it. Indeed, they now use the infrastructure this commit adds to implement their own configuration actions.
This commit is contained in:
Tamás Bálint Misius 2024-11-20 19:02:05 +01:00
parent eaef47074b
commit afb9fa7df2
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
11 changed files with 56 additions and 14 deletions

View File

@ -1121,14 +1121,7 @@ void GameController::SetActiveTool(int toolSelection, Tool * tool)
gameModel->GetRendererSettings().gravityZonesEnabled = true;
}
}
if (tool->Identifier == "DEFAULT_UI_PROPERTY")
{
static_cast<PropertyTool *>(tool)->OpenWindow(gameModel->GetSimulation(), std::nullopt);
}
if(tool->Identifier == "DEFAULT_UI_ADDLIFE")
{
static_cast<GOLTool *>(tool)->OpenWindow(gameModel->GetSimulation(), toolSelection);
}
tool->Select(toolSelection);
}
void GameController::SetActiveTool(int toolSelection, ByteString identifier)

View File

@ -1775,7 +1775,7 @@ void GameModel::InitTools()
AllocTool(std::make_unique<DecorationTool>(view, DECO_SMUDGE , "SMDG", "Smudge tool, blends surrounding deco together.", 0x000000_rgb, "DEFAULT_DECOR_SMDG"));
AllocTool(std::make_unique<DecorationTool>(view, DECO_CLEAR , "CLR" , "Erase any set decoration." , 0x000000_rgb, "DEFAULT_DECOR_CLR" ));
AllocTool(std::make_unique<DecorationTool>(view, DECO_DRAW , "SET" , "Draw decoration (No blending)." , 0x000000_rgb, "DEFAULT_DECOR_SET" ));
AllocTool(std::make_unique<PropertyTool>());
AllocTool(std::make_unique<PropertyTool>(*this));
AllocTool(std::make_unique<SignTool>(*this));
AllocTool(std::make_unique<SampleTool>(*this));
AllocTool(std::make_unique<GOLTool>(*this));

View File

@ -200,3 +200,8 @@ void GOLTool::OpenWindow(Simulation *sim, int toolSelection, int rule, RGB<uint8
{
new GOLWindow(gameModel, toolSelection, rule, colour1, colour2);
}
void GOLTool::Select(int toolSelection)
{
OpenWindow(gameModel.GetSimulation(), toolSelection);
}

View File

@ -22,4 +22,6 @@ public:
void DrawLine(Simulation * sim, Brush const &brush, ui::Point position1, ui::Point position2, bool dragging) override { };
void DrawRect(Simulation * sim, Brush const &brush, ui::Point position1, ui::Point position2) override { };
void DrawFill(Simulation * sim, Brush const &brush, ui::Point position) override { };
void Select(int toolSelection) final override;
};

View File

@ -2,6 +2,7 @@
#include "prefs/GlobalPrefs.h"
#include "gui/Style.h"
#include "gui/game/Brush.h"
#include "gui/game/GameModel.h"
#include "gui/interface/Window.h"
#include "gui/interface/Button.h"
#include "gui/interface/Textbox.h"
@ -280,3 +281,8 @@ void PropertyTool::DrawFill(Simulation *sim, Brush const &cBrush, ui::Point posi
if (configuration)
sim->flood_prop(position.X, position.Y, configuration->changeProperty);
}
void PropertyTool::Select(int toolSelection)
{
OpenWindow(gameModel.GetSimulation(), std::nullopt);
}

View File

@ -18,15 +18,16 @@ private:
void SetProperty(Simulation *sim, ui::Point position);
void SetConfiguration(std::optional<Configuration> newConfiguration);
GameModel &gameModel;
std::optional<Configuration> configuration;
friend class PropertyWindow;
public:
PropertyTool():
PropertyTool(GameModel &newGameModel):
Tool(0, "PROP", "Property Drawing Tool. Use to alter the properties of elements in the field.",
0xFEA900_rgb, "DEFAULT_UI_PROPERTY", NULL
)
), gameModel(newGameModel)
{}
void OpenWindow(Simulation *sim, std::optional<int> takePropertyFrom);
@ -40,4 +41,6 @@ public:
{
return configuration;
}
void Select(int toolSelection) final override;
};

View File

@ -61,6 +61,8 @@ public:
virtual void Drag(Simulation *sim, const Brush &brush, ui::Point position1, ui::Point position2)
{
}
virtual void Select(int toolSelection)
{
}
};

View File

@ -67,6 +67,7 @@ struct CustomTool
LuaSmartRef drawLine;
LuaSmartRef drawRect;
LuaSmartRef drawFill;
LuaSmartRef select;
};
class LuaScriptInterface : public CommandInterface

View File

@ -250,6 +250,24 @@ static void luaDrawFillWrapper(SimTool *tool, Simulation *sim, const Brush &brus
}
}
static void luaSelectWrapper(SimTool *tool, int toolSelection)
{
auto *lsi = GetLSI();
auto L = lsi->L;
auto index = *lsi->gameModel->GetToolIndex(tool);
auto &customTools = lsi->customTools;
if (customTools[index].select)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, customTools[index].select);
lua_pushinteger(L, toolSelection);
if (tpt_lua_pcall(L, 1, 0, 0, eventTraitNone))
{
lsi->Log(CommandInterface::LogError, "In select func: " + LuaGetError());
lua_pop(L, 1);
}
}
}
template <typename T>
struct DependentFalse : std::false_type
{
@ -301,7 +319,8 @@ static int property(lua_State *L)
handleCallback(&CustomTool::draw , &SimTool::PerformDraw , luaDrawWrapper , "Draw" ) ||
handleCallback(&CustomTool::drawLine, &SimTool::PerformDrawLine, luaDrawLineWrapper, "DrawLine") ||
handleCallback(&CustomTool::drawRect, &SimTool::PerformDrawRect, luaDrawRectWrapper, "DrawRect") ||
handleCallback(&CustomTool::drawFill, &SimTool::PerformDrawFill, luaDrawFillWrapper, "DrawFill"))
handleCallback(&CustomTool::drawFill, &SimTool::PerformDrawFill, luaDrawFillWrapper, "DrawFill") ||
handleCallback(&CustomTool::select , &SimTool::PerformSelect , luaSelectWrapper , "Select" ))
{
return 0;
}

View File

@ -104,6 +104,7 @@ SimTool::SimTool()
PerformDrawLine = defaultPerformDrawLine;
PerformDrawRect = defaultPerformDrawRect;
PerformDrawFill = nullptr;
PerformSelect = nullptr;
}
void SimTool::Click(Simulation * sim, const Brush &brush, ui::Point position)
@ -153,3 +154,11 @@ void SimTool::DrawFill(Simulation * sim, const Brush &brush, ui::Point position)
PerformDrawFill(this, sim, brush, position);
}
}
void SimTool::Select(int toolSelection)
{
if (PerformSelect)
{
PerformSelect(this, toolSelection);
}
}

View File

@ -15,6 +15,7 @@ public:
void (*PerformDrawLine)(SimTool *tool, Simulation *sim, const Brush &brush, ui::Point position1, ui::Point position2, bool dragging);
void (*PerformDrawRect)(SimTool *tool, Simulation *sim, const Brush &brush, ui::Point position1, ui::Point position2);
void (*PerformDrawFill)(SimTool *tool, Simulation *sim, const Brush &brush, ui::Point position);
void (*PerformSelect )(SimTool *tool, int toolSelection);
#define TOOL_NUMBERS_DECLARE
@ -30,4 +31,5 @@ public:
void DrawLine(Simulation *sim, const Brush &brush, ui::Point position1, ui::Point position2, bool dragging) override;
void DrawRect(Simulation *sim, const Brush &brush, ui::Point position1, ui::Point position2) override;
void DrawFill(Simulation * sim, Brush const &brush, ui::Point position) override;
void Select(int toolSelection) override;
};