Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

separated MouseAPIExample from MouseAPI

finished comments in MouseAPI.h

File size: 9.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"
[12348]20#include "tools/interfaces/Tickable.h"
[12213]21
[12352]22/* this class implements a basic mouse-api
[12333]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
[12352]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
[12333]37 */
38
[12213]39namespace orxonox
40{
[12352]41typedef uint ClickableElementID;
[12271]42typedef uint ScrollableElementID;
43
[12348]44class MouseAPI : public InputHandler, public Singleton<MouseAPI>,public Tickable
[12213]45{
[12271]46friend class Singleton<MouseAPI>;
[12213]47private:
48
[12333]49    // Elements that can be clicked on are stored as clickableElement
[12213]50    struct clickableElement
51    {
[12352]52        ClickableElementID id;
[12213]53        Vector3 position;
54        float radius;
[12247]55        std::list<MouseButtonCode::ByEnum> buttons;
[12253]56        std::function<void(MouseButtonCode::ByEnum button)> onClickedFunction;
[12352]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),
[12275]58            radius(radius), buttons(buttons), onClickedFunction(onClickedFunction){}
[12213]59    };
60
[12333]61    /* Elements that can be "scrolled on" are stored as scrollElement
62     * there are 2 diffrent types, hence the overloaded constructor:
[12352]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)
[12333]65     */
[12213]66    struct scrollElement
67    {
[12271]68        ScrollableElementID id;
[12213]69        bool considerPosition;
70        Vector3 position;
71        float radius;
[12271]72        std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction;
[12333]73        // constructor for scrollElement type 1
[12275]74        scrollElement(ScrollableElementID id,std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction):id(id),considerPosition(false),
75            onScrolledFunction(onScrolledFunction){}
[12333]76        // constructor fro scrollElement type 2
[12275]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){}
[12213]79    };
80
[12333]81    // pointer to our class (required by singleton)
[12271]82    static MouseAPI* singletonPtr_s;
[12333]83
84    // lists with all our Elements that can be clicked / scrolled on
[12213]85    std::list<clickableElement> clickEvents;
86    std::list<scrollElement> scrollEvents;
[12333]87
88    // pointer to the game-camera
[12247]89    Ogre::Camera *cam ;
[12333]90
[12287]91    //IntVector2 mousePos;
[12333]92
93    // pointer to our input-state
[12217]94    InputState* state;
[12333]95
96    // true => MouseAPI has been activated, false => MouseAPI has not been activated
[12271]97    bool active = false;
[12213]98
99
[12271]100
[12213]101public:
102
[12253]103    MouseAPI();
[12213]104    ~MouseAPI();
[12333]105
[12348]106    virtual void tick(float dt) override;
107
[12333]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     */
[12217]111    virtual void buttonPressed (MouseButtonCode::ByEnum button) override;
[12333]112
113    // not used
[12217]114    virtual void buttonReleased(MouseButtonCode::ByEnum button)  override{}
[12333]115
116    // not used
[12217]117    virtual void buttonHeld    (MouseButtonCode::ByEnum button) override{}
[12333]118
119    // not used
[12217]120    virtual void mouseMoved    (IntVector2 abs, IntVector2 rel, IntVector2 clippingSize) override;
[12333]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     */
[12217]126    virtual void mouseScrolled (int abs, int rel) override;
[12213]127
[12333]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
[12352]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)
[12333]133     *      buttons: the function will only be called, if one of these buttons is pressed
134     *      onClickedFunction: the function that will be called
135     */
[12352]136    ClickableElementID addClickableElement(const Vector3& position,float radius,const std::list<MouseButtonCode::ByEnum>& buttons,std::function<void(MouseButtonCode::ByEnum button)>  onClickedFunction);
[12333]137
[12352]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
[12333]144     */
[12271]145    ScrollableElementID addScrollElement(const Vector3& position,float radius,std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction);
[12333]146
[12352]147    /* add a scrollElement to the list
148     * Arguments:
149     *      onScrolledFunction: the function that will be called, no matter where the cursor is
[12333]150     */
[12271]151    ScrollableElementID addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction);
[12217]152
[12352]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     */
[12275]171    bool changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position);
[12352]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     */
[12275]191    bool changeRadiusOfScrollableElement(ScrollableElementID id,float radius);
[12352]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     */
[12275]209    bool deleteScrollableElement(ScrollableElementID id);
[12253]210
[12352]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     */
[12302]221    float getRadiusScroll(ScrollableElementID id);
[12352]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     */
[12309]227    Vector2 getMousePosition();
228
[12352]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     */
[12253]233    void activate();
[12352]234
235    // returns true if MouseAPI is active, false otherwise
[12271]236    static bool isActive(){return singletonPtr_s != nullptr && getInstance().active;}
[12352]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     */
[12253]242    void deactivate();
[12213]243};
244}
245#endif // MOUSEAPI_H
Note: See TracBrowser for help on using the repository browser.