| 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 <sstream> |
| 22 | |
| 23 | #include "DataModel/Accessory.h" |
| 24 | #include "Server/Web/HtmlTagAccessory.h" |
| 25 | |
| 26 | using std::string; |
| 27 | using std::to_string; |
| 28 | |
| 29 | namespace Server { namespace Web |
| 30 | { |
| 31 | HtmlTagAccessory::HtmlTagAccessory(const DataModel::Accessory* accessory) |
| 32 | : HtmlTagLayoutItem(dynamic_cast<const DataModel::LayoutItem*>(accessory)) |
| 33 | { |
| 34 | const DataModel::AccessoryType accessoryType = static_cast<DataModel::AccessoryType>(accessory->GetAccessoryType() & DataModel::AccessoryTypeMask); |
| 35 | switch (accessoryType) |
| 36 | { |
| 37 | case DataModel::AccessoryTypeStraight: |
| 38 | case DataModel::AccessoryTypeDecoupler: |
| 39 | image = "<polygon class=\"track\" points=\"15,0 21,0 21,36 15,36\"/>"; |
| 40 | break; |
| 41 | |
| 42 | case DataModel::AccessoryTypeTurn: |
| 43 | image = "<polygon class=\"track\" points=\"0,21 0,15 21,36 15,36\"/>"; |
| 44 | break; |
| 45 | |
| 46 | case DataModel::AccessoryTypeDefault: |
| 47 | case DataModel::AccessoryTypeLight: |
| 48 | default: |
| 49 | break; |
| 50 | } |
| 51 | switch (accessoryType) |
| 52 | { |
| 53 | case DataModel::AccessoryTypeDefault: |
| 54 | case DataModel::AccessoryTypeStraight: |
| 55 | case DataModel::AccessoryTypeTurn: |
| 56 | image += "<polygon class=\"accessory\" points=\"10,10 26,10 26,26 10,26\" />"; |
| 57 | break; |
| 58 | |
| 59 | case DataModel::AccessoryTypeDecoupler: |
| 60 | image += "<polygon class=\"accessory\" points=\"13,6 13,30 11,28 11,8\" />"; |
| 61 | image += "<polygon class=\"accessory\" points=\"23,6 25,8 25,28 23,30\" />"; |
| 62 | break; |
| 63 | |
| 64 | case DataModel::AccessoryTypeLight: |
| 65 | image = "<polyline points=\"15.5,22.3 14.8,21.8 14.2,21.2 13.7,20.5 13.3,19.7 13.1,18.9 13,18 13.1,17.1 13.3,16.3 13.7,15.5 14.2,14.8 14.8,14.2 15.5,13.7 16.3,13.3 17.1,13.1 18,13 18.9,13.1 19.7,13.3 20.5,13.7 21.2,14.2 21.8,14.8 22.3,15.5 22.7,16.3 22.9,17.1 23,18 22.9,18.9 22.7,19.7 22.3,20.5 21.8,21.2 21.2,21.8 20.5,22.3\" stroke=\"white\" stroke-width=\"0\" fill=\"white\"/>" |
| 66 | "<polyline points=\"15,23 21,23 21,30 18,32 15,30\" stroke=\"white\" stroke-width=\"0\" fill=\"white\"/>" |
| 67 | "<polyline points=\"10.2,22 5,25.5\" stroke=\"white\" stroke-width=\"2\" class=\"accessory_on\"/>" |
| 68 | "<polyline points=\"9,18 3,18\" stroke=\"white\" stroke-width=\"2\" class=\"accessory_on\"/>" |
| 69 | "<polyline points=\"10.2,13.5 5,10.5\" stroke=\"white\" stroke-width=\"2\" class=\"accessory_on\"/>" |
| 70 | "<polyline points=\"13.5,10.2 10.5,5\" stroke=\"white\" stroke-width=\"2\" class=\"accessory_on\"/>" |
| 71 | "<polyline points=\"18,9 18,3\" stroke=\"white\" stroke-width=\"2\" class=\"accessory_on\"/>" |
| 72 | "<polyline points=\"22.5,10.2 25.5,5\" stroke=\"white\" stroke-width=\"2\" class=\"accessory_on\"/>" |
| 73 | "<polyline points=\"25.8,13.5 31,10.5\" stroke=\"white\" stroke-width=\"2\" class=\"accessory_on\"/>" |
| 74 | "<polyline points=\"27,18 33,18\" stroke=\"white\" stroke-width=\"2\" class=\"accessory_on\"/>" |
| 75 | "<polyline points=\"25.8,22.5 31,25.5\" stroke=\"white\" stroke-width=\"2\" class=\"accessory_on\"/>"; |
| 76 | break; |
| 77 | |
| 78 | case DataModel::AccessoryTypeLightInhouse: |
| 79 | image = //"<polyline points=\"3,3 1.5,3.5 1,5 1,33 1.5,34.5 3,35 33,35 34.5,34.5 35,33 35,5 34.5,3.5 33,3 3,3\" stroke=\"white\" stroke-width=\"1\" fill=\"none\"/>" |
| 80 | "<polyline points=\"0,10.5 18,0.5 36,10.5\" stroke=\"white\" stroke-width=\"1\" fill=\"none\"/>" |
| 81 | "<polyline points=\"3.5,35.5 3.5,8.5 32.5,8.5 32.5,35.5 3.5,35.5\" stroke=\"white\" stroke-width=\"1\" fill=\"none\"/>" |
| 82 | "<polyline points=\"23,10 22.9,10.9 22.7,11.7 22.3,12.5 21.8,13.2 21.2,13.8 20.5,14.3 19.7,14.7 18.9,14.9 18,15 17.1,14.9 16.3,14.7 15.5,14.3 14.8,13.8 14.2,13.2 13.7,12.5 13.3,11.7 13.1,10.9 13,10\" stroke=\"white\" stroke-width=\"0\" fill=\"white\"/>" |
| 83 | "<polyline points=\"25.8,14.5 31,17.5\" stroke=\"white\" stroke-width=\"2\" class=\"accessory_on\"/>" |
| 84 | "<polyline points=\"22.5,17.8 25.5,23\" stroke=\"white\" stroke-width=\"2\" class=\"accessory_on\"/>" |
| 85 | "<polyline points=\"18,19 18,25\" stroke=\"white\" stroke-width=\"2\" class=\"accessory_on\"/>" |
| 86 | "<polyline points=\"13.5,17.8 10.5,23\" stroke=\"white\" stroke-width=\"2\" class=\"accessory_on\"/>" |
| 87 | "<polyline points=\"10.2,14.5 5,17.5\" stroke=\"white\" stroke-width=\"2\" class=\"accessory_on\"/>"; |
| 88 | break; |
| 89 | |
| 90 | case DataModel::AccessoryTypeLightStreet: |
| 91 | image = "<polyline points=\"4.5,36 4.5,9.5 6.5,7.5 23,3.5\" stroke=\"white\" stroke-width=\"1\" fill=\"none\"/>" |
| 92 | "<polyline points=\"23,5 22.9,5.9 22.7,6.7 22.3,7.5 21.8,8.2 21.2,8.8 20.5,9.3 19.7,9.7 18.9,9.9 18,10 17.1,9.9 16.3,9.7 15.5,9.3 14.8,8.8 14.2,8.2 13.7,7.5\" stroke=\"white\" stroke-width=\"0\" fill=\"white\"/>" |
| 93 | "<polyline points=\"25.8,9.5 31,12.5\" stroke=\"white\" stroke-width=\"2\" class=\"accessory_on\"/>" |
| 94 | "<polyline points=\"22.5,12.8 25.5,18\" stroke=\"white\" stroke-width=\"2\" class=\"accessory_on\"/>" |
| 95 | "<polyline points=\"18,14 18,20\" stroke=\"white\" stroke-width=\"2\" class=\"accessory_on\"/>" |
| 96 | "<polyline points=\"13.5,12.8 10.5,18\" stroke=\"white\" stroke-width=\"2\" class=\"accessory_on\"/>"; |
| 97 | break; |
| 98 | |
| 99 | default: |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | const DataModel::AccessoryState state = accessory->GetAccessoryState(); |
| 104 | |
| 105 | string accessoryIdString = to_string(accessory->GetID()); |
| 106 | imageDiv.AddClass("accessory_item"); |
| 107 | switch (accessory->GetAccessoryType() & DataModel::AccessoryTypeConnectionMask) |
| 108 | { |
| 109 | case DataModel::AccessoryTypeOnOn: |
| 110 | case DataModel::AccessoryTypeOnOff: |
| 111 | imageDiv.AddClass(state == DataModel::AccessoryStateOn ? "accessory_on" : "accessory_off"); |
| 112 | imageDiv.AddAttribute("onclick", "return onClickAccessory(" + accessoryIdString + ");"); |
| 113 | break; |
| 114 | |
| 115 | case DataModel::AccessoryTypeOnPush: |
| 116 | imageDiv.AddClass("accessory_off"); |
| 117 | imageDiv.AddAttribute("onpointerdown", "return onPointerDownAccessory(" + accessoryIdString + ");"); |
| 118 | imageDiv.AddAttribute("onpointerup", "return onPointerUpAccessory(" + accessoryIdString + ");"); |
| 119 | break; |
| 120 | |
| 121 | default: |
| 122 | imageDiv.AddClass("accessory_off"); |
| 123 | break; |
| 124 | } |
| 125 | |
| 126 | const string& accessoryName = accessory->GetName(); |
| 127 | AddToolTip(accessoryName + " (addr=" + to_string(accessory->GetAddress()) + ")"); |
| 128 | AddContextMenuEntry(accessoryName); |
| 129 | AddContextMenuEntry(Languages::TextReleaseAccessory, "fireRequestAndForget('/?cmd=accessoryrelease&accessory=" + accessoryIdString + "');"); |
| 130 | AddContextMenuEntry(Languages::TextEditAccessory, "loadPopup('/?cmd=accessoryedit&accessory=" + accessoryIdString + "');"); |
| 131 | AddContextMenuEntry(Languages::TextDeleteAccessory, "loadPopup('/?cmd=accessoryaskdelete&accessory=" + accessoryIdString + "');"); |
| 132 | FinishInit(); |
| 133 | } |
| 134 | }} // namespace Server::Web |