[12213] | 1 | #ifndef MOUSEAPI_H |
---|
| 2 | #define MOUSEAPI_H |
---|
| 3 | |
---|
[12247] | 4 | |
---|
| 5 | #include "OrxonoxPrereqs.h" |
---|
| 6 | #include "util/OgreForwardRefs.h" |
---|
| 7 | #include "graphics/Camera.h" |
---|
[12213] | 8 | #include <util/Math.h> |
---|
| 9 | #include <list> |
---|
| 10 | #include <core/input/InputHandler.h> |
---|
[12217] | 11 | #include <graphics/Camera.h> |
---|
| 12 | #include <core/GraphicsManager.h> |
---|
| 13 | #include <core/input/InputState.h> |
---|
[12247] | 14 | #include <OgreCamera.h> |
---|
| 15 | #include <OgreViewport.h> |
---|
[12253] | 16 | #include "CameraManager.h" |
---|
| 17 | #include <functional> |
---|
[12309] | 18 | #include "core/GUIManager.h" |
---|
| 19 | #include "core/input/KeyBinderManager.h" |
---|
[12348] | 20 | #include "tools/interfaces/Tickable.h" |
---|
[12213] | 21 | |
---|
[12333] | 22 | /* This class implements a basic mouse-api |
---|
| 23 | * supported are mouse-clicks (left, right, mousewheel, ...) and scrolling |
---|
| 24 | * |
---|
| 25 | * mouse-clicks always are asscociated with an ingame element that has a position and a sphere with a certain radius around it |
---|
| 26 | * if the cursor is inside this sphere and a button is pressed, a user-defined function will be called |
---|
| 27 | * |
---|
| 28 | * scrolling can either be global (independent of where the cursor is) or local (same as a mouse-click) |
---|
| 29 | * in both cases a user-defined function will be called |
---|
| 30 | * |
---|
| 31 | * in short the class works by storing every element that can be clicked / scrolled on in a list |
---|
| 32 | * everytime a button is clicked or the mousewheel is turned, the list gets traversed and every element checked wheter it is clicked / scrolled on |
---|
| 33 | * checking happens by casting a ray from the camera through the mouse-cursor and testing wheter it intersects the sphere of the element |
---|
| 34 | */ |
---|
| 35 | |
---|
[12213] | 36 | namespace orxonox |
---|
| 37 | { |
---|
[12271] | 38 | typedef uint ClickableObjectID; |
---|
| 39 | typedef uint ScrollableElementID; |
---|
| 40 | |
---|
[12348] | 41 | class MouseAPI : public InputHandler, public Singleton<MouseAPI>,public Tickable |
---|
[12213] | 42 | { |
---|
[12271] | 43 | friend class Singleton<MouseAPI>; |
---|
[12213] | 44 | private: |
---|
| 45 | |
---|
[12333] | 46 | // Elements that can be clicked on are stored as clickableElement |
---|
[12213] | 47 | struct clickableElement |
---|
| 48 | { |
---|
[12271] | 49 | ClickableObjectID id; |
---|
[12213] | 50 | Vector3 position; |
---|
| 51 | float radius; |
---|
[12247] | 52 | std::list<MouseButtonCode::ByEnum> buttons; |
---|
[12253] | 53 | std::function<void(MouseButtonCode::ByEnum button)> onClickedFunction; |
---|
[12275] | 54 | clickableElement(ClickableObjectID id,const Vector3& position,float radius,const std::list<MouseButtonCode::ByEnum>& buttons,std::function<void(MouseButtonCode::ByEnum button)> onClickedFunction):id(id),position(position), |
---|
| 55 | radius(radius), buttons(buttons), onClickedFunction(onClickedFunction){} |
---|
[12213] | 56 | }; |
---|
| 57 | |
---|
[12333] | 58 | /* Elements that can be "scrolled on" are stored as scrollElement |
---|
| 59 | * there are 2 diffrent types, hence the overloaded constructor: |
---|
| 60 | * 1) the function is called whenever one scrolls, independet from position of object and cursor |
---|
| 61 | * 2) the function is only called when the cursor is over the object (same as with a clickElement) |
---|
| 62 | */ |
---|
[12213] | 63 | struct scrollElement |
---|
| 64 | { |
---|
[12271] | 65 | ScrollableElementID id; |
---|
[12213] | 66 | bool considerPosition; |
---|
| 67 | Vector3 position; |
---|
| 68 | float radius; |
---|
[12271] | 69 | std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction; |
---|
[12333] | 70 | // constructor for scrollElement type 1 |
---|
[12275] | 71 | scrollElement(ScrollableElementID id,std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction):id(id),considerPosition(false), |
---|
| 72 | onScrolledFunction(onScrolledFunction){} |
---|
[12333] | 73 | // constructor fro scrollElement type 2 |
---|
[12275] | 74 | scrollElement(ScrollableElementID id,const Vector3& position, float radius, std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction):id(id),considerPosition(true), |
---|
| 75 | position(position), radius(radius), onScrolledFunction(onScrolledFunction){} |
---|
[12213] | 76 | }; |
---|
| 77 | |
---|
[12333] | 78 | // pointer to our class (required by singleton) |
---|
[12271] | 79 | static MouseAPI* singletonPtr_s; |
---|
[12333] | 80 | |
---|
| 81 | // lists with all our Elements that can be clicked / scrolled on |
---|
[12213] | 82 | std::list<clickableElement> clickEvents; |
---|
| 83 | std::list<scrollElement> scrollEvents; |
---|
[12333] | 84 | |
---|
| 85 | // pointer to the game-camera |
---|
[12247] | 86 | Ogre::Camera *cam ; |
---|
[12333] | 87 | |
---|
[12287] | 88 | //IntVector2 mousePos; |
---|
[12333] | 89 | |
---|
| 90 | // pointer to our input-state |
---|
[12217] | 91 | InputState* state; |
---|
[12333] | 92 | |
---|
| 93 | // true => MouseAPI has been activated, false => MouseAPI has not been activated |
---|
[12271] | 94 | bool active = false; |
---|
[12213] | 95 | |
---|
| 96 | |
---|
[12271] | 97 | |
---|
[12213] | 98 | public: |
---|
| 99 | |
---|
[12253] | 100 | MouseAPI(); |
---|
[12213] | 101 | ~MouseAPI(); |
---|
[12333] | 102 | |
---|
[12348] | 103 | virtual void tick(float dt) override; |
---|
| 104 | |
---|
[12333] | 105 | /* everytime a mousebutton is pressed, this function is called and checks if the cursor is over an element that can be clicked on |
---|
| 106 | * if yes, the function associated with this element will be called with the corresponding button as argument |
---|
| 107 | */ |
---|
[12217] | 108 | virtual void buttonPressed (MouseButtonCode::ByEnum button) override; |
---|
[12333] | 109 | |
---|
| 110 | // not used |
---|
[12217] | 111 | virtual void buttonReleased(MouseButtonCode::ByEnum button) override{} |
---|
[12333] | 112 | |
---|
| 113 | // not used |
---|
[12217] | 114 | virtual void buttonHeld (MouseButtonCode::ByEnum button) override{} |
---|
[12333] | 115 | |
---|
| 116 | // not used |
---|
[12217] | 117 | virtual void mouseMoved (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) override; |
---|
[12333] | 118 | |
---|
| 119 | /* everytime someone scrolls, this function is called and checks for all scrollElements, wheter a position is required and wheter the curser is over said position |
---|
| 120 | * if yes, the function associated with this element will be called |
---|
| 121 | * if there is an element without position-requirement and an element the cursor is over, both their functions will be called |
---|
| 122 | */ |
---|
[12217] | 123 | virtual void mouseScrolled (int abs, int rel) override; |
---|
[12213] | 124 | |
---|
[12333] | 125 | /* add a clickableElement to the list |
---|
| 126 | * see mouseapiexample for an example-implementation |
---|
| 127 | * Arguments: |
---|
| 128 | * position: the point that needs to be clicked |
---|
| 129 | * radius: radius of the sphere around the position, if the cursor is inside this radius, the function will be executed (because clicking on a single point is pretty hard) |
---|
| 130 | * buttons: the function will only be called, if one of these buttons is pressed |
---|
| 131 | * onClickedFunction: the function that will be called |
---|
| 132 | * |
---|
| 133 | */ |
---|
[12271] | 134 | ClickableObjectID addClickableObject(const Vector3& position,float radius,const std::list<MouseButtonCode::ByEnum>& buttons,std::function<void(MouseButtonCode::ByEnum button)> onClickedFunction); |
---|
[12333] | 135 | |
---|
| 136 | /* |
---|
| 137 | * |
---|
| 138 | */ |
---|
[12271] | 139 | ScrollableElementID addScrollElement(const Vector3& position,float radius,std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction); |
---|
[12333] | 140 | |
---|
| 141 | /* |
---|
| 142 | * |
---|
| 143 | */ |
---|
[12271] | 144 | ScrollableElementID addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction); |
---|
[12217] | 145 | |
---|
[12275] | 146 | //true: success; false: element not found |
---|
| 147 | bool changePositionOfClickableObject(ClickableObjectID id,const Vector3& position); |
---|
| 148 | bool changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position); |
---|
| 149 | bool changeRadiusOfClickableObject(ClickableObjectID id,float radius); |
---|
| 150 | bool changeRadiusOfScrollableElement(ScrollableElementID id,float radius); |
---|
| 151 | bool deleteClickableObject(ClickableObjectID id); |
---|
| 152 | bool deleteScrollableElement(ScrollableElementID id); |
---|
[12253] | 153 | |
---|
[12302] | 154 | float getRadiusClick(ClickableObjectID id); |
---|
| 155 | float getRadiusScroll(ScrollableElementID id); |
---|
[12309] | 156 | Vector2 getMousePosition(); |
---|
| 157 | |
---|
[12253] | 158 | void activate(); |
---|
[12271] | 159 | static bool isActive(){return singletonPtr_s != nullptr && getInstance().active;} |
---|
[12253] | 160 | void deactivate(); |
---|
[12213] | 161 | }; |
---|
| 162 | } |
---|
| 163 | #endif // MOUSEAPI_H |
---|