#include "mouseapi.h" namespace orxonox{ ManageScopedSingleton(MouseAPI, ScopeID::GRAPHICS, false); MouseAPI::MouseAPI() { } void MouseAPI::activate() { if(!active) { active = true; if(InputManager::exists()) { state = InputManager::getInstance().createInputState("MouseAPI",true,true,99); state->setMouseExclusive(false);//does this work state->setMouseHandler(this); gameInputActivated = false; state->setKeyHandler(KeyBinderManager::getInstance().getDefaultAsHandler()); InputManager::getInstance().enterState("guiMouseOnly"); InputManager::getInstance().enterState("MouseAPI"); InputManager::getInstance().setMouseExclusive("game",false); } } } void MouseAPI::deactivate() { if(active) { active = false; if(InputManager::exists()) { InputManager::getInstance().leaveState("MouseAPI"); state->setMouseHandler(nullptr); InputManager::getInstance().destroyState("MouseAPI"); InputManager::getInstance().enterState("game"); } clickEvents.clear(); scrollEvents.clear(); gameInputActivated=false; } } MouseAPI::~MouseAPI() { } void MouseAPI::buttonPressed(MouseButtonCode::ByEnum button) { cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); int mouseposX = InputManager::getInstance().getMousePosition().first; int mouseposY = InputManager::getInstance().getMousePosition().second; Ogre::Ray ray = cam->getCameraToViewportRay(mouseposX/((float)vp->getActualWidth()),mouseposY/((float)vp->getActualHeight())); for(auto event: clickEvents) { for(auto wantedButton:event.buttons) { Ogre::Sphere sphere(event.position,event.radius); if(wantedButton == button && ray.intersects(sphere).first && cam->isVisible(sphere)) event.onClickedFunction(button); } } } void MouseAPI::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) { } void MouseAPI::tick(float dt) { if(active && !gameInputActivated) { InputManager::getInstance().leaveState("game"); } } void MouseAPI::mouseScrolled(int abs, int rel) { cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); int mouseposX = InputManager::getInstance().getMousePosition().first; int mouseposY = InputManager::getInstance().getMousePosition().second; Ogre::Ray ray = cam->getCameraToViewportRay(mouseposX/((float)vp->getActualWidth()),mouseposY/((float)vp->getActualHeight())); for(auto event:scrollEvents) { if(!event.considerPosition || (ray.intersects(Ogre::Sphere(event.position,event.radius)).first && cam->isVisible(Ogre::Sphere(event.position,event.radius)))) event.onScrolledFunction(abs,rel,IntVector2(mouseposX,mouseposY)); } } ClickableElementID MouseAPI::addClickableElement(const Vector3& position, float radius, const std::list& buttons, std::function onClickedFunction) { ClickableElementID id = !clickEvents.empty() ? clickEvents.back().id + 1:0; clickEvents.insert(clickEvents.end(),{id,position,radius,buttons,onClickedFunction}); return id; } ScrollableElementID MouseAPI::addScrollElement(const Vector3& position, float radius, std::function onScrolledFunction) { ScrollableElementID id = !scrollEvents.empty() ? scrollEvents.back().id + 1:0; scrollEvents.insert(scrollEvents.end(),{id,position,radius,onScrolledFunction}); return id; } ScrollableElementID MouseAPI::addScrollElement(std::function onScrolledFunction) { ScrollableElementID id = !scrollEvents.empty() ? scrollEvents.back().id + 1:0; scrollEvents.insert(scrollEvents.end(),{id,onScrolledFunction}); return id; } bool MouseAPI::changePositionOfClickableElement(ClickableElementID id,const Vector3& position) { for(auto event:clickEvents) { if(event.id == id) { event.position = position; return true; } } return false; } bool MouseAPI::changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position) { for(auto event:scrollEvents) { if(event.id == id) { event.position = position; return true; } } return false; } bool MouseAPI::changeRadiusOfClickableElement(ClickableElementID id,float radius) { for(auto event = clickEvents.begin();event != clickEvents.end();event++ ) { if(event->id == id) { event->radius = radius; return true; } } return false; } bool MouseAPI::changeRadiusOfScrollableElement(ScrollableElementID id,float radius) { for(auto event = scrollEvents.begin();event != scrollEvents.end();event++ ) { if(event->id == id) { event->radius = radius; return true; } } return false; } bool MouseAPI::deleteClickableElement(ClickableElementID id) { for(auto eventIt = clickEvents.begin();eventIt != clickEvents.end();eventIt++ ) { if(eventIt->id == id) { clickEvents.erase(eventIt); return true; } } return false; } bool MouseAPI::deleteScrollableElement(ScrollableElementID id) { for(auto eventIt = scrollEvents.begin();eventIt != scrollEvents.end();eventIt++ ) { if(eventIt->id == id) { scrollEvents.erase(eventIt); return true; } } return false; } float MouseAPI::getRadiusClick(ClickableElementID id) { for(auto eventIt = clickEvents.begin();eventIt != clickEvents.end();eventIt++ ) { if(eventIt->id == id) { return eventIt->radius; } } return 0; } float MouseAPI::getRadiusScroll(ScrollableElementID id) { for(auto eventIt = scrollEvents.begin();eventIt != scrollEvents.end();eventIt++ ) { if(eventIt->id == id) { return eventIt->radius; } } return 0; } Vector2 MouseAPI::getMousePosition() { Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); return Vector2(InputManager::getInstance().getMousePosition().first/((float)vp->getActualWidth()),InputManager::getInstance().getMousePosition().second/((float)vp->getActualHeight())); } void MouseAPI::activateGameInput() { gameInputActivated = true; state->setKeyHandler(nullptr); } void MouseAPI::deactivateGameInput() { gameInputActivated = false; state->setKeyHandler(KeyBinderManager::getInstance().getDefaultAsHandler()); } }