2004-07-22 18:19:37 +02:00
|
|
|
// JavaScript interface to native code selection and group objects
|
|
|
|
|
|
|
|
// Mark Thompson (mot20@cam.ac.uk / mark@wildfiregames.com)
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
#include "JSInterface_Selection.h"
|
2004-10-07 21:23:35 +02:00
|
|
|
#include "scripting/JSCollection.h"
|
2004-07-22 18:19:37 +02:00
|
|
|
#include "Interact.h"
|
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
JSBool JSI_Selection::getSelection( JSContext* context, JSObject* globalObject, jsval id, jsval* vp )
|
|
|
|
{
|
2004-10-07 21:23:35 +02:00
|
|
|
*vp = OBJECT_TO_JSVAL( EntityCollection::CreateReference( &( g_Selection.m_selected ) ) );
|
2004-08-03 01:14:54 +02:00
|
|
|
|
2004-07-22 18:19:37 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
JSBool JSI_Selection::setSelection( JSContext* context, JSObject* globalObject, jsval id, jsval* vp )
|
2004-07-22 18:19:37 +02:00
|
|
|
{
|
2004-08-03 01:14:54 +02:00
|
|
|
if( !JSVAL_IS_OBJECT( *vp ) )
|
2004-07-22 18:19:37 +02:00
|
|
|
{
|
2004-10-07 21:23:35 +02:00
|
|
|
JS_ReportError( context, "Not a valid Collection" );
|
2004-08-03 01:14:54 +02:00
|
|
|
*vp = JSVAL_NULL;
|
2004-07-22 18:19:37 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
2004-10-07 21:23:35 +02:00
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
JSObject* selectionArray = JSVAL_TO_OBJECT( *vp );
|
2004-10-07 21:23:35 +02:00
|
|
|
EntityCollection::CJSCollectionData* Info = (EntityCollection::CJSCollectionData*)JS_GetInstancePrivate( context, JSVAL_TO_OBJECT( *vp ), &EntityCollection::JSI_class, NULL );
|
2004-08-03 01:14:54 +02:00
|
|
|
|
2004-10-07 21:23:35 +02:00
|
|
|
if( !Info )
|
2004-07-22 18:19:37 +02:00
|
|
|
{
|
2004-10-07 21:23:35 +02:00
|
|
|
JS_ReportError( context, "Not a valid Collection" );
|
2004-08-03 01:14:54 +02:00
|
|
|
*vp = JSVAL_NULL;
|
2004-07-22 18:19:37 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
2004-08-03 01:14:54 +02:00
|
|
|
|
|
|
|
g_Selection.clearSelection();
|
|
|
|
std::vector<HEntity>::iterator it;
|
|
|
|
|
2004-10-07 21:23:35 +02:00
|
|
|
for( it = Info->m_Data->begin(); it < Info->m_Data->end(); it++ )
|
|
|
|
g_Selection.addSelection( *it );
|
2005-04-22 09:12:55 +02:00
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool JSI_Selection::getGroups( JSContext* context, JSObject* obj, jsval id, jsval* vp )
|
|
|
|
{
|
|
|
|
JSObject* groupsArray = JS_NewArrayObject( context, 0, NULL );
|
|
|
|
|
2004-12-23 23:24:09 +01:00
|
|
|
JS_AddRoot( context, &groupsArray );
|
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
for( i8 groupId = 0; groupId < MAX_GROUPS; groupId++ )
|
|
|
|
{
|
2004-10-07 21:23:35 +02:00
|
|
|
jsval v = OBJECT_TO_JSVAL( EntityCollection::CreateReference( &( g_Selection.m_groups[groupId] ) ) );
|
2004-08-03 01:14:54 +02:00
|
|
|
JS_SetElement( context, groupsArray, groupId, &v );
|
2004-07-22 18:19:37 +02:00
|
|
|
}
|
2004-08-03 01:14:54 +02:00
|
|
|
|
|
|
|
*vp = OBJECT_TO_JSVAL( groupsArray );
|
2004-12-23 23:24:09 +01:00
|
|
|
|
|
|
|
JS_RemoveRoot( context, &groupsArray );
|
|
|
|
|
2004-07-22 18:19:37 +02:00
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
JSBool JSI_Selection::setGroups( JSContext* context, JSObject* obj, jsval id, jsval* vp )
|
|
|
|
{
|
|
|
|
JSObject* groupsArray;
|
|
|
|
if( !JSVAL_IS_OBJECT( *vp ) || !JS_IsArrayObject( context, groupsArray = JSVAL_TO_OBJECT( *vp ) ) )
|
|
|
|
{
|
|
|
|
JS_ReportError( context, "Not a valid group array" );
|
|
|
|
*vp = JSVAL_NULL;
|
|
|
|
return( JS_TRUE );
|
|
|
|
}
|
|
|
|
for( i8 groupId = 0; groupId < MAX_GROUPS; groupId++ )
|
|
|
|
{
|
|
|
|
jsval v;
|
|
|
|
if( JS_GetElement( context, groupsArray, groupId, &v ) && JSVAL_IS_OBJECT( v ) )
|
|
|
|
{
|
|
|
|
JSObject* group = JSVAL_TO_OBJECT( v );
|
2004-10-07 21:23:35 +02:00
|
|
|
EntityCollection::CJSCollectionData* Info = (EntityCollection::CJSCollectionData*)JS_GetInstancePrivate( context, group, &EntityCollection::JSI_class, NULL );
|
|
|
|
if( Info )
|
2004-08-03 01:14:54 +02:00
|
|
|
{
|
|
|
|
g_Selection.m_groups[groupId].clear();
|
|
|
|
std::vector<HEntity>::iterator it;
|
|
|
|
|
2004-10-07 21:23:35 +02:00
|
|
|
for( it = Info->m_Data->begin(); it < Info->m_Data->end(); it++ )
|
|
|
|
g_Selection.addToGroup( groupId, *it );
|
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-10-07 21:23:35 +02:00
|
|
|
|
2004-08-03 01:14:54 +02:00
|
|
|
return( JS_TRUE );
|
2004-10-07 21:23:35 +02:00
|
|
|
}
|