Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 12271 for code/branches


Ignore:
Timestamp:
Apr 4, 2019, 3:28:50 PM (6 years ago)
Author:
tkuonen
Message:

Begin extending MouseAPI

Location:
code/branches/MouseAPI_FS19
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • code/branches/MouseAPI_FS19/data/levels/MouseAPIExample.oxw

    r12263 r12271  
    1212?>
    1313
    14 <?lua
    15   include("templates/spaceshipEscort.oxt")
    16 ?>
     14
    1715
    1816<Level >
     
    2523    skybox       = "Orxonox/Starbox"
    2624  >
    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 
    3028
    3129    <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"/>
    3331   
    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>
    3537            <attached>
    3638                <Model position="0,0,0" mesh="cube.mesh" scale3D="10,10,10" />
     
    4042            </collisionShapes>
    4143       
    42         </MouseAPIExample>
     44</MouseAPIExample>
     45
     46
    4347   
    4448  </Scene>
  • code/branches/MouseAPI_FS19/src/modules/MouseAPI/mouseapi.cc

    r12263 r12271  
    11#include "mouseapi.h"
     2#include "core/singleton/ScopedSingletonIncludes.h"
     3namespace orxonox{
    24
    3 namespace orxonox{
     5ManageScopedSingleton(MouseAPI, ScopeID::GRAPHICS, false);
    46
    57MouseAPI::MouseAPI()
     
    1012void MouseAPI::activate()
    1113{
     14    active = true;
    1215     if(InputManager::exists())
    1316     {
     
    2326void MouseAPI::deactivate()
    2427{
     28    active = false;
    2529    if(InputManager::exists())
    2630    {
     
    2933        InputManager::getInstance().destroyState("MouseAPI");
    3034    }
     35    clickEvents.clear();
     36    scrollEvents.clear();
    3137}
    3238
     
    3844void MouseAPI::buttonPressed(MouseButtonCode::ByEnum button)
    3945{
    40     cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera();
     46    cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera();//todo: trycatch
    4147    Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport();
    4248    Ogre::Ray ray = cam->getCameraToViewportRay(mousePos.x/((float)vp->getActualWidth()),mousePos.y/((float)vp->getActualHeight()));
     
    5763void MouseAPI::mouseScrolled(int abs, int rel)
    5864{
     65    cam = CameraManager::getInstance().getActiveCamera()->getOgreCamera();
    5966    Ogre::Viewport *vp = GraphicsManager::getInstance().getViewport();
    6067    Ogre::Ray ray = cam->getCameraToViewportRay(mousePos.x/((float)vp->getActualWidth()),mousePos.y/((float)vp->getActualHeight()));
    6168    for(auto event:scrollEvents){
    6269        if(!event.considerPosition || ray.intersects(Ogre::Sphere(event.position,event.radius)).first)
    63             (*(event.onScrolledFunction))(abs,rel,mousePos);
     70            event.onScrolledFunction(abs,rel,mousePos);
    6471    }
    6572}
    6673
    67 void MouseAPI::addClickableObject(const Vector3& position, float radius, const std::list<MouseButtonCode::ByEnum>& buttons, std::function<void(MouseButtonCode::ByEnum)> onClickedFunction)
     74ClickableObjectID MouseAPI::addClickableObject(const Vector3& position, float radius, const std::list<MouseButtonCode::ByEnum>& buttons, std::function<void(MouseButtonCode::ByEnum)> onClickedFunction)
    6875{
    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;
    7078}
    71 void MouseAPI::addScrollElement(const Vector3& position,float radius,void (*onScrolledFunction)(int abs,int rel,const IntVector2&  mousePos))
     79ScrollableElementID MouseAPI::addScrollElement(const Vector3& position, float radius, std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction)
    7280{
    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;
    7483}
    75 void MouseAPI::addScrollElement(void (*onScrolledFunction)(int abs,int rel,const IntVector2&  mousePos))
     84ScrollableElementID MouseAPI::addScrollElement(std::function<void(int abs,int rel,const IntVector2& mousePos)> onScrolledFunction)
    7685{
    77     scrollEvents.insert(scrollEvents.begin(),{onScrolledFunction});
     86    scrollEvents.insert(scrollEvents.begin(),{!scrollEvents.empty() ? scrollEvents.back().id + 1:0,onScrolledFunction});
     87    return scrollEvents.back().id;
    7888}
    7989
    80 void MouseAPI::changeCamera(Camera& camera)
    81 {
    82     cam = camera.getOgreCamera();
    83 }
     90//todo
     91void MouseAPI::changePositionOfClickableObject(ClickableObjectID id,const Vector3& position){}
     92void MouseAPI::changePositionOfScrollableElement(ScrollableElementID id,const Vector3& position){}
     93void MouseAPI::changeRadiusOfClickableObject(ClickableObjectID id,float radius){}
     94void MouseAPI::changeRadiusOfScrollableElement(ScrollableElementID id,float radius){}
     95void MouseAPI::deleteClickableObject(ClickableObjectID){}
     96void MouseAPI::deleteScrollableElement(ScrollableElementID){}
    8497
    8598}
  • code/branches/MouseAPI_FS19/src/modules/MouseAPI/mouseapi.h

    r12253 r12271  
    2020{
    2121
    22 class MouseAPI : public InputHandler
     22typedef uint ClickableObjectID;
     23typedef uint ScrollableElementID;
     24
     25class MouseAPI : public InputHandler, public Singleton<MouseAPI>
    2326{
    24 
     27friend class Singleton<MouseAPI>;
    2528private:
    2629
    2730    struct clickableElement
    2831    {
     32        //static ClickableObjectID lastClID;
     33        ClickableObjectID id;
    2934        Vector3 position;
    3035        float radius;
    3136        std::list<MouseButtonCode::ByEnum> buttons;
    3237        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){}
    3540    };
    3641
    3742    struct scrollElement
    3843    {
     44        static ScrollableElementID lastScID;
     45        ScrollableElementID id;
    3946        bool considerPosition;
    4047        Vector3 position;
    4148        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){}
    4754    };
    4855
    49 
     56    static MouseAPI* singletonPtr_s;
    5057    std::list<clickableElement> clickEvents;
    5158    std::list<scrollElement> scrollEvents;
     
    5360    IntVector2 mousePos;
    5461    InputState* state;
     62    bool active = false;
     63
    5564
    5665
     
    6574    virtual void mouseScrolled (int abs, int rel) override;
    6675
    67     void addClickableObject(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);
    7079
    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);
    7286
    7387    void activate();
     88    static bool isActive(){return singletonPtr_s != nullptr && getInstance().active;}
    7489    void deactivate();
    7590};
  • code/branches/MouseAPI_FS19/src/modules/MouseAPI/mouseapiexample.cc

    r12255 r12271  
    66RegisterClass(MouseAPIExample);
    77
    8 MouseAPIExample::MouseAPIExample(Context* context) : StaticEntity(context)
     8MouseAPIExample::MouseAPIExample(Context* context) : ControllableEntity(context)
    99{
    1010    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();
    1312
    1413}
     
    1615MouseAPIExample::~MouseAPIExample()
    1716{
    18     mouseapi.deactivate();
     17    if(MouseAPI::isActive())
     18        MouseAPI::getInstance().deactivate();
     19}
     20
     21void MouseAPIExample::tick(float dt)
     22{
     23    ControllableEntity::tick(dt);
     24
    1925}
    2026
     
    2733{
    2834    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
    2938}
    3039}
  • code/branches/MouseAPI_FS19/src/modules/MouseAPI/mouseapiexample.h

    r12255 r12271  
    77#include "util/output/OutputManager.h"
    88#include "util/output/ConsoleWriter.h"
    9 #include "worldentities/StaticEntity.h"
     9#include "worldentities/ControllableEntity.h"
    1010#include "core/CoreIncludes.h"
    1111
    1212namespace orxonox
    1313{
    14 class MouseAPIExample : public StaticEntity
     14class MouseAPIExample : public ControllableEntity
    1515{
    1616public:
     
    1818    ~MouseAPIExample();
    1919    virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
    20     MouseAPI mouseapi;
    2120    void testfunction(MouseButtonCode::ByEnum mouse);
     21    virtual void tick(float dt) override;
    2222};
    2323}
Note: See TracChangeset for help on using the changeset viewer.