[12213] | 1 | #ifndef MOUSEAPI_H |
---|
| 2 | #define MOUSEAPI_H |
---|
| 3 | |
---|
[12247] | 4 | #include "OrxonoxPrereqs.h" |
---|
| 5 | #include "util/OgreForwardRefs.h" |
---|
| 6 | #include "graphics/Camera.h" |
---|
[12213] | 7 | #include <util/Math.h> |
---|
| 8 | #include <list> |
---|
| 9 | #include <core/input/InputHandler.h> |
---|
[12217] | 10 | #include <graphics/Camera.h> |
---|
| 11 | #include <core/GraphicsManager.h> |
---|
| 12 | #include <core/input/InputState.h> |
---|
[12247] | 13 | #include <OgreCamera.h> |
---|
| 14 | #include <OgreViewport.h> |
---|
[12253] | 15 | #include "CameraManager.h" |
---|
| 16 | #include <functional> |
---|
[12309] | 17 | #include "core/GUIManager.h" |
---|
| 18 | #include "core/input/KeyBinderManager.h" |
---|
[12348] | 19 | #include "tools/interfaces/Tickable.h" |
---|
[12362] | 20 | #include "core/singleton/ScopedSingletonIncludes.h" |
---|
[12213] | 21 | |
---|
[12352] | 22 | /* this class implements a basic mouse-api |
---|
[12333] | 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 |
---|
[12352] | 32 | * everytime a button is clicked or the mousewheel is turned, the list gets traversed and every element checked if it is clicked / scrolled on |
---|
| 33 | * checking happens by casting a ray from the camera through the mouse-cursor and testing if it intersects the sphere of the element |
---|
| 34 | * |
---|
| 35 | * to make it work, one has to add mouseapi in LINK_LIBRARIES in the file CMakeLists.txt of the level |
---|
| 36 | * see CMakeLists.txt in MouseAPIExample |
---|
[12333] | 37 | */ |
---|
| 38 | |
---|
[12213] | 39 | namespace orxonox |
---|
| 40 | { |
---|
[12352] | 41 | typedef uint ClickableElementID; |
---|
[12271] | 42 | typedef uint ScrollableElementID; |
---|
| 43 | |
---|
[12348] | 44 | class MouseAPI : public InputHandler, public Singleton<MouseAPI>,public Tickable |
---|
[12213] | 45 | { |
---|
[12271] | 46 | friend class Singleton<MouseAPI>; |
---|
[12362] | 47 | |
---|
[12213] | 48 | private: |
---|
| 49 | |
---|
[12333] | 50 | // Elements that can be clicked on are stored as clickableElement |
---|
[12213] | 51 | struct clickableElement |
---|
| 52 | { |
---|
[12352] | 53 | ClickableElementID id; |
---|
[12213] | 54 | Vector3 position; |
---|
| 55 | float radius; |
---|
[12247] | 56 | std::list<MouseButtonCode::ByEnum> buttons; |
---|
[12253] | 57 | std::function<void(MouseButtonCode::ByEnum button)> onClickedFunction; |
---|
[12352] | 58 | clickableElement(ClickableElementID id,const Vector3& position,float radius,const std::list<MouseButtonCode::ByEnum>& buttons,std::function<void(MouseButtonCode::ByEnum button)> onClickedFunction):id(id),position(position), |
---|
[12275] | 59 | radius(radius), buttons(buttons), onClickedFunction(onClickedFunction){} |
---|
[12213] | 60 | }; |
---|
| 61 | |
---|
[12333] | 62 | /* Elements that can be "scrolled on" are stored as scrollElement |
---|
| 63 | * there are 2 diffrent types, hence the overloaded constructor: |
---|
[12352] | 64 | * 1) the function is called whenever one scrolls, independet from position of element and cursor |
---|
| 65 | * 2) the function is only called when the cursor is placed over the element (identical to the clickableElement) |
---|
[12333] | 66 | */ |
---|
[12213] | 67 | struct scrollElement |
---|
| 68 | { |
---|
[12271] | 69 | ScrollableElementID id; |
---|
[12213] | 70 | bool considerPosition; |
---|
| 71 | Vector3 position; |
---|
| 72 | float radius; |
---|
[12271] | 73 | std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction; |
---|
[12333] | 74 | // constructor for scrollElement type 1 |
---|
[12275] | 75 | scrollElement(ScrollableElementID id,std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction):id(id),considerPosition(false), |
---|
| 76 | onScrolledFunction(onScrolledFunction){} |
---|
[12333] | 77 | // constructor fro scrollElement type 2 |
---|
[12275] | 78 | scrollElement(ScrollableElementID id,const Vector3& position, float radius, std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction):id(id),considerPosition(true), |
---|
| 79 | position(position), radius(radius), onScrolledFunction(onScrolledFunction){} |
---|
[12213] | 80 | }; |
---|
| 81 | |
---|
[12333] | 82 | // pointer to our class (required by singleton) |
---|
[12271] | 83 | static MouseAPI* singletonPtr_s; |
---|
[12333] | 84 | |
---|
| 85 | // lists with all our Elements that can be clicked / scrolled on |
---|
[12213] | 86 | std::list<clickableElement> clickEvents; |
---|
| 87 | std::list<scrollElement> scrollEvents; |
---|
[12333] | 88 | |
---|
| 89 | // pointer to the game-camera |
---|
[12247] | 90 | Ogre::Camera *cam ; |
---|
[12333] | 91 | |
---|
| 92 | // pointer to our input-state |
---|
[12217] | 93 | InputState* state; |
---|
[12333] | 94 | |
---|
| 95 | // true => MouseAPI has been activated, false => MouseAPI has not been activated |
---|
[12271] | 96 | bool active = false; |
---|
[12213] | 97 | |
---|
[12363] | 98 | Ogre::PanelOverlayElement* cursor; |
---|
| 99 | |
---|
[12213] | 100 | public: |
---|
| 101 | |
---|
[12253] | 102 | MouseAPI(); |
---|
[12213] | 103 | ~MouseAPI(); |
---|
[12333] | 104 | |
---|
[12348] | 105 | virtual void tick(float dt) override; |
---|
| 106 | |
---|
[12333] | 107 | /* everytime a mousebutton is pressed, this function is called and checks if the cursor is over an element that can be clicked on |
---|
| 108 | * if yes, the function associated with this element will be called with the corresponding button as argument |
---|
| 109 | */ |
---|
[12217] | 110 | virtual void buttonPressed (MouseButtonCode::ByEnum button) override; |
---|
[12333] | 111 | |
---|
| 112 | // not used |
---|
[12217] | 113 | virtual void buttonReleased(MouseButtonCode::ByEnum button) override{} |
---|
[12333] | 114 | |
---|
| 115 | // not used |
---|
[12217] | 116 | virtual void buttonHeld (MouseButtonCode::ByEnum button) override{} |
---|
[12333] | 117 | |
---|
| 118 | // not used |
---|
[12217] | 119 | virtual void mouseMoved (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) override; |
---|
[12333] | 120 | |
---|
| 121 | /* 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 |
---|
| 122 | * if yes, the function associated with this element will be called |
---|
| 123 | * if there is an element without position-requirement and an element the cursor is over, both their functions will be called |
---|
| 124 | */ |
---|
[12217] | 125 | virtual void mouseScrolled (int abs, int rel) override; |
---|
[12213] | 126 | |
---|
[12333] | 127 | /* add a clickableElement to the list |
---|
| 128 | * see mouseapiexample for an example-implementation |
---|
| 129 | * Arguments: |
---|
| 130 | * position: the point that needs to be clicked |
---|
[12352] | 131 | * 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) |
---|
[12333] | 132 | * buttons: the function will only be called, if one of these buttons is pressed |
---|
| 133 | * onClickedFunction: the function that will be called |
---|
| 134 | */ |
---|
[12352] | 135 | ClickableElementID addClickableElement(const Vector3& position,float radius,const std::list<MouseButtonCode::ByEnum>& buttons,std::function<void(MouseButtonCode::ByEnum button)> onClickedFunction); |
---|
[12333] | 136 | |
---|
[12352] | 137 | /* add a scrollElement to the list |
---|
| 138 | * see mouseapiexample for an example-implementation |
---|
| 139 | * Arguments: |
---|
| 140 | * position: the point the cursor needs to be over |
---|
| 141 | * radius: radius of the sphere around the position; if the cursor is inside this radius, the function will be executed |
---|
| 142 | * onScrolledFunction: the function that will be called |
---|
[12333] | 143 | */ |
---|
[12271] | 144 | ScrollableElementID addScrollElement(const Vector3& position,float radius,std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction); |
---|
[12333] | 145 | |
---|
[12352] | 146 | /* add a scrollElement to the list |
---|
| 147 | * Arguments: |
---|
| 148 | * onScrolledFunction: the function that will be called, no matter where the cursor is |
---|
[12333] | 149 | */ |
---|
[12271] | 150 | ScrollableElementID addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction); |
---|
[12217] | 151 | |
---|
[12352] | 152 | /* change the position of a clickableElement |
---|
| 153 | * Arguments: |
---|
| 154 | * id: the ClickableElementID of the element |
---|
| 155 | * position: the new position of the element |
---|
| 156 | * Return: |
---|
| 157 | * true if successfull |
---|
| 158 | * false if not successfull |
---|
| 159 | */ |
---|
| 160 | bool changePositionOfClickableElement(ClickableElementID id,const Vector3& position); |
---|
| 161 | |
---|
| 162 | /* change the position of a scrollElement |
---|
| 163 | * Arguments: |
---|
| 164 | * id: the ScrollableElementID of the element |
---|
| 165 | * position: the new position of the element |
---|
| 166 | * Return: |
---|
| 167 | * true if successfull |
---|
| 168 | * false if not successfull |
---|
| 169 | */ |
---|
[12275] | 170 | bool changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position); |
---|
[12352] | 171 | |
---|
| 172 | /* change the radius of a clickableElement |
---|
| 173 | * Arguments: |
---|
| 174 | * id: the ClickableElementID of the element |
---|
| 175 | * radius: the new radius of the element |
---|
| 176 | * Return: |
---|
| 177 | * true if successfull |
---|
| 178 | * false if not successfull |
---|
| 179 | */ |
---|
| 180 | bool changeRadiusOfClickableElement(ClickableElementID id,float radius); |
---|
| 181 | |
---|
| 182 | /* change the radius of a scrollElement |
---|
| 183 | * Arguments: |
---|
| 184 | * id: the ScrollableElementID of the element |
---|
| 185 | * radius: the new radius of the element |
---|
| 186 | * Return: |
---|
| 187 | * true if successfull |
---|
| 188 | * false if not successfull |
---|
| 189 | */ |
---|
[12275] | 190 | bool changeRadiusOfScrollableElement(ScrollableElementID id,float radius); |
---|
[12352] | 191 | |
---|
| 192 | /* remove a clickableElement |
---|
| 193 | * Arguments: |
---|
| 194 | * id: the ClickableElementID of the element |
---|
| 195 | * Return: |
---|
| 196 | * true if successfull |
---|
| 197 | * false if not successfull |
---|
| 198 | */ |
---|
| 199 | bool deleteClickableElement(ClickableElementID id); |
---|
| 200 | |
---|
| 201 | /* remove a scrollElement |
---|
| 202 | * Arguments: |
---|
| 203 | * id: the ScrollableElementID of the element |
---|
| 204 | * Return: |
---|
| 205 | * true if successfull |
---|
| 206 | * false if not successfull |
---|
| 207 | */ |
---|
[12275] | 208 | bool deleteScrollableElement(ScrollableElementID id); |
---|
[12253] | 209 | |
---|
[12352] | 210 | /* get the current radius of a clickableElement |
---|
| 211 | * Arguments: |
---|
| 212 | * id: the ClickableElementID of the element |
---|
| 213 | */ |
---|
| 214 | float getRadiusClick(ClickableElementID id); |
---|
| 215 | |
---|
| 216 | /* get the current radius of a scrollElement |
---|
| 217 | * Arguments: |
---|
| 218 | * id: the ScrollableElementID of the element |
---|
| 219 | */ |
---|
[12302] | 220 | float getRadiusScroll(ScrollableElementID id); |
---|
[12352] | 221 | |
---|
| 222 | /* get the current relative Position of the cursor |
---|
| 223 | * returns a value between 0 and 1 for both x and y component |
---|
| 224 | * (0,0) top left corner, (1,1) bottom right corner |
---|
| 225 | */ |
---|
[12309] | 226 | Vector2 getMousePosition(); |
---|
| 227 | |
---|
[12352] | 228 | /* activate the MouseAPI |
---|
| 229 | * has to be called after the level has been created (i.e. inside the xml-port |
---|
| 230 | * can be called multiple times, since the function checks the status of MouseAPI and does nothing if it already is active |
---|
| 231 | */ |
---|
[12253] | 232 | void activate(); |
---|
[12352] | 233 | |
---|
| 234 | // returns true if MouseAPI is active, false otherwise |
---|
[12271] | 235 | static bool isActive(){return singletonPtr_s != nullptr && getInstance().active;} |
---|
[12352] | 236 | |
---|
| 237 | /* deactivate the MouseAPI |
---|
| 238 | * has to be called, when the level gets closed (i.e. inside the level-destructor) |
---|
| 239 | * the function does nothing if MouseAPI is not active |
---|
| 240 | */ |
---|
[12253] | 241 | void deactivate(); |
---|
[12213] | 242 | }; |
---|
| 243 | } |
---|
| 244 | #endif // MOUSEAPI_H |
---|