Skyscraper 2.0
buttonpanel.cpp
Go to the documentation of this file.
1/*
2 Scalable Building Simulator - Elevator Button Panel Object
3 The Skyscraper Project - Version 2.0
4 Copyright (C)2004-2024 Ryan Thoryk
5 https://www.skyscrapersim.net
6 https://sourceforge.net/projects/skyscraper/
7 Contact - ryan@skyscrapersim.net
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22*/
23
24#include "globals.h"
25#include "sbs.h"
26#include "mesh.h"
27#include "texture.h"
28#include "elevatorcar.h"
29#include "action.h"
30#include "control.h"
31#include "callstation.h"
32#include "buttonpanel.h"
33
34namespace SBS {
35
36ButtonPanel::ButtonPanel(Object *parent, int index, const std::string &texture, int rows, int columns, const std::string &direction, Real CenterX, Real CenterZ, Real buttonwidth, Real buttonheight, Real spacingX, Real spacingY, Real voffset, Real tw, Real th, bool autosize) : Object(parent)
37{
38 //Create an elevator button panel
39 //index is for specifying multiple panels within the same elevator
40
41 //set up SBS object
42 SetValues("ButtonPanel", "", false);
43
44 IsEnabled = true;
45 Index = index;
46 Direction = direction;
47 ButtonWidth = buttonwidth;
48 ButtonHeight = buttonheight;
49 Rows = rows;
50 Columns = columns;
51 SpacingX = ButtonWidth * spacingX;
52 SpacingY = ButtonHeight * spacingY;
53 off_action = 0;
54
55 SetCase(Direction, false);
57
58 //total spacing plus total button widths
59 Width = ((Columns + 1) * SpacingX) + (Columns * ButtonWidth);
60 Height = ((Rows + 1) * SpacingY) + (Rows * ButtonHeight);
61
62 //create mesh
63 std::string name = "Button Panel " + ToString(index);
64 SetName(name);
65 mesh = new MeshObject(this, name, 0, "", "", sbs->GetConfigFloat("Skyscraper.SBS.MaxSmallRenderDistance", 100));
66
67 //create panel back
68 if (texture != "")
69 {
71 if (Direction == "front")
72 {
73 sbs->DrawWalls(true, false, false, false, false, false);
74 AddWall("Panel", texture, 0, -(Width / 2), 0, Width / 2, 0, Height, Height, 0, 0, tw, th, autosize);
75 }
76 if (Direction == "back")
77 {
78 sbs->DrawWalls(false, true, false, false, false, false);
79 AddWall("Panel", texture, 0, -(Width / 2), 0, Width / 2, 0, Height, Height, 0, 0, tw, th, autosize);
80 }
81 if (Direction == "left")
82 {
83 sbs->DrawWalls(true, false, false, false, false, false);
84 AddWall("Panel", texture, 0, 0, -(Width / 2), 0, Width / 2, Height, Height, 0, 0, tw, th, autosize);
85 }
86 if (Direction == "right")
87 {
88 sbs->DrawWalls(false, true, false, false, false, false);
89 AddWall("Panel", texture, 0, 0, -(Width / 2), 0, Width / 2, Height, Height, 0, 0, tw, th, autosize);
90 }
91 sbs->ResetWalls();
93 }
94
95 //set position of object
96 Move(CenterX, voffset - (Height / 2), CenterZ);
97}
98
100{
101 //delete controls
102 for (size_t i = 0; i < controls.size(); i++)
103 {
104 if (controls[i])
105 {
106 controls[i]->parent_deleting = true;
107 delete controls[i];
108 }
109 controls[i] = 0;
110 }
111 //delete panel
112 if (mesh)
113 {
114 mesh->parent_deleting = true;
115 delete mesh;
116 }
117 mesh = 0;
118
119 if (sbs->FastDelete == false)
120 {
121 //unregister with parent object
122 if (parent_deleting == false)
123 {
124 std::string type = GetParent()->GetType();
125
126 if (type == "ElevatorCar")
127 static_cast<ElevatorCar*>(GetParent())->RemovePanel(this);
128
129 if (type == "CallStation")
130 static_cast<CallStation*>(GetParent())->RemovePanel();
131 }
132
133 //remove associated actions
134 for (size_t i = 0; i < action_list.size(); i++)
136 }
137}
138
139Control* ButtonPanel::AddButton(const std::string &sound, const std::string &texture, const std::string &texture_lit, int row, int column, const std::string &type, Real width, Real height, Real hoffset, Real voffset)
140{
141 //create a standard button at specified row/column position
142 //width and height are the button size percentage that the button should be (divided by 100); default is 1 for each, aka 100%.
143 //hoffset and voffset are the optional horizontal and vertical offsets to place the button (in order to place it outside of the default grid position)
144
145 //type is an elevator action, defined in action.cpp
146
147 std::vector<std::string> textures, names;
148 std::string newtype = type;
149 SetCase(newtype, false);
150
151 //switch 'stop' to 'estop' for compatibility
152 if (newtype == "stop")
153 newtype = "estop";
154
155 textures.emplace_back(texture);
156 textures.emplace_back(texture_lit);
157 sbs->GetTextureManager()->EnableLighting(texture_lit, false);
158
159 if (IsNumeric(newtype) == true)
160 {
161 names.emplace_back("off");
162 names.emplace_back(newtype);
163 }
164 else
165 names.emplace_back(newtype);
166
167 return AddControl(sound, row, column, width, height, hoffset, voffset, 1, names, textures);
168}
169
170Control* ButtonPanel::AddControl(const std::string &sound, int row, int column, Real bwidth, Real bheight, Real hoffset, Real voffset, int selection_position, std::vector<std::string> &action_names, std::vector<std::string> &textures)
171{
172 //create an elevator control (button, switch, knob)
173
174 Vector3 position = Vector3::ZERO;
175
176 //set to default if value is 0
177 if (bwidth == 0)
178 bwidth = 1;
179
180 if (bheight == 0)
181 bheight = 1;
182
183 //vertical position is the top of the panel, minus the total spacing above it,
184 //minus the total button spaces above (and including) it, minus half of the extra height multiplier
185 position.y = Height - (SpacingY * row) - (ButtonHeight * row) - ((ButtonHeight * (bheight - 1)) / 2);
186 position.y += voffset * ButtonHeight;
187
188 if (Direction == "front")
189 {
190 //horizontal position is the left-most edge of the panel (origin aka center minus
191 //half the width), plus total spacing to the left of it, plus total button spaces
192 //to the left of it, plus half of the extra width multiplier
193 position.x = (-Width / 2) + (SpacingX * column) + (ButtonWidth * (column - 1)) - ((ButtonWidth * (bwidth - 1)) / 2);
194 position.z = -0.01;
195 position.x += hoffset * ButtonWidth;
196 }
197 if (Direction == "back")
198 {
199 //back
200 position.x = (Width / 2) - (SpacingX * column) - (ButtonWidth * (column - 1)) + ((ButtonWidth * (bwidth - 1)) / 2);
201 position.z = 0.01;
202 position.x -= hoffset * ButtonWidth;
203 }
204 if (Direction == "left")
205 {
206 position.x = -0.01;
207 position.z = (Width / 2) - (SpacingX * column) - (ButtonWidth * (column - 1)) + ((ButtonWidth * (bwidth - 1)) / 2);
208 position.z -= hoffset * ButtonWidth;
209 }
210 if (Direction == "right")
211 {
212 //right
213 position.x = 0.01;
214 position.z = (-Width / 2) + (SpacingX * column) + (ButtonWidth * (column - 1)) - ((ButtonWidth * (bwidth - 1)) / 2);
215 position.z += hoffset * ButtonWidth;
216 }
217
218 //create control object
219 controls.resize(controls.size() + 1);
220 int control_index = (int)controls.size() - 1;
221 std::string name = GetName() + ": Control " + ToString(control_index);
222 TrimString(name);
223
224 //register actions
225 std::vector<Action*> actions;
226 std::vector<std::string> actionsnull; //not used
227
228 for (size_t i = 0; i < action_names.size(); i++)
229 {
230 std::string newname = GetParent()->GetName() + ":" + action_names[i];
231 std::vector<Object*> parents;
232 parents.emplace_back(GetParent());
233 if ((off_action == 0 && action_names[i] == "off") || action_names[i] != "off")
234 {
235 Action* action = sbs->AddAction(newname, parents, action_names[i]);
236 actions.emplace_back(action);
237 action_list.emplace_back(action);
238 if (action_names[i] == "off")
239 off_action = action;
240 }
241 else
242 actions.emplace_back(off_action);
243 }
244
245 Control *control = controls[control_index] = new Control(this, name, false, sound, actionsnull, actions, textures, Direction, ButtonWidth * bwidth, ButtonHeight * bheight, false, selection_position);
246
247 //move control
248 controls[control_index]->Move(position);
249
250 return control;
251}
252
253void ButtonPanel::Enabled(bool value)
254{
255 //enable or disable button panel
256
257 if (IsEnabled == value)
258 return;
259
260 mesh->Enabled(value);
261
262 for (size_t i = 0; i < controls.size(); i++)
263 {
264 controls[i]->Enabled(value);
265 }
266 IsEnabled = value;
267}
268
269bool ButtonPanel::AddWall(const std::string &name, const std::string &texture, Real thickness, Real x1, Real z1, Real x2, Real z2, Real height1, Real height2, Real voffset1, Real voffset2, Real tw, Real th, bool autosize)
270{
271 //Adds a wall with the specified dimensions
272
273 Wall *wall = mesh->CreateWallObject(name);
274 return sbs->AddWallMain(wall, name, texture, thickness, x1, z1, x2, z2, height1, height2, voffset1, voffset2, tw, th, autosize);
275}
276
277void ButtonPanel::ChangeLight(int floor, bool value)
278{
279 //change light status for all buttons that call the specified floor
280
281 for (size_t i = 0; i < controls.size(); i++)
282 {
283 controls[i]->ChangeFloorLight(floor, value);
284 }
285}
286
288{
289 //turns on or off all lights (for floor buttons)
290
291 for (size_t i = 0; i < controls.size(); i++)
292 {
293 controls[i]->ChangeLight(value);
294 }
295}
296
298{
299 //return the first control found for the specified floor
300
301 return GetControl(ToString(floor));
302}
303
305{
306 if (index < 0 || index > (int)controls.size() - 1)
307 return 0;
308
309 return controls[index];
310}
311
312Control* ButtonPanel::GetControl(const std::string &name)
313{
314 //return the first control found that has an action that matches 'name'
315
316 if (controls.empty() == false)
317 {
318 for (size_t i = 0; i < controls.size(); i++)
319 {
320 if (controls[i]->FindActionPosition(name) > 0)
321 return controls[i];
322 }
323 }
324 return 0;
325}
326
328{
329 //remove a control from the array (does not delete the object)
330 for (size_t i = 0; i < controls.size(); i++)
331 {
332 if (controls[i] == control)
333 {
334 controls.erase(controls.begin() + i);
335 return;
336 }
337 }
338}
339
341{
342 return (int)controls.size();
343}
344
345void ButtonPanel::SetControls(const std::string &action_name)
346{
347 //set all controls that have an action specified by 'name',
348 //to that action's selection position
349
350 if (controls.empty() == true)
351 return;
352
353 for (size_t i = 0; i < controls.size(); i++)
354 {
355 int index = controls[i]->FindActionPosition(action_name);
356 if (index > 0 && controls[i]->GetSelectPosition() != index)
357 controls[i]->SetSelectPosition(index);
358 }
359}
360
361}
ButtonPanel(Object *parent, int index, const std::string &texture, int rows, int columns, const std::string &direction, Real CenterX, Real CenterZ, Real buttonwidth, Real buttonheight, Real spacingX, Real spacingY, Real voffset, Real tw, Real th, bool autosize=true)
Action * off_action
Definition buttonpanel.h:64
bool AddWall(const std::string &name, const std::string &texture, Real thickness, Real x1, Real z1, Real x2, Real z2, Real height1, Real height2, Real voffset1, Real voffset2, Real tw, Real th, bool autosize)
std::string Direction
Definition buttonpanel.h:34
std::vector< Action * > action_list
Definition buttonpanel.h:66
void RemoveControl(Control *control)
Control * AddButton(const std::string &sound, const std::string &texture, const std::string &texture_lit, int row, int column, const std::string &type, Real width, Real height, Real hoffset=0, Real voffset=0)
void Enabled(bool value)
void ChangeAllLights(bool value)
void ChangeLight(int floor, bool value)
Control * AddControl(const std::string &sound, int row, int column, Real bwidth, Real bheight, Real hoffset, Real voffset, int selection_position, std::vector< std::string > &action_names, std::vector< std::string > &textures)
MeshObject * mesh
Definition buttonpanel.h:63
Control * GetFloorButton(int floor)
std::vector< Control * > controls
Definition buttonpanel.h:65
Control * GetControl(int index)
void SetControls(const std::string &action_name)
int FindActionPosition(const std::string &name)
Definition control.cpp:330
void Enabled(bool value)
Definition mesh.cpp:154
Wall * CreateWallObject(const std::string &name)
Definition mesh.cpp:208
const std::string & GetName()
Definition object.cpp:53
Object * GetParent()
Definition object.cpp:42
void SetName(const std::string &name)
Definition object.cpp:72
bool parent_deleting
Definition object.h:64
virtual void Move(const Vector3 &vector, Real speed=1.0)
Definition object.cpp:253
void SetValues(const std::string &type, const std::string &name, bool is_permanent, bool is_movable=true)
Definition object.cpp:144
const std::string & GetType()
Definition object.cpp:177
Action * AddAction(const std::string &name, std::vector< Object * > &action_parents, const std::string &command, const std::vector< std::string > &parameters)
Definition sbs.cpp:3413
Real GetConfigFloat(const std::string &key, Real default_value)
Definition sbs.cpp:3249
bool AddWallMain(Wall *wallobject, const std::string &name, const std::string &texture, Real thickness, Real x1, Real z1, Real x2, Real z2, Real height_in1, Real height_in2, Real altitude1, Real altitude2, Real tw, Real th, bool autosize)
Definition sbs.cpp:690
void ResetWalls(bool ToDefaults=false)
Definition sbs.cpp:1854
bool FastDelete
Definition sbs.h:188
TextureManager * GetTextureManager()
Definition sbs.cpp:4558
bool RemoveAction(std::string name)
Definition sbs.cpp:3515
void DrawWalls(bool MainN, bool MainP, bool SideN, bool SideP, bool Top, bool Bottom)
Definition sbs.cpp:1833
void ResetTextureMapping(bool todefaults=false)
Definition texture.cpp:1444
void EnableLighting(const std::string &material_name, bool value)
Definition texture.cpp:2207
Ogre::Vector3 Vector3
Definition globals.h:58
Ogre::Real Real
Definition globals.h:57
void SetCase(std::string &string, bool uppercase)
Definition globals.cpp:172
std::string ToString(int number)
Definition globals.cpp:279
bool IsNumeric(char character)
Definition globals.cpp:48
void TrimString(std::string &string)
Definition globals.cpp:188