Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Extend MouseAPI

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