improve cpp template (add more event handler) (#1118)

* improve cpp template (add more event handler)

* fix C-style pointer casting
This commit is contained in:
aismann 2023-03-14 08:25:12 +01:00 committed by GitHub
parent 80a5191af2
commit 487e7bbe76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 95 additions and 0 deletions

View File

@ -78,6 +78,27 @@ bool HelloWorld::init()
/////////////////////////////
// 3. add your codes below...
// Some templates (uncomment what you need)
auto touchListener = EventListenerTouchAllAtOnce::create();
touchListener->onTouchesBegan = AX_CALLBACK_2(HelloWorld::onTouchesBegan, this);
touchListener->onTouchesMoved = AX_CALLBACK_2(HelloWorld::onTouchesMoved, this);
touchListener->onTouchesEnded = AX_CALLBACK_2(HelloWorld::onTouchesEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
//auto mouseListener = EventListenerMouse::create();
//mouseListener->onMouseMove = AX_CALLBACK_1(HelloWorld::onMouseMove, this);
//mouseListener->onMouseUp = AX_CALLBACK_1(HelloWorld::onMouseUp, this);
//mouseListener->onMouseDown = AX_CALLBACK_1(HelloWorld::onMouseDown, this);
//mouseListener->onMouseScroll = AX_CALLBACK_1(HelloWorld::onMouseScroll, this);
//_eventDispatcher->addEventListenerWithSceneGraphPriority(mouseListener, this);
//auto keyboardListener = EventListenerKeyboard::create();
//keyboardListener->onKeyPressed = AX_CALLBACK_2(HelloWorld::onKeyPressed, this);
//keyboardListener->onKeyReleased = AX_CALLBACK_2(HelloWorld::onKeyReleased, this);
//_eventDispatcher->addEventListenerWithFixedPriority(keyboardListener, 11);
// add a label shows "Hello World"
// create and initialize a label
@ -122,6 +143,65 @@ bool HelloWorld::init()
return true;
}
void HelloWorld::onTouchesBegan(const std::vector<ax::Touch*>& touches, ax::Event* event)
{
for (auto&& t : touches)
{
AXLOG("onTouchesBegan detected, X:%f Y:%f", t->getLocation().x, t->getLocation().y);
}
}
void HelloWorld::onTouchesMoved(const std::vector<ax::Touch*>& touches, ax::Event* event)
{
for (auto&& t : touches)
{
AXLOG("onTouchesMoved detected, X:%f Y:%f", t->getLocation().x, t->getLocation().y);
}
}
void HelloWorld::onTouchesEnded(const std::vector<ax::Touch*>& touches, ax::Event* event)
{
for (auto&& t : touches)
{
AXLOG("onTouchesEnded detected, X:%f Y:%f", t->getLocation().x, t->getLocation().y);
}
}
void HelloWorld::onMouseDown(Event* event)
{
EventMouse* e = static_cast<EventMouse*>(event);
AXLOG("onMouseDown detected, Key: %d", static_cast<int>(e->getMouseButton()));
}
void HelloWorld::onMouseUp(Event* event)
{
EventMouse* e = static_cast<EventMouse*>(event);
AXLOG("onMouseUp detected, Key: %d", static_cast<int>(e->getMouseButton()));
}
void HelloWorld::onMouseMove(Event* event)
{
EventMouse* e = static_cast<EventMouse*>(event);
AXLOG("onMouseMove detected, X:%f Y:%f", e->getCursorX(), e->getCursorY());
}
void HelloWorld::onMouseScroll(Event* event)
{
EventMouse* e = static_cast<EventMouse*>(event);
AXLOG("onMouseScroll detected, X:%f Y:%f", e->getScrollX(), e->getScrollY());
}
void HelloWorld::onKeyPressed(EventKeyboard::KeyCode code, Event* event)
{
AXLOG("onKeyPressed, keycode: %d", static_cast<int>(code));
}
void HelloWorld::onKeyReleased(EventKeyboard::KeyCode code, Event* event)
{
AXLOG("onKeyReleased, keycode: %d", static_cast<int>(code));
}
void HelloWorld::update(float delta)
{
switch (_gameState)

View File

@ -45,6 +45,21 @@ public:
bool init() override;
void update(float delta) override;
// touch
void onTouchesBegan(const std::vector<ax::Touch*>& touches, ax::Event* event);
void onTouchesMoved(const std::vector<ax::Touch*>& touches, ax::Event* event);
void onTouchesEnded(const std::vector<ax::Touch*>& touches, ax::Event* event);
// mouse
void onMouseDown(ax::Event* event);
void onMouseUp(ax::Event* event);
void onMouseMove(ax::Event* event);
void onMouseScroll(ax::Event* event);
// Keyboard
void onKeyPressed(ax::EventKeyboard::KeyCode code, ax::Event* event);
void onKeyReleased(ax::EventKeyboard::KeyCode code, ax::Event* event);
// a selector callback
void menuCloseCallback(Ref* sender);