Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/MouseAPI_FS19/src/modules/MouseAPIExample/mouseapiexample.cc @ 12384

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

separated MouseAPIExample from MouseAPI

finished comments in MouseAPI.h

File size: 4.4 KB
Line 
1#include "mouseapiexample.h"
2
3namespace orxonox
4{
5
6RegisterClass(MouseAPIExample);
7
8MouseAPIExample::MouseAPIExample(Context* context) : ControllableEntity(context)
9{
10    RegisterObject(MouseAPIExample);
11}
12
13MouseAPIExample::~MouseAPIExample()
14{
15    if(MouseAPI::isActive())
16        MouseAPI::getInstance().deactivate();
17}
18
19// change the size of the cube by a random number between 0.5 and 5 by clicking on it
20void MouseAPIExample::changesizeonclick(MouseButtonCode::ByEnum mouse)
21{
22    // generate random number between 0.5 and 5
23    float randomnumber = (rand()%10+1)/2.0;
24    // scale of the cube with this random number
25    this->setScale(randomnumber);
26    // change the radius of the clickableElement to the new size
27    MouseAPI::getInstance().changeRadiusOfClickableElement(cubeid,randomnumber*10);
28}
29
30// change the size of the sphere by scrolling on it
31void MouseAPIExample::changesizeonscroll(int abs,int rel,const IntVector2& mousePos)
32{
33    // get current radius of the sphere
34    float curRadius = MouseAPI::getInstance().getRadiusScroll(sphereid);
35    // set factor to 120% or 80% of the current size, depending on increase or decrease
36    float factor = curRadius/10*(1+rel/600.0);
37    // return if factor is outside of range [0.5,5] (limit size)
38    if(factor > 5 || factor < 0.5) return;
39    //scale the sphere with this factor and change the radius
40    this->setScale(factor);
41    MouseAPI::getInstance().changeRadiusOfScrollableElement(sphereid,factor*10);
42}
43
44// scale the z-component up / down with left-mouse / right-mouse (max. factor 2, min. factor 1)
45void MouseAPIExample::clickleft(MouseButtonCode::ByEnum mouse)
46{
47     // action after left-click
48     if (mouse == MouseButtonCode::Left)
49     {
50         Vector3 scale = this->getScale3D();
51         if (scale.z <= 2) this->setScale3D(scale.x,scale.y,scale.z+0.1);
52     }
53     // action after right-click
54     else if (mouse == MouseButtonCode::Right)
55     {
56         Vector3 scale = this->getScale3D();
57         if (scale.z > 1) this->setScale3D(scale.x,scale.y,scale.z-0.1);
58     }
59}
60
61// scale the y-component up / down with left-mouse / right-mouse (max. factor 2, min. factor 1)
62void MouseAPIExample::clickright(MouseButtonCode::ByEnum mouse)
63{
64    // action after left-click
65    if (mouse == MouseButtonCode::Left)
66    {
67        Vector3 scale = this->getScale3D();
68        if (scale.y <= 2) this->setScale3D(scale.x,scale.y+0.1,scale.z);
69    }
70    // action after right-click
71    else if (mouse == MouseButtonCode::Right)
72    {
73        Vector3 scale = this->getScale3D();
74        if (scale.y > 1) this->setScale3D(scale.x,scale.y-0.1,scale.z);
75    }
76}
77
78// standard XML-Port
79void MouseAPIExample::XMLPort(Element& xmlelement, XMLPort::Mode mode)
80{
81    SUPER(MouseAPIExample, XMLPort, xmlelement, mode);
82
83    // differentiate between several elements by an identifier "id"
84    XMLPortParam(MouseAPIExample, "id", setId, getId, xmlelement, mode);
85    if(this->getId() == 1) // id == 1; cube
86    {
87        // add the cube to the list with clickable elements, set the radius to 10, define the function changesizeonclick to be called after a left-click
88        cubeid = MouseAPI::getInstance().addClickableElement(this->getWorldPosition(),10,std::list<MouseButtonCode::ByEnum>{MouseButtonCode::Left},[this](MouseButtonCode::ByEnum mouse){this->changesizeonclick(mouse);});
89    }
90    else if(this->getId() == 2) // id == 2; sphere
91    {
92        // add the sphere to the list with scrollable Elements, set the radius to 10, define the function changesizeonscroll to be called while scrolling
93        sphereid = MouseAPI::getInstance().addScrollElement(this->getWorldPosition(), 10, [this](int abs, int rel, const IntVector2& mousePos){this->changesizeonscroll(abs,rel,mousePos);});
94    }
95    else if(this->getId() == 3) // id == 3; long block
96    {
97        // add the left and right part of the long block to the list with clickable Elements and define clickleft/clickright to be called
98        // (Position (0,70,+/-70) is hardcoded)
99        leftid = MouseAPI::getInstance().addClickableElement(Vector3(0,70,-70),20,std::list<MouseButtonCode::ByEnum>{MouseButtonCode::Left,MouseButtonCode::Right},[this](MouseButtonCode::ByEnum mouse){this->clickleft(mouse);});
100        rightid = MouseAPI::getInstance().addClickableElement(Vector3(0,70,70),20,std::list<MouseButtonCode::ByEnum>{MouseButtonCode::Left,MouseButtonCode::Right},[this](MouseButtonCode::ByEnum mouse){this->clickright(mouse);});
101    }
102
103    // activate MouseAPI
104    MouseAPI::getInstance().activate();
105}
106}
Note: See TracBrowser for help on using the repository browser.