| 1 |
/* |
| 2 |
RailControl - Model Railway Control Software |
| 3 |
|
| 4 |
Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org |
| 5 |
|
| 6 |
RailControl is free software; you can redistribute it and/or modify it |
| 7 |
under the terms of the GNU General Public License as published by the |
| 8 |
Free Software Foundation; either version 3, or (at your option) any |
| 9 |
later version. |
| 10 |
|
| 11 |
RailControl is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 |
See the GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with RailControl; see the file LICENCE. If not see |
| 18 |
<http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#pragma once |
| 22 |
|
| 23 |
#include <map> |
| 24 |
#include <string> |
| 25 |
|
| 26 |
#include "DataTypes.h" |
| 27 |
#include "DataModel/Object.h" |
| 28 |
|
| 29 |
namespace DataModel |
| 30 |
{ |
| 31 |
class LayoutItem : public Object |
| 32 |
{ |
| 33 |
public: |
| 34 |
enum LayoutRotationEnum : unsigned char |
| 35 |
{ |
| 36 |
Rotation0 = 0, |
| 37 |
Rotation90, |
| 38 |
Rotation180, |
| 39 |
Rotation270, |
| 40 |
RotationNotRelevant |
| 41 |
}; |
| 42 |
|
| 43 |
class LayoutRotation |
| 44 |
{ |
| 45 |
public: |
| 46 |
inline LayoutRotation() |
| 47 |
: rotation(Rotation0) |
| 48 |
{ |
| 49 |
} |
| 50 |
|
| 51 |
inline LayoutRotation(const LayoutRotationEnum rotation) |
| 52 |
: rotation(rotation) |
| 53 |
{ |
| 54 |
} |
| 55 |
|
| 56 |
inline LayoutRotation(const int rotation) |
| 57 |
{ |
| 58 |
*this = rotation; |
| 59 |
} |
| 60 |
|
| 61 |
inline LayoutRotation& operator=(const int rotation) |
| 62 |
{ |
| 63 |
this->rotation = (rotation > RotationNotRelevant || rotation < Rotation0) ? Rotation0 : static_cast<LayoutRotationEnum>(rotation); |
| 64 |
return *this; |
| 65 |
} |
| 66 |
|
| 67 |
inline LayoutRotation& operator=(const LayoutRotationEnum rotation) |
| 68 |
{ |
| 69 |
this->rotation = rotation; |
| 70 |
return *this; |
| 71 |
} |
| 72 |
|
| 73 |
inline bool operator==(const LayoutRotationEnum rotation) const |
| 74 |
{ |
| 75 |
return this->rotation == rotation; |
| 76 |
} |
| 77 |
|
| 78 |
inline bool operator!=(const LayoutRotationEnum rotation) const |
| 79 |
{ |
| 80 |
return this->rotation != rotation; |
| 81 |
} |
| 82 |
|
| 83 |
inline LayoutRotation& operator++() |
| 84 |
{ |
| 85 |
unsigned char r = static_cast<unsigned char>(rotation); |
| 86 |
++r; |
| 87 |
r &= 0x03; |
| 88 |
rotation = static_cast<LayoutRotationEnum>(r); |
| 89 |
return *this; |
| 90 |
} |
| 91 |
|
| 92 |
inline operator LayoutRotationEnum() const |
| 93 |
{ |
| 94 |
return rotation; |
| 95 |
} |
| 96 |
|
| 97 |
private: |
| 98 |
LayoutRotationEnum rotation; |
| 99 |
}; |
| 100 |
|
| 101 |
typedef unsigned char LayoutItemSize; |
| 102 |
typedef signed char LayoutPosition; |
| 103 |
|
| 104 |
enum Visible : unsigned char |
| 105 |
{ |
| 106 |
VisibleNo = 0, |
| 107 |
VisibleYes, |
| 108 |
VisibleNotRelevant |
| 109 |
}; |
| 110 |
|
| 111 |
static const LayoutItemSize Width1 = 1; |
| 112 |
static const LayoutItemSize Height1 = 1; |
| 113 |
static const LayoutItemSize Height2 = 2; |
| 114 |
|
| 115 |
inline LayoutItem(const ObjectID objectID) |
| 116 |
: Object(objectID), |
| 117 |
visible(VisibleYes), |
| 118 |
posX(0), |
| 119 |
posY(0), |
| 120 |
posZ(0), |
| 121 |
width(Width1), |
| 122 |
height(Height1), |
| 123 |
rotation(Rotation0) |
| 124 |
{ |
| 125 |
} |
| 126 |
|
| 127 |
inline LayoutItem() |
| 128 |
: LayoutItem(0) |
| 129 |
{ |
| 130 |
} |
| 131 |
|
| 132 |
virtual ~LayoutItem() {} |
| 133 |
|
| 134 |
static bool MapPosition(const LayoutPosition posX, const LayoutPosition posY, const LayoutItemSize width, const LayoutItemSize height, const LayoutRotation rotation, LayoutPosition& x, LayoutPosition& y, LayoutItemSize& w, LayoutItemSize& h); |
| 135 |
|
| 136 |
virtual bool Position(LayoutPosition& x, |
| 137 |
LayoutPosition& y, |
| 138 |
LayoutPosition& z, |
| 139 |
LayoutItemSize& w, |
| 140 |
LayoutItemSize& h, |
| 141 |
LayoutRotation& r) const |
| 142 |
{ |
| 143 |
z = posZ; |
| 144 |
r = rotation; |
| 145 |
return MapPosition(posX, posY, width, height, rotation, x, y, w, h); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
virtual bool CheckPositionFree(const LayoutPosition posX, const LayoutPosition posY, const LayoutPosition posZ); |
| 150 |
virtual std::string Serialize() const override; |
| 151 |
virtual bool Deserialize(const std::string& serialized) override; |
| 152 |
virtual std::string GetLayoutType() const = 0; |
| 153 |
|
| 154 |
inline void SetVisible(const Visible visible) |
| 155 |
{ |
| 156 |
this->visible = visible; |
| 157 |
} |
| 158 |
|
| 159 |
inline Visible GetVisible() const |
| 160 |
{ |
| 161 |
return visible; |
| 162 |
} |
| 163 |
|
| 164 |
inline void SetPosX(const LayoutPosition x) |
| 165 |
{ |
| 166 |
this->posX = x; |
| 167 |
} |
| 168 |
|
| 169 |
inline LayoutPosition GetPosX() const |
| 170 |
{ |
| 171 |
return posX; |
| 172 |
} |
| 173 |
|
| 174 |
inline void SetPosY(const LayoutPosition y) |
| 175 |
{ |
| 176 |
this->posY = y; |
| 177 |
} |
| 178 |
|
| 179 |
inline LayoutPosition GetPosY() const |
| 180 |
{ |
| 181 |
return posY; |
| 182 |
} |
| 183 |
|
| 184 |
inline void SetPosZ(const LayoutPosition z) |
| 185 |
{ |
| 186 |
this->posZ = z; |
| 187 |
} |
| 188 |
|
| 189 |
inline LayoutPosition GetPosZ() const |
| 190 |
{ |
| 191 |
return posZ; |
| 192 |
} |
| 193 |
|
| 194 |
inline bool HasPosition(const LayoutPosition x, |
| 195 |
const LayoutPosition y, |
| 196 |
const LayoutPosition z) const |
| 197 |
{ |
| 198 |
return posX == x && posY == y && posZ == z; |
| 199 |
} |
| 200 |
|
| 201 |
inline bool IsVisibleOnLayer(const LayoutPosition z) const |
| 202 |
{ |
| 203 |
return posZ == z && visible == VisibleYes; |
| 204 |
} |
| 205 |
|
| 206 |
inline void SetWidth(const LayoutItemSize width) |
| 207 |
{ |
| 208 |
this->width = width; |
| 209 |
} |
| 210 |
|
| 211 |
inline LayoutItemSize GetWidth() const |
| 212 |
{ |
| 213 |
return width; |
| 214 |
} |
| 215 |
|
| 216 |
inline void SetHeight(const LayoutItemSize height) |
| 217 |
{ |
| 218 |
this->height = height; |
| 219 |
} |
| 220 |
|
| 221 |
inline LayoutItemSize GetHeight() const |
| 222 |
{ |
| 223 |
return height; |
| 224 |
} |
| 225 |
|
| 226 |
inline void SetRotation(const LayoutRotation rotation) |
| 227 |
{ |
| 228 |
this->rotation = rotation; |
| 229 |
} |
| 230 |
|
| 231 |
inline LayoutRotation GetRotation() const |
| 232 |
{ |
| 233 |
return rotation; |
| 234 |
} |
| 235 |
|
| 236 |
virtual std::string Rotation() const |
| 237 |
{ |
| 238 |
return Rotation(rotation); |
| 239 |
} |
| 240 |
|
| 241 |
inline void Rotate() |
| 242 |
{ |
| 243 |
++rotation; |
| 244 |
} |
| 245 |
|
| 246 |
static std::string Rotation(LayoutRotation rotation); |
| 247 |
|
| 248 |
protected: |
| 249 |
virtual bool Deserialize(const std::map<std::string,std::string>& arguments) override; |
| 250 |
|
| 251 |
private: |
| 252 |
Visible visible; |
| 253 |
LayoutPosition posX; |
| 254 |
LayoutPosition posY; |
| 255 |
LayoutPosition posZ; |
| 256 |
LayoutItemSize width; |
| 257 |
LayoutItemSize height; |
| 258 |
LayoutRotation rotation; |
| 259 |
}; |
| 260 |
} // namespace DataModel |
| 261 |
|