Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Repair MouseAPI

File size: 2.6 KB
Line 
1#include "mouseapi.h"
2
3namespace orxonox{
4
5MouseAPI::MouseAPI()
6{
7
8}
9
10void MouseAPI::activate()
11{
12     if(InputManager::exists())
13     {
14        //cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera();
15        state = InputManager::getInstance().createInputState("MouseAPI",true,true,99);
16        state->setMouseExclusive(false);//does this work
17        state->setMouseHandler(this);
18        InputManager::getInstance().enterState("MouseAPI");
19    }
20
21}
22
23void MouseAPI::deactivate()
24{
25    if(InputManager::exists())
26    {
27        InputManager::getInstance().leaveState("MouseAPI");
28        state->setMouseHandler(nullptr);
29        InputManager::getInstance().destroyState("MouseAPI");
30    }
31}
32
33MouseAPI::~MouseAPI()
34{
35
36}
37
38void MouseAPI::buttonPressed(MouseButtonCode::ByEnum button)
39{
40    cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera();
41    Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport();
42    Ogre::Ray ray = cam->getCameraToViewportRay(mousePos.x/((float)vp->getActualWidth()),mousePos.y/((float)vp->getActualHeight()));
43    for(auto event: clickEvents)
44    {
45        for(auto wantedButton:event.buttons){
46            if(wantedButton == button && ray.intersects(Ogre::Sphere(event.position,event.radius)).first)
47                event.onClickedFunction(button);
48        }
49    }
50}
51
52void MouseAPI::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
53{
54    mousePos = abs;
55}
56
57void MouseAPI::mouseScrolled(int abs, int rel)
58{
59    Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport();
60    Ogre::Ray ray = cam->getCameraToViewportRay(mousePos.x/((float)vp->getActualWidth()),mousePos.y/((float)vp->getActualHeight()));
61    for(auto event:scrollEvents){
62        if(!event.considerPosition || ray.intersects(Ogre::Sphere(event.position,event.radius)).first)
63            (*(event.onScrolledFunction))(abs,rel,mousePos);
64    }
65}
66
67void MouseAPI::addClickableObject(const Vector3& position, float radius, const std::list<MouseButtonCode::ByEnum>& buttons, std::function<void(MouseButtonCode::ByEnum)> onClickedFunction)
68{
69    clickEvents.insert(clickEvents.begin(),{position,radius,buttons,onClickedFunction});
70}
71void MouseAPI::addScrollElement(const Vector3& position,float radius,void (*onScrolledFunction)(int abs,int rel,const IntVector2&  mousePos))
72{
73    scrollEvents.insert(scrollEvents.begin(),{position,radius,onScrolledFunction});
74}
75void MouseAPI::addScrollElement(void (*onScrolledFunction)(int abs,int rel,const IntVector2&  mousePos))
76{
77    scrollEvents.insert(scrollEvents.begin(),{onScrolledFunction});
78}
79
80void MouseAPI::changeCamera(Camera& camera)
81{
82    cam = camera.getOgreCamera();
83}
84
85}
Note: See TracBrowser for help on using the repository browser.