Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 2, 2019, 4:02:17 PM (6 years ago)
Author:
mkarpf
Message:

some comments added

Location:
code/branches/MouseAPI_FS19/src/modules/MouseAPI
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/MouseAPI_FS19/src/modules/MouseAPI/mouseapi.h

    r12309 r12333  
    1919#include "core/input/KeyBinderManager.h"
    2020
     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
    2135namespace orxonox
    2236{
    23 
    2437typedef uint ClickableObjectID;
    2538typedef uint ScrollableElementID;
     
    3043private:
    3144
     45    // Elements that can be clicked on are stored as clickableElement
    3246    struct clickableElement
    3347    {
     
    4155    };
    4256
     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     */
    4362    struct scrollElement
    4463    {
     
    4867        float radius;
    4968        std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction;
     69        // constructor for scrollElement type 1
    5070        scrollElement(ScrollableElementID id,std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction):id(id),considerPosition(false),
    5171            onScrolledFunction(onScrolledFunction){}
     72        // constructor fro scrollElement type 2
    5273        scrollElement(ScrollableElementID id,const Vector3& position, float radius, std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction):id(id),considerPosition(true),
    5374            position(position), radius(radius), onScrolledFunction(onScrolledFunction){}
    5475    };
    5576
     77    // pointer to our class (required by singleton)
    5678    static MouseAPI* singletonPtr_s;
     79
     80    // lists with all our Elements that can be clicked / scrolled on
    5781    std::list<clickableElement> clickEvents;
    5882    std::list<scrollElement> scrollEvents;
     83
     84    // pointer to the game-camera
    5985    Ogre::Camera *cam ;
     86
    6087    //IntVector2 mousePos;
     88
     89    // pointer to our input-state
    6190    InputState* state;
     91
     92    // true => MouseAPI has been activated, false => MouseAPI has not been activated
    6293    bool active = false;
    6394
     
    6899    MouseAPI();
    69100    ~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     */
    70105    virtual void buttonPressed (MouseButtonCode::ByEnum button) override;
     106
     107    // not used
    71108    virtual void buttonReleased(MouseButtonCode::ByEnum button)  override{}
     109
     110    // not used
    72111    virtual void buttonHeld    (MouseButtonCode::ByEnum button) override{}
     112
     113    // not used
    73114    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     */
    74120    virtual void mouseScrolled (int abs, int rel) override;
    75121
     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     */
    76131    ClickableObjectID addClickableObject(const Vector3& position,float radius,const std::list<MouseButtonCode::ByEnum>& buttons,std::function<void(MouseButtonCode::ByEnum button)>  onClickedFunction);
     132
     133    /*
     134     *
     135     */
    77136    ScrollableElementID addScrollElement(const Vector3& position,float radius,std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction);
     137
     138    /*
     139     *
     140     */
    78141    ScrollableElementID addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction);
    79142
     
    88151    float getRadiusClick(ClickableObjectID id);
    89152    float getRadiusScroll(ScrollableElementID id);
    90 
    91153    Vector2 getMousePosition();
    92154
  • code/branches/MouseAPI_FS19/src/modules/MouseAPI/mouseapiexample.cc

    r12330 r12333  
    9696    {
    9797        // add the left and right part of the long block to the list with clickable Objects and define clickleft/clickright to be called
     98        // (Position (0,70,+/-70) is hardcoded)
    9899        leftid = MouseAPI::getInstance().addClickableObject(Vector3(0,70,-70),20,std::list<MouseButtonCode::ByEnum>{MouseButtonCode::Left,MouseButtonCode::Right},[this](MouseButtonCode::ByEnum mouse){this->clickleft(mouse);});
    99100        rightid = MouseAPI::getInstance().addClickableObject(Vector3(0,70,70),20,std::list<MouseButtonCode::ByEnum>{MouseButtonCode::Left,MouseButtonCode::Right},[this](MouseButtonCode::ByEnum mouse){this->clickright(mouse);});
Note: See TracChangeset for help on using the changeset viewer.