1 | #include "mouseapi.h" |
---|
2 | #include "core/singleton/ScopedSingletonIncludes.h" |
---|
3 | namespace orxonox{ |
---|
4 | |
---|
5 | ManageScopedSingleton(MouseAPI, ScopeID::GRAPHICS, false); |
---|
6 | |
---|
7 | MouseAPI::MouseAPI() |
---|
8 | { |
---|
9 | |
---|
10 | } |
---|
11 | |
---|
12 | void 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 | |
---|
26 | void 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 | |
---|
39 | MouseAPI::~MouseAPI() |
---|
40 | { |
---|
41 | |
---|
42 | } |
---|
43 | |
---|
44 | void 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 | |
---|
58 | void MouseAPI::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) |
---|
59 | { |
---|
60 | mousePos = abs; |
---|
61 | } |
---|
62 | |
---|
63 | void 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 | |
---|
74 | ClickableObjectID 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 | } |
---|
79 | ScrollableElementID 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 | } |
---|
84 | ScrollableElementID 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 |
---|
91 | void MouseAPI::changePositionOfClickableObject(ClickableObjectID id,const Vector3& position){} |
---|
92 | void MouseAPI::changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position){} |
---|
93 | void MouseAPI::changeRadiusOfClickableObject(ClickableObjectID id,float radius){} |
---|
94 | void MouseAPI::changeRadiusOfScrollableElement(ScrollableElementID id,float radius){} |
---|
95 | void MouseAPI::deleteClickableObject(ClickableObjectID){} |
---|
96 | void MouseAPI::deleteScrollableElement(ScrollableElementID){} |
---|
97 | |
---|
98 | } |
---|