Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Add first version of mousecursor

File size: 7.1 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            state->setKeyHandler(KeyBinderManager::getInstance().getDefaultAsHandler());
24            InputManager::getInstance().enterState("guiMouseOnly");
25            InputManager::getInstance().enterState("MouseAPI");
26            InputManager::getInstance().setMouseExclusive("game",false);
27            //InputManager::getInstance().setMouseExclusive("guiMouseOnly",false);
28            //InputManager::getInstance().getState("game")->
29        }
30        //GUIManager::getInstance().showGUI("MouseAPICursor", true);//Display a mouse cursor by displaying a empty menu
31    }
32
33}
34
35void MouseAPI::deactivate()
36{
37    if(active)
38    {
39        GUIManager::getInstance().showGUI("MouseAPICursor", true);
40        active = false;
41        if(InputManager::exists())
42        {
43            InputManager::getInstance().leaveState("MouseAPI");
44            state->setMouseHandler(nullptr);
45            InputManager::getInstance().destroyState("MouseAPI");
46            InputManager::getInstance().enterState("game");
47        }
48        clickEvents.clear();
49        scrollEvents.clear();
50    }
51}
52
53MouseAPI::~MouseAPI()
54{
55
56}
57
58void MouseAPI::buttonPressed(MouseButtonCode::ByEnum button)
59{
60    cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera();//todo: trycatch
61    Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport();
62    int mouseposX = InputManager::getInstance().getMousePosition().first;
63    int mouseposY = InputManager::getInstance().getMousePosition().second;
64    Ogre::Ray ray = cam->getCameraToViewportRay(mouseposX/((float)vp->getActualWidth()),mouseposY/((float)vp->getActualHeight()));
65    for(auto event: clickEvents)
66    {
67        for(auto wantedButton:event.buttons)
68        {
69            Ogre::Sphere sphere(event.position,event.radius);
70            if(wantedButton == button && ray.intersects(sphere).first && cam->isVisible(sphere))
71                event.onClickedFunction(button);
72        }
73    }
74}
75
76void MouseAPI::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
77{
78    //mousePos = abs;
79    InputManager::getInstance().leaveState("game");//hack: todo: crate 2nd input state with prioritz 98 for cegui(cursor)
80    GUIManager::getInstance().showGUI("MouseAPICursor", true);//hack todo: only if gui not shown & evt better if not in mouse mooved
81}
82
83void MouseAPI::mouseScrolled(int abs, int rel)
84{
85    cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera();
86    Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport();
87    int mouseposX = InputManager::getInstance().getMousePosition().first;
88    int mouseposY = InputManager::getInstance().getMousePosition().second;
89    Ogre::Ray ray = cam->getCameraToViewportRay(mouseposX/((float)vp->getActualWidth()),mouseposY/((float)vp->getActualHeight()));
90    for(auto event:scrollEvents)
91    {
92        if(!event.considerPosition || (ray.intersects(Ogre::Sphere(event.position,event.radius)).first && cam->isVisible(Ogre::Sphere(event.position,event.radius))))
93            event.onScrolledFunction(abs,rel,IntVector2(mouseposX,mouseposY));
94    }
95}
96
97ClickableObjectID MouseAPI::addClickableObject(const Vector3& position, float radius, const std::list<MouseButtonCode::ByEnum>& buttons, std::function<void(MouseButtonCode::ByEnum)> onClickedFunction)
98{
99    clickEvents.insert(clickEvents.begin(),{!clickEvents.empty() ? clickEvents.back().id + 1:0,position,radius,buttons,onClickedFunction});
100    return clickEvents.back().id;
101}
102ScrollableElementID MouseAPI::addScrollElement(const Vector3& position, float radius, std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction)
103{
104    scrollEvents.insert(scrollEvents.begin(),{!scrollEvents.empty() ? scrollEvents.back().id + 1:0,position,radius,onScrolledFunction});
105    return scrollEvents.back().id;
106}
107ScrollableElementID MouseAPI::addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction)
108{
109    scrollEvents.insert(scrollEvents.begin(),{!scrollEvents.empty() ? scrollEvents.back().id + 1:0,onScrolledFunction});
110    return scrollEvents.back().id;
111}
112
113
114bool MouseAPI::changePositionOfClickableObject(ClickableObjectID id,const Vector3& position)
115{
116    for(auto event:clickEvents)
117    {
118        if(event.id == id)
119        {
120            event.position = position;
121            return true;
122        }
123    }
124    return false;
125}
126bool MouseAPI::changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position)
127{
128    for(auto event:scrollEvents)
129    {
130        if(event.id == id)
131        {
132            event.position = position;
133            return true;
134        }
135    }
136    return false;
137}
138bool MouseAPI::changeRadiusOfClickableObject(ClickableObjectID id,float radius)
139{
140    for(auto event = clickEvents.begin();event != clickEvents.end();event++ )
141    {
142        if(event->id == id)
143        {
144            event->radius = radius;
145            return true;
146        }
147    }
148    return false;
149}
150bool MouseAPI::changeRadiusOfScrollableElement(ScrollableElementID id,float radius)
151{
152    for(auto event = scrollEvents.begin();event != scrollEvents.end();event++ )
153    {
154        if(event->id == id)
155        {
156            event->radius = radius;
157            return true;
158        }
159    }
160    return false;
161}
162bool MouseAPI::deleteClickableObject(ClickableObjectID id)
163{
164    for(auto eventIt = clickEvents.begin();eventIt != clickEvents.end();eventIt++ )
165    {
166        if(eventIt->id == id)
167        {
168            clickEvents.erase(eventIt);
169            return true;
170        }
171    }
172    return false;
173}
174bool MouseAPI::deleteScrollableElement(ScrollableElementID id)
175{
176    for(auto eventIt = scrollEvents.begin();eventIt != scrollEvents.end();eventIt++ )
177    {
178        if(eventIt->id == id)
179        {
180            scrollEvents.erase(eventIt);
181            return true;
182        }
183    }
184    return false;
185}
186
187float MouseAPI::getRadiusClick(ClickableObjectID id)
188{
189     for(auto eventIt = clickEvents.begin();eventIt != clickEvents.end();eventIt++ )
190     {
191         if(eventIt->id == id)
192         {
193             return eventIt->radius;
194         }
195     }
196     return 0;
197}
198
199float MouseAPI::getRadiusScroll(ScrollableElementID id)
200{
201     for(auto eventIt = scrollEvents.begin();eventIt != scrollEvents.end();eventIt++ )
202     {
203         if(eventIt->id == id)
204         {
205             return eventIt->radius;
206         }
207     }
208     return 0;
209}
210
211//returns relative Position of the Mouse
212Vector2 MouseAPI::getMousePosition()
213{
214    Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport();
215    return Vector2(InputManager::getInstance().getMousePosition().first/((float)vp->getActualWidth()),InputManager::getInstance().getMousePosition().second/((float)vp->getActualHeight()));
216}
217
218}
Note: See TracBrowser for help on using the repository browser.