board: added label tile

Dieser Commit ist enthalten in:
Reinder Feenstra 2024-04-25 00:14:08 +02:00
Ursprung 73d2303f4c
Commit 08f9a40039
11 geänderte Dateien mit 370 neuen und 6 gelöschten Zeilen

Datei anzeigen

@ -3,7 +3,7 @@
*
* This file is part of the traintastic source code.
*
* Copyright (C) 2020-2023 Reinder Feenstra
* Copyright (C) 2020-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
@ -30,6 +30,7 @@
#include "getboardcolorscheme.hpp"
#include "tilepainter.hpp"
#include "../network/board.hpp"
#include "../network/object.tpp"
#include "../network/object/blockrailtile.hpp"
#include "../network/object/nxbuttonrailtile.hpp"
#include "../network/abstractproperty.hpp"
@ -184,6 +185,13 @@ void BoardAreaWidget::tileObjectAdded(int16_t x, int16_t y, const ObjectPtr& obj
case TileId::RailLink:
case TileId::ReservedForFutureExpension:
break;
case TileId::Label:
tryConnect("background_color");
tryConnect("text");
tryConnect("text_align");
tryConnect("text_color");
break;
}
}
@ -598,6 +606,22 @@ void BoardAreaWidget::paintEvent(QPaintEvent* event)
tilePainter.drawRailNX(r, a, isReserved, getNXButtonEnabled(it.first), getNXButtonPressed(it.first));
break;
case TileId::Label:
{
if(auto label = m_board.board().getTileObject(it.first)) /*[[likely]]*/
{
tilePainter.drawLabel(r, a,
label->getPropertyValueString("text"),
label->getPropertyValueEnum<TextAlign>("text_align", TextAlign::Center),
label->getPropertyValueEnum<Color>("text_color", Color::None),
label->getPropertyValueEnum<Color>("background_color", Color::None));
}
else
{
tilePainter.drawLabel(r, a);
}
break;
}
case TileId::None:
case TileId::ReservedForFutureExpension:
default:

Datei anzeigen

@ -3,7 +3,7 @@
*
* This file is part of the traintastic source code.
*
* Copyright (C) 2020-2023 Reinder Feenstra
* Copyright (C) 2020-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
@ -168,6 +168,10 @@ void TilePainter::draw(TileId id, const QRectF& r, TileRotate rotate, bool isRes
drawRailNX(r, rotate, isReserved);
break;
case TileId::Label:
drawLabel(r, rotate);
break;
case TileId::None:
case TileId::ReservedForFutureExpension:
break;
@ -1295,3 +1299,28 @@ void TilePainter::drawRailNX(const QRectF& r, TileRotate rotate, bool isReserved
drawStraight(r, rotate);
drawPushButton(r, pressed ? Color::White : (isEnabled ? Color::Blue : Color::Gray));
}
void TilePainter::drawLabel(const QRectF& r, TileRotate rotate, const QString& text, TextAlign textAlign, Color textColor, Color backgroundColor)
{
m_painter.save();
m_painter.setPen(Qt::NoPen);
m_painter.setBrush(backgroundColor == Color::None ? m_colorScheme.background : toQColor(backgroundColor));
m_painter.drawRect(r.adjusted(1, 1, -1, -1));
if(!text.isEmpty())
{
m_painter.translate(r.center());
m_painter.rotate(toDeg(rotate));
m_painter.setPen(textColor == Color::None ? m_colorScheme.foreground : toQColor(textColor));
QRectF textRect{r};
if(rotate == TileRotate::Deg90 || rotate == TileRotate::Deg270)
{
textRect = textRect.transposed();
}
textRect.moveCenter({0., 0.});
m_painter.drawText(textRect, text, QTextOption(toAlignment(textAlign)));
}
m_painter.restore();
}

Datei anzeigen

@ -3,7 +3,7 @@
*
* This file is part of the traintastic source code.
*
* Copyright (C) 2020-2023 Reinder Feenstra
* Copyright (C) 2020-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
@ -37,6 +37,7 @@
#include <traintastic/enum/tristate.hpp>
#include <traintastic/enum/turnoutposition.hpp>
#include "../enum/color.hpp"
#include "../enum/textalign.hpp"
#include "../network/objectptr.hpp"
struct BoardColorScheme;
@ -111,6 +112,8 @@ class TilePainter
void drawRailDecoupler(const QRectF& r, TileRotate rotate, bool isReserved = false, DecouplerState active = DecouplerState::Deactivated);
void drawRailNX(const QRectF& r, TileRotate rotate, bool isReserved = false, bool isEnabled = false, bool pressed = false);
void drawLabel(const QRectF& r, TileRotate rotate, const QString& text = "txt", TextAlign textAlign = TextAlign::Center, Color textColor = Color::None, Color backgroundColor = Color::None);
};
#endif

Datei anzeigen

@ -0,0 +1,66 @@
/**
* client/src/enum/textalign.hpp
*
* This file is part of the traintastic source code.
*
* Copyright (C) 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
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef TRAINTASTIC_CLIENT_ENUM_TEXTALIGN_HPP
#define TRAINTASTIC_CLIENT_ENUM_TEXTALIGN_HPP
#include <traintastic/enum/textalign.hpp>
#include <QtGlobal>
constexpr Qt::Alignment toAlignment(TextAlign value)
{
switch(value)
{
case TextAlign::TopLeft:
return Qt::AlignTop | Qt::AlignLeft;
case TextAlign::TopCenter:
return Qt::AlignTop | Qt::AlignHCenter;
case TextAlign::TopRight:
return Qt::AlignTop | Qt::AlignRight;
case TextAlign::CenterLeft:
return Qt::AlignVCenter | Qt::AlignLeft;
case TextAlign::Center:
return Qt::AlignCenter;
case TextAlign::CenterRight:
return Qt::AlignVCenter | Qt::AlignRight;
case TextAlign::BottomLeft:
return Qt::AlignBottom | Qt::AlignLeft;
case TextAlign::BottomCenter:
return Qt::AlignBottom | Qt::AlignHCenter;
case TextAlign::BottomRight:
return Qt::AlignBottom | Qt::AlignRight;
default:
break;
}
return Qt::Alignment();
}
#endif

Datei anzeigen

@ -49,6 +49,7 @@
#include <traintastic/enum/signalaspect.hpp>
#include <traintastic/enum/singleoutputaction.hpp>
#include <traintastic/enum/speedunit.hpp>
#include <traintastic/enum/textalign.hpp>
#include <traintastic/enum/trainmode.hpp>
#include <traintastic/enum/traintasticdiyinterfacetype.hpp>
#include <traintastic/enum/turnoutposition.hpp>
@ -117,6 +118,7 @@ QString translateEnum(const QString& enumName, qint64 value)
TRANSLATE_ENUM(SingleOutputAction)
TRANSLATE_ENUM(SignalAspect)
TRANSLATE_ENUM(SpeedUnit)
TRANSLATE_ENUM(TextAlign)
TRANSLATE_ENUM(TrainMode)
TRANSLATE_ENUM(TraintasticDIYInterfaceType)
TRANSLATE_ENUM(TurnoutPosition)

Datei anzeigen

@ -0,0 +1,48 @@
/**
* server/src/board/tile/misc/labeltile.cpp
*
* This file is part of the traintastic source code.
*
* Copyright (C) 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
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "labeltile.hpp"
#include "../../../core/attributes.hpp"
CREATE_IMPL(LabelTile)
LabelTile::LabelTile(World& world, std::string_view _id)
: Tile(world, _id, TileId::Label)
, backgroundColor{this, "background_color", Color::None, PropertyFlags::ReadWrite | PropertyFlags::Store | PropertyFlags::ScriptReadWrite}
, text{this, "text", id, PropertyFlags::ReadWrite | PropertyFlags::Store | PropertyFlags::ScriptReadWrite}
, textAlign{this, "text_align", TextAlign::Center, PropertyFlags::ReadWrite | PropertyFlags::Store | PropertyFlags::ScriptReadWrite}
, textColor{this, "text_color", Color::None, PropertyFlags::ReadWrite | PropertyFlags::Store | PropertyFlags::ScriptReadWrite}
{
Attributes::setMax<uint8_t>(height, 16);
Attributes::setMax<uint8_t>(width, 16);
m_interfaceItems.add(text);
Attributes::addValues(textAlign, textAlignValues);
m_interfaceItems.add(textAlign);
Attributes::addValues(textColor, colorValues);
m_interfaceItems.add(textColor);
Attributes::addValues(backgroundColor, colorValues);
m_interfaceItems.add(backgroundColor);
}

Datei anzeigen

@ -0,0 +1,45 @@
/**
* server/src/board/tile/misc/labeltile.hpp
*
* This file is part of the traintastic source code.
*
* Copyright (C) 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
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef TRAINTASTIC_SERVER_BOARD_TILE_MISC_LABELTILE_HPP
#define TRAINTASTIC_SERVER_BOARD_TILE_MISC_LABELTILE_HPP
#include "../tile.hpp"
#include "../../../enum/color.hpp"
#include <traintastic/enum/textalign.hpp>
class LabelTile : public Tile
{
CLASS_ID("board_tile.misc.label")
DEFAULT_ID("label")
CREATE_DEF(LabelTile)
public:
Property<Color> backgroundColor;
Property<std::string> text;
Property<TextAlign> textAlign;
Property<Color> textColor;
LabelTile(World& world, std::string_view _id);
};
#endif

Datei anzeigen

@ -3,7 +3,7 @@
*
* This file is part of the traintastic source code.
*
* Copyright (C) 2020-2023 Reinder Feenstra
* Copyright (C) 2020-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
@ -24,6 +24,7 @@
#include "rail/decouplerrailtile.hpp"
#include "rail/linkrailtile.hpp"
#include "rail/nxbuttonrailtile.hpp"
#include "misc/labeltile.hpp"
#include "../../utils/ifclassidcreate.hpp"
#include "../../world/world.hpp"
@ -59,6 +60,7 @@ std::shared_ptr<Tile> Tiles::create(World& world, std::string_view classId, std:
IF_CLASSID_CREATE(PushButtonTile)
IF_CLASSID_CREATE(DecouplerRailTile)
IF_CLASSID_CREATE(NXButtonRailTile)
IF_CLASSID_CREATE(LabelTile)
return std::shared_ptr<Tile>();
}
@ -69,6 +71,7 @@ const std::vector<Tiles::Info>& Tiles::getInfo()
static constexpr uint8_t rotate0and90 = 0x05; //!< only 0 or 90 deg
static constexpr uint8_t rotateHalf = 0x0F; //!< only 0, 45, 90 or 135 deg
static constexpr uint8_t rotateFull = 0xFF;
static constexpr uint8_t rotateFull90 = 0x55; //!< only 0, 90, 180, 270 deg
static constexpr std::string_view straight = "tile_menu:straight";
static constexpr std::string_view curve = "tile_menu:curve";
@ -115,6 +118,7 @@ const std::vector<Tiles::Info>& Tiles::getInfo()
Info{Signal3AspectRailTile::classId, TileId::RailSignal3Aspect, rotateFull, {signal}},
Info{PushButtonTile::classId, TileId::PushButton, rotateNone, {miscellaneous}},
Info{LabelTile::classId, TileId::Label, rotateFull90, {miscellaneous}},
}};
return info;

Datei anzeigen

@ -3,7 +3,7 @@
*
* This file is part of the traintastic source code.
*
* Copyright (C) 2020-2023 Reinder Feenstra
* Copyright (C) 2020-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
@ -58,6 +58,7 @@ enum class TileId : uint16_t // 10 bit
RailLink = 28,
RailDecoupler = 29,
RailNXButton = 30,
Label = 31,
ReservedForFutureExpension = 1023
};
@ -191,6 +192,7 @@ constexpr bool isActive(TileId id)
case TileId::RailLink:
case TileId::RailDecoupler:
case TileId::RailNXButton:
case TileId::Label:
return true;
default:

Datei anzeigen

@ -0,0 +1,77 @@
/**
* shared/src/traintastic/enum/textalign.hpp
*
* This file is part of the traintastic source code.
*
* Copyright (C) 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
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef TRAINTASTIC_SHARED_TRAINTASTIC_ENUM_TEXTALIGN_HPP
#define TRAINTASTIC_SHARED_TRAINTASTIC_ENUM_TEXTALIGN_HPP
#include <cstdint>
#include <array>
#include "enum.hpp"
enum class TextAlign : uint8_t
{
// Horizontal:
Left = 0x01,
HCenter = 0x02,
Right = 0x04,
// Vertical:
Top = 0x10,
VCenter = 0x20,
Bottom = 0x40,
// Combinations:
TopLeft = Top | Left,
TopCenter = Top | HCenter,
TopRight = Top | Right,
CenterLeft = VCenter | Left,
Center = VCenter | HCenter,
CenterRight = VCenter | Right,
BottomLeft = Bottom | Left,
BottomCenter = Bottom | HCenter,
BottomRight = Bottom | Right,
};
TRAINTASTIC_ENUM(TextAlign, "text_align", 9,
{
{TextAlign::TopLeft, "top_left"},
{TextAlign::TopCenter, "top_center"},
{TextAlign::TopRight, "top_right"},
{TextAlign::CenterLeft, "center_left"},
{TextAlign::Center, "center"},
{TextAlign::CenterRight, "center_right"},
{TextAlign::BottomLeft, "bottom_left"},
{TextAlign::BottomCenter, "bottom_center"},
{TextAlign::BottomRight, "bottom_right"},
});
inline constexpr std::array<TextAlign, 9> textAlignValues{{
TextAlign::TopLeft,
TextAlign::TopCenter,
TextAlign::TopRight,
TextAlign::CenterLeft,
TextAlign::Center,
TextAlign::CenterRight,
TextAlign::BottomLeft,
TextAlign::BottomCenter,
TextAlign::BottomRight,
}};
#endif

Datei anzeigen

@ -4698,5 +4698,69 @@
{
"term": "output_map:address_x",
"definition": "Address %1"
},
{
"term": "class_id:board_tile.rail.nx_button",
"definition": "NX button"
},
{
"term": "class_id:board_tile.misc.label",
"definition": "Label"
},
{
"term": "board_tile.misc.label:text",
"definition": "Text"
},
{
"term": "board_tile.misc.label:text_align",
"definition": "Text align"
},
{
"term": "board_tile.misc.label:text_color",
"definition": "Text color"
},
{
"term": "color:none",
"definition": "None"
},
{
"term": "board_tile.misc.label:background_color",
"definition": "Background color"
},
{
"term": "text_align:top_left",
"definition": "Top left"
},
{
"term": "text_align:top_center",
"definition": "Top center"
},
{
"term": "text_align:top_right",
"definition": "Top right"
},
{
"term": "text_align:center_left",
"definition": "Center left"
},
{
"term": "text_align:center",
"definition": "Center"
},
{
"term": "text_align:center_right",
"definition": "Center right"
},
{
"term": "text_align:bottom_left",
"definition": "Bottom left"
},
{
"term": "text_align:bottom_center",
"definition": "Bottom center"
},
{
"term": "text_align:bottom_right",
"definition": "Bottom right"
}
]
]