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 | //scale the sphere with this factor and change the radius |
---|
38 | this->setScale(factor); |
---|
39 | MouseAPI::getInstance().changeRadiusOfScrollableElement(sphereid,factor*10); |
---|
40 | } |
---|
41 | |
---|
42 | // standard XML-Port |
---|
43 | void MouseAPIExample::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
44 | { |
---|
45 | SUPER(MouseAPIExample, XMLPort, xmlelement, mode); |
---|
46 | |
---|
47 | // differentiate between several objects by an identifier "id" |
---|
48 | XMLPortParam(MouseAPIExample, "id", setId, getId, xmlelement, mode); |
---|
49 | if(this->getId() == 1) // id == 1; cube |
---|
50 | { |
---|
51 | // 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 |
---|
52 | cubeid = MouseAPI::getInstance().addClickableObject(this->getWorldPosition(),10,std::list<MouseButtonCode::ByEnum>{MouseButtonCode::Left},[this](MouseButtonCode::ByEnum mouse){this->changesizeonclick(mouse);}); |
---|
53 | } |
---|
54 | else if(this->getId() == 2) // id == 2; sphere |
---|
55 | { |
---|
56 | // add the sphere to the list with scrollable Objects, set the radius to 10, define the function changesizeonscroll to be called while scrolling |
---|
57 | sphereid = MouseAPI::getInstance().addScrollElement(this->getWorldPosition(), 10, [this](int abs, int rel, const IntVector2& mousePos){this->changesizeonscroll(abs,rel,mousePos);}); |
---|
58 | } |
---|
59 | |
---|
60 | // activate MouseAPI |
---|
61 | MouseAPI::getInstance().activate(); |
---|
62 | } |
---|
63 | } |
---|