PageRenderTime 40ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/Core/Dependencies/Engine/Editor.cpp

https://bitbucket.org/barakianc/nvidia-physx-and-apex-in-gge
C++ | 1485 lines | 1043 code | 181 blank | 261 comment | 148 complexity | 6bbf40da53d88618364e61b88e638b9d MD5 | raw file
  1. #include "StdAfx.h"
  2. #ifdef GLE_EDITOR
  3. // Base Includes
  4. #include "Engine.h"
  5. //#include "GameScreen.h"
  6. // Other
  7. #include <OgreMath.h>
  8. // Editor Includes
  9. #include "Editor.h"
  10. #ifdef HAVOK
  11. #include "HavokWrapper.h"
  12. #endif
  13. #ifdef PHYSX
  14. #include "PhysXObject.h"
  15. #endif
  16. #include <vector>
  17. // -------------------------------------------------------
  18. // Chain of command includes
  19. // -------------------------------------------------------
  20. #include "EditorMsgManager/IMEssageHandle.h"
  21. #include "EditorMsgManager/EditorMsgManager_ScreenActivationKeyboardInput.h"
  22. #include "EditorMsgManager/EditorMsgManager_SceneNodePRoperties.h"
  23. #include "EditorMsgManager/EditorMsgManager_SceneNodeAndManager.h"
  24. #include "EditorMsgManager/EditorMsgManager_ParticleSystem.h"
  25. #include "EditorMsgManager/EditorMsgManager_MouseInput.h"
  26. #include "EditorMsgManager/EditorMsgManager_LightComponents.h"
  27. #include "EditorMsgManager/EditorMsgManager_GLEBaseProperties.h"
  28. #include "EditorMsgManager/EditorMsgManager_GLE_Zoom.h"
  29. #include "EditorMsgManager/EditorMsgManager_GLE_Set_Object_Properties.h"
  30. #include "EditorMsgManager/EditorMsgManager_GLE_Query_SceneNode_Children.h"
  31. #include "EditorMsgManager/EditorMsgManager_GLE_Query_Object_Properties.h"
  32. #include "EditorMsgManager/EditorMsgManager_GLE_Focus.h"
  33. #include "EditorMsgManager/EditorMsgManager_GLE_Entity_Create.h"
  34. #include "EditorMsgManager/EditorMsgManager_GLE_AttachObject.h"
  35. #include "EditorMsgManager/EditorMsgManager_AnimationSystem.h"
  36. #include "EditorMsgManager/EditorMsgManager_SkyBoxSubSystem.h"
  37. // Debug output
  38. #define GLE_DEBUG_NETWORK
  39. using namespace std;
  40. namespace GamePipe
  41. {
  42. /*! Constructor
  43. */
  44. Editor::Editor()
  45. {
  46. // Init some variables that the editor needs
  47. m_pSceneNodeSelected = NULL;
  48. // Default values for the editor
  49. SetEditorMode(false);
  50. SetUseEditorInput(false);
  51. SetEditorConnectionStatus (GGE_DISCONNECTED);
  52. // Input
  53. ed_MouseState.clear();
  54. ed_KeyCode = 100000;
  55. ed_KeyPress = false;
  56. // Editor cameras speed
  57. m_fCameraMovementSpeed = 0.05f;
  58. m_fCameraRotationSpeed = 0.13f;
  59. // Animations
  60. m_CurrentAnimationName = "";
  61. m_sMVCurrentGameObjectName = "";
  62. m_pMVGameObject = NULL;
  63. // Init message handles
  64. InitMessageHandels();
  65. }
  66. /*! Destructor
  67. */
  68. Editor::~Editor()
  69. {
  70. }
  71. /*! This function initializes the chain of command system that handles the messages as they income
  72. */
  73. void Editor::InitMessageHandels()
  74. {
  75. m_vMessageHandles.push_back(new EditorMsgManager_ScreenActivationKeyboardInput(this));
  76. m_vMessageHandles.push_back(new EditorMsgManager_SceneNodePRoperties(this));
  77. m_vMessageHandles.push_back(new EditorMsgManager_SceneNodeAndManager(this));
  78. m_vMessageHandles.push_back(new EditorMsgManager_ParticleSystem(this));
  79. m_vMessageHandles.push_back(new EditorMsgManager_MouseInput(this));
  80. m_vMessageHandles.push_back(new EditorMsgManager_LightComponents(this));
  81. m_vMessageHandles.push_back(new EditorMsgManager_GLE_Zoom(this));
  82. m_vMessageHandles.push_back(new EditorMsgManager_GLE_Set_Object_Properties(this));
  83. m_vMessageHandles.push_back(new EditorMsgManager_GLE_Query_SceneNode_Children(this));
  84. m_vMessageHandles.push_back(new EditorMsgManager_GLE_Query_Object_Properties(this));
  85. m_vMessageHandles.push_back(new EditorMsgManager_GLE_Focus(this));
  86. m_vMessageHandles.push_back(new EditorMsgManager_GLE_Entity_Create(this));
  87. m_vMessageHandles.push_back(new EditorMsgManager_GLE_AttachObject(this));
  88. m_vMessageHandles.push_back(new EditorMsgManager_AnimationSystem(this));
  89. m_vMessageHandles.push_back(new EditorMsgManager_GLEBaseProperties(this));
  90. m_vMessageHandles.push_back(new EditorMsgManager_SkyBoxSubSystem(this));
  91. }
  92. /*! This function is called every frame, it is actually in charge of changing the connection states.
  93. * @return S_OK if everything went well
  94. */
  95. int Editor::UpdateEditor()
  96. {
  97. // In charge of changing the states of the app
  98. switch( GetEditorConnectionStatus() )
  99. {
  100. case GGE_DISCONNECTED:
  101. Editor_StartServer();
  102. break;
  103. case GGE_READY_TO_ACCEPT:
  104. Editor_AcceptConnection();
  105. break;
  106. case GGE_CONNECTED:
  107. Editor_Receive();
  108. break;
  109. case GGE_ERROR:
  110. // Reset the connection and
  111. closesocket( m_sockListen );
  112. closesocket( m_sockMessages );
  113. WSACleanup();
  114. // Restart the server
  115. SetEditorConnectionStatus( GGE_DISCONNECTED );
  116. break;
  117. }
  118. //If there is an active animation
  119. if(m_pVecGameScreens->size())
  120. {
  121. //if (m_pMVGameObject != NULL)
  122. //{
  123. //m_pMVGameObject->update();
  124. //}
  125. }
  126. return S_OK;
  127. }
  128. Ogre::Entity* Editor::GetMVEntity()
  129. {
  130. if (m_pMVGameObject==NULL)
  131. {
  132. return NULL;
  133. }
  134. else
  135. {
  136. if (m_pMVGameObject->m_pGraphicsObject == NULL)
  137. {
  138. return NULL;
  139. }
  140. else
  141. {
  142. return m_pMVGameObject->m_pGraphicsObject->m_pOgreEntity;
  143. }
  144. }
  145. }
  146. Ogre::SceneNode* Editor::GetMVSceneNode()
  147. {
  148. if (m_pMVGameObject==NULL)
  149. {
  150. return NULL;
  151. }
  152. else
  153. {
  154. if (m_pMVGameObject->m_pGraphicsObject == NULL)
  155. {
  156. return NULL;
  157. }
  158. else
  159. {
  160. return m_pMVGameObject->m_pGraphicsObject->m_pOgreSceneNode;
  161. }
  162. }
  163. }
  164. /*! Start a server in port 19000 if it is the editor or 19001 if it is the modelviewer
  165. * @return S_OK if everything went well
  166. */
  167. int Editor::Editor_StartServer()
  168. {
  169. // Connecting
  170. WSADATA wsaData;
  171. int socket_type = SOCK_STREAM;
  172. //HACK ports are hard coded here, but not in GLE
  173. #ifdef GLE_EDITOR_MODELVIEW
  174. unsigned short port = 19001;
  175. #else
  176. unsigned short port = 19000;
  177. #endif
  178. char *pInterface = NULL;
  179. struct sockaddr_in local;
  180. // Start winsock
  181. if( WSAStartup(0x202,&wsaData ) == SOCKET_ERROR )
  182. {
  183. GGETRACELOG("WSAStartup failed with error %d", WSAGetLastError());
  184. SetEditorConnectionStatus( GGE_ERROR );
  185. return -1;
  186. }
  187. local.sin_family = AF_INET;
  188. local.sin_addr.s_addr = (!pInterface)?INADDR_ANY:inet_addr(pInterface);
  189. local.sin_port = htons(port);
  190. // Start the socket
  191. m_sockListen = socket(AF_INET, socket_type, 0); // TCP socket
  192. if(m_sockListen == INVALID_SOCKET)
  193. {
  194. GGETRACELOG("socket() failed with error %d",WSAGetLastError());
  195. SetEditorConnectionStatus( GGE_ERROR );
  196. return -1;
  197. }
  198. // bind() associates a local address and port combination with the socket just created.
  199. if(::bind(m_sockListen,(struct sockaddr*)&local,sizeof(local) ) == SOCKET_ERROR)
  200. {
  201. GGETRACELOG("bind() failed with error %d, on port: %d",WSAGetLastError(), port);
  202. SetEditorConnectionStatus( GGE_ERROR );
  203. return -1;
  204. }
  205. // Since the connection is TCP we can LISTEN
  206. if (listen(m_sockListen, 1) == SOCKET_ERROR)
  207. {
  208. GGETRACELOG("listen() failed with error %d",WSAGetLastError());
  209. SetEditorConnectionStatus( GGE_ERROR );
  210. return -1;
  211. }
  212. // Change the status, from now on we are going to check if GLE want to connect to GGE
  213. SetEditorConnectionStatus( GGE_READY_TO_ACCEPT );
  214. return 1;
  215. }
  216. /*! Check if there is a client (non-blocking)
  217. * @return S_OK if everything went well
  218. */
  219. int Editor::Editor_AcceptConnection()
  220. {
  221. FD_SET set;
  222. timeval waitTimeStr;
  223. struct sockaddr_in from;
  224. int fromlen = 0;
  225. int retval = 0;
  226. // Check if there is a message to read (non-blocking)
  227. FD_ZERO( &set);
  228. FD_SET( m_sockListen, &set);
  229. waitTimeStr.tv_sec = 0;
  230. waitTimeStr.tv_usec =0;
  231. retval = select( FD_SETSIZE, &set, NULL, NULL, &waitTimeStr );
  232. if( retval == 0) return 0;
  233. if( retval == SOCKET_ERROR)
  234. {
  235. GGETRACELOG("select() error %d",WSAGetLastError());
  236. SetEditorConnectionStatus( GGE_ERROR );
  237. return -1;
  238. }
  239. // Accept a client
  240. fromlen = sizeof(from);
  241. m_sockMessages = accept(m_sockListen,(struct sockaddr*)&from, &fromlen);
  242. if(m_sockMessages == INVALID_SOCKET)
  243. {
  244. GGETRACELOG("accept() error %d",WSAGetLastError());
  245. SetEditorConnectionStatus( GGE_ERROR );
  246. return -1;
  247. }
  248. int flag = 1;
  249. int result = setsockopt(m_sockMessages, /* socket affected */
  250. IPPROTO_TCP, /* set option at TCP level */
  251. TCP_NODELAY, /* name of option */
  252. (char *) &flag, /* the cast is historical
  253. cruft */
  254. sizeof(int));
  255. GGETRACELOG("GLE connected from %s, port %d", inet_ntoa(from.sin_addr), htons(from.sin_port)) ;
  256. // Update the status
  257. SetEditorConnectionStatus (GGE_CONNECTED);
  258. // Send Ready to GLE so that we can start editing !
  259. Editor_GenericCommand(GGE_Ready, "0");// Sends the GGE_Ready message
  260. //Dynamic object editing tool
  261. CUBE_SIZE = 10;
  262. Ogre::MaterialPtr red = Ogre::MaterialManager::getSingleton().create("RedMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
  263. // Get a material pass
  264. Ogre::Pass *m_pPass = red->getTechnique(0)->getPass(0);
  265. // Creating texture unit
  266. m_pPass->setDiffuse(1,0,0,1);
  267. m_pPass->setAmbient(1,0,0);
  268. m_pPass->setDepthCheckEnabled(false);
  269. m_pPass->setLightingEnabled(true);
  270. red->load();
  271. Ogre::MaterialPtr blue = Ogre::MaterialManager::getSingleton().create("BlueMaterial", "General", true);
  272. blue->setDiffuse(0,0,1,1);
  273. blue->setAmbient(0,0,1);
  274. blue->setReceiveShadows(false);
  275. blue->setDepthCheckEnabled(false);
  276. Ogre::MaterialPtr green = Ogre::MaterialManager::getSingleton().create("GreenMaterial", "General", true);
  277. green->setDiffuse(0,1,0,1);
  278. green->setAmbient(0,1,0);
  279. green->setReceiveShadows(false);
  280. green->setDepthCheckEnabled(false);
  281. Ogre::MaterialPtr white = Ogre::MaterialManager::getSingleton().create("WhiteMaterial", "General", true);
  282. white->setDiffuse(1,1,1,1);
  283. white->setAmbient(1,1,1);
  284. white->setReceiveShadows(false);
  285. white->setDepthCheckEnabled(false);
  286. //Note that while Cubes are used as the default. They will be swapped out with spheres and cones when running the dynamic editing tool.
  287. m_DynamicCubeXMarker = new Ogre::ManualObject("DynamicCubeXMarker");
  288. m_DynamicSphereXMarker = new Ogre::ManualObject("DynamicSphereXMarker");
  289. m_DynamicConeXMarker = new Ogre::ManualObject("DynamicConeXMarker");
  290. m_DynamicToolXNode = GetSceneManager()->getRootSceneNode()->createChildSceneNode( m_DynamicCubeXMarker->getName() );
  291. SetupCube(m_DynamicCubeXMarker, red->getName());
  292. SetupSphere(m_DynamicSphereXMarker, red->getName());
  293. SetupCone(m_DynamicConeXMarker, red->getName());
  294. b_DynamicToolXMarkerSelected = false;
  295. m_DynamicToolXNode->attachObject(m_DynamicCubeXMarker);
  296. m_DynamicToolXNode->setVisible(false);
  297. m_DynamicToolXNode->roll( Ogre::Radian(Ogre::Real(-1.57)) );
  298. m_DynamicCubeYMarker = new Ogre::ManualObject("DynamicCubeYMarker");
  299. m_DynamicSphereYMarker = new Ogre::ManualObject("DynamicSphereYMarker");
  300. m_DynamicConeYMarker = new Ogre::ManualObject("DynamicConeYMarker");
  301. m_DynamicToolYNode = GetSceneManager()->getRootSceneNode()->createChildSceneNode( m_DynamicCubeYMarker->getName() );
  302. SetupCube(m_DynamicCubeYMarker, blue->getName());
  303. SetupSphere(m_DynamicSphereYMarker, blue->getName());
  304. SetupCone(m_DynamicConeYMarker, blue->getName());
  305. b_DynamicToolYMarkerSelected = false;
  306. m_DynamicToolYNode->attachObject(m_DynamicCubeYMarker);
  307. m_DynamicToolYNode->setVisible(false);
  308. m_DynamicCubeZMarker = new Ogre::ManualObject("DynamicCubeZMarker");
  309. m_DynamicSphereZMarker = new Ogre::ManualObject("DynamicSphereZMarker");
  310. m_DynamicConeZMarker = new Ogre::ManualObject("DynamicConeZMarker");
  311. m_DynamicToolZNode = GetSceneManager()->getRootSceneNode()->createChildSceneNode( m_DynamicCubeZMarker->getName() );
  312. SetupCube(m_DynamicCubeZMarker, green->getName());
  313. SetupSphere(m_DynamicSphereZMarker, green->getName());
  314. SetupCone(m_DynamicConeZMarker, green->getName());
  315. b_DynamicToolZMarkerSelected = false;
  316. m_DynamicToolZNode->attachObject(m_DynamicCubeZMarker);
  317. m_DynamicToolZNode->setVisible(false);
  318. m_DynamicToolZNode->pitch( Ogre::Radian(Ogre::Real(1.57)));
  319. m_FreeDynamicCubeMarker = new Ogre::ManualObject("FreeDynamicCubeMarker");
  320. m_FreeDynamicSphereMarker = new Ogre::ManualObject("FreeDynamicSphereMarker");
  321. m_FreeDynamicToolNode = GetSceneManager()->getRootSceneNode()->createChildSceneNode( m_FreeDynamicCubeMarker->getName() );
  322. SetupCube(m_FreeDynamicCubeMarker, white->getName());
  323. SetupSphere(m_FreeDynamicSphereMarker, white->getName());
  324. b_FreeDynamicToolMarkerSelected = false;
  325. m_FreeDynamicToolNode->attachObject(m_FreeDynamicCubeMarker);
  326. m_FreeDynamicToolNode->setVisible(false);
  327. //The dynamic editing tool is off by default
  328. b_DynamicScalingMode = false;
  329. b_DynamicPositioningMode = false;
  330. b_DynamicRotationMode = false;
  331. b_DynamicToolActive = false;
  332. return 1;
  333. }
  334. /*! Check if there is any new packet (non-blocking)
  335. * @return 1 if everything went well
  336. */
  337. int Editor::Editor_Receive()
  338. {
  339. int retval = 0;
  340. FD_SET set;
  341. timeval waitTimeStr;
  342. char buffer[MAX_PACKET_SIZE];
  343. memset(buffer, '\0', MAX_PACKET_SIZE);
  344. // Check if there is a message to read (non-blocking)
  345. FD_ZERO( &set);
  346. FD_SET( m_sockMessages, &set);
  347. waitTimeStr.tv_sec = 0;
  348. waitTimeStr.tv_usec =0;
  349. retval = select( FD_SETSIZE, &set, NULL, NULL, &waitTimeStr );
  350. if( retval == 0) return 0;
  351. if( retval == SOCKET_ERROR)
  352. {
  353. GGETRACELOG("select() error %d",WSAGetLastError());
  354. SetEditorConnectionStatus( GGE_ERROR );
  355. return -1;
  356. }
  357. // If we are here is because there is something to read
  358. retval = recv(m_sockMessages, buffer, sizeof (buffer),0 );
  359. if(retval == SOCKET_ERROR)
  360. {
  361. GGETRACELOG("recv() failed: error %d", WSAGetLastError());
  362. SetEditorConnectionStatus( GGE_ERROR );
  363. return -1;
  364. }
  365. if(retval == 0)
  366. {
  367. GGETRACELOG("Client closed connection");
  368. SetEditorConnectionStatus( GGE_ERROR );
  369. return 0;
  370. }
  371. // Parse the message and do something
  372. // HACK commented out code
  373. //Editor_Parser( retval, buffer );
  374. // Parse the message and do something
  375. Ogre::String sBuffer = Ogre::String(buffer);
  376. if (sBuffer == ";") return 0;
  377. try
  378. {
  379. if (sBuffer.substr(sBuffer.length() - 2, 2) == ";;")
  380. {
  381. if (m_msgLeftOvers.length() > 0)
  382. {
  383. m_msgLeftOvers.append(buffer);
  384. //GGETRACELOG("LONG PACKET HANDLED: %s", m_msgLeftOvers.c_str());
  385. Editor_Parser( retval, m_msgLeftOvers );
  386. m_msgLeftOvers.clear();
  387. }
  388. else
  389. Editor_Parser( retval, sBuffer);
  390. }
  391. else
  392. {
  393. //GGETRACELOG("OH SNAP TOO LONG PACKET: %s", buffer);
  394. m_msgLeftOvers.clear();
  395. m_msgLeftOvers.append(sBuffer);
  396. }
  397. return 1;
  398. }
  399. catch (...)
  400. {
  401. return 0;
  402. }
  403. }
  404. /*! Send a packet to GLE
  405. * @return The number of bytes actually sent
  406. */
  407. int Editor::Editor_Send( const char * cpBuffer )
  408. {
  409. #ifdef GLE_DEBUG_NETWORK
  410. GGETRACELOG("Sending message: [%s]", cpBuffer);
  411. #endif
  412. int iRet;
  413. iRet = send(m_sockMessages, cpBuffer, strlen(cpBuffer), 0);
  414. // Check errors
  415. if(iRet == SOCKET_ERROR)
  416. GGETRACELOG("send() failed: error %d", WSAGetLastError());
  417. return iRet;
  418. }
  419. /*! Build and send a packet to GLE
  420. * @param[in] packedID The ID of the packet that we want to send
  421. * @param[in] uniqueID ID generated automatically that identifies any transaction
  422. * @param[in] args Extra arguments that might be needed by the packet
  423. * @return The number of bytes actually sent
  424. */
  425. int Editor::Editor_GenericCommand(int packetID, int uniqueID, std::string args)
  426. {
  427. stringstream ss;
  428. ss << packetID << "||" << uniqueID << "||" << args << ";;";
  429. return Editor_Send(ss.str().c_str());
  430. }
  431. /*! Build and send a packet to GLE
  432. * @param[in] packedID The ID of the packet that we want to send
  433. * @param[in] args Extra arguments that might be needed by the packet
  434. * @return The number of bytes actually sent
  435. */
  436. int Editor::Editor_GenericCommand(int packetID, std::string args)
  437. {
  438. return Editor_GenericCommand(packetID, -1, args);
  439. }
  440. /*! Build and send a packet to GLE
  441. * @param[in] string Packet received
  442. * @param[out] tokens Vector of strings extracted from the string. It will contain the packetID, uniqueID, arguments.
  443. * @param[in] delimiters Delimiters
  444. */
  445. void Editor::Tokenize(const Ogre::String& str, std::vector<Ogre::String>& tokens, const Ogre::String& delimiters)
  446. {
  447. // Skip delimiters at beginning.
  448. string::size_type lastPos = str.find_first_not_of(delimiters, 0);
  449. // Find first "non-delimiter".
  450. string::size_type pos = str.find_first_of(delimiters, lastPos);
  451. while (string::npos != pos || string::npos != lastPos)
  452. {
  453. // Found a token, add it to the vector.
  454. Ogre::String token = str.substr(lastPos, pos - lastPos);
  455. if (token.length() > 0)
  456. tokens.push_back(token);
  457. // Skip delimiters. Note the "not_of"
  458. lastPos = str.find_first_not_of(delimiters, pos);
  459. // Find next "non-delimiter"
  460. pos = str.find_first_of(delimiters, lastPos);
  461. }
  462. }
  463. void Editor::AltTokenize(const Ogre::String& str, std::vector<Ogre::String>& tokens, const Ogre::String& delimiters)
  464. {
  465. Ogre::String tempString = str;
  466. Ogre::String outString;
  467. int tokenIdx = 0;
  468. while(tempString.size() > 0 && tokenIdx != -1)
  469. {
  470. tokenIdx = tempString.find(delimiters);
  471. outString = "";
  472. outString += tempString.substr(0, tokenIdx);
  473. tempString = tempString.substr(tokenIdx + delimiters.size());
  474. tokens.push_back(outString);
  475. }
  476. if(tempString.size() > 0)
  477. tokens.push_back(tempString);
  478. else
  479. tokens.push_back("");
  480. }
  481. /*! Convert a string to a float
  482. * @param[in] str String to convert
  483. * @return Float
  484. */
  485. float Editor::ParseFloat(const string& str)
  486. {
  487. float r = 0;
  488. istringstream iss(str);
  489. if (! (iss >> r))
  490. GGETRACELOG(string("Error parsing to float: " + str).c_str());
  491. return r;
  492. }
  493. /*! Convert a string to a int
  494. * @param[in] str String to convert
  495. * @return Int
  496. */
  497. int Editor::ParseInt(const string& str)
  498. {
  499. int r = -1;
  500. istringstream iss(str);
  501. if (! (iss >> r))
  502. GGETRACELOG(string("Error parsing to int: " + str).c_str());
  503. return r;
  504. }
  505. /*! Convert a string to a bool
  506. * @param[in] str String to convert
  507. * @return Boolean
  508. */
  509. bool Editor::ParseBool(const string& str)
  510. {
  511. bool r = false;
  512. if (StrCompareNoCase(str, "false") || (StrCompareNoCase(str, "0")))
  513. r = false;
  514. else if (StrCompareNoCase(str, "true") || (StrCompareNoCase(str, "1")))
  515. r = true;
  516. else
  517. GGETRACELOG(string("Error parsing to bool: " + str).c_str());
  518. return r;
  519. }
  520. /*! Compare two strings
  521. * @param[in] String1 String to compare
  522. * @param[in] String2 String to compare
  523. * @return True if the string are the same
  524. */
  525. bool Editor::StrCompareNoCase( string String1, string String2 )
  526. {
  527. transform(String1.begin(), String1.end(), String1.begin(), tolower);
  528. transform(String2.begin(), String2.end(), String2.begin(), tolower);
  529. return String1 == String2;
  530. }
  531. /*! Convert a string to a vector of tuples with properties and values
  532. * @param[in] str String to convert
  533. * @return Vector of properties and values
  534. */
  535. vector<GLE_PropTuple> Editor::ParseTuples(const string& str)
  536. {
  537. string cstr = str;
  538. vector<GLE_PropTuple> tuples;
  539. while (true)
  540. {
  541. int first = cstr.find("[[");
  542. int last = cstr.find("]]");
  543. int mid = cstr.find("][");
  544. if (first >= 0 && last >= 0 && mid >= 0)
  545. {
  546. int propType = ParseInt(cstr.substr(first + 2, mid - first - 2));
  547. string value = cstr.substr(mid + 2, last - mid - 2);
  548. cstr = cstr.erase(0, last + 2);
  549. tuples.push_back(GLE_PropTuple(propType, value));
  550. }
  551. else
  552. break;
  553. }
  554. return tuples;
  555. }
  556. /*! Convert an integer to string
  557. * @param[in] i Integer to convert
  558. * @return String
  559. */
  560. string Editor::Int2Str(int i)
  561. {
  562. stringstream ss;
  563. ss << i;
  564. return ss.str();
  565. }
  566. /*! Convert an float to string
  567. * @param[in] f Float to convert
  568. * @return String
  569. */
  570. string Editor::Float2Str(float f)
  571. {
  572. stringstream ss;
  573. ss << fixed << setprecision(6) << f;
  574. return ss.str();
  575. }
  576. /*! Send a message to GLE reporting an error
  577. * @param[in] sMessage Message to send to GLE
  578. * @return The number of bytes sent
  579. */
  580. int Editor::Editor_ReportErrorToGLE( const Ogre::String& sMessage )
  581. {
  582. // Report
  583. GGETRACELOG("Error: [%s] sending to GLE", sMessage.c_str());
  584. // Send the string
  585. return Editor_GenericCommand(GGE_Error, sMessage);
  586. }
  587. /*! Send an exception to GLE reporting an error
  588. * @param[in] ex Exception to send to GLE
  589. * @return The number of bytes sent
  590. */
  591. int Editor::Editor_ReportErrorToGLE( const Ogre::Exception& ex )
  592. {
  593. // Send the string
  594. return Editor_ReportErrorToGLE(ex.getDescription());
  595. }
  596. /*! Send a message to GLE reporting the use of a deprecated function
  597. * @param[in] packedID ID of the packet being used
  598. * @return The number of bytes sent
  599. */
  600. int Editor::Editor_ReportDeprecated( int packetID )
  601. {
  602. return Editor_ReportErrorToGLE(Ogre::String("Method deprecated: " + Int2Str(packetID)));
  603. }
  604. /*! Send a message to GLE reporting the use of an unimplemented function
  605. * @param[in] packedID ID of the packet
  606. * @return The number of bytes sent
  607. */
  608. int Editor::Editor_ReportUnimplemented( int packetID )
  609. {
  610. return Editor_ReportErrorToGLE(Ogre::String("Method not implemented: " + Int2Str(packetID)));
  611. }
  612. /*! Send a message to GLE with the properties of an object
  613. * @return The number of bytes sent
  614. */
  615. int Editor::Editor_GGE_Response_Object_Properties(string nodeName, int nodeType, vector<GLE_PropTuple>& tuples)
  616. {
  617. stringstream ss;
  618. ss << nodeName << "||";
  619. ss << nodeType << "||";
  620. for(unsigned int i = 0; i < tuples.size(); i++)
  621. {
  622. GLE_PropTuple t = tuples[i];
  623. ss << "[[" << t.property << "][" << t.value << "]]";
  624. }
  625. return Editor_GenericCommand(GGE_Response_Object_Properties, ss.str());
  626. }
  627. /*! Send a basic message to the GLE, message will be showed as a
  628. * message box on the GLE side.
  629. * @param[in] the message to be displayed in the message box
  630. * @return the number of bytes sent
  631. */
  632. int Editor::SendSimpleMessage(std::string smessage)
  633. {
  634. return Editor_GenericCommand(GLE_StandardMessage, smessage);
  635. }
  636. /*! Return a saved message depending of the input protocol selected
  637. * some classes (as the skybox) need to save several definition elements
  638. * this way, other objects can access those definitions through the string message that created them
  639. * @param[in] the protocol which has to be accessed
  640. * @return the most relevant message for that protocol
  641. */
  642. std::string Editor::AskStatusMessage(EditorProtocol protocolDefinition)
  643. {
  644. bool retval = false;
  645. string message;
  646. for(unsigned int con = 0; con < m_vMessageHandles.size(); con++)
  647. {
  648. if(m_vMessageHandles[con]->AskCurrentStatusMessage(protocolDefinition, message))
  649. {
  650. retval = true;
  651. break;
  652. }
  653. }
  654. if(!retval)
  655. {
  656. message = "";
  657. }
  658. return message;
  659. }
  660. /*! Main function. Called when receiving packets, it will parse the packet and execute the associated functionality
  661. * @param[in] iNumBytesSent Number of bytes received from GLE
  662. * @param[in] cpBuffer Message received from GLE
  663. * @return -1 if error
  664. */
  665. int Editor::Editor_Parser( int iNumBytesSent, Ogre::String cpBuffer )
  666. {
  667. Ogre::String packet = Ogre::String(cpBuffer);
  668. #ifdef GLE_DEBUG_NETWORK
  669. GGETRACELOG("Received %d bytes, data: [%s] from client", iNumBytesSent, packet.c_str());
  670. #endif
  671. // Separate all messages
  672. vector<Ogre::String> packets;
  673. AltTokenize(packet, packets, ";;");//Tokenize(packet, packets, ";;");
  674. // process each message
  675. for (unsigned int i = 0; i < packets.size(); i++)
  676. {
  677. int iPacketID = -1;
  678. int iUniqueID = -1;
  679. // Update the text on the console
  680. #ifdef GLE_DEBUG_NETWORK
  681. GGETRACELOG("Message from client: [%s]", packets[i].c_str());
  682. #endif
  683. vector<Ogre::String> tokens;
  684. AltTokenize(packets[i], tokens, "||");//Tokenize(packets[i], tokens, "||");
  685. if (tokens.size() < 2)
  686. continue;
  687. bool retval = false;
  688. for(unsigned int con = 0; con < m_vMessageHandles.size(); con++)
  689. {
  690. if(m_vMessageHandles[con]->ProcessMessage(tokens))
  691. {
  692. retval = true;
  693. break;
  694. }
  695. }
  696. if(retval == false)
  697. return -1;
  698. }
  699. return 1;
  700. }
  701. /*! Close Winsock and both sockets.
  702. * @return S_OK if everything correct
  703. */
  704. int Editor::FinalizeEditor()
  705. {
  706. closesocket( m_sockListen );
  707. closesocket( m_sockMessages );
  708. WSACleanup();
  709. return S_OK;
  710. }
  711. /*! Obtain all the information from a node and create a vector with it
  712. * @param[in] node Node to extract information
  713. * @return Vector with the properties and values of the scene node
  714. */
  715. vector<GLE_PropTuple> Editor::GetSceneNodeProperties(Ogre::SceneNode* node)
  716. {
  717. vector<GLE_PropTuple> tuples;
  718. Ogre::Vector3 Pos = node->getPosition();
  719. Ogre::Vector3 Scale = node->getScale();
  720. Ogre::Quaternion Orientation = node->getOrientation();
  721. tuples.push_back(GLE_PropTuple(PROP_Pos_X, Pos.x));
  722. tuples.push_back(GLE_PropTuple(PROP_Pos_Y, Pos.y));
  723. tuples.push_back(GLE_PropTuple(PROP_Pos_Z, Pos.z));
  724. // tuples.push_back(GLE_PropTuple(PROP_RotationQuat_X, Orientation.x));
  725. // tuples.push_back(GLE_PropTuple(PROP_RotationQuat_Y, Orientation.y));
  726. // tuples.push_back(GLE_PropTuple(PROP_RotationQuat_Z, Orientation.z)));
  727. // tuples.push_back(GLE_PropTuple(PROP_RotationQuat_W, Orientation.w));
  728. tuples.push_back(GLE_PropTuple(PROP_Rotation_X, Orientation.getPitch().valueDegrees()));
  729. tuples.push_back(GLE_PropTuple(PROP_Rotation_Y, Orientation.getYaw().valueDegrees()));
  730. tuples.push_back(GLE_PropTuple(PROP_Rotation_Z, Orientation.getRoll().valueDegrees()));
  731. tuples.push_back(GLE_PropTuple(PROP_Scale_X, Scale.x));
  732. tuples.push_back(GLE_PropTuple(PROP_Scale_Y, Scale.y));
  733. tuples.push_back(GLE_PropTuple(PROP_Scale_Z, Scale.z));
  734. return tuples;
  735. }
  736. /*! Obtain all the information from an entity and create a vector with it
  737. * @param[in] entity Entity to extract information
  738. * @return Vector with the properties and values of the entity
  739. */
  740. vector<GLE_PropTuple> Editor::GetEntityProperties( Ogre::Entity* entity )
  741. {
  742. // TODO-SV: return havokFile and havokName
  743. vector<GLE_PropTuple> tuples;
  744. tuples.push_back(GLE_PropTuple(PROP_MovableType, entity->getMovableType()));
  745. tuples.push_back(GLE_PropTuple(PROP_Material, entity->getSubEntity(0)->getMaterialName()));
  746. tuples.push_back(GLE_PropTuple(PROP_Shadow, entity->getCastShadows()));
  747. return tuples;
  748. }
  749. /*! Obtain all the information from a light and create a vector with it
  750. * @param[in] light Light to extract information
  751. * @return Vector with the properties and values of the light
  752. */
  753. vector<GLE_PropTuple> Editor::GetLightProperties( Ogre::Light* light )
  754. {
  755. vector<GLE_PropTuple> tuples;
  756. Ogre::ColourValue spec = light->getSpecularColour();
  757. Ogre::ColourValue diff = light->getDiffuseColour();
  758. Ogre::Vector3 dir = light->getDirection();
  759. Ogre::Light::LightTypes type = light->getType();
  760. tuples.push_back(GLE_PropTuple(PROP_Light_Type, float(type)));
  761. tuples.push_back(GLE_PropTuple(PROP_Direction_X, dir.x));
  762. tuples.push_back(GLE_PropTuple(PROP_Direction_Y, dir.y));
  763. tuples.push_back(GLE_PropTuple(PROP_Direction_Z, dir.z));
  764. tuples.push_back(GLE_PropTuple(PROP_Color_Diff_R, diff.r));
  765. tuples.push_back(GLE_PropTuple(PROP_Color_Diff_G, diff.g));
  766. tuples.push_back(GLE_PropTuple(PROP_Color_Diff_B, diff.b));
  767. tuples.push_back(GLE_PropTuple(PROP_Color_Spec_R, spec.r));
  768. tuples.push_back(GLE_PropTuple(PROP_Color_Spec_G, spec.g));
  769. tuples.push_back(GLE_PropTuple(PROP_Color_Spec_B, spec.b));
  770. return tuples;
  771. }
  772. /*! Set the scene node properties from a tuple
  773. * @param[in] tuple Property-Value to set
  774. * @param[in] node Node that will receive the information
  775. */
  776. void Editor::SetSceneNodeProperty( GLE_PropTuple &tuple, Ogre::SceneNode* node )
  777. {
  778. try
  779. {
  780. switch (tuple.property)
  781. {
  782. case PROP_Pos_X:
  783. {
  784. node->setPosition(ParseFloat(tuple.value), node->getPosition().y, node->getPosition().z);
  785. break;
  786. }
  787. case PROP_Pos_Y:
  788. {
  789. node->setPosition(node->getPosition().x, ParseFloat(tuple.value), node->getPosition().z);
  790. break;
  791. }
  792. case PROP_Pos_Z:
  793. {
  794. node->setPosition(node->getPosition().x, node->getPosition().y, ParseFloat(tuple.value));
  795. break;
  796. }
  797. case PROP_RotationQuat_X:
  798. {
  799. node->setOrientation( node->getOrientation().w, ParseFloat(tuple.value), node->getOrientation().y, node->getOrientation().z );
  800. break;
  801. }
  802. case PROP_RotationQuat_Y:
  803. {
  804. node->setOrientation( node->getOrientation().w, node->getOrientation().x, ParseFloat(tuple.value), node->getOrientation().z );
  805. break;
  806. }
  807. case PROP_RotationQuat_Z:
  808. {
  809. node->setOrientation( node->getOrientation().w, node->getOrientation().x, node->getOrientation().y, ParseFloat(tuple.value) );
  810. break;
  811. }
  812. case PROP_RotationQuat_W:
  813. {
  814. node->setOrientation( ParseFloat(tuple.value), node->getOrientation().x, node->getOrientation().y, node->getOrientation().z );
  815. break;
  816. }
  817. case PROP_Rotation_Y:
  818. {
  819. Ogre::Quaternion q;
  820. q.FromAngleAxis( Ogre::Degree(ParseFloat(tuple.value)), Ogre::Vector3::UNIT_Y );
  821. node->setOrientation(q);
  822. break;
  823. }
  824. case PROP_Rotation_X:
  825. {
  826. Ogre::Quaternion q;
  827. q.FromAngleAxis( Ogre::Degree(ParseFloat(tuple.value)), Ogre::Vector3::UNIT_X );
  828. node->setOrientation(q);
  829. break;
  830. }
  831. case PROP_Rotation_Z:
  832. {
  833. Ogre::Quaternion q;
  834. q.FromAngleAxis( Ogre::Degree(ParseFloat(tuple.value)), Ogre::Vector3::UNIT_Z );
  835. node->setOrientation(q);
  836. break;
  837. }
  838. case PROP_Scale_X:
  839. {
  840. node->setScale(ParseFloat(tuple.value),node->getScale().y,node->getScale().z);
  841. break;
  842. }
  843. case PROP_Scale_Y:
  844. {
  845. node->setScale(node->getScale().x,ParseFloat(tuple.value),node->getScale().z);
  846. break;
  847. }
  848. case PROP_Scale_Z:
  849. {
  850. node->setScale(node->getScale().x,node->getScale().y,ParseFloat(tuple.value));
  851. break;
  852. }
  853. }
  854. // If the node contains an entity with physics the physics object needs to be updated
  855. Ogre::SceneNode::ObjectIterator childNodeIterator = node->getAttachedObjectIterator();
  856. while (childNodeIterator.hasMoreElements())
  857. {
  858. Ogre::MovableObject* childNode = childNodeIterator.getNext();
  859. if (m_pEngine->GetForemostGameScreen(true)->GetDefaultSceneManager()->hasEntity(childNode->getName()))
  860. {
  861. if (m_pEngine->GetForemostGameScreen(true)->GetGameObjectManager()->HasGameObject(childNode->getName()))
  862. {
  863. GameObject* gameObject = m_pEngine->GetForemostGameScreen(true)->GetGameObjectManager()->GetGameObject(childNode->getName());
  864. bool bStandard = false;
  865. #ifdef HAVOK
  866. hkVector4 newPosition(node->getPosition().x, node->getPosition().y, node->getPosition().z);
  867. hkQuaternion newRotation(node->getOrientation().x, node->getOrientation().y, node->getOrientation().z, node->getOrientation().w);
  868. PhysicsPrimitiveObject* f_pTempObject = (PhysicsPrimitiveObject*)gameObject->m_pPhysicsObject;
  869. bStandard = true;
  870. if (f_pTempObject!=NULL)
  871. {
  872. if(f_pTempObject->getRigidBody() != NULL)
  873. {
  874. f_pTempObject->getRigidBody()->setPositionAndRotation(newPosition, newRotation);
  875. f_pTempObject->getRigidBody()->setLinearVelocity(hkVector4::getZero());
  876. f_pTempObject->getRigidBody()->setAngularVelocity(hkVector4::getZero());
  877. bStandard = false;
  878. }
  879. }
  880. #endif
  881. #ifdef PHYSX
  882. physx::PxVec3 Pos(node->getPosition().x, node->getPosition().y, node->getPosition().z);
  883. physx::PxQuat Quat(node->getOrientation().x, node->getOrientation().y, node->getOrientation().z, node->getOrientation().w);
  884. PhysicsPrimitiveObject* f_pTempObject = (PhysicsPrimitiveObject*)gameObject->m_pPhysicsObject;
  885. if (f_pTempObject != NULL) {
  886. if(f_pTempObject->getGameObjectType() == PHYSICS_DYNAMIC) {
  887. f_pTempObject->getDynamicRigidBody()->setGlobalPose(physx::PxTransform(Pos, Quat));
  888. f_pTempObject->getDynamicRigidBody()->setLinearVelocity(physx::PxVec3(0,0,0));
  889. f_pTempObject->getDynamicRigidBody()->setAngularVelocity(physx::PxVec3(0,0,0));
  890. }
  891. else if (f_pTempObject->getGameObjectType() == PHYSICS_FIXED) {
  892. f_pTempObject->getStaticRigidBody()->setGlobalPose(physx::PxTransform(Pos, Quat));
  893. }
  894. }
  895. #endif
  896. if(bStandard)
  897. {
  898. gameObject = m_pEngine->GetForemostGameScreen(true)->GetGameObjectManager()->GetGameObject(childNode->getName());
  899. gameObject->setPosition(node->getPosition().x, node->getPosition().y, node->getPosition().z);
  900. }
  901. gameObject->resetScale(node->getScale().x, node->getScale().y, node->getScale().z);
  902. }
  903. }
  904. }
  905. }
  906. catch(...)
  907. {
  908. }
  909. }
  910. /*! Set the light properties from a tuple
  911. * @param[in] tuple Property-Value to set
  912. * @param[in] light Light that will receive the information
  913. */
  914. void Editor::SetLightProperty( GLE_PropTuple &tuple, Ogre::Light* light)
  915. {
  916. switch (tuple.property)
  917. {
  918. case PROP_Light_Type:
  919. {
  920. int type = ParseInt(tuple.value);
  921. if (type >= 0)
  922. light->setType((Ogre::Light::LightTypes)ParseInt(tuple.value));
  923. break;
  924. }
  925. case PROP_Direction_X:
  926. {
  927. light->setDirection(ParseFloat(tuple.value), light->getDirection().y, light->getDirection().z);
  928. break;
  929. }
  930. case PROP_Direction_Y:
  931. {
  932. light->setDirection(light->getDirection().x, ParseFloat(tuple.value), light->getDirection().z);
  933. break;
  934. }
  935. case PROP_Direction_Z:
  936. {
  937. light->setDirection(light->getDirection().x, light->getDirection().y, ParseFloat(tuple.value));
  938. break;
  939. }
  940. case PROP_Color_Diff_R:
  941. {
  942. light->setDiffuseColour(ParseFloat(tuple.value), light->getDiffuseColour().g, light->getDiffuseColour().b);
  943. break;
  944. }
  945. case PROP_Color_Diff_G:
  946. {
  947. light->setDiffuseColour(light->getDiffuseColour().r, ParseFloat(tuple.value), light->getDiffuseColour().b);
  948. break;
  949. }
  950. case PROP_Color_Diff_B:
  951. {
  952. light->setDiffuseColour(light->getDiffuseColour().r, light->getDiffuseColour().g, ParseFloat(tuple.value));
  953. break;
  954. }
  955. case PROP_Color_Spec_R:
  956. {
  957. light->setSpecularColour(ParseFloat(tuple.value), light->getSpecularColour().g, light->getSpecularColour().b);
  958. break;
  959. }
  960. case PROP_Color_Spec_G:
  961. {
  962. light->setSpecularColour(light->getSpecularColour().r, ParseFloat(tuple.value), light->getSpecularColour().b);
  963. break;
  964. }
  965. case PROP_Color_Spec_B:
  966. {
  967. light->setSpecularColour(light->getSpecularColour().r, light->getSpecularColour().g, ParseFloat(tuple.value));
  968. break;
  969. }
  970. }
  971. }
  972. void Editor::SetEntityProperty( GLE_PropTuple &tuple, Ogre::Entity* entity)
  973. {
  974. try
  975. {
  976. switch(tuple.property)
  977. {
  978. case PROP_Material:
  979. GGETRACELOG("Entered PROP_Material case");
  980. entity->setMaterialName(tuple.value);
  981. break;
  982. case PROP_Shadow:
  983. GGETRACELOG("Entered PROP_Shadow case");
  984. entity->setCastShadows(ParseBool(tuple.value));
  985. break;
  986. }
  987. }catch(...)
  988. {
  989. }
  990. }
  991. /*! Toogle the grid of the scene manager (only one by scene manager)
  992. * @param[in] tokens List of tokens
  993. */
  994. void Editor::GridToggle( vector<Ogre::String> tokens )
  995. {
  996. Ogre::String nm;
  997. // Parse the packet
  998. nm=tokens[2];
  999. // Check if we are activating or deactivating the grid
  1000. // if we are activating then we need to receive other information ...
  1001. if (nm == "1" &&
  1002. !GetSceneManager()->hasSceneNode("Editor_GridSceneNode") &&
  1003. !GetSceneManager()->hasManualObject("Editor_Grid"))
  1004. {
  1005. int iCount = 0 ;
  1006. // Get the information from the packet
  1007. int m_iGridMaxX = ParseInt(tokens[3]);
  1008. int m_iGridMaxZ = ParseInt(tokens[4]);
  1009. int m_iGridSeparation = ParseInt(tokens[5]);
  1010. Ogre::Real iGridAxesColorR = Ogre::Real(ParseFloat(tokens[6]));
  1011. Ogre::Real iGridAxesColorG = Ogre::Real(ParseFloat(tokens[7]));
  1012. Ogre::Real iGridAxesColorB = Ogre::Real(ParseFloat(tokens[8]));
  1013. Ogre::Real iGridLineColorR = Ogre::Real(ParseFloat(tokens[9]));
  1014. Ogre::Real iGridLineColorG = Ogre::Real(ParseFloat(tokens[10]));
  1015. Ogre::Real iGridLineColorB = Ogre::Real(ParseFloat(tokens[11]));
  1016. // Create the grids
  1017. // extra grids are used for orthogonal cameras
  1018. Ogre::ManualObject * mGrid = GetSceneManager()->createManualObject("Editor_Grid");
  1019. mGrid->begin( "BaseWhiteNoLighting", Ogre::RenderOperation::OT_LINE_LIST );
  1020. Ogre::ManualObject * mGridSide = GetSceneManager()->createManualObject("Editor_Grid_Side");
  1021. mGridSide->begin( "BaseWhiteNoLighting", Ogre::RenderOperation::OT_LINE_LIST );
  1022. Ogre::ManualObject * mGridFront = GetSceneManager()->createManualObject("Editor_Grid_Front");
  1023. mGridFront->begin( "BaseWhiteNoLighting", Ogre::RenderOperation::OT_LINE_LIST );
  1024. for( int i = -m_iGridMaxZ ; i <= m_iGridMaxZ ; i += m_iGridSeparation)
  1025. {
  1026. mGrid->position(-Ogre::Real(m_iGridMaxX), 0.0f, Ogre::Real(i));
  1027. mGrid->colour(iGridLineColorR,iGridLineColorG,iGridLineColorB);
  1028. if( i == 0 ) mGrid->colour(iGridAxesColorR,iGridAxesColorG,iGridAxesColorB);
  1029. mGrid->position(Ogre::Real(m_iGridMaxX), 0.0f, Ogre::Real(i));
  1030. //
  1031. mGridSide->position(0.0f, -Ogre::Real(m_iGridMaxX), Ogre::Real(i));
  1032. mGridSide->colour(iGridLineColorR,iGridLineColorG,iGridLineColorB);
  1033. if( i == 0 ) mGridSide->colour(iGridAxesColorR,iGridAxesColorG,iGridAxesColorB);
  1034. mGridSide->position(0.0f, Ogre::Real(m_iGridMaxX), Ogre::Real(i));
  1035. //
  1036. mGridFront->position(-Ogre::Real(m_iGridMaxX), Ogre::Real(i), 0.0f);
  1037. mGridFront->colour(iGridLineColorR,iGridLineColorG,iGridLineColorB);
  1038. if( i == 0 ) mGridFront->colour(iGridAxesColorR,iGridAxesColorG,iGridAxesColorB);
  1039. mGridFront->position(Ogre::Real(m_iGridMaxX), Ogre::Real(i), 0.0f);
  1040. //
  1041. iCount++;
  1042. }
  1043. iCount = 0;
  1044. for( int i = -m_iGridMaxX ; i <= m_iGridMaxX ; i += m_iGridSeparation)
  1045. {
  1046. mGrid->position(Ogre::Real(i), 0.0f, -Ogre::Real(m_iGridMaxZ) );
  1047. mGrid->colour(iGridLineColorR,iGridLineColorG,iGridLineColorB);
  1048. if( i == 0 ) mGrid->colour(iGridAxesColorR,iGridAxesColorG,iGridAxesColorB);
  1049. mGrid->position(Ogre::Real(i), 0.0f, Ogre::Real(m_iGridMaxZ) );
  1050. //
  1051. mGridSide->position(0.0f, Ogre::Real(i), -Ogre::Real(m_iGridMaxZ) );
  1052. mGridSide->colour(iGridLineColorR,iGridLineColorG,iGridLineColorB);
  1053. if( i == 0 ) mGridSide->colour(iGridAxesColorR,iGridAxesColorG,iGridAxesColorB);
  1054. mGridSide->position(0.0f, Ogre::Real(i), Ogre::Real(m_iGridMaxZ) );
  1055. //
  1056. mGridFront->position(Ogre::Real(i), -Ogre::Real(m_iGridMaxZ), 0.0f);
  1057. mGridFront->colour(iGridLineColorR,iGridLineColorG,iGridLineColorB);
  1058. if( i == 0 ) mGridFront->colour(iGridAxesColorR,iGridAxesColorG,iGridAxesColorB);
  1059. mGridFront->position(Ogre::Real(i), Ogre::Real(m_iGridMaxZ), 0.0f);
  1060. //
  1061. iCount++;
  1062. }
  1063. mGrid->end();
  1064. mGridSide->end();
  1065. mGridFront->end();
  1066. // Add it to a new scene node
  1067. GetSceneManager()->getRootSceneNode()->createChildSceneNode("Editor_GridSceneNode")->attachObject(mGrid);
  1068. //if older orthogonal grids still exist then destroy them
  1069. if(GetSceneManager()->hasSceneNode("Editor_GridSceneNode_Side") &&
  1070. GetSceneManager()->hasManualObject("Editor_Grid_Side") &&
  1071. GetSceneManager()->hasSceneNode("Editor_GridSceneNode_Front") &&
  1072. GetSceneManager()->hasManualObject("Editor_Grid_Front")) {
  1073. GetSceneManager()->destroyManualObject("Editor_Grid_Side");
  1074. GetSceneManager()->destroySceneNode("Editor_GridSceneNode_Side");
  1075. GetSceneManager()->destroyManualObject("Editor_Grid_Front");
  1076. GetSceneManager()->destroySceneNode("Editor_GridSceneNode_Front");
  1077. }
  1078. //add the new orthogonal new ones
  1079. GetSceneManager()->getRootSceneNode()->createChildSceneNode("Editor_GridSceneNode_Side")->attachObject(mGridSide);
  1080. GetSceneManager()->getRootSceneNode()->createChildSceneNode("Editor_GridSceneNode_Front")->attachObject(mGridFront);
  1081. //get the current camera
  1082. const Ogre::String cName = m_pEngine->GetForemostGameScreen()->GetActiveCamera()->getName();
  1083. //hide grids based on current camera
  1084. if(cName == "Editor_CameraPerspective" || cName == "Editor_CameraTop") {
  1085. GetSceneManager()->getManualObject("Editor_Grid")->setVisible(true);
  1086. GetSceneManager()->getManualObject("Editor_Grid_Side")->setVisible(false);
  1087. GetSceneManager()->getManualObject("Editor_Grid_Front")->setVisible(false);
  1088. }
  1089. else if(cName == "Editor_CameraSide") {
  1090. GetSceneManager()->getManualObject("Editor_Grid")->setVisible(false);
  1091. GetSceneManager()->getManualObject("Editor_Grid_Side")->setVisible(true);
  1092. GetSceneManager()->getManualObject("Editor_Grid_Front")->setVisible(false);
  1093. }
  1094. else if(cName == "Editor_CameraFront") {
  1095. GetSceneManager()->getManualObject("Editor_Grid")->setVisible(false);
  1096. GetSceneManager()->getManualObject("Editor_Grid_Side")->setVisible(false);
  1097. GetSceneManager()->getManualObject("Editor_Grid_Front")->setVisible(true);
  1098. }
  1099. }else if( nm == "0" &&
  1100. GetSceneManager()->hasSceneNode("Editor_GridSceneNode") &&
  1101. GetSceneManager()->hasManualObject("Editor_Grid") &&
  1102. //orthogonal grids
  1103. GetSceneManager()->hasSceneNode("Editor_GridSceneNode_Side") &&
  1104. GetSceneManager()->hasManualObject("Editor_Grid_Side") &&
  1105. GetSceneManager()->hasSceneNode("Editor_GridSceneNode_Front") &&
  1106. GetSceneManager()->hasManualObject("Editor_Grid_Front"))
  1107. {
  1108. // Destroy them
  1109. GetSceneManager()->destroyManualObject("Editor_Grid");
  1110. GetSceneManager()->destroySceneNode("Editor_GridSceneNode");
  1111. // Destroy orthogonal grids
  1112. GetSceneManager()->destroyManualObject("Editor_Grid_Side");
  1113. GetSceneManager()->destroySceneNode("Editor_GridSceneNode_Side");
  1114. GetSceneManager()->destroyManualObject("Editor_Grid_Front");
  1115. GetSceneManager()->destroySceneNode("Editor_GridSceneNode_Front");
  1116. }
  1117. }
  1118. #ifdef GLE_EDITOR_MODELVIEW
  1119. // TODO remove deprecated feature
  1120. /*! Set the active camera (DEPRECATED)
  1121. * @param[in] tokens List of tokens
  1122. */
  1123. void Editor::MVCameraSet( vector<Ogre::String> tokens )
  1124. {
  1125. // Parse the packet
  1126. int iCameraType = ParseInt(tokens[2]);
  1127. int iSizeX = ParseInt(tokens[3]);
  1128. int iSizeY = ParseInt(tokens[4]);
  1129. float fFov = ParseFloat(tokens[5]);
  1130. if (iCameraType==0)
  1131. m_pEngine->GetGameScreen("BlankScreen")->SetActiveCamera( m_pRenderer->getSceneManager("BlankScreenSceneManager")->getCamera("CameraPerspective"));
  1132. if (iCameraType==1)
  1133. m_pEngine->GetGameScreen("BlankScreen")->SetActiveCamera( m_pRenderer->getSceneManager("BlankScreenSceneManager")->getCamera("CameraTop"));
  1134. if (iCameraType==2)
  1135. m_pEngine->GetGameScreen("BlankScreen")->SetActiveCamera( m_pRenderer->getSceneManager("BlankScreenSceneManager")->getCamera("CameraSide"));
  1136. if (iCameraType==3)
  1137. m_pEngine->GetGameScreen("BlankScreen")->SetActiveCamera( m_pRenderer->getSceneManager("BlankScreenSceneManager")->getCamera("CameraFront"));
  1138. // HACK FOV commented out
  1139. // m_pEngine->GetGameScreen("BlankScreen")->GetActiveCamera()->setFOVy(Ogre::Radian(fFov));
  1140. }
  1141. #endif
  1142. /*! Set the active camera in editor mode
  1143. * @param[in] tokens List of tokens
  1144. */
  1145. void Editor::CameraSet( vector<Ogre::String> tokens )
  1146. {
  1147. // Parse the packet
  1148. int iCameraType = ParseInt(tokens[2]);
  1149. int iSizeX = ParseInt(tokens[3]);
  1150. int iSizeY = ParseInt(tokens[4]);
  1151. float fFov = ParseFloat(tokens[5]);
  1152. // Initialize the camera just in case the scene manager doesn't have them
  1153. InitEditorCameras(GetSceneManager(), false);
  1154. //Check if the grids are present
  1155. bool hastop = GetSceneManager()->hasManualObject("Editor_Grid");
  1156. bool hasside = GetSceneManager()->hasManualObject("Editor_Grid_Side");
  1157. bool hasfront = GetSceneManager()->hasManualObject("Editor_Grid_Front");
  1158. // Set the camera and make sure the relevant grid is visible
  1159. if (iCameraType==0) {
  1160. m_pEngine->GetForemostGameScreen()->SetActiveCamera( GetSceneManager()->getCamera("Editor_CameraPerspective"));
  1161. if(hastop) {
  1162. GetSceneManager()->getManualObject("Editor_Grid")->setVisible(true);
  1163. }
  1164. if(hasside) {
  1165. GetSceneManager()->getManualObject("Editor_Grid_Side")->setVisible(false);
  1166. }
  1167. if(hasfront) {
  1168. GetSceneManager()->getManualObject("Editor_Grid_Front")->setVisible(false);
  1169. }
  1170. }
  1171. if (iCameraType==1) {
  1172. m_pEngine->GetForemostGameScreen()->SetActiveCamera( GetSceneManager()->getCamera("Editor_CameraTop"));
  1173. if(hastop) {
  1174. GetSceneManager()->getManualObject("Editor_Grid")->setVisible(true);
  1175. }
  1176. if(hasside) {
  1177. GetSceneManager()->getManualObject("Editor_Grid_Side")->setVisible(false);
  1178. }
  1179. if(hasfront) {
  1180. GetSceneManager()->getManualObject("Editor_Grid_Front")->setVisible(false);
  1181. }
  1182. }
  1183. if (iCameraType==2){
  1184. m_pEngine->GetForemostGameScreen()->SetActiveCamera( GetSceneManager()->getCamera("Editor_CameraSide"));
  1185. if(hastop) {
  1186. GetSceneManager()->getManualObject("Editor_Grid")->setVisible(false);
  1187. }
  1188. if(hasside) {
  1189. GetSceneManager()->getManualObject("Editor_Grid_Side")->setVisible(true);
  1190. }
  1191. if(hasfront) {
  1192. GetSceneManager()->getManualObject("Editor_Grid_Front")->setVisible(false);
  1193. }
  1194. }
  1195. if (iCameraType==3){
  1196. m_pEngine->GetForemostGameScreen()->SetActiveCamera( GetSceneManager()->getCamera("Editor_CameraFront"));
  1197. if(hastop) {
  1198. GetSceneManager()->getManualObject("Editor_Grid")->setVisible(false);
  1199. }
  1200. if(hasside) {
  1201. GetSceneManager()->getManualObject("Editor_Grid_Side")->setVisible(false);
  1202. }
  1203. if(hasfront) {
  1204. GetSceneManager()->getManualObject("Editor_Grid_Front")->setVisible(true);
  1205. }
  1206. }
  1207. // FOV
  1208. // Size
  1209. //m_pEngine->GetForemostGameScreen()->GetActiveCamera()->setFOVy(Ogre::Radian(fFov));
  1210. }
  1211. /*! Create an editor camera
  1212. * @param[in] pSceneManager Scene manager (the camera will only be created once in every scene manager)
  1213. * @param[in] sCameraSceneNodeName Name of the scene node that will contain the camera
  1214. * @param[in] sCameraName Name of the camera
  1215. * @param[out] bNeedToInitCamera True, we need to initialize the camera because it has been created
  1216. * @return Pointer to the camera created
  1217. */
  1218. Ogre::Camera* Editor::CreateEditorCamera( Ogre::SceneManager* pSceneManager, Ogre::String sCameraSceneNodeName, Ogre::String sCameraName, bool * bNeedToInitCamera)
  1219. {
  1220. Ogre::Camera * pCamera;
  1221. Ogre::SceneNode* pCameraSN;
  1222. // Check if the scene node was created
  1223. if( !pSceneManager->hasSceneNode(sCameraSceneNodeName) ) {
  1224. pCameraSN = pSceneManager->getRootSceneNode()->createChildSceneNode(sCameraSceneNodeName);
  1225. *bNeedToInitCamera = true;
  1226. }else{
  1227. pCameraSN = pSceneManager->getSceneNode(sCameraSceneNodeName);
  1228. *bNeedToInitCamera = false;
  1229. }
  1230. // Check if the camera was created
  1231. if( !pSceneManager->hasCamera(sCameraName) )
  1232. {
  1233. pCamera = pSceneManager->createCamera(sCameraName);
  1234. // Attach the new camera
  1235. pCameraSN->attachObject(pCamera);
  1236. *bNeedToInitCamera = true;
  1237. }else{
  1238. pCamera = pSceneManager->getCamera(sCameraName);
  1239. *bNeedToInitCamera = false;
  1240. }
  1241. return pCamera;
  1242. }
  1243. /*! Initialize all the editor cameras
  1244. * @param[in] pSceneManager Scene manager (the cameras will only be created once in every scene manager)
  1245. * @param[in] bNeedToInitCamera True, we need to initialize the camera
  1246. */
  1247. void Editor::InitEditorCameras( Ogre::SceneManager* pSceneManager, bool bNeedToResetCameras )
  1248. {
  1249. Ogre::Camera * pCamera;
  1250. bool bRes = false;
  1251. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1252. // Perspective camera
  1253. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1254. pCamera = CreateEditorCamera(pSceneManager, "Editor_CameraPerspective_SceneNode", "Editor_CameraPerspective", &bRes );
  1255. // Check if we have to initialize the camera
  1256. if(bRes || bNeedToResetCameras)
  1257. {
  1258. InputPtr->m_fRadius = 40.0;
  1259. InputPtr->m_fYawValue = 0.0;
  1260. InputPtr->m_fPitchValue = -Ogre::Math::PI;
  1261. // Set camera parameters
  1262. pCamera->setNearClipDistance((Ogre::Real)0.05);
  1263. pCamera->setPosition (Ogre::Vector3(0.0000, 0.0000, 0.0000));
  1264. pCamera->setOrientation (Ogre::Quaternion::IDENTITY);
  1265. pCamera->yaw (Ogre::Degree(InputPtr->m_fYawValue));
  1266. pCamera->pitch (Ogre::Degree(InputPtr->m_fPitchValue));
  1267. pCamera->moveRelative (Ogre::Vector3(0.0,0.0, InputPtr->m_fRadius));
  1268. }
  1269. bRes = false;
  1270. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1271. // Top camera
  1272. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1273. pCamera = CreateEditorCamera(pSceneManager, "Editor_CameraTop_SceneNode", "Editor_CameraTop", &bRes );
  1274. // Check if we have to initialize the camera
  1275. if(bRes || bNeedToResetCameras)
  1276. {
  1277. // Set camera parameters
  1278. pCamera->getParentSceneNode()->setPosition( 0, 50000, 0); //was 1, caused clipping issues -11/2/09
  1279. pCamera->getParentSceneNode()->setDirection(Ogre::Vector3(0,-1,0));
  1280. pCamera->setProjectionType( Ogre::PT_ORTHOGRAPHIC );
  1281. pCamera->setNearClipDistance((Ogre::Real)0.01);
  1282. pCamera->setFarClipDistance((Ogre::Real)100000);
  1283. pCamera->setOrthoWindow(Ogre::Real(m_pEngine->GetRenderWindow()->getWidth()/2), Ogre::Real(m_pEngine->GetRenderWindow()->getHeight()/2));
  1284. }
  1285. bRes = false;
  1286. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1287. // Init Side Camera
  1288. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1289. pCamera = CreateEditorCamera(pSceneManager, "Editor_CameraSide_SceneNode", "Editor_CameraSide", &bRes );
  1290. // Check if we have to initialize the camera
  1291. if(bRes || bNeedToResetCameras)
  1292. {
  1293. // Set camera parameters
  1294. pCamera->getParentSceneNode()->setPosition( 50000, 0, 0); //was 1, caused clipping issues -11/2/09
  1295. pCamera->getParentSceneNode()->setDirection(Ogre::Vector3(-1,0,0));
  1296. pCamera->setProjectionType( Ogre::PT_ORTHOGRAPHIC );
  1297. pCamera->setNearClipDistance((Ogre::Real)0.01);
  1298. pCamera->setFarClipDistance(100000);
  1299. pCamera->setOrthoWindow(Ogre::Real(m_pEngine->GetRenderWindow()->getWidth()/2), Ogre::Real(m_pEngine->GetRenderWindow()->getHeight()/2));
  1300. }
  1301. bRes = false;
  1302. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1303. // Init Front Camera
  1304. ////////////