2018-01-29 16:25:32 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2010-11-18 14:50:28 +08:00
|
|
|
#include "Box2dView.h"
|
|
|
|
#include "GLES-Render.h"
|
|
|
|
#include "Test.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "renderer/CCRenderer.h"
|
2010-11-18 14:50:28 +08:00
|
|
|
|
2015-04-09 08:37:30 +08:00
|
|
|
USING_NS_CC;
|
|
|
|
|
2010-09-14 16:23:50 +08:00
|
|
|
#define kAccelerometerFrequency 30
|
|
|
|
#define FRAMES_BETWEEN_PRESSES_FOR_DOUBLE_CLICK 10
|
|
|
|
|
2010-11-18 14:50:28 +08:00
|
|
|
extern int g_totalEntries;
|
2010-09-14 16:23:50 +08:00
|
|
|
|
|
|
|
Settings settings;
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
kTagBox2DNode,
|
2010-11-18 14:50:28 +08:00
|
|
|
};
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
Box2dTestBedSuite::Box2dTestBedSuite()
|
|
|
|
{
|
|
|
|
for (int entryId = 0; entryId < g_totalEntries; ++entryId)
|
|
|
|
{
|
2015-06-08 11:06:02 +08:00
|
|
|
addTestCase(g_testEntries[entryId].name, [entryId](){
|
2015-04-03 14:31:03 +08:00
|
|
|
return Box2dTestBed::createWithEntryID(entryId);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2015-06-08 11:06:02 +08:00
|
|
|
|
2010-09-14 16:23:50 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
2015-04-03 14:31:03 +08:00
|
|
|
// Box2dTestBed
|
2010-09-14 16:23:50 +08:00
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
2010-11-18 14:50:28 +08:00
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
Box2dTestBed::Box2dTestBed()
|
2010-11-18 14:50:28 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
Box2dTestBed::~Box2dTestBed()
|
2010-11-18 14:50:28 +08:00
|
|
|
{
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->removeEventListener(_touchListener);
|
2010-11-18 14:50:28 +08:00
|
|
|
}
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
Box2dTestBed* Box2dTestBed::createWithEntryID(int entryId)
|
2010-09-14 16:23:50 +08:00
|
|
|
{
|
2015-04-03 14:31:03 +08:00
|
|
|
auto layer = new (std::nothrow) Box2dTestBed();
|
2013-07-23 08:25:44 +08:00
|
|
|
layer->initWithEntryID(entryId);
|
|
|
|
layer->autorelease();
|
2010-09-14 16:23:50 +08:00
|
|
|
|
2013-07-23 08:25:44 +08:00
|
|
|
return layer;
|
2010-09-14 16:23:50 +08:00
|
|
|
}
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
bool Box2dTestBed::initWithEntryID(int entryId)
|
2010-09-14 16:23:50 +08:00
|
|
|
{
|
2015-06-08 11:06:02 +08:00
|
|
|
if (!TestCase::init())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-16 16:05:27 +08:00
|
|
|
auto director = Director::getInstance();
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 visibleOrigin = director->getVisibleOrigin();
|
2013-07-23 08:25:44 +08:00
|
|
|
Size visibleSize = director->getVisibleSize();
|
2012-10-14 22:21:57 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_entryID = entryId;
|
|
|
|
|
|
|
|
Box2DView* view = Box2DView::viewWithEntryID( entryId );
|
|
|
|
addChild(view, 0, kTagBox2DNode);
|
|
|
|
view->setScale(15);
|
2014-05-15 01:07:09 +08:00
|
|
|
view->setAnchorPoint( Vec2(0,0) );
|
2014-08-28 11:41:18 +08:00
|
|
|
view->setPosition(visibleOrigin.x+visibleSize.width/2, visibleOrigin.y+visibleSize.height/3);
|
2014-04-09 23:31:05 +08:00
|
|
|
auto label = Label::createWithTTF(view->title().c_str(), "fonts/arial.ttf", 28);
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(label, 1);
|
2014-08-28 11:41:18 +08:00
|
|
|
label->setPosition(visibleOrigin.x+visibleSize.width/2, visibleOrigin.y+visibleSize.height-50);
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2013-09-16 15:55:03 +08:00
|
|
|
// Adds touch event listener
|
2013-10-23 11:27:24 +08:00
|
|
|
auto listener = EventListenerTouchOneByOne::create();
|
2013-09-03 18:22:03 +08:00
|
|
|
listener->setSwallowTouches(true);
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
listener->onTouchBegan = CC_CALLBACK_2(Box2dTestBed::onTouchBegan, this);
|
|
|
|
listener->onTouchMoved = CC_CALLBACK_2(Box2dTestBed::onTouchMoved, this);
|
2010-09-14 16:23:50 +08:00
|
|
|
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithFixedPriority(listener, 1);
|
2013-10-23 11:27:24 +08:00
|
|
|
|
2013-09-14 09:02:49 +08:00
|
|
|
_touchListener = listener;
|
2013-10-23 11:27:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2010-09-14 16:23:50 +08:00
|
|
|
}
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
bool Box2dTestBed::onTouchBegan(Touch* touch, Event* event)
|
2010-09-14 16:23:50 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2010-09-14 16:23:50 +08:00
|
|
|
}
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
void Box2dTestBed::onTouchMoved(Touch* touch, Event* event)
|
2010-09-14 16:23:50 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto diff = touch->getDelta();
|
|
|
|
auto node = getChildByTag( kTagBox2DNode );
|
|
|
|
auto currentPos = node->getPosition();
|
2013-07-11 16:38:58 +08:00
|
|
|
node->setPosition(currentPos + diff);
|
2010-11-18 14:50:28 +08:00
|
|
|
}
|
|
|
|
|
2010-09-14 16:23:50 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Box2DView
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
2010-11-18 14:50:28 +08:00
|
|
|
Box2DView::Box2DView(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-09-14 16:23:50 +08:00
|
|
|
Box2DView* Box2DView::viewWithEntryID(int entryId)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
Box2DView* pView = new (std::nothrow) Box2DView();
|
2012-04-19 14:35:52 +08:00
|
|
|
pView->initWithEntryID(entryId);
|
|
|
|
pView->autorelease();
|
2010-09-14 16:23:50 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return pView;
|
2010-09-14 16:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Box2DView::initWithEntryID(int entryId)
|
2014-03-05 15:55:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_entry = g_testEntries + entryId;
|
2013-09-03 18:22:03 +08:00
|
|
|
m_test = m_entry->createFcn();
|
|
|
|
|
|
|
|
|
2013-09-16 15:55:03 +08:00
|
|
|
// Adds Touch Event Listener
|
2013-10-23 11:27:24 +08:00
|
|
|
auto listener = EventListenerTouchOneByOne::create();
|
2013-09-03 18:22:03 +08:00
|
|
|
listener->setSwallowTouches(true);
|
|
|
|
|
|
|
|
listener->onTouchBegan = CC_CALLBACK_2(Box2DView::onTouchBegan, this);
|
|
|
|
listener->onTouchMoved = CC_CALLBACK_2(Box2DView::onTouchMoved, this);
|
|
|
|
listener->onTouchEnded = CC_CALLBACK_2(Box2DView::onTouchEnded, this);
|
|
|
|
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithFixedPriority(listener, -10);
|
2013-09-14 09:02:49 +08:00
|
|
|
_touchListener = listener;
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2014-03-31 12:07:21 +08:00
|
|
|
auto keyboardListener = EventListenerKeyboard::create();
|
|
|
|
keyboardListener->onKeyPressed = CC_CALLBACK_2(Box2DView::onKeyPressed, this);
|
|
|
|
keyboardListener->onKeyReleased = CC_CALLBACK_2(Box2DView::onKeyReleased, this);
|
|
|
|
_eventDispatcher->addEventListenerWithFixedPriority(keyboardListener, -11);
|
|
|
|
_keyboardListener = keyboardListener;
|
|
|
|
|
2010-11-18 14:50:28 +08:00
|
|
|
return true;
|
2010-09-14 16:23:50 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string Box2DView::title() const
|
2010-09-14 16:23:50 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return std::string(m_entry->name);
|
2010-09-14 16:23:50 +08:00
|
|
|
}
|
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void Box2DView::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
|
2010-09-14 16:23:50 +08:00
|
|
|
{
|
2014-05-31 07:42:05 +08:00
|
|
|
Layer::draw(renderer, transform, flags);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-01-16 06:00:49 +08:00
|
|
|
_customCmd.init(_globalZOrder, transform, flags);
|
2014-05-31 07:42:05 +08:00
|
|
|
_customCmd.func = CC_CALLBACK_0(Box2DView::onDraw, this, transform, flags);
|
2014-03-01 08:10:48 +08:00
|
|
|
renderer->addCommand(&_customCmd);
|
2013-12-26 15:48:26 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-31 07:42:05 +08:00
|
|
|
void Box2DView::onDraw(const Mat4 &transform, uint32_t flags)
|
2013-12-26 15:48:26 +08:00
|
|
|
{
|
2014-04-03 15:59:55 +08:00
|
|
|
Director* director = Director::getInstance();
|
2017-02-10 10:35:52 +08:00
|
|
|
CCASSERT(nullptr != director, "Director is null when setting matrix stack");
|
2014-04-03 15:59:55 +08:00
|
|
|
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
|
|
|
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform);
|
2014-03-06 07:49:08 +08:00
|
|
|
|
2019-02-19 16:06:17 +08:00
|
|
|
// glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
|
2014-03-05 15:40:16 +08:00
|
|
|
m_test->Step(&settings);
|
2012-04-19 14:35:52 +08:00
|
|
|
m_test->m_world->DrawDebugData();
|
2013-12-26 15:48:26 +08:00
|
|
|
|
2014-04-03 15:59:55 +08:00
|
|
|
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
2010-09-14 16:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Box2DView::~Box2DView()
|
|
|
|
{
|
2013-10-23 11:27:24 +08:00
|
|
|
// Removes Touch Event Listener
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->removeEventListener(_touchListener);
|
2014-03-31 12:07:21 +08:00
|
|
|
_eventDispatcher->removeEventListener(_keyboardListener);
|
2012-04-19 14:35:52 +08:00
|
|
|
delete m_test;
|
2010-09-14 16:23:50 +08:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
bool Box2DView::onTouchBegan(Touch* touch, Event* event)
|
2010-09-14 16:23:50 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto touchLocation = touch->getLocation();
|
2010-09-14 16:23:50 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto nodePosition = convertToNodeSpace( touchLocation );
|
2013-09-03 18:22:03 +08:00
|
|
|
log("Box2DView::onTouchBegan, pos: %f,%f -> %f,%f", touchLocation.x, touchLocation.y, nodePosition.x, nodePosition.y);
|
2010-09-14 16:23:50 +08:00
|
|
|
|
2014-03-31 12:07:21 +08:00
|
|
|
return m_test->MouseDown(b2Vec2(nodePosition.x,nodePosition.y));
|
2010-09-14 16:23:50 +08:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void Box2DView::onTouchMoved(Touch* touch, Event* event)
|
2010-09-14 16:23:50 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto touchLocation = touch->getLocation();
|
|
|
|
auto nodePosition = convertToNodeSpace( touchLocation );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
log("Box2DView::onTouchMoved, pos: %f,%f -> %f,%f", touchLocation.x, touchLocation.y, nodePosition.x, nodePosition.y);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_test->MouseMove(b2Vec2(nodePosition.x,nodePosition.y));
|
2010-09-14 16:23:50 +08:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void Box2DView::onTouchEnded(Touch* touch, Event* event)
|
2010-09-14 16:23:50 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto touchLocation = touch->getLocation();
|
|
|
|
auto nodePosition = convertToNodeSpace( touchLocation );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
log("Box2DView::onTouchEnded, pos: %f,%f -> %f,%f", touchLocation.x, touchLocation.y, nodePosition.x, nodePosition.y);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_test->MouseUp(b2Vec2(nodePosition.x,nodePosition.y));
|
2010-09-14 16:23:50 +08:00
|
|
|
}
|
|
|
|
|
2014-03-31 12:07:21 +08:00
|
|
|
void Box2DView::onKeyPressed(EventKeyboard::KeyCode code, Event* event)
|
|
|
|
{
|
2016-04-18 17:52:58 +08:00
|
|
|
log("Box2dView:onKeyPressed, keycode: %d", static_cast<int>(code));
|
2014-03-31 12:07:21 +08:00
|
|
|
m_test->Keyboard(static_cast<unsigned char>(code));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Box2DView::onKeyReleased(EventKeyboard::KeyCode code, Event* event)
|
|
|
|
{
|
2016-04-18 17:52:58 +08:00
|
|
|
log("onKeyReleased, keycode: %d", static_cast<int>(code));
|
2014-03-31 12:07:21 +08:00
|
|
|
m_test->KeyboardUp(static_cast<unsigned char>(code));
|
|
|
|
}
|