AnimatLab  2
Test
SimulationWindowMgr.cpp
Go to the documentation of this file.
1 
7 #include "StdAfx.h"
8 #include "IMovableItemCallback.h"
9 #include "ISimGUICallback.h"
10 #include "AnimatBase.h"
11 
12 #include "Gain.h"
13 #include "Node.h"
14 #include "Link.h"
15 #include "IPhysicsMovableItem.h"
16 #include "IPhysicsBody.h"
17 #include "BoundingBox.h"
18 #include "MovableItem.h"
19 #include "BodyPart.h"
20 #include "Joint.h"
21 #include "ReceptiveField.h"
22 #include "ContactSensor.h"
23 #include "RigidBody.h"
24 #include "Structure.h"
25 #include "NeuralModule.h"
26 #include "Adapter.h"
27 #include "NervousSystem.h"
28 #include "Organism.h"
29 #include "ActivatedItem.h"
30 #include "ActivatedItemMgr.h"
31 #include "DataColumn.h"
32 #include "DataChart.h"
33 #include "DataChartMgr.h"
34 #include "ExternalStimuliMgr.h"
35 #include "KeyFrame.h"
36 #include "SimulationRecorder.h"
37 #include "OdorType.h"
38 #include "Odor.h"
39 #include "SimulationWindow.h"
40 #include "HudItem.h"
41 #include "Hud.h"
42 #include "SimulationWindowMgr.h"
43 #include "Light.h"
44 #include "LightManager.h"
45 #include "Simulator.h"
46 
47 
48 namespace AnimatSim
49 {
50 
58 {
59  m_lpHudMgr = NULL;
60 }
61 
69 {
70 
71 try
72 {
73  Close();
74 }
75 catch(...)
76 {Std_TraceMsg(0, "Caught Error in desctructor of SimulationWindowMgr\r\n", "", -1, false, true);}
77 }
78 
80 {
82 
83  if(m_lpHudMgr)
85 }
86 
101 SimulationWindow *SimulationWindowMgr::AddSimulationWindow(std::string strModule, std::string strType, bool bInit, HWND win, std::string strXml)
102 {
103  SimulationWindow *lpWin=NULL;
104 
105 try
106 {
107  //Create a simulation window from the graphics library being used
108  lpWin = dynamic_cast<SimulationWindow *>(m_lpSim->CreateObject(strModule, "SimulationWindow", strType));
109 
110  lpWin->SetSystemPointers(m_lpSim, NULL, NULL, NULL, true);
111  lpWin->Load(strXml);
112 
113  //initialize the window
114  lpWin->WindowID(win);
115  if(bInit)
116  {
117  lpWin->Initialize();
118  lpWin->OnGetFocus();
119  }
120 
121  //add the window to the list of windows
122  m_aryWindows.Add(lpWin);
123 
124  return lpWin;
125 }
126 catch(CStdErrorInfo oError)
127 {
128  if(lpWin) delete lpWin;
129  RELAY_ERROR(oError);
130  return NULL;
131 }
132 catch(...)
133 {
134  if(lpWin) delete lpWin;
135  THROW_ERROR(Std_Err_lUnspecifiedError, Std_Err_strUnspecifiedError);
136  return NULL;
137 }
138 }
139 
147 {
148  int iCount = m_aryWindows.GetSize();
149  for(int iIndex=0; iIndex<iCount; iIndex++)
151 }
152 
165 SimulationWindow *SimulationWindowMgr::FindSimulationWindow(HWND win, int &iIndex, bool bThrowError)
166 {
167  int iCount = m_aryWindows.GetSize();
168  for(int iIdx=0; iIdx<iCount; iIdx++)
169  {
170  if(m_aryWindows[iIdx]->WindowID() == win)
171  {
172  iIndex = iIdx;
173  return m_aryWindows[iIdx];
174  }
175  }
176 
177  iIndex = -1;
178  if(bThrowError)
179  THROW_ERROR(Al_Err_lSimWindowNotFound, Al_Err_strSimWindowNotFound);
180  return NULL;
181 
182 }
183 
193 {
194  int iIndex = 0;
195  SimulationWindow *lpWin = FindSimulationWindow(win, iIndex, false);
196 
197  if(lpWin)
198  {
199  lpWin->Close();
200  m_aryWindows.RemoveAt(iIndex);
201  }
202 }
203 
213 {
214  int iCount = m_aryWindows.GetSize();
215 
216  //First update the hud item data.
217  if(iCount > 0)
218  {
219  m_lpHudMgr->Update();
220 
221  for(int iIndex=0; iIndex<iCount; iIndex++)
222  m_aryWindows[iIndex]->Update();
223  }
224 
225  return true;
226 }
227 
235 {
236  if(m_lpHudMgr)
237  {
238  m_lpHudMgr->Reset();
239  delete m_lpHudMgr;
240  m_lpHudMgr = NULL;
241  }
242 
243  CloseAllWindows();
244 }
245 
253 {
254  int iCount = m_aryWindows.GetSize();
255  for(int iIndex=0; iIndex<iCount; iIndex++)
256  m_aryWindows[iIndex]->Close();
257  m_aryWindows.clear();
258 }
259 
267 {
268  int iCount = m_aryWindows.GetSize();
269  for(int iIndex=0; iIndex<iCount; iIndex++)
270  m_aryWindows[iIndex]->SetupTrackCamera(true);
271 }
272 
282 {
283  int iCount = m_aryWindows.GetSize();
284  for(int iIndex=0; iIndex<iCount; iIndex++)
285  {
286  if(!m_aryWindows[iIndex]->StandAlone())
287  return true;
288  }
289  return false;
290 }
291 
293 {
295 
296  int iCount = m_aryWindows.GetSize();
297  for(int iIndex=0; iIndex<iCount; iIndex++)
298  m_aryWindows[iIndex]->ResetSimulation();
299 }
300 
301 void SimulationWindowMgr::Load(CStdXml &oXml)
302 {
304 
305  Close();
306 
307  if(oXml.FindChildElement("WindowMgr", false))
308  {
309  AnimatBase::Load(oXml);
310 
311  //If the user has specified to not create any visible graphics windows then lets skip loading them.
312  if(!m_lpSim->ForceNoWindows())
313  {
314  oXml.IntoElem(); //Into WindowMgr Element
315 
316  if(oXml.FindChildElement("Position", false))
317  {
318  Std_LoadPoint(oXml, "Position", m_ptPosition);
319  if(m_ptPosition.x < 0 || m_ptPosition.y < 0)
320  THROW_TEXT_ERROR(Al_Err_lSimWinPosInvalid, Al_Err_strSimWinPosInvalid, "POS: (" + STR(m_ptPosition.x) + ", " + STR(m_ptPosition.y) + ")");
321  }
322 
323  if(oXml.FindChildElement("Size", false))
324  {
325  Std_LoadPoint(oXml, "Size", m_ptSize);
326  if(m_ptSize.x < 0 || m_ptSize.y < 0)
327  THROW_TEXT_ERROR(Al_Err_lSimWinSizeInvalid, Al_Err_strSimWinSizeInvalid, "Size: (" + STR(m_ptSize.x) + ", " + STR(m_ptSize.y) + ")");
328  }
329 
330  if(oXml.FindChildElement("Windows", false))
331  {
332  oXml.IntoChildElement("Windows"); //There must be a windows section if we get here.
333  int iCount = oXml.NumberOfChildren();
334  SimulationWindow *lpItem = NULL;
335  for(int iIndex=0; iIndex<iCount; iIndex++)
336  {
337  oXml.FindChildByIndex(iIndex);
338  lpItem = LoadSimulationWindow(oXml);
339  m_aryWindows.Add(lpItem);
340  }
341  oXml.OutOfElem(); //OutOf Windows Element
342  }
343 
344  if(oXml.FindChildElement("Hud", false))
345  {
346  //Create and load the HUD manager
347  oXml.IntoElem();
348  std::string strModuleName = oXml.GetChildString("ModuleName", "");
349  std::string strType = oXml.GetChildString("Type");
350  oXml.OutOfElem();
351 
352  m_lpHudMgr = dynamic_cast<Hud *>(m_lpSim->CreateObject(strModuleName, "Hud", strType));
353 
354  m_lpHudMgr->SetSystemPointers(m_lpSim, NULL, NULL, NULL, true);
355  m_lpHudMgr->Load(oXml);
356  }
357  }
358 
359  oXml.OutOfElem(); //OutOf WindowMgr Element
360  }
361 }
362 
374 {
375  SimulationWindow *lpWin=NULL;
376  std::string strModuleName, strType;
377 
378 try
379 {
380  oXml.IntoElem(); //Into Column Element
381  strModuleName = oXml.GetChildString("ModuleName", "");
382  strType = oXml.GetChildString("Type");
383  oXml.OutOfElem(); //OutOf Column Element
384 
385  lpWin = dynamic_cast<SimulationWindow *>(m_lpSim->CreateObject(strModuleName, "SimulationWindow", strType));
386  if(!lpWin)
387  THROW_TEXT_ERROR(Al_Err_lConvertingClassToType, Al_Err_strConvertingClassToType, "SimulationWindow");
388 
389  lpWin->SetSystemPointers(m_lpSim, NULL, NULL, NULL, true);
390  lpWin->Load(oXml);
391 
392  return lpWin;
393 }
394 catch(CStdErrorInfo oError)
395 {
396  if(lpWin) delete lpWin;
397  RELAY_ERROR(oError);
398  return NULL;
399 }
400 catch(...)
401 {
402  if(lpWin) delete lpWin;
403  THROW_ERROR(Std_Err_lUnspecifiedError, Std_Err_strUnspecifiedError);
404  return NULL;
405 }
406 }
407 
408 }// end AnimatSim
virtual void ResetSimulation()
Resets the simulation back to time 0.
Definition: Hud.cpp:73
Base class file for all Animat simulation objects.
virtual void ResetSimulation()
Resets the simulation back to time 0.
Declares the nervous system class.
Declares the simulation recorder class.
virtual void SetSystemPointers(Simulator *lpSim, Structure *lpStructure, NeuralModule *lpModule, Node *lpNode, bool bVerify)
Sets the system pointers.
CStdFPoint m_ptPosition
If this is a stand-alone group window then this is the position of the group window.
virtual bool FindChildElement(std::string strElementName, bool fThrowError=true)
Finds a child element by name.
Definition: StdXml.cpp:256
Root namespace for the base simulation library for AnimatLab.
Declares the body part class.
Simulator * m_lpSim
The pointer to a Simulation.
Definition: AnimatBase.h:43
virtual void UpdateBackgroundColor()
Updates the background color for all simulation windows.
Information about the standard error.
Definition: StdErrorInfo.h:19
virtual bool IntoElem()
Goes into the next element where the cursor is located.
Definition: StdXml.cpp:42
Base class for a simulation window.
SimulationWindow * LoadSimulationWindow(CStdXml &oXml)
Loads a simulation window.
virtual void Initialize()
Initializes this object.
Definition: AnimatBase.cpp:573
virtual bool HasContainedWindow()
Query if this object has contained window.
Declares the simulation window manager class.
Declares the key frame class.
Declares the joint class.
Declares the organism class.
Declares a light object.
CStdPtrArray< SimulationWindow > m_aryWindows
Array of SimulationWindow objects.
Hud * m_lpHudMgr
Manager for heads-up display items.
CStdFPoint m_ptSize
If this is a stand-alone group window then this is the size of the group window.
Declares the activated item class.
Declares the data column class.
virtual CStdSerialize * CreateObject(std::string strModule, std::string strClassName, std::string strType, bool bThrowError=true)
Creates an object using a class factory.
Definition: Simulator.cpp:3440
Declares a light manager object.
Declares the simulation Windows Form.
Declares the bounding box class.
virtual void RemoveSimulationWindow(HWND win)
Removes the simulation window described by the HWND win.
Declares the gain base class.
virtual void Reset()
Resets this object.
Definition: Hud.cpp:68
virtual void Close()
Closes all windows.
virtual void Initialize()=0
Initializes this object.
A standard xml manipulation class.
Definition: StdXml.h:19
Declares the node class.
virtual std::string GetChildString(std::string strElementName)
Gets a string value from the element with the specified name.
Definition: StdXml.cpp:307
virtual void Initialize()
Initializes this object.
virtual void CloseAllWindows()
Closes all windows internally.
virtual void Load(StdUtils::CStdXml &oXml)
Loads the item using an XML data packet.
Definition: AnimatBase.cpp:771
void Std_TraceMsg(const int iLevel, std::string strMessage, std::string strSourceFile, int iSourceLine, bool bLogToFile, bool bPrintHeader)
Traces a message to the debugger window.
virtual int NumberOfChildren()
Gets the number of children of the current element.
Definition: StdXml.cpp:202
virtual bool OutOfElem()
Goes out of the element where the cursor is located.
Definition: StdXml.cpp:56
Declares the data chart class.
virtual void OnGetFocus()
Called by the GUI when this window gets the focus.
SimulationWindowMgr(void)
Default constructor.
virtual SimulationWindow * FindSimulationWindow(HWND win, int &iIndex, bool bThrowError=true)
Searches for simulation window with the specified HWND.
Declares the data chart manager class.
Declares the rigid body class.
virtual bool Update()
Updates all of the windows.
virtual bool IntoChildElement(std::string strElementName, bool bThrowError=true)
Goes into the child element with the specified name.
Definition: StdXml.cpp:278
bool Std_LoadPoint(CStdXml &oXml, std::string strName, CStdIPoint &oPoint, bool bThrowError)
Standard load point.
Declares the structure class.
Declares the odor type class.
virtual void SetupCameras()
Sets up the cameras for each window.
virtual SimulationWindow * AddSimulationWindow(std::string strModule, std::string strType, bool bInit, HWND win, std::string strHudXml)
Adds a simulation window.
virtual void VerifySystemPointers()
Verify that system pointers have been set correctly.
Definition: AnimatBase.cpp:315
virtual void Close()=0
Closes this window.
Declares the odor class.
Declares the simulator class.
Declares the neural module class.
virtual bool FindChildByIndex(int iIndex, bool bThrowError=true)
Finds a child element by index.
Definition: StdXml.cpp:225
Declares the activated item manager class.
virtual HWND WindowID()
Gets the HWND window identifier.
Declares the contact sensor class.
Declares the external stimuli manager class.
Declares the receptive field class.