[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(); |
---|
[12287] | 54 | int mouseposX = InputManager::getInstance().getMousePosition().first; |
---|
| 55 | int mouseposY = InputManager::getInstance().getMousePosition().second; |
---|
| 56 | Ogre::Ray ray = cam->getCameraToViewportRay(mouseposX/((float)vp->getActualWidth()),mouseposY/((float)vp->getActualHeight())); |
---|
[12217] | 57 | for(auto event: clickEvents) |
---|
| 58 | { |
---|
[12275] | 59 | for(auto wantedButton:event.buttons) |
---|
| 60 | { |
---|
[12247] | 61 | if(wantedButton == button && ray.intersects(Ogre::Sphere(event.position,event.radius)).first) |
---|
[12253] | 62 | event.onClickedFunction(button); |
---|
[12217] | 63 | } |
---|
| 64 | } |
---|
| 65 | } |
---|
[12213] | 66 | |
---|
[12247] | 67 | void MouseAPI::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) |
---|
[12213] | 68 | { |
---|
[12287] | 69 | //mousePos = abs; |
---|
[12213] | 70 | } |
---|
[12217] | 71 | |
---|
[12247] | 72 | void MouseAPI::mouseScrolled(int abs, int rel) |
---|
[12213] | 73 | { |
---|
[12271] | 74 | cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); |
---|
[12247] | 75 | Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); |
---|
[12287] | 76 | int mouseposX = InputManager::getInstance().getMousePosition().first; |
---|
| 77 | int mouseposY = InputManager::getInstance().getMousePosition().second; |
---|
| 78 | Ogre::Ray ray = cam->getCameraToViewportRay(mouseposX/((float)vp->getActualWidth()),mouseposY/((float)vp->getActualHeight())); |
---|
[12275] | 79 | for(auto event:scrollEvents) |
---|
| 80 | { |
---|
[12247] | 81 | if(!event.considerPosition || ray.intersects(Ogre::Sphere(event.position,event.radius)).first) |
---|
[12287] | 82 | event.onScrolledFunction(abs,rel,IntVector2(mouseposX,mouseposY)); |
---|
[12217] | 83 | } |
---|
| 84 | } |
---|
| 85 | |
---|
[12271] | 86 | ClickableObjectID MouseAPI::addClickableObject(const Vector3& position, float radius, const std::list<MouseButtonCode::ByEnum>& buttons, std::function<void(MouseButtonCode::ByEnum)> onClickedFunction) |
---|
[12217] | 87 | { |
---|
[12271] | 88 | clickEvents.insert(clickEvents.begin(),{!clickEvents.empty() ? clickEvents.back().id + 1:0,position,radius,buttons,onClickedFunction}); |
---|
| 89 | return clickEvents.back().id; |
---|
[12217] | 90 | } |
---|
[12271] | 91 | ScrollableElementID MouseAPI::addScrollElement(const Vector3& position, float radius, std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction) |
---|
[12217] | 92 | { |
---|
[12271] | 93 | scrollEvents.insert(scrollEvents.begin(),{!scrollEvents.empty() ? scrollEvents.back().id + 1:0,position,radius,onScrolledFunction}); |
---|
| 94 | return scrollEvents.back().id; |
---|
[12213] | 95 | } |
---|
[12271] | 96 | ScrollableElementID MouseAPI::addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction) |
---|
[12213] | 97 | { |
---|
[12271] | 98 | scrollEvents.insert(scrollEvents.begin(),{!scrollEvents.empty() ? scrollEvents.back().id + 1:0,onScrolledFunction}); |
---|
| 99 | return scrollEvents.back().id; |
---|
[12213] | 100 | } |
---|
[12217] | 101 | |
---|
[12247] | 102 | |
---|
[12275] | 103 | bool MouseAPI::changePositionOfClickableObject(ClickableObjectID id,const Vector3& position) |
---|
| 104 | { |
---|
| 105 | for(auto event:clickEvents) |
---|
| 106 | { |
---|
| 107 | if(event.id == id) |
---|
| 108 | { |
---|
| 109 | event.position = position; |
---|
| 110 | return true; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | return false; |
---|
[12247] | 114 | } |
---|
[12275] | 115 | bool MouseAPI::changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position) |
---|
| 116 | { |
---|
| 117 | for(auto event:scrollEvents) |
---|
| 118 | { |
---|
| 119 | if(event.id == id) |
---|
| 120 | { |
---|
| 121 | event.position = position; |
---|
| 122 | return true; |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | return false; |
---|
| 126 | } |
---|
| 127 | bool MouseAPI::changeRadiusOfClickableObject(ClickableObjectID id,float radius) |
---|
| 128 | { |
---|
[12302] | 129 | for(auto event = clickEvents.begin();event != clickEvents.end();event++ ) |
---|
[12275] | 130 | { |
---|
[12302] | 131 | if(event->id == id) |
---|
[12275] | 132 | { |
---|
[12302] | 133 | event->radius = radius; |
---|
[12275] | 134 | return true; |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | return false; |
---|
| 138 | } |
---|
| 139 | bool MouseAPI::changeRadiusOfScrollableElement(ScrollableElementID id,float radius) |
---|
| 140 | { |
---|
[12302] | 141 | for(auto event = scrollEvents.begin();event != scrollEvents.end();event++ ) |
---|
[12275] | 142 | { |
---|
[12302] | 143 | if(event->id == id) |
---|
[12275] | 144 | { |
---|
[12302] | 145 | event->radius = radius; |
---|
[12275] | 146 | return true; |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | return false; |
---|
| 150 | } |
---|
| 151 | bool MouseAPI::deleteClickableObject(ClickableObjectID id) |
---|
| 152 | { |
---|
| 153 | for(auto eventIt = clickEvents.begin();eventIt != clickEvents.end();eventIt++ ) |
---|
| 154 | { |
---|
| 155 | if(eventIt->id == id) |
---|
| 156 | { |
---|
| 157 | clickEvents.erase(eventIt); |
---|
| 158 | return true; |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | return false; |
---|
| 162 | } |
---|
| 163 | bool MouseAPI::deleteScrollableElement(ScrollableElementID id) |
---|
| 164 | { |
---|
| 165 | for(auto eventIt = scrollEvents.begin();eventIt != scrollEvents.end();eventIt++ ) |
---|
| 166 | { |
---|
| 167 | if(eventIt->id == id) |
---|
| 168 | { |
---|
| 169 | scrollEvents.erase(eventIt); |
---|
| 170 | return true; |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | return false; |
---|
| 174 | } |
---|
| 175 | |
---|
[12302] | 176 | float MouseAPI::getRadiusClick(ClickableObjectID id) |
---|
| 177 | { |
---|
| 178 | for(auto eventIt = clickEvents.begin();eventIt != clickEvents.end();eventIt++ ) |
---|
| 179 | { |
---|
| 180 | if(eventIt->id == id) |
---|
| 181 | { |
---|
| 182 | return eventIt->radius; |
---|
| 183 | } |
---|
| 184 | } |
---|
[12275] | 185 | } |
---|
[12302] | 186 | |
---|
| 187 | float MouseAPI::getRadiusScroll(ScrollableElementID id) |
---|
| 188 | { |
---|
| 189 | for(auto eventIt = scrollEvents.begin();eventIt != scrollEvents.end();eventIt++ ) |
---|
| 190 | { |
---|
| 191 | if(eventIt->id == id) |
---|
| 192 | { |
---|
| 193 | return eventIt->radius; |
---|
| 194 | } |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | } |
---|