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