diff --git a/client/src/board/boardareawidget.cpp b/client/src/board/boardareawidget.cpp index 10b0d2c9..00e54b85 100644 --- a/client/src/board/boardareawidget.cpp +++ b/client/src/board/boardareawidget.cpp @@ -175,6 +175,8 @@ void BoardAreaWidget::tileObjectAdded(int16_t x, int16_t y, const ObjectPtr& obj case TileId::PushButton: tryConnect("color"); + tryConnect("text"); + tryConnect("text_color"); break; case TileId::RailNXButton: @@ -462,8 +464,7 @@ QString BoardAreaWidget::getTileToolTip(const TileLocation& l) const return text; } } - else if(tileId == TileId::PushButton || - tileId == TileId::RailNXButton) + else if(tileId == TileId::RailNXButton) { if(auto tile = m_board->getTileObject(l)) { @@ -740,7 +741,17 @@ void BoardAreaWidget::paintEvent(QPaintEvent* event) break; case TileId::PushButton: - tilePainter.drawPushButton(r, getColor(it.first)); + if(auto button = m_board->getTileObject(it.first)) [[likely]] + { + tilePainter.drawPushButton(r, + button->getPropertyValueEnum("color", Color::Yellow), + button->getPropertyValueEnum("text_color", Color::Black), + button->getPropertyValueString("text")); + } + else + { + tilePainter.drawPushButton(r); + } break; case TileId::RailDecoupler: diff --git a/client/src/board/tilepainter.cpp b/client/src/board/tilepainter.cpp index d78b45e9..c7953748 100644 --- a/client/src/board/tilepainter.cpp +++ b/client/src/board/tilepainter.cpp @@ -3,7 +3,7 @@ * * This file is part of the traintastic source code. * - * Copyright (C) 2020-2024 Reinder Feenstra + * Copyright (C) 2020-2025 Reinder Feenstra * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -438,12 +438,28 @@ void TilePainter::drawBlock(TileId id, const QRectF& r, TileRotate rotate, bool } } -void TilePainter::drawPushButton(const QRectF& r, Color color) +void TilePainter::drawPushButton(const QRectF& r, Color color, Color textColor, const QString& text) { - m_painter.setPen(QPen(Qt::gray, r.width() / 10)); + const auto size = std::min(r.height(), r.width()); + m_painter.setPen(QPen(Qt::gray, size / 10)); m_painter.setBrush(toQColor(color)); - const qreal radius = r.width() * 0.4; - m_painter.drawEllipse(r.center(), radius, radius); + const qreal radius = size * 0.4; + if(r.height() == r.width()) + { + m_painter.drawEllipse(r.center(), radius, radius); + } + else + { + const auto margin = size * 0.1; + m_painter.drawRoundedRect(r.adjusted(margin, margin, -margin, -margin), radius, radius); + } + + if(!text.isEmpty()) + { + const auto margin = size * 0.2; + m_painter.setPen(toQColor(textColor)); + m_painter.drawText(r.adjusted(margin, margin, -margin, -margin), text, QTextOption(Qt::AlignCenter)); + } } void TilePainter::drawSwitch(const QRectF& r, bool value, Color colorOn, Color colorOff) diff --git a/client/src/board/tilepainter.hpp b/client/src/board/tilepainter.hpp index 24b47ffb..0b2a7140 100644 --- a/client/src/board/tilepainter.hpp +++ b/client/src/board/tilepainter.hpp @@ -3,7 +3,7 @@ * * This file is part of the traintastic source code. * - * Copyright (C) 2020-2024 Reinder Feenstra + * Copyright (C) 2020-2025 Reinder Feenstra * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -107,7 +107,7 @@ class TilePainter void drawSignal(TileId id, const QRectF& r, TileRotate rotate, bool isReserved = false, SignalAspect aspect = SignalAspect::Unknown); void drawBlock(TileId id, const QRectF& r, TileRotate rotate, bool isReservedA = false, bool isReservedB = false, const ObjectPtr& blockTile = {}); - void drawPushButton(const QRectF& r, Color color = Color::Yellow); + void drawPushButton(const QRectF& r, Color color = Color::Yellow, Color textColor = Color::Black, const QString& text = {}); void drawSwitch(const QRectF& r, bool value = false, Color colorOn = Color::Yellow, Color colorOff = Color::Gray); void drawRailDecoupler(const QRectF& r, TileRotate rotate, bool isReserved = false, DecouplerState active = DecouplerState::Deactivated); diff --git a/manual/luadoc/object/pushbuttontile.json b/manual/luadoc/object/pushbuttontile.json index 2aad86c4..4c52226a 100644 --- a/manual/luadoc/object/pushbuttontile.json +++ b/manual/luadoc/object/pushbuttontile.json @@ -1,6 +1,7 @@ { - "name": {}, "color": {}, + "text": {}, + "text_color": {}, "on_pressed": { "parameters": [ { diff --git a/server/src/board/tile/misc/pushbuttontile.cpp b/server/src/board/tile/misc/pushbuttontile.cpp index 5128a957..2f0e5016 100644 --- a/server/src/board/tile/misc/pushbuttontile.cpp +++ b/server/src/board/tile/misc/pushbuttontile.cpp @@ -3,7 +3,7 @@ * * This file is part of the traintastic source code. * - * Copyright (C) 2022,2024 Reinder Feenstra + * Copyright (C) 2022-2025 Reinder Feenstra * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -30,8 +30,9 @@ CREATE_IMPL(PushButtonTile) PushButtonTile::PushButtonTile(World& world, std::string_view _id) : Tile(world, _id, TileId::PushButton) - , name{this, "name", id, PropertyFlags::ReadWrite | PropertyFlags::Store | PropertyFlags::ScriptReadOnly} - , color{this, "color", Color::Yellow, PropertyFlags::ReadWrite | PropertyFlags::Store | PropertyFlags::ScriptReadOnly} + , color{this, "color", Color::Yellow, PropertyFlags::ReadWrite | PropertyFlags::Store | PropertyFlags::ScriptReadWrite} + , text{this, "text", "", PropertyFlags::ReadWrite | PropertyFlags::Store | PropertyFlags::ScriptReadWrite} + , textColor{this, "text_color", Color::Black, PropertyFlags::ReadWrite | PropertyFlags::Store | PropertyFlags::ScriptReadWrite} , pressed{*this, "pressed", [this]() { @@ -39,28 +40,19 @@ PushButtonTile::PushButtonTile(World& world, std::string_view _id) }} , onPressed{*this, "on_pressed", EventFlags::Scriptable} { - const bool editable = contains(m_world.state.value(), WorldState::Edit); + Attributes::setMax(height, 16); + Attributes::setMax(width, 16); - Attributes::addDisplayName(name, DisplayName::Object::name); - Attributes::addEnabled(name, editable); - m_interfaceItems.add(name); - - Attributes::addEnabled(color, editable); Attributes::addValues(color, colorValuesWithoutNone); m_interfaceItems.add(color); + m_interfaceItems.add(text); + + Attributes::addValues(textColor, colorValuesWithoutNone); + m_interfaceItems.add(textColor); + Attributes::addObjectEditor(pressed, false); m_interfaceItems.add(pressed); m_interfaceItems.add(onPressed); } - -void PushButtonTile::worldEvent(WorldState worldState, WorldEvent worldEvent) -{ - Tile::worldEvent(worldState, worldEvent); - - const bool editable = contains(worldState, WorldState::Edit); - - Attributes::setEnabled(name, editable); - Attributes::setEnabled(color, editable); -} diff --git a/server/src/board/tile/misc/pushbuttontile.hpp b/server/src/board/tile/misc/pushbuttontile.hpp index 76c07048..7ae9dfc9 100644 --- a/server/src/board/tile/misc/pushbuttontile.hpp +++ b/server/src/board/tile/misc/pushbuttontile.hpp @@ -3,7 +3,7 @@ * * This file is part of the traintastic source code. * - * Copyright (C) 2022,2024 Reinder Feenstra + * Copyright (C) 2022-2025 Reinder Feenstra * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -34,12 +34,10 @@ class PushButtonTile : public Tile DEFAULT_ID("push_button") CREATE_DEF(PushButtonTile) - protected: - void worldEvent(WorldState worldState, WorldEvent worldEvent) final; - public: - Property name; Property color; + Property text; + Property textColor; Method pressed; Event> onPressed; diff --git a/shared/translations/en-us.json b/shared/translations/en-us.json index e014408c..eead7963 100644 --- a/shared/translations/en-us.json +++ b/shared/translations/en-us.json @@ -83,6 +83,14 @@ "term": "board_tile.misc.push_button:color", "definition": "Color" }, + { + "term": "board_tile.misc.push_button:text", + "definition": "Text" + }, + { + "term": "board_tile.misc.push_button:text_color", + "definition": "Text color" + }, { "term": "board_tile.misc.switch:color_off", "definition": "Color when off"