This was SVN commit r1737.

This commit is contained in:
MarkT 2005-01-18 10:52:22 +00:00
parent 60afb0ee2d
commit 1b961ec163
3 changed files with 68 additions and 0 deletions

View File

@ -13,6 +13,7 @@ class CStrW;
class CScriptObject;
class CObjectEntry;
class CVector3D;
class CPlayer;
// -----
//
@ -105,6 +106,10 @@ template<> jsval ToJSVal<CObjectEntry>( CObjectEntry*& Native );
template<> HEntity* ToNative<HEntity>( JSContext* cx, JSObject* obj );
template<> JSObject* ToScript<HEntity>( HEntity* Native );
// CPlayer*
template<> bool ToPrimitive<CPlayer*>( JSContext* cx, jsval v, CPlayer*& Storage );
template<> JSObject* ToScript<CPlayer*>( CPlayer** Native );
// CScriptObject
template<> bool ToPrimitive<CScriptObject>( JSContext* cx, jsval v, CScriptObject& Storage );
template<> jsval ToJSVal<CScriptObject>( CScriptObject& Native );

View File

@ -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<jsval, &SColour::ToString>( "toString", 0 );
CJSObject<SColour>::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<float>( argv[3] );
SColour* col = new SColour( ToPrimitive<float>( argv[0] ),
ToPrimitive<float>( argv[1] ),
ToPrimitive<float>( argv[2] ),
alpha );
col->m_EngineOwned = false;
*rval = OBJECT_TO_JSVAL( col->GetScript() );
return( JS_TRUE );
}

View File

@ -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<SColour>
{
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