1
0
forked from 0ad/0ad

JS GUI interface changes to handle floating-point sizes, plus a few fewer bugs

This was SVN commit r1111.
This commit is contained in:
Ykkrosh 2004-09-03 14:12:43 +00:00
parent 348505ee14
commit c0342b1ff7
2 changed files with 47 additions and 24 deletions

View File

@ -1,4 +1,4 @@
// $Id: JSInterface_GUITypes.cpp,v 1.5 2004/07/24 14:03:16 philip Exp $
// $Id: JSInterface_GUITypes.cpp,v 1.6 2004/09/03 14:12:43 philip Exp $
#include "precompiled.h"
@ -77,28 +77,37 @@ JSBool JSI_GUISize::construct(JSContext* cx, JSObject* obj, unsigned int argc, j
}
// Produces "10", "-10", "50%", "50%-10", "50%+10", etc
CStr ToPercentString(int pix, int per)
CStr ToPercentString(double pix, double per)
{
if (per == 0)
return CStr(pix);
else
return CStr(per)+CStr("%")+( pix == 0 ? CStr() : pix > 0 ? CStr("+")+CStr(pix) : CStr(pix) );
return CStr(per)+CStr("%")+( pix == 0.0 ? CStr() : pix > 0.0 ? CStr("+")+CStr(pix) : CStr(pix) );
}
JSBool JSI_GUISize::toString(JSContext* cx, JSObject* obj, uintN UNUSEDPARAM(argc), jsval* UNUSEDPARAM(argv), jsval* rval)
{
char buffer[256];
snprintf(buffer, 256, "%s %s %s %s",
ToPercentString(JSVAL_TO_INT(g_ScriptingHost.GetObjectProperty(obj, "left" )),
JSVAL_TO_INT(g_ScriptingHost.GetObjectProperty(obj, "rleft" ))).c_str(),
ToPercentString(JSVAL_TO_INT(g_ScriptingHost.GetObjectProperty(obj, "top" )),
JSVAL_TO_INT(g_ScriptingHost.GetObjectProperty(obj, "rtop" ))).c_str(),
ToPercentString(JSVAL_TO_INT(g_ScriptingHost.GetObjectProperty(obj, "right" )),
JSVAL_TO_INT(g_ScriptingHost.GetObjectProperty(obj, "rright" ))).c_str(),
ToPercentString(JSVAL_TO_INT(g_ScriptingHost.GetObjectProperty(obj, "bottom" )),
JSVAL_TO_INT(g_ScriptingHost.GetObjectProperty(obj, "rbottom" ))).c_str() );
CStr buffer;
*rval = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, buffer));
try
{
#define SIDE(side) buffer += ToPercentString(g_ScriptingHost.GetObjectProperty_Double(obj, #side), g_ScriptingHost.GetObjectProperty_Double(obj, "r"#side));
SIDE(left);
buffer += " ";
SIDE(top);
buffer += " ";
SIDE(right);
buffer += " ";
SIDE(bottom);
#undef SIDE
}
catch (PSERROR_Scripting_ConversionFailed)
{
*rval = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, "<Error converting value to numbers>"));
return JS_TRUE;
}
*rval = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, buffer.c_str()));
return JS_TRUE;
}

View File

@ -1,4 +1,4 @@
// $Id: JSInterface_IGUIObject.cpp,v 1.11 2004/09/02 03:03:13 gee Exp $
// $Id: JSInterface_IGUIObject.cpp,v 1.12 2004/09/03 14:12:43 philip Exp $
#include "precompiled.h"
@ -131,8 +131,10 @@ JSBool JSI_IGUIObject::getProperty(JSContext* cx, JSObject* obj, jsval id, jsval
CClientArea area;
GUI<CClientArea>::GetSetting(e, propName, area);
JSObject* obj = JS_NewObject(cx, &JSI_GUISize::JSI_class, NULL, NULL);
#define P(x, y, z) jsval z = INT_TO_JSVAL(area.x.y); JS_SetProperty(cx, obj, #z, &z)
JS_AddRoot(cx, obj);
try
{
#define P(x, y, z) g_ScriptingHost.SetObjectProperty_Double(obj, #z, area.x.y)
P(pixel, left, left);
P(pixel, top, top);
P(pixel, right, right);
@ -142,8 +144,16 @@ JSBool JSI_IGUIObject::getProperty(JSContext* cx, JSObject* obj, jsval id, jsval
P(percent, right, rright);
P(percent, bottom, rbottom);
#undef P
}
catch (PSERROR_Scripting_ConversionFailed)
{
debug_warn("Error creating size object!");
JS_RemoveRoot(cx, obj);
break;
}
*vp = OBJECT_TO_JSVAL(obj);
JS_RemoveRoot(cx, obj);
break;
}
@ -169,7 +179,7 @@ JSBool JSI_IGUIObject::getProperty(JSContext* cx, JSObject* obj, jsval id, jsval
default:
JS_ReportError(cx, "Setting '%s' uses an unimplemented type", propName.c_str());
assert(! "This shouldn't happen");
debug_warn("This shouldn't happen");
return JS_FALSE;
}
@ -267,12 +277,16 @@ JSBool JSI_IGUIObject::setProperty(JSContext* cx, JSObject* obj, jsval id, jsval
GUI<CClientArea>::GetSetting(e, propName, area);
JSObject* obj = JSVAL_TO_OBJECT(*vp);
jsval t; int32 s;
#define PROP(x) JS_GetProperty(cx, obj, #x, &t); \
JS_ValueToInt32(cx, t, &s); \
area.pixel.x = s
PROP(left); PROP(top); PROP(right); PROP(bottom);
#undef PROP
#define P(x, y, z) area.x.y = (float)g_ScriptingHost.GetObjectProperty_Double(obj, #z)
P(pixel, left, left);
P(pixel, top, top);
P(pixel, right, right);
P(pixel, bottom, bottom);
P(percent, left, rleft);
P(percent, top, rtop);
P(percent, right, rright);
P(percent, bottom, rbottom);
#undef P
GUI<CClientArea>::SetSetting(e, propName, area);
}