Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Begin extending MouseAPI

File size: 3.6 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            if(wantedButton == button && ray.intersects(Ogre::Sphere(event.position,event.radius)).first)
53                event.onClickedFunction(button);
54        }
55    }
56}
57
58void MouseAPI::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
59{
60    mousePos = abs;
61}
62
63void MouseAPI::mouseScrolled(int abs, int rel)
64{
65    cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera();
66    Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport();
67    Ogre::Ray ray = cam->getCameraToViewportRay(mousePos.x/((float)vp->getActualWidth()),mousePos.y/((float)vp->getActualHeight()));
68    for(auto event:scrollEvents){
69        if(!event.considerPosition || ray.intersects(Ogre::Sphere(event.position,event.radius)).first)
70            event.onScrolledFunction(abs,rel,mousePos);
71    }
72}
73
74ClickableObjectID MouseAPI::addClickableObject(const Vector3& position, float radius, const std::list<MouseButtonCode::ByEnum>& buttons, std::function<void(MouseButtonCode::ByEnum)> onClickedFunction)
75{
76    clickEvents.insert(clickEvents.begin(),{!clickEvents.empty() ? clickEvents.back().id + 1:0,position,radius,buttons,onClickedFunction});
77    return clickEvents.back().id;
78}
79ScrollableElementID MouseAPI::addScrollElement(const Vector3& position, float radius, std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction)
80{
81    scrollEvents.insert(scrollEvents.begin(),{!scrollEvents.empty() ? scrollEvents.back().id + 1:0,position,radius,onScrolledFunction});
82    return scrollEvents.back().id;
83}
84ScrollableElementID MouseAPI::addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction)
85{
86    scrollEvents.insert(scrollEvents.begin(),{!scrollEvents.empty() ? scrollEvents.back().id + 1:0,onScrolledFunction});
87    return scrollEvents.back().id;
88}
89
90//todo
91void MouseAPI::changePositionOfClickableObject(ClickableObjectID id,const Vector3& position){}
92void MouseAPI::changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position){}
93void MouseAPI::changeRadiusOfClickableObject(ClickableObjectID id,float radius){}
94void MouseAPI::changeRadiusOfScrollableElement(ScrollableElementID id,float radius){}
95void MouseAPI::deleteClickableObject(ClickableObjectID){}
96void MouseAPI::deleteScrollableElement(ScrollableElementID){}
97
98}
Note: See TracBrowser for help on using the repository browser.