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