Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12353 was 12352, checked in by mkarpf, 5 years ago

separated MouseAPIExample from MouseAPI

finished comments in MouseAPI.h

File size: 9.7 KB
Line 
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 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>;
47private:
48
49    // Elements that can be clicked on are stored as clickableElement
50    struct clickableElement
51    {
52        ClickableElementID id;
53        Vector3 position;
54        float radius;
55        std::list<MouseButtonCode::ByEnum> buttons;
56        std::function<void(MouseButtonCode::ByEnum button)> onClickedFunction;
57        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),
58            radius(radius), buttons(buttons), onClickedFunction(onClickedFunction){}
59    };
60
61    /* Elements that can be "scrolled on" are stored as scrollElement
62     * there are 2 diffrent types, hence the overloaded constructor:
63     *      1) the function is called whenever one scrolls, independet from position of element and cursor
64     *      2) the function is only called when the cursor is placed over the element (identical to the clickableElement)
65     */
66    struct scrollElement
67    {
68        ScrollableElementID id;
69        bool considerPosition;
70        Vector3 position;
71        float radius;
72        std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction;
73        // constructor for scrollElement type 1
74        scrollElement(ScrollableElementID id,std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction):id(id),considerPosition(false),
75            onScrolledFunction(onScrolledFunction){}
76        // constructor fro scrollElement type 2
77        scrollElement(ScrollableElementID id,const Vector3& position, float radius, std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction):id(id),considerPosition(true),
78            position(position), radius(radius), onScrolledFunction(onScrolledFunction){}
79    };
80
81    // pointer to our class (required by singleton)
82    static MouseAPI* singletonPtr_s;
83
84    // lists with all our Elements that can be clicked / scrolled on
85    std::list<clickableElement> clickEvents;
86    std::list<scrollElement> scrollEvents;
87
88    // pointer to the game-camera
89    Ogre::Camera *cam ;
90
91    //IntVector2 mousePos;
92
93    // pointer to our input-state
94    InputState* state;
95
96    // true => MouseAPI has been activated, false => MouseAPI has not been activated
97    bool active = false;
98
99
100
101public:
102
103    MouseAPI();
104    ~MouseAPI();
105
106    virtual void tick(float dt) override;
107
108    /* everytime a mousebutton is pressed, this function is called and checks if the cursor is over an element that can be clicked on
109     * if yes, the function associated with this element will be called with the corresponding button as argument
110     */
111    virtual void buttonPressed (MouseButtonCode::ByEnum button) override;
112
113    // not used
114    virtual void buttonReleased(MouseButtonCode::ByEnum button)  override{}
115
116    // not used
117    virtual void buttonHeld    (MouseButtonCode::ByEnum button) override{}
118
119    // not used
120    virtual void mouseMoved    (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) override;
121
122    /* 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
123     * if yes, the function associated with this element will be called
124     * if there is an element without position-requirement and an element the cursor is over, both their functions will be called
125     */
126    virtual void mouseScrolled (int abs, int rel) override;
127
128    /* add a clickableElement to the list
129     * see mouseapiexample for an example-implementation
130     * Arguments:
131     *      position: the point that needs to be clicked
132     *      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)
133     *      buttons: the function will only be called, if one of these buttons is pressed
134     *      onClickedFunction: the function that will be called
135     */
136    ClickableElementID addClickableElement(const Vector3& position,float radius,const std::list<MouseButtonCode::ByEnum>& buttons,std::function<void(MouseButtonCode::ByEnum button)>  onClickedFunction);
137
138    /* add a scrollElement to the list
139     * see mouseapiexample for an example-implementation
140     * Arguments:
141     *      position: the point the cursor needs to be over
142     *      radius: radius of the sphere around the position; if the cursor is inside this radius, the function will be executed
143     *      onScrolledFunction: the function that will be called
144     */
145    ScrollableElementID addScrollElement(const Vector3& position,float radius,std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction);
146
147    /* add a scrollElement to the list
148     * Arguments:
149     *      onScrolledFunction: the function that will be called, no matter where the cursor is
150     */
151    ScrollableElementID addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction);
152
153    /* change the position of a clickableElement
154     * Arguments:
155     *      id: the ClickableElementID of the element
156     *      position: the new position of the element
157     * Return:
158     *      true if successfull
159     *      false if not successfull
160     */
161    bool changePositionOfClickableElement(ClickableElementID id,const Vector3& position);
162
163    /* change the position of a scrollElement
164     * Arguments:
165     *      id: the ScrollableElementID of the element
166     *      position: the new position of the element
167     * Return:
168     *      true if successfull
169     *      false if not successfull
170     */
171    bool changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position);
172
173    /* change the radius of a clickableElement
174     * Arguments:
175     *      id: the ClickableElementID of the element
176     *      radius: the new radius of the element
177     * Return:
178     *      true if successfull
179     *      false if not successfull
180     */
181    bool changeRadiusOfClickableElement(ClickableElementID id,float radius);
182
183    /* change the radius of a scrollElement
184     * Arguments:
185     *      id: the ScrollableElementID of the element
186     *      radius: the new radius of the element
187     * Return:
188     *      true if successfull
189     *      false if not successfull
190     */
191    bool changeRadiusOfScrollableElement(ScrollableElementID id,float radius);
192
193    /* remove a clickableElement
194     * Arguments:
195     *      id: the ClickableElementID of the element
196     * Return:
197     *      true if successfull
198     *      false if not successfull
199     */
200    bool deleteClickableElement(ClickableElementID id);
201
202    /* remove a scrollElement
203     * Arguments:
204     *      id: the ScrollableElementID of the element
205     * Return:
206     *      true if successfull
207     *      false if not successfull
208     */
209    bool deleteScrollableElement(ScrollableElementID id);
210
211    /* get the current radius of a clickableElement
212     * Arguments:
213     *      id: the ClickableElementID of the element
214     */
215    float getRadiusClick(ClickableElementID id);
216
217    /* get the current radius of a scrollElement
218     * Arguments:
219     *      id: the ScrollableElementID of the element
220     */
221    float getRadiusScroll(ScrollableElementID id);
222
223    /* get the current relative Position of the cursor
224     * returns a value between 0 and 1 for both x and y component
225     * (0,0) top left corner, (1,1) bottom right corner
226     */
227    Vector2 getMousePosition();
228
229    /* activate the MouseAPI
230     * has to be called after the level has been created (i.e. inside the xml-port
231     * can be called multiple times, since the function checks the status of MouseAPI and does nothing if it already is active
232     */
233    void activate();
234
235    // returns true if MouseAPI is active, false otherwise
236    static bool isActive(){return singletonPtr_s != nullptr && getInstance().active;}
237
238    /* deactivate the MouseAPI
239     * has to be called, when the level gets closed (i.e. inside the level-destructor)
240     * the function does nothing if MouseAPI is not active
241     */
242    void deactivate();
243};
244}
245#endif // MOUSEAPI_H
Note: See TracBrowser for help on using the repository browser.