| 1 | #include "mouseapi.h" |
|---|
| 2 | |
|---|
| 3 | #if OGRE_VERSION >= 0x010900 |
|---|
| 4 | # include <Overlay/OgreOverlayManager.h> |
|---|
| 5 | # include <Overlay/OgrePanelOverlayElement.h> |
|---|
| 6 | #else |
|---|
| 7 | # include <OgreOverlayManager.h> |
|---|
| 8 | # include <OgrePanelOverlayElement.h> |
|---|
| 9 | #endif |
|---|
| 10 | |
|---|
| 11 | #include "util/StringUtils.h" |
|---|
| 12 | |
|---|
| 13 | namespace orxonox{ |
|---|
| 14 | |
|---|
| 15 | ManageScopedSingleton(MouseAPI, ScopeID::GRAPHICS, false); |
|---|
| 16 | |
|---|
| 17 | MouseAPI::MouseAPI() |
|---|
| 18 | { |
|---|
| 19 | |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | void MouseAPI::activate() |
|---|
| 23 | { |
|---|
| 24 | if(!active) |
|---|
| 25 | { |
|---|
| 26 | active = true; |
|---|
| 27 | if(InputManager::exists()) |
|---|
| 28 | { |
|---|
| 29 | //cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); |
|---|
| 30 | state = InputManager::getInstance().createInputState("MouseAPI",true,true,99); |
|---|
| 31 | state->setMouseExclusive(false);//does this work |
|---|
| 32 | state->setMouseHandler(this); |
|---|
| 33 | state->setKeyHandler(KeyBinderManager::getInstance().getDefaultAsHandler()); |
|---|
| 34 | InputManager::getInstance().enterState("guiMouseOnly"); |
|---|
| 35 | InputManager::getInstance().enterState("MouseAPI"); |
|---|
| 36 | InputManager::getInstance().setMouseExclusive("game",false); |
|---|
| 37 | //InputManager::getInstance().setMouseExclusive("guiMouseOnly",false); |
|---|
| 38 | //InputManager::getInstance().getState("game")-> |
|---|
| 39 | } |
|---|
| 40 | //GUIManager::getInstance().showGUI("MouseAPICursor", true);//Display a mouse cursor by displaying a empty menu |
|---|
| 41 | /*cursor = static_cast<Ogre::PanelOverlayElement*>(Ogre::OverlayManager::getSingleton() |
|---|
| 42 | .createOverlayElement("Panel", "MouseAPI_cursor_" + getUniqueNumberString())); |
|---|
| 43 | cursor->setMaterialName("Orxonox/RadarMarker");//todo: better material |
|---|
| 44 | cursor->setPosition(0,0); |
|---|
| 45 | cursor->setDimensions(0.1,0.1); |
|---|
| 46 | Ogre::Overlay* overlay = Ogre::OverlayManager::getSingleton().create( "MouseAPI_cursor_" + getUniqueNumberString() ); |
|---|
| 47 | overlay->show();*/ |
|---|
| 48 | //this->overlay_->add2D(this->cursor); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | void MouseAPI::deactivate() |
|---|
| 54 | { |
|---|
| 55 | if(active) |
|---|
| 56 | { |
|---|
| 57 | GUIManager::getInstance().showGUI("MouseAPICursor", true); |
|---|
| 58 | active = false; |
|---|
| 59 | if(InputManager::exists()) |
|---|
| 60 | { |
|---|
| 61 | InputManager::getInstance().leaveState("MouseAPI"); |
|---|
| 62 | state->setMouseHandler(nullptr); |
|---|
| 63 | InputManager::getInstance().destroyState("MouseAPI"); |
|---|
| 64 | InputManager::getInstance().enterState("game"); |
|---|
| 65 | } |
|---|
| 66 | clickEvents.clear(); |
|---|
| 67 | scrollEvents.clear(); |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | MouseAPI::~MouseAPI() |
|---|
| 72 | { |
|---|
| 73 | |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | void MouseAPI::buttonPressed(MouseButtonCode::ByEnum button) |
|---|
| 77 | { |
|---|
| 78 | cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera();//todo: trycatch |
|---|
| 79 | Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); |
|---|
| 80 | int mouseposX = InputManager::getInstance().getMousePosition().first; |
|---|
| 81 | int mouseposY = InputManager::getInstance().getMousePosition().second; |
|---|
| 82 | Ogre::Ray ray = cam->getCameraToViewportRay(mouseposX/((float)vp->getActualWidth()),mouseposY/((float)vp->getActualHeight())); |
|---|
| 83 | for(auto event: clickEvents) |
|---|
| 84 | { |
|---|
| 85 | for(auto wantedButton:event.buttons) |
|---|
| 86 | { |
|---|
| 87 | Ogre::Sphere sphere(event.position,event.radius); |
|---|
| 88 | if(wantedButton == button && ray.intersects(sphere).first && cam->isVisible(sphere)) |
|---|
| 89 | event.onClickedFunction(button); |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | void MouseAPI::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) |
|---|
| 95 | { |
|---|
| 96 | //mousePos = abs; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | void MouseAPI::tick(float dt) |
|---|
| 100 | { |
|---|
| 101 | if(active) |
|---|
| 102 | { |
|---|
| 103 | InputManager::getInstance().leaveState("game");//hack: todo: crate 2nd input state with prioritz 98 for cegui(cursor) |
|---|
| 104 | //GUIManager::getInstance().showGUI("MouseAPICursor", false);//hack todo: only if gui not shown & evt better if not in mouse mooved |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | void MouseAPI::mouseScrolled(int abs, int rel) |
|---|
| 111 | { |
|---|
| 112 | cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); |
|---|
| 113 | Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); |
|---|
| 114 | int mouseposX = InputManager::getInstance().getMousePosition().first; |
|---|
| 115 | int mouseposY = InputManager::getInstance().getMousePosition().second; |
|---|
| 116 | Ogre::Ray ray = cam->getCameraToViewportRay(mouseposX/((float)vp->getActualWidth()),mouseposY/((float)vp->getActualHeight())); |
|---|
| 117 | for(auto event:scrollEvents) |
|---|
| 118 | { |
|---|
| 119 | if(!event.considerPosition || (ray.intersects(Ogre::Sphere(event.position,event.radius)).first && cam->isVisible(Ogre::Sphere(event.position,event.radius)))) |
|---|
| 120 | event.onScrolledFunction(abs,rel,IntVector2(mouseposX,mouseposY)); |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | ClickableElementID MouseAPI::addClickableElement(const Vector3& position, float radius, const std::list<MouseButtonCode::ByEnum>& buttons, std::function<void(MouseButtonCode::ByEnum)> onClickedFunction) |
|---|
| 125 | { |
|---|
| 126 | ClickableElementID id = !clickEvents.empty() ? clickEvents.back().id + 1:0; |
|---|
| 127 | clickEvents.insert(clickEvents.end(),{id,position,radius,buttons,onClickedFunction}); |
|---|
| 128 | return id; |
|---|
| 129 | } |
|---|
| 130 | ScrollableElementID MouseAPI::addScrollElement(const Vector3& position, float radius, std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction) |
|---|
| 131 | { |
|---|
| 132 | ScrollableElementID id = !scrollEvents.empty() ? scrollEvents.back().id + 1:0; |
|---|
| 133 | scrollEvents.insert(scrollEvents.end(),{id,position,radius,onScrolledFunction}); |
|---|
| 134 | return id; |
|---|
| 135 | } |
|---|
| 136 | ScrollableElementID MouseAPI::addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction) |
|---|
| 137 | { |
|---|
| 138 | ScrollableElementID id = !scrollEvents.empty() ? scrollEvents.back().id + 1:0; |
|---|
| 139 | scrollEvents.insert(scrollEvents.end(),{id,onScrolledFunction}); |
|---|
| 140 | return id; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | bool MouseAPI::changePositionOfClickableElement(ClickableElementID id,const Vector3& position) |
|---|
| 145 | { |
|---|
| 146 | for(auto event:clickEvents) |
|---|
| 147 | { |
|---|
| 148 | if(event.id == id) |
|---|
| 149 | { |
|---|
| 150 | event.position = position; |
|---|
| 151 | return true; |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | return false; |
|---|
| 155 | } |
|---|
| 156 | bool MouseAPI::changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position) |
|---|
| 157 | { |
|---|
| 158 | for(auto event:scrollEvents) |
|---|
| 159 | { |
|---|
| 160 | if(event.id == id) |
|---|
| 161 | { |
|---|
| 162 | event.position = position; |
|---|
| 163 | return true; |
|---|
| 164 | } |
|---|
| 165 | } |
|---|
| 166 | return false; |
|---|
| 167 | } |
|---|
| 168 | bool MouseAPI::changeRadiusOfClickableElement(ClickableElementID id,float radius) |
|---|
| 169 | { |
|---|
| 170 | for(auto event = clickEvents.begin();event != clickEvents.end();event++ ) |
|---|
| 171 | { |
|---|
| 172 | if(event->id == id) |
|---|
| 173 | { |
|---|
| 174 | event->radius = radius; |
|---|
| 175 | return true; |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | return false; |
|---|
| 179 | } |
|---|
| 180 | bool MouseAPI::changeRadiusOfScrollableElement(ScrollableElementID id,float radius) |
|---|
| 181 | { |
|---|
| 182 | for(auto event = scrollEvents.begin();event != scrollEvents.end();event++ ) |
|---|
| 183 | { |
|---|
| 184 | if(event->id == id) |
|---|
| 185 | { |
|---|
| 186 | event->radius = radius; |
|---|
| 187 | return true; |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | return false; |
|---|
| 191 | } |
|---|
| 192 | bool MouseAPI::deleteClickableElement(ClickableElementID id) |
|---|
| 193 | { |
|---|
| 194 | for(auto eventIt = clickEvents.begin();eventIt != clickEvents.end();eventIt++ ) |
|---|
| 195 | { |
|---|
| 196 | if(eventIt->id == id) |
|---|
| 197 | { |
|---|
| 198 | clickEvents.erase(eventIt); |
|---|
| 199 | return true; |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | return false; |
|---|
| 203 | } |
|---|
| 204 | bool MouseAPI::deleteScrollableElement(ScrollableElementID id) |
|---|
| 205 | { |
|---|
| 206 | for(auto eventIt = scrollEvents.begin();eventIt != scrollEvents.end();eventIt++ ) |
|---|
| 207 | { |
|---|
| 208 | if(eventIt->id == id) |
|---|
| 209 | { |
|---|
| 210 | scrollEvents.erase(eventIt); |
|---|
| 211 | return true; |
|---|
| 212 | } |
|---|
| 213 | } |
|---|
| 214 | return false; |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | float MouseAPI::getRadiusClick(ClickableElementID id) |
|---|
| 218 | { |
|---|
| 219 | for(auto eventIt = clickEvents.begin();eventIt != clickEvents.end();eventIt++ ) |
|---|
| 220 | { |
|---|
| 221 | if(eventIt->id == id) |
|---|
| 222 | { |
|---|
| 223 | return eventIt->radius; |
|---|
| 224 | } |
|---|
| 225 | } |
|---|
| 226 | return 0; |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | float MouseAPI::getRadiusScroll(ScrollableElementID id) |
|---|
| 230 | { |
|---|
| 231 | for(auto eventIt = scrollEvents.begin();eventIt != scrollEvents.end();eventIt++ ) |
|---|
| 232 | { |
|---|
| 233 | if(eventIt->id == id) |
|---|
| 234 | { |
|---|
| 235 | return eventIt->radius; |
|---|
| 236 | } |
|---|
| 237 | } |
|---|
| 238 | return 0; |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | //returns relative Position of the Mouse |
|---|
| 242 | Vector2 MouseAPI::getMousePosition() |
|---|
| 243 | { |
|---|
| 244 | Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); |
|---|
| 245 | return Vector2(InputManager::getInstance().getMousePosition().first/((float)vp->getActualWidth()),InputManager::getInstance().getMousePosition().second/((float)vp->getActualHeight())); |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | } |
|---|