| 1 | #include "mouseapi.h" |
|---|
| 2 | |
|---|
| 3 | MouseAPI::MouseAPI(Camera& camera) |
|---|
| 4 | { |
|---|
| 5 | cam = camera.getOgreCamera(); |
|---|
| 6 | state = InputManager::getInstance().createInputState("MouseAPI"); |
|---|
| 7 | state->setMouseExclusive(false);//does this work |
|---|
| 8 | state->setMouseHandler(this); |
|---|
| 9 | InputManager::getInstance().enterState("MouseAPI"); |
|---|
| 10 | |
|---|
| 11 | } |
|---|
| 12 | |
|---|
| 13 | MouseAPI::~MouseAPI(){ |
|---|
| 14 | //todo look at gslevel destructor and implement this one |
|---|
| 15 | } |
|---|
| 16 | |
|---|
| 17 | virtual void MouseAPI::buttonPressed (MouseButtonCode::ByEnum button) |
|---|
| 18 | { |
|---|
| 19 | Ogre::Viewport vp = GraphicsManager::getInstance().getViewport(); |
|---|
| 20 | Ogre::Ray ray = getCameraToViewPortRay(mousePos.x/((float)vp.getActualWidth()),mousePos.y/((float)vp.getActualHeight())); |
|---|
| 21 | for(auto event: clickEvents) |
|---|
| 22 | { |
|---|
| 23 | for(auto wantedButton:buttons){ |
|---|
| 24 | if(wantedButton == button && ray.intersects(Ogre::Sphere(event.position,radius))) |
|---|
| 25 | (&(event.onClickedFunction))(button); |
|---|
| 26 | } |
|---|
| 27 | } |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | virtual void MouseAPI::mouseMoved (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) |
|---|
| 31 | { |
|---|
| 32 | mousePos = abs; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | virtual void MouseAPI::mouseScrolled (int abs, int rel) |
|---|
| 36 | { |
|---|
| 37 | Ogre::Viewport vp = GraphicsManager::getInstance().getViewport(); |
|---|
| 38 | Ogre::Ray ray = getCameraToViewPortRay(mousePos.x/((float)vp.getActualWidth()),mousePos.y/((float)vp.getActualHeight())); |
|---|
| 39 | for(auto event:scrollEvents){ |
|---|
| 40 | if(!event.considerPosition || ray.intersects(Ogre::Sphere(event.position,radius))) |
|---|
| 41 | (*(event.onScrolledFunction))(abs,rel,mousePos); |
|---|
| 42 | } |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | void MouseAPI::addClickableObject(const Vector3& position,float radius,const list<MouseButtonCode::ByEnum>& buttons,void (*onClickedFunction)(MouseButtonCode::ByEnum button)) |
|---|
| 46 | { |
|---|
| 47 | clickEvents.insert({position,radius,buttons,onClickedFunction}); |
|---|
| 48 | } |
|---|
| 49 | void MouseAPI::addScrollElement(const Vector3& position,float radius,void (*onScrolledFunction)(int abs,int rel,const IntVector2& mousePos)) |
|---|
| 50 | { |
|---|
| 51 | scrollEvents.insert(position,radius,onScrolledFunction); |
|---|
| 52 | } |
|---|
| 53 | void MouseAPI::addScrollElement(void (*onScrolledFunction)(int abs,int rel,const IntVector2& mousePos)) |
|---|
| 54 | { |
|---|
| 55 | scrollEvents.insert(onScrolledFunction); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | void MouseAPI::changeCamera(Camera& camera) |
|---|
| 59 | { |
|---|
| 60 | cam = camera.getOgreCamera(); |
|---|
| 61 | } |
|---|