Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Begin extending MouseAPI

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