Sample properties when shift is held

Rather than when the active tool is the property tool. Solves the problem of not being able to sample "away" from the property tool when it's already selected. Also bring up the property window after each such sampling of properties.
This commit is contained in:
Tamás Bálint Misius 2023-12-26 13:17:21 +01:00
parent c5b72b213b
commit d84e0a0c3e
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
6 changed files with 83 additions and 52 deletions

View File

@ -396,6 +396,10 @@ void GameController::DrawPoints(int toolSelection, ui::Point oldPos, ui::Point n
}
activeTool->Strength = gameModel->GetToolStrength();
// This is a joke, the game mvc has to go >_>
activeTool->shiftBehaviour = gameView->ShiftBehaviour();
activeTool->ctrlBehaviour = gameView->CtrlBehaviour();
activeTool->altBehaviour = gameView->AltBehaviour();
if (!held)
activeTool->Draw(sim, cBrush, newPos);
else
@ -1105,8 +1109,10 @@ void GameController::SetActiveTool(int toolSelection, Tool * tool)
if(gameModel->GetActiveTool(i) == gameModel->GetMenuList().at(SC_WALL)->GetToolList().at(WL_GRAV))
gameModel->GetRenderer()->gravityZonesEnabled = true;
}
if(tool->Identifier == "DEFAULT_UI_PROPERTY")
((PropertyTool *)tool)->OpenWindow(gameModel->GetSimulation());
if (tool->Identifier == "DEFAULT_UI_PROPERTY")
{
static_cast<PropertyTool *>(tool)->OpenWindow(gameModel->GetSimulation(), nullptr);
}
if(tool->Identifier == "DEFAULT_UI_ADDLIFE")
{
((GOLTool *)tool)->OpenWindow(gameModel->GetSimulation(), toolSelection);
@ -1126,6 +1132,11 @@ void GameController::SetLastTool(Tool * tool)
gameModel->SetLastTool(tool);
}
Tool *GameController::GetLastTool()
{
return gameModel->GetLastTool();
}
int GameController::GetReplaceModeFlags()
{
return gameModel->GetSimulation()->replaceModeFlags;

View File

@ -133,6 +133,7 @@ public:
void SetActiveTool(int toolSelection, Tool * tool);
void SetActiveTool(int toolSelection, ByteString identifier);
void SetLastTool(Tool * tool);
Tool *GetLastTool();
int GetReplaceModeFlags();
void SetReplaceModeFlags(int flags);
void SetActiveColourPreset(int preset);

View File

@ -2086,6 +2086,11 @@ void GameView::UpdateDrawMode()
drawMode = DrawLine;
else
drawMode = DrawPoints;
// TODO: have tools decide on draw mode
if (c->GetLastTool() && c->GetLastTool()->Identifier == "DEFAULT_UI_SAMPLE")
{
drawMode = DrawPoints;
}
}
void GameView::UpdateToolStrength()

View File

@ -28,6 +28,7 @@
class PropertyWindow: public ui::Window
{
void HandlePropertyChange();
std::optional<std::pair<int, String>> TakePropertyFrom(const Particle *takePropertyFrom) const;
public:
ui::DropDown * property;
@ -36,7 +37,7 @@ public:
Simulation *sim;
std::vector<StructProperty> properties;
std::optional<PropertyTool::Configuration> configuration;
PropertyWindow(PropertyTool *tool_, Simulation *sim);
PropertyWindow(PropertyTool *tool_, Simulation *sim, const Particle *takePropertyFrom);
void SetProperty();
void CheckProperty();
void Update();
@ -46,7 +47,7 @@ public:
virtual ~PropertyWindow() {}
};
PropertyWindow::PropertyWindow(PropertyTool * tool_, Simulation *sim_):
PropertyWindow::PropertyWindow(PropertyTool * tool_, Simulation *sim_, const Particle *takePropertyFrom):
ui::Window(ui::Point(-1, -1), ui::Point(200, 87)),
tool(tool_),
sim(sim_)
@ -81,23 +82,67 @@ sim(sim_)
property->AddOption(std::pair<String, int>(properties[i].Name.FromAscii(), i));
}
auto &prefs = GlobalPrefs::Ref();
property->SetOption(prefs.Get("Prop.Type", 0));
textField = new ui::Textbox(ui::Point(8, 46), ui::Point(Size.X-16, 16), "", "[value]");
textField->Appearance.HorizontalAlign = ui::Appearance::AlignLeft;
textField->Appearance.VerticalAlign = ui::Appearance::AlignMiddle;
textField->SetText(prefs.Get("Prop.Value", String("")));
textField->SetActionCallback({ [this]() {
Update();
} });
AddComponent(textField);
{
auto &prefs = GlobalPrefs::Ref();
auto propertyIndex = prefs.Get("Prop.Type", 0);
auto valueString = prefs.Get("Prop.Value", String(""));
auto taken = TakePropertyFrom(takePropertyFrom);
if (taken)
{
std::tie(propertyIndex, valueString) = *taken;
}
property->SetOption(propertyIndex);
textField->SetText(valueString);
}
FocusComponent(textField);
Update();
MakeActiveWindow();
}
std::optional<std::pair<int, String>> PropertyWindow::TakePropertyFrom(const Particle *takePropertyFrom) const
{
auto toolConfiguration = tool->GetConfiguration();
if (!toolConfiguration || !takePropertyFrom)
{
return {};
}
auto *prop = reinterpret_cast<const char *>(takePropertyFrom) + toolConfiguration->prop.Offset;
auto takeValue = [this, toolConfiguration](auto &value) -> std::optional<std::pair<int, String>> {
auto it = std::find(properties.begin(), properties.end(), toolConfiguration->prop);
if (it != properties.end())
{
return std::pair{ int(it - properties.begin()), String::Build(value) };
}
return {};
};
switch (toolConfiguration->prop.Type)
{
case StructProperty::Float:
return takeValue(*reinterpret_cast<const float *>(prop));
case StructProperty::ParticleType:
case StructProperty::Integer:
return takeValue(*reinterpret_cast<const int *>(prop));
case StructProperty::UInteger:
return takeValue(*reinterpret_cast<const unsigned int *>(prop));
default:
break;
}
return {};
}
void PropertyWindow::HandlePropertyChange()
{
FocusComponent(textField);
@ -287,9 +332,9 @@ void PropertyWindow::OnKeyPress(int key, int scan, bool repeat, bool shift, bool
}
}
void PropertyTool::OpenWindow(Simulation *sim)
void PropertyTool::OpenWindow(Simulation *sim, const Particle *takePropertyFrom)
{
new PropertyWindow(this, sim);
new PropertyWindow(this, sim, takePropertyFrom);
}
void PropertyTool::SetProperty(Simulation *sim, ui::Point position)
@ -325,39 +370,6 @@ void PropertyTool::SetProperty(Simulation *sim, ui::Point position)
}
}
void PropertyTool::UpdateConfigurationFromParticle(const Particle &part)
{
auto configuration = GetConfiguration();
switch (configuration->prop.Type)
{
case StructProperty::Float:
{
auto value = *((float*)(((char*)&part)+configuration->prop.Offset));;
configuration->propValue = value;
configuration->propertyValueStr = String::Build(value);
}
break;
case StructProperty::ParticleType:
case StructProperty::Integer:
{
auto value = *((int*)(((char*)&part)+configuration->prop.Offset));;
configuration->propValue = value;
configuration->propertyValueStr = String::Build(value);
}
break;
case StructProperty::UInteger:
{
auto value = *((unsigned int*)(((char*)&part)+configuration->prop.Offset));;
configuration->propValue = value;
configuration->propertyValueStr = String::Build(value);
}
break;
default:
break;
}
SetConfiguration(configuration);
}
void PropertyTool::Draw(Simulation *sim, Brush const &cBrush, ui::Point position)
{
for (ui::Point off : cBrush)

View File

@ -40,11 +40,11 @@ void SampleTool::Draw(Simulation * sim, Brush const &brush, ui::Point position)
}
if (part)
{
auto *propTool = static_cast<PropertyTool *>(gameModel.GetToolFromIdentifier("DEFAULT_UI_PROPERTY"));
if (gameModel.GetActiveTool(0) == propTool && propTool->GetConfiguration())
if (shiftBehaviour)
{
propTool->UpdateConfigurationFromParticle(*part);
gameModel.SetActiveTool(0, propTool); // trigger change so Renderer::findingElement is updated
auto *propTool = static_cast<PropertyTool *>(gameModel.GetToolFromIdentifier("DEFAULT_UI_PROPERTY"));
gameModel.SetActiveTool(0, propTool);
propTool->OpenWindow(gameModel.GetSimulation(), part);
}
else if (part->type == PT_LIFE)
{

View File

@ -25,6 +25,9 @@ public:
RGB<uint8_t> const Colour;
bool const Blocky;
float Strength = 1.0f;
bool shiftBehaviour = false;
bool ctrlBehaviour = false;
bool altBehaviour = false;
Tool(int id, String name, String description,
RGB<uint8_t> colour, ByteString identifier, std::unique_ptr<VideoBuffer> (*textureGen)(int, Vec2<int>) = NULL, bool blocky = false
@ -110,6 +113,9 @@ public:
};
private:
void SetProperty(Simulation *sim, ui::Point position);
void SetConfiguration(std::optional<Configuration> newConfiguration);
GameModel &gameModel;
std::optional<Configuration> configuration;
@ -126,17 +132,13 @@ public:
virtual ~PropertyTool()
{}
void OpenWindow(Simulation *sim);
void SetProperty(Simulation *sim, ui::Point position);
void UpdateConfigurationFromParticle(const Particle &part);
void OpenWindow(Simulation *sim, const Particle *takePropertyFrom);
void Click(Simulation * sim, Brush const &brush, ui::Point position) override { }
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 = false) 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 SetConfiguration(std::optional<Configuration> newConfiguration);
std::optional<Configuration> GetConfiguration() const
{
return configuration;