| 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 |
#include <map> |
| 22 |
#include <sstream> |
| 23 |
#include <string> |
| 24 |
|
| 25 |
#include "DataModel/LayoutItem.h" |
| 26 |
#include "Utils/Utils.h" |
| 27 |
|
| 28 |
using std::map; |
| 29 |
using std::stringstream; |
| 30 |
using std::string; |
| 31 |
|
| 32 |
namespace DataModel |
| 33 |
{ |
| 34 |
bool LayoutItem::MapPosition(const LayoutPosition posX, |
| 35 |
const LayoutPosition posY, |
| 36 |
const LayoutItemSize width, |
| 37 |
const LayoutItemSize height, |
| 38 |
const LayoutRotation rotation, |
| 39 |
LayoutPosition& x, |
| 40 |
LayoutPosition& y, |
| 41 |
LayoutItemSize& w, |
| 42 |
LayoutItemSize& h) |
| 43 |
{ |
| 44 |
x = posX; |
| 45 |
y = posY; |
| 46 |
switch (rotation) |
| 47 |
{ |
| 48 |
case Rotation0: |
| 49 |
case Rotation180: |
| 50 |
w = width; |
| 51 |
h = height; |
| 52 |
return true; |
| 53 |
|
| 54 |
case Rotation90: |
| 55 |
case Rotation270: |
| 56 |
w = height; |
| 57 |
h = width; |
| 58 |
return true; |
| 59 |
|
| 60 |
default: |
| 61 |
return false; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
bool LayoutItem::CheckPositionFree(const LayoutPosition posX, const LayoutPosition posY, const LayoutPosition posZ) |
| 66 |
{ |
| 67 |
if (this->visible == false) |
| 68 |
{ |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
if (this->posZ != posZ) |
| 73 |
{ |
| 74 |
return true; |
| 75 |
} |
| 76 |
|
| 77 |
LayoutPosition x; |
| 78 |
LayoutPosition y; |
| 79 |
LayoutItemSize w; |
| 80 |
LayoutItemSize h; |
| 81 |
bool ret = MapPosition(this->posX, this->posY, this->width, this->height, this->rotation, x, y, w, h); |
| 82 |
if (ret == false) |
| 83 |
{ |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
for (LayoutPosition ix = x; ix < x + w; ix++) |
| 88 |
{ |
| 89 |
for (LayoutPosition iy = y; iy < y + h; iy++) |
| 90 |
{ |
| 91 |
if (ix == posX && iy == posY) |
| 92 |
{ |
| 93 |
return false; |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
std::string LayoutItem::Serialize() const |
| 101 |
{ |
| 102 |
stringstream ss; |
| 103 |
ss << Object::Serialize() |
| 104 |
<< ";visible=" << static_cast<int>(visible) |
| 105 |
<< ";posX=" << static_cast<int>(posX) |
| 106 |
<< ";posY=" << static_cast<int>(posY) |
| 107 |
<< ";posZ=" << static_cast<int>(posZ) |
| 108 |
<< ";width=" << static_cast<int>(width) |
| 109 |
<< ";height=" << static_cast<int>(height) |
| 110 |
<< ";rotation=" << static_cast<int>(rotation); |
| 111 |
return ss.str(); |
| 112 |
} |
| 113 |
|
| 114 |
bool LayoutItem::Deserialize(const std::string& serialized) |
| 115 |
{ |
| 116 |
map<string,string> arguments; |
| 117 |
ParseArguments(serialized, arguments); |
| 118 |
return Deserialize(arguments); |
| 119 |
} |
| 120 |
|
| 121 |
bool LayoutItem::Deserialize(const map<string,string>& arguments) |
| 122 |
{ |
| 123 |
Object::Deserialize(arguments); |
| 124 |
visible = static_cast<Visible>(Utils::Utils::GetIntegerMapEntry(arguments, "visible")); |
| 125 |
if (visible > VisibleYes) |
| 126 |
{ |
| 127 |
visible = VisibleYes; |
| 128 |
} |
| 129 |
posX = Utils::Utils::GetIntegerMapEntry(arguments, "posX", 0); |
| 130 |
posY = Utils::Utils::GetIntegerMapEntry(arguments, "posY", 0); |
| 131 |
posZ = Utils::Utils::GetIntegerMapEntry(arguments, "posZ", 0); |
| 132 |
width = Utils::Utils::GetIntegerMapEntry(arguments, "width", Width1); |
| 133 |
height = Utils::Utils::GetIntegerMapEntry(arguments, "height", Height1); |
| 134 |
rotation = Utils::Utils::GetIntegerMapEntry(arguments, "rotation", Rotation0); |
| 135 |
if (rotation > Rotation270) |
| 136 |
{ |
| 137 |
rotation = Rotation0; |
| 138 |
} |
| 139 |
return true; |
| 140 |
} |
| 141 |
|
| 142 |
std::string LayoutItem::Rotation(LayoutRotation rotation) |
| 143 |
{ |
| 144 |
switch (rotation) |
| 145 |
{ |
| 146 |
case Rotation90: |
| 147 |
return "90"; |
| 148 |
|
| 149 |
case Rotation180: |
| 150 |
return "180"; |
| 151 |
|
| 152 |
case Rotation270: |
| 153 |
return "270"; |
| 154 |
|
| 155 |
case Rotation0: |
| 156 |
default: |
| 157 |
return "0"; |
| 158 |
} |
| 159 |
} |
| 160 |
} // namespace DataModel |
| 161 |
|