mirror of https://github.com/axmolengine/axmol.git
issue #2087: [dispatcher] New Dispatcher Test: Fixed priority.
This commit is contained in:
parent
16b13ce522
commit
b3cade3be0
|
@ -13,6 +13,7 @@ namespace {
|
|||
std::function<Layer*()> createFunctions[] =
|
||||
{
|
||||
CL(TouchableSpriteTest),
|
||||
CL(FixedPriorityChangedTest)
|
||||
};
|
||||
|
||||
unsigned int TEST_CASE_COUNT = sizeof(createFunctions) / sizeof(createFunctions[0]);
|
||||
|
@ -156,10 +157,6 @@ void TouchableSpriteTest::onEnter()
|
|||
|
||||
listener1->onTouchMoved = [](Touch* touch, Event* event){
|
||||
auto target = static_cast<Sprite*>(event->getCurrentTarget());
|
||||
|
||||
Point locationInNode = target->convertToNodeSpace(touch->getLocation());
|
||||
log("sprite tag: %d moved... x = %f, y = %f", target->getTag(), locationInNode.x, locationInNode.y);
|
||||
|
||||
target->setPosition(target->getPosition() + touch->getDelta());
|
||||
};
|
||||
|
||||
|
@ -191,3 +188,114 @@ std::string TouchableSpriteTest::subtitle()
|
|||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
// FixedPriorityChangedTest
|
||||
|
||||
class TouchableSpriteWithFixedPriority : public Sprite
|
||||
{
|
||||
public:
|
||||
TouchableSpriteWithFixedPriority()
|
||||
: _listener(nullptr)
|
||||
, _fixedPriority(0)
|
||||
, _useNodePriority(false)
|
||||
{
|
||||
}
|
||||
|
||||
void setPriority(int fixedPriority) { _fixedPriority = fixedPriority; _useNodePriority = false; };
|
||||
void setPriorityWithThis(bool useNodePriority) { _useNodePriority = useNodePriority; _fixedPriority = true; }
|
||||
|
||||
void onEnter() override
|
||||
{
|
||||
Sprite::onEnter();
|
||||
|
||||
auto dispatcher = EventDispatcher::getInstance();
|
||||
|
||||
auto listener = TouchEventListener::create(Touch::DispatchMode::ONE_BY_ONE);
|
||||
listener->setSwallowTouches(true);
|
||||
|
||||
listener->onTouchBegan = [&](Touch* touch, Event* event){
|
||||
|
||||
Point locationInNode = this->convertToNodeSpace(touch->getLocation());
|
||||
Size s = this->getContentSize();
|
||||
Rect rect = Rect(0, 0, s.width, s.height);
|
||||
|
||||
if (rect.containsPoint(locationInNode))
|
||||
{
|
||||
this->setColor(Color3B::RED);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
listener->onTouchMoved = [&](Touch* touch, Event* event){
|
||||
//this->setPosition(this->getPosition() + touch->getDelta());
|
||||
};
|
||||
|
||||
listener->onTouchEnded = [&](Touch* touch, Event* event){
|
||||
this->setColor(Color3B::WHITE);
|
||||
};
|
||||
|
||||
if (_useNodePriority)
|
||||
{
|
||||
dispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
||||
}
|
||||
else
|
||||
{
|
||||
dispatcher->addEventListenerWithFixedPriority(listener, _fixedPriority);
|
||||
}
|
||||
_listener = listener;
|
||||
}
|
||||
|
||||
void onExit() override
|
||||
{
|
||||
auto dispatcher = EventDispatcher::getInstance();
|
||||
dispatcher->removeEventListener(_listener);
|
||||
|
||||
Sprite::onExit();
|
||||
}
|
||||
|
||||
private:
|
||||
EventListener* _listener;
|
||||
int _fixedPriority;
|
||||
bool _useNodePriority;
|
||||
};
|
||||
|
||||
void FixedPriorityChangedTest::onEnter()
|
||||
{
|
||||
EventDispatcherTestDemo::onEnter();
|
||||
|
||||
Point origin = Director::getInstance()->getVisibleOrigin();
|
||||
Size size = Director::getInstance()->getVisibleSize();
|
||||
|
||||
auto sprite1 = new TouchableSpriteWithFixedPriority();
|
||||
sprite1->initWithFile("Images/CyanSquare.png");
|
||||
sprite1->autorelease();
|
||||
sprite1->setPriority(30);
|
||||
sprite1->setPosition(origin+Point(size.width/2, size.height/2) + Point(-80, 40));
|
||||
addChild(sprite1, 10);
|
||||
|
||||
auto sprite2 = new TouchableSpriteWithFixedPriority();
|
||||
sprite2->initWithFile("Images/MagentaSquare.png");
|
||||
sprite2->autorelease();
|
||||
sprite2->setPriority(20);
|
||||
sprite2->setPosition(origin+Point(size.width/2, size.height/2));
|
||||
addChild(sprite2, 20);
|
||||
|
||||
auto sprite3 = new TouchableSpriteWithFixedPriority();
|
||||
sprite3->initWithFile("Images/YellowSquare.png");
|
||||
sprite3->autorelease();
|
||||
sprite3->setPriority(10);
|
||||
sprite3->setPosition(Point(0, 0));
|
||||
sprite2->addChild(sprite3, 1);
|
||||
|
||||
}
|
||||
|
||||
std::string FixedPriorityChangedTest::title()
|
||||
{
|
||||
return "Fixed priority changed test";
|
||||
}
|
||||
|
||||
std::string FixedPriorityChangedTest::subtitle()
|
||||
{
|
||||
return "Fixed Priority, Blue: 30, Red: 20, Yellow: 10\n The lower value the higher priority will be.";
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue