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

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