train: added on_block_entered and on_block_left events.

Dieser Commit ist enthalten in:
Reinder Feenstra 2024-06-02 16:13:25 +02:00
Ursprung 07f449f024
Commit 1296484ab2
4 geänderte Dateien mit 72 neuen und 6 gelöschten Zeilen

Datei anzeigen

@ -5,5 +5,33 @@
"emergency_stop": {},
"powered": {},
"active": {},
"mode": {}
}
"mode": {},
"on_block_entered": {
"parameters": [
{
"name": "train"
},
{
"name": "block"
},
{
"name": "direction"
}
],
"since": "0.3"
},
"on_block_left": {
"parameters": [
{
"name": "train"
},
{
"name": "block"
},
{
"name": "direction"
}
],
"since": "0.3"
}
}

Datei anzeigen

@ -295,6 +295,8 @@ void BlockRailTile::inputItemValueChanged(BlockInputMapItem& item)
trains.appendInternal(blockStatus);
updateTrainMethodEnabled();
train->blockEntered(*this, direction);
fireEvent(
onTrainEntered,
blockStatus->train.value(),
@ -366,11 +368,16 @@ void BlockRailTile::inputItemValueChanged(BlockInputMapItem& item)
updateState();
auto train = blockStatus->train.value();
auto direction = blockStatus->direction.value();
train->blockLeft(*this, direction);
fireEvent(
onTrainLeft,
blockStatus->train.value(),
train,
shared_ptr<BlockRailTile>(),
blockStatus->direction.value());
direction);
blockStatus->destroy();
#ifndef NDEBUG

Datei anzeigen

@ -3,7 +3,7 @@
*
* This file is part of the traintastic source code.
*
* Copyright (C) 2019-2021,2023-2024 Reinder Feenstra
* Copyright (C) 2019-2024 Reinder Feenstra
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -146,6 +146,8 @@ Train::Train(World& world, std::string_view _id) :
mode{this, "mode", TrainMode::ManualUnprotected, PropertyFlags::ReadWrite | PropertyFlags::StoreState | PropertyFlags::ScriptReadOnly},
blocks{*this, "blocks", {}, PropertyFlags::ReadOnly | PropertyFlags::StoreState},
notes{this, "notes", "", PropertyFlags::ReadWrite | PropertyFlags::Store}
, onBlockEntered{*this, "on_block_entered", EventFlags::Scriptable}
, onBlockLeft{*this, "on_block_left", EventFlags::Scriptable}
{
vehicles.setValueInternal(std::make_shared<TrainVehicleList>(*this, vehicles.name()));
@ -199,9 +201,30 @@ Train::Train(World& world, std::string_view _id) :
Attributes::addDisplayName(notes, DisplayName::Object::notes);
m_interfaceItems.add(notes);
m_interfaceItems.add(onBlockEntered);
m_interfaceItems.add(onBlockLeft);
updateEnabled();
}
void Train::blockEntered(BlockRailTile& block, BlockTrainDirection trainDirection)
{
fireEvent(
onBlockEntered,
shared_ptr<Train>(),
block.shared_ptr<BlockRailTile>(),
trainDirection);
}
void Train::blockLeft(BlockRailTile& block, BlockTrainDirection trainDirection)
{
fireEvent(
onBlockLeft,
shared_ptr<Train>(),
block.shared_ptr<BlockRailTile>(),
trainDirection);
}
void Train::addToWorld()
{
IdObject::addToWorld();

Datei anzeigen

@ -3,7 +3,7 @@
*
* This file is part of the traintastic source code.
*
* Copyright (C) 2019-2021,2023 Reinder Feenstra
* Copyright (C) 2019-2024 Reinder Feenstra
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -25,7 +25,9 @@
#include "../core/idobject.hpp"
#include <boost/asio/steady_timer.hpp>
#include <traintastic/enum/blocktraindirection.hpp>
#include <traintastic/enum/trainmode.hpp>
#include "../core/event.hpp"
#include "../core/method.hpp"
#include "../core/objectproperty.hpp"
#include "../core/objectvectorproperty.hpp"
@ -36,6 +38,7 @@
class TrainVehicleList;
class TrainBlockStatus;
class BlockRailTile;
class PoweredRailVehicle;
class Train : public IdObject
@ -98,8 +101,13 @@ class Train : public IdObject
//! If the train changes direction this list will be reversed.
ObjectVectorProperty<TrainBlockStatus> blocks;
Property<std::string> notes;
Event<const std::shared_ptr<Train>&, const std::shared_ptr<BlockRailTile>&, BlockTrainDirection> onBlockEntered;
Event<const std::shared_ptr<Train>&, const std::shared_ptr<BlockRailTile>&, BlockTrainDirection> onBlockLeft;
Train(World& world, std::string_view _id);
void blockEntered(BlockRailTile& block, BlockTrainDirection trainDirection);
void blockLeft(BlockRailTile& block, BlockTrainDirection trainDirection);
};
#endif