Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

some comments added

File size: 6.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
21/* This class implements a basic mouse-api
22 * supported are mouse-clicks (left, right, mousewheel, ...) and scrolling
23 *
24 * mouse-clicks always are asscociated with an ingame element that has a position and a sphere with a certain radius around it
25 * if the cursor is inside this sphere and a button is pressed, a user-defined function will be called
26 *
27 * scrolling can either be global (independent of where the cursor is) or local (same as a mouse-click)
28 * in both cases a user-defined function will be called
29 *
30 * in short the class works by storing every element that can be clicked / scrolled on in a list
31 * everytime a button is clicked or the mousewheel is turned, the list gets traversed and every element checked wheter it is clicked / scrolled on
32 * checking happens by casting a ray from the camera through the mouse-cursor and testing wheter it intersects the sphere of the element
33 */
34
35namespace orxonox
36{
37typedef uint ClickableObjectID;
38typedef uint ScrollableElementID;
39
40class MouseAPI : public InputHandler, public Singleton<MouseAPI>
41{
42friend class Singleton<MouseAPI>;
43private:
44
45    // Elements that can be clicked on are stored as clickableElement
46    struct clickableElement
47    {
48        ClickableObjectID id;
49        Vector3 position;
50        float radius;
51        std::list<MouseButtonCode::ByEnum> buttons;
52        std::function<void(MouseButtonCode::ByEnum button)> onClickedFunction;
53        clickableElement(ClickableObjectID id,const Vector3& position,float radius,const std::list<MouseButtonCode::ByEnum>& buttons,std::function<void(MouseButtonCode::ByEnum button)> onClickedFunction):id(id),position(position),
54            radius(radius), buttons(buttons), onClickedFunction(onClickedFunction){}
55    };
56
57    /* Elements that can be "scrolled on" are stored as scrollElement
58     * there are 2 diffrent types, hence the overloaded constructor:
59     *      1) the function is called whenever one scrolls, independet from position of object and cursor
60     *      2) the function is only called when the cursor is over the object (same as with a clickElement)
61     */
62    struct scrollElement
63    {
64        ScrollableElementID id;
65        bool considerPosition;
66        Vector3 position;
67        float radius;
68        std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction;
69        // constructor for scrollElement type 1
70        scrollElement(ScrollableElementID id,std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction):id(id),considerPosition(false),
71            onScrolledFunction(onScrolledFunction){}
72        // constructor fro scrollElement type 2
73        scrollElement(ScrollableElementID id,const Vector3& position, float radius, std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction):id(id),considerPosition(true),
74            position(position), radius(radius), onScrolledFunction(onScrolledFunction){}
75    };
76
77    // pointer to our class (required by singleton)
78    static MouseAPI* singletonPtr_s;
79
80    // lists with all our Elements that can be clicked / scrolled on
81    std::list<clickableElement> clickEvents;
82    std::list<scrollElement> scrollEvents;
83
84    // pointer to the game-camera
85    Ogre::Camera *cam ;
86
87    //IntVector2 mousePos;
88
89    // pointer to our input-state
90    InputState* state;
91
92    // true => MouseAPI has been activated, false => MouseAPI has not been activated
93    bool active = false;
94
95
96
97public:
98
99    MouseAPI();
100    ~MouseAPI();
101
102    /* everytime a mousebutton is pressed, this function is called and checks if the cursor is over an element that can be clicked on
103     * if yes, the function associated with this element will be called with the corresponding button as argument
104     */
105    virtual void buttonPressed (MouseButtonCode::ByEnum button) override;
106
107    // not used
108    virtual void buttonReleased(MouseButtonCode::ByEnum button)  override{}
109
110    // not used
111    virtual void buttonHeld    (MouseButtonCode::ByEnum button) override{}
112
113    // not used
114    virtual void mouseMoved    (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) override;
115
116    /* 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
117     * if yes, the function associated with this element will be called
118     * if there is an element without position-requirement and an element the cursor is over, both their functions will be called
119     */
120    virtual void mouseScrolled (int abs, int rel) override;
121
122    /* add a clickableElement to the list
123     * see mouseapiexample for an example-implementation
124     * Arguments:
125     *      position: the point that needs to be clicked
126     *      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)
127     *      buttons: the function will only be called, if one of these buttons is pressed
128     *      onClickedFunction: the function that will be called
129     *
130     */
131    ClickableObjectID addClickableObject(const Vector3& position,float radius,const std::list<MouseButtonCode::ByEnum>& buttons,std::function<void(MouseButtonCode::ByEnum button)>  onClickedFunction);
132
133    /*
134     *
135     */
136    ScrollableElementID addScrollElement(const Vector3& position,float radius,std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction);
137
138    /*
139     *
140     */
141    ScrollableElementID addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction);
142
143    //true: success; false: element not found
144    bool changePositionOfClickableObject(ClickableObjectID id,const Vector3& position);
145    bool changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position);
146    bool changeRadiusOfClickableObject(ClickableObjectID id,float radius);
147    bool changeRadiusOfScrollableElement(ScrollableElementID id,float radius);
148    bool deleteClickableObject(ClickableObjectID id);
149    bool deleteScrollableElement(ScrollableElementID id);
150
151    float getRadiusClick(ClickableObjectID id);
152    float getRadiusScroll(ScrollableElementID id);
153    Vector2 getMousePosition();
154
155    void activate();
156    static bool isActive(){return singletonPtr_s != nullptr && getInstance().active;}
157    void deactivate();
158};
159}
160#endif // MOUSEAPI_H
Note: See TracBrowser for help on using the repository browser.