From fc433273ea7cd770f1d5e553bcda806b47475e3d Mon Sep 17 00:00:00 2001 From: Reinder Feenstra Date: Thu, 10 Jun 2021 00:05:00 +0200 Subject: [PATCH] sensorrailtile: added name, invert and state property --- server/src/board/tile/rail/sensorrailtile.cpp | 55 ++++++++++++++++++- server/src/board/tile/rail/sensorrailtile.hpp | 11 +++- 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/server/src/board/tile/rail/sensorrailtile.cpp b/server/src/board/tile/rail/sensorrailtile.cpp index 56c273f9..74eeffc2 100644 --- a/server/src/board/tile/rail/sensorrailtile.cpp +++ b/server/src/board/tile/rail/sensorrailtile.cpp @@ -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, 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& 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(&input->value)) + state.setValueInternal(input->value.value() ^ invert.value()); } diff --git a/server/src/board/tile/rail/sensorrailtile.hpp b/server/src/board/tile/rail/sensorrailtile.hpp index 6cd15c1e..59144f03 100644 --- a/server/src/board/tile/rail/sensorrailtile.hpp +++ b/server/src/board/tile/rail/sensorrailtile.hpp @@ -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 name; ObjectProperty input; + Property invert; + Property state; SensorRailTile(const std::weak_ptr& world, std::string_view _id); };