label JS fixes

This commit is contained in:
Ricardo Quesada 2015-12-02 17:23:23 -08:00
parent 6e50cf883f
commit 48523572b7
3 changed files with 75 additions and 54 deletions

View File

@ -154,7 +154,7 @@ public:
Label* Label::create() Label* Label::create()
{ {
auto ret = new (std::nothrow) Label(); auto ret = new (std::nothrow) Label;
if (ret) if (ret)
{ {
@ -200,21 +200,13 @@ Label* Label::createWithTTF(const std::string& text, const std::string& fontFile
{ {
auto ret = new (std::nothrow) Label(hAlignment,vAlignment); auto ret = new (std::nothrow) Label(hAlignment,vAlignment);
if (ret && FileUtils::getInstance()->isFileExist(fontFile)) if (ret && ret->initWithTTF(text, fontFile, fontSize, dimensions, hAlignment, vAlignment))
{ {
TTFConfig ttfConfig(fontFile.c_str(),fontSize,GlyphCollection::DYNAMIC);
if (ret->setTTFConfig(ttfConfig))
{
ret->setDimensions(dimensions.width,dimensions.height);
ret->setString(text);
ret->autorelease(); ret->autorelease();
return ret; return ret;
} }
}
delete ret; CC_SAFE_DELETE(ret);
return nullptr; return nullptr;
} }
@ -222,16 +214,13 @@ Label* Label::createWithTTF(const TTFConfig& ttfConfig, const std::string& text,
{ {
auto ret = new (std::nothrow) Label(hAlignment); auto ret = new (std::nothrow) Label(hAlignment);
if (ret && FileUtils::getInstance()->isFileExist(ttfConfig.fontFilePath) && ret->setTTFConfig(ttfConfig)) if (ret && ret->initWithTTF(ttfConfig, text, hAlignment, maxLineWidth))
{ {
ret->setMaxLineWidth(maxLineWidth);
ret->setString(text);
ret->autorelease(); ret->autorelease();
return ret; return ret;
} }
delete ret; CC_SAFE_DELETE(ret);
return nullptr; return nullptr;
} }
@ -310,6 +299,34 @@ bool Label::setCharMap(const std::string& plistFile)
return true; return true;
} }
bool Label::initWithTTF(const std::string& text, const std::string& fontFilePath, float fontSize,
const Size& dimensions, TextHAlignment hAlignment, TextVAlignment vAlignment)
{
if (FileUtils::getInstance()->isFileExist(fontFilePath))
{
TTFConfig ttfConfig(fontFilePath, fontSize, GlyphCollection::DYNAMIC);
if (setTTFConfig(ttfConfig))
{
setDimensions(dimensions.width, dimensions.height);
setString(text);
}
return true;
}
return false;
}
bool Label::initWithTTF(const TTFConfig& ttfConfig, const std::string& text, TextHAlignment hAlignment, int maxLineWidth)
{
if (FileUtils::getInstance()->isFileExist(ttfConfig.fontFilePath) && setTTFConfig(ttfConfig))
{
setMaxLineWidth(maxLineWidth);
setString(text);
return true;
}
return false;
}
bool Label::setCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap) bool Label::setCharMap(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
{ {
auto newAtlas = FontAtlasCache::getFontAtlasCharMap(texture,itemWidth,itemHeight,startCharMap); auto newAtlas = FontAtlasCache::getFontAtlasCharMap(texture,itemWidth,itemHeight,startCharMap);

View File

@ -55,7 +55,7 @@ typedef struct _ttfConfig
bool distanceFieldEnabled; bool distanceFieldEnabled;
int outlineSize; int outlineSize;
_ttfConfig(const char* filePath = "",float size = 12, const GlyphCollection& glyphCollection = GlyphCollection::DYNAMIC, _ttfConfig(const std::string& filePath = "",float size = 12, const GlyphCollection& glyphCollection = GlyphCollection::DYNAMIC,
const char *customGlyphCollection = nullptr, bool useDistanceField = false, int outline = 0) const char *customGlyphCollection = nullptr, bool useDistanceField = false, int outline = 0)
: fontFilePath(filePath) : fontFilePath(filePath)
, fontSize(size) , fontSize(size)
@ -540,6 +540,13 @@ CC_CONSTRUCTOR_ACCESS:
*/ */
virtual ~Label(); virtual ~Label();
bool initWithTTF(const std::string& text, const std::string& fontFilePath, float fontSize,
const Size& dimensions = Size::ZERO, TextHAlignment hAlignment = TextHAlignment::LEFT,
TextVAlignment vAlignment = TextVAlignment::TOP);
bool initWithTTF(const TTFConfig& ttfConfig, const std::string& text,
TextHAlignment hAlignment = TextHAlignment::LEFT, int maxLineWidth = 0);
protected: protected:
struct LetterInfo struct LetterInfo
{ {

View File

@ -3765,7 +3765,7 @@ bool js_cocos2dx_ccquatMultiply(JSContext *cx, uint32_t argc, jsval *vp)
} }
// TODO: This function is deprecated. The new API is "new Sprite" instead of "Sprite.create" // TODO: This function is deprecated. The new API is "new Sprite" instead of "Sprite.create"
// There are not js tests for this function. Impossible to know wether it works Ok. // There are not js tests for this function. Impossible to know weather it works Ok.
bool js_cocos2dx_Sprite_create(JSContext *cx, uint32_t argc, jsval *vp) bool js_cocos2dx_Sprite_create(JSContext *cx, uint32_t argc, jsval *vp)
{ {
JS::CallArgs args = JS::CallArgsFromVp(argc, vp); JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
@ -4764,6 +4764,8 @@ bool jsval_to_TTFConfig(JSContext *cx, jsval v, TTFConfig* ret) {
return true; return true;
} }
// TODO: This function is deprecated. The new API is "new Label" instead of "Label.create"
// There are not js tests for this function. Impossible to know weather it works Ok.
bool js_cocos2dx_Label_createWithTTF(JSContext *cx, uint32_t argc, jsval *vp) bool js_cocos2dx_Label_createWithTTF(JSContext *cx, uint32_t argc, jsval *vp)
{ {
if (argc < 2) if (argc < 2)
@ -4778,49 +4780,44 @@ bool js_cocos2dx_Label_createWithTTF(JSContext *cx, uint32_t argc, jsval *vp)
ok &= jsval_to_TTFConfig(cx, args.get(0), &ttfConfig); ok &= jsval_to_TTFConfig(cx, args.get(0), &ttfConfig);
ok &= jsval_to_std_string(cx, args.get(1), &text); ok &= jsval_to_std_string(cx, args.get(1), &text);
cocos2d::Label* ret = nullptr; cocos2d::Label* label = nullptr;
if (argc == 2) { if (argc == 2) {
JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_Label_createWithTTF : Error processing arguments"); JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_Label_createWithTTF : Error processing arguments");
ret = cocos2d::Label::createWithTTF(ttfConfig, text); label = new (std::nothrow) cocos2d::Label;
jsval jsret = JSVAL_NULL; label->initWithTTF(ttfConfig, text);
if (ret) {
js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::Label>(cx, (cocos2d::Label*)ret);
jsret = OBJECT_TO_JSVAL(proxy->obj);
} }
args.rval().set(jsret); else if (argc == 3)
return true; {
}
if (argc == 3) {
int arg2; int arg2;
ok &= jsval_to_int32(cx, args.get(2), (int32_t *)&arg2); ok &= jsval_to_int32(cx, args.get(2), (int32_t *)&arg2);
JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_Label_createWithTTF : Error processing arguments"); JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_Label_createWithTTF : Error processing arguments");
TextHAlignment alignment = TextHAlignment(arg2); TextHAlignment alignment = TextHAlignment(arg2);
ret = cocos2d::Label::createWithTTF(ttfConfig, text, alignment); label = new (std::nothrow) cocos2d::Label;
jsval jsret = JSVAL_NULL; label->initWithTTF(ttfConfig, text, alignment);
if (ret) {
js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::Label>(cx, (cocos2d::Label*)ret);
jsret = OBJECT_TO_JSVAL(proxy->obj);
} }
args.rval().set(jsret); else if (argc == 4)
return true; {
}
if (argc == 4) {
int arg2,arg3; int arg2,arg3;
ok &= jsval_to_int32(cx, args.get(2), (int32_t *)&arg2); ok &= jsval_to_int32(cx, args.get(2), (int32_t *)&arg2);
ok &= jsval_to_int32(cx, args.get(3), (int32_t *)&arg3); ok &= jsval_to_int32(cx, args.get(3), (int32_t *)&arg3);
JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_Label_createWithTTF : Error processing arguments"); JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_Label_createWithTTF : Error processing arguments");
TextHAlignment alignment = TextHAlignment(arg2); TextHAlignment alignment = TextHAlignment(arg2);
ret = cocos2d::Label::createWithTTF(ttfConfig, text, alignment, arg3); label = new (std::nothrow) cocos2d::Label;
jsval jsret = JSVAL_NULL; label->initWithTTF(ttfConfig, text, alignment, arg3);
if (ret) {
js_proxy_t *proxy = js_get_or_create_proxy<cocos2d::Label>(cx, (cocos2d::Label*)ret);
jsret = OBJECT_TO_JSVAL(proxy->obj);
} }
args.rval().set(jsret);
if (ok)
{
js_type_class_t *typeClass = js_get_type_from_native<cocos2d::Label>(label);
// link the native object with the javascript object
JS::RootedObject jsobj(cx, jsb_ref_create_jsobject(cx, label, typeClass, "cocos2d::Label"));
args.rval().set(OBJECT_TO_JSVAL(jsobj));
return true; return true;
} }
// else
JS_ReportError(cx, "js_cocos2dx_Label_createWithTTF : wrong number of arguments"); JS_ReportError(cx, "js_cocos2dx_Label_createWithTTF : wrong number of arguments");
return false; return false;
} }