issue #4636. refactor Scale9Sprite

This commit is contained in:
andyque 2014-04-02 18:35:08 +08:00
parent b27fe6ecc2
commit 0a6b0a4389
6 changed files with 58 additions and 47 deletions

View File

@ -167,9 +167,9 @@ void Button::setScale9Enabled(bool able)
_buttonDisableRenderer = Sprite::create(); _buttonDisableRenderer = Sprite::create();
} }
loadTextureNormal(_normalFileName.c_str(), _normalTexType); loadTextureNormal(_normalFileName, _normalTexType);
loadTexturePressed(_clickedFileName.c_str(), _pressedTexType); loadTexturePressed(_clickedFileName, _pressedTexType);
loadTextureDisabled(_disabledFileName.c_str(), _disabledTexType); loadTextureDisabled(_disabledFileName, _disabledTexType);
addProtectedChild(_buttonNormalRenderer, NORMAL_RENDERER_Z, -1); addProtectedChild(_buttonNormalRenderer, NORMAL_RENDERER_Z, -1);
addProtectedChild(_buttonClickedRenderer, PRESSED_RENDERER_Z, -1); addProtectedChild(_buttonClickedRenderer, PRESSED_RENDERER_Z, -1);
addProtectedChild(_buttonDisableRenderer, DISABLED_RENDERER_Z, -1); addProtectedChild(_buttonDisableRenderer, DISABLED_RENDERER_Z, -1);
@ -227,10 +227,10 @@ void Button::loadTextureNormal(const std::string& normal,TextureResType texType)
switch (_normalTexType) switch (_normalTexType)
{ {
case UI_TEX_TYPE_LOCAL: case UI_TEX_TYPE_LOCAL:
normalRendererScale9->initWithFile(normal.c_str()); normalRendererScale9->initWithFile(normal);
break; break;
case UI_TEX_TYPE_PLIST: case UI_TEX_TYPE_PLIST:
normalRendererScale9->initWithSpriteFrameName(normal.c_str()); normalRendererScale9->initWithSpriteFrameName(normal);
break; break;
default: default:
break; break;
@ -275,10 +275,10 @@ void Button::loadTexturePressed(const std::string& selected,TextureResType texTy
switch (_pressedTexType) switch (_pressedTexType)
{ {
case UI_TEX_TYPE_LOCAL: case UI_TEX_TYPE_LOCAL:
clickedRendererScale9->initWithFile(selected.c_str()); clickedRendererScale9->initWithFile(selected);
break; break;
case UI_TEX_TYPE_PLIST: case UI_TEX_TYPE_PLIST:
clickedRendererScale9->initWithSpriteFrameName(selected.c_str()); clickedRendererScale9->initWithSpriteFrameName(selected);
break; break;
default: default:
break; break;
@ -323,10 +323,10 @@ void Button::loadTextureDisabled(const std::string& disabled,TextureResType texT
switch (_disabledTexType) switch (_disabledTexType)
{ {
case UI_TEX_TYPE_LOCAL: case UI_TEX_TYPE_LOCAL:
disabledScale9->initWithFile(disabled.c_str()); disabledScale9->initWithFile(disabled);
break; break;
case UI_TEX_TYPE_PLIST: case UI_TEX_TYPE_PLIST:
disabledScale9->initWithSpriteFrameName(disabled.c_str()); disabledScale9->initWithSpriteFrameName(disabled);
break; break;
default: default:
break; break;
@ -745,9 +745,9 @@ void Button::copySpecialProperties(Widget *widget)
{ {
_prevIgnoreSize = button->_prevIgnoreSize; _prevIgnoreSize = button->_prevIgnoreSize;
setScale9Enabled(button->_scale9Enabled); setScale9Enabled(button->_scale9Enabled);
loadTextureNormal(button->_normalFileName.c_str(), button->_normalTexType); loadTextureNormal(button->_normalFileName, button->_normalTexType);
loadTexturePressed(button->_clickedFileName.c_str(), button->_pressedTexType); loadTexturePressed(button->_clickedFileName, button->_pressedTexType);
loadTextureDisabled(button->_disabledFileName.c_str(), button->_disabledTexType); loadTextureDisabled(button->_disabledFileName, button->_disabledTexType);
setCapInsetsNormalRenderer(button->_capInsetsNormal); setCapInsetsNormalRenderer(button->_capInsetsNormal);
setCapInsetsPressedRenderer(button->_capInsetsPressed); setCapInsetsPressedRenderer(button->_capInsetsPressed);
setCapInsetsDisabledRenderer(button->_capInsetsDisabled); setCapInsetsDisabledRenderer(button->_capInsetsDisabled);

View File

@ -386,6 +386,22 @@ TextField* TextField::create()
return nullptr; return nullptr;
} }
TextField* TextField::create(const std::string &placeholder, const std::string &fontName, int fontSize)
{
TextField* widget = new TextField();
if (widget && widget->init())
{
widget->setTouchEnabled(true);
widget->setPlaceHolder(placeholder);
widget->setFontName(fontName);
widget->setFontSize(fontSize);
widget->autorelease();
return widget;
}
CC_SAFE_DELETE(widget);
return nullptr;
}
bool TextField::init() bool TextField::init()
{ {
if (Widget::init()) if (Widget::init())

View File

@ -110,6 +110,9 @@ public:
TextField(); TextField();
virtual ~TextField(); virtual ~TextField();
static TextField* create(); static TextField* create();
static TextField* create(const std::string& placeholder,
const std::string& fontName,
int fontSize);
void setTouchSize(const Size &size); void setTouchSize(const Size &size);
Size getTouchSize(); Size getTouchSize();
void setTouchAreaEnabled(bool enable); void setTouchAreaEnabled(bool enable);

View File

@ -458,16 +458,14 @@ void Scale9Sprite::updatePositions()
_centre->setPosition(Point(leftWidth, bottomHeight)); _centre->setPosition(Point(leftWidth, bottomHeight));
} }
bool Scale9Sprite::initWithFile(const char* file, const Rect& rect, const Rect& capInsets) bool Scale9Sprite::initWithFile(const std::string& file, const Rect& rect, const Rect& capInsets)
{ {
CCASSERT(file != NULL, "Invalid file for sprite");
SpriteBatchNode *batchnode = SpriteBatchNode::create(file, 9); SpriteBatchNode *batchnode = SpriteBatchNode::create(file, 9);
bool pReturn = this->initWithBatchNode(batchnode, rect, capInsets); bool pReturn = this->initWithBatchNode(batchnode, rect, capInsets);
return pReturn; return pReturn;
} }
Scale9Sprite* Scale9Sprite::create(const char* file, const Rect& rect, const Rect& capInsets) Scale9Sprite* Scale9Sprite::create(const std::string& file, const Rect& rect, const Rect& capInsets)
{ {
Scale9Sprite* pReturn = new Scale9Sprite(); Scale9Sprite* pReturn = new Scale9Sprite();
if ( pReturn && pReturn->initWithFile(file, rect, capInsets) ) if ( pReturn && pReturn->initWithFile(file, rect, capInsets) )
@ -479,14 +477,13 @@ Scale9Sprite* Scale9Sprite::create(const char* file, const Rect& rect, const Re
return NULL; return NULL;
} }
bool Scale9Sprite::initWithFile(const char* file, const Rect& rect) bool Scale9Sprite::initWithFile(const std::string& file, const Rect& rect)
{ {
CCASSERT(file != NULL, "Invalid file for sprite");
bool pReturn = this->initWithFile(file, rect, Rect::ZERO); bool pReturn = this->initWithFile(file, rect, Rect::ZERO);
return pReturn; return pReturn;
} }
Scale9Sprite* Scale9Sprite::create(const char* file, const Rect& rect) Scale9Sprite* Scale9Sprite::create(const std::string& file, const Rect& rect)
{ {
Scale9Sprite* pReturn = new Scale9Sprite(); Scale9Sprite* pReturn = new Scale9Sprite();
if ( pReturn && pReturn->initWithFile(file, rect) ) if ( pReturn && pReturn->initWithFile(file, rect) )
@ -499,13 +496,13 @@ Scale9Sprite* Scale9Sprite::create(const char* file, const Rect& rect)
} }
bool Scale9Sprite::initWithFile(const Rect& capInsets, const char* file) bool Scale9Sprite::initWithFile(const Rect& capInsets, const std::string& file)
{ {
bool pReturn = this->initWithFile(file, Rect::ZERO, capInsets); bool pReturn = this->initWithFile(file, Rect::ZERO, capInsets);
return pReturn; return pReturn;
} }
Scale9Sprite* Scale9Sprite::create(const Rect& capInsets, const char* file) Scale9Sprite* Scale9Sprite::create(const Rect& capInsets, const std::string& file)
{ {
Scale9Sprite* pReturn = new Scale9Sprite(); Scale9Sprite* pReturn = new Scale9Sprite();
if ( pReturn && pReturn->initWithFile(capInsets, file) ) if ( pReturn && pReturn->initWithFile(capInsets, file) )
@ -517,14 +514,14 @@ Scale9Sprite* Scale9Sprite::create(const Rect& capInsets, const char* file)
return NULL; return NULL;
} }
bool Scale9Sprite::initWithFile(const char* file) bool Scale9Sprite::initWithFile(const std::string& file)
{ {
bool pReturn = this->initWithFile(file, Rect::ZERO); bool pReturn = this->initWithFile(file, Rect::ZERO);
return pReturn; return pReturn;
} }
Scale9Sprite* Scale9Sprite::create(const char* file) Scale9Sprite* Scale9Sprite::create(const std::string& file)
{ {
Scale9Sprite* pReturn = new Scale9Sprite(); Scale9Sprite* pReturn = new Scale9Sprite();
if ( pReturn && pReturn->initWithFile(file) ) if ( pReturn && pReturn->initWithFile(file) )
@ -578,7 +575,7 @@ Scale9Sprite* Scale9Sprite::createWithSpriteFrame(SpriteFrame* spriteFrame)
return NULL; return NULL;
} }
bool Scale9Sprite::initWithSpriteFrameName(const char* spriteFrameName, const Rect& capInsets) bool Scale9Sprite::initWithSpriteFrameName(const std::string& spriteFrameName, const Rect& capInsets)
{ {
CCASSERT((SpriteFrameCache::getInstance()) != NULL, "SpriteFrameCache::getInstance() must be non-NULL"); CCASSERT((SpriteFrameCache::getInstance()) != NULL, "SpriteFrameCache::getInstance() must be non-NULL");
@ -591,7 +588,7 @@ bool Scale9Sprite::initWithSpriteFrameName(const char* spriteFrameName, const Re
return pReturn; return pReturn;
} }
Scale9Sprite* Scale9Sprite::createWithSpriteFrameName(const char* spriteFrameName, const Rect& capInsets) Scale9Sprite* Scale9Sprite::createWithSpriteFrameName(const std::string& spriteFrameName, const Rect& capInsets)
{ {
Scale9Sprite* pReturn = new Scale9Sprite(); Scale9Sprite* pReturn = new Scale9Sprite();
if ( pReturn && pReturn->initWithSpriteFrameName(spriteFrameName, capInsets) ) if ( pReturn && pReturn->initWithSpriteFrameName(spriteFrameName, capInsets) )
@ -603,16 +600,14 @@ Scale9Sprite* Scale9Sprite::createWithSpriteFrameName(const char* spriteFrameNam
return NULL; return NULL;
} }
bool Scale9Sprite::initWithSpriteFrameName(const char* spriteFrameName) bool Scale9Sprite::initWithSpriteFrameName(const std::string& spriteFrameName)
{ {
bool pReturn = this->initWithSpriteFrameName(spriteFrameName, Rect::ZERO); bool pReturn = this->initWithSpriteFrameName(spriteFrameName, Rect::ZERO);
return pReturn; return pReturn;
} }
Scale9Sprite* Scale9Sprite::createWithSpriteFrameName(const char* spriteFrameName) Scale9Sprite* Scale9Sprite::createWithSpriteFrameName(const std::string& spriteFrameName)
{ {
CCASSERT(spriteFrameName != NULL, "spriteFrameName must be non-NULL");
Scale9Sprite* pReturn = new Scale9Sprite(); Scale9Sprite* pReturn = new Scale9Sprite();
if ( pReturn && pReturn->initWithSpriteFrameName(spriteFrameName) ) if ( pReturn && pReturn->initWithSpriteFrameName(spriteFrameName) )
{ {

View File

@ -75,7 +75,7 @@ public:
* *
* @see initWithFile(const char *file, const Rect& rect, const Rect& capInsets) * @see initWithFile(const char *file, const Rect& rect, const Rect& capInsets)
*/ */
static Scale9Sprite* create(const char* file, const Rect& rect, const Rect& capInsets); static Scale9Sprite* create(const std::string& file, const Rect& rect, const Rect& capInsets);
/** /**
* Creates a 9-slice sprite with a texture file. The whole texture will be * Creates a 9-slice sprite with a texture file. The whole texture will be
@ -83,7 +83,7 @@ public:
* *
* @see initWithFile(const Rect& capInsets, const char *file) * @see initWithFile(const Rect& capInsets, const char *file)
*/ */
static Scale9Sprite* create(const Rect& capInsets, const char* file); static Scale9Sprite* create(const Rect& capInsets, const std::string& file);
/** /**
* Creates a 9-slice sprite with a texture file and a delimitation zone. The * Creates a 9-slice sprite with a texture file and a delimitation zone. The
@ -91,7 +91,7 @@ public:
* *
* @see initWithFile(const char *file, const Rect& rect) * @see initWithFile(const char *file, const Rect& rect)
*/ */
static Scale9Sprite* create(const char* file, const Rect& rect); static Scale9Sprite* create(const std::string& file, const Rect& rect);
/** /**
* Creates a 9-slice sprite with a texture file. The whole texture will be * Creates a 9-slice sprite with a texture file. The whole texture will be
@ -99,7 +99,7 @@ public:
* *
* @see initWithFile(const char *file) * @see initWithFile(const char *file)
*/ */
static Scale9Sprite* create(const char* file); static Scale9Sprite* create(const std::string& file);
/** /**
* Creates a 9-slice sprite with an sprite frame. * Creates a 9-slice sprite with an sprite frame.
@ -129,7 +129,7 @@ public:
* *
* @see initWithSpriteFrameName(const char *spriteFrameName) * @see initWithSpriteFrameName(const char *spriteFrameName)
*/ */
static Scale9Sprite* createWithSpriteFrameName(const char*spriteFrameName); static Scale9Sprite* createWithSpriteFrameName(const std::string& spriteFrameName);
/** /**
* Creates a 9-slice sprite with an sprite frame name and the centre of its * Creates a 9-slice sprite with an sprite frame name and the centre of its
@ -140,7 +140,7 @@ public:
* *
* @see initWithSpriteFrameName(const char *spriteFrameName, const Rect& capInsets) * @see initWithSpriteFrameName(const char *spriteFrameName, const Rect& capInsets)
*/ */
static Scale9Sprite* createWithSpriteFrameName(const char*spriteFrameName, const Rect& capInsets); static Scale9Sprite* createWithSpriteFrameName(const std::string& spriteFrameName, const Rect& capInsets);
/** /**
* Initializes a 9-slice sprite with a texture file, a delimitation zone and * Initializes a 9-slice sprite with a texture file, a delimitation zone and
@ -155,7 +155,7 @@ public:
* texture's full rect. * texture's full rect.
* @param capInsets The values to use for the cap insets. * @param capInsets The values to use for the cap insets.
*/ */
virtual bool initWithFile(const char* file, const Rect& rect, const Rect& capInsets); virtual bool initWithFile(const std::string& file, const Rect& rect, const Rect& capInsets);
/** /**
* Initializes a 9-slice sprite with a texture file and a delimitation zone. The * Initializes a 9-slice sprite with a texture file and a delimitation zone. The
@ -169,7 +169,7 @@ public:
* is the whole image. If the shape is the whole texture, set this to the * is the whole image. If the shape is the whole texture, set this to the
* texture's full rect. * texture's full rect.
*/ */
virtual bool initWithFile(const char* file, const Rect& rect); virtual bool initWithFile(const std::string& file, const Rect& rect);
/** /**
* Initializes a 9-slice sprite with a texture file and with the specified cap * Initializes a 9-slice sprite with a texture file and with the specified cap
@ -181,7 +181,7 @@ public:
* @param file The name of the texture file. * @param file The name of the texture file.
* @param capInsets The values to use for the cap insets. * @param capInsets The values to use for the cap insets.
*/ */
virtual bool initWithFile(const Rect& capInsets, const char* file); virtual bool initWithFile(const Rect& capInsets, const std::string& file);
/** /**
* Initializes a 9-slice sprite with a texture file. The whole texture will be * Initializes a 9-slice sprite with a texture file. The whole texture will be
@ -192,7 +192,7 @@ public:
* *
* @param file The name of the texture file. * @param file The name of the texture file.
*/ */
virtual bool initWithFile(const char* file); virtual bool initWithFile(const std::string& file);
/** /**
* Initializes a 9-slice sprite with an sprite frame and with the specified * Initializes a 9-slice sprite with an sprite frame and with the specified
@ -226,7 +226,7 @@ public:
* @param spriteFrameName The sprite frame name. * @param spriteFrameName The sprite frame name.
* @param capInsets The values to use for the cap insets. * @param capInsets The values to use for the cap insets.
*/ */
virtual bool initWithSpriteFrameName(const char*spriteFrameName, const Rect& capInsets); virtual bool initWithSpriteFrameName(const std::string& spriteFrameName, const Rect& capInsets);
/** /**
* Initializes a 9-slice sprite with an sprite frame name. * Initializes a 9-slice sprite with an sprite frame name.
@ -236,7 +236,7 @@ public:
* *
* @param spriteFrameName The sprite frame name. * @param spriteFrameName The sprite frame name.
*/ */
virtual bool initWithSpriteFrameName(const char*spriteFrameName); virtual bool initWithSpriteFrameName(const std::string& spriteFrameName);
virtual bool init(); virtual bool init();
virtual bool initWithBatchNode(SpriteBatchNode* batchnode, const Rect& rect, bool rotated, const Rect& capInsets); virtual bool initWithBatchNode(SpriteBatchNode* batchnode, const Rect& rect, bool rotated, const Rect& capInsets);

View File

@ -38,11 +38,8 @@ bool UITextFieldTest::init()
_uiLayer->addChild(alert); _uiLayer->addChild(alert);
// Create the textfield // Create the textfield
TextField* textField = TextField::create(); TextField* textField = TextField::create("input words here","fonts/Marker Felt.ttf",30);
textField->setTouchEnabled(true);
textField->setFontName("fonts/Marker Felt.ttf");
textField->setFontSize(30);
textField->setPlaceHolder("input words here");
textField->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f)); textField->setPosition(Point(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
textField->addEventListenerTextField(this, textfieldeventselector(UITextFieldTest::textFieldEvent)); textField->addEventListenerTextField(this, textfieldeventselector(UITextFieldTest::textFieldEvent));
_uiLayer->addChild(textField); _uiLayer->addChild(textField);