| [12213] | 1 | #include "mouseapi.h" |
|---|
| [12271] | 2 | #include "core/singleton/ScopedSingletonIncludes.h" |
|---|
| [12247] | 3 | namespace orxonox{ |
|---|
| 4 | |
|---|
| [12271] | 5 | ManageScopedSingleton(MouseAPI, ScopeID::GRAPHICS, false); |
|---|
| 6 | |
|---|
| [12253] | 7 | MouseAPI::MouseAPI() |
|---|
| [12213] | 8 | { |
|---|
| 9 | |
|---|
| 10 | } |
|---|
| 11 | |
|---|
| [12253] | 12 | void MouseAPI::activate() |
|---|
| 13 | { |
|---|
| [12279] | 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); |
|---|
| [12309] | 23 | state->setKeyHandler(KeyBinderManager::getInstance().getDefaultAsHandler()); |
|---|
| 24 | InputManager::getInstance().enterState("guiMouseOnly"); |
|---|
| [12279] | 25 | InputManager::getInstance().enterState("MouseAPI"); |
|---|
| [12309] | 26 | InputManager::getInstance().setMouseExclusive("game",false); |
|---|
| 27 | //InputManager::getInstance().setMouseExclusive("guiMouseOnly",false); |
|---|
| 28 | //InputManager::getInstance().getState("game")-> |
|---|
| [12279] | 29 | } |
|---|
| [12309] | 30 | //GUIManager::getInstance().showGUI("MouseAPICursor", true);//Display a mouse cursor by displaying a empty menu |
|---|
| [12253] | 31 | } |
|---|
| 32 | |
|---|
| [12217] | 33 | } |
|---|
| [12213] | 34 | |
|---|
| [12253] | 35 | void MouseAPI::deactivate() |
|---|
| 36 | { |
|---|
| [12279] | 37 | if(active) |
|---|
| [12253] | 38 | { |
|---|
| [12309] | 39 | GUIManager::getInstance().showGUI("MouseAPICursor", true); |
|---|
| [12279] | 40 | active = false; |
|---|
| 41 | if(InputManager::exists()) |
|---|
| 42 | { |
|---|
| 43 | InputManager::getInstance().leaveState("MouseAPI"); |
|---|
| 44 | state->setMouseHandler(nullptr); |
|---|
| 45 | InputManager::getInstance().destroyState("MouseAPI"); |
|---|
| [12309] | 46 | InputManager::getInstance().enterState("game"); |
|---|
| [12279] | 47 | } |
|---|
| 48 | clickEvents.clear(); |
|---|
| 49 | scrollEvents.clear(); |
|---|
| [12253] | 50 | } |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | MouseAPI::~MouseAPI() |
|---|
| 54 | { |
|---|
| 55 | |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| [12247] | 58 | void MouseAPI::buttonPressed(MouseButtonCode::ByEnum button) |
|---|
| [12217] | 59 | { |
|---|
| [12271] | 60 | cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera();//todo: trycatch |
|---|
| [12247] | 61 | Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); |
|---|
| [12287] | 62 | int mouseposX = InputManager::getInstance().getMousePosition().first; |
|---|
| 63 | int mouseposY = InputManager::getInstance().getMousePosition().second; |
|---|
| 64 | Ogre::Ray ray = cam->getCameraToViewportRay(mouseposX/((float)vp->getActualWidth()),mouseposY/((float)vp->getActualHeight())); |
|---|
| [12217] | 65 | for(auto event: clickEvents) |
|---|
| 66 | { |
|---|
| [12275] | 67 | for(auto wantedButton:event.buttons) |
|---|
| 68 | { |
|---|
| [12309] | 69 | Ogre::Sphere sphere(event.position,event.radius); |
|---|
| 70 | if(wantedButton == button && ray.intersects(sphere).first && cam->isVisible(sphere)) |
|---|
| [12253] | 71 | event.onClickedFunction(button); |
|---|
| [12217] | 72 | } |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| [12213] | 75 | |
|---|
| [12247] | 76 | void MouseAPI::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) |
|---|
| [12213] | 77 | { |
|---|
| [12287] | 78 | //mousePos = abs; |
|---|
| [12309] | 79 | InputManager::getInstance().leaveState("game");//hack: todo: crate 2nd input state with prioritz 98 for cegui(cursor) |
|---|
| 80 | GUIManager::getInstance().showGUI("MouseAPICursor", true);//hack todo: only if gui not shown & evt better if not in mouse mooved |
|---|
| [12213] | 81 | } |
|---|
| [12217] | 82 | |
|---|
| [12247] | 83 | void MouseAPI::mouseScrolled(int abs, int rel) |
|---|
| [12213] | 84 | { |
|---|
| [12271] | 85 | cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); |
|---|
| [12247] | 86 | Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); |
|---|
| [12287] | 87 | int mouseposX = InputManager::getInstance().getMousePosition().first; |
|---|
| 88 | int mouseposY = InputManager::getInstance().getMousePosition().second; |
|---|
| 89 | Ogre::Ray ray = cam->getCameraToViewportRay(mouseposX/((float)vp->getActualWidth()),mouseposY/((float)vp->getActualHeight())); |
|---|
| [12275] | 90 | for(auto event:scrollEvents) |
|---|
| 91 | { |
|---|
| [12309] | 92 | if(!event.considerPosition || (ray.intersects(Ogre::Sphere(event.position,event.radius)).first && cam->isVisible(Ogre::Sphere(event.position,event.radius)))) |
|---|
| [12287] | 93 | event.onScrolledFunction(abs,rel,IntVector2(mouseposX,mouseposY)); |
|---|
| [12217] | 94 | } |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| [12271] | 97 | ClickableObjectID MouseAPI::addClickableObject(const Vector3& position, float radius, const std::list<MouseButtonCode::ByEnum>& buttons, std::function<void(MouseButtonCode::ByEnum)> onClickedFunction) |
|---|
| [12217] | 98 | { |
|---|
| [12311] | 99 | ClickableObjectID id = !clickEvents.empty() ? clickEvents.back().id + 1:0; |
|---|
| 100 | clickEvents.insert(clickEvents.end(),{id,position,radius,buttons,onClickedFunction}); |
|---|
| 101 | return id; |
|---|
| [12217] | 102 | } |
|---|
| [12271] | 103 | ScrollableElementID MouseAPI::addScrollElement(const Vector3& position, float radius, std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction) |
|---|
| [12217] | 104 | { |
|---|
| [12311] | 105 | ScrollableElementID id = !scrollEvents.empty() ? scrollEvents.back().id + 1:0; |
|---|
| 106 | scrollEvents.insert(scrollEvents.end(),{id,position,radius,onScrolledFunction}); |
|---|
| 107 | return id; |
|---|
| [12213] | 108 | } |
|---|
| [12271] | 109 | ScrollableElementID MouseAPI::addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction) |
|---|
| [12213] | 110 | { |
|---|
| [12311] | 111 | ScrollableElementID id = !scrollEvents.empty() ? scrollEvents.back().id + 1:0; |
|---|
| 112 | scrollEvents.insert(scrollEvents.end(),{id,onScrolledFunction}); |
|---|
| 113 | return id; |
|---|
| [12213] | 114 | } |
|---|
| [12217] | 115 | |
|---|
| [12247] | 116 | |
|---|
| [12275] | 117 | bool MouseAPI::changePositionOfClickableObject(ClickableObjectID id,const Vector3& position) |
|---|
| 118 | { |
|---|
| 119 | for(auto event:clickEvents) |
|---|
| 120 | { |
|---|
| 121 | if(event.id == id) |
|---|
| 122 | { |
|---|
| 123 | event.position = position; |
|---|
| 124 | return true; |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | return false; |
|---|
| [12247] | 128 | } |
|---|
| [12275] | 129 | bool MouseAPI::changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position) |
|---|
| 130 | { |
|---|
| 131 | for(auto event:scrollEvents) |
|---|
| 132 | { |
|---|
| 133 | if(event.id == id) |
|---|
| 134 | { |
|---|
| 135 | event.position = position; |
|---|
| 136 | return true; |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | return false; |
|---|
| 140 | } |
|---|
| 141 | bool MouseAPI::changeRadiusOfClickableObject(ClickableObjectID id,float radius) |
|---|
| 142 | { |
|---|
| [12302] | 143 | for(auto event = clickEvents.begin();event != clickEvents.end();event++ ) |
|---|
| [12275] | 144 | { |
|---|
| [12302] | 145 | if(event->id == id) |
|---|
| [12275] | 146 | { |
|---|
| [12302] | 147 | event->radius = radius; |
|---|
| [12275] | 148 | return true; |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | return false; |
|---|
| 152 | } |
|---|
| 153 | bool MouseAPI::changeRadiusOfScrollableElement(ScrollableElementID id,float radius) |
|---|
| 154 | { |
|---|
| [12302] | 155 | for(auto event = scrollEvents.begin();event != scrollEvents.end();event++ ) |
|---|
| [12275] | 156 | { |
|---|
| [12302] | 157 | if(event->id == id) |
|---|
| [12275] | 158 | { |
|---|
| [12302] | 159 | event->radius = radius; |
|---|
| [12275] | 160 | return true; |
|---|
| 161 | } |
|---|
| 162 | } |
|---|
| 163 | return false; |
|---|
| 164 | } |
|---|
| 165 | bool MouseAPI::deleteClickableObject(ClickableObjectID id) |
|---|
| 166 | { |
|---|
| 167 | for(auto eventIt = clickEvents.begin();eventIt != clickEvents.end();eventIt++ ) |
|---|
| 168 | { |
|---|
| 169 | if(eventIt->id == id) |
|---|
| 170 | { |
|---|
| 171 | clickEvents.erase(eventIt); |
|---|
| 172 | return true; |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | return false; |
|---|
| 176 | } |
|---|
| 177 | bool MouseAPI::deleteScrollableElement(ScrollableElementID id) |
|---|
| 178 | { |
|---|
| 179 | for(auto eventIt = scrollEvents.begin();eventIt != scrollEvents.end();eventIt++ ) |
|---|
| 180 | { |
|---|
| 181 | if(eventIt->id == id) |
|---|
| 182 | { |
|---|
| 183 | scrollEvents.erase(eventIt); |
|---|
| 184 | return true; |
|---|
| 185 | } |
|---|
| 186 | } |
|---|
| 187 | return false; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| [12302] | 190 | float MouseAPI::getRadiusClick(ClickableObjectID id) |
|---|
| 191 | { |
|---|
| 192 | for(auto eventIt = clickEvents.begin();eventIt != clickEvents.end();eventIt++ ) |
|---|
| 193 | { |
|---|
| 194 | if(eventIt->id == id) |
|---|
| 195 | { |
|---|
| 196 | return eventIt->radius; |
|---|
| 197 | } |
|---|
| 198 | } |
|---|
| [12309] | 199 | return 0; |
|---|
| [12275] | 200 | } |
|---|
| [12302] | 201 | |
|---|
| 202 | float MouseAPI::getRadiusScroll(ScrollableElementID id) |
|---|
| 203 | { |
|---|
| 204 | for(auto eventIt = scrollEvents.begin();eventIt != scrollEvents.end();eventIt++ ) |
|---|
| 205 | { |
|---|
| 206 | if(eventIt->id == id) |
|---|
| 207 | { |
|---|
| 208 | return eventIt->radius; |
|---|
| 209 | } |
|---|
| 210 | } |
|---|
| [12309] | 211 | return 0; |
|---|
| [12302] | 212 | } |
|---|
| 213 | |
|---|
| [12309] | 214 | //returns relative Position of the Mouse |
|---|
| 215 | Vector2 MouseAPI::getMousePosition() |
|---|
| 216 | { |
|---|
| 217 | Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); |
|---|
| 218 | return Vector2(InputManager::getInstance().getMousePosition().first/((float)vp->getActualWidth()),InputManager::getInstance().getMousePosition().second/((float)vp->getActualHeight())); |
|---|
| [12302] | 219 | } |
|---|
| [12309] | 220 | |
|---|
| 221 | } |
|---|