Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/MouseAPI_FS19/src/modules/MouseAPI/mouseapiexample.cc @ 12348

Last change on this file since 12348 was 12333, checked in by mkarpf, 6 years ago

some comments added

File size: 4.4 KB
RevLine 
[12255]1#include "mouseapiexample.h"
2
3namespace orxonox
4{
5
6RegisterClass(MouseAPIExample);
7
[12271]8MouseAPIExample::MouseAPIExample(Context* context) : ControllableEntity(context)
[12255]9{
10    RegisterObject(MouseAPIExample);
11}
12
13MouseAPIExample::~MouseAPIExample()
14{
[12271]15    if(MouseAPI::isActive())
16        MouseAPI::getInstance().deactivate();
[12255]17}
18
[12306]19// change the size of the cube by a random number between 0.5 and 5 by clicking on it
[12302]20void MouseAPIExample::changesizeonclick(MouseButtonCode::ByEnum mouse)
[12271]21{
[12306]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
[12285]25    this->setScale(randomnumber);
[12302]26    // change the radius of the clickableObject to the new size
[12306]27    MouseAPI::getInstance().changeRadiusOfClickableObject(cubeid,randomnumber*10);
[12302]28}
[12271]29
[12302]30// change the size of the sphere by scrolling on it
31void MouseAPIExample::changesizeonscroll(int abs,int rel,const IntVector2& mousePos)
32{
[12306]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);
[12311]37    // return if factor is outside of range [0.5,5] (limit size)
38    if(factor > 5 || factor < 0.5) return;
[12306]39    //scale the sphere with this factor and change the radius
40    this->setScale(factor);
41    MouseAPI::getInstance().changeRadiusOfScrollableElement(sphereid,factor*10);
[12271]42}
43
[12330]44// scale the z-component up / down with left-mouse / right-mouse (max. factor 2, min. factor 1)
[12311]45void MouseAPIExample::clickleft(MouseButtonCode::ByEnum mouse)
46{
[12330]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     }
[12311]59}
60
[12330]61// scale the y-component up / down with left-mouse / right-mouse (max. factor 2, min. factor 1)
[12311]62void MouseAPIExample::clickright(MouseButtonCode::ByEnum mouse)
63{
[12330]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    }
[12311]76}
77
[12306]78// standard XML-Port
[12255]79void MouseAPIExample::XMLPort(Element& xmlelement, XMLPort::Mode mode)
80{
81    SUPER(MouseAPIExample, XMLPort, xmlelement, mode);
[12306]82
83    // differentiate between several objects 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 Objects, set the radius to 10, define the function changesizeonclick to be called after a left-click
88        cubeid = MouseAPI::getInstance().addClickableObject(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 Objects, 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    }
[12311]95    else if(this->getId() == 3) // id == 3; long block
96    {
[12330]97        // add the left and right part of the long block to the list with clickable Objects and define clickleft/clickright to be called
[12333]98        // (Position (0,70,+/-70) is hardcoded)
[12330]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);});
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);});
[12311]101    }
[12306]102
103    // activate MouseAPI
[12271]104    MouseAPI::getInstance().activate();
[12255]105}
106}
Note: See TracBrowser for help on using the repository browser.