uiMgr.cpp

Go to the documentation of this file.
00001 /*
00002  * uiMgr.cpp
00003  *
00004  *  Created on: Feb 18, 2012
00005  *      Author: sushil
00006  */
00007 
00008 //using namespace OgreGFX;
00009 
00010 #include<cfloat>
00011 
00012 #include "DEBUG.h"
00013 #include <engine.h>
00014 #include <uiMgr.h>
00015 #include <GraphicsInteractionManager.h>
00016 
00017 #include <DebugDrawer.h>
00018 
00019 #include <groupAI.h>
00020 #include <unitAI.h>
00021 #include <flock.h>
00022 #include <target.h>
00023 #include <command.h>
00024 
00025 #include <commandHelp.h>
00026 #include <const.h>
00027 #include <enums.h>
00028 
00029 #include <ent.h>
00030 
00031 #include <creationMouseHandler.h>
00032 #include <controlGroupsHandler.h>
00033 #include "Rect.h"
00034 #include "HealthBar.h"
00035 
00036 OgreGFX::UIMgr::UIMgr(OgreGFX::GraphicsInteractionManager *gim) : GFXMgr(gim), OIS::KeyListener(), OIS::MouseListener(), bars(HealthBar(gim->mSceneMgr->createManualObject("manual"), 100.0f, true))
00037 {//Engine* eng,  Ogre::RenderWindow *win, Ogre::SceneManager *sm, Ogre::Camera* cam): OgreGFX::Mgr(eng), trayMgr(0), inputManager(0), mouse(0), keyboard(0) {
00038     shutDown = false;
00039     selectionDistanceSquaredThreshold = 10000;
00040     camera = gfx->mCamera;
00041     renderWindow = gfx->mWindow;
00042     sceneManager = gfx->mSceneMgr;
00043     createInputSystem();
00044     //then
00045     cameraMgr = new OgreGFX::CameraMgr(gfx); //, camera);
00046 
00047     selectionBox = new OgreGFX::SelectionBox("SelectionBox");
00048     sceneManager->getRootSceneNode()->createChildSceneNode()->attachObject(selectionBox);
00049     volQuery = sceneManager->createPlaneBoundedVolumeQuery(Ogre::PlaneBoundedVolumeList());
00050     rayDistanceForVolume = 10;
00051     selectingNow = false;
00052     volumeSelectingNow = false;
00053 
00054     clearModifiersDown();
00055     //currentSelection = gim->gfxNodes[0].node;
00056 
00057     new DebugDrawer(gfx->mSceneMgr, 0.5f);
00058     this->posUnderMouse = Ogre::Vector3::NEGATIVE_UNIT_Y;
00059     sceneManager->getRootSceneNode()->createChildSceneNode()->attachObject(bars.emptyBar->mObj);
00060 
00061 }
00062 
00063 void OgreGFX::UIMgr::initialize()
00064 {
00065     DEBUG(std::cout << "Calling uiMgr initializer" << std::endl;)
00066 
00067 
00068     std::set<OIS::KeyCode> *creationMods = new std::set<OIS::KeyCode > ();
00069     creationMods->insert(OIS::KC_LCONTROL);
00070     CreationMouseHandler *cmh = new CreationMouseHandler(this->gfx, creationMods);
00071     this->registerMouseHandler(creationMods, OIS::MB_Left, cmh);
00072     DEBUG(std::cout << "Registered creation mouse handler" << std::endl;)
00073 
00074     controlGroupsHandler = new ControlGroupsHandler(gfx);
00075 
00076     minimap = new Minimap(gfx);
00077     //minimap->init();
00078 
00079 }
00080 
00081 void OgreGFX::UIMgr::clearModifiersDown()
00082 {
00083     shiftDown = false;
00084     ctrlDown = false;
00085     altDown = false;
00086 }
00087 
00088 void OgreGFX::UIMgr::createInputSystem()
00089 {
00090     //Ogre::LogManager::getSingletonPtr()->logMessage("*** Initializing OIS ***");
00091     DEBUG(std::cout << "*** Initializing OIS ***" << std::endl;)
00092     OIS::ParamList pl;
00093     size_t windowHnd = 0;
00094     std::ostringstream windowHndStr;
00095 
00096     renderWindow->getCustomAttribute("WINDOW", &windowHnd);
00097     windowHndStr << windowHnd;
00098     pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
00099 
00100 
00101 #if defined OIS_LINUX_PLATFORM
00102     pl.insert(std::make_pair(std::string("x11_mouse_grab"), std::string("false")));
00103     pl.insert(std::make_pair(std::string("x11_mouse_hide"), std::string("false")));
00104     pl.insert(std::make_pair(std::string("x11_keyboard_grab"), std::string("false")));
00105     pl.insert(std::make_pair(std::string("XAutoRepeatOn"), std::string("true")));
00106 #elif defined OIS_WIN32_PLATFORM
00107     pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND")));
00108     pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCUSIVE")));
00109     pl.insert(std::make_pair(std::string("w32_keyboard"), std::string("DISCL_FOREGROUND")));
00110     pl.insert(std::make_pair(std::string("w32_keyboard"), std::string("DISCL_NONEXCLUSIVE")));
00111 #endif
00112 
00113 
00114     inputManager = OIS::InputManager::createInputSystem(pl);
00115 
00116     keyboard = static_cast<OIS::Keyboard*> (inputManager->createInputObject(OIS::OISKeyboard, true));
00117     mouse = static_cast<OIS::Mouse*> (inputManager->createInputObject(OIS::OISMouse, true));
00118     mouse->capture();
00119     ms = mouse->getMouseState();
00120     ms.width = gfx->mWindow->getWidth();
00121     ms.height = gfx->mWindow->getHeight();
00122 
00123     mouse->setEventCallback(this);
00124     keyboard->setEventCallback(this);
00125 
00126     //Set initial mouse clipping size
00127     windowResized(renderWindow);
00128 
00129     //Register as a Window listener
00130     Ogre::WindowEventUtilities::addWindowEventListener(renderWindow, this);
00131 
00132     //mRoot->addFrameListener(this); Added in createInputSystem in GraphicsInteractionManager
00133 }
00134 
00135 void OgreGFX::UIMgr::DebugDrawTest()
00136 {
00137     for (int i = 0; i < 5; ++i)
00138     {
00139         for (int j = 0; j < 5; j++)
00140         {
00141             for (int k = 0; k < 5; k++)
00142             {
00143                 Ogre::AxisAlignedBox box(Ogre::Vector3(i * 10.0f + 2.0f, j * 10.0f + 2.0f, k * 10.0f + 2.0f),
00144                                          Ogre::Vector3((i + 1) * 10.0f - 2.0f, (j + 1) * 10.0f - 2.0f, (k + 1) * 10.0f - 2.0f));
00145                 DebugDrawer::getSingleton().drawCuboid(box.getAllCorners(),
00146                                                        Ogre::ColourValue(51.0f * i / 255.0f, 51.0f * j / 255.0f, 51.0f * k / 255.0f), true);
00147             }
00148         }
00149     }
00150     //DebugDrawer::getSingletonPtr()->build();
00151 }
00152 
00153 bool OgreGFX::UIMgr::frameStarted(const Ogre::FrameEvent& evt)
00154 {
00155     //DebugDrawTest();
00156     //drawSelectionCircles();
00157     decorateSelectedEntities();
00158     drawUnderMouseCircle();
00159 
00160     DebugDrawer::getSingletonPtr()->build(); //One build to draw all the debug objects
00161 
00162     DEBUG(std::cout << renderWindow->getAverageFPS() << std::endl;)
00163     return true;
00164 }
00165 
00166 bool OgreGFX::UIMgr::frameEnded(const Ogre::FrameEvent& evt)
00167 {
00168     DebugDrawer::getSingletonPtr()->clear();
00169     bars.ClearVertices();
00170     //  for (std::list<OgreGFX::GFXNode*>::iterator it = currentSelections.begin(); it != currentSelections.end(); ++it){
00171     //          FastEcslent::Entity * ent = gfx->engine->entityMgr->ents[(*it)->id];
00172     //          if (fabs(ent->attractivePotential) < 10 ) {
00173     //                  std::cout << "Potential vector length, attractive Potential: " << ent->potentialVec.length() << ", "
00174     //                                  << ent->attractivePotential << std::endl;
00175     //          }
00176     //  }
00177     return true;
00178 }
00179 
00180 bool mouseDidScroll(int z)
00181 {
00182     return (z < -1.0 || z > 1.0);
00183 }
00184 
00185 bool OgreGFX::UIMgr::frameRenderingQueued(const Ogre::FrameEvent& evt)
00186 {
00187     if (renderWindow->isClosed())
00188         return false;
00189 
00190     //Need to capture/update each device
00191     keyboard->capture();
00192     mouse->capture();
00193 
00194     cameraMgr->frameRenderingQueued(evt); // if dialog isn't up, then update the camera
00195 
00196     return true;
00197 }
00198 
00199 void OgreGFX::UIMgr::checkSetModifiers(const OIS::KeyEvent &arg, bool value)
00200 {
00201     if (arg.key == OIS::KC_LSHIFT || arg.key == OIS::KC_RSHIFT)
00202     {
00203         shiftDown = value;
00204 
00205     }
00206     else if (arg.key == OIS::KC_LCONTROL || arg.key == OIS::KC_RCONTROL)
00207     {
00208         ctrlDown = value;
00209 
00210     }
00211     else if (arg.key == OIS::KC_LMENU || arg.key == OIS::KC_RMENU)
00212     {
00213         altDown = value;
00214 
00215     }
00216 
00217 }
00218 
00219 bool OgreGFX::UIMgr::keyPressed(const OIS::KeyEvent &arg)
00220 {
00221     if (arg.key == OIS::KC_ESCAPE)
00222     {
00223         shutDown = true;
00224         //return false;
00225     }
00226     DEBUG(std::cout << "Key pressed: " << arg.key << std::endl;)
00227     checkSetModifiers(arg, true);
00228 
00229     //  else if (arg.key == OIS::KC_F12) {
00230     //buildCubes();
00231     //}
00232 
00233     // Will need to check if camera related keys were pressed.
00234     // if a camera related key is pressed:
00235     cameraMgr->injectKeyDown(arg);
00236     // else if a registered key combo is pressed:
00237     //call key handler
00238     //else
00239     // do nothing
00240 
00241     callKeyHandlers(arg);
00242     DEBUG(std::cout << "Control down is: " << ctrlDown << std::endl;)
00243     controlGroupsHandler->checkHandleControlGroupKeys(ctrlDown, arg.key);
00244 
00245     return true;
00246 }
00247 
00248 bool OgreGFX::UIMgr::keyReleased(const OIS::KeyEvent &arg)
00249 {
00250     checkSetModifiers(arg, false);
00251     cameraMgr->injectKeyUp(arg);
00252     return true;
00253 }
00254 
00255 bool OgreGFX::UIMgr::mouseMoved(const OIS::MouseEvent &arg)
00256 {
00257     if (selectingNow)
00258     {
00259         volumeSelectingNow = true;
00260         ms = mouse->getMouseState();
00261         stopPos.x = ms.X.abs / (float) ms.width;
00262         stopPos.y = ms.Y.abs / (float) ms.height;
00263         selectionBox->setCorners(startPos, stopPos);
00264     }
00265     else if (mouseDidScroll(arg.state.Z.rel) && !arg.state.buttonDown(OIS::MB_Left) && !arg.state.buttonDown(OIS::MB_Right) && !arg.state.buttonDown(OIS::MB_Middle))
00266     {
00267         cameraMgr->handleMouseScrollCameraZoom(arg);
00268     }
00269     //else if ()
00270     this->gfx->widgetMgr->mouseOverButton(arg);
00271     return true;
00272 }
00273 
00274 //-------------------------Manage Selection ---------------------------------------------
00275 
00276 void OgreGFX::UIMgr::decorateSelectedEntities()
00277 {
00278     Ogre::Vector3 pos;
00279     Ogre::Vector3 start;
00280     Ogre::Vector3 end;
00281     float selectionCircleRadius = 100.0f;
00282     FastEcslent::Entity* ent;
00283     int entId;
00284     for (std::list<OgreGFX::GFXNode*>::iterator it = currentSelections.begin(); it != currentSelections.end(); ++it)
00285     {
00286         pos = (*it)->node->getPosition();
00287         Ogre::Vector3 cpos = Ogre::Vector3(pos.x, pos.y + 1, pos.z);
00288         selectionCircleRadius = gfx->engine->entityMgr->ents[(*it)->id]->length;
00289         DebugDrawer::getSingleton().drawCircle(cpos, selectionCircleRadius, FastEcslent::NCircleSegments, Ogre::ColourValue(0.5f, 0.5f, 0.9f, 0.2f), true);
00290 
00291         entId = (*it)->id;
00292         ent = gfx->engine->entityMgr->ents[entId];
00293         start = ent->pos;
00294 
00295         if (ent->entityClass != FastEcslent::STATIC)
00296         {
00297             float lineLength = ent->length * (1.0 + (ent->speed / ent->speedRange));
00298             DebugDrawer::getSingleton().drawAngleLine(cpos, Ogre::Radian(ent->heading), lineLength, Ogre::ColourValue(1.0f, 1.0f, 0.0f, 1.0));
00299             lineLength = ent->length * (1.0 + ent->desiredSpeed / ent->speedRange);
00300             DebugDrawer::getSingleton().drawAngleLine(cpos, Ogre::Radian(ent->desiredHeading), lineLength, Ogre::ColourValue(1.0f, 1.0f, 1.0f, 0.5));
00301         }
00302         Ogre::Vector3 leftPoint = Ogre::Vector3(cpos.x - FastEcslent::healthLineLength / 2, cpos.y, cpos.z - ent->length);
00303         Ogre::Vector3 rightPoint = Ogre::Vector3(cpos.x + FastEcslent::healthLineLength / 2, cpos.y, cpos.z - ent->length);
00304         DebugDrawer::getSingleton().drawLine(leftPoint, rightPoint, Ogre::ColourValue(1.0f, 0.0f, 0.0f, 0.5f));
00305         DEBUG(std::cout << "Hit points: " << ent->weapon->hitpoints << " Max Hit Points: " << gfx->engine->weaponMgr->maxHitpoints[ent->entityType] << std::endl;)
00306         float healthNow = FastEcslent::healthLineLength * ent->weapon->hitpoints / gfx->engine->weaponMgr->maxHitpoints[ent->entityType];
00307         rightPoint.x = leftPoint.x + healthNow;
00308         DebugDrawer::getSingleton().drawLine(leftPoint, rightPoint, Ogre::ColourValue(0.0f, 1.0f, 0.0f, 0.5f));
00309 
00310 
00311         bars.percent = 100.0 * (ent->weapon->hitpoints / gfx->engine->weaponMgr->maxHitpoints[ent->entityType]);
00312         bars.Draw(Ogre::Vector3(pos.x, pos.y + 1, pos.z - selectionCircleRadius));
00313 
00314 
00315         // Debug potential fields
00316         //DebugDrawer::getSingleton().drawAngleLine(cpos, Ogre::Radian(ent->potentialVec.angleBetween(Ogre::Vector3::UNIT_X)), ent->potentialVec.length() * 100, Ogre::ColourValue(1.0f, 0.0f, 0.0f, 1.0));
00317         //DebugDrawer::getSingleton().drawLine(cpos, cpos + (ent->potentialVec * 100), Ogre::ColourValue(1.0f, 0.0f, 0.0f, 1.0));
00318     }
00319 
00320 }
00321 
00322 
00323 //void OgreGFX::UIMgr::drawSelectionCircles(){
00324 //      Ogre::Vector3 pos;
00325 //
00326 //      for (std::list<OgreGFX::GFXNode*>::iterator it = currentSelections.begin(); it != currentSelections.end(); ++it){
00327 //              pos = (*it)->node->getPosition();
00328 //              Ogre::Vector3 cpos = Ogre::Vector3(pos.x, pos.y+1, pos.z);
00329 //              //DebugDrawer::getSingleton().drawCircle((*it)->node->getPosition(), 50.0f, 8, Ogre::ColourValue(255.0f, 255.0f, 255.0f), true );
00330 //              //DebugDrawer::getSingleton().drawCircle(cpos, 50.0f, 8, Ogre::ColourValue(255.0f, 255.0f, 255.0f), true );
00331 //              float radius = gfx->engine->entityMgr->ents[(*it)->id]->length;
00332 //              DebugDrawer::getSingleton().drawCircle(cpos, radius, FastEcslent::NCircleSegments, Ogre::ColourValue(0.0f, 1.0f, 0.0f, 0.5f), true );
00333 //      }
00334 //}
00335 
00336 void OgreGFX::UIMgr::drawUnderMouseCircle()
00337 {
00338     Ogre::Vector3 pos;
00339     Ogre::Vector3 newPos;
00340     std::pair<int, Ogre::Vector3> underMouse;
00341     float radius;
00342     int id;
00343     underMouse = entIndexUnderMouse();
00344     if (underMouse.first >= 0 && underMouse.first < gfx->nGFXNodes)
00345     {
00346         DEBUG(std::cout << "Found an ent under mouse" << "\n";)
00347         id = gfx->gfxNodes[underMouse.first].id;
00348         pos = gfx->engine->entityMgr->ents[id]->pos;
00349         newPos = Ogre::Vector3(pos.x, pos.y + 1, pos.z);
00350         radius = gfx->engine->entityMgr->ents[id]->length;
00351         DebugDrawer::getSingleton().drawCircle(newPos, radius * 1.5, FastEcslent::NCircleSegments, Ogre::ColourValue(0.0f, 0.0f, 1.0f, 1.0f), false);
00352     }
00353 }
00354 
00355 std::pair<int, Ogre::Vector3> OgreGFX::UIMgr::entIndexUnderMouse()
00356 {
00357     OIS::MouseState ms;
00358     ms = mouse->getMouseState();
00359     return entIndexUnderMouse(ms);
00360 }
00361 
00362 std::pair<int, Ogre::Vector3> OgreGFX::UIMgr::entIndexUnderMouse(const OIS::MouseEvent &arg)
00363 {
00364     OIS::MouseState ms;
00365     ms = mouse->getMouseState();
00366     return entIndexUnderMouse(ms);
00367 }
00368 
00369 std::pair<bool, Ogre::Vector3> OgreGFX::UIMgr::getMouseWorldPos(const OIS::MouseState &arg)
00370 {
00371     Ogre::Vector3 pos = Ogre::Vector3::NEGATIVE_UNIT_Y;
00372     Ogre::Ray mouseRay = camera->getCameraToViewportRay(arg.X.abs / (float) ms.width, arg.Y.abs / (float) ms.height);
00373 
00374     std::pair<bool, float> result = mouseRay.intersects(gfx->oceanSurface);
00375     if (result.first)
00376     {
00377         pos = mouseRay.getPoint(result.second);
00378     }
00379     //result.second = mouseRay.getPoint(result.second);
00380     return std::pair<bool, Ogre::Vector3 > (result.first, pos);
00381 
00382 }
00383 
00384 std::pair<int, Ogre::Vector3> OgreGFX::UIMgr::entIndexUnderMouse(const OIS::MouseState &arg)
00385 {
00386 
00387     int index = -1;
00388     std::pair<bool, Ogre::Vector3> mousePos = getMouseWorldPos(arg);
00389     //Ogre::Vector3 pos = Ogre::Vector3::NEGATIVE_UNIT_Y;
00390     Ogre::Ray mouseRay = camera->getCameraToViewportRay(arg.X.abs / (float) ms.width, arg.Y.abs / (float) ms.height);
00391     //this->raySceneQuery->setRay(mouseRay);
00392     std::pair<bool, float> result = mouseRay.intersects(gfx->oceanSurface);
00393     if (result.first)
00394     {
00395         this->posUnderMouse = mouseRay.getPoint(result.second);
00396         //this->posUnderMouse = pos;
00397         float minDistanceSquared = FLT_MAX;
00398         float distanceSquared;
00399         // find ent that is 1. Selectable && 2. Within threshold distance && 3. Nearest to mouse cursor
00400         for (int i = 0; i < gfx->nGFXNodes; i++)
00401         {
00402             if (gfx->gfxNodes[i].selectable)
00403             {
00404                 distanceSquared = this->posUnderMouse.squaredDistance(gfx->gfxNodes[i].node->getPosition());
00405                 if (distanceSquared < selectionDistanceSquaredThreshold)
00406                 {
00407                     if (distanceSquared < minDistanceSquared)
00408                     {
00409                         index = i;
00410                         minDistanceSquared = distanceSquared;
00411                     }
00412                 }
00413             }
00414         }
00415     }
00416     return std::pair<int, Ogre::Vector3 > (index, this->posUnderMouse);
00417 }
00418 
00419 void OgreGFX::UIMgr::selectEntity(int index)
00420 {
00421     currentSelections.push_back(&gfx->gfxNodes[index]); // add node to empty or non-empty list
00422     //gfx->gfxNodes[index].node->showBoundingBox(true);   // and show boundingbox
00423     //gfx->engine->entityMgr->ens[gfxNode.id] is entity
00424 }
00425 
00426 void OgreGFX::UIMgr::handleSingleSelection(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
00427 {
00428 
00429     if (volumeSelectingNow) return;
00430     std::pair<int, Ogre::Vector3> result = entIndexUnderMouse(arg);
00431     int index = result.first;
00432     // Found an ent or none
00433     if (index >= 0 && index < gfx->nGFXNodes)
00434     { //if something is near enough the cursor to be selected
00435         if (!shiftDown)
00436         { // if it is not to be added to the selection
00437             clearSelectionsAndUpdateFEEngine(); //clear currently selected
00438         }
00439         selectEntity(index);
00440 
00441     }
00442     else
00443     { //None: nothing close enough to be selected, so clear selections
00444         clearSelectionsAndUpdateFEEngine();
00445     }
00446     updateFEEngineWithSelections();
00447 }
00448 
00449 FastEcslent::Group* OgreGFX::UIMgr::groupFromSelections()
00450 {
00451 
00452     FastEcslent::Entity * ents[FastEcslent::MaxEnts];
00453     int i = 0;
00454     for (std::list<OgreGFX::GFXNode*>::iterator it = currentSelections.begin(); it != currentSelections.end(); ++it)
00455     {
00456         ents[i++] = gfx->engine->entityMgr->ents[(*it)->id];
00457     }
00458     FastEcslent::Group* group = gfx->engine->groupMgr->createGroup(ents, i);
00459 
00460     //group->addMember(gfx->engine->entityMgr->ents[(*it)->id]);
00461     return group;
00462 }
00463 
00464 void OgreGFX::UIMgr::handleVolumeSelection(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
00465 {
00466 
00467     startPos.x = arg.state.X.abs / (float) ms.width;
00468     startPos.y = arg.state.Y.abs / (float) ms.height;
00469     stopPos = startPos;
00470     selectingNow = true;
00471     selectionBox->clear();
00472     selectionBox->setVisible(true);
00473 }
00474 
00475 void OgreGFX::UIMgr::printCurrentSelections()
00476 {
00477     for (std::list<OgreGFX::GFXNode*>::iterator it = currentSelections.begin(); it != currentSelections.end(); ++it)
00478     {
00479         DEBUG(std::cout << (*it)->id << ", ";)
00480     }
00481     DEBUG(std::cout << std::endl;)
00482     //  for (std::list<OgreGFX::GFXNode*>::iterator it = currentSelections.begin(); it != currentSelections.end(); it++){
00483     //          std::cout << (*it)->id << ", ";
00484     //  }
00485     DEBUG(std::cout << std::endl;)
00486 }
00487 
00488 void OgreGFX::UIMgr::updateFEEngineWithSelections()
00489 {
00490     boost::mutex::scoped_lock scoped_lock(selectionUpdateLock);
00491     for (std::list<OgreGFX::GFXNode*>::iterator it = currentSelections.begin(); it != currentSelections.end(); ++it)
00492     {
00493         gfx->engine->selectionMgr->select((*it)->id);
00494     }
00495 
00496     if (currentSelections.size() == 0)
00497     {
00498         gfx->widgetMgr->deactivateMenu();
00499     }
00500     else if (currentSelections.size() == 1)
00501     {
00502         gfx->widgetMgr->activateMenu(gfx->engine->entityMgr->ents[(*currentSelections.begin())->id]->entityType);
00503 
00504     }
00505     else
00506     {
00507         gfx->widgetMgr->activateMenu(FastEcslent::NENTITYTYPES);
00508     }
00509 }
00510 
00511 void OgreGFX::UIMgr::clearSelectionsAndUpdateFEEngine()
00512 {
00513     boost::mutex::scoped_lock scoped_lock(selectionUpdateLock);
00514     for (std::list<OgreGFX::GFXNode*>::iterator it = currentSelections.begin(); it != currentSelections.end(); ++it)
00515     {
00516         //boost::mutex::scoped_lock scoped_lock(selectionUpdateLock);
00517         gfx->engine->selectionMgr->unselect((*it)->id);
00518     }
00519 
00520     currentSelections.clear();
00521 }
00522 
00523 void swap(float &x, float &y)
00524 {
00525     float tmp = x;
00526     x = y;
00527     y = tmp;
00528 }
00529 
00530 void OgreGFX::UIMgr::doVolumeSelection(const Ogre::Vector2& first, const Ogre::Vector2& second)
00531 {
00532 
00533     float left = first.x, right = second.x, top = first.y, bottom = second.y;
00534     if (left > right) swap(left, right);
00535     if (top > bottom) swap(top, bottom);
00536     if ((right - left) * (bottom - top) < 0.01) return;
00537 
00538     Ogre::Ray topLeft = camera->getCameraToViewportRay(left, top);
00539     Ogre::Ray topRight = camera->getCameraToViewportRay(right, top);
00540     Ogre::Ray bottomLeft = camera->getCameraToViewportRay(left, bottom);
00541     Ogre::Ray bottomRight = camera->getCameraToViewportRay(right, bottom);
00542 
00543     Ogre::PlaneBoundedVolume vol;
00544     vol.planes.push_back(Ogre::Plane(topLeft.getPoint(3), topRight.getPoint(3), bottomRight.getPoint(3))); //front (closest to cam) plane
00545     vol.planes.push_back(Ogre::Plane(topLeft.getOrigin(), topLeft.getPoint(10), topRight.getPoint(10))); //top plane
00546     vol.planes.push_back(Ogre::Plane(topLeft.getOrigin(), bottomLeft.getPoint(10), topLeft.getPoint(10))); //left plane
00547     vol.planes.push_back(Ogre::Plane(bottomLeft.getOrigin(), bottomRight.getPoint(10), bottomLeft.getPoint(10))); //bottom Plane
00548     vol.planes.push_back(Ogre::Plane(topRight.getOrigin(), topRight.getPoint(10), bottomRight.getPoint(10))); //right Plane
00549 
00550     Ogre::PlaneBoundedVolumeList volList;
00551     volList.push_back(vol);
00552 
00553     volQuery->setVolumes(volList);
00554     Ogre::SceneQueryResult result = volQuery->execute();
00555     clearSelectionsAndUpdateFEEngine();
00556 
00557     Ogre::SceneQueryResultMovableList::iterator iter; // for some reason the last sceneNode is always wrong
00558     int n = result.movables.size(); //have to throw away last item in iterator. I don't know why?
00559     //  int i = 0;                      //ToDo -> fix iterator, why is it n-1?
00560     // The issue is std::map with returns an iterator that points to the end of the map if the key is not in the map.
00561     //Causes all kinds of strange behavior
00562     DEBUG(std::cout << "Number of movables: " << n << std::endl;)
00563 
00564     for (iter = result.movables.begin(); iter != result.movables.end(); ++iter)
00565     {
00566         //for(iter = result.movables.begin(); iter != result.movables.end(); ++iter){
00567         //std::cout << gfx->sceneNodeToEntIdMap[(*iter)->getParentSceneNode()] << std::endl;
00568         if (gfx->sceneNodeToEntIdMap.find((*iter)->getParentSceneNode()) != gfx->sceneNodeToEntIdMap.end())
00569         {
00570             selectEntity(gfx->sceneNodeToEntIdMap[(*iter)->getParentSceneNode()]); //why parent scene node?
00571         }
00572     }
00573     updateFEEngineWithSelections();
00574 }
00575 //-------------------------End Manage Selection ---------------------------------------------
00576 
00577 //------------------------Handle Commands------------------------------------------------
00578 
00579 void OgreGFX::UIMgr::CommandMaintainAttack(int i)
00580 {
00581     DEBUG(std::cout << "MaintainAttacking: (" << gfx->engine->entityMgr->ents[i]->uiname << ")" << std::endl;)
00582 }
00583 
00584 void OgreGFX::UIMgr::CommandGatherOrPFMove(int i)
00585 {
00586     DEBUG(std::cout << "Gathing (" << gfx->engine->entityMgr->ents[i]->uiname << ")" << std::endl;)
00587         FastEcslent::Entity * ent;
00588         for (std::list<OgreGFX::GFXNode*>::iterator it = currentSelections.begin(); it != currentSelections.end(); ++it)
00589         {
00590                 ent = gfx->engine->entityMgr->ents[(*it)->id];
00591                 if (ent->entityClass == FastEcslent::STATIC)   //buildings, continue
00592                 {
00593                         continue;
00594                 }else if (ent->entityType == FastEcslent::SCV){//scvs, gathering
00595                         FastEcslent::setGatherForEnt(ent, gfx->engine->entityMgr->ents[i], shiftDown);
00596                 }else {  //units other than scv, move to the mineral
00597                         FastEcslent::setPotentialMoveForEnt(ent, gfx->engine->entityMgr->ents[i]->pos, shiftDown);
00598                 }
00599         }
00600 }
00601 
00602 void OgreGFX::UIMgr::CommandPotentialFieldsMove(Ogre::Vector3 pos)
00603 {
00604     DEBUG(std::cout << "Potential Fields: Moving to: (" << pos.x << ", " << pos.z << ")" << std::endl;)
00605     FastEcslent::Entity * ent;
00606     for (std::list<OgreGFX::GFXNode*>::iterator it = currentSelections.begin(); it != currentSelections.end(); ++it)
00607     {
00608         ent = gfx->engine->entityMgr->ents[(*it)->id];
00609         if (ent->entityClass == FastEcslent::STATIC)
00610         {
00611             continue;
00612         }
00613         FastEcslent::setPotentialMoveForEnt(ent, pos, shiftDown);
00614     }
00615 }
00616 
00617 void OgreGFX::UIMgr::CommandMove(Ogre::Vector3 pos)
00618 {
00619     DEBUG(std::cout << "Moving to: (" << pos.x << ", " << pos.z << ")" << std::endl;)
00620 
00621     if (currentSelections.size() <= 0) return;
00622 
00623     if (currentSelections.size() > 1)
00624     {
00625         FastEcslent::Group* group = groupFromSelections();
00626         for (int i = 0; i < group->nEntitiesInGroup; i++)
00627         {
00628             group->members[i]->ai->commands.clear();
00629         }
00630         group->setLeaderByIndex(0);
00631         FastEcslent::GroupAI* gai = new FastEcslent::GroupAI(group, FastEcslent::GROUPAI);
00632         FastEcslent::GroupTarget* gt = new FastEcslent::GroupTarget;
00633         gt->target.location = pos;
00634         FastEcslent::Flock* flock = new FastEcslent::Flock(group, gt);
00635         //flock->changeLeadership(FastEcslent::MostMassive);
00636         DEBUG(std::cout << "Left Shift: " << shiftDown << std::endl;)
00637         if (shiftDown)
00638         {
00639             DEBUG(std::cout << "Adding flock" << std::endl;)
00640             FastEcslent::addMoveForEnt(gfx->engine->entityMgr->ents[currentSelections.front()->id], pos); //commandHelper
00641             //gai->addCommand(flock);
00642         }
00643         else
00644         {
00645             FastEcslent::setMoveForEnt(gfx->engine->entityMgr->ents[currentSelections.front()->id], pos); //commandHelper
00646             //gai->setCommand(flock);
00647         }
00648         group->addAspect(gai);
00649     }
00650     else
00651     {
00652         FastEcslent::Entity * ent = gfx->engine->entityMgr->ents[currentSelections.front()->id];
00653         boost::mutex::scoped_lock scoped_lock(selectionUpdateLock);
00654         FastEcslent::setPotentialMoveForEnt(ent, pos, shiftDown);
00655     }
00656 }
00657 
00658 void OgreGFX::UIMgr::handleCommand(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
00659 {
00660     std::pair<int, Ogre::Vector3> result = entIndexUnderMouse(arg);
00661     int index = result.first;
00662 
00663     if (index >= 0 && index < gfx->nGFXNodes)
00664     { //do_maintain()
00665         if(gfx->engine->entityMgr->ents[index]->entityType == FastEcslent::MINERALS){
00666                 CommandGatherOrPFMove(index);
00667         }else{
00668                 CommandMaintainAttack(index);
00669         }
00670     }
00671     else
00672     {
00673         CommandPotentialFieldsMove(result.second);
00674     }
00675     DEBUG(std::cout << "MousePos: " << getMouseWorldPos(arg.state).second << std::endl;)
00676     DEBUG(std::cout << "Game State: ";)
00677     for (int i = 0; i < this->gfx->engine->entityMgr->nEnts; i++)
00678     {
00679         DEBUG(std::cout << this->gfx->engine->entityMgr->ents[i]->entityType << "," << this->gfx->engine->entityMgr->ents[i]->pos.x << "," << this->gfx->engine->entityMgr->ents[i]->pos.y << "," << this->gfx->engine->entityMgr->ents[i]->pos.z << " || ";)
00680 
00681     }
00682     DEBUG(std::cout << std::endl;)
00683 
00684 }
00685 //------------------------Handle Mouse Commands------------------------------------------------
00686 
00687 bool OgreGFX::UIMgr::mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
00688 {
00689     DEBUG(std::cout << "Input System: Mouse Pressed (" << arg.state.X.abs << ", " << arg.state.Y.abs << ")" << std::endl;)
00690     if (this->gfx->widgetMgr->mouseOverOverlay(arg)) return true;
00691     if (id == OIS::MB_Left)
00692     { //select
00693         handleSingleSelection(arg, id);
00694         handleVolumeSelection(arg, id);
00695     }
00696     else if (id == OIS::MB_Right)
00697     {
00698         handleCommand(arg, id);
00699     }
00700     //callMouseHandlers(arg, id);
00701     return true;
00702 }
00703 
00704 bool OgreGFX::UIMgr::mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
00705 {
00706     if (id == OIS::MB_Left)
00707     {
00708         doVolumeSelection(startPos, stopPos);
00709         selectingNow = false;
00710         volumeSelectingNow = false;
00711         selectionBox->setVisible(false);
00712     }
00713     callMouseHandlers(arg, id);
00714     return true;
00715 }
00716 
00717 //----------------------------------------Handle key registration-----------------------------------
00718 
00719 bool OgreGFX::UIMgr::registerKeyHandler(const std::set<OIS::KeyCode> *modifiers, const OIS::KeyCode key, OgreGFX::KeyHandler *handler)
00720 {
00721     OgreGFX::KeySet keySet = OgreGFX::KeySet(modifiers, key);
00722     std::string keySetString = keySet.toString(); //convertKeySetToString(keySet);
00723     //keyHandlers[keySetString] = handler; // Map should make it easier to select handler to run
00724     keyHandlers[keySetString].push_back(handler); // Map should make it easier to select handler to run
00725     return true;
00726 }
00727 
00728 bool OgreGFX::UIMgr::registerMouseHandler(const std::set<OIS::KeyCode> *modifiers, const OIS::MouseButtonID buttonId, OgreGFX::MouseHandler *handler)
00729 {
00730     OgreGFX::MouseButtonKeySet mbKeySet = OgreGFX::MouseButtonKeySet(modifiers, buttonId);
00731     std::string mouseButtonKeySetString = mbKeySet.toString(); //convertMouseButtonKeySetToString(mbKeySet);
00732     //mouseHandlers[mouseButtonKeySetString] = handler;
00733     mouseHandlers[mouseButtonKeySetString].push_back(handler);
00734     return true;
00735 }
00736 
00737 // --------------------------------------Call key/mouse Handlers-------------------------------------------
00738 
00739 std::set<OIS::KeyCode> *OgreGFX::UIMgr::makeModifiersSet()
00740 {
00741     std::set<OIS::KeyCode> *mods = new std::set<OIS::KeyCode > ();
00742     if (shiftDown)
00743         mods->insert(OIS::KC_LSHIFT);
00744     if (altDown)
00745         mods->insert(OIS::KC_LMENU);
00746     if (ctrlDown)
00747         mods->insert(OIS::KC_LCONTROL);
00748     return mods;
00749 }
00750 
00751 std::string OgreGFX::UIMgr::modifierKeyHash(const OIS::KeyCode keyCode)
00752 {
00753 
00754     std::set<OIS::KeyCode> *mods = makeModifiersSet();
00755     OgreGFX::KeySet ks = OgreGFX::KeySet(mods, keyCode);
00756     return ks.toString();
00757 }
00758 
00759 std::string OgreGFX::UIMgr::modifierKeyMouseHash(const OIS::MouseButtonID id)
00760 {
00761 
00762     std::set<OIS::KeyCode> *mods = makeModifiersSet();
00763     OgreGFX::MouseButtonKeySet mbks = OgreGFX::MouseButtonKeySet(mods, id);
00764     return mbks.toString();
00765 }
00766 
00767 bool OgreGFX::UIMgr::callKeyHandlers(const OIS::KeyEvent &arg)
00768 {
00769 
00770     std::string keySetHash = modifierKeyHash(arg.key);
00771     if (this->keyHandlers.count(keySetHash) > 0)
00772     {
00773         for (std::list<KeyHandler *>::iterator khi = this->keyHandlers[keySetHash].begin(); khi != this->keyHandlers[keySetHash].end(); ++khi)
00774         {
00775             (*khi)->handleKeyEvent();
00776         }
00777         //this->keyHandlers[keySetHash]->handleKeyEvent();
00778         return true;
00779     }
00780     return false;
00781 }
00782 
00783 bool OgreGFX::UIMgr::callMouseHandlers(const OIS::MouseEvent &arg, const OIS::MouseButtonID id)
00784 {
00785 
00786     std::string mouseButtonKeySetHash = modifierKeyMouseHash(id);
00787     if (this->mouseHandlers.count(mouseButtonKeySetHash) > 0)
00788     {
00789         for (std::list<MouseHandler *>::iterator mhi = this->mouseHandlers[mouseButtonKeySetHash].begin(); mhi != this->mouseHandlers[mouseButtonKeySetHash].end(); ++mhi)
00790         {
00791             (*mhi)->handleMouseEvent(arg);
00792         }
00793         //this->mouseHandlers[mouseButtonKeySetHash]->handleMouseEvent(arg);
00794         return true;
00795     }
00796     return false;
00797 }
00798 //--------------------------------------------------------------------------------------------------
00799 
00800 //void OgreGFX::UIMgr::createNewEntity(FastEcslent::Identity id){
00801 //
00802 //}
00803 
00804 //--------------------------------------------------------------------------------------------------
00805 
00806 //Adjust mouse clipping area
00807 
00808 void OgreGFX::UIMgr::windowResized(Ogre::RenderWindow* rw)
00809 {
00810     unsigned int width, height, depth;
00811     int left, top;
00812     rw->getMetrics(width, height, depth, left, top);
00813 
00814     const OIS::MouseState &ms = mouse->getMouseState();
00815     ms.width = width;
00816     ms.height = height;
00817 }
00818 
00819 OgreGFX::UIMgr::~UIMgr()
00820 {
00821 
00822     kill();
00823 }
00824 
00825 void OgreGFX::UIMgr::kill()
00826 {
00827     //sceneManager->destroyQuery(volQuery);
00828 
00829 
00830 
00831     if (selectionBox)
00832     {
00833         //delete selectionBox;
00834     }
00835 
00836     delete DebugDrawer::getSingletonPtr();
00837 
00838     if (cameraMgr)
00839     {
00840         delete cameraMgr;
00841         cameraMgr = 0;
00842     }
00843     if (inputManager)
00844     {
00845         inputManager->destroyInputObject(mouse);
00846         inputManager->destroyInputObject(keyboard);
00847 
00848         OIS::InputManager::destroyInputSystem(inputManager);
00849         inputManager = 0;
00850     }
00851     if (renderWindow)
00852     {
00853         Ogre::WindowEventUtilities::removeWindowEventListener(renderWindow, this);
00854         //if(renderWindow) windowClosed(renderWindow);
00855     }
00856 
00857 
00858 }
00859 
00860 void OgreGFX::UIMgr::windowClosed(Ogre::RenderWindow* rw)
00861 {
00862     //Only close for window that created OIS (the main window in these demos)
00863     if (rw == renderWindow)
00864     {
00865         kill();
00866     }
00867 }
00868 
00869 //void OgreGFX::UIMgr::buttonHit(OgreBites::Button* button)
00870 //{
00871 //
00872 //}

Generated on Fri Dec 13 14:54:17 2013 for FastECSLent by  doxygen 1.5.4