diff --git a/source/scripting/JSConversions.h b/source/scripting/JSConversions.h index 374b9c2535..841225e4c4 100755 --- a/source/scripting/JSConversions.h +++ b/source/scripting/JSConversions.h @@ -13,6 +13,7 @@ class CStrW; class CScriptObject; class CObjectEntry; class CVector3D; +class CPlayer; // ----- // @@ -105,6 +106,10 @@ template<> jsval ToJSVal( CObjectEntry*& Native ); template<> HEntity* ToNative( JSContext* cx, JSObject* obj ); template<> JSObject* ToScript( HEntity* Native ); +// CPlayer* +template<> bool ToPrimitive( JSContext* cx, jsval v, CPlayer*& Storage ); +template<> JSObject* ToScript( CPlayer** Native ); + // CScriptObject template<> bool ToPrimitive( JSContext* cx, jsval v, CScriptObject& Storage ); template<> jsval ToJSVal( CScriptObject& Native ); diff --git a/source/scripting/ScriptCustomTypes.cpp b/source/scripting/ScriptCustomTypes.cpp index b5e12939b7..b6dd2da406 100755 --- a/source/scripting/ScriptCustomTypes.cpp +++ b/source/scripting/ScriptCustomTypes.cpp @@ -1,6 +1,7 @@ #include "precompiled.h" #include "ScriptingHost.h" +#include "ScriptCustomTypes.h" // POINT2D @@ -37,3 +38,51 @@ JSBool Point2d_Constructor(JSContext* UNUSEDPARAM(cx), JSObject* obj, uintN argc return JS_TRUE; } +// Colour + +void SColour::SColourInit( float _r, float _g, float _b, float _a ) +{ + AddProperty( L"r", &r ); + AddProperty( L"g", &g ); + AddProperty( L"b", &b ); + AddProperty( L"a", &a ); + + r = _r; g = _g; b = _b; a = _a; +} + +void SColour::ScriptingInit() +{ + AddMethod( "toString", 0 ); + CJSObject::ScriptingInit( "Colour", SColour::Construct, 3 ); +} + +jsval SColour::ToString( JSContext* cx, uintN argc, jsval* argv ) +{ + wchar_t buffer[256]; + + swprintf( buffer, 256, L"[object Colour: ( %f, %f, %f, %f )]", r, g, b, a ); + buffer[255] = 0; + + utf16string str16(buffer, buffer+wcslen(buffer)); + + return( STRING_TO_JSVAL( JS_NewUCStringCopyZ( cx, buffer ) ) ); +} + + +JSBool SColour::Construct( JSContext* cx, JSObject* obj, unsigned int argc, jsval* argv, jsval* rval ) +{ + assert( argc >= 3 ); + float alpha = 1.0; + if( argc >= 4 ) alpha = ToPrimitive( argv[3] ); + + SColour* col = new SColour( ToPrimitive( argv[0] ), + ToPrimitive( argv[1] ), + ToPrimitive( argv[2] ), + alpha ); + + col->m_EngineOwned = false; + + *rval = OBJECT_TO_JSVAL( col->GetScript() ); + + return( JS_TRUE ); +} \ No newline at end of file diff --git a/source/scripting/ScriptCustomTypes.h b/source/scripting/ScriptCustomTypes.h index 1b387f9c64..c45ac1f156 100755 --- a/source/scripting/ScriptCustomTypes.h +++ b/source/scripting/ScriptCustomTypes.h @@ -1,3 +1,4 @@ +#include "scripting/ScriptableObject.h" #ifndef _SCRIPTCUSTOMTYPES_H_ #define _SCRIPTCUSTOMTYPES_H_ @@ -12,6 +13,19 @@ extern JSClass Point2dClass; extern JSPropertySpec Point2dProperties[]; JSBool Point2d_Constructor(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval); +// Colour +struct SColour : public CJSObject +{ +public: + float r, g, b, a; /* 0...1 */ + SColour() { SColourInit( 0.0f, 0.0f, 0.0f, 0.0f ); } + SColour( float r, float g, float b ) { SColourInit( r, g, b, 1.0f ); } + SColour( float r, float g, float b, float a ) { SColourInit( r, g, b, a ); } + void SColourInit( float r, float g, float b, float a ); + jsval ToString( JSContext* cx, uintN argc, jsval* argv ); + static void ScriptingInit(); + static JSBool Construct( JSContext* cx, JSObject* obj, unsigned int argc, jsval* argv, jsval* rval ); +}; #endif