[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 | //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; |
---|
[12213] | 79 | } |
---|
[12217] | 80 | |
---|
[12334] | 81 | void MouseAPI::tick(float dt) |
---|
| 82 | { |
---|
| 83 | if(active) |
---|
| 84 | { |
---|
| 85 | InputManager::getInstance().leaveState("game");//hack: todo: crate 2nd input state with prioritz 98 for cegui(cursor) |
---|
| 86 | GUIManager::getInstance().showGUI("MouseAPICursor", false);//hack todo: only if gui not shown & evt better if not in mouse mooved |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | |
---|
[12247] | 92 | void MouseAPI::mouseScrolled(int abs, int rel) |
---|
[12213] | 93 | { |
---|
[12271] | 94 | cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); |
---|
[12247] | 95 | Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); |
---|
[12287] | 96 | int mouseposX = InputManager::getInstance().getMousePosition().first; |
---|
| 97 | int mouseposY = InputManager::getInstance().getMousePosition().second; |
---|
| 98 | Ogre::Ray ray = cam->getCameraToViewportRay(mouseposX/((float)vp->getActualWidth()),mouseposY/((float)vp->getActualHeight())); |
---|
[12275] | 99 | for(auto event:scrollEvents) |
---|
| 100 | { |
---|
[12309] | 101 | if(!event.considerPosition || (ray.intersects(Ogre::Sphere(event.position,event.radius)).first && cam->isVisible(Ogre::Sphere(event.position,event.radius)))) |
---|
[12287] | 102 | event.onScrolledFunction(abs,rel,IntVector2(mouseposX,mouseposY)); |
---|
[12217] | 103 | } |
---|
| 104 | } |
---|
| 105 | |
---|
[12352] | 106 | ClickableElementID MouseAPI::addClickableElement(const Vector3& position, float radius, const std::list<MouseButtonCode::ByEnum>& buttons, std::function<void(MouseButtonCode::ByEnum)> onClickedFunction) |
---|
[12217] | 107 | { |
---|
[12352] | 108 | ClickableElementID id = !clickEvents.empty() ? clickEvents.back().id + 1:0; |
---|
[12311] | 109 | clickEvents.insert(clickEvents.end(),{id,position,radius,buttons,onClickedFunction}); |
---|
| 110 | return id; |
---|
[12217] | 111 | } |
---|
[12271] | 112 | ScrollableElementID MouseAPI::addScrollElement(const Vector3& position, float radius, std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction) |
---|
[12217] | 113 | { |
---|
[12311] | 114 | ScrollableElementID id = !scrollEvents.empty() ? scrollEvents.back().id + 1:0; |
---|
| 115 | scrollEvents.insert(scrollEvents.end(),{id,position,radius,onScrolledFunction}); |
---|
| 116 | return id; |
---|
[12213] | 117 | } |
---|
[12271] | 118 | ScrollableElementID MouseAPI::addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction) |
---|
[12213] | 119 | { |
---|
[12311] | 120 | ScrollableElementID id = !scrollEvents.empty() ? scrollEvents.back().id + 1:0; |
---|
| 121 | scrollEvents.insert(scrollEvents.end(),{id,onScrolledFunction}); |
---|
| 122 | return id; |
---|
[12213] | 123 | } |
---|
[12217] | 124 | |
---|
[12247] | 125 | |
---|
[12352] | 126 | bool MouseAPI::changePositionOfClickableElement(ClickableElementID id,const Vector3& position) |
---|
[12275] | 127 | { |
---|
| 128 | for(auto event:clickEvents) |
---|
| 129 | { |
---|
| 130 | if(event.id == id) |
---|
| 131 | { |
---|
| 132 | event.position = position; |
---|
| 133 | return true; |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | return false; |
---|
[12247] | 137 | } |
---|
[12275] | 138 | bool MouseAPI::changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position) |
---|
| 139 | { |
---|
| 140 | for(auto event:scrollEvents) |
---|
| 141 | { |
---|
| 142 | if(event.id == id) |
---|
| 143 | { |
---|
| 144 | event.position = position; |
---|
| 145 | return true; |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | return false; |
---|
| 149 | } |
---|
[12352] | 150 | bool MouseAPI::changeRadiusOfClickableElement(ClickableElementID id,float radius) |
---|
[12275] | 151 | { |
---|
[12302] | 152 | for(auto event = clickEvents.begin();event != clickEvents.end();event++ ) |
---|
[12275] | 153 | { |
---|
[12302] | 154 | if(event->id == id) |
---|
[12275] | 155 | { |
---|
[12302] | 156 | event->radius = radius; |
---|
[12275] | 157 | return true; |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | return false; |
---|
| 161 | } |
---|
| 162 | bool MouseAPI::changeRadiusOfScrollableElement(ScrollableElementID id,float radius) |
---|
| 163 | { |
---|
[12302] | 164 | for(auto event = scrollEvents.begin();event != scrollEvents.end();event++ ) |
---|
[12275] | 165 | { |
---|
[12302] | 166 | if(event->id == id) |
---|
[12275] | 167 | { |
---|
[12302] | 168 | event->radius = radius; |
---|
[12275] | 169 | return true; |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | return false; |
---|
| 173 | } |
---|
[12352] | 174 | bool MouseAPI::deleteClickableElement(ClickableElementID id) |
---|
[12275] | 175 | { |
---|
| 176 | for(auto eventIt = clickEvents.begin();eventIt != clickEvents.end();eventIt++ ) |
---|
| 177 | { |
---|
| 178 | if(eventIt->id == id) |
---|
| 179 | { |
---|
| 180 | clickEvents.erase(eventIt); |
---|
| 181 | return true; |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | return false; |
---|
| 185 | } |
---|
| 186 | bool MouseAPI::deleteScrollableElement(ScrollableElementID id) |
---|
| 187 | { |
---|
| 188 | for(auto eventIt = scrollEvents.begin();eventIt != scrollEvents.end();eventIt++ ) |
---|
| 189 | { |
---|
| 190 | if(eventIt->id == id) |
---|
| 191 | { |
---|
| 192 | scrollEvents.erase(eventIt); |
---|
| 193 | return true; |
---|
| 194 | } |
---|
| 195 | } |
---|
| 196 | return false; |
---|
| 197 | } |
---|
| 198 | |
---|
[12352] | 199 | float MouseAPI::getRadiusClick(ClickableElementID id) |
---|
[12302] | 200 | { |
---|
| 201 | for(auto eventIt = clickEvents.begin();eventIt != clickEvents.end();eventIt++ ) |
---|
| 202 | { |
---|
| 203 | if(eventIt->id == id) |
---|
| 204 | { |
---|
| 205 | return eventIt->radius; |
---|
| 206 | } |
---|
| 207 | } |
---|
[12309] | 208 | return 0; |
---|
[12275] | 209 | } |
---|
[12302] | 210 | |
---|
| 211 | float MouseAPI::getRadiusScroll(ScrollableElementID id) |
---|
| 212 | { |
---|
| 213 | for(auto eventIt = scrollEvents.begin();eventIt != scrollEvents.end();eventIt++ ) |
---|
| 214 | { |
---|
| 215 | if(eventIt->id == id) |
---|
| 216 | { |
---|
| 217 | return eventIt->radius; |
---|
| 218 | } |
---|
| 219 | } |
---|
[12309] | 220 | return 0; |
---|
[12302] | 221 | } |
---|
| 222 | |
---|
[12309] | 223 | //returns relative Position of the Mouse |
---|
| 224 | Vector2 MouseAPI::getMousePosition() |
---|
| 225 | { |
---|
| 226 | Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); |
---|
| 227 | return Vector2(InputManager::getInstance().getMousePosition().first/((float)vp->getActualWidth()),InputManager::getInstance().getMousePosition().second/((float)vp->getActualHeight())); |
---|
[12302] | 228 | } |
---|
[12309] | 229 | |
---|
| 230 | } |
---|