| 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 | state->setKeyHandler(KeyBinderManager::getInstance().getDefaultAsHandler()); |
|---|
| 24 | InputManager::getInstance().enterState("guiMouseOnly"); |
|---|
| 25 | InputManager::getInstance().enterState("MouseAPI"); |
|---|
| 26 | InputManager::getInstance().setMouseExclusive("game",false); |
|---|
| 27 | //InputManager::getInstance().setMouseExclusive("guiMouseOnly",false); |
|---|
| 28 | //InputManager::getInstance().getState("game")-> |
|---|
| 29 | } |
|---|
| 30 | //GUIManager::getInstance().showGUI("MouseAPICursor", true);//Display a mouse cursor by displaying a empty menu |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | void MouseAPI::deactivate() |
|---|
| 36 | { |
|---|
| 37 | if(active) |
|---|
| 38 | { |
|---|
| 39 | GUIManager::getInstance().showGUI("MouseAPICursor", true); |
|---|
| 40 | active = false; |
|---|
| 41 | if(InputManager::exists()) |
|---|
| 42 | { |
|---|
| 43 | InputManager::getInstance().leaveState("MouseAPI"); |
|---|
| 44 | state->setMouseHandler(nullptr); |
|---|
| 45 | InputManager::getInstance().destroyState("MouseAPI"); |
|---|
| 46 | InputManager::getInstance().enterState("game"); |
|---|
| 47 | } |
|---|
| 48 | clickEvents.clear(); |
|---|
| 49 | scrollEvents.clear(); |
|---|
| 50 | } |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | MouseAPI::~MouseAPI() |
|---|
| 54 | { |
|---|
| 55 | |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | void MouseAPI::buttonPressed(MouseButtonCode::ByEnum button) |
|---|
| 59 | { |
|---|
| 60 | cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera();//todo: trycatch |
|---|
| 61 | Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); |
|---|
| 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())); |
|---|
| 65 | for(auto event: clickEvents) |
|---|
| 66 | { |
|---|
| 67 | for(auto wantedButton:event.buttons) |
|---|
| 68 | { |
|---|
| 69 | Ogre::Sphere sphere(event.position,event.radius); |
|---|
| 70 | if(wantedButton == button && ray.intersects(sphere).first && cam->isVisible(sphere)) |
|---|
| 71 | event.onClickedFunction(button); |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | void MouseAPI::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) |
|---|
| 77 | { |
|---|
| 78 | //mousePos = abs; |
|---|
| 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 |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | void MouseAPI::mouseScrolled(int abs, int rel) |
|---|
| 84 | { |
|---|
| 85 | cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); |
|---|
| 86 | Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); |
|---|
| 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())); |
|---|
| 90 | for(auto event:scrollEvents) |
|---|
| 91 | { |
|---|
| 92 | if(!event.considerPosition || (ray.intersects(Ogre::Sphere(event.position,event.radius)).first && cam->isVisible(Ogre::Sphere(event.position,event.radius)))) |
|---|
| 93 | event.onScrolledFunction(abs,rel,IntVector2(mouseposX,mouseposY)); |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | ClickableObjectID MouseAPI::addClickableObject(const Vector3& position, float radius, const std::list<MouseButtonCode::ByEnum>& buttons, std::function<void(MouseButtonCode::ByEnum)> onClickedFunction) |
|---|
| 98 | { |
|---|
| 99 | ClickableObjectID id = !clickEvents.empty() ? clickEvents.back().id + 1:0; |
|---|
| 100 | clickEvents.insert(clickEvents.end(),{id,position,radius,buttons,onClickedFunction}); |
|---|
| 101 | return id; |
|---|
| 102 | } |
|---|
| 103 | ScrollableElementID MouseAPI::addScrollElement(const Vector3& position, float radius, std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction) |
|---|
| 104 | { |
|---|
| 105 | ScrollableElementID id = !scrollEvents.empty() ? scrollEvents.back().id + 1:0; |
|---|
| 106 | scrollEvents.insert(scrollEvents.end(),{id,position,radius,onScrolledFunction}); |
|---|
| 107 | return id; |
|---|
| 108 | } |
|---|
| 109 | ScrollableElementID MouseAPI::addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction) |
|---|
| 110 | { |
|---|
| 111 | ScrollableElementID id = !scrollEvents.empty() ? scrollEvents.back().id + 1:0; |
|---|
| 112 | scrollEvents.insert(scrollEvents.end(),{id,onScrolledFunction}); |
|---|
| 113 | return id; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 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; |
|---|
| 128 | } |
|---|
| 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 | { |
|---|
| 143 | for(auto event = clickEvents.begin();event != clickEvents.end();event++ ) |
|---|
| 144 | { |
|---|
| 145 | if(event->id == id) |
|---|
| 146 | { |
|---|
| 147 | event->radius = radius; |
|---|
| 148 | return true; |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | return false; |
|---|
| 152 | } |
|---|
| 153 | bool MouseAPI::changeRadiusOfScrollableElement(ScrollableElementID id,float radius) |
|---|
| 154 | { |
|---|
| 155 | for(auto event = scrollEvents.begin();event != scrollEvents.end();event++ ) |
|---|
| 156 | { |
|---|
| 157 | if(event->id == id) |
|---|
| 158 | { |
|---|
| 159 | event->radius = radius; |
|---|
| 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 | |
|---|
| 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 | } |
|---|
| 199 | return 0; |
|---|
| 200 | } |
|---|
| 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 | } |
|---|
| 211 | return 0; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 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())); |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | } |
|---|