Changeset 12271
- Timestamp:
- Apr 4, 2019, 3:28:50 PM (6 years ago)
- Location:
- code/branches/MouseAPI_FS19
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/MouseAPI_FS19/data/levels/MouseAPIExample.oxw
r12263 r12271 12 12 ?> 13 13 14 <?lua 15 include("templates/spaceshipEscort.oxt") 16 ?> 14 17 15 18 16 <Level > … … 25 23 skybox = "Orxonox/Starbox" 26 24 > 27 <camerapositions>28 <CameraPosition position="0,0,0" lookat="100,0,0" absolute=true drag=true mouselook=true/>29 </camerapositions>25 26 <CameraPosition position="0,0,0" lookat="100,0,0" absolute=true drag=false mouselook=false/> 27 30 28 31 29 <Light type=directional position="0,0,0" direction="0.253, 0.593, -0.765" diffuse="1.0, 0.9, 0.9, 1.0" specular="1.0, 0.9, 0.9, 1.0"/> 32 <SpawnPoint team=0 position="0,0,0" lookat=" 0,0,0"/>30 <SpawnPoint team=0 position="0,0,0" lookat="100,0,0"/> 33 31 34 <MouseAPIExample position="100,0,0" direction="0,-1,0" collisionType=static mass=100000 friction=0.01 > 32 33 <MouseAPIExample position="100,0,0" direction="0,-1,0" collisionType=dynamic mass=100000 friction=0.01 cameraOffset=100> 34 <camerapositions> 35 <CameraPosition position="0,0,0" absolute=true mouselook=false drag=false direction="1,0,0"/> 36 </camerapositions> 35 37 <attached> 36 38 <Model position="0,0,0" mesh="cube.mesh" scale3D="10,10,10" /> … … 40 42 </collisionShapes> 41 43 42 </MouseAPIExample> 44 </MouseAPIExample> 45 46 43 47 44 48 </Scene> -
code/branches/MouseAPI_FS19/src/modules/MouseAPI/mouseapi.cc
r12263 r12271 1 1 #include "mouseapi.h" 2 #include "core/singleton/ScopedSingletonIncludes.h" 3 namespace orxonox{ 2 4 3 namespace orxonox{ 5 ManageScopedSingleton(MouseAPI, ScopeID::GRAPHICS, false); 4 6 5 7 MouseAPI::MouseAPI() … … 10 12 void MouseAPI::activate() 11 13 { 14 active = true; 12 15 if(InputManager::exists()) 13 16 { … … 23 26 void MouseAPI::deactivate() 24 27 { 28 active = false; 25 29 if(InputManager::exists()) 26 30 { … … 29 33 InputManager::getInstance().destroyState("MouseAPI"); 30 34 } 35 clickEvents.clear(); 36 scrollEvents.clear(); 31 37 } 32 38 … … 38 44 void MouseAPI::buttonPressed(MouseButtonCode::ByEnum button) 39 45 { 40 cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); 46 cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera();//todo: trycatch 41 47 Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); 42 48 Ogre::Ray ray = cam->getCameraToViewportRay(mousePos.x/((float)vp->getActualWidth()),mousePos.y/((float)vp->getActualHeight())); … … 57 63 void MouseAPI::mouseScrolled(int abs, int rel) 58 64 { 65 cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera(); 59 66 Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport(); 60 67 Ogre::Ray ray = cam->getCameraToViewportRay(mousePos.x/((float)vp->getActualWidth()),mousePos.y/((float)vp->getActualHeight())); 61 68 for(auto event:scrollEvents){ 62 69 if(!event.considerPosition || ray.intersects(Ogre::Sphere(event.position,event.radius)).first) 63 (*(event.onScrolledFunction))(abs,rel,mousePos);70 event.onScrolledFunction(abs,rel,mousePos); 64 71 } 65 72 } 66 73 67 voidMouseAPI::addClickableObject(const Vector3& position, float radius, const std::list<MouseButtonCode::ByEnum>& buttons, std::function<void(MouseButtonCode::ByEnum)> onClickedFunction)74 ClickableObjectID MouseAPI::addClickableObject(const Vector3& position, float radius, const std::list<MouseButtonCode::ByEnum>& buttons, std::function<void(MouseButtonCode::ByEnum)> onClickedFunction) 68 75 { 69 clickEvents.insert(clickEvents.begin(),{position,radius,buttons,onClickedFunction}); 76 clickEvents.insert(clickEvents.begin(),{!clickEvents.empty() ? clickEvents.back().id + 1:0,position,radius,buttons,onClickedFunction}); 77 return clickEvents.back().id; 70 78 } 71 void MouseAPI::addScrollElement(const Vector3& position,float radius,void (*onScrolledFunction)(int abs,int rel,const IntVector2& mousePos))79 ScrollableElementID MouseAPI::addScrollElement(const Vector3& position, float radius, std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction) 72 80 { 73 scrollEvents.insert(scrollEvents.begin(),{position,radius,onScrolledFunction}); 81 scrollEvents.insert(scrollEvents.begin(),{!scrollEvents.empty() ? scrollEvents.back().id + 1:0,position,radius,onScrolledFunction}); 82 return scrollEvents.back().id; 74 83 } 75 void MouseAPI::addScrollElement(void (*onScrolledFunction)(int abs,int rel,const IntVector2& mousePos))84 ScrollableElementID MouseAPI::addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction) 76 85 { 77 scrollEvents.insert(scrollEvents.begin(),{onScrolledFunction}); 86 scrollEvents.insert(scrollEvents.begin(),{!scrollEvents.empty() ? scrollEvents.back().id + 1:0,onScrolledFunction}); 87 return scrollEvents.back().id; 78 88 } 79 89 80 void MouseAPI::changeCamera(Camera& camera) 81 { 82 cam = camera.getOgreCamera(); 83 } 90 //todo 91 void MouseAPI::changePositionOfClickableObject(ClickableObjectID id,const Vector3& position){} 92 void MouseAPI::changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position){} 93 void MouseAPI::changeRadiusOfClickableObject(ClickableObjectID id,float radius){} 94 void MouseAPI::changeRadiusOfScrollableElement(ScrollableElementID id,float radius){} 95 void MouseAPI::deleteClickableObject(ClickableObjectID){} 96 void MouseAPI::deleteScrollableElement(ScrollableElementID){} 84 97 85 98 } -
code/branches/MouseAPI_FS19/src/modules/MouseAPI/mouseapi.h
r12253 r12271 20 20 { 21 21 22 class MouseAPI : public InputHandler 22 typedef uint ClickableObjectID; 23 typedef uint ScrollableElementID; 24 25 class MouseAPI : public InputHandler, public Singleton<MouseAPI> 23 26 { 24 27 friend class Singleton<MouseAPI>; 25 28 private: 26 29 27 30 struct clickableElement 28 31 { 32 //static ClickableObjectID lastClID; 33 ClickableObjectID id; 29 34 Vector3 position; 30 35 float radius; 31 36 std::list<MouseButtonCode::ByEnum> buttons; 32 37 std::function<void(MouseButtonCode::ByEnum button)> onClickedFunction; 33 clickableElement( const Vector3& position,float radius,const std::list<MouseButtonCode::ByEnum>& buttons,std::function<void(MouseButtonCode::ByEnum button)> onClickedFunction):position(position),34 radius(radius), buttons(buttons), onClickedFunction(onClickedFunction) {}38 clickableElement(ClickableObjectID id,const Vector3& position,float radius,const std::list<MouseButtonCode::ByEnum>& buttons,std::function<void(MouseButtonCode::ByEnum button)> onClickedFunction):position(position), 39 radius(radius), buttons(buttons), onClickedFunction(onClickedFunction),id(id){} 35 40 }; 36 41 37 42 struct scrollElement 38 43 { 44 static ScrollableElementID lastScID; 45 ScrollableElementID id; 39 46 bool considerPosition; 40 47 Vector3 position; 41 48 float radius; 42 void (*onScrolledFunction)(int abs,int rel,const IntVector2& mousePos);43 scrollElement( void (*onScrolledFunction)(int abs,int rel,const IntVector2& mousePos)):considerPosition(false),44 onScrolledFunction(onScrolledFunction) {}45 scrollElement( const Vector3& position, float radius, void (*onScrolledFunction)(int abs,int rel,const IntVector2& mousePos)):considerPosition(true),46 position(position), radius(radius), onScrolledFunction(onScrolledFunction) {}49 std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction; 50 scrollElement(ScrollableElementID id,std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction):considerPosition(false), 51 onScrolledFunction(onScrolledFunction),id(id){} 52 scrollElement(ScrollableElementID id,const Vector3& position, float radius, std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction):considerPosition(true), 53 position(position), radius(radius), onScrolledFunction(onScrolledFunction),id(id){} 47 54 }; 48 55 49 56 static MouseAPI* singletonPtr_s; 50 57 std::list<clickableElement> clickEvents; 51 58 std::list<scrollElement> scrollEvents; … … 53 60 IntVector2 mousePos; 54 61 InputState* state; 62 bool active = false; 63 55 64 56 65 … … 65 74 virtual void mouseScrolled (int abs, int rel) override; 66 75 67 voidaddClickableObject(const Vector3& position,float radius,const std::list<MouseButtonCode::ByEnum>& buttons,std::function<void(MouseButtonCode::ByEnum button)> onClickedFunction);68 void addScrollElement(const Vector3& position,float radius,void (*onScrolledFunction)(int abs,int rel,const IntVector2& mousePos));69 void addScrollElement(void (*onScrolledFunction)(int abs,int rel,const IntVector2& mousePos));76 ClickableObjectID addClickableObject(const Vector3& position,float radius,const std::list<MouseButtonCode::ByEnum>& buttons,std::function<void(MouseButtonCode::ByEnum button)> onClickedFunction); 77 ScrollableElementID addScrollElement(const Vector3& position,float radius,std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction); 78 ScrollableElementID addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction); 70 79 71 void changeCamera(Camera& camera); 80 void changePositionOfClickableObject(ClickableObjectID id,const Vector3& position); 81 void changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position); 82 void changeRadiusOfClickableObject(ClickableObjectID id,float radius); 83 void changeRadiusOfScrollableElement(ScrollableElementID id,float radius); 84 void deleteClickableObject(ClickableObjectID); 85 void deleteScrollableElement(ScrollableElementID); 72 86 73 87 void activate(); 88 static bool isActive(){return singletonPtr_s != nullptr && getInstance().active;} 74 89 void deactivate(); 75 90 }; -
code/branches/MouseAPI_FS19/src/modules/MouseAPI/mouseapiexample.cc
r12255 r12271 6 6 RegisterClass(MouseAPIExample); 7 7 8 MouseAPIExample::MouseAPIExample(Context* context) : StaticEntity(context)8 MouseAPIExample::MouseAPIExample(Context* context) : ControllableEntity(context) 9 9 { 10 10 RegisterObject(MouseAPIExample); 11 mouseapi.activate(); 12 mouseapi.addClickableObject(getPosition(),10,std::list<MouseButtonCode::ByEnum>{MouseButtonCode::Left},[this](MouseButtonCode::ByEnum mouse){this->testfunction(mouse);}); 11 //mouseapi = MouseAPI::getInstance(); 13 12 14 13 } … … 16 15 MouseAPIExample::~MouseAPIExample() 17 16 { 18 mouseapi.deactivate(); 17 if(MouseAPI::isActive()) 18 MouseAPI::getInstance().deactivate(); 19 } 20 21 void MouseAPIExample::tick(float dt) 22 { 23 ControllableEntity::tick(dt); 24 19 25 } 20 26 … … 27 33 { 28 34 SUPER(MouseAPIExample, XMLPort, xmlelement, mode); 35 MouseAPI::getInstance().activate(); 36 MouseAPI::getInstance().addClickableObject(this->getWorldPosition(),10,std::list<MouseButtonCode::ByEnum>{MouseButtonCode::Left},[this](MouseButtonCode::ByEnum mouse){this->testfunction(mouse);}); 37 29 38 } 30 39 } -
code/branches/MouseAPI_FS19/src/modules/MouseAPI/mouseapiexample.h
r12255 r12271 7 7 #include "util/output/OutputManager.h" 8 8 #include "util/output/ConsoleWriter.h" 9 #include "worldentities/ StaticEntity.h"9 #include "worldentities/ControllableEntity.h" 10 10 #include "core/CoreIncludes.h" 11 11 12 12 namespace orxonox 13 13 { 14 class MouseAPIExample : public StaticEntity14 class MouseAPIExample : public ControllableEntity 15 15 { 16 16 public: … … 18 18 ~MouseAPIExample(); 19 19 virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); 20 MouseAPI mouseapi;21 20 void testfunction(MouseButtonCode::ByEnum mouse); 21 virtual void tick(float dt) override; 22 22 }; 23 23 }
Note: See TracChangeset
for help on using the changeset viewer.