Add custom widget

This commit is contained in:
CaiWenzhi 2014-03-07 15:36:45 +08:00
parent 5ee35be0ad
commit 62dfe630c7
15 changed files with 369 additions and 32 deletions

View File

@ -1 +1 @@
d5aad52713bc25f053afa48235e48aae9155f328
35abe9ed0238f007fc0b75f05c9e1a5a59d8be90

View File

@ -72,17 +72,17 @@ bool AppDelegate::applicationDidFinishLaunching()
{
auto resourceSize = Size(960, 640);
searchPaths.push_back("hd");
searchPaths.push_back("hd/scenetest");
searchPaths.push_back("hd/scenetest/ArmatureComponentTest");
searchPaths.push_back("hd/scenetest/AttributeComponentTest");
searchPaths.push_back("hd/scenetest/BackgroundComponentTest");
searchPaths.push_back("hd/scenetest/EffectComponentTest");
searchPaths.push_back("hd/scenetest/LoadSceneEdtiorFileTest");
searchPaths.push_back("hd/scenetest/ParticleComponentTest");
searchPaths.push_back("hd/scenetest/SpriteComponentTest");
searchPaths.push_back("hd/scenetest/TmxMapComponentTest");
searchPaths.push_back("hd/scenetest/UIComponentTest");
searchPaths.push_back("hd/scenetest/TriggerTest");
// searchPaths.push_back("hd/scenetest");
// searchPaths.push_back("hd/scenetest/ArmatureComponentTest");
// searchPaths.push_back("hd/scenetest/AttributeComponentTest");
// searchPaths.push_back("hd/scenetest/BackgroundComponentTest");
// searchPaths.push_back("hd/scenetest/EffectComponentTest");
// searchPaths.push_back("hd/scenetest/LoadSceneEdtiorFileTest");
// searchPaths.push_back("hd/scenetest/ParticleComponentTest");
// searchPaths.push_back("hd/scenetest/SpriteComponentTest");
// searchPaths.push_back("hd/scenetest/TmxMapComponentTest");
// searchPaths.push_back("hd/scenetest/UIComponentTest");
// searchPaths.push_back("hd/scenetest/TriggerTest");
director->setContentScaleFactor(resourceSize.height/designSize.height);
}
else

View File

@ -3,6 +3,7 @@
#include "CustomGUIScene.h"
#include "CocoStudioGUITest.h"
#include "CustomTest/CustomImageTest/CustomImageTest.h"
#include "CustomTest/CustomParticleWidgetTest/CustomParticleWidgetTest.h"
enum
@ -27,6 +28,15 @@ g_guisTests[] =
pScene->release();
}
},
{
"custom gui particle widget Test",
[](Ref* sender)
{
CustomParticleWidgetScene* pScene = new CustomParticleWidgetScene();
pScene->runThisTest();
pScene->release();
}
},
};
static const int g_maxTests = sizeof(g_guisTests) / sizeof(g_guisTests[0]);
@ -53,8 +63,8 @@ void CustomGUITestMainLayer::onEnter()
for (int i = 0; i < g_maxTests; ++i)
{
auto pItem = MenuItemFont::create(g_guisTests[i].name, g_guisTests[i].callback);
pItem->setPosition(Point(s.width / 2, s.height / 2));
// pItem->setPosition(Point(s.width / 2, s.height - (i + 1) * LINE_SPACE));
// pItem->setPosition(Point(s.width / 2, s.height / 2));
pItem->setPosition(Point(s.width / 2, s.height - (i + 1) * LINE_SPACE));
_itemMenu->addChild(pItem, kItemTagBasic + i);
}

View File

@ -7,6 +7,8 @@
#include "../../CustomWidget/CustomReader.h"
#include "cocostudio/CCSGUIReader.h"
USING_NS_CC;
USING_NS_CC_EXT;
#pragma mark -
#pragma mark CustomImageLayer

View File

@ -7,10 +7,7 @@
#include "extensions/cocos-ext.h"
#include "../../../../testBasic.h"
USING_NS_CC;
USING_NS_CC_EXT;
class CustomImageLayer : public Layer
class CustomImageLayer : public cocos2d::Layer
{
public:
virtual void onEnter();
@ -21,7 +18,7 @@ class CustomImageScene : public TestScene
public:
virtual void onEnter();
virtual void runThisTest();
void BackCallback(Ref* pSender);
void BackCallback(cocos2d::Ref* pSender);
};
#endif /* defined(__TestCpp__CustomUIScene__) */

View File

@ -0,0 +1,79 @@
//
// CustomParticleWidgetTest.cpp
// CustomUI
//
// Created by cai wenzhi on 14-3-7.
//
//
#include "CustomParticleWidgetTest.h"
#include "../../CustomGUIScene.h"
#include "../../CustomWidget/CustomParticleWidget.h"
#include "../../CustomWidget/CustomParticleWidgetReader.h"
#include "cocostudio/CCSGUIReader.h"
USING_NS_CC;
USING_NS_CC_EXT;
using namespace ui;
using namespace cocostudio;
#pragma mark -
#pragma mark CustomParticleWidgetLayer
#pragma mark -
void CustomParticleWidgetLayer::onEnter()
{
CCLayer::onEnter();
GUIReader* guiReader = GUIReader::getInstance();
guiReader->registerTypeAndCallBack("CustomParticleWidget",
&CustomParticleWidget::createInstance,
CustomParticleWidgetReader::getInstance(),
parseselector(CustomParticleWidgetReader::setProperties));
CustomParticleWidget* custom = CustomParticleWidget::create();
custom->setParticlePlist("Particles/BoilingFoam.plist");
addChild(custom, 10, -1);
}
#pragma mark -
#pragma mark CustomImageScene
#pragma mark -
void CustomParticleWidgetScene::onEnter()
{
CCScene::onEnter();
Layer* pLayer = new CustomParticleWidgetLayer();
addChild(pLayer);
pLayer->release();
LabelTTF* label = LabelTTF::create("Back", "Arial", 20);
//#endif
MenuItemLabel* pMenuItem = MenuItemLabel::create(label, CC_CALLBACK_1(CustomParticleWidgetScene::BackCallback, this));
Menu* pMenu = Menu::create(pMenuItem, NULL);
pMenu->setPosition( Point::ZERO );
pMenuItem->setPosition( Point( VisibleRect::right().x - 50, VisibleRect::bottom().y + 25) );
addChild(pMenu, 1);
}
void CustomParticleWidgetScene::runThisTest()
{
Layer* pLayer = new CustomParticleWidgetLayer();
addChild(pLayer);
pLayer->release();
CCDirector::getInstance()->replaceScene(this);
}
void CustomParticleWidgetScene::BackCallback(Ref* pSender)
{
CustomGUITestScene* pScene = new CustomGUITestScene();
pScene->runThisTest();
pScene->release();
}

View File

@ -0,0 +1,30 @@
//
// CustomParticleWidgetTest.h
// CustomUI
//
// Created by cai wenzhi on 14-3-7.
//
//
#ifndef __CustomUI__CustomParticleWidgetTest__
#define __CustomUI__CustomParticleWidgetTest__
#include "cocos2d.h"
#include "extensions/cocos-ext.h"
#include "../../../../testBasic.h"
class CustomParticleWidgetLayer : public cocos2d::Layer
{
public:
virtual void onEnter();
};
class CustomParticleWidgetScene : public cocos2d::Scene
{
public:
virtual void onEnter();
virtual void runThisTest();
void BackCallback(cocos2d::Ref* pSender);
};
#endif /* defined(__CustomUI__CustomParticleWidgetTest__) */

View File

@ -2,6 +2,9 @@
#include "CustomImageView.h"
USING_NS_CC;
using namespace ui;
CustomImageView::CustomImageView()
: _label(NULL)
{

View File

@ -4,10 +4,7 @@
#include "gui/CocosGUI.h"
USING_NS_CC;
using namespace cocos2d::ui;
class CustomImageView : public ImageView
class CustomImageView : public cocos2d::ui::ImageView
{
public:
@ -15,7 +12,7 @@ public:
~CustomImageView();
static CustomImageView* create();
static Ref* createInstance();
static cocos2d::Ref* createInstance();
void setText(const std::string& text);
const std::string& getText() const;
@ -25,7 +22,7 @@ protected:
virtual void initRenderer() override;
protected:
LabelTTF* _label;
cocos2d::LabelTTF* _label;
};
#endif /* defined(__TestCpp__CustomImageView__) */

View File

@ -3,6 +3,11 @@
#include "CustomImageViewReader.h"
#include "CustomImageView.h"
USING_NS_CC;
USING_NS_CC_EXT;
using namespace cocos2d::ui;
using namespace cocostudio;
static CustomImageViewReader* _instanceCustomImageViewReader = NULL;
CustomImageViewReader::CustomImageViewReader()

View File

@ -7,12 +7,7 @@
#include "cocostudio/DictionaryHelper.h"
#include "gui/CocosGUI.h"
USING_NS_CC;
USING_NS_CC_EXT;
using namespace cocos2d::ui;
using namespace cocostudio;
class CustomImageViewReader : public Ref
class CustomImageViewReader : public cocos2d::Ref
{
public:
@ -22,7 +17,7 @@ public:
static CustomImageViewReader* getInstance();
static void purge();
virtual void setProperties(const std::string& classType, Widget* widget, const rapidjson::Value& customOptions);
virtual void setProperties(const std::string& classType, cocos2d::ui::Widget* widget, const rapidjson::Value& customOptions);
};
#endif /* defined(__TestCpp__CustomImageViewReader__) */

View File

@ -0,0 +1,97 @@
//
// CustomParticleWidget.cpp
// CustomUI
//
// Created by cai wenzhi on 14-3-7.
//
//
#include "CustomParticleWidget.h"
USING_NS_CC;
using namespace ui;
CustomParticleWidget::CustomParticleWidget()
: _emitter(NULL)
, _emitterPlist("")
{
}
CustomParticleWidget::~CustomParticleWidget()
{
}
Ref* CustomParticleWidget::createInstance()
{
return create();
}
CustomParticleWidget* CustomParticleWidget::create()
{
CustomParticleWidget* custom = new CustomParticleWidget();
if (custom && custom->init())
{
custom->autorelease();
return custom;
}
CC_SAFE_DELETE(custom);
return NULL;
}
bool CustomParticleWidget::init()
{
if (Widget::init())
{
return true;
}
return false;
}
void CustomParticleWidget::initRenderer()
{
Widget::initRenderer();
}
void CustomParticleWidget::removeAllChildren()
{
Widget::removeAllChildren();
}
void CustomParticleWidget::setParticlePlist(const char *plist)
{
if (!_emitter)
{
_emitter = ParticleSystemQuad::create(plist);
}
else
{
_emitter->removeFromParent();
_emitter = ParticleSystemQuad::create(plist);
}
Node::addChild(_emitter , getZOrder() + 1, -1);
_emitterPlist = plist;
}
const char* CustomParticleWidget::getParticlePlist() const
{
return _emitterPlist;
}
void CustomParticleWidget::setParticlePosition(const Point &pos)
{
_emitter->setPosition(pos);
_emitterPostion = pos;
}
const CCPoint& CustomParticleWidget::getParticlePosition() const
{
return _emitterPostion;
}

View File

@ -0,0 +1,43 @@
//
// CustomParticleWidget.h
// CustomUI
//
// Created by cai wenzhi on 14-3-7.
//
//
#ifndef __CustomUI__CustomParticleWidget__
#define __CustomUI__CustomParticleWidget__
#include "gui/CocosGUI.h"
class CustomParticleWidget : public cocos2d::ui::Widget
{
public:
CustomParticleWidget();
~CustomParticleWidget();
static CustomParticleWidget* create();
static cocos2d::Ref* createInstance();
void setParticlePlist(const char* plist);
const char* getParticlePlist() const;
void setParticlePosition(const cocos2d::Point& pos);
const cocos2d::Point& getParticlePosition() const;
protected:
virtual bool init();
virtual void initRenderer();
virtual void removeAllChildren();
protected:
cocos2d::ParticleSystem* _emitter;
const char* _emitterPlist;
cocos2d::Point _emitterPostion;
};
#endif /* defined(__CustomUI__CustomParticleWidget__) */

View File

@ -0,0 +1,51 @@
//
// CustomParticleWidgetReader.cpp
// CustomUI
//
// Created by cai wenzhi on 14-3-7.
//
//
#include "CustomParticleWidgetReader.h"
#include "CustomParticleWidget.h"
USING_NS_CC;
USING_NS_CC_EXT;
using namespace ui;
using namespace cocostudio;
CustomParticleWidgetReader::CustomParticleWidgetReader()
{
}
CustomParticleWidgetReader::~CustomParticleWidgetReader()
{
}
static CustomParticleWidgetReader* _instanceCustomParticleWidgetReader = NULL;
CustomParticleWidgetReader* CustomParticleWidgetReader::getInstance()
{
if (!_instanceCustomParticleWidgetReader)
{
_instanceCustomParticleWidgetReader = new CustomParticleWidgetReader();
}
return _instanceCustomParticleWidgetReader;
}
void CustomParticleWidgetReader::setProperties(const std::string& classType,
Widget *widget,
const rapidjson::Value &customOptions)
{
CustomParticleWidget* custom = static_cast<CustomParticleWidget*>(widget);
bool isExistParticlePlist = DICTOOL->checkObjectExist_json(customOptions, "ParticlePlist");
if (isExistParticlePlist)
{
const char* ParticlePlist = DICTOOL->getStringValue_json(customOptions, "ParticlePlist");
custom->setParticlePlist(ParticlePlist);
}
}

View File

@ -0,0 +1,28 @@
//
// CustomParticleWidgetReader.h
// CustomUI
//
// Created by cai wenzhi on 14-3-7.
//
//
#ifndef __CustomUI__CustomParticleWidgetReader__
#define __CustomUI__CustomParticleWidgetReader__
#include "extensions/cocos-ext.h"
#include "cocostudio/DictionaryHelper.h"
#include "gui/CocosGUI.h"
class CustomParticleWidgetReader : public cocos2d::Ref
{
public:
CustomParticleWidgetReader();
~CustomParticleWidgetReader();
static CustomParticleWidgetReader* getInstance();
static void purge();
virtual void setProperties(const std::string& classType, cocos2d::ui::Widget* widget, const rapidjson::Value& customOptions);
};
#endif /* defined(__CustomUI__CustomParticleWidgetReader__) */