[12255] | 1 | #include "mouseapiexample.h" |
---|
| 2 | |
---|
| 3 | namespace orxonox |
---|
| 4 | { |
---|
| 5 | |
---|
| 6 | RegisterClass(MouseAPIExample); |
---|
| 7 | |
---|
[12271] | 8 | MouseAPIExample::MouseAPIExample(Context* context) : ControllableEntity(context) |
---|
[12255] | 9 | { |
---|
| 10 | RegisterObject(MouseAPIExample); |
---|
| 11 | } |
---|
| 12 | |
---|
| 13 | MouseAPIExample::~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] | 20 | void 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 |
---|
| 31 | void 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); |
---|
| 37 | //scale the sphere with this factor and change the radius |
---|
| 38 | this->setScale(factor); |
---|
| 39 | MouseAPI::getInstance().changeRadiusOfScrollableElement(sphereid,factor*10); |
---|
[12271] | 40 | } |
---|
| 41 | |
---|
[12306] | 42 | // standard XML-Port |
---|
[12255] | 43 | void MouseAPIExample::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 44 | { |
---|
| 45 | SUPER(MouseAPIExample, XMLPort, xmlelement, mode); |
---|
[12306] | 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 |
---|
[12271] | 61 | MouseAPI::getInstance().activate(); |
---|
[12255] | 62 | } |
---|
| 63 | } |
---|