[12213] | 1 | #include "mouseapi.h" |
---|
| 2 | |
---|
[12247] | 3 | namespace orxonox{ |
---|
| 4 | |
---|
[12253] | 5 | MouseAPI::MouseAPI() |
---|
[12213] | 6 | { |
---|
| 7 | |
---|
| 8 | } |
---|
| 9 | |
---|
[12253] | 10 | void MouseAPI::activate() |
---|
| 11 | { |
---|
| 12 | if(InputManager::exists()) |
---|
| 13 | { |
---|
[12263] | 14 | //cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); |
---|
| 15 | state = InputManager::getInstance().createInputState("MouseAPI",true,true,99); |
---|
[12253] | 16 | state->setMouseExclusive(false);//does this work |
---|
| 17 | state->setMouseHandler(this); |
---|
| 18 | InputManager::getInstance().enterState("MouseAPI"); |
---|
| 19 | } |
---|
| 20 | |
---|
[12217] | 21 | } |
---|
[12213] | 22 | |
---|
[12253] | 23 | void MouseAPI::deactivate() |
---|
| 24 | { |
---|
| 25 | if(InputManager::exists()) |
---|
| 26 | { |
---|
| 27 | InputManager::getInstance().leaveState("MouseAPI"); |
---|
| 28 | state->setMouseHandler(nullptr); |
---|
| 29 | InputManager::getInstance().destroyState("MouseAPI"); |
---|
| 30 | } |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | MouseAPI::~MouseAPI() |
---|
| 34 | { |
---|
| 35 | |
---|
| 36 | } |
---|
| 37 | |
---|
[12247] | 38 | void MouseAPI::buttonPressed(MouseButtonCode::ByEnum button) |
---|
[12217] | 39 | { |
---|
[12263] | 40 | cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); |
---|
[12247] | 41 | Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); |
---|
| 42 | Ogre::Ray ray = cam->getCameraToViewportRay(mousePos.x/((float)vp->getActualWidth()),mousePos.y/((float)vp->getActualHeight())); |
---|
[12217] | 43 | for(auto event: clickEvents) |
---|
| 44 | { |
---|
[12247] | 45 | for(auto wantedButton:event.buttons){ |
---|
| 46 | if(wantedButton == button && ray.intersects(Ogre::Sphere(event.position,event.radius)).first) |
---|
[12253] | 47 | event.onClickedFunction(button); |
---|
[12217] | 48 | } |
---|
| 49 | } |
---|
| 50 | } |
---|
[12213] | 51 | |
---|
[12247] | 52 | void MouseAPI::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) |
---|
[12213] | 53 | { |
---|
[12217] | 54 | mousePos = abs; |
---|
[12213] | 55 | } |
---|
[12217] | 56 | |
---|
[12247] | 57 | void MouseAPI::mouseScrolled(int abs, int rel) |
---|
[12213] | 58 | { |
---|
[12247] | 59 | Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); |
---|
| 60 | Ogre::Ray ray = cam->getCameraToViewportRay(mousePos.x/((float)vp->getActualWidth()),mousePos.y/((float)vp->getActualHeight())); |
---|
[12217] | 61 | for(auto event:scrollEvents){ |
---|
[12247] | 62 | if(!event.considerPosition || ray.intersects(Ogre::Sphere(event.position,event.radius)).first) |
---|
[12217] | 63 | (*(event.onScrolledFunction))(abs,rel,mousePos); |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | |
---|
[12253] | 67 | void MouseAPI::addClickableObject(const Vector3& position, float radius, const std::list<MouseButtonCode::ByEnum>& buttons, std::function<void(MouseButtonCode::ByEnum)> onClickedFunction) |
---|
[12217] | 68 | { |
---|
[12247] | 69 | clickEvents.insert(clickEvents.begin(),{position,radius,buttons,onClickedFunction}); |
---|
[12217] | 70 | } |
---|
| 71 | void MouseAPI::addScrollElement(const Vector3& position,float radius,void (*onScrolledFunction)(int abs,int rel,const IntVector2& mousePos)) |
---|
| 72 | { |
---|
[12247] | 73 | scrollEvents.insert(scrollEvents.begin(),{position,radius,onScrolledFunction}); |
---|
[12213] | 74 | } |
---|
[12217] | 75 | void MouseAPI::addScrollElement(void (*onScrolledFunction)(int abs,int rel,const IntVector2& mousePos)) |
---|
[12213] | 76 | { |
---|
[12247] | 77 | scrollEvents.insert(scrollEvents.begin(),{onScrolledFunction}); |
---|
[12213] | 78 | } |
---|
[12217] | 79 | |
---|
| 80 | void MouseAPI::changeCamera(Camera& camera) |
---|
| 81 | { |
---|
| 82 | cam = camera.getOgreCamera(); |
---|
| 83 | } |
---|
[12247] | 84 | |
---|
| 85 | } |
---|