[12213] | 1 | #include "mouseapi.h" |
---|
[12271] | 2 | #include "core/singleton/ScopedSingletonIncludes.h" |
---|
[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 | { |
---|
[12271] | 14 | active = true; |
---|
[12253] | 15 | if(InputManager::exists()) |
---|
| 16 | { |
---|
[12263] | 17 | //cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); |
---|
| 18 | state = InputManager::getInstance().createInputState("MouseAPI",true,true,99); |
---|
[12253] | 19 | state->setMouseExclusive(false);//does this work |
---|
| 20 | state->setMouseHandler(this); |
---|
| 21 | InputManager::getInstance().enterState("MouseAPI"); |
---|
| 22 | } |
---|
| 23 | |
---|
[12217] | 24 | } |
---|
[12213] | 25 | |
---|
[12253] | 26 | void MouseAPI::deactivate() |
---|
| 27 | { |
---|
[12271] | 28 | active = false; |
---|
[12253] | 29 | if(InputManager::exists()) |
---|
| 30 | { |
---|
| 31 | InputManager::getInstance().leaveState("MouseAPI"); |
---|
| 32 | state->setMouseHandler(nullptr); |
---|
| 33 | InputManager::getInstance().destroyState("MouseAPI"); |
---|
| 34 | } |
---|
[12271] | 35 | clickEvents.clear(); |
---|
| 36 | scrollEvents.clear(); |
---|
[12253] | 37 | } |
---|
| 38 | |
---|
| 39 | MouseAPI::~MouseAPI() |
---|
| 40 | { |
---|
| 41 | |
---|
| 42 | } |
---|
| 43 | |
---|
[12247] | 44 | void MouseAPI::buttonPressed(MouseButtonCode::ByEnum button) |
---|
[12217] | 45 | { |
---|
[12271] | 46 | cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera();//todo: trycatch |
---|
[12247] | 47 | Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); |
---|
| 48 | Ogre::Ray ray = cam->getCameraToViewportRay(mousePos.x/((float)vp->getActualWidth()),mousePos.y/((float)vp->getActualHeight())); |
---|
[12217] | 49 | for(auto event: clickEvents) |
---|
| 50 | { |
---|
[12275] | 51 | for(auto wantedButton:event.buttons) |
---|
| 52 | { |
---|
[12247] | 53 | if(wantedButton == button && ray.intersects(Ogre::Sphere(event.position,event.radius)).first) |
---|
[12253] | 54 | event.onClickedFunction(button); |
---|
[12217] | 55 | } |
---|
| 56 | } |
---|
| 57 | } |
---|
[12213] | 58 | |
---|
[12247] | 59 | void MouseAPI::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) |
---|
[12213] | 60 | { |
---|
[12217] | 61 | mousePos = abs; |
---|
[12213] | 62 | } |
---|
[12217] | 63 | |
---|
[12247] | 64 | void MouseAPI::mouseScrolled(int abs, int rel) |
---|
[12213] | 65 | { |
---|
[12271] | 66 | cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); |
---|
[12247] | 67 | Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); |
---|
| 68 | Ogre::Ray ray = cam->getCameraToViewportRay(mousePos.x/((float)vp->getActualWidth()),mousePos.y/((float)vp->getActualHeight())); |
---|
[12275] | 69 | for(auto event:scrollEvents) |
---|
| 70 | { |
---|
[12247] | 71 | if(!event.considerPosition || ray.intersects(Ogre::Sphere(event.position,event.radius)).first) |
---|
[12271] | 72 | event.onScrolledFunction(abs,rel,mousePos); |
---|
[12217] | 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
[12271] | 76 | ClickableObjectID MouseAPI::addClickableObject(const Vector3& position, float radius, const std::list<MouseButtonCode::ByEnum>& buttons, std::function<void(MouseButtonCode::ByEnum)> onClickedFunction) |
---|
[12217] | 77 | { |
---|
[12271] | 78 | clickEvents.insert(clickEvents.begin(),{!clickEvents.empty() ? clickEvents.back().id + 1:0,position,radius,buttons,onClickedFunction}); |
---|
| 79 | return clickEvents.back().id; |
---|
[12217] | 80 | } |
---|
[12271] | 81 | ScrollableElementID MouseAPI::addScrollElement(const Vector3& position, float radius, std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction) |
---|
[12217] | 82 | { |
---|
[12271] | 83 | scrollEvents.insert(scrollEvents.begin(),{!scrollEvents.empty() ? scrollEvents.back().id + 1:0,position,radius,onScrolledFunction}); |
---|
| 84 | return scrollEvents.back().id; |
---|
[12213] | 85 | } |
---|
[12271] | 86 | ScrollableElementID MouseAPI::addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction) |
---|
[12213] | 87 | { |
---|
[12271] | 88 | scrollEvents.insert(scrollEvents.begin(),{!scrollEvents.empty() ? scrollEvents.back().id + 1:0,onScrolledFunction}); |
---|
| 89 | return scrollEvents.back().id; |
---|
[12213] | 90 | } |
---|
[12217] | 91 | |
---|
[12247] | 92 | |
---|
[12275] | 93 | bool MouseAPI::changePositionOfClickableObject(ClickableObjectID id,const Vector3& position) |
---|
| 94 | { |
---|
| 95 | for(auto event:clickEvents) |
---|
| 96 | { |
---|
| 97 | if(event.id == id) |
---|
| 98 | { |
---|
| 99 | event.position = position; |
---|
| 100 | return true; |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | return false; |
---|
[12247] | 104 | } |
---|
[12275] | 105 | bool MouseAPI::changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position) |
---|
| 106 | { |
---|
| 107 | for(auto event:scrollEvents) |
---|
| 108 | { |
---|
| 109 | if(event.id == id) |
---|
| 110 | { |
---|
| 111 | event.position = position; |
---|
| 112 | return true; |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | return false; |
---|
| 116 | } |
---|
| 117 | bool MouseAPI::changeRadiusOfClickableObject(ClickableObjectID id,float radius) |
---|
| 118 | { |
---|
| 119 | for(auto event:clickEvents) |
---|
| 120 | { |
---|
| 121 | if(event.id == id) |
---|
| 122 | { |
---|
| 123 | event.radius = radius; |
---|
| 124 | return true; |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | return false; |
---|
| 128 | } |
---|
| 129 | bool MouseAPI::changeRadiusOfScrollableElement(ScrollableElementID id,float radius) |
---|
| 130 | { |
---|
| 131 | for(auto event:scrollEvents) |
---|
| 132 | { |
---|
| 133 | if(event.id == id) |
---|
| 134 | { |
---|
| 135 | event.radius = radius; |
---|
| 136 | return true; |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | return false; |
---|
| 140 | } |
---|
| 141 | bool MouseAPI::deleteClickableObject(ClickableObjectID id) |
---|
| 142 | { |
---|
| 143 | for(auto eventIt = clickEvents.begin();eventIt != clickEvents.end();eventIt++ ) |
---|
| 144 | { |
---|
| 145 | if(eventIt->id == id) |
---|
| 146 | { |
---|
| 147 | clickEvents.erase(eventIt); |
---|
| 148 | return true; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | return false; |
---|
| 152 | } |
---|
| 153 | bool MouseAPI::deleteScrollableElement(ScrollableElementID id) |
---|
| 154 | { |
---|
| 155 | for(auto eventIt = scrollEvents.begin();eventIt != scrollEvents.end();eventIt++ ) |
---|
| 156 | { |
---|
| 157 | if(eventIt->id == id) |
---|
| 158 | { |
---|
| 159 | scrollEvents.erase(eventIt); |
---|
| 160 | return true; |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | return false; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | } |
---|