Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12301 was 12287, checked in by tkuonen, 6 years ago

MouseAPI: improve getting mouse pos

File size: 5.2 KB
Line 
1#include "mouseapi.h"
2#include "core/singleton/ScopedSingletonIncludes.h"
3namespace orxonox{
4
5ManageScopedSingleton(MouseAPI, ScopeID::GRAPHICS, false);
6
7MouseAPI::MouseAPI()
8{
9
10}
11
12void MouseAPI::activate()
13{
14    if(!active)
15    {
16        active = true;
17        if(InputManager::exists())
18        {
19            //cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera();
20            state = InputManager::getInstance().createInputState("MouseAPI",true,true,99);
21            state->setMouseExclusive(false);//does this work
22            state->setMouseHandler(this);
23            InputManager::getInstance().enterState("MouseAPI");
24        }
25    }
26
27}
28
29void MouseAPI::deactivate()
30{
31    if(active)
32    {
33        active = false;
34        if(InputManager::exists())
35        {
36            InputManager::getInstance().leaveState("MouseAPI");
37            state->setMouseHandler(nullptr);
38            InputManager::getInstance().destroyState("MouseAPI");
39        }
40        clickEvents.clear();
41        scrollEvents.clear();
42    }
43}
44
45MouseAPI::~MouseAPI()
46{
47
48}
49
50void MouseAPI::buttonPressed(MouseButtonCode::ByEnum button)
51{
52    cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera();//todo: trycatch
53    Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport();
54    int mouseposX = InputManager::getInstance().getMousePosition().first;
55    int mouseposY = InputManager::getInstance().getMousePosition().second;
56    Ogre::Ray ray = cam->getCameraToViewportRay(mouseposX/((float)vp->getActualWidth()),mouseposY/((float)vp->getActualHeight()));
57    for(auto event: clickEvents)
58    {
59        for(auto wantedButton:event.buttons)
60        {
61            if(wantedButton == button && ray.intersects(Ogre::Sphere(event.position,event.radius)).first)
62                event.onClickedFunction(button);
63        }
64    }
65}
66
67void MouseAPI::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
68{
69    //mousePos = abs;
70}
71
72void MouseAPI::mouseScrolled(int abs, int rel)
73{
74    cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera();
75    Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport();
76    int mouseposX = InputManager::getInstance().getMousePosition().first;
77    int mouseposY = InputManager::getInstance().getMousePosition().second;
78    Ogre::Ray ray = cam->getCameraToViewportRay(mouseposX/((float)vp->getActualWidth()),mouseposY/((float)vp->getActualHeight()));
79    for(auto event:scrollEvents)
80    {
81        if(!event.considerPosition || ray.intersects(Ogre::Sphere(event.position,event.radius)).first)
82            event.onScrolledFunction(abs,rel,IntVector2(mouseposX,mouseposY));
83    }
84}
85
86ClickableObjectID MouseAPI::addClickableObject(const Vector3& position, float radius, const std::list<MouseButtonCode::ByEnum>& buttons, std::function<void(MouseButtonCode::ByEnum)> onClickedFunction)
87{
88    clickEvents.insert(clickEvents.begin(),{!clickEvents.empty() ? clickEvents.back().id + 1:0,position,radius,buttons,onClickedFunction});
89    return clickEvents.back().id;
90}
91ScrollableElementID MouseAPI::addScrollElement(const Vector3& position, float radius, std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction)
92{
93    scrollEvents.insert(scrollEvents.begin(),{!scrollEvents.empty() ? scrollEvents.back().id + 1:0,position,radius,onScrolledFunction});
94    return scrollEvents.back().id;
95}
96ScrollableElementID MouseAPI::addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction)
97{
98    scrollEvents.insert(scrollEvents.begin(),{!scrollEvents.empty() ? scrollEvents.back().id + 1:0,onScrolledFunction});
99    return scrollEvents.back().id;
100}
101
102
103bool MouseAPI::changePositionOfClickableObject(ClickableObjectID id,const Vector3& position)
104{
105    for(auto event:clickEvents)
106    {
107        if(event.id == id)
108        {
109            event.position = position;
110            return true;
111        }
112    }
113    return false;
114}
115bool MouseAPI::changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position)
116{
117    for(auto event:scrollEvents)
118    {
119        if(event.id == id)
120        {
121            event.position = position;
122            return true;
123        }
124    }
125    return false;
126}
127bool MouseAPI::changeRadiusOfClickableObject(ClickableObjectID id,float radius)
128{
129    for(auto event:clickEvents)
130    {
131        if(event.id == id)
132        {
133            event.radius = radius;
134            return true;
135        }
136    }
137    return false;
138}
139bool MouseAPI::changeRadiusOfScrollableElement(ScrollableElementID id,float radius)
140{
141    for(auto event:scrollEvents)
142    {
143        if(event.id == id)
144        {
145            event.radius = radius;
146            return true;
147        }
148    }
149    return false;
150}
151bool MouseAPI::deleteClickableObject(ClickableObjectID id)
152{
153    for(auto eventIt = clickEvents.begin();eventIt != clickEvents.end();eventIt++ )
154    {
155        if(eventIt->id == id)
156        {
157            clickEvents.erase(eventIt);
158            return true;
159        }
160    }
161    return false;
162}
163bool MouseAPI::deleteScrollableElement(ScrollableElementID id)
164{
165    for(auto eventIt = scrollEvents.begin();eventIt != scrollEvents.end();eventIt++ )
166    {
167        if(eventIt->id == id)
168        {
169            scrollEvents.erase(eventIt);
170            return true;
171        }
172    }
173    return false;
174}
175
176}
Note: See TracBrowser for help on using the repository browser.