axmol/samples/Cpp/TestCpp/Classes/ConfigurationTest/ConfigurationTest.cpp

207 lines
4.6 KiB
C++
Raw Normal View History

2013-05-23 08:56:05 +08:00
#include "ConfigurationTest.h"
#include "../testResource.h"
#include "cocos2d.h"
2013-11-23 04:15:41 +08:00
static std::function<Layer*()> createFunctions[] = {
CL(ConfigurationLoadConfig),
CL(ConfigurationQuery),
CL(ConfigurationInvalid),
CL(ConfigurationDefault),
CL(ConfigurationSet)
2013-05-23 08:56:05 +08:00
};
static int sceneIdx=-1;
#define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0]))
static Layer* nextAction()
2013-05-23 08:56:05 +08:00
{
sceneIdx++;
sceneIdx = sceneIdx % MAX_LAYER;
auto layer = (createFunctions[sceneIdx])();
return layer;
2013-05-23 08:56:05 +08:00
}
static Layer* backAction()
2013-05-23 08:56:05 +08:00
{
sceneIdx--;
int total = MAX_LAYER;
if( sceneIdx < 0 )
sceneIdx += total;
auto layer = (createFunctions[sceneIdx])();
return layer;
2013-05-23 08:56:05 +08:00
}
static Layer* restartAction()
2013-05-23 08:56:05 +08:00
{
2013-11-14 07:55:36 +08:00
auto layer = (createFunctions[sceneIdx])();
return layer;
2013-05-23 08:56:05 +08:00
}
void ConfigurationTestScene::runThisTest()
{
sceneIdx = -1;
addChild(nextAction());
Director::getInstance()->replaceScene(this);
2013-05-23 08:56:05 +08:00
}
std::string ConfigurationBase::title()
{
return "Configuration Test";
}
std::string ConfigurationBase::subtitle()
{
return "";
}
void ConfigurationBase::onEnter()
{
BaseTest::onEnter();
2013-05-23 08:56:05 +08:00
}
void ConfigurationBase::onExit()
{
BaseTest::onExit();
2013-05-23 08:56:05 +08:00
}
void ConfigurationBase::restartCallback(Object* sender)
2013-05-23 08:56:05 +08:00
{
auto s = new ConfigurationTestScene();
2013-05-23 08:56:05 +08:00
s->addChild( restartAction() );
Director::getInstance()->replaceScene(s);
2013-05-23 08:56:05 +08:00
s->release();
}
void ConfigurationBase::nextCallback(Object* sender)
2013-05-23 08:56:05 +08:00
{
auto s = new ConfigurationTestScene();
2013-05-23 08:56:05 +08:00
s->addChild( nextAction() );
Director::getInstance()->replaceScene(s);
2013-05-23 08:56:05 +08:00
s->release();
}
void ConfigurationBase::backCallback(Object* sender)
2013-05-23 08:56:05 +08:00
{
auto s = new ConfigurationTestScene();
2013-05-23 08:56:05 +08:00
s->addChild( backAction() );
Director::getInstance()->replaceScene(s);
2013-05-23 08:56:05 +08:00
s->release();
}
//------------------------------------------------------------------
//
// ConfigurationLoadConfig
//
//------------------------------------------------------------------
void ConfigurationLoadConfig::onEnter()
{
ConfigurationBase::onEnter();
Configuration::getInstance()->loadConfigFile("configs/config-test-ok.plist");
Configuration::getInstance()->dumpInfo();
2013-05-23 08:56:05 +08:00
}
std::string ConfigurationLoadConfig::subtitle()
{
return "Loading config file manually. See console";
}
//------------------------------------------------------------------
//
// ConfigurationQuery
//
//------------------------------------------------------------------
void ConfigurationQuery::onEnter()
{
ConfigurationBase::onEnter();
2013-12-11 15:08:00 +08:00
CCLOG("cocos2d version: %s", Configuration::getInstance()->getValue("cocos2d.x.version").asString().c_str() );
CCLOG("OpenGL version: %s", Configuration::getInstance()->getValue("gl.version").asString().c_str() );
2013-05-23 08:56:05 +08:00
}
std::string ConfigurationQuery::subtitle()
{
2013-12-11 15:08:00 +08:00
return "Check the console";
2013-05-23 08:56:05 +08:00
}
//------------------------------------------------------------------
//
// ConfigurationInvalid
//
//------------------------------------------------------------------
void ConfigurationInvalid::onEnter()
{
ConfigurationBase::onEnter();
Configuration::getInstance()->loadConfigFile("configs/config-test-invalid.plist");
}
std::string ConfigurationInvalid::subtitle()
{
return "Loading an invalid config file";
}
//------------------------------------------------------------------
//
// ConfigurationDefault
//
//------------------------------------------------------------------
void ConfigurationDefault::onEnter()
{
ConfigurationBase::onEnter();
2013-12-11 15:08:00 +08:00
std::string c_value = Configuration::getInstance()->getValue("invalid.key", Value("no key")).asString();
if( c_value != "no key" )
CCLOG("1. Test failed!");
else
CCLOG("1. Test OK!");
2013-12-11 15:08:00 +08:00
bool b_value = Configuration::getInstance()->getValue("invalid.key", Value(true)).asBool();
if( ! b_value )
CCLOG("2. Test failed!");
else
CCLOG("2. Test OK!");
2013-12-11 15:08:00 +08:00
double d_value = Configuration::getInstance()->getValue("invalid.key", Value(42.42)).asDouble();
if( d_value != 42.42 )
CCLOG("3. Test failed!");
else
CCLOG("3. Test OK!");
}
std::string ConfigurationDefault::subtitle()
{
return "Tests defaults values";
}
//------------------------------------------------------------------
//
// ConfigurationSet
//
//------------------------------------------------------------------
void ConfigurationSet::onEnter()
{
ConfigurationBase::onEnter();
Configuration *conf = Configuration::getInstance();
2013-12-11 15:08:00 +08:00
conf->setValue("this.is.an.int.value", Value(10) );
conf->setValue("this.is.a.bool.value", Value(true) );
conf->setValue("this.is.a.string.value", Value("hello world") );
conf->dumpInfo();
}
std::string ConfigurationSet::subtitle()
{
return "Tests setting values manually";
}