Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/MouseAPI_FS19/src/modules/MouseAPI/mouseapi.cc @ 12363

Last change on this file since 12363 was 12363, checked in by tkuonen, 5 years ago

Added MouseCursor

File size: 8.0 KB
RevLine 
[12213]1#include "mouseapi.h"
[12362]2
[12363]3#if OGRE_VERSION >= 0x010900
4#   include <Overlay/OgreOverlayManager.h>
5#   include <Overlay/OgrePanelOverlayElement.h>
6#else
7#   include <OgreOverlayManager.h>
8#   include <OgrePanelOverlayElement.h>
9#endif
10
11#include "util/StringUtils.h"
12
[12247]13namespace orxonox{
14
[12271]15ManageScopedSingleton(MouseAPI, ScopeID::GRAPHICS, false);
16
[12253]17MouseAPI::MouseAPI()
[12213]18{
19
20}
21
[12253]22void MouseAPI::activate()
23{
[12279]24    if(!active)
25    {
26        active = true;
27        if(InputManager::exists())
28        {
29            //cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera();
30            state = InputManager::getInstance().createInputState("MouseAPI",true,true,99);
31            state->setMouseExclusive(false);//does this work
32            state->setMouseHandler(this);
[12309]33            state->setKeyHandler(KeyBinderManager::getInstance().getDefaultAsHandler());
34            InputManager::getInstance().enterState("guiMouseOnly");
[12279]35            InputManager::getInstance().enterState("MouseAPI");
[12309]36            InputManager::getInstance().setMouseExclusive("game",false);
37            //InputManager::getInstance().setMouseExclusive("guiMouseOnly",false);
38            //InputManager::getInstance().getState("game")->
[12279]39        }
[12309]40        //GUIManager::getInstance().showGUI("MouseAPICursor", true);//Display a mouse cursor by displaying a empty menu
[12363]41        /*cursor = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton()
42                                                               .createOverlayElement("Panel", "MouseAPI_cursor_" + getUniqueNumberString()));
43        cursor->setMaterialName("Orxonox/RadarMarker");//todo: better material
44        cursor->setPosition(0,0);
45        cursor->setDimensions(0.1,0.1);
46        Ogre::Overlay* overlay = Ogre::OverlayManager::getSingleton().create( "MouseAPI_cursor_" + getUniqueNumberString() );
47        overlay->show();*/
48        //this->overlay_->add2D(this->cursor);
[12253]49    }
50
[12217]51}
[12213]52
[12253]53void MouseAPI::deactivate()
54{
[12279]55    if(active)
[12253]56    {
[12309]57        GUIManager::getInstance().showGUI("MouseAPICursor", true);
[12279]58        active = false;
59        if(InputManager::exists())
60        {
61            InputManager::getInstance().leaveState("MouseAPI");
62            state->setMouseHandler(nullptr);
63            InputManager::getInstance().destroyState("MouseAPI");
[12309]64            InputManager::getInstance().enterState("game");
[12279]65        }
66        clickEvents.clear();
67        scrollEvents.clear();
[12253]68    }
69}
70
71MouseAPI::~MouseAPI()
72{
73
74}
75
[12247]76void MouseAPI::buttonPressed(MouseButtonCode::ByEnum button)
[12217]77{
[12271]78    cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera();//todo: trycatch
[12247]79    Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport();
[12287]80    int mouseposX = InputManager::getInstance().getMousePosition().first;
81    int mouseposY = InputManager::getInstance().getMousePosition().second;
82    Ogre::Ray ray = cam->getCameraToViewportRay(mouseposX/((float)vp->getActualWidth()),mouseposY/((float)vp->getActualHeight()));
[12217]83    for(auto event: clickEvents)
84    {
[12275]85        for(auto wantedButton:event.buttons)
86        {
[12309]87            Ogre::Sphere sphere(event.position,event.radius);
88            if(wantedButton == button && ray.intersects(sphere).first && cam->isVisible(sphere))
[12253]89                event.onClickedFunction(button);
[12217]90        }
91    }
92}
[12213]93
[12247]94void MouseAPI::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
[12213]95{
[12287]96    //mousePos = abs;
[12213]97}
[12217]98
[12334]99void MouseAPI::tick(float dt)
100{
101    if(active)
102    {
103        InputManager::getInstance().leaveState("game");//hack: todo: crate 2nd input state with prioritz 98 for cegui(cursor)
[12363]104        //GUIManager::getInstance().showGUI("MouseAPICursor", false);//hack todo: only if gui not shown & evt better if not in mouse mooved
[12334]105    }
106
107}
108
109
[12247]110void MouseAPI::mouseScrolled(int abs, int rel)
[12213]111{
[12271]112    cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera();
[12247]113    Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport();
[12287]114    int mouseposX = InputManager::getInstance().getMousePosition().first;
115    int mouseposY = InputManager::getInstance().getMousePosition().second;
116    Ogre::Ray ray = cam->getCameraToViewportRay(mouseposX/((float)vp->getActualWidth()),mouseposY/((float)vp->getActualHeight()));
[12275]117    for(auto event:scrollEvents)
118    {
[12309]119        if(!event.considerPosition || (ray.intersects(Ogre::Sphere(event.position,event.radius)).first && cam->isVisible(Ogre::Sphere(event.position,event.radius))))
[12287]120            event.onScrolledFunction(abs,rel,IntVector2(mouseposX,mouseposY));
[12217]121    }
122}
123
[12352]124ClickableElementID MouseAPI::addClickableElement(const Vector3& position, float radius, const std::list<MouseButtonCode::ByEnum>& buttons, std::function<void(MouseButtonCode::ByEnum)> onClickedFunction)
[12217]125{
[12352]126    ClickableElementID id = !clickEvents.empty() ? clickEvents.back().id + 1:0;
[12311]127    clickEvents.insert(clickEvents.end(),{id,position,radius,buttons,onClickedFunction});
128    return id;
[12217]129}
[12271]130ScrollableElementID MouseAPI::addScrollElement(const Vector3& position, float radius, std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction)
[12217]131{
[12311]132    ScrollableElementID id = !scrollEvents.empty() ? scrollEvents.back().id + 1:0;
133    scrollEvents.insert(scrollEvents.end(),{id,position,radius,onScrolledFunction});
134    return id;
[12213]135}
[12271]136ScrollableElementID MouseAPI::addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction)
[12213]137{
[12311]138    ScrollableElementID id = !scrollEvents.empty() ? scrollEvents.back().id + 1:0;
139    scrollEvents.insert(scrollEvents.end(),{id,onScrolledFunction});
140    return id;
[12213]141}
[12217]142
[12247]143
[12352]144bool MouseAPI::changePositionOfClickableElement(ClickableElementID id,const Vector3& position)
[12275]145{
146    for(auto event:clickEvents)
147    {
148        if(event.id == id)
149        {
150            event.position = position;
151            return true;
152        }
153    }
154    return false;
[12247]155}
[12275]156bool MouseAPI::changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position)
157{
158    for(auto event:scrollEvents)
159    {
160        if(event.id == id)
161        {
162            event.position = position;
163            return true;
164        }
165    }
166    return false;
167}
[12352]168bool MouseAPI::changeRadiusOfClickableElement(ClickableElementID id,float radius)
[12275]169{
[12302]170    for(auto event = clickEvents.begin();event != clickEvents.end();event++ )
[12275]171    {
[12302]172        if(event->id == id)
[12275]173        {
[12302]174            event->radius = radius;
[12275]175            return true;
176        }
177    }
178    return false;
179}
180bool MouseAPI::changeRadiusOfScrollableElement(ScrollableElementID id,float radius)
181{
[12302]182    for(auto event = scrollEvents.begin();event != scrollEvents.end();event++ )
[12275]183    {
[12302]184        if(event->id == id)
[12275]185        {
[12302]186            event->radius = radius;
[12275]187            return true;
188        }
189    }
190    return false;
191}
[12352]192bool MouseAPI::deleteClickableElement(ClickableElementID id)
[12275]193{
194    for(auto eventIt = clickEvents.begin();eventIt != clickEvents.end();eventIt++ )
195    {
196        if(eventIt->id == id)
197        {
198            clickEvents.erase(eventIt);
199            return true;
200        }
201    }
202    return false;
203}
204bool MouseAPI::deleteScrollableElement(ScrollableElementID id)
205{
206    for(auto eventIt = scrollEvents.begin();eventIt != scrollEvents.end();eventIt++ )
207    {
208        if(eventIt->id == id)
209        {
210            scrollEvents.erase(eventIt);
211            return true;
212        }
213    }
214    return false;
215}
216
[12352]217float MouseAPI::getRadiusClick(ClickableElementID id)
[12302]218{
219     for(auto eventIt = clickEvents.begin();eventIt != clickEvents.end();eventIt++ )
220     {
221         if(eventIt->id == id)
222         {
223             return eventIt->radius;
224         }
225     }
[12309]226     return 0;
[12275]227}
[12302]228
229float MouseAPI::getRadiusScroll(ScrollableElementID id)
230{
231     for(auto eventIt = scrollEvents.begin();eventIt != scrollEvents.end();eventIt++ )
232     {
233         if(eventIt->id == id)
234         {
235             return eventIt->radius;
236         }
237     }
[12309]238     return 0;
[12302]239}
240
[12309]241//returns relative Position of the Mouse
242Vector2 MouseAPI::getMousePosition()
243{
244    Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport();
245    return Vector2(InputManager::getInstance().getMousePosition().first/((float)vp->getActualWidth()),InputManager::getInstance().getMousePosition().second/((float)vp->getActualHeight()));
[12302]246}
[12309]247
248}
Note: See TracBrowser for help on using the repository browser.