File: LayoutItem.h

package info (click to toggle)
railcontrol 24%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,540 kB
  • sloc: cpp: 40,823; javascript: 2,509; makefile: 110; php: 97; sh: 60
file content (261 lines) | stat: -rw-r--r-- 5,607 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
RailControl - Model Railway Control Software

Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org

RailControl 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 3, or (at your option) any
later version.

RailControl 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 RailControl; see the file LICENCE. If not see
<http://www.gnu.org/licenses/>.
*/

#pragma once

#include <map>
#include <string>

#include "DataTypes.h"
#include "DataModel/Object.h"

namespace DataModel
{
	class LayoutItem : public Object
	{
		public:
			enum LayoutRotationEnum : unsigned char
			{
				Rotation0 = 0,
				Rotation90,
				Rotation180,
				Rotation270,
				RotationNotRelevant
			};

			class LayoutRotation
			{
				public:
					inline LayoutRotation()
					:	rotation(Rotation0)
					{
					}

					inline LayoutRotation(const LayoutRotationEnum rotation)
					:	rotation(rotation)
					{
					}

					inline LayoutRotation(const int rotation)
					{
						*this = rotation;
					}

					inline LayoutRotation& operator=(const int rotation)
					{
						this->rotation = static_cast<LayoutRotationEnum>(rotation & 0x03);
						return *this;
					}

					inline LayoutRotation& operator=(const LayoutRotationEnum rotation)
					{
						this->rotation = rotation;
						return *this;
					}

					inline bool operator==(const LayoutRotationEnum rotation) const
					{
						return this->rotation == rotation;
					}

					inline bool operator!=(const LayoutRotationEnum rotation) const
					{
						return this->rotation != rotation;
					}

					inline LayoutRotation& operator++()
					{
						unsigned char r = static_cast<unsigned char>(rotation);
						++r;
						r &= 0x03;
						rotation = static_cast<LayoutRotationEnum>(r);
						return *this;
					}

					inline operator LayoutRotationEnum() const
					{
						return rotation;
					}

				private:
					LayoutRotationEnum rotation;
			};

			typedef unsigned char LayoutItemSize;
			typedef signed char LayoutPosition;

			enum Visible : unsigned char
			{
				VisibleNo = 0,
				VisibleYes,
				VisibleNotRelevant
			};

			static const LayoutItemSize Width1 = 1;
			static const LayoutItemSize Height1 = 1;
			static const LayoutItemSize Height2 = 2;

			inline LayoutItem(const ObjectID objectID)
			:	Object(objectID),
				visible(VisibleYes),
				posX(0),
				posY(0),
				posZ(0),
				width(Width1),
				height(Height1),
				rotation(Rotation0)
			{
			}

			inline LayoutItem()
			:	LayoutItem(0)
			{
			}

			virtual ~LayoutItem() {}

			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);

			virtual bool Position(LayoutPosition& x,
				LayoutPosition& y,
				LayoutPosition& z,
				LayoutItemSize& w,
				LayoutItemSize& h,
				LayoutRotation& r) const
			{
				z = posZ;
				r = rotation;
				return MapPosition(posX, posY, width, height, rotation, x, y, w, h);
			}


			virtual bool CheckPositionFree(const LayoutPosition posX, const LayoutPosition posY, const LayoutPosition posZ);
			virtual std::string Serialize() const override;
			virtual void Deserialize(const std::string& serialized) override;
			virtual std::string GetLayoutType() const = 0;

			inline void SetVisible(const Visible visible)
			{
				this->visible = visible;
			}

			inline Visible GetVisible() const
			{
				return visible;
			}

			inline void SetPosX(const LayoutPosition x)
			{
				this->posX = x;
			}

			inline LayoutPosition GetPosX() const
			{
				return posX;
			}

			inline void SetPosY(const LayoutPosition y)
			{
				this->posY = y;
			}

			inline LayoutPosition GetPosY() const
			{
				return posY;
			}

			inline void SetPosZ(const LayoutPosition z)
			{
				this->posZ = z;
			}

			inline LayoutPosition GetPosZ() const
			{
				return posZ;
			}

			inline bool HasPosition(const LayoutPosition x,
				const LayoutPosition y,
				const LayoutPosition z) const
			{
				return (posX == x) && (posY == y) && (posZ == z);
			}

			inline bool IsVisibleOnLayer(const LayoutPosition z) const
			{
				return (posZ == z) && (visible == VisibleYes);
			}

			inline void SetWidth(const LayoutItemSize width)
			{
				this->width = width;
			}

			inline LayoutItemSize GetWidth() const
			{
				return width;
			}

			inline void SetHeight(const LayoutItemSize height)
			{
				this->height = height;
			}

			inline LayoutItemSize GetHeight() const
			{
				return height;
			}

			inline void SetRotation(const LayoutRotation rotation)
			{
				this->rotation = rotation;
			}

			inline LayoutRotation GetRotation() const
			{
				return rotation;
			}

			virtual std::string Rotation() const
			{
				return Rotation(rotation);
			}

			inline void Rotate()
			{
				++rotation;
			}

			static std::string Rotation(LayoutRotation rotation);

		protected:
			virtual void Deserialize(const std::map<std::string,std::string>& arguments) override;
			
		private:
			Visible visible;
			LayoutPosition posX;
			LayoutPosition posY;
			LayoutPosition posZ;
			LayoutItemSize width;
			LayoutItemSize height;
			LayoutRotation rotation;
	};
} // namespace DataModel