| 1 | #ifndef MOUSEAPI_H |
|---|
| 2 | #define MOUSEAPI_H |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | #include "OrxonoxPrereqs.h" |
|---|
| 6 | #include "util/OgreForwardRefs.h" |
|---|
| 7 | #include "graphics/Camera.h" |
|---|
| 8 | #include <util/Math.h> |
|---|
| 9 | #include <list> |
|---|
| 10 | #include <core/input/InputHandler.h> |
|---|
| 11 | #include <graphics/Camera.h> |
|---|
| 12 | #include <core/GraphicsManager.h> |
|---|
| 13 | #include <core/input/InputState.h> |
|---|
| 14 | #include <OgreCamera.h> |
|---|
| 15 | #include <OgreViewport.h> |
|---|
| 16 | #include "CameraManager.h" |
|---|
| 17 | #include <functional> |
|---|
| 18 | #include "core/GUIManager.h" |
|---|
| 19 | #include "core/input/KeyBinderManager.h" |
|---|
| 20 | #include "tools/interfaces/Tickable.h" |
|---|
| 21 | |
|---|
| 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 | |
|---|
| 36 | namespace orxonox |
|---|
| 37 | { |
|---|
| 38 | typedef uint ClickableObjectID; |
|---|
| 39 | typedef uint ScrollableElementID; |
|---|
| 40 | |
|---|
| 41 | class MouseAPI : public InputHandler, public Singleton<MouseAPI>,public Tickable |
|---|
| 42 | { |
|---|
| 43 | friend class Singleton<MouseAPI>; |
|---|
| 44 | private: |
|---|
| 45 | |
|---|
| 46 | // Elements that can be clicked on are stored as clickableElement |
|---|
| 47 | struct clickableElement |
|---|
| 48 | { |
|---|
| 49 | ClickableObjectID id; |
|---|
| 50 | Vector3 position; |
|---|
| 51 | float radius; |
|---|
| 52 | std::list<MouseButtonCode::ByEnum> buttons; |
|---|
| 53 | std::function<void(MouseButtonCode::ByEnum button)> onClickedFunction; |
|---|
| 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){} |
|---|
| 56 | }; |
|---|
| 57 | |
|---|
| 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 | */ |
|---|
| 63 | struct scrollElement |
|---|
| 64 | { |
|---|
| 65 | ScrollableElementID id; |
|---|
| 66 | bool considerPosition; |
|---|
| 67 | Vector3 position; |
|---|
| 68 | float radius; |
|---|
| 69 | std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction; |
|---|
| 70 | // constructor for scrollElement type 1 |
|---|
| 71 | scrollElement(ScrollableElementID id,std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction):id(id),considerPosition(false), |
|---|
| 72 | onScrolledFunction(onScrolledFunction){} |
|---|
| 73 | // constructor fro scrollElement type 2 |
|---|
| 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){} |
|---|
| 76 | }; |
|---|
| 77 | |
|---|
| 78 | // pointer to our class (required by singleton) |
|---|
| 79 | static MouseAPI* singletonPtr_s; |
|---|
| 80 | |
|---|
| 81 | // lists with all our Elements that can be clicked / scrolled on |
|---|
| 82 | std::list<clickableElement> clickEvents; |
|---|
| 83 | std::list<scrollElement> scrollEvents; |
|---|
| 84 | |
|---|
| 85 | // pointer to the game-camera |
|---|
| 86 | Ogre::Camera *cam ; |
|---|
| 87 | |
|---|
| 88 | //IntVector2 mousePos; |
|---|
| 89 | |
|---|
| 90 | // pointer to our input-state |
|---|
| 91 | InputState* state; |
|---|
| 92 | |
|---|
| 93 | // true => MouseAPI has been activated, false => MouseAPI has not been activated |
|---|
| 94 | bool active = false; |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | public: |
|---|
| 99 | |
|---|
| 100 | MouseAPI(); |
|---|
| 101 | ~MouseAPI(); |
|---|
| 102 | |
|---|
| 103 | virtual void tick(float dt) override; |
|---|
| 104 | |
|---|
| 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 | */ |
|---|
| 108 | virtual void buttonPressed (MouseButtonCode::ByEnum button) override; |
|---|
| 109 | |
|---|
| 110 | // not used |
|---|
| 111 | virtual void buttonReleased(MouseButtonCode::ByEnum button) override{} |
|---|
| 112 | |
|---|
| 113 | // not used |
|---|
| 114 | virtual void buttonHeld (MouseButtonCode::ByEnum button) override{} |
|---|
| 115 | |
|---|
| 116 | // not used |
|---|
| 117 | virtual void mouseMoved (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) override; |
|---|
| 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 | */ |
|---|
| 123 | virtual void mouseScrolled (int abs, int rel) override; |
|---|
| 124 | |
|---|
| 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 | */ |
|---|
| 134 | ClickableObjectID addClickableObject(const Vector3& position,float radius,const std::list<MouseButtonCode::ByEnum>& buttons,std::function<void(MouseButtonCode::ByEnum button)> onClickedFunction); |
|---|
| 135 | |
|---|
| 136 | /* |
|---|
| 137 | * |
|---|
| 138 | */ |
|---|
| 139 | ScrollableElementID addScrollElement(const Vector3& position,float radius,std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction); |
|---|
| 140 | |
|---|
| 141 | /* |
|---|
| 142 | * |
|---|
| 143 | */ |
|---|
| 144 | ScrollableElementID addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction); |
|---|
| 145 | |
|---|
| 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); |
|---|
| 153 | |
|---|
| 154 | float getRadiusClick(ClickableObjectID id); |
|---|
| 155 | float getRadiusScroll(ScrollableElementID id); |
|---|
| 156 | Vector2 getMousePosition(); |
|---|
| 157 | |
|---|
| 158 | void activate(); |
|---|
| 159 | static bool isActive(){return singletonPtr_s != nullptr && getInstance().active;} |
|---|
| 160 | void deactivate(); |
|---|
| 161 | }; |
|---|
| 162 | } |
|---|
| 163 | #endif // MOUSEAPI_H |
|---|