From d84e0a0c3e876bea6ac0d2614e22ee16c2032085 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Tue, 26 Dec 2023 13:17:21 +0100 Subject: [PATCH] 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. --- src/gui/game/GameController.cpp | 15 +++++- src/gui/game/GameController.h | 1 + src/gui/game/GameView.cpp | 5 ++ src/gui/game/PropertyTool.cpp | 94 +++++++++++++++++++-------------- src/gui/game/SampleTool.cpp | 8 +-- src/gui/game/Tool.h | 12 +++-- 6 files changed, 83 insertions(+), 52 deletions(-) diff --git a/src/gui/game/GameController.cpp b/src/gui/game/GameController.cpp index a99cefac0..9dab16681 100644 --- a/src/gui/game/GameController.cpp +++ b/src/gui/game/GameController.cpp @@ -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(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; diff --git a/src/gui/game/GameController.h b/src/gui/game/GameController.h index a5191809d..05b3c8df9 100644 --- a/src/gui/game/GameController.h +++ b/src/gui/game/GameController.h @@ -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); diff --git a/src/gui/game/GameView.cpp b/src/gui/game/GameView.cpp index eaea0cac0..1f6161f7b 100644 --- a/src/gui/game/GameView.cpp +++ b/src/gui/game/GameView.cpp @@ -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() diff --git a/src/gui/game/PropertyTool.cpp b/src/gui/game/PropertyTool.cpp index 36b80d659..c0bc2991e 100644 --- a/src/gui/game/PropertyTool.cpp +++ b/src/gui/game/PropertyTool.cpp @@ -28,6 +28,7 @@ class PropertyWindow: public ui::Window { void HandlePropertyChange(); + std::optional> TakePropertyFrom(const Particle *takePropertyFrom) const; public: ui::DropDown * property; @@ -36,7 +37,7 @@ public: Simulation *sim; std::vector properties; std::optional 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(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> PropertyWindow::TakePropertyFrom(const Particle *takePropertyFrom) const +{ + auto toolConfiguration = tool->GetConfiguration(); + if (!toolConfiguration || !takePropertyFrom) + { + return {}; + } + auto *prop = reinterpret_cast(takePropertyFrom) + toolConfiguration->prop.Offset; + auto takeValue = [this, toolConfiguration](auto &value) -> std::optional> { + 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(prop)); + + case StructProperty::ParticleType: + case StructProperty::Integer: + return takeValue(*reinterpret_cast(prop)); + + case StructProperty::UInteger: + return takeValue(*reinterpret_cast(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) diff --git a/src/gui/game/SampleTool.cpp b/src/gui/game/SampleTool.cpp index b50330da5..8bf7a67ca 100644 --- a/src/gui/game/SampleTool.cpp +++ b/src/gui/game/SampleTool.cpp @@ -40,11 +40,11 @@ void SampleTool::Draw(Simulation * sim, Brush const &brush, ui::Point position) } if (part) { - auto *propTool = static_cast(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(gameModel.GetToolFromIdentifier("DEFAULT_UI_PROPERTY")); + gameModel.SetActiveTool(0, propTool); + propTool->OpenWindow(gameModel.GetSimulation(), part); } else if (part->type == PT_LIFE) { diff --git a/src/gui/game/Tool.h b/src/gui/game/Tool.h index 273b347b1..469b4530c 100644 --- a/src/gui/game/Tool.h +++ b/src/gui/game/Tool.h @@ -25,6 +25,9 @@ public: RGB 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 colour, ByteString identifier, std::unique_ptr (*textureGen)(int, Vec2) = NULL, bool blocky = false @@ -110,6 +113,9 @@ public: }; private: + void SetProperty(Simulation *sim, ui::Point position); + void SetConfiguration(std::optional newConfiguration); + GameModel &gameModel; std::optional 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 newConfiguration); - std::optional GetConfiguration() const { return configuration;