1 | #include "mouseapi.h" |
---|
2 | |
---|
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 | state = InputManager::getInstance().createInputState("MouseAPI",true,true,99); |
---|
20 | state->setMouseExclusive(false);//does this work |
---|
21 | state->setMouseHandler(this); |
---|
22 | gameInputActivated = false; |
---|
23 | state->setKeyHandler(KeyBinderManager::getInstance().getDefaultAsHandler()); |
---|
24 | InputManager::getInstance().enterState("guiMouseOnly"); |
---|
25 | InputManager::getInstance().enterState("MouseAPI"); |
---|
26 | InputManager::getInstance().setMouseExclusive("game",false); |
---|
27 | } |
---|
28 | } |
---|
29 | |
---|
30 | } |
---|
31 | |
---|
32 | void MouseAPI::deactivate() |
---|
33 | { |
---|
34 | if(active) |
---|
35 | { |
---|
36 | active = false; |
---|
37 | if(InputManager::exists()) |
---|
38 | { |
---|
39 | InputManager::getInstance().leaveState("MouseAPI"); |
---|
40 | state->setMouseHandler(nullptr); |
---|
41 | InputManager::getInstance().destroyState("MouseAPI"); |
---|
42 | InputManager::getInstance().enterState("game"); |
---|
43 | } |
---|
44 | clickEvents.clear(); |
---|
45 | scrollEvents.clear(); |
---|
46 | gameInputActivated=false; |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | MouseAPI::~MouseAPI() |
---|
51 | { |
---|
52 | |
---|
53 | } |
---|
54 | |
---|
55 | void MouseAPI::buttonPressed(MouseButtonCode::ByEnum button) |
---|
56 | { |
---|
57 | cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); |
---|
58 | Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); |
---|
59 | int mouseposX = InputManager::getInstance().getMousePosition().first; |
---|
60 | int mouseposY = InputManager::getInstance().getMousePosition().second; |
---|
61 | Ogre::Ray ray = cam->getCameraToViewportRay(mouseposX/((float)vp->getActualWidth()),mouseposY/((float)vp->getActualHeight())); |
---|
62 | for(auto event: clickEvents) |
---|
63 | { |
---|
64 | for(auto wantedButton:event.buttons) |
---|
65 | { |
---|
66 | Ogre::Sphere sphere(event.position,event.radius); |
---|
67 | if(wantedButton == button && ray.intersects(sphere).first && cam->isVisible(sphere)) |
---|
68 | event.onClickedFunction(button); |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | void MouseAPI::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) |
---|
74 | { |
---|
75 | } |
---|
76 | |
---|
77 | void MouseAPI::tick(float dt) |
---|
78 | { |
---|
79 | if(active && !gameInputActivated) |
---|
80 | { |
---|
81 | InputManager::getInstance().leaveState("game"); |
---|
82 | } |
---|
83 | |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | void MouseAPI::mouseScrolled(int abs, int rel) |
---|
88 | { |
---|
89 | cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); |
---|
90 | Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); |
---|
91 | int mouseposX = InputManager::getInstance().getMousePosition().first; |
---|
92 | int mouseposY = InputManager::getInstance().getMousePosition().second; |
---|
93 | Ogre::Ray ray = cam->getCameraToViewportRay(mouseposX/((float)vp->getActualWidth()),mouseposY/((float)vp->getActualHeight())); |
---|
94 | for(auto event:scrollEvents) |
---|
95 | { |
---|
96 | if(!event.considerPosition || (ray.intersects(Ogre::Sphere(event.position,event.radius)).first && cam->isVisible(Ogre::Sphere(event.position,event.radius)))) |
---|
97 | event.onScrolledFunction(abs,rel,IntVector2(mouseposX,mouseposY)); |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | ClickableElementID MouseAPI::addClickableElement(const Vector3& position, float radius, const std::list<MouseButtonCode::ByEnum>& buttons, std::function<void(MouseButtonCode::ByEnum)> onClickedFunction) |
---|
102 | { |
---|
103 | ClickableElementID id = !clickEvents.empty() ? clickEvents.back().id + 1:0; |
---|
104 | clickEvents.insert(clickEvents.end(),{id,position,radius,buttons,onClickedFunction}); |
---|
105 | return id; |
---|
106 | } |
---|
107 | ScrollableElementID MouseAPI::addScrollElement(const Vector3& position, float radius, std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction) |
---|
108 | { |
---|
109 | ScrollableElementID id = !scrollEvents.empty() ? scrollEvents.back().id + 1:0; |
---|
110 | scrollEvents.insert(scrollEvents.end(),{id,position,radius,onScrolledFunction}); |
---|
111 | return id; |
---|
112 | } |
---|
113 | ScrollableElementID MouseAPI::addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction) |
---|
114 | { |
---|
115 | ScrollableElementID id = !scrollEvents.empty() ? scrollEvents.back().id + 1:0; |
---|
116 | scrollEvents.insert(scrollEvents.end(),{id,onScrolledFunction}); |
---|
117 | return id; |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | bool MouseAPI::changePositionOfClickableElement(ClickableElementID id,const Vector3& position) |
---|
122 | { |
---|
123 | for(auto event:clickEvents) |
---|
124 | { |
---|
125 | if(event.id == id) |
---|
126 | { |
---|
127 | event.position = position; |
---|
128 | return true; |
---|
129 | } |
---|
130 | } |
---|
131 | return false; |
---|
132 | } |
---|
133 | bool MouseAPI::changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position) |
---|
134 | { |
---|
135 | for(auto event:scrollEvents) |
---|
136 | { |
---|
137 | if(event.id == id) |
---|
138 | { |
---|
139 | event.position = position; |
---|
140 | return true; |
---|
141 | } |
---|
142 | } |
---|
143 | return false; |
---|
144 | } |
---|
145 | bool MouseAPI::changeRadiusOfClickableElement(ClickableElementID id,float radius) |
---|
146 | { |
---|
147 | for(auto event = clickEvents.begin();event != clickEvents.end();event++ ) |
---|
148 | { |
---|
149 | if(event->id == id) |
---|
150 | { |
---|
151 | event->radius = radius; |
---|
152 | return true; |
---|
153 | } |
---|
154 | } |
---|
155 | return false; |
---|
156 | } |
---|
157 | bool MouseAPI::changeRadiusOfScrollableElement(ScrollableElementID id,float radius) |
---|
158 | { |
---|
159 | for(auto event = scrollEvents.begin();event != scrollEvents.end();event++ ) |
---|
160 | { |
---|
161 | if(event->id == id) |
---|
162 | { |
---|
163 | event->radius = radius; |
---|
164 | return true; |
---|
165 | } |
---|
166 | } |
---|
167 | return false; |
---|
168 | } |
---|
169 | bool MouseAPI::deleteClickableElement(ClickableElementID id) |
---|
170 | { |
---|
171 | for(auto eventIt = clickEvents.begin();eventIt != clickEvents.end();eventIt++ ) |
---|
172 | { |
---|
173 | if(eventIt->id == id) |
---|
174 | { |
---|
175 | clickEvents.erase(eventIt); |
---|
176 | return true; |
---|
177 | } |
---|
178 | } |
---|
179 | return false; |
---|
180 | } |
---|
181 | bool MouseAPI::deleteScrollableElement(ScrollableElementID id) |
---|
182 | { |
---|
183 | for(auto eventIt = scrollEvents.begin();eventIt != scrollEvents.end();eventIt++ ) |
---|
184 | { |
---|
185 | if(eventIt->id == id) |
---|
186 | { |
---|
187 | scrollEvents.erase(eventIt); |
---|
188 | return true; |
---|
189 | } |
---|
190 | } |
---|
191 | return false; |
---|
192 | } |
---|
193 | |
---|
194 | float MouseAPI::getRadiusClick(ClickableElementID id) |
---|
195 | { |
---|
196 | for(auto eventIt = clickEvents.begin();eventIt != clickEvents.end();eventIt++ ) |
---|
197 | { |
---|
198 | if(eventIt->id == id) |
---|
199 | { |
---|
200 | return eventIt->radius; |
---|
201 | } |
---|
202 | } |
---|
203 | return 0; |
---|
204 | } |
---|
205 | |
---|
206 | float MouseAPI::getRadiusScroll(ScrollableElementID id) |
---|
207 | { |
---|
208 | for(auto eventIt = scrollEvents.begin();eventIt != scrollEvents.end();eventIt++ ) |
---|
209 | { |
---|
210 | if(eventIt->id == id) |
---|
211 | { |
---|
212 | return eventIt->radius; |
---|
213 | } |
---|
214 | } |
---|
215 | return 0; |
---|
216 | } |
---|
217 | |
---|
218 | Vector2 MouseAPI::getMousePosition() |
---|
219 | { |
---|
220 | Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); |
---|
221 | return Vector2(InputManager::getInstance().getMousePosition().first/((float)vp->getActualWidth()),InputManager::getInstance().getMousePosition().second/((float)vp->getActualHeight())); |
---|
222 | } |
---|
223 | |
---|
224 | void MouseAPI::activateGameInput() |
---|
225 | { |
---|
226 | gameInputActivated = true; |
---|
227 | state->setKeyHandler(nullptr); |
---|
228 | } |
---|
229 | |
---|
230 | void MouseAPI::deactivateGameInput() |
---|
231 | { |
---|
232 | gameInputActivated = false; |
---|
233 | state->setKeyHandler(KeyBinderManager::getInstance().getDefaultAsHandler()); |
---|
234 | } |
---|
235 | |
---|
236 | } |
---|