00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00034 #include "SO3WidgetManager.h"
00035 #include "../SO3SceneGraph/SO3Scene.h"
00036 #include "../SO3Renderer/SO3Window.h"
00037 #include <Ogre.h>
00038
00039 namespace Ogre
00040 {
00041
00042 template<> SO3::SWidgetManager* Ogre::Singleton<SO3::SWidgetManager>::ms_Singleton = 0;
00043 }
00044
00045 namespace SO3
00046 {
00047
00048 SWidgetManager* SWidgetManager::getSingletonPtr()
00049 {
00050 return ms_Singleton;
00051 }
00052
00053 SWidgetManager& SWidgetManager::getSingleton()
00054 {
00055 assert(ms_Singleton);
00056 return (*ms_Singleton);
00057 }
00058
00059 SWidgetManager::SWidgetManager()
00060 {
00061 focusedWidget = 0;
00062 }
00063
00064 SWidgetManager::~SWidgetManager()
00065 {
00066 focusedWidget = 0;
00067
00068
00069 WidgetFactoryList widgetFactoryListCopy = widgetFactoryList;
00070 WidgetFactoryList::iterator iWidgetFactoryList = widgetFactoryListCopy.begin();
00071 while (iWidgetFactoryList != widgetFactoryListCopy.end())
00072 {
00073 RemoveWidgetFactory(iWidgetFactoryList->second);
00074 iWidgetFactoryList++;
00075 }
00076 widgetFactoryList.clear();
00077 }
00078
00079 void SWidgetManager::AddWidgetFactory(SWidgetFactory* newWidgetFactory)
00080 {
00081 WidgetFactoryList::iterator iWidgetFactorySearched = widgetFactoryList.find(newWidgetFactory->GetType());
00082 if(iWidgetFactorySearched == widgetFactoryList.end())
00083 widgetFactoryList.insert(WidgetFactoryList::value_type(newWidgetFactory->GetType(), newWidgetFactory));
00084 else
00085 OGRE_EXCEPT(Ogre::Exception::ERR_DUPLICATE_ITEM, "Cannot add factory for widget \""+ newWidgetFactory->GetType() +"\", factory already exists in widget manager!", "SWidgetManager::AddWidgetFactory");
00086 }
00087
00088 void SWidgetManager::RemoveWidgetFactory(SWidgetFactory* existingWidgetFactory)
00089 {
00090 RemoveWidgetFactory(existingWidgetFactory->GetType());
00091 }
00092
00093 void SWidgetManager::RemoveWidgetFactory(std::string existingWidgetFactoryName)
00094 {
00095 WidgetFactoryList::iterator iWidgetFactorySearched = widgetFactoryList.find(existingWidgetFactoryName);
00096 if(iWidgetFactorySearched != widgetFactoryList.end())
00097 widgetFactoryList.erase(iWidgetFactorySearched);
00098 else
00099 OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, "Cannot remove factory for widget \""+ existingWidgetFactoryName +"\", factory does not exists in widget manager!", "SWidgetManager::RemoveWidgetFactory");
00100 }
00101
00102 SWidget* SWidgetManager::CreateWidget(SScene* targetScene, std::string widgetName, int xPos, int yPos, unsigned short width, unsigned short height, SViewPort* targetViewport, unsigned int widgetZOrder, std::string widgetType)
00103 {
00104 SWidget* newWidget = 0;
00105 WidgetFactoryList::iterator iWidgetFactorySearched = widgetFactoryList.find(widgetType);
00106 if(iWidgetFactorySearched != widgetFactoryList.end())
00107 {
00108 WidgetList::iterator iWidgetSearched = widgetList.find(widgetName);
00109 if(iWidgetSearched == widgetList.end())
00110 {
00111 newWidget = iWidgetFactorySearched->second->CreateWidget(targetScene, widgetName, xPos, yPos, width, height, targetViewport, widgetZOrder);
00112 widgetList.insert(WidgetList::value_type(widgetName, newWidget));
00113 overlayedWidgetList.insert(WidgetList::value_type(widgetName, newWidget));
00114
00115
00116 UpdateWidgetsZOrder();
00117 }
00118 else
00119 OGRE_EXCEPT(Ogre::Exception::ERR_DUPLICATE_ITEM, "Cannot create a widget named \""+ widgetName +"\", a widget with the same name already exists!", "SWidgetManager::CreateWidget");
00120 }
00121 else
00122 OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, "Cannot create a widget with factory \""+ widgetType +"\", factory does not exists in widget manager!", "SWidgetManager::CreateWidget");
00123
00124 return newWidget;
00125 }
00126
00127 SWidget* SWidgetManager::CreateWidget(SScene* targetScene, std::string widgetName, int xPos, int yPos, unsigned short width, unsigned short height, SViewPort* targetViewport, std::string widgetType)
00128 {
00129 SWidget* newWidget = 0;
00130 WidgetFactoryList::iterator iWidgetFactorySearched = widgetFactoryList.find(widgetType);
00131 if(iWidgetFactorySearched != widgetFactoryList.end())
00132 {
00133 WidgetList::iterator iWidgetSearched = widgetList.find(widgetName);
00134 if(iWidgetSearched == widgetList.end())
00135 {
00136 newWidget = iWidgetFactorySearched->second->CreateWidget(targetScene, widgetName, xPos, yPos, width, height, targetViewport);
00137 widgetList.insert(WidgetList::value_type(widgetName, newWidget));
00138 }
00139 else
00140 OGRE_EXCEPT(Ogre::Exception::ERR_DUPLICATE_ITEM, "Cannot create a widget named \""+ widgetName +"\", a widget with the same name already exists!", "SWidgetManager::CreateWidget");
00141 }
00142 else
00143 OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, "Cannot create a widget with factory \""+ widgetType +"\", factory does not exists in widget manager!", "SWidgetManager::CreateWidget");
00144
00145 return newWidget;
00146 }
00147
00148 SWidget* SWidgetManager::CreateWidget(SScene* targetScene, std::string widgetName, unsigned short width, unsigned short height, SMaterial* targetMaterial, unsigned short targetTechnique, unsigned short targetPass, unsigned short targetTextureUnit, std::string widgetType)
00149 {
00150 SWidget* newWidget = 0;
00151 WidgetFactoryList::iterator iWidgetFactorySearched = widgetFactoryList.find(widgetType);
00152 if(iWidgetFactorySearched != widgetFactoryList.end())
00153 {
00154 WidgetList::iterator iWidgetSearched = widgetList.find(widgetName);
00155 if(iWidgetSearched == widgetList.end())
00156 {
00157 newWidget = iWidgetFactorySearched->second->CreateWidget(targetScene, widgetName, width, height, targetMaterial, targetTechnique, targetPass, targetTextureUnit);
00158 widgetList.insert(WidgetList::value_type(widgetName, newWidget));
00159 }
00160 else
00161 OGRE_EXCEPT(Ogre::Exception::ERR_DUPLICATE_ITEM, "Cannot create a widget named \""+ widgetName +"\", a widget with the same name already exists!", "SWidgetManager::CreateWidget");
00162 }
00163 else
00164 OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, "Cannot create a widget with factory \""+ widgetType +"\", factory does not exists in widget manager!", "SWidgetManager::CreateWidget");
00165
00166 return newWidget;
00167 }
00168
00169 void SWidgetManager::DeleteWidget(SWidget* existingWidget)
00170 {
00171 WidgetFactoryList::iterator iWidgetFactorySearched = widgetFactoryList.find(existingWidget->GetType());
00172 if(iWidgetFactorySearched != widgetFactoryList.end())
00173 {
00174 std::string widgetName = existingWidget->GetName();
00175 WidgetList::iterator iWidgetSearched = widgetList.find(widgetName);
00176 if(iWidgetSearched != widgetList.end())
00177 {
00178
00179 WidgetList::iterator iOverlayedWidgetSearched = overlayedWidgetList.find(widgetName);
00180 if(iOverlayedWidgetSearched != overlayedWidgetList.end())
00181 overlayedWidgetList.erase(iOverlayedWidgetSearched);
00182
00183
00184 if(focusedWidget == existingWidget)
00185 UpdateFocusedWidget(0);
00186
00187
00188 iWidgetFactorySearched->second->DeleteWidget(existingWidget);
00189 widgetList.erase(widgetName);
00190
00191 }
00192 else
00193 OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, "Cannot delete a widget named \""+ widgetName +"\", widget not found!", "SWidgetManager::DeleteWidget");
00194 }
00195 else
00196 OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, "Cannot delete a widget with factory \""+ existingWidget->GetType() +"\", factory does not exists in widget manager!", "SWidgetManager::DeleteWidget");
00197 }
00198
00199 void SWidgetManager::InjectMouseMove(SWindow* originWindow, int xPos, int yPos, MouseButtonId button)
00200 {
00201
00202 SWidget* widgetUnder = GetWidgetUnder(originWindow, xPos, yPos);
00203
00204 if(widgetUnder != 0)
00205 if(widgetUnder->GetMouseEnable())
00206 if(widgetUnder->isOverlayed)
00207 {
00208 widgetUnder->InjectMouseMove(xPos, yPos, button);
00209 }
00210 else
00211 {
00212 SRaycastResult raycast = originWindow->GetLastRayCast();
00213 unsigned int leftPos = static_cast <int> (raycast.uvResult.x * widgetUnder->GetWidth());
00214 unsigned int topPos = static_cast <int> (raycast.uvResult.y * widgetUnder->GetHeight());
00215 widgetUnder->InjectMouseMove(leftPos, topPos, button);
00216 }
00217
00218
00219 WidgetList::iterator iWidgetList = overlayedWidgetList.begin();
00220 while(iWidgetList != overlayedWidgetList.end())
00221 {
00222
00223 if((iWidgetList->second != widgetUnder) && (iWidgetList->second->GetMouseEnable()))
00224 iWidgetList->second->InjectMouseMove(xPos, yPos, button);
00225
00226 iWidgetList++;
00227 }
00228 }
00229
00230 void SWidgetManager::InjectMouseWheel(SWindow* originWindow, int scrollX, int scrollY, int relativeScroll)
00231 {
00232 if(focusedWidget != 0)
00233 if(focusedWidget->GetMouseEnable())
00234 {
00235 focusedWidget->InjectMouseWheel(scrollX, scrollY, relativeScroll);
00236 _FireMouseWheelWidgetEvent(focusedWidget, relativeScroll);
00237 }
00238 }
00239
00240 void SWidgetManager::InjectMouseDown(SWindow* originWindow, int xPos, int yPos, MouseButtonId button)
00241 {
00242
00243 UpdateFocusedWidget(GetWidgetUnder(originWindow, xPos, yPos));
00244
00245 if(focusedWidget != 0)
00246 if(focusedWidget->GetMouseEnable())
00247 {
00248 if(focusedWidget->isOverlayed)
00249 {
00250 focusedWidget->InjectMouseDown(xPos, yPos, button);
00251 }
00252 else
00253 {
00254 SRaycastResult raycast = originWindow->GetLastRayCast();
00255 unsigned int leftPos = static_cast <int> (raycast.uvResult.x * focusedWidget->GetWidth());
00256 unsigned int topPos = static_cast <int> (raycast.uvResult.y * focusedWidget->GetHeight());
00257 focusedWidget->InjectMouseDown(leftPos, topPos, button);
00258 }
00259
00260 _FireMouseDownWidgetEvent(focusedWidget, xPos, yPos, button);
00261 }
00262 }
00263
00264 void SWidgetManager::InjectMouseUp(SWindow* originWindow, int xPos, int yPos, MouseButtonId button)
00265 {
00266 if(focusedWidget != 0)
00267 if(focusedWidget->GetMouseEnable())
00268 {
00269 if(focusedWidget->isOverlayed)
00270 {
00271 focusedWidget->InjectMouseUp(xPos, yPos, button);
00272 }
00273 else
00274 {
00275 SRaycastResult raycast = originWindow->GetLastRayCast();
00276 unsigned int leftPos = static_cast <int> (raycast.uvResult.x * focusedWidget->GetWidth());
00277 unsigned int topPos = static_cast <int> (raycast.uvResult.y * focusedWidget->GetHeight());
00278 focusedWidget->InjectMouseUp(leftPos, topPos, button);
00279 }
00280
00281 _FireMouseUpWidgetEvent(focusedWidget, xPos, yPos, button);
00282 }
00283 }
00284
00285 void SWidgetManager::InjectKeyEvent(UINT msg, WindowHandle hwnd, WPARAM wParam, LPARAM lParam)
00286 {
00287 if(focusedWidget != 0)
00288 if(focusedWidget->GetKeyboardEnable())
00289 focusedWidget->InjectKeyEvent(msg, hwnd, wParam, lParam);
00290 }
00291
00292 void SWidgetManager::InjectTextEvent(std::string text)
00293 {
00294 if(focusedWidget != 0)
00295 focusedWidget->InjectTextEvent(text);
00296 }
00297
00298 void SWidgetManager::UpdateWidgetsZOrder()
00299 {
00300
00301 std::multimap<unsigned short, SWidget*> widgetListForeground;
00302 std::multimap<unsigned short, SWidget*> widgetListNotForeground;
00303 widgetListByZOrder.clear();
00304
00305
00306 WidgetList::iterator iWidgetList = overlayedWidgetList.begin();
00307 while(iWidgetList != overlayedWidgetList.end())
00308 {
00309 if(iWidgetList->second->GetForeground())
00310 widgetListForeground.insert(std::multimap<unsigned short, SWidget*>::value_type(iWidgetList->second->GetZOrder(), iWidgetList->second));
00311 else
00312 widgetListNotForeground.insert(std::multimap<unsigned short, SWidget*>::value_type(iWidgetList->second->GetZOrder(), iWidgetList->second));
00313
00314 iWidgetList++;
00315 }
00316
00317
00318 unsigned short zOrder = 650;
00319 std::multimap<unsigned short, SWidget*>::reverse_iterator iOrderedWidgetIterator = widgetListForeground.rbegin();
00320 while(iOrderedWidgetIterator != widgetListForeground.rend())
00321 {
00322 SWidget* currentWidget = iOrderedWidgetIterator->second;
00323 currentWidget->_SetOgreOverlayZOrder(zOrder);
00324 widgetListByZOrder.insert(WidgetListByZOrder::value_type(zOrder, currentWidget));
00325 iOrderedWidgetIterator++;
00326 zOrder--;
00327 }
00328
00329
00330 unsigned short topOnFocusZOrder = zOrder;
00331 if(zOrder > 0)
00332 zOrder--;
00333
00334
00335 iOrderedWidgetIterator = widgetListNotForeground.rbegin();
00336 while(iOrderedWidgetIterator != widgetListNotForeground.rend())
00337 {
00338 SWidget* currentWidget = iOrderedWidgetIterator->second;
00339 bool forceZOrder = false;
00340
00341
00342 if(currentWidget == focusedWidget)
00343 if(currentWidget->GetTopOnFocus())
00344 {
00345 currentWidget->_SetOgreOverlayZOrder(topOnFocusZOrder);
00346 widgetListByZOrder.insert(WidgetListByZOrder::value_type(topOnFocusZOrder, currentWidget));
00347 forceZOrder = true;
00348 }
00349
00350
00351 if(!forceZOrder)
00352 {
00353 currentWidget->_SetOgreOverlayZOrder(zOrder);
00354 widgetListByZOrder.insert(WidgetListByZOrder::value_type(zOrder, currentWidget));
00355
00356
00357 if(zOrder > 0)
00358 zOrder--;
00359 }
00360
00361 iOrderedWidgetIterator++;
00362 }
00363 }
00364
00365 void SWidgetManager::UpdateFocusedWidget(SWidget* focusTargetedWidget)
00366 {
00367 focusedWidget = 0;
00368 WidgetList::iterator iWidgetList = widgetList.begin();
00369 while (iWidgetList != widgetList.end())
00370 {
00371 SWidget* currentWidget = iWidgetList->second;
00372 if(currentWidget == focusTargetedWidget)
00373 {
00374 currentWidget->SetFocusImpl(true);
00375 focusedWidget = focusTargetedWidget;
00376 }
00377 else
00378 currentWidget->SetFocusImpl(false);
00379
00380 iWidgetList++;
00381 }
00382
00383 UpdateWidgetsZOrder();
00384 }
00385
00386 SWidget* SWidgetManager::GetFocusedWidget()
00387 {
00388 return focusedWidget;
00389 }
00390
00391 SWidget* SWidgetManager::GetWidgetUnder(SWindow* originWindow, int posX, int posY)
00392 {
00393 SWidget* returnWidget = 0;
00394
00395
00396 WidgetListByZOrder::reverse_iterator iWidgetList = widgetListByZOrder.rbegin();
00397 while(iWidgetList != widgetListByZOrder.rend())
00398 {
00399
00400 SWidget* currentWidget = iWidgetList->second;
00401 SPoint<int> position = currentWidget->GetPosition();
00402 unsigned short widgetWidth = currentWidget->GetWidth();
00403 unsigned short widgetHeight = currentWidget->GetHeight();
00404
00405 if((posX >= position.x)
00406 && (posY >= position.y)
00407 && (posX <= (position.x + widgetWidth))
00408 && (posY <= (position.y + widgetHeight))
00409 && (currentWidget->GetVisible())
00410 && (currentWidget->GetMouseEnable()))
00411 {
00412 if (currentWidget->isTransparent && currentWidget->transparentIgnorePixels)
00413 {
00414 if (currentWidget->GetPixelUnder(posX - position.x, posY - position.y) != -1)
00415 {
00416
00417 if(!currentWidget->mouseIsOver)
00418 _FireEnterWidgetEvent(currentWidget);
00419 else
00420 _FireInsideWidgetEvent(currentWidget);
00421
00422
00423 if(returnWidget == 0)
00424 returnWidget = currentWidget;
00425 }
00426 else
00427 {
00428
00429 if(currentWidget->mouseIsOver)
00430 _FireExitWidgetEvent(currentWidget);
00431 }
00432 }
00433 else
00434 {
00435
00436 if(!currentWidget->mouseIsOver)
00437 _FireEnterWidgetEvent(currentWidget);
00438 else
00439 _FireInsideWidgetEvent(currentWidget);
00440
00441
00442 if(returnWidget == 0)
00443 returnWidget = currentWidget;
00444 }
00445 }
00446 else
00447 {
00448
00449 if(currentWidget->mouseIsOver)
00450 _FireExitWidgetEvent(currentWidget);
00451 }
00452
00453 iWidgetList++;
00454 }
00455
00456
00457 WidgetList::iterator iWidgetGlobalList = widgetList.begin();
00458 while(iWidgetGlobalList != widgetList.end())
00459 {
00460 SWidget* currentWidget = iWidgetGlobalList->second;
00461 if(!currentWidget->isOverlayed)
00462 {
00463
00464 SMaterial* materialUnderMouse = originWindow->GetLastRayCast().material;
00465 if(materialUnderMouse != 0)
00466 {
00467 if(materialUnderMouse->GetAssociatedWidget() == currentWidget)
00468 {
00469
00470 if(!currentWidget->mouseIsOver)
00471 _FireEnterWidgetEvent(currentWidget);
00472 else
00473 _FireInsideWidgetEvent(currentWidget);
00474
00475
00476 if(returnWidget == 0)
00477 returnWidget = currentWidget;
00478 }
00479 }
00480 else
00481 {
00482
00483 if(currentWidget->mouseIsOver)
00484 _FireExitWidgetEvent(currentWidget);
00485 }
00486 }
00487 iWidgetGlobalList++;
00488 }
00489
00490 return returnWidget;
00491 }
00492
00493 void SWidgetManager::AddWidgetListener(SWidgetListener* newWidgetListener)
00494 {
00495 if(widgetListenerList.count(newWidgetListener))
00496 OGRE_EXCEPT(Ogre::Exception::ERR_DUPLICATE_ITEM, "Cannot add this listener, it already exists in the listener list!", "SWidgetManager::AddWidgetListener");
00497
00498 widgetListenerList.insert(newWidgetListener);
00499 }
00500
00501 void SWidgetManager::RemoveWidgetListener(SWidgetListener* existingWidgetListener)
00502 {
00503 if(!widgetListenerList.count(existingWidgetListener))
00504 OGRE_EXCEPT(Ogre::Exception::ERR_DUPLICATE_ITEM, "Cannot remove listener, this listener does not exists in the listener list!", "SWidgetManager::RemoveWidgetListener");
00505
00506 widgetListenerList.erase(existingWidgetListener);
00507 }
00508
00509 void SWidgetManager::_FireEnterWidgetEvent(SWidget* targetedWidget)
00510 {
00511 targetedWidget->mouseIsOver = true;
00512 WidgetListenerList::iterator iWidgetListenerList = widgetListenerList.begin();
00513 while(iWidgetListenerList != widgetListenerList.end())
00514 {
00515 (*iWidgetListenerList)->OnEnter(targetedWidget);
00516 iWidgetListenerList++;
00517 }
00518 }
00519
00520 void SWidgetManager::_FireInsideWidgetEvent(SWidget* targetedWidget)
00521 {
00522 WidgetListenerList::iterator iWidgetListenerList = widgetListenerList.begin();
00523 while(iWidgetListenerList != widgetListenerList.end())
00524 {
00525 (*iWidgetListenerList)->OnInside(targetedWidget);
00526 iWidgetListenerList++;
00527 }
00528 }
00529
00530 void SWidgetManager::_FireExitWidgetEvent(SWidget* targetedWidget)
00531 {
00532 targetedWidget->mouseIsOver = false;
00533 WidgetListenerList::iterator iWidgetListenerList = widgetListenerList.begin();
00534 while(iWidgetListenerList != widgetListenerList.end())
00535 {
00536 (*iWidgetListenerList)->OnExit(targetedWidget);
00537 iWidgetListenerList++;
00538 }
00539 }
00540
00541 void SWidgetManager::_FireMouseDownWidgetEvent(SWidget* targetedWidget, int mousePosX, int mousePosY, MouseButtonId mouseButton)
00542 {
00543 WidgetListenerList::iterator iWidgetListenerList = widgetListenerList.begin();
00544 while(iWidgetListenerList != widgetListenerList.end())
00545 {
00546 (*iWidgetListenerList)->OnMouseDown(targetedWidget, mousePosX, mousePosY, mouseButton);
00547 iWidgetListenerList++;
00548 }
00549 }
00550
00551 void SWidgetManager::_FireMouseUpWidgetEvent(SWidget* targetedWidget, int mousePosX, int mousePosY, MouseButtonId mouseButton)
00552 {
00553 WidgetListenerList::iterator iWidgetListenerList = widgetListenerList.begin();
00554 while(iWidgetListenerList != widgetListenerList.end())
00555 {
00556 (*iWidgetListenerList)->OnMouseUp(targetedWidget, mousePosX, mousePosY, mouseButton);
00557 iWidgetListenerList++;
00558 }
00559 }
00560
00561 void SWidgetManager::_FireMouseWheelWidgetEvent(SWidget* targetedWidget, int relativeScroll)
00562 {
00563 WidgetListenerList::iterator iWidgetListenerList = widgetListenerList.begin();
00564 while(iWidgetListenerList != widgetListenerList.end())
00565 {
00566 (*iWidgetListenerList)->OnMouseWheel(targetedWidget, relativeScroll);
00567 iWidgetListenerList++;
00568 }
00569 }
00570
00571 void SWidgetManager::_FireScriptWidgetEvent(SWidget* targetedWidget, SWidget::WidgetScriptFunction functionCalled)
00572 {
00573 WidgetListenerList::iterator iWidgetListenerList = widgetListenerList.begin();
00574 while(iWidgetListenerList != widgetListenerList.end())
00575 {
00576 (*iWidgetListenerList)->OnScriptCall(targetedWidget, functionCalled);
00577 iWidgetListenerList++;
00578 }
00579 }
00580
00581 }