[12213] | 1 | #include "mouseapi.h" |
---|
[12362] | 2 | |
---|
[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 | state = InputManager::getInstance().createInputState("MouseAPI",true,true,99); |
---|
| 20 | state->setMouseExclusive(false);//does this work |
---|
| 21 | state->setMouseHandler(this); |
---|
[12377] | 22 | gameInputActivated = false; |
---|
[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); |
---|
[12279] | 27 | } |
---|
[12253] | 28 | } |
---|
| 29 | |
---|
[12217] | 30 | } |
---|
[12213] | 31 | |
---|
[12253] | 32 | void MouseAPI::deactivate() |
---|
| 33 | { |
---|
[12279] | 34 | if(active) |
---|
[12253] | 35 | { |
---|
[12279] | 36 | active = false; |
---|
| 37 | if(InputManager::exists()) |
---|
| 38 | { |
---|
| 39 | InputManager::getInstance().leaveState("MouseAPI"); |
---|
| 40 | state->setMouseHandler(nullptr); |
---|
| 41 | InputManager::getInstance().destroyState("MouseAPI"); |
---|
[12309] | 42 | InputManager::getInstance().enterState("game"); |
---|
[12279] | 43 | } |
---|
| 44 | clickEvents.clear(); |
---|
| 45 | scrollEvents.clear(); |
---|
[12377] | 46 | gameInputActivated=false; |
---|
[12253] | 47 | } |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | MouseAPI::~MouseAPI() |
---|
| 51 | { |
---|
| 52 | |
---|
| 53 | } |
---|
| 54 | |
---|
[12247] | 55 | void MouseAPI::buttonPressed(MouseButtonCode::ByEnum button) |
---|
[12217] | 56 | { |
---|
[12377] | 57 | cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); |
---|
[12247] | 58 | Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); |
---|
[12287] | 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())); |
---|
[12217] | 62 | for(auto event: clickEvents) |
---|
| 63 | { |
---|
[12275] | 64 | for(auto wantedButton:event.buttons) |
---|
| 65 | { |
---|
[12309] | 66 | Ogre::Sphere sphere(event.position,event.radius); |
---|
| 67 | if(wantedButton == button && ray.intersects(sphere).first && cam->isVisible(sphere)) |
---|
[12253] | 68 | event.onClickedFunction(button); |
---|
[12217] | 69 | } |
---|
| 70 | } |
---|
| 71 | } |
---|
[12213] | 72 | |
---|
[12247] | 73 | void MouseAPI::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) |
---|
[12213] | 74 | { |
---|
| 75 | } |
---|
[12217] | 76 | |
---|
[12334] | 77 | void MouseAPI::tick(float dt) |
---|
| 78 | { |
---|
[12377] | 79 | if(active && !gameInputActivated) |
---|
[12334] | 80 | { |
---|
[12377] | 81 | InputManager::getInstance().leaveState("game"); |
---|
[12334] | 82 | } |
---|
| 83 | |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | |
---|
[12247] | 87 | void MouseAPI::mouseScrolled(int abs, int rel) |
---|
[12213] | 88 | { |
---|
[12271] | 89 | cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); |
---|
[12247] | 90 | Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); |
---|
[12287] | 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())); |
---|
[12275] | 94 | for(auto event:scrollEvents) |
---|
| 95 | { |
---|
[12309] | 96 | if(!event.considerPosition || (ray.intersects(Ogre::Sphere(event.position,event.radius)).first && cam->isVisible(Ogre::Sphere(event.position,event.radius)))) |
---|
[12287] | 97 | event.onScrolledFunction(abs,rel,IntVector2(mouseposX,mouseposY)); |
---|
[12217] | 98 | } |
---|
| 99 | } |
---|
| 100 | |
---|
[12352] | 101 | ClickableElementID MouseAPI::addClickableElement(const Vector3& position, float radius, const std::list<MouseButtonCode::ByEnum>& buttons, std::function<void(MouseButtonCode::ByEnum)> onClickedFunction) |
---|
[12217] | 102 | { |
---|
[12352] | 103 | ClickableElementID id = !clickEvents.empty() ? clickEvents.back().id + 1:0; |
---|
[12311] | 104 | clickEvents.insert(clickEvents.end(),{id,position,radius,buttons,onClickedFunction}); |
---|
| 105 | return id; |
---|
[12217] | 106 | } |
---|
[12271] | 107 | ScrollableElementID MouseAPI::addScrollElement(const Vector3& position, float radius, std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction) |
---|
[12217] | 108 | { |
---|
[12311] | 109 | ScrollableElementID id = !scrollEvents.empty() ? scrollEvents.back().id + 1:0; |
---|
| 110 | scrollEvents.insert(scrollEvents.end(),{id,position,radius,onScrolledFunction}); |
---|
| 111 | return id; |
---|
[12213] | 112 | } |
---|
[12271] | 113 | ScrollableElementID MouseAPI::addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction) |
---|
[12213] | 114 | { |
---|
[12311] | 115 | ScrollableElementID id = !scrollEvents.empty() ? scrollEvents.back().id + 1:0; |
---|
| 116 | scrollEvents.insert(scrollEvents.end(),{id,onScrolledFunction}); |
---|
| 117 | return id; |
---|
[12213] | 118 | } |
---|
[12217] | 119 | |
---|
[12247] | 120 | |
---|
[12352] | 121 | bool MouseAPI::changePositionOfClickableElement(ClickableElementID id,const Vector3& position) |
---|
[12275] | 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; |
---|
[12247] | 132 | } |
---|
[12275] | 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 | } |
---|
[12352] | 145 | bool MouseAPI::changeRadiusOfClickableElement(ClickableElementID id,float radius) |
---|
[12275] | 146 | { |
---|
[12302] | 147 | for(auto event = clickEvents.begin();event != clickEvents.end();event++ ) |
---|
[12275] | 148 | { |
---|
[12302] | 149 | if(event->id == id) |
---|
[12275] | 150 | { |
---|
[12302] | 151 | event->radius = radius; |
---|
[12275] | 152 | return true; |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | return false; |
---|
| 156 | } |
---|
| 157 | bool MouseAPI::changeRadiusOfScrollableElement(ScrollableElementID id,float radius) |
---|
| 158 | { |
---|
[12302] | 159 | for(auto event = scrollEvents.begin();event != scrollEvents.end();event++ ) |
---|
[12275] | 160 | { |
---|
[12302] | 161 | if(event->id == id) |
---|
[12275] | 162 | { |
---|
[12302] | 163 | event->radius = radius; |
---|
[12275] | 164 | return true; |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | return false; |
---|
| 168 | } |
---|
[12352] | 169 | bool MouseAPI::deleteClickableElement(ClickableElementID id) |
---|
[12275] | 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 | |
---|
[12352] | 194 | float MouseAPI::getRadiusClick(ClickableElementID id) |
---|
[12302] | 195 | { |
---|
| 196 | for(auto eventIt = clickEvents.begin();eventIt != clickEvents.end();eventIt++ ) |
---|
| 197 | { |
---|
| 198 | if(eventIt->id == id) |
---|
| 199 | { |
---|
| 200 | return eventIt->radius; |
---|
| 201 | } |
---|
| 202 | } |
---|
[12309] | 203 | return 0; |
---|
[12275] | 204 | } |
---|
[12302] | 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 | } |
---|
[12309] | 215 | return 0; |
---|
[12302] | 216 | } |
---|
| 217 | |
---|
[12309] | 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())); |
---|
[12302] | 222 | } |
---|
[12309] | 223 | |
---|
[12377] | 224 | void MouseAPI::activateGameInput() |
---|
| 225 | { |
---|
| 226 | gameInputActivated = true; |
---|
| 227 | state->setKeyHandler(nullptr); |
---|
[12309] | 228 | } |
---|
[12377] | 229 | |
---|
| 230 | void MouseAPI::deactivateGameInput() |
---|
| 231 | { |
---|
| 232 | gameInputActivated = false; |
---|
| 233 | state->setKeyHandler(KeyBinderManager::getInstance().getDefaultAsHandler()); |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | } |
---|