Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/MouseAPI_FS19/src/modules/MouseAPI/mouseapi.h @ 12363

Last change on this file since 12363 was 12363, checked in by tkuonen, 5 years ago

Added MouseCursor

File size: 9.8 KB
Line 
1#ifndef MOUSEAPI_H
2#define MOUSEAPI_H
3
4#include "OrxonoxPrereqs.h"
5#include "util/OgreForwardRefs.h"
6#include "graphics/Camera.h"
7#include <util/Math.h>
8#include <list>
9#include <core/input/InputHandler.h>
10#include <graphics/Camera.h>
11#include <core/GraphicsManager.h>
12#include <core/input/InputState.h>
13#include <OgreCamera.h>
14#include <OgreViewport.h>
15#include "CameraManager.h"
16#include <functional>
17#include "core/GUIManager.h"
18#include "core/input/KeyBinderManager.h"
19#include "tools/interfaces/Tickable.h"
20#include "core/singleton/ScopedSingletonIncludes.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 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
37 */
38
39namespace orxonox
40{
41typedef uint ClickableElementID;
42typedef uint ScrollableElementID;
43
44class MouseAPI : public InputHandler, public Singleton<MouseAPI>,public Tickable
45{
46friend class Singleton<MouseAPI>;
47
48private:
49
50    // Elements that can be clicked on are stored as clickableElement
51    struct clickableElement
52    {
53        ClickableElementID id;
54        Vector3 position;
55        float radius;
56        std::list<MouseButtonCode::ByEnum> buttons;
57        std::function<void(MouseButtonCode::ByEnum button)> onClickedFunction;
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),
59            radius(radius), buttons(buttons), onClickedFunction(onClickedFunction){}
60    };
61
62    /* Elements that can be "scrolled on" are stored as scrollElement
63     * there are 2 diffrent types, hence the overloaded constructor:
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)
66     */
67    struct scrollElement
68    {
69        ScrollableElementID id;
70        bool considerPosition;
71        Vector3 position;
72        float radius;
73        std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction;
74        // constructor for scrollElement type 1
75        scrollElement(ScrollableElementID id,std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction):id(id),considerPosition(false),
76            onScrolledFunction(onScrolledFunction){}
77        // constructor fro scrollElement type 2
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){}
80    };
81
82    // pointer to our class (required by singleton)
83    static MouseAPI* singletonPtr_s;
84
85    // lists with all our Elements that can be clicked / scrolled on
86    std::list<clickableElement> clickEvents;
87    std::list<scrollElement> scrollEvents;
88
89    // pointer to the game-camera
90    Ogre::Camera *cam ;
91
92    // pointer to our input-state
93    InputState* state;
94
95    // true => MouseAPI has been activated, false => MouseAPI has not been activated
96    bool active = false;
97
98    Ogre::PanelOverlayElement* cursor;
99
100public:
101
102    MouseAPI();
103    ~MouseAPI();
104
105    virtual void tick(float dt) override;
106
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     */
110    virtual void buttonPressed (MouseButtonCode::ByEnum button) override;
111
112    // not used
113    virtual void buttonReleased(MouseButtonCode::ByEnum button)  override{}
114
115    // not used
116    virtual void buttonHeld    (MouseButtonCode::ByEnum button) override{}
117
118    // not used
119    virtual void mouseMoved    (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) override;
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     */
125    virtual void mouseScrolled (int abs, int rel) override;
126
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
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)
132     *      buttons: the function will only be called, if one of these buttons is pressed
133     *      onClickedFunction: the function that will be called
134     */
135    ClickableElementID addClickableElement(const Vector3& position,float radius,const std::list<MouseButtonCode::ByEnum>& buttons,std::function<void(MouseButtonCode::ByEnum button)>  onClickedFunction);
136
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
143     */
144    ScrollableElementID addScrollElement(const Vector3& position,float radius,std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction);
145
146    /* add a scrollElement to the list
147     * Arguments:
148     *      onScrolledFunction: the function that will be called, no matter where the cursor is
149     */
150    ScrollableElementID addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction);
151
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     */
170    bool changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position);
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     */
190    bool changeRadiusOfScrollableElement(ScrollableElementID id,float radius);
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     */
208    bool deleteScrollableElement(ScrollableElementID id);
209
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     */
220    float getRadiusScroll(ScrollableElementID id);
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     */
226    Vector2 getMousePosition();
227
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     */
232    void activate();
233
234    // returns true if MouseAPI is active, false otherwise
235    static bool isActive(){return singletonPtr_s != nullptr && getInstance().active;}
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     */
241    void deactivate();
242};
243}
244#endif // MOUSEAPI_H
Note: See TracBrowser for help on using the repository browser.