sensorrailtile: added name, invert and state property

Dieser Commit ist enthalten in:
Reinder Feenstra 2021-06-10 00:05:00 +02:00
Ursprung c26cb4b401
Commit fc433273ea
2 geänderte Dateien mit 62 neuen und 4 gelöschten Zeilen

Datei anzeigen

@ -3,7 +3,7 @@
*
* This file is part of the traintastic source code.
*
* Copyright (C) 2020 Reinder Feenstra
* Copyright (C) 2020-2021 Reinder Feenstra
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -26,19 +26,68 @@
SensorRailTile::SensorRailTile(const std::weak_ptr<World>& world, std::string_view _id) :
StraightRailTile(world, _id, TileId::RailSensor),
input{this, "input", nullptr, PropertyFlags::ReadWrite | PropertyFlags::Store}
name{this, "name", id, PropertyFlags::ReadWrite | PropertyFlags::Store},
input{this, "input", nullptr, PropertyFlags::ReadWrite | PropertyFlags::Store,
[this](const std::shared_ptr<Input>& value)
{
if(input)
input->propertyChanged.disconnect(m_inputPropertyChanged);
if(value)
{
m_inputPropertyChanged = value->propertyChanged.connect(std::bind(&SensorRailTile::inputPropertyChanged, this, std::placeholders::_1));
inputPropertyChanged(input->value);
}
else
state.setValueInternal(TriState::Undefined);
return true;
}},
invert{this, "invert", false, PropertyFlags::ReadWrite | PropertyFlags::Store,
[this](bool)
{
if(input)
inputPropertyChanged(input->value);
}},
state{this, "state", TriState::Undefined, PropertyFlags::ReadOnly | PropertyFlags::StoreState}
{
auto w = world.lock();
const bool editable = w && contains(w->state.value(), WorldState::Edit);
Attributes::addEnabled(name, editable);
Attributes::addDisplayName(name, "object:name");
m_interfaceItems.add(name);
Attributes::addEnabled(input, editable);
Attributes::addObjectList(input, w->inputs);
m_interfaceItems.add(input);
Attributes::addEnabled(invert, editable);
m_interfaceItems.add(invert);
Attributes::addValues(state, TriStateValues);
m_interfaceItems.add(state);
}
void SensorRailTile::loaded()
{
StraightRailTile::loaded();
if(input)
m_inputPropertyChanged = input->propertyChanged.connect(std::bind(&SensorRailTile::inputPropertyChanged, this, std::placeholders::_1));
}
void SensorRailTile::worldEvent(WorldState state, WorldEvent event)
{
StraightRailTile::worldEvent(state, event);
input.setAttributeEnabled(contains(state, WorldState::Edit));
const bool editable = contains(state, WorldState::Edit);
Attributes::setEnabled(name, editable);
Attributes::setEnabled(input, editable);
Attributes::setEnabled(invert, editable);
}
void SensorRailTile::inputPropertyChanged(AbstractProperty& property)
{
assert(input);
if(&property == static_cast<AbstractProperty*>(&input->value))
state.setValueInternal(input->value.value() ^ invert.value());
}

Datei anzeigen

@ -3,7 +3,7 @@
*
* This file is part of the traintastic source code.
*
* Copyright (C) 2020 Reinder Feenstra
* Copyright (C) 2020-2021 Reinder Feenstra
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -33,11 +33,20 @@ class SensorRailTile : public StraightRailTile
DEFAULT_ID("sensor")
CREATE(SensorRailTile)
private:
boost::signals2::connection m_inputPropertyChanged;
void inputPropertyChanged(AbstractProperty& property);
protected:
void loaded() override;
void worldEvent(WorldState state, WorldEvent event) override;
public:
Property<std::string> name;
ObjectProperty<Input> input;
Property<bool> invert;
Property<TriState> state;
SensorRailTile(const std::weak_ptr<World>& world, std::string_view _id);
};