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 | int mouseposX = InputManager::getInstance().getMousePosition().first; |
---|
55 | int mouseposY = InputManager::getInstance().getMousePosition().second; |
---|
56 | Ogre::Ray ray = cam->getCameraToViewportRay(mouseposX/((float)vp->getActualWidth()),mouseposY/((float)vp->getActualHeight())); |
---|
57 | for(auto event: clickEvents) |
---|
58 | { |
---|
59 | for(auto wantedButton:event.buttons) |
---|
60 | { |
---|
61 | if(wantedButton == button && ray.intersects(Ogre::Sphere(event.position,event.radius)).first) |
---|
62 | event.onClickedFunction(button); |
---|
63 | } |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | void MouseAPI::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) |
---|
68 | { |
---|
69 | //mousePos = abs; |
---|
70 | } |
---|
71 | |
---|
72 | void MouseAPI::mouseScrolled(int abs, int rel) |
---|
73 | { |
---|
74 | cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); |
---|
75 | Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); |
---|
76 | int mouseposX = InputManager::getInstance().getMousePosition().first; |
---|
77 | int mouseposY = InputManager::getInstance().getMousePosition().second; |
---|
78 | Ogre::Ray ray = cam->getCameraToViewportRay(mouseposX/((float)vp->getActualWidth()),mouseposY/((float)vp->getActualHeight())); |
---|
79 | for(auto event:scrollEvents) |
---|
80 | { |
---|
81 | if(!event.considerPosition || ray.intersects(Ogre::Sphere(event.position,event.radius)).first) |
---|
82 | event.onScrolledFunction(abs,rel,IntVector2(mouseposX,mouseposY)); |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | ClickableObjectID MouseAPI::addClickableObject(const Vector3& position, float radius, const std::list<MouseButtonCode::ByEnum>& buttons, std::function<void(MouseButtonCode::ByEnum)> onClickedFunction) |
---|
87 | { |
---|
88 | clickEvents.insert(clickEvents.begin(),{!clickEvents.empty() ? clickEvents.back().id + 1:0,position,radius,buttons,onClickedFunction}); |
---|
89 | return clickEvents.back().id; |
---|
90 | } |
---|
91 | ScrollableElementID MouseAPI::addScrollElement(const Vector3& position, float radius, std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction) |
---|
92 | { |
---|
93 | scrollEvents.insert(scrollEvents.begin(),{!scrollEvents.empty() ? scrollEvents.back().id + 1:0,position,radius,onScrolledFunction}); |
---|
94 | return scrollEvents.back().id; |
---|
95 | } |
---|
96 | ScrollableElementID MouseAPI::addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction) |
---|
97 | { |
---|
98 | scrollEvents.insert(scrollEvents.begin(),{!scrollEvents.empty() ? scrollEvents.back().id + 1:0,onScrolledFunction}); |
---|
99 | return scrollEvents.back().id; |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | bool MouseAPI::changePositionOfClickableObject(ClickableObjectID id,const Vector3& position) |
---|
104 | { |
---|
105 | for(auto event:clickEvents) |
---|
106 | { |
---|
107 | if(event.id == id) |
---|
108 | { |
---|
109 | event.position = position; |
---|
110 | return true; |
---|
111 | } |
---|
112 | } |
---|
113 | return false; |
---|
114 | } |
---|
115 | bool MouseAPI::changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position) |
---|
116 | { |
---|
117 | for(auto event:scrollEvents) |
---|
118 | { |
---|
119 | if(event.id == id) |
---|
120 | { |
---|
121 | event.position = position; |
---|
122 | return true; |
---|
123 | } |
---|
124 | } |
---|
125 | return false; |
---|
126 | } |
---|
127 | bool MouseAPI::changeRadiusOfClickableObject(ClickableObjectID id,float radius) |
---|
128 | { |
---|
129 | for(auto event:clickEvents) |
---|
130 | { |
---|
131 | if(event.id == id) |
---|
132 | { |
---|
133 | event.radius = radius; |
---|
134 | return true; |
---|
135 | } |
---|
136 | } |
---|
137 | return false; |
---|
138 | } |
---|
139 | bool MouseAPI::changeRadiusOfScrollableElement(ScrollableElementID id,float radius) |
---|
140 | { |
---|
141 | for(auto event:scrollEvents) |
---|
142 | { |
---|
143 | if(event.id == id) |
---|
144 | { |
---|
145 | event.radius = radius; |
---|
146 | return true; |
---|
147 | } |
---|
148 | } |
---|
149 | return false; |
---|
150 | } |
---|
151 | bool MouseAPI::deleteClickableObject(ClickableObjectID id) |
---|
152 | { |
---|
153 | for(auto eventIt = clickEvents.begin();eventIt != clickEvents.end();eventIt++ ) |
---|
154 | { |
---|
155 | if(eventIt->id == id) |
---|
156 | { |
---|
157 | clickEvents.erase(eventIt); |
---|
158 | return true; |
---|
159 | } |
---|
160 | } |
---|
161 | return false; |
---|
162 | } |
---|
163 | bool MouseAPI::deleteScrollableElement(ScrollableElementID id) |
---|
164 | { |
---|
165 | for(auto eventIt = scrollEvents.begin();eventIt != scrollEvents.end();eventIt++ ) |
---|
166 | { |
---|
167 | if(eventIt->id == id) |
---|
168 | { |
---|
169 | scrollEvents.erase(eventIt); |
---|
170 | return true; |
---|
171 | } |
---|
172 | } |
---|
173 | return false; |
---|
174 | } |
---|
175 | |
---|
176 | } |
---|