Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Test Level created
ATTENTION: Level is buggy and freezes Screen!

File size: 2.5 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");
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    Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport();
41    Ogre::Ray ray = cam->getCameraToViewportRay(mousePos.x/((float)vp->getActualWidth()),mousePos.y/((float)vp->getActualHeight()));
42    for(auto event: clickEvents)
43    {
44        for(auto wantedButton:event.buttons){
45            if(wantedButton == button && ray.intersects(Ogre::Sphere(event.position,event.radius)).first)
46                event.onClickedFunction(button);
47        }
48    }
49}
50
51void MouseAPI::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
52{
53    mousePos = abs;
54}
55
56void MouseAPI::mouseScrolled(int abs, int rel)
57{
58    Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport();
59    Ogre::Ray ray = cam->getCameraToViewportRay(mousePos.x/((float)vp->getActualWidth()),mousePos.y/((float)vp->getActualHeight()));
60    for(auto event:scrollEvents){
61        if(!event.considerPosition || ray.intersects(Ogre::Sphere(event.position,event.radius)).first)
62            (*(event.onScrolledFunction))(abs,rel,mousePos);
63    }
64}
65
66void MouseAPI::addClickableObject(const Vector3& position, float radius, const std::list<MouseButtonCode::ByEnum>& buttons, std::function<void(MouseButtonCode::ByEnum)> onClickedFunction)
67{
68    clickEvents.insert(clickEvents.begin(),{position,radius,buttons,onClickedFunction});
69}
70void MouseAPI::addScrollElement(const Vector3& position,float radius,void (*onScrolledFunction)(int abs,int rel,const IntVector2&  mousePos))
71{
72    scrollEvents.insert(scrollEvents.begin(),{position,radius,onScrolledFunction});
73}
74void MouseAPI::addScrollElement(void (*onScrolledFunction)(int abs,int rel,const IntVector2&  mousePos))
75{
76    scrollEvents.insert(scrollEvents.begin(),{onScrolledFunction});
77}
78
79void MouseAPI::changeCamera(Camera& camera)
80{
81    cam = camera.getOgreCamera();
82}
83
84}
Note: See TracBrowser for help on using the repository browser.