1 | #include "mouseapiexample.h" |
---|
2 | |
---|
3 | namespace orxonox |
---|
4 | { |
---|
5 | |
---|
6 | RegisterClass(MouseAPIExample); |
---|
7 | |
---|
8 | MouseAPIExample::MouseAPIExample(Context* context) : ControllableEntity(context) |
---|
9 | { |
---|
10 | RegisterObject(MouseAPIExample); |
---|
11 | } |
---|
12 | |
---|
13 | MouseAPIExample::~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 |
---|
20 | void 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 clickableObject to the new size |
---|
27 | MouseAPI::getInstance().changeRadiusOfClickableObject(cubeid,randomnumber*10); |
---|
28 | } |
---|
29 | |
---|
30 | // change the size of the sphere by scrolling on it |
---|
31 | void 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 | // |
---|
45 | void MouseAPIExample::clickleft(MouseButtonCode::ByEnum mouse) |
---|
46 | { |
---|
47 | //MouseAPI::getInstance().changeRadiusOfClickableObject(cubeid,10); |
---|
48 | orxout() << "left" << "\n"; |
---|
49 | } |
---|
50 | |
---|
51 | // |
---|
52 | void MouseAPIExample::clickright(MouseButtonCode::ByEnum mouse) |
---|
53 | { |
---|
54 | //MouseAPI::getInstance().changeRadiusOfClickableObject(cubeid,10); |
---|
55 | orxout() << "right" << "\n"; |
---|
56 | } |
---|
57 | |
---|
58 | // standard XML-Port |
---|
59 | void MouseAPIExample::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
60 | { |
---|
61 | SUPER(MouseAPIExample, XMLPort, xmlelement, mode); |
---|
62 | |
---|
63 | // differentiate between several objects by an identifier "id" |
---|
64 | XMLPortParam(MouseAPIExample, "id", setId, getId, xmlelement, mode); |
---|
65 | if(this->getId() == 1) // id == 1; cube |
---|
66 | { |
---|
67 | // 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 |
---|
68 | cubeid = MouseAPI::getInstance().addClickableObject(this->getWorldPosition(),10,std::list<MouseButtonCode::ByEnum>{MouseButtonCode::Left},[this](MouseButtonCode::ByEnum mouse){this->changesizeonclick(mouse);}); |
---|
69 | } |
---|
70 | else if(this->getId() == 2) // id == 2; sphere |
---|
71 | { |
---|
72 | // add the sphere to the list with scrollable Objects, set the radius to 10, define the function changesizeonscroll to be called while scrolling |
---|
73 | sphereid = MouseAPI::getInstance().addScrollElement(this->getWorldPosition(), 10, [this](int abs, int rel, const IntVector2& mousePos){this->changesizeonscroll(abs,rel,mousePos);}); |
---|
74 | } |
---|
75 | else if(this->getId() == 3) // id == 3; long block |
---|
76 | { |
---|
77 | // add the left and right outermost part of the long block to the list with clickable Objects and define clickleft/clickright to be called |
---|
78 | leftid = MouseAPI::getInstance().addClickableObject(this->getWorldPosition(),10,std::list<MouseButtonCode::ByEnum>{MouseButtonCode::Left,MouseButtonCode::Right},[this](MouseButtonCode::ByEnum mouse){this->clickleft(mouse);}); |
---|
79 | rightid = MouseAPI::getInstance().addClickableObject(this->getWorldPosition(),10,std::list<MouseButtonCode::ByEnum>{MouseButtonCode::Left,MouseButtonCode::Right},[this](MouseButtonCode::ByEnum mouse){this->clickright(mouse);}); |
---|
80 | } |
---|
81 | |
---|
82 | // activate MouseAPI |
---|
83 | MouseAPI::getInstance().activate(); |
---|
84 | } |
---|
85 | } |
---|