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()
{
auto ret = new (std::nothrow) Label();
auto ret = new (std::nothrow) Label;
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);
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();
return ret;
}
ret->autorelease();
return ret;
}
delete ret;
CC_SAFE_DELETE(ret);
return nullptr;
}
@ -222,16 +214,13 @@ Label* Label::createWithTTF(const TTFConfig& ttfConfig, const std::string& text,
{
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();
return ret;
}
delete ret;
CC_SAFE_DELETE(ret);
return nullptr;
}
@ -310,6 +299,34 @@ bool Label::setCharMap(const std::string& plistFile)
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)
{
auto newAtlas = FontAtlasCache::getFontAtlasCharMap(texture,itemWidth,itemHeight,startCharMap);

View File

@ -55,21 +55,21 @@ typedef struct _ttfConfig
bool distanceFieldEnabled;
int outlineSize;
_ttfConfig(const char* filePath = "",float size = 12, const GlyphCollection& glyphCollection = GlyphCollection::DYNAMIC,
const char *customGlyphCollection = nullptr,bool useDistanceField = false,int outline = 0)
:fontFilePath(filePath)
,fontSize(size)
,glyphs(glyphCollection)
,customGlyphs(customGlyphCollection)
,distanceFieldEnabled(useDistanceField)
,outlineSize(outline)
_ttfConfig(const std::string& filePath = "",float size = 12, const GlyphCollection& glyphCollection = GlyphCollection::DYNAMIC,
const char *customGlyphCollection = nullptr, bool useDistanceField = false, int outline = 0)
: fontFilePath(filePath)
, fontSize(size)
, glyphs(glyphCollection)
, customGlyphs(customGlyphCollection)
, distanceFieldEnabled(useDistanceField)
, outlineSize(outline)
{
if(outline > 0)
{
distanceFieldEnabled = false;
}
}
}TTFConfig;
} TTFConfig;
class Sprite;
class SpriteBatchNode;
@ -540,6 +540,13 @@ CC_CONSTRUCTOR_ACCESS:
*/
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:
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"
// 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)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
@ -4764,6 +4764,8 @@ bool jsval_to_TTFConfig(JSContext *cx, jsval v, TTFConfig* ret) {
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)
{
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_std_string(cx, args.get(1), &text);
cocos2d::Label* ret = nullptr;
cocos2d::Label* label = nullptr;
if (argc == 2) {
JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_Label_createWithTTF : Error processing arguments");
ret = cocos2d::Label::createWithTTF(ttfConfig, text);
jsval jsret = JSVAL_NULL;
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);
return true;
label = new (std::nothrow) cocos2d::Label;
label->initWithTTF(ttfConfig, text);
}
if (argc == 3) {
else if (argc == 3)
{
int arg2;
ok &= jsval_to_int32(cx, args.get(2), (int32_t *)&arg2);
JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_Label_createWithTTF : Error processing arguments");
TextHAlignment alignment = TextHAlignment(arg2);
ret = cocos2d::Label::createWithTTF(ttfConfig, text, alignment);
jsval jsret = JSVAL_NULL;
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);
return true;
label = new (std::nothrow) cocos2d::Label;
label->initWithTTF(ttfConfig, text, alignment);
}
if (argc == 4) {
else if (argc == 4)
{
int arg2,arg3;
ok &= jsval_to_int32(cx, args.get(2), (int32_t *)&arg2);
ok &= jsval_to_int32(cx, args.get(3), (int32_t *)&arg3);
JSB_PRECONDITION2(ok, cx, false, "js_cocos2dx_Label_createWithTTF : Error processing arguments");
TextHAlignment alignment = TextHAlignment(arg2);
ret = cocos2d::Label::createWithTTF(ttfConfig, text, alignment, arg3);
jsval jsret = JSVAL_NULL;
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);
label = new (std::nothrow) cocos2d::Label;
label->initWithTTF(ttfConfig, text, alignment, arg3);
}
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;
}
// else
JS_ReportError(cx, "js_cocos2dx_Label_createWithTTF : wrong number of arguments");
return false;
}