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 | if(!active) |
---|
15 | { |
---|
16 | active = true; |
---|
17 | if(InputManager::exists()) |
---|
18 | { |
---|
19 | //cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); |
---|
20 | state = InputManager::getInstance().createInputState("MouseAPI",true,true,99); |
---|
21 | state->setMouseExclusive(false);//does this work |
---|
22 | state->setMouseHandler(this); |
---|
23 | InputManager::getInstance().enterState("MouseAPI"); |
---|
24 | } |
---|
25 | } |
---|
26 | |
---|
27 | } |
---|
28 | |
---|
29 | void MouseAPI::deactivate() |
---|
30 | { |
---|
31 | if(active) |
---|
32 | { |
---|
33 | active = false; |
---|
34 | if(InputManager::exists()) |
---|
35 | { |
---|
36 | InputManager::getInstance().leaveState("MouseAPI"); |
---|
37 | state->setMouseHandler(nullptr); |
---|
38 | InputManager::getInstance().destroyState("MouseAPI"); |
---|
39 | } |
---|
40 | clickEvents.clear(); |
---|
41 | scrollEvents.clear(); |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | MouseAPI::~MouseAPI() |
---|
46 | { |
---|
47 | |
---|
48 | } |
---|
49 | |
---|
50 | void MouseAPI::buttonPressed(MouseButtonCode::ByEnum button) |
---|
51 | { |
---|
52 | cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera();//todo: trycatch |
---|
53 | Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); |
---|
54 | Ogre::Ray ray = cam->getCameraToViewportRay(mousePos.x/((float)vp->getActualWidth()),mousePos.y/((float)vp->getActualHeight())); |
---|
55 | for(auto event: clickEvents) |
---|
56 | { |
---|
57 | for(auto wantedButton:event.buttons) |
---|
58 | { |
---|
59 | if(wantedButton == button && ray.intersects(Ogre::Sphere(event.position,event.radius)).first) |
---|
60 | event.onClickedFunction(button); |
---|
61 | } |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | void MouseAPI::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) |
---|
66 | { |
---|
67 | mousePos = abs; |
---|
68 | } |
---|
69 | |
---|
70 | void MouseAPI::mouseScrolled(int abs, int rel) |
---|
71 | { |
---|
72 | cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); |
---|
73 | Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); |
---|
74 | Ogre::Ray ray = cam->getCameraToViewportRay(mousePos.x/((float)vp->getActualWidth()),mousePos.y/((float)vp->getActualHeight())); |
---|
75 | for(auto event:scrollEvents) |
---|
76 | { |
---|
77 | if(!event.considerPosition || ray.intersects(Ogre::Sphere(event.position,event.radius)).first) |
---|
78 | event.onScrolledFunction(abs,rel,mousePos); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | ClickableObjectID MouseAPI::addClickableObject(const Vector3& position, float radius, const std::list<MouseButtonCode::ByEnum>& buttons, std::function<void(MouseButtonCode::ByEnum)> onClickedFunction) |
---|
83 | { |
---|
84 | clickEvents.insert(clickEvents.begin(),{!clickEvents.empty() ? clickEvents.back().id + 1:0,position,radius,buttons,onClickedFunction}); |
---|
85 | return clickEvents.back().id; |
---|
86 | } |
---|
87 | ScrollableElementID MouseAPI::addScrollElement(const Vector3& position, float radius, std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction) |
---|
88 | { |
---|
89 | scrollEvents.insert(scrollEvents.begin(),{!scrollEvents.empty() ? scrollEvents.back().id + 1:0,position,radius,onScrolledFunction}); |
---|
90 | return scrollEvents.back().id; |
---|
91 | } |
---|
92 | ScrollableElementID MouseAPI::addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction) |
---|
93 | { |
---|
94 | scrollEvents.insert(scrollEvents.begin(),{!scrollEvents.empty() ? scrollEvents.back().id + 1:0,onScrolledFunction}); |
---|
95 | return scrollEvents.back().id; |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | bool MouseAPI::changePositionOfClickableObject(ClickableObjectID id,const Vector3& position) |
---|
100 | { |
---|
101 | for(auto event:clickEvents) |
---|
102 | { |
---|
103 | if(event.id == id) |
---|
104 | { |
---|
105 | event.position = position; |
---|
106 | return true; |
---|
107 | } |
---|
108 | } |
---|
109 | return false; |
---|
110 | } |
---|
111 | bool MouseAPI::changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position) |
---|
112 | { |
---|
113 | for(auto event:scrollEvents) |
---|
114 | { |
---|
115 | if(event.id == id) |
---|
116 | { |
---|
117 | event.position = position; |
---|
118 | return true; |
---|
119 | } |
---|
120 | } |
---|
121 | return false; |
---|
122 | } |
---|
123 | bool MouseAPI::changeRadiusOfClickableObject(ClickableObjectID id,float radius) |
---|
124 | { |
---|
125 | for(auto event:clickEvents) |
---|
126 | { |
---|
127 | if(event.id == id) |
---|
128 | { |
---|
129 | event.radius = radius; |
---|
130 | return true; |
---|
131 | } |
---|
132 | } |
---|
133 | return false; |
---|
134 | } |
---|
135 | bool MouseAPI::changeRadiusOfScrollableElement(ScrollableElementID id,float radius) |
---|
136 | { |
---|
137 | for(auto event:scrollEvents) |
---|
138 | { |
---|
139 | if(event.id == id) |
---|
140 | { |
---|
141 | event.radius = radius; |
---|
142 | return true; |
---|
143 | } |
---|
144 | } |
---|
145 | return false; |
---|
146 | } |
---|
147 | bool MouseAPI::deleteClickableObject(ClickableObjectID id) |
---|
148 | { |
---|
149 | for(auto eventIt = clickEvents.begin();eventIt != clickEvents.end();eventIt++ ) |
---|
150 | { |
---|
151 | if(eventIt->id == id) |
---|
152 | { |
---|
153 | clickEvents.erase(eventIt); |
---|
154 | return true; |
---|
155 | } |
---|
156 | } |
---|
157 | return false; |
---|
158 | } |
---|
159 | bool MouseAPI::deleteScrollableElement(ScrollableElementID id) |
---|
160 | { |
---|
161 | for(auto eventIt = scrollEvents.begin();eventIt != scrollEvents.end();eventIt++ ) |
---|
162 | { |
---|
163 | if(eventIt->id == id) |
---|
164 | { |
---|
165 | scrollEvents.erase(eventIt); |
---|
166 | return true; |
---|
167 | } |
---|
168 | } |
---|
169 | return false; |
---|
170 | } |
---|
171 | |
---|
172 | } |
---|