2010-11-18 14:50:28 +08:00
|
|
|
#include "Box2dView.h"
|
|
|
|
#include "GLES-Render.h"
|
|
|
|
#include "Test.h"
|
2013-12-26 15:48:26 +08:00
|
|
|
#include "renderer/CCRenderer.h"
|
2010-11-18 14:50:28 +08:00
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-09-14 16:23:50 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// MenuLayer
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
enum
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
IDC_NEXT = 100,
|
|
|
|
IDC_BACK,
|
|
|
|
IDC_RESTART
|
2010-11-18 14:50:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
MenuLayer::MenuLayer(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MenuLayer::~MenuLayer(void)
|
|
|
|
{
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->removeEventListener(_touchListener);
|
2010-11-18 14:50:28 +08:00
|
|
|
}
|
|
|
|
|
2010-09-14 16:23:50 +08:00
|
|
|
MenuLayer* MenuLayer::menuWithEntryID(int entryId)
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto layer = new MenuLayer();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
bool MenuLayer::initWithEntryID(int entryId)
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto director = Director::getInstance();
|
2013-07-23 08:25:44 +08:00
|
|
|
Point visibleOrigin = director->getVisibleOrigin();
|
|
|
|
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);
|
2013-07-12 14:11:55 +08:00
|
|
|
view->setAnchorPoint( Point(0,0) );
|
2014-03-26 23:33:58 +08:00
|
|
|
view->setPosition( Point(visibleOrigin.x+visibleSize.width/2, visibleOrigin.y+visibleSize.height/3) );
|
|
|
|
auto label = Label::create(view->title().c_str(), "fonts/arial.ttf", 28);
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(label, 1);
|
2013-07-12 14:11:55 +08:00
|
|
|
label->setPosition( Point(visibleOrigin.x+visibleSize.width/2, visibleOrigin.y+visibleSize.height-50) );
|
2010-09-14 16:23:50 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto item1 = MenuItemImage::create("Images/b1.png", "Images/b2.png", CC_CALLBACK_1(MenuLayer::backCallback, this) );
|
|
|
|
auto item2 = MenuItemImage::create("Images/r1.png","Images/r2.png", CC_CALLBACK_1( MenuLayer::restartCallback, this) );
|
|
|
|
auto item3 = MenuItemImage::create("Images/f1.png", "Images/f2.png", CC_CALLBACK_1(MenuLayer::nextCallback, this) );
|
2010-09-14 16:23:50 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto menu = Menu::create(item1, item2, item3, NULL);
|
2010-09-14 16:23:50 +08:00
|
|
|
|
2013-07-12 14:47:36 +08:00
|
|
|
menu->setPosition( Point::ZERO );
|
2013-07-12 14:11:55 +08:00
|
|
|
item1->setPosition(Point(VisibleRect::center().x - item2->getContentSize().width*2, VisibleRect::bottom().y+item2->getContentSize().height/2));
|
|
|
|
item2->setPosition(Point(VisibleRect::center().x, VisibleRect::bottom().y+item2->getContentSize().height/2));
|
|
|
|
item3->setPosition(Point(VisibleRect::center().x + item2->getContentSize().width*2, VisibleRect::bottom().y+item2->getContentSize().height/2));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
addChild(menu, 1);
|
|
|
|
|
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(MenuLayer::onTouchBegan, this);
|
|
|
|
listener->onTouchMoved = CC_CALLBACK_2(MenuLayer::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
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void MenuLayer::restartCallback(Ref* sender)
|
2010-09-14 16:23:50 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = new Box2dTestBedScene();
|
|
|
|
auto box = MenuLayer::menuWithEntryID(m_entryID);
|
2012-04-19 14:35:52 +08:00
|
|
|
s->addChild( box );
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene( s );
|
2010-09-14 16:23:50 +08:00
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void MenuLayer::nextCallback(Ref* sender)
|
2010-09-14 16:23:50 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = new Box2dTestBedScene();
|
2012-04-19 14:35:52 +08:00
|
|
|
int next = m_entryID + 1;
|
|
|
|
if( next >= g_totalEntries)
|
|
|
|
next = 0;
|
2013-08-16 16:05:27 +08:00
|
|
|
auto box = MenuLayer::menuWithEntryID(next);
|
2012-04-19 14:35:52 +08:00
|
|
|
s->addChild( box );
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene( s );
|
2010-09-14 16:23:50 +08:00
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void MenuLayer::backCallback(Ref* sender)
|
2010-09-14 16:23:50 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = new Box2dTestBedScene();
|
2012-04-19 14:35:52 +08:00
|
|
|
int next = m_entryID - 1;
|
|
|
|
if( next < 0 ) {
|
|
|
|
next = g_totalEntries - 1;
|
|
|
|
}
|
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto box = MenuLayer::menuWithEntryID(next);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
s->addChild( box );
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene( s );
|
2010-09-14 16:23:50 +08:00
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
//void MenuLayer::registerWithTouchDispatcher()
|
|
|
|
//{
|
|
|
|
// auto director = Director::getInstance();
|
|
|
|
// director->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
|
|
|
|
//}
|
2010-09-14 16:23:50 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
bool MenuLayer::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
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
//-(void) MenuLayer::ccTouchEnded:(UITouch *)touch withEvent:(Event *)event
|
2010-09-14 16:23:50 +08:00
|
|
|
//{
|
|
|
|
//}
|
|
|
|
//
|
2013-06-20 14:17:10 +08:00
|
|
|
//-(void) MenuLayer::ccTouchCancelled:(UITouch *)touch withEvent:(Event *)event
|
2010-09-14 16:23:50 +08:00
|
|
|
//{
|
|
|
|
//}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void MenuLayer::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)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
Box2DView* pView = new Box2DView();
|
|
|
|
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-03-01 08:10:48 +08:00
|
|
|
void Box2DView::draw(Renderer *renderer, const kmMat4 &transform, bool transformUpdated)
|
2010-09-14 16:23:50 +08:00
|
|
|
{
|
2014-03-01 08:10:48 +08:00
|
|
|
Layer::draw(renderer, transform, transformUpdated);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-01-19 03:35:27 +08:00
|
|
|
_customCmd.init(_globalZOrder);
|
2014-03-06 07:49:08 +08:00
|
|
|
_customCmd.func = CC_CALLBACK_0(Box2DView::onDraw, this, transform, transformUpdated);
|
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-03-06 07:49:08 +08:00
|
|
|
void Box2DView::onDraw(const kmMat4 &transform, bool transformUpdated)
|
2013-12-26 15:48:26 +08:00
|
|
|
{
|
2014-03-06 07:49:08 +08:00
|
|
|
kmGLPushMatrix();
|
|
|
|
kmGLLoadMatrix(&transform);
|
|
|
|
|
2013-12-26 15:48:26 +08:00
|
|
|
GL::enableVertexAttribs( cocos2d::GL::VERTEX_ATTRIB_FLAG_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();
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2013-12-26 15:48:26 +08:00
|
|
|
|
2014-03-06 07:49:08 +08:00
|
|
|
kmGLPopMatrix();
|
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
|
|
|
//
|
|
|
|
//void Box2DView::registerWithTouchDispatcher()
|
|
|
|
//{
|
|
|
|
// // higher priority than dragging
|
|
|
|
// auto director = Director::getInstance();
|
|
|
|
// director->getTouchDispatcher()->addTargetedDelegate(this, -10, true);
|
|
|
|
//}
|
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)
|
|
|
|
{
|
|
|
|
log("Box2dView:onKeyPressed, keycode: %d", code);
|
|
|
|
m_test->Keyboard(static_cast<unsigned char>(code));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Box2DView::onKeyReleased(EventKeyboard::KeyCode code, Event* event)
|
|
|
|
{
|
|
|
|
log("onKeyReleased, keycode: %d", code);
|
|
|
|
m_test->KeyboardUp(static_cast<unsigned char>(code));
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
// void Box2DView::accelerometer(UIAccelerometer* accelerometer, Acceleration* acceleration)
|
2010-11-18 14:50:28 +08:00
|
|
|
// {
|
2012-04-19 14:35:52 +08:00
|
|
|
// //// Only run for valid values
|
|
|
|
// //if (acceleration.y!=0 && acceleration.x!=0)
|
|
|
|
// //{
|
|
|
|
// // if (test) test->SetGravity((float)-acceleration.y,(float)acceleration.x);
|
|
|
|
// //}
|
2010-11-18 14:50:28 +08:00
|
|
|
// }
|
2010-09-14 16:23:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
void Box2dTestBedScene::runThisTest()
|
|
|
|
{
|
|
|
|
addChild(MenuLayer::menuWithEntryID(0));
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene(this);
|
2010-09-14 16:23:50 +08:00
|
|
|
}
|