2021-07-06 21:02:47 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Copyright (c) 2021 @aismann; Peter Eismann, Germany; dreifrankensoft
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-08-05 17:23:55 +08:00
|
|
|
#include "platform/CCPlatformConfig.h"
|
2021-08-19 13:43:18 +08:00
|
|
|
#include "extensions/cocos-ext.h"
|
2021-08-19 14:35:44 +08:00
|
|
|
#include "ImGuiEXT/CCImGuiEXT.h"
|
|
|
|
#include "cocos2d.h"
|
|
|
|
|
|
|
|
#include "Box2DTestBed.h"
|
2021-08-12 15:34:24 +08:00
|
|
|
#include "tests/test.h"
|
|
|
|
#include "tests/settings.h"
|
2021-08-05 17:23:55 +08:00
|
|
|
|
2021-08-12 13:03:14 +08:00
|
|
|
USING_NS_CC;
|
|
|
|
USING_NS_CC_EXT;
|
2021-08-12 11:54:06 +08:00
|
|
|
|
2021-12-28 21:00:45 +08:00
|
|
|
enum {
|
|
|
|
kTagParentNode = 1,
|
2021-08-12 15:34:24 +08:00
|
|
|
};
|
|
|
|
|
2021-07-06 21:02:47 +08:00
|
|
|
Settings settings;
|
2021-07-19 18:33:03 +08:00
|
|
|
cocos2d::Label* labelDebugDraw;
|
2021-07-06 21:02:47 +08:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2021-12-28 21:00:45 +08:00
|
|
|
kTagBox2DNode,
|
2021-07-06 21:02:47 +08:00
|
|
|
};
|
|
|
|
|
2021-12-28 21:00:45 +08:00
|
|
|
|
|
|
|
TestEntry g_testEntries[MAX_TESTS] = { {nullptr} };
|
|
|
|
int g_testCount = 0;
|
2021-08-12 15:34:24 +08:00
|
|
|
|
|
|
|
int RegisterTest(const char* category, const char* name, TestCreateFcn* fcn)
|
|
|
|
{
|
2021-12-28 21:00:45 +08:00
|
|
|
int index = g_testCount;
|
|
|
|
if (index < MAX_TESTS)
|
|
|
|
{
|
|
|
|
g_testEntries[index] = { category, name, fcn };
|
|
|
|
++g_testCount;
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2021-08-12 15:34:24 +08:00
|
|
|
}
|
2021-07-06 21:02:47 +08:00
|
|
|
|
|
|
|
Box2DTestBedTests::Box2DTestBedTests()
|
|
|
|
{
|
2021-12-28 21:00:45 +08:00
|
|
|
for (int entryId = 0; entryId < g_testCount; ++entryId)
|
|
|
|
{
|
|
|
|
addTestCase(g_testEntries[entryId].name, [entryId]() {
|
|
|
|
return Box2DTestBed::createWithEntryID(entryId);
|
|
|
|
});
|
|
|
|
}
|
2021-07-06 21:02:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Box2dTestBed
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
|
2021-12-28 21:00:45 +08:00
|
|
|
Box2DTestBed::Box2DTestBed()
|
|
|
|
{
|
|
|
|
}
|
2021-07-06 21:02:47 +08:00
|
|
|
|
|
|
|
Box2DTestBed::~Box2DTestBed()
|
|
|
|
{
|
2021-12-28 21:00:45 +08:00
|
|
|
Layer::_eventDispatcher->removeEventListener(_touchListener);
|
2021-07-06 21:02:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Box2DTestBed* Box2DTestBed::createWithEntryID(int entryId)
|
|
|
|
{
|
2021-12-28 21:00:45 +08:00
|
|
|
auto layer = new Box2DTestBed();
|
|
|
|
layer->initWithEntryID(entryId);
|
|
|
|
// layer->autorelease();
|
2021-07-06 21:02:47 +08:00
|
|
|
|
2021-12-28 21:00:45 +08:00
|
|
|
return layer;
|
2021-07-06 21:02:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Box2DTestBed::initWithEntryID(int entryId)
|
|
|
|
{
|
2021-12-28 21:00:45 +08:00
|
|
|
if (!TestCase::init())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto director = Director::getInstance();
|
|
|
|
Vec2 visibleOrigin = director->getVisibleOrigin();
|
|
|
|
Size visibleSize = director->getVisibleSize();
|
|
|
|
|
|
|
|
m_entryID = entryId;
|
|
|
|
|
|
|
|
m_entry = g_testEntries + entryId;
|
|
|
|
m_test = m_entry->createFcn();
|
|
|
|
|
|
|
|
debugDrawNode = g_debugDraw.GetDrawNode();
|
|
|
|
m_test->debugDrawNode = debugDrawNode;
|
|
|
|
m_test->g_debugDraw = g_debugDraw;
|
|
|
|
|
|
|
|
TestCase::addChild(debugDrawNode, 100);
|
|
|
|
|
|
|
|
// init physics
|
|
|
|
this->initPhysics();
|
|
|
|
|
|
|
|
auto label = Label::createWithTTF(m_entry->name, "fonts/arial.ttf", 28);
|
|
|
|
TestCase::addChild(label, 1);
|
|
|
|
label->setPosition(visibleOrigin.x + visibleSize.width / 2, visibleOrigin.y + visibleSize.height - 50);
|
|
|
|
|
|
|
|
// Adds touch event listener
|
|
|
|
_touchListener = EventListenerTouchOneByOne::create();
|
|
|
|
_touchListener->setSwallowTouches(true);
|
|
|
|
_touchListener->onTouchBegan = CC_CALLBACK_2(Box2DTestBed::onTouchBegan, this);
|
|
|
|
_touchListener->onTouchMoved = CC_CALLBACK_2(Box2DTestBed::onTouchMoved, this);
|
|
|
|
_touchListener->onTouchEnded = CC_CALLBACK_2(Box2DTestBed::onTouchEnded, this);
|
|
|
|
TestCase::_eventDispatcher->addEventListenerWithFixedPriority(_touchListener,10);
|
|
|
|
|
|
|
|
// Adds Keyboard event listener
|
|
|
|
_keyboardListener = EventListenerKeyboard::create();
|
|
|
|
_keyboardListener->onKeyPressed = CC_CALLBACK_2(Box2DTestBed::onKeyPressed, this);
|
|
|
|
_keyboardListener->onKeyReleased = CC_CALLBACK_2(Box2DTestBed::onKeyReleased, this);
|
|
|
|
TestCase::_eventDispatcher->addEventListenerWithFixedPriority(_keyboardListener,11);
|
|
|
|
|
|
|
|
auto _mouseListener = EventListenerMouse::create();
|
|
|
|
_mouseListener->onMouseMove = CC_CALLBACK_1(Box2DTestBed::onMouseMove, this);
|
|
|
|
_mouseListener->onMouseUp = CC_CALLBACK_1(Box2DTestBed::onMouseUp, this);
|
|
|
|
_mouseListener->onMouseDown = CC_CALLBACK_1(Box2DTestBed::onMouseDown, this);
|
|
|
|
_mouseListener->onMouseScroll = CC_CALLBACK_1(Box2DTestBed::onMouseScroll, this);
|
|
|
|
TestCase::_eventDispatcher->addEventListenerWithFixedPriority(_mouseListener, 12);
|
|
|
|
|
|
|
|
// Demo messageString
|
|
|
|
labelDebugDraw = Label::createWithTTF("TEST", "fonts/arial.ttf", 8.0f);
|
|
|
|
labelDebugDraw->setAnchorPoint(Vec2(0, 1));
|
|
|
|
labelDebugDraw->setPosition(VisibleRect::left().x, VisibleRect::top().y - 10);
|
|
|
|
labelDebugDraw->setColor(Color3B::WHITE);
|
|
|
|
TestCase::addChild(labelDebugDraw, 100);
|
|
|
|
|
|
|
|
TestCase::scheduleUpdate();
|
|
|
|
|
|
|
|
return true;
|
2021-07-06 21:02:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Box2DTestBed::onTouchBegan(Touch* touch, Event* event)
|
|
|
|
{
|
2021-12-28 21:00:45 +08:00
|
|
|
auto location = touch->getLocation() - g_debugDraw.debugNodeOffset;
|
|
|
|
b2Vec2 pos = { location.x / g_debugDraw.mRatio, location.y / g_debugDraw.mRatio };
|
|
|
|
return m_test->MouseDown(pos);
|
2021-07-06 21:02:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Box2DTestBed::onTouchMoved(Touch* touch, Event* event)
|
|
|
|
{
|
2021-12-28 21:00:45 +08:00
|
|
|
auto location = touch->getLocation() - g_debugDraw.debugNodeOffset;
|
|
|
|
b2Vec2 pos = { location.x / g_debugDraw.mRatio, location.y / g_debugDraw.mRatio };
|
|
|
|
m_test->MouseMove(pos);
|
2021-07-06 21:02:47 +08:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:34:24 +08:00
|
|
|
void Box2DTestBed::onTouchEnded(Touch* touch, Event* event)
|
2021-07-06 21:02:47 +08:00
|
|
|
{
|
2021-12-28 21:00:45 +08:00
|
|
|
auto location = touch->getLocation() - g_debugDraw.debugNodeOffset;
|
|
|
|
b2Vec2 pos = { location.x / g_debugDraw.mRatio, location.y / g_debugDraw.mRatio };
|
|
|
|
m_test->MouseUp(pos);
|
2021-07-06 21:02:47 +08:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:34:24 +08:00
|
|
|
void Box2DTestBed::onKeyPressed(EventKeyboard::KeyCode code, Event* event)
|
2021-07-06 21:02:47 +08:00
|
|
|
{
|
2021-12-28 21:00:45 +08:00
|
|
|
CCLOG("onKeyPressed, keycode: %d", static_cast<int>(code));
|
|
|
|
m_test->Keyboard((static_cast<int>(code) - 59)); // its a bad hack!
|
2021-07-06 21:02:47 +08:00
|
|
|
}
|
2021-08-05 17:23:55 +08:00
|
|
|
|
2021-08-12 15:34:24 +08:00
|
|
|
void Box2DTestBed::onKeyReleased(EventKeyboard::KeyCode code, Event* event)
|
2021-08-12 13:03:14 +08:00
|
|
|
{
|
2021-12-28 21:00:45 +08:00
|
|
|
CCLOG("onKeyPressed, keycode: %d", static_cast<int>(code));
|
|
|
|
m_test->KeyboardUp((static_cast<int>(code) - 59)); // its a bad hack!
|
|
|
|
|
2021-08-12 13:03:14 +08:00
|
|
|
}
|
|
|
|
|
2021-08-19 14:35:44 +08:00
|
|
|
void Box2DTestBed::onMouseDown(Event* event)
|
|
|
|
{
|
2021-12-28 21:00:45 +08:00
|
|
|
EventMouse* e = (EventMouse*)event;
|
|
|
|
switch (e->getMouseButton())
|
|
|
|
{
|
|
|
|
button[(int)EventMouse::MouseButton::BUTTON_LEFT] = false;
|
|
|
|
button[(int)EventMouse::MouseButton::BUTTON_RIGHT] = false;
|
|
|
|
button[(int)EventMouse::MouseButton::BUTTON_MIDDLE] = false;
|
|
|
|
case EventMouse::MouseButton::BUTTON_LEFT:
|
|
|
|
button[(int)EventMouse::MouseButton::BUTTON_LEFT] = true;
|
|
|
|
break;
|
|
|
|
case EventMouse::MouseButton::BUTTON_RIGHT:
|
|
|
|
button[(int)EventMouse::MouseButton::BUTTON_RIGHT] = true;
|
|
|
|
break;
|
|
|
|
case EventMouse::MouseButton::BUTTON_MIDDLE:
|
|
|
|
button[(int)EventMouse::MouseButton::BUTTON_MIDDLE] = true;
|
|
|
|
break;
|
|
|
|
}
|
2021-08-19 14:35:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Box2DTestBed::onMouseUp(Event* event)
|
|
|
|
{
|
2021-12-28 21:00:45 +08:00
|
|
|
button[(int)EventMouse::MouseButton::BUTTON_LEFT] = false;
|
|
|
|
button[(int)EventMouse::MouseButton::BUTTON_RIGHT] = false;
|
|
|
|
button[(int)EventMouse::MouseButton::BUTTON_MIDDLE] = false;
|
2021-08-19 14:35:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Box2DTestBed::onMouseMove(Event* event)
|
|
|
|
{
|
2021-12-28 21:00:45 +08:00
|
|
|
EventMouse* e = (EventMouse*)event;
|
|
|
|
pos = { e->getCursorX() / g_debugDraw.mRatio , e->getCursorY() / g_debugDraw.mRatio };
|
|
|
|
|
|
|
|
if (button[(int)EventMouse::MouseButton::BUTTON_RIGHT])
|
|
|
|
{
|
|
|
|
(pos.x > oldPos.x) ? g_debugDraw.debugNodeOffset.x += 4 : g_debugDraw.debugNodeOffset.x -= 4;
|
|
|
|
(pos.y < oldPos.y) ? g_debugDraw.debugNodeOffset.y -= 2 : g_debugDraw.debugNodeOffset.y += 2;
|
|
|
|
}
|
|
|
|
oldPos = pos;
|
2021-08-19 14:35:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Box2DTestBed::onMouseScroll(Event* event)
|
|
|
|
{
|
2021-12-28 21:00:45 +08:00
|
|
|
EventMouse* e = (EventMouse*)event;
|
|
|
|
g_debugDraw.mRatio += e->getScrollY();
|
2021-08-19 14:35:44 +08:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:34:24 +08:00
|
|
|
void Box2DTestBed::onEnter()
|
2021-08-12 13:03:14 +08:00
|
|
|
{
|
2021-12-28 21:00:45 +08:00
|
|
|
Scene::onEnter();
|
|
|
|
ImGuiEXT::getInstance()->addFont(FileUtils::getInstance()->fullPathForFilename("fonts/arial.ttf"));
|
|
|
|
ImGuiEXT::getInstance()->addRenderLoop("#im01", CC_CALLBACK_0(Box2DTestBed::onDrawImGui, this), this);
|
2021-08-12 13:03:14 +08:00
|
|
|
}
|
2021-08-12 15:34:24 +08:00
|
|
|
void Box2DTestBed::onExit()
|
2021-08-12 13:03:14 +08:00
|
|
|
{
|
2021-12-28 21:00:45 +08:00
|
|
|
Scene::onExit();
|
|
|
|
ImGuiEXT::getInstance()->removeRenderLoop("#im01");
|
2021-08-12 13:03:14 +08:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:34:24 +08:00
|
|
|
void Box2DTestBed::update(float dt)
|
2021-08-12 13:03:14 +08:00
|
|
|
{
|
2021-12-28 21:00:45 +08:00
|
|
|
// Debug draw
|
|
|
|
m_test->debugString = "";
|
|
|
|
labelDebugDraw->setString("");
|
|
|
|
debugDrawNode->clear();
|
|
|
|
m_test->Step(settings);
|
|
|
|
m_test->m_world->DebugDraw();
|
2021-08-12 13:03:14 +08:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:34:24 +08:00
|
|
|
void Box2DTestBed::initPhysics()
|
2021-08-05 17:23:55 +08:00
|
|
|
{
|
2021-12-28 21:00:45 +08:00
|
|
|
uint32 flags = 0;
|
|
|
|
flags += 1 * b2Draw::e_shapeBit;
|
|
|
|
flags += 1 * b2Draw::e_jointBit;
|
|
|
|
flags += 0 * b2Draw::e_aabbBit;
|
|
|
|
flags += 0 * b2Draw::e_centerOfMassBit;
|
|
|
|
g_debugDraw.SetFlags(flags);
|
|
|
|
g_debugDraw.mRatio = 8;
|
|
|
|
m_test->m_world->SetDebugDraw(&g_debugDraw);
|
|
|
|
m_test->g_debugDraw = g_debugDraw;
|
|
|
|
g_debugDraw.debugNodeOffset = { 250, 70 };
|
|
|
|
m_test->g_debugDraw.debugNodeOffset = g_debugDraw.debugNodeOffset;
|
|
|
|
|
|
|
|
settings.m_hertz = 60;
|
2021-08-12 13:03:14 +08:00
|
|
|
}
|
|
|
|
|
2021-08-12 15:34:24 +08:00
|
|
|
void Box2DTestBed::onDrawImGui()
|
2021-08-12 13:03:14 +08:00
|
|
|
{
|
2021-12-28 21:00:45 +08:00
|
|
|
m_test->UpdateUI();
|
2021-08-12 15:34:24 +08:00
|
|
|
}
|