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, 6 years ago

some comments added

File size: 6.7 KB
RevLine 
[12213]1#ifndef MOUSEAPI_H
2#define MOUSEAPI_H
3
[12247]4
5#include "OrxonoxPrereqs.h"
6#include "util/OgreForwardRefs.h"
7#include "graphics/Camera.h"
[12213]8#include <util/Math.h>
9#include <list>
10#include <core/input/InputHandler.h>
[12217]11#include <graphics/Camera.h>
12#include <core/GraphicsManager.h>
13#include <core/input/InputState.h>
[12247]14#include <OgreCamera.h>
15#include <OgreViewport.h>
[12253]16#include "CameraManager.h"
17#include <functional>
[12309]18#include "core/GUIManager.h"
19#include "core/input/KeyBinderManager.h"
[12213]20
[12333]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
[12213]35namespace orxonox
36{
[12271]37typedef uint ClickableObjectID;
38typedef uint ScrollableElementID;
39
40class MouseAPI : public InputHandler, public Singleton<MouseAPI>
[12213]41{
[12271]42friend class Singleton<MouseAPI>;
[12213]43private:
44
[12333]45    // Elements that can be clicked on are stored as clickableElement
[12213]46    struct clickableElement
47    {
[12271]48        ClickableObjectID id;
[12213]49        Vector3 position;
50        float radius;
[12247]51        std::list<MouseButtonCode::ByEnum> buttons;
[12253]52        std::function<void(MouseButtonCode::ByEnum button)> onClickedFunction;
[12275]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){}
[12213]55    };
56
[12333]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     */
[12213]62    struct scrollElement
63    {
[12271]64        ScrollableElementID id;
[12213]65        bool considerPosition;
66        Vector3 position;
67        float radius;
[12271]68        std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction;
[12333]69        // constructor for scrollElement type 1
[12275]70        scrollElement(ScrollableElementID id,std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction):id(id),considerPosition(false),
71            onScrolledFunction(onScrolledFunction){}
[12333]72        // constructor fro scrollElement type 2
[12275]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){}
[12213]75    };
76
[12333]77    // pointer to our class (required by singleton)
[12271]78    static MouseAPI* singletonPtr_s;
[12333]79
80    // lists with all our Elements that can be clicked / scrolled on
[12213]81    std::list<clickableElement> clickEvents;
82    std::list<scrollElement> scrollEvents;
[12333]83
84    // pointer to the game-camera
[12247]85    Ogre::Camera *cam ;
[12333]86
[12287]87    //IntVector2 mousePos;
[12333]88
89    // pointer to our input-state
[12217]90    InputState* state;
[12333]91
92    // true => MouseAPI has been activated, false => MouseAPI has not been activated
[12271]93    bool active = false;
[12213]94
95
[12271]96
[12213]97public:
98
[12253]99    MouseAPI();
[12213]100    ~MouseAPI();
[12333]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     */
[12217]105    virtual void buttonPressed (MouseButtonCode::ByEnum button) override;
[12333]106
107    // not used
[12217]108    virtual void buttonReleased(MouseButtonCode::ByEnum button)  override{}
[12333]109
110    // not used
[12217]111    virtual void buttonHeld    (MouseButtonCode::ByEnum button) override{}
[12333]112
113    // not used
[12217]114    virtual void mouseMoved    (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) override;
[12333]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     */
[12217]120    virtual void mouseScrolled (int abs, int rel) override;
[12213]121
[12333]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     */
[12271]131    ClickableObjectID addClickableObject(const Vector3& position,float radius,const std::list<MouseButtonCode::ByEnum>& buttons,std::function<void(MouseButtonCode::ByEnum button)>  onClickedFunction);
[12333]132
133    /*
134     *
135     */
[12271]136    ScrollableElementID addScrollElement(const Vector3& position,float radius,std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction);
[12333]137
138    /*
139     *
140     */
[12271]141    ScrollableElementID addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction);
[12217]142
[12275]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);
[12253]150
[12302]151    float getRadiusClick(ClickableObjectID id);
152    float getRadiusScroll(ScrollableElementID id);
[12309]153    Vector2 getMousePosition();
154
[12253]155    void activate();
[12271]156    static bool isActive(){return singletonPtr_s != nullptr && getInstance().active;}
[12253]157    void deactivate();
[12213]158};
159}
160#endif // MOUSEAPI_H
Note: See TracBrowser for help on using the repository browser.