Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12362 was 12362, checked in by mkarpf, 5 years ago

cleanup (minor)

File size: 7.2 KB
Line 
1#include "mouseapi.h"
2
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}
80
81void MouseAPI::tick(float dt)
82{
83    if(active)
84    {
85        InputManager::getInstance().leaveState("game");//hack: todo: crate 2nd input state with prioritz 98 for cegui(cursor)
86        GUIManager::getInstance().showGUI("MouseAPICursor", false);//hack todo: only if gui not shown & evt better if not in mouse mooved
87    }
88
89}
90
91
92void MouseAPI::mouseScrolled(int abs, int rel)
93{
94    cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera();
95    Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport();
96    int mouseposX = InputManager::getInstance().getMousePosition().first;
97    int mouseposY = InputManager::getInstance().getMousePosition().second;
98    Ogre::Ray ray = cam->getCameraToViewportRay(mouseposX/((float)vp->getActualWidth()),mouseposY/((float)vp->getActualHeight()));
99    for(auto event:scrollEvents)
100    {
101        if(!event.considerPosition || (ray.intersects(Ogre::Sphere(event.position,event.radius)).first && cam->isVisible(Ogre::Sphere(event.position,event.radius))))
102            event.onScrolledFunction(abs,rel,IntVector2(mouseposX,mouseposY));
103    }
104}
105
106ClickableElementID MouseAPI::addClickableElement(const Vector3& position, float radius, const std::list<MouseButtonCode::ByEnum>& buttons, std::function<void(MouseButtonCode::ByEnum)> onClickedFunction)
107{
108    ClickableElementID id = !clickEvents.empty() ? clickEvents.back().id + 1:0;
109    clickEvents.insert(clickEvents.end(),{id,position,radius,buttons,onClickedFunction});
110    return id;
111}
112ScrollableElementID MouseAPI::addScrollElement(const Vector3& position, float radius, std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction)
113{
114    ScrollableElementID id = !scrollEvents.empty() ? scrollEvents.back().id + 1:0;
115    scrollEvents.insert(scrollEvents.end(),{id,position,radius,onScrolledFunction});
116    return id;
117}
118ScrollableElementID MouseAPI::addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction)
119{
120    ScrollableElementID id = !scrollEvents.empty() ? scrollEvents.back().id + 1:0;
121    scrollEvents.insert(scrollEvents.end(),{id,onScrolledFunction});
122    return id;
123}
124
125
126bool MouseAPI::changePositionOfClickableElement(ClickableElementID id,const Vector3& position)
127{
128    for(auto event:clickEvents)
129    {
130        if(event.id == id)
131        {
132            event.position = position;
133            return true;
134        }
135    }
136    return false;
137}
138bool MouseAPI::changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position)
139{
140    for(auto event:scrollEvents)
141    {
142        if(event.id == id)
143        {
144            event.position = position;
145            return true;
146        }
147    }
148    return false;
149}
150bool MouseAPI::changeRadiusOfClickableElement(ClickableElementID id,float radius)
151{
152    for(auto event = clickEvents.begin();event != clickEvents.end();event++ )
153    {
154        if(event->id == id)
155        {
156            event->radius = radius;
157            return true;
158        }
159    }
160    return false;
161}
162bool MouseAPI::changeRadiusOfScrollableElement(ScrollableElementID id,float radius)
163{
164    for(auto event = scrollEvents.begin();event != scrollEvents.end();event++ )
165    {
166        if(event->id == id)
167        {
168            event->radius = radius;
169            return true;
170        }
171    }
172    return false;
173}
174bool MouseAPI::deleteClickableElement(ClickableElementID id)
175{
176    for(auto eventIt = clickEvents.begin();eventIt != clickEvents.end();eventIt++ )
177    {
178        if(eventIt->id == id)
179        {
180            clickEvents.erase(eventIt);
181            return true;
182        }
183    }
184    return false;
185}
186bool MouseAPI::deleteScrollableElement(ScrollableElementID id)
187{
188    for(auto eventIt = scrollEvents.begin();eventIt != scrollEvents.end();eventIt++ )
189    {
190        if(eventIt->id == id)
191        {
192            scrollEvents.erase(eventIt);
193            return true;
194        }
195    }
196    return false;
197}
198
199float MouseAPI::getRadiusClick(ClickableElementID id)
200{
201     for(auto eventIt = clickEvents.begin();eventIt != clickEvents.end();eventIt++ )
202     {
203         if(eventIt->id == id)
204         {
205             return eventIt->radius;
206         }
207     }
208     return 0;
209}
210
211float MouseAPI::getRadiusScroll(ScrollableElementID id)
212{
213     for(auto eventIt = scrollEvents.begin();eventIt != scrollEvents.end();eventIt++ )
214     {
215         if(eventIt->id == id)
216         {
217             return eventIt->radius;
218         }
219     }
220     return 0;
221}
222
223//returns relative Position of the Mouse
224Vector2 MouseAPI::getMousePosition()
225{
226    Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport();
227    return Vector2(InputManager::getInstance().getMousePosition().first/((float)vp->getActualWidth()),InputManager::getInstance().getMousePosition().second/((float)vp->getActualHeight()));
228}
229
230}
Note: See TracBrowser for help on using the repository browser.