Merge pull request #44 from gfgit/work/active_train

Train: new active property
Dieser Commit ist enthalten in:
Reinder Feenstra 2023-04-25 16:12:30 +02:00 committet von GitHub
Commit 7be15c5b12
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
4 geänderte Dateien mit 82 neuen und 5 gelöschten Zeilen

Datei anzeigen

@ -102,8 +102,12 @@ Train::Train(World& world, std::string_view _id) :
updateEnabled();
}
for(const auto& vehicle : m_poweredVehicles)
vehicle->setEmergencyStop(value);
if(active)
{
//Propagate to all vehicles in this Train
for(const auto& vehicle : m_poweredVehicles)
vehicle->setEmergencyStop(value);
}
}},
weight{*this, "weight", 0, WeightUnit::Ton, PropertyFlags::ReadWrite | PropertyFlags::Store},
overrideWeight{this, "override_weight", false, PropertyFlags::ReadWrite | PropertyFlags::Store,
@ -115,6 +119,12 @@ Train::Train(World& world, std::string_view _id) :
}},
vehicles{this, "vehicles", nullptr, PropertyFlags::ReadOnly | PropertyFlags::Store | PropertyFlags::SubObject},
powered{this, "powered", false, PropertyFlags::ReadOnly | PropertyFlags::NoStore | PropertyFlags::ScriptReadOnly},
active{this, "active", false, PropertyFlags::ReadWrite | PropertyFlags::StoreState | PropertyFlags::ScriptReadOnly,
[this](bool)
{
updateSpeed();
},
std::bind(&Train::setTrainActive, this, std::placeholders::_1)},
notes{this, "notes", "", PropertyFlags::ReadWrite | PropertyFlags::Store}
{
vehicles.setValueInternal(std::make_shared<TrainVehicleList>(*this, vehicles.name()));
@ -154,6 +164,10 @@ Train::Train(World& world, std::string_view _id) :
m_interfaceItems.add(weight);
m_interfaceItems.add(overrideWeight);
m_interfaceItems.add(vehicles);
Attributes::addEnabled(active, true);
m_interfaceItems.add(active);
Attributes::addObjectEditor(powered, false);
m_interfaceItems.add(powered);
Attributes::addDisplayName(notes, DisplayName::Object::notes);
@ -204,6 +218,9 @@ void Train::updateSpeed()
if(m_speedState == SpeedState::Idle)
return;
if(m_speedState == SpeedState::Accelerate && !active)
return;
const double targetSpeed = throttleSpeed.getValue(SpeedUnit::MeterPerSecond);
double currentSpeed = speed.getValue(SpeedUnit::MeterPerSecond);
@ -327,3 +344,53 @@ void Train::updateEnabled()
Attributes::setEnabled(vehicles->move, stopped);
Attributes::setEnabled(vehicles->reverse, stopped);
}
bool Train::setTrainActive(bool val)
{
auto self = this->shared_ptr<Train>();
if(val)
{
//To activate a train, ensure all vehicles are stopped and free
for(const auto& vehicle : *vehicles)
{
assert(vehicle->activeTrain.value() != self);
if(vehicle->activeTrain.value())
{
return false; //Not free
}
if(auto decoder = vehicle->decoder.value(); decoder && !almostZero(decoder->throttle.value()))
{
return false; //Already running
}
}
//Now really activate
//Register this train as activeTrain
for(const auto& vehicle : *vehicles)
{
vehicle->activeTrain.setValueInternal(self);
}
//Sync Emergency Stop state
const bool stopValue = emergencyStop;
for(const auto& vehicle : m_poweredVehicles)
vehicle->setEmergencyStop(stopValue);
}
else
{
//To deactivate a Train it must be stopped first
if(!isStopped)
return false;
//Deactivate all vehicles
for(const auto& vehicle : *vehicles)
{
assert(vehicle->activeTrain.value() == self);
vehicle->activeTrain.setValueInternal(nullptr);
}
}
return true;
}

Datei anzeigen

@ -59,6 +59,7 @@ class Train : public IdObject
void updatePowered();
void updateSpeedMax();
void updateEnabled();
bool setTrainActive(bool val);
protected:
void addToWorld() override;
@ -84,9 +85,10 @@ class Train : public IdObject
Property<bool> overrideWeight;
ObjectProperty<TrainVehicleList> vehicles;
Property<bool> powered;
Property<bool> active;
Property<std::string> notes;
Train(World& world, std::string_view _id);
};
#endif
#endif

Datei anzeigen

@ -34,7 +34,8 @@ RailVehicle::RailVehicle(World& world, std::string_view _id) :
lob{*this, "lob", 0, LengthUnit::MilliMeter, PropertyFlags::ReadWrite | PropertyFlags::Store},
speedMax{*this, "speed_max", 0, SpeedUnit::KiloMeterPerHour, PropertyFlags::ReadWrite | PropertyFlags::Store},
weight{*this, "weight", 0, WeightUnit::Ton, PropertyFlags::ReadWrite | PropertyFlags::Store, [this](double /*value*/, WeightUnit /*unit*/){ updateTotalWeight(); }},
totalWeight{*this, "total_weight", 0, WeightUnit::Ton, PropertyFlags::ReadOnly | PropertyFlags::NoStore}
totalWeight{*this, "total_weight", 0, WeightUnit::Ton, PropertyFlags::ReadOnly | PropertyFlags::NoStore},
activeTrain{this, "active_train", nullptr, PropertyFlags::ReadOnly | PropertyFlags::ScriptReadOnly | PropertyFlags::StoreState}
{
const bool editable = contains(m_world.state.value(), WorldState::Edit);
@ -58,6 +59,10 @@ RailVehicle::RailVehicle(World& world, std::string_view _id) :
Attributes::addObjectEditor(totalWeight, false);
Attributes::addDisplayName(totalWeight, DisplayName::Vehicle::Rail::totalWeight);
m_interfaceItems.insertBefore(totalWeight, notes);
Attributes::addDisplayName(activeTrain, DisplayName::Vehicle::Rail::train); //TODO: "Active"
Attributes::addEnabled(activeTrain, true);
m_interfaceItems.insertBefore(activeTrain, notes);
}
void RailVehicle::addToWorld()

Datei anzeigen

@ -27,6 +27,7 @@
#include "../../core/lengthproperty.hpp"
#include "../../core/speedproperty.hpp"
#include "../../core/weightproperty.hpp"
#include "../../core/trainproperty.hpp"
#include "../../hardware/decoder/decoder.hpp"
class RailVehicle : public Vehicle
@ -48,6 +49,8 @@ class RailVehicle : public Vehicle
SpeedProperty speedMax;
WeightProperty weight;
WeightProperty totalWeight;
TrainProperty activeTrain;
};
#endif
#endif