1
0
forked from 0ad/0ad
0ad/source/scripting/DOMEvent.cpp
janwas c0ed950657 had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).

it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.

after several hours, the code now requires fewer casts and less
guesswork.

other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.

This was SVN commit r5942.
2008-05-11 18:48:32 +00:00

195 lines
5.1 KiB
C++

#include "precompiled.h"
#include "DOMEvent.h"
#include "lib/timer.h"
#include "ps/Profile.h"
#include "simulation/ScriptObject.h"
IEventTarget::~IEventTarget()
{
HandlerMap::iterator it;
for( it = m_Handlers_name.begin(); it != m_Handlers_name.end(); it++ )
delete( it->second );
}
bool IEventTarget::_DispatchEvent( CScriptEvent* evt, IEventTarget* target )
{
PROFILE_START( "_DispatchEvent" );
// TODO: Deal correctly with multiple handlers
if( before && before->_DispatchEvent( evt, target ) )
{
return( true ); // Stop propagation.
}
evt->m_CurrentTarget = this;
HandlerList::const_iterator it;
const HandlerList &handlers=m_Handlers_id[evt->m_TypeCode];
for( it = handlers.begin(); it != handlers.end(); it++ )
{
DOMEventHandler id = *it;
if( id && id->DispatchEvent( GetScriptExecContext( target ), evt ) )
{
return( true );
}
}
HandlerRange range = m_Handlers_name.equal_range( evt->m_Type );
HandlerMap::iterator itm;
for( itm = range.first; itm != range.second; itm++ )
{
DOMEventHandler id = itm->second;
if( id && id->DispatchEvent( GetScriptExecContext( target ), evt ) )
{
return( true );
}
}
if( after && after->_DispatchEvent( evt, target ) )
{
return( true ); // Stop propagation.
}
return( false );
PROFILE_END( "_DispatchEvent" );
}
// Dispatch an event to its handler.
// returns: whether the event arrived (i.e. wasn't cancelled) [bool]
bool IEventTarget::DispatchEvent( CScriptEvent* evt )
{
char* data;
PROFILE_START( "intern string" );
data = (char*) g_Profiler.InternString( "script: " + (CStr8)evt->m_Type );
PROFILE_END( "intern string" );
g_Profiler.StartScript( data );
evt->m_Target = this;
_DispatchEvent( evt, this );
g_Profiler.Stop();
return( !evt->m_Cancelled );
}
bool IEventTarget::AddHandler( size_t TypeCode, DOMEventHandler handler )
{
HandlerList::iterator it;
for( it = m_Handlers_id[TypeCode].begin(); it != m_Handlers_id[TypeCode].end(); it++ )
if( **it == *handler ) return( false );
m_Handlers_id[TypeCode].push_back( handler );
return( true );
}
bool IEventTarget::AddHandler( const CStrW& TypeString, DOMEventHandler handler )
{
HandlerMap::iterator it;
HandlerRange range = m_Handlers_name.equal_range( TypeString );
for( it = range.first; it != range.second; it++ )
if( *( it->second ) == *handler ) return( false );
m_Handlers_name.insert( HandlerMap::value_type( TypeString, handler ) );
return( true );
}
bool IEventTarget::RemoveHandler( size_t TypeCode, DOMEventHandler handler )
{
HandlerList::iterator it;
for( it = m_Handlers_id[TypeCode].begin(); it != m_Handlers_id[TypeCode].end(); it++ )
if( **it == *handler )
{
m_Handlers_id[TypeCode].erase( it );
return( true );
}
return( false );
}
bool IEventTarget::RemoveHandler( const CStrW& TypeString, DOMEventHandler handler )
{
HandlerMap::iterator it;
HandlerRange range = m_Handlers_name.equal_range( TypeString );
for( it = range.first; it != range.second; it++ )
if( *( it->second ) == *handler )
{
delete( it->second );
m_Handlers_name.erase( it );
return( true );
}
return( false );
}
bool IEventTarget::AddHandlerJS( JSContext* UNUSED(cx), uintN argc, jsval* argv )
{
debug_assert( argc >= 2 );
DOMEventHandler handler = new CScriptObject( argv[1] );
if( !handler->Defined() )
{
delete( handler );
return( false );
}
if( !AddHandler( ToPrimitive<CStrW>( argv[0] ), handler ) )
{
delete( handler );
return( false );
}
return( true );
}
bool IEventTarget::RemoveHandlerJS( JSContext* UNUSED(cx), uintN argc, jsval* argv )
{
debug_assert( argc >= 2 );
DOMEventHandler handler = new CScriptObject( argv[1] );
if( !handler->Defined() )
{
delete( handler );
return( false );
}
if( !RemoveHandler( ToPrimitive<CStrW>( argv[0] ), handler ) )
{
delete( handler );
return( false );
}
delete( handler );
return( true );
}
CScriptEvent::CScriptEvent( const CStrW& Type, size_t TypeCode, bool Cancelable, bool Blockable )
{
m_Type = Type; m_TypeCode = TypeCode;
m_Cancelable = Cancelable; m_Cancelled = false;
m_Blockable = Blockable; m_Blocked = false;
m_Timestamp = (long)( timer_Time() * 1000.0 );
}
void CScriptEvent::ScriptingInit()
{
AddMethod<CStr, &CScriptEvent::ToString>( "toString", 0 );
AddMethod<void, &CScriptEvent::PreventDefault>( "preventDefault", 0 );
AddMethod<void, &CScriptEvent::PreventDefault>( "cancel", 0 );
AddMethod<void, &CScriptEvent::StopPropagation>( "stopPropagation", 0 );
AddProperty( L"type", &CScriptEvent::m_Type, true );
AddProperty( L"cancelable", &CScriptEvent::m_Cancelable, true );
AddProperty( L"blockable", &CScriptEvent::m_Blockable, true );
AddProperty( L"timestamp", &CScriptEvent::m_Timestamp, true );
CJSObject<CScriptEvent>::ScriptingInit( "Event" );
}
void CScriptEvent::PreventDefault( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) )
{
if( m_Cancelable )
m_Cancelled = true;
}
void CScriptEvent::StopPropagation( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) )
{
if( m_Blockable )
m_Blocked = true;
}
CStr CScriptEvent::ToString( JSContext* UNUSED(cx), uintN UNUSED(argc), jsval* UNUSED(argv) )
{
return "[object Event: " + CStr(m_Type) + "]";
}