Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file was 12377, checked in by tkuonen, 6 years ago

Improve MouseAPI
Add Comments
Cleanup

File size: 10.6 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 * 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)
42 */
43
44namespace orxonox
45{
46typedef uint ClickableElementID;
47typedef uint ScrollableElementID;
48
49class MouseAPI : public InputHandler, public Singleton<MouseAPI>,public Tickable
50{
51friend class Singleton<MouseAPI>;
52
53private:
54
55    // Elements that can be clicked on are stored as clickableElement
56    struct clickableElement
57    {
58        ClickableElementID id;
59        Vector3 position;
60        float radius;
61        std::list<MouseButtonCode::ByEnum> buttons;
62        std::function<void(MouseButtonCode::ByEnum button)> onClickedFunction;
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),
64            radius(radius), buttons(buttons), onClickedFunction(onClickedFunction){}
65    };
66
67    /* Elements that can be "scrolled on" are stored as scrollElement
68     * there are 2 diffrent types, hence the overloaded constructor:
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)
71     */
72    struct scrollElement
73    {
74        ScrollableElementID id;
75        bool considerPosition;
76        Vector3 position;
77        float radius;
78        std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction;
79        // constructor for scrollElement type 1
80        scrollElement(ScrollableElementID id,std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction):id(id),considerPosition(false),
81            onScrolledFunction(onScrolledFunction){}
82        // constructor fro scrollElement type 2
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){}
85    };
86
87    // pointer to our class (required by singleton)
88    static MouseAPI* singletonPtr_s;
89
90    // lists with all our Elements that can be clicked / scrolled on
91    std::list<clickableElement> clickEvents;
92    std::list<scrollElement> scrollEvents;
93
94    // pointer to the game-camera
95    Ogre::Camera *cam ;
96
97    // pointer to our input-state
98    InputState* state;
99
100    // true => MouseAPI has been activated, false => MouseAPI has not been activated
101    bool active = false;
102
103    //true => allow ship-controller to get mouse input; Default: false; Gets reset after each transition from deactivated to activated
104    bool gameInputActivated = false;
105
106
107public:
108
109    MouseAPI();
110    ~MouseAPI();
111
112    virtual void tick(float dt) override;
113
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     */
117    virtual void buttonPressed (MouseButtonCode::ByEnum button) override;
118
119    // not used
120    virtual void buttonReleased(MouseButtonCode::ByEnum button)  override{}
121
122    // not used
123    virtual void buttonHeld    (MouseButtonCode::ByEnum button) override{}
124
125    // not used
126    virtual void mouseMoved    (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) override;
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     */
132    virtual void mouseScrolled (int abs, int rel) override;
133
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
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)
139     *      buttons: the function will only be called, if one of these buttons is pressed
140     *      onClickedFunction: the function that will be called
141     */
142    ClickableElementID addClickableElement(const Vector3& position,float radius,const std::list<MouseButtonCode::ByEnum>& buttons,std::function<void(MouseButtonCode::ByEnum button)>  onClickedFunction);
143
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
150     */
151    ScrollableElementID addScrollElement(const Vector3& position,float radius,std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction);
152
153    /* add a scrollElement to the list
154     * Arguments:
155     *      onScrolledFunction: the function that will be called, no matter where the cursor is
156     */
157    ScrollableElementID addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction);
158
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     */
177    bool changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position);
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     */
197    bool changeRadiusOfScrollableElement(ScrollableElementID id,float radius);
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     */
215    bool deleteScrollableElement(ScrollableElementID id);
216
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     */
227    float getRadiusScroll(ScrollableElementID id);
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     */
233    Vector2 getMousePosition();
234
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     */
239    void activate();
240
241    // returns true if MouseAPI is active, false otherwise
242    static bool isActive(){return singletonPtr_s != nullptr && getInstance().active;}
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     */
248    void deactivate();
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();
260};
261}
262#endif // MOUSEAPI_H
Note: See TracBrowser for help on using the repository browser.