new "manual" conversion for ccFontDefinition in JS and also updated LabelTest.js using this new way of passing parameters to CCLabelTTF through ccFontDefinition

This commit is contained in:
carlo morgantini 2013-05-07 17:22:25 -07:00
parent d77604b958
commit 315999edad
3 changed files with 243 additions and 2 deletions

View File

@ -1236,7 +1236,7 @@ JSBool jsval_to_cccolor3b(JSContext *cx, jsval v, ccColor3B* ret) {
ret->r = r;
ret->g = g;
ret->b = b;
return JS_TRUE;
return JS_TRUE;
}
JSBool jsval_to_ccarray_of_CCPoint(JSContext* cx, jsval v, CCPoint **points, int *numPoints) {
@ -2003,3 +2003,241 @@ void* serverEntryPoint(void*)
pthread_mutex_destroy(&g_qMutex);
return NULL;
}
ccColor3B getColorFromJSObject(JSContext *cx, JSObject *colorObject)
{
jsval jsr;
ccColor3B out;
JS_GetProperty(cx, colorObject, "r", &jsr);
double fontR = 0.0;
JS_ValueToNumber(cx, jsr, &fontR);
JS_GetProperty(cx, colorObject, "g", &jsr);
double fontG = 0.0;
JS_ValueToNumber(cx, jsr, &fontG);
JS_GetProperty(cx, colorObject, "b", &jsr);
double fontB = 0.0;
JS_ValueToNumber(cx, jsr, &fontB);
// the out
out.r = (unsigned char)fontR;
out.g = (unsigned char)fontG;
out.b = (unsigned char)fontB;
return out;
}
CCSize getSizeFromJSObject(JSContext *cx, JSObject *sizeObject)
{
jsval jsr;
CCSize out;
JS_GetProperty(cx, sizeObject, "width", &jsr);
double width = 0.0;
JS_ValueToNumber(cx, jsr, &width);
JS_GetProperty(cx, sizeObject, "height", &jsr);
double height = 0.0;
JS_ValueToNumber(cx, jsr, &height);
// the out
out.width = width;
out.height = height;
return out;
}
JSBool jsval_to_ccfontdefinition( JSContext *cx, jsval vp, ccFontDefinition *out )
{
JSObject *jsobj;
if( ! JS_ValueToObject( cx, vp, &jsobj ) )
return JS_FALSE;
JSB_PRECONDITION( jsobj, "Not a valid JS object");
// defaul values
const char * defautlFontName = "Arial";
const int defaultFontSize = 32;
CCTextAlignment defaultTextAlignment = kCCTextAlignmentLeft;
CCVerticalTextAlignment defaultTextVAlignment = kCCVerticalTextAlignmentTop;
// by default shadow and stroke are off
out->m_shadow.m_shadowEnabled = false;
out->m_stroke.m_strokeEnabled = false;
// white text by default
out->m_fontFillColor = ccWHITE;
// font name
jsval jsr;
JS_GetProperty(cx, jsobj, "FontName", &jsr);
JS_ValueToString(cx, jsr);
JSStringWrapper wrapper(jsr);
if ( wrapper )
{
out->m_fontName = (char*)wrapper;
}
else
{
out->m_fontName = defautlFontName;
}
// font size
JSBool hasProperty;
JS_HasProperty(cx, jsobj, "FontSize", &hasProperty);
if ( hasProperty )
{
JS_GetProperty(cx, jsobj, "FontSize", &jsr);
double fontSize = 0.0;
JS_ValueToNumber(cx, jsr, &fontSize);
out->m_fontSize = fontSize;
}
else
{
out->m_fontSize = defaultFontSize;
}
// font alignment horizontal
JS_HasProperty(cx, jsobj, "FontAlignmentH", &hasProperty);
if ( hasProperty )
{
JS_GetProperty(cx, jsobj, "FontAlignmentH", &jsr);
double fontAlign = 0.0;
JS_ValueToNumber(cx, jsr, &fontAlign);
out->m_alignment = (CCTextAlignment)fontAlign;
}
else
{
out->m_alignment = defaultTextAlignment;
}
// font alignment vertical
JS_HasProperty(cx, jsobj, "FontAlignmentV", &hasProperty);
if ( hasProperty )
{
JS_GetProperty(cx, jsobj, "FontAlignmentV", &jsr);
double fontAlign = 0.0;
JS_ValueToNumber(cx, jsr, &fontAlign);
out->m_vertAlignment = (CCVerticalTextAlignment)fontAlign;
}
else
{
out->m_vertAlignment = defaultTextVAlignment;
}
// font fill color
JS_HasProperty(cx, jsobj, "FontFillColor", &hasProperty);
if ( hasProperty )
{
JS_GetProperty(cx, jsobj, "FontFillColor", &jsr);
JSObject *jsobjColor;
if( ! JS_ValueToObject( cx, jsr, &jsobjColor ) )
return JS_FALSE;
out->m_fontFillColor = getColorFromJSObject(cx, jsobjColor);
}
// font rendering box dimensions
JS_HasProperty(cx, jsobj, "FontDimensions", &hasProperty);
if ( hasProperty )
{
JS_GetProperty(cx, jsobj, "FontDimensions", &jsr);
JSObject *jsobjSize;
if( ! JS_ValueToObject( cx, jsr, &jsobjSize ) )
return JS_FALSE;
out->m_dimensions = getSizeFromJSObject(cx, jsobjSize);
}
// shadow
JS_HasProperty(cx, jsobj, "ShadowEnabled", &hasProperty);
if ( hasProperty )
{
JS_GetProperty(cx, jsobj, "ShadowEnabled", &jsr);
out->m_shadow.m_shadowEnabled = ToBoolean(jsr);
if( out->m_shadow.m_shadowEnabled )
{
// default shadow values
out->m_shadow.m_shadowOffset = CCSize(5, 5);
out->m_shadow.m_shadowBlur = 1;
out->m_shadow.m_shadowOpacity = 1;
// shado offset
JS_HasProperty(cx, jsobj, "ShadowOffset", &hasProperty);
if ( hasProperty )
{
JS_GetProperty(cx, jsobj, "ShadowOffset", &jsr);
JSObject *jsobjShadowOffset;
if( ! JS_ValueToObject( cx, jsr, &jsobjShadowOffset ) )
return JS_FALSE;
out->m_shadow.m_shadowOffset = getSizeFromJSObject(cx, jsobjShadowOffset);
}
// shadow blur
JS_HasProperty(cx, jsobj, "ShadowBlur", &hasProperty);
if ( hasProperty )
{
JS_GetProperty(cx, jsobj, "ShadowBlur", &jsr);
double shadowBlur = 0.0;
JS_ValueToNumber(cx, jsr, &shadowBlur);
out->m_shadow.m_shadowBlur = shadowBlur;
}
// shadow intensity
JS_HasProperty(cx, jsobj, "ShadowOpacity", &hasProperty);
if ( hasProperty )
{
JS_GetProperty(cx, jsobj, "ShadowOpacity", &jsr);
double shadowOpacity = 0.0;
JS_ValueToNumber(cx, jsr, &shadowOpacity);
out->m_shadow.m_shadowOpacity = shadowOpacity;
}
}
}
// stroke
JS_HasProperty(cx, jsobj, "StrokeEnabled", &hasProperty);
if ( hasProperty )
{
JS_GetProperty(cx, jsobj, "StrokeEnabled", &jsr);
out->m_stroke.m_strokeEnabled = ToBoolean(jsr);
if( out->m_stroke.m_strokeEnabled )
{
// default stroke values
out->m_stroke.m_strokeSize = 1;
out->m_stroke.m_strokeColor = ccBLUE;
// stroke color
JS_HasProperty(cx, jsobj, "StrokeColor", &hasProperty);
if ( hasProperty )
{
JS_GetProperty(cx, jsobj, "StrokeColor", &jsr);
JSObject *jsobjStrokeColor;
if( ! JS_ValueToObject( cx, jsr, &jsobjStrokeColor ) )
return JS_FALSE;
out->m_stroke.m_strokeColor = getColorFromJSObject(cx, jsobjStrokeColor);
}
// stroke size
JS_HasProperty(cx, jsobj, "StrokeSize", &hasProperty);
if ( hasProperty )
{
JS_GetProperty(cx, jsobj, "StrokeSize", &jsr);
double strokeSize = 0.0;
JS_ValueToNumber(cx, jsr, &strokeSize);
out->m_stroke.m_strokeSize = strokeSize;
}
}
}
// we are done here
return JS_TRUE;
}

View File

@ -225,6 +225,7 @@ JSBool jsval_to_ccdictionary(JSContext* cx, jsval v, CCDictionary** ret);
JSBool jsval_to_ccacceleration(JSContext* cx,jsval v, CCAcceleration* ret);
JSBool jsvals_variadic_to_ccarray( JSContext *cx, jsval *vp, int argc, CCArray** ret);
JSBool jsval_to_ccaffinetransform(JSContext* cx, jsval v, CCAffineTransform* ret);
JSBool jsval_to_ccfontdefinition( JSContext *cx, jsval vp, ccFontDefinition *out );
// from native
jsval int32_to_jsval( JSContext *cx, int32_t l);

View File

@ -99,7 +99,9 @@ skip = CCNode::[convertToWindowSpace ^setPosition$ getGrid setGLServerState desc
*::[copyWith.* onEnter.* onExit.* ^description$ getObjectType],
CCFileUtils::[(g|s)etSearchResolutionsOrder$ (g|s)etSearchPaths$ getClassTypeInfo],
SimpleAudioEngine::[getClassTypeInfo],
CCApplication::[^application.* ^run$]
CCApplication::[^application.* ^run$],
ccFontDefinition::[*]
rename_functions = CCDirector::[sharedDirector=getInstance],
CCSpriteFrameCache::[sharedSpriteFrameCache=getInstance addSpriteFramesWithFile=addSpriteFrames spriteFrameByName=getSpriteFrame isFlipX=isFlippedX isFlipY=isFlippedY],