Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Improve MouseAPI
Add Comments
Cleanup

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