Changeset 12333 for code/branches/MouseAPI_FS19/src/modules/MouseAPI
- Timestamp:
- May 2, 2019, 4:02:17 PM (6 years ago)
- 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 19 19 #include "core/input/KeyBinderManager.h" 20 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 21 35 namespace orxonox 22 36 { 23 24 37 typedef uint ClickableObjectID; 25 38 typedef uint ScrollableElementID; … … 30 43 private: 31 44 45 // Elements that can be clicked on are stored as clickableElement 32 46 struct clickableElement 33 47 { … … 41 55 }; 42 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 */ 43 62 struct scrollElement 44 63 { … … 48 67 float radius; 49 68 std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction; 69 // constructor for scrollElement type 1 50 70 scrollElement(ScrollableElementID id,std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction):id(id),considerPosition(false), 51 71 onScrolledFunction(onScrolledFunction){} 72 // constructor fro scrollElement type 2 52 73 scrollElement(ScrollableElementID id,const Vector3& position, float radius, std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction):id(id),considerPosition(true), 53 74 position(position), radius(radius), onScrolledFunction(onScrolledFunction){} 54 75 }; 55 76 77 // pointer to our class (required by singleton) 56 78 static MouseAPI* singletonPtr_s; 79 80 // lists with all our Elements that can be clicked / scrolled on 57 81 std::list<clickableElement> clickEvents; 58 82 std::list<scrollElement> scrollEvents; 83 84 // pointer to the game-camera 59 85 Ogre::Camera *cam ; 86 60 87 //IntVector2 mousePos; 88 89 // pointer to our input-state 61 90 InputState* state; 91 92 // true => MouseAPI has been activated, false => MouseAPI has not been activated 62 93 bool active = false; 63 94 … … 68 99 MouseAPI(); 69 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 */ 70 105 virtual void buttonPressed (MouseButtonCode::ByEnum button) override; 106 107 // not used 71 108 virtual void buttonReleased(MouseButtonCode::ByEnum button) override{} 109 110 // not used 72 111 virtual void buttonHeld (MouseButtonCode::ByEnum button) override{} 112 113 // not used 73 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 */ 74 120 virtual void mouseScrolled (int abs, int rel) override; 75 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 */ 76 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 */ 77 136 ScrollableElementID addScrollElement(const Vector3& position,float radius,std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction); 137 138 /* 139 * 140 */ 78 141 ScrollableElementID addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction); 79 142 … … 88 151 float getRadiusClick(ClickableObjectID id); 89 152 float getRadiusScroll(ScrollableElementID id); 90 91 153 Vector2 getMousePosition(); 92 154 -
code/branches/MouseAPI_FS19/src/modules/MouseAPI/mouseapiexample.cc
r12330 r12333 96 96 { 97 97 // 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) 98 99 leftid = MouseAPI::getInstance().addClickableObject(Vector3(0,70,-70),20,std::list<MouseButtonCode::ByEnum>{MouseButtonCode::Left,MouseButtonCode::Right},[this](MouseButtonCode::ByEnum mouse){this->clickleft(mouse);}); 99 100 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.