2010-11-18 14:50:28 +08:00
|
|
|
#include "Box2dView.h"
|
|
|
|
#include "GLES-Render.h"
|
|
|
|
#include "Test.h"
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-09-14 16:23:50 +08:00
|
|
|
MenuLayer* MenuLayer::menuWithEntryID(int entryId)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
MenuLayer* pLayer = new MenuLayer();
|
|
|
|
pLayer->initWithEntryID(entryId);
|
|
|
|
pLayer->autorelease();
|
2010-09-14 16:23:50 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return pLayer;
|
2010-09-14 16:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MenuLayer::initWithEntryID(int entryId)
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Director* pDirector = Director::sharedDirector();
|
|
|
|
Point visibleOrigin = pDirector->getVisibleOrigin();
|
|
|
|
Size visibleSize = pDirector->getVisibleSize();
|
2012-10-14 22:21:57 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_entryID = entryId;
|
|
|
|
|
2012-06-15 15:10:40 +08:00
|
|
|
setTouchEnabled( true );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
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) );
|
|
|
|
view->setPosition( Point(visibleOrigin.x+visibleSize.width/2, visibleOrigin.y+visibleSize.height/3) );
|
2012-01-15 22:57:10 +08:00
|
|
|
//#if (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
|
2013-06-20 14:17:10 +08:00
|
|
|
// LabelBMFont* label = LabelBMFont::create(view->title().c_str(), "fonts/arial16.fnt");
|
2012-04-19 14:35:52 +08:00
|
|
|
//#else
|
2013-06-20 14:17:10 +08:00
|
|
|
LabelTTF* label = LabelTTF::create(view->title().c_str(), "Arial", 28);
|
2012-01-15 22:57:10 +08:00
|
|
|
//#endif
|
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-06-20 14:17:10 +08:00
|
|
|
MenuItemImage *item1 = MenuItemImage::create("Images/b1.png", "Images/b2.png", CC_CALLBACK_1(MenuLayer::backCallback, this) );
|
|
|
|
MenuItemImage *item2 = MenuItemImage::create("Images/r1.png","Images/r2.png", CC_CALLBACK_1( MenuLayer::restartCallback, this) );
|
|
|
|
MenuItemImage *item3 = MenuItemImage::create("Images/f1.png", "Images/f2.png", CC_CALLBACK_1(MenuLayer::nextCallback, this) );
|
2010-09-14 16:23:50 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Menu *menu = Menu::create(item1, item2, item3, NULL);
|
2010-09-14 16:23:50 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
menu->setPosition( PointZero );
|
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
|
|
|
|
|
|
|
addChild(menu, 1);
|
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::restartCallback(Object* sender)
|
2010-09-14 16:23:50 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Scene* s = new Box2dTestBedScene();
|
2012-04-19 14:35:52 +08:00
|
|
|
MenuLayer* box = MenuLayer::menuWithEntryID(m_entryID);
|
|
|
|
s->addChild( box );
|
2013-06-20 14:17:10 +08:00
|
|
|
Director::sharedDirector()->replaceScene( s );
|
2010-09-14 16:23:50 +08:00
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void MenuLayer::nextCallback(Object* sender)
|
2010-09-14 16:23:50 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Scene* s = new Box2dTestBedScene();
|
2012-04-19 14:35:52 +08:00
|
|
|
int next = m_entryID + 1;
|
|
|
|
if( next >= g_totalEntries)
|
|
|
|
next = 0;
|
|
|
|
MenuLayer* box = MenuLayer::menuWithEntryID(next);
|
|
|
|
s->addChild( box );
|
2013-06-20 14:17:10 +08:00
|
|
|
Director::sharedDirector()->replaceScene( s );
|
2010-09-14 16:23:50 +08:00
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void MenuLayer::backCallback(Object* sender)
|
2010-09-14 16:23:50 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Scene* s = new Box2dTestBedScene();
|
2012-04-19 14:35:52 +08:00
|
|
|
int next = m_entryID - 1;
|
|
|
|
if( next < 0 ) {
|
|
|
|
next = g_totalEntries - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
MenuLayer* box = MenuLayer::menuWithEntryID(next);
|
|
|
|
|
|
|
|
s->addChild( box );
|
2013-06-20 14:17:10 +08:00
|
|
|
Director::sharedDirector()->replaceScene( s );
|
2010-09-14 16:23:50 +08:00
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MenuLayer::registerWithTouchDispatcher()
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Director* pDirector = Director::sharedDirector();
|
2012-03-21 17:35:20 +08:00
|
|
|
pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
|
2010-09-14 16:23:50 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
bool MenuLayer::ccTouchBegan(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-06-20 14:17:10 +08:00
|
|
|
void MenuLayer::ccTouchMoved(Touch* touch, Event* event)
|
2010-09-14 16:23:50 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Point diff = touch->getDelta();
|
|
|
|
Node *node = getChildByTag( kTagBox2DNode );
|
|
|
|
Point 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)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// setIsAccelerometerEnabled( true );
|
2012-06-15 15:10:40 +08:00
|
|
|
setTouchEnabled( true );
|
2010-09-14 16:23:50 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
schedule( schedule_selector(Box2DView::tick) );
|
2010-09-14 16:23:50 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_entry = g_testEntries + entryId;
|
|
|
|
m_test = m_entry->createFcn();
|
|
|
|
|
2010-11-18 14:50:28 +08:00
|
|
|
return true;
|
2010-09-14 16:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Box2DView::title()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return std::string(m_entry->name);
|
2010-09-14 16:23:50 +08:00
|
|
|
}
|
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
void Box2DView::tick(float dt)
|
2010-09-14 16:23:50 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_test->Step(&settings);
|
2010-09-14 16:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Box2DView::draw()
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer::draw();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
ccGLEnableVertexAttribs( kVertexAttribFlag_Position );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
kmGLPushMatrix();
|
|
|
|
|
|
|
|
m_test->m_world->DrawDebugData();
|
|
|
|
|
|
|
|
kmGLPopMatrix();
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2010-09-14 16:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Box2DView::~Box2DView()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
delete m_test;
|
2010-09-14 16:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Box2DView::registerWithTouchDispatcher()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// higher priority than dragging
|
2013-06-20 14:17:10 +08:00
|
|
|
Director* pDirector = Director::sharedDirector();
|
2012-03-21 17:35:20 +08:00
|
|
|
pDirector->getTouchDispatcher()->addTargetedDelegate(this, -10, true);
|
2010-09-14 16:23:50 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
bool Box2DView::ccTouchBegan(Touch* touch, Event* event)
|
2010-09-14 16:23:50 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Point touchLocation = touch->getLocation();
|
2010-09-14 16:23:50 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Point nodePosition = convertToNodeSpace( touchLocation );
|
2012-04-19 14:35:52 +08:00
|
|
|
// NSLog(@"pos: %f,%f -> %f,%f", touchLocation.x, touchLocation.y, nodePosition.x, nodePosition.y);
|
2010-09-14 16:23:50 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_test->MouseDown(b2Vec2(nodePosition.x,nodePosition.y));
|
2010-09-14 16:23:50 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void Box2DView::ccTouchMoved(Touch* touch, Event* event)
|
2010-09-14 16:23:50 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Point touchLocation = touch->getLocation();
|
|
|
|
Point nodePosition = convertToNodeSpace( touchLocation );
|
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-06-20 14:17:10 +08:00
|
|
|
void Box2DView::ccTouchEnded(Touch* touch, Event* event)
|
2010-09-14 16:23:50 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Point touchLocation = touch->getLocation();
|
|
|
|
Point nodePosition = convertToNodeSpace( touchLocation );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
m_test->MouseUp(b2Vec2(nodePosition.x,nodePosition.y));
|
2010-09-14 16:23:50 +08:00
|
|
|
}
|
|
|
|
|
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-06-20 14:17:10 +08:00
|
|
|
Director::sharedDirector()->replaceScene(this);
|
2010-09-14 16:23:50 +08:00
|
|
|
}
|