doom3-bfg/neo/swf/SWF_Events.cpp

554 lines
17 KiB
C++
Raw Normal View History

2012-11-26 18:58:24 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
2012-11-26 18:58:24 +00:00
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
2012-11-26 18:58:24 +00:00
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#pragma hdrstop
#include "precompiled.h"
2012-11-26 18:58:24 +00:00
/*
===================
idSWF::HitTest
===================
*/
idSWFScriptObject* idSWF::HitTest( idSWFSpriteInstance* spriteInstance, const swfRenderState_t& renderState, int x, int y, idSWFScriptObject* parentObject )
{
if( spriteInstance->parent != NULL )
{
swfDisplayEntry_t* thisDisplayEntry = spriteInstance->parent->FindDisplayEntry( spriteInstance->depth );
if( thisDisplayEntry->cxf.mul.w + thisDisplayEntry->cxf.add.w < 0.001f )
{
2012-11-26 18:58:24 +00:00
return NULL;
}
}
if( !spriteInstance->isVisible )
{
2012-11-26 18:58:24 +00:00
return NULL;
}
if( spriteInstance->scriptObject->HasValidProperty( "onRelease" )
|| spriteInstance->scriptObject->HasValidProperty( "onPress" )
|| spriteInstance->scriptObject->HasValidProperty( "onRollOver" )
|| spriteInstance->scriptObject->HasValidProperty( "onRollOut" )
|| spriteInstance->scriptObject->HasValidProperty( "onDrag" )
)
{
2012-11-26 18:58:24 +00:00
parentObject = spriteInstance->scriptObject;
}
2012-11-26 18:58:24 +00:00
// rather than returning the first object we find, we actually want to return the last object we find
idSWFScriptObject* returnObject = NULL;
2012-11-26 18:58:24 +00:00
float xOffset = spriteInstance->xOffset;
float yOffset = spriteInstance->yOffset;
for( int i = 0; i < spriteInstance->displayList.Num(); i++ )
{
const swfDisplayEntry_t& display = spriteInstance->displayList[i];
idSWFDictionaryEntry* entry = FindDictionaryEntry( display.characterID );
if( entry == NULL )
{
2012-11-26 18:58:24 +00:00
continue;
}
swfRenderState_t renderState2;
renderState2.matrix = display.matrix.Multiply( renderState.matrix );
renderState2.ratio = display.ratio;
if( entry->type == SWF_DICT_SPRITE )
{
idSWFScriptObject* object = HitTest( display.spriteInstance, renderState2, x, y, parentObject );
if( object != NULL && object->Get( "_visible" ).ToBool() )
{
2012-11-26 18:58:24 +00:00
returnObject = object;
}
}
else if( entry->type == SWF_DICT_SHAPE && ( parentObject != NULL ) )
{
idSWFShape* shape = entry->shape;
for( int i = 0; i < shape->fillDraws.Num(); i++ )
{
const idSWFShapeDrawFill& fill = shape->fillDraws[i];
for( int j = 0; j < fill.indices.Num(); j += 3 )
{
idVec2 xy1 = renderState2.matrix.Transform( fill.startVerts[fill.indices[j + 0]] );
idVec2 xy2 = renderState2.matrix.Transform( fill.startVerts[fill.indices[j + 1]] );
idVec2 xy3 = renderState2.matrix.Transform( fill.startVerts[fill.indices[j + 2]] );
2012-11-26 18:58:24 +00:00
idMat3 edgeEquations;
edgeEquations[0].Set( xy1.x + xOffset, xy1.y + yOffset, 1.0f );
edgeEquations[1].Set( xy2.x + xOffset, xy2.y + yOffset, 1.0f );
edgeEquations[2].Set( xy3.x + xOffset, xy3.y + yOffset, 1.0f );
edgeEquations.InverseSelf();
2012-11-26 18:58:24 +00:00
idVec3 p( x, y, 1.0f );
idVec3 signs = p * edgeEquations;
2012-11-26 18:58:24 +00:00
bool bx = signs.x > 0;
bool by = signs.y > 0;
bool bz = signs.z > 0;
if( bx == by && bx == bz )
{
2012-11-26 18:58:24 +00:00
// point inside
returnObject = parentObject;
}
}
}
}
else if( entry->type == SWF_DICT_MORPH )
{
2012-11-26 18:58:24 +00:00
// FIXME: this should be roughly the same as SWF_DICT_SHAPE
}
else if( entry->type == SWF_DICT_TEXT )
{
2012-11-26 18:58:24 +00:00
// FIXME: this should be roughly the same as SWF_DICT_SHAPE
}
else if( entry->type == SWF_DICT_EDITTEXT )
{
idSWFScriptObject* editObject = NULL;
if( display.textInstance->scriptObject.HasProperty( "onRelease" ) || display.textInstance->scriptObject.HasProperty( "onPress" ) )
{
2012-11-26 18:58:24 +00:00
// if the edit box itself can be clicked, then we want to return it when it's clicked on
editObject = &display.textInstance->scriptObject;
}
else if( parentObject != NULL )
{
2012-11-26 18:58:24 +00:00
// otherwise, we want to return the parent object
editObject = parentObject;
}
if( editObject == NULL )
{
2012-11-26 18:58:24 +00:00
continue;
}
if( display.textInstance->text.IsEmpty() )
{
2012-11-26 18:58:24 +00:00
continue;
}
const idSWFEditText* shape = entry->edittext;
const idSWFEditText* text = display.textInstance->GetEditText();
2012-11-26 18:58:24 +00:00
float textLength = display.textInstance->GetTextLength();
2012-11-26 18:58:24 +00:00
float lengthDiff = fabs( shape->bounds.br.x - shape->bounds.tl.x ) - textLength;
idVec3 tl;
idVec3 tr;
idVec3 br;
2012-11-26 18:58:24 +00:00
idVec3 bl;
2012-11-26 18:58:24 +00:00
float xOffset = spriteInstance->xOffset;
float yOffset = spriteInstance->yOffset;
2012-11-26 18:58:24 +00:00
float topOffset = 0.0f;
if( text->align == SWF_ET_ALIGN_LEFT )
{
tl.ToVec2() = renderState2.matrix.Transform( idVec2( shape->bounds.tl.x + xOffset, shape->bounds.tl.y + topOffset + yOffset ) );
tr.ToVec2() = renderState2.matrix.Transform( idVec2( shape->bounds.br.x - lengthDiff + xOffset, shape->bounds.tl.y + topOffset + yOffset ) );
br.ToVec2() = renderState2.matrix.Transform( idVec2( shape->bounds.br.x - lengthDiff + xOffset, shape->bounds.br.y + topOffset + yOffset ) );
bl.ToVec2() = renderState2.matrix.Transform( idVec2( shape->bounds.tl.x + xOffset, shape->bounds.br.y + topOffset + yOffset ) );
}
else if( text->align == SWF_ET_ALIGN_RIGHT )
{
2012-11-26 18:58:24 +00:00
tl.ToVec2() = renderState2.matrix.Transform( idVec2( shape->bounds.tl.x + lengthDiff + xOffset, shape->bounds.tl.y + topOffset + yOffset ) );
tr.ToVec2() = renderState2.matrix.Transform( idVec2( shape->bounds.br.x + xOffset, shape->bounds.tl.y + topOffset + yOffset ) );
br.ToVec2() = renderState2.matrix.Transform( idVec2( shape->bounds.br.x + xOffset, shape->bounds.br.y + topOffset + yOffset ) );
bl.ToVec2() = renderState2.matrix.Transform( idVec2( shape->bounds.tl.x + lengthDiff + xOffset, shape->bounds.br.y + topOffset + yOffset ) );
}
else if( text->align == SWF_ET_ALIGN_CENTER )
{
2012-11-26 18:58:24 +00:00
float middle = ( ( shape->bounds.br.x + xOffset ) + ( shape->bounds.tl.x + xOffset ) ) / 2.0f;
tl.ToVec2() = renderState2.matrix.Transform( idVec2( middle - ( textLength / 2.0f ), shape->bounds.tl.y + topOffset + yOffset ) );
tr.ToVec2() = renderState2.matrix.Transform( idVec2( middle + ( textLength / 2.0f ), shape->bounds.tl.y + topOffset + yOffset ) );
br.ToVec2() = renderState2.matrix.Transform( idVec2( middle + ( textLength / 2.0f ), shape->bounds.br.y + topOffset + yOffset ) );
bl.ToVec2() = renderState2.matrix.Transform( idVec2( middle - ( textLength / 2.0f ), shape->bounds.br.y + topOffset + yOffset ) );
}
else
{
2012-11-26 18:58:24 +00:00
tl.ToVec2() = renderState2.matrix.Transform( idVec2( shape->bounds.tl.x + xOffset, shape->bounds.tl.y + topOffset + yOffset ) );
tr.ToVec2() = renderState2.matrix.Transform( idVec2( shape->bounds.br.x + xOffset, shape->bounds.tl.y + topOffset + yOffset ) );
br.ToVec2() = renderState2.matrix.Transform( idVec2( shape->bounds.br.x + xOffset, shape->bounds.br.y + topOffset + yOffset ) );
bl.ToVec2() = renderState2.matrix.Transform( idVec2( shape->bounds.tl.x + xOffset, shape->bounds.br.y + topOffset + yOffset ) );
}
2012-11-26 18:58:24 +00:00
tl.z = 1.0f;
tr.z = 1.0f;
br.z = 1.0f;
bl.z = 1.0f;
2012-11-26 18:58:24 +00:00
idMat3 edgeEquations;
edgeEquations[0] = tl;
edgeEquations[1] = tr;
edgeEquations[2] = br;
edgeEquations.InverseSelf();
2012-11-26 18:58:24 +00:00
idVec3 p( x, y, 1.0f );
idVec3 signs = p * edgeEquations;
2012-11-26 18:58:24 +00:00
bool bx = signs.x > 0;
bool by = signs.y > 0;
bool bz = signs.z > 0;
if( bx == by && bx == bz )
{
2012-11-26 18:58:24 +00:00
// point inside top right triangle
returnObject = editObject;
}
2012-11-26 18:58:24 +00:00
edgeEquations[0] = tl;
edgeEquations[1] = br;
edgeEquations[2] = bl;
edgeEquations.InverseSelf();
signs = p * edgeEquations;
2012-11-26 18:58:24 +00:00
bx = signs.x > 0;
by = signs.y > 0;
bz = signs.z > 0;
if( bx == by && bx == bz )
{
2012-11-26 18:58:24 +00:00
// point inside bottom left triangle
returnObject = editObject;
}
}
}
return returnObject;
}
/*
===================
idSWF::HandleEvent
===================
*/
bool idSWF::HandleEvent( const sysEvent_t* event )
{
if( !IsLoaded() || !IsActive() || ( !inhibitControl && useInhibtControl ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
if( event->evType == SE_KEY )
{
if( event->evValue == K_MOUSE1 )
{
2012-11-26 18:58:24 +00:00
mouseEnabled = true;
idSWFScriptVar var;
if( event->evValue2 )
{
2012-11-26 18:58:24 +00:00
idSWFScriptVar waitInput = globals->Get( "waitInput" );
if( waitInput.IsFunction() )
{
2012-11-26 18:58:24 +00:00
useMouse = false;
idSWFParmList waitParms;
waitParms.Append( event->evValue );
waitInput.GetFunction()->Call( NULL, waitParms );
waitParms.Clear();
}
else
{
2012-11-26 18:58:24 +00:00
useMouse = true;
}
idSWFScriptObject* hitObject = HitTest( mainspriteInstance, swfRenderState_t(), mouseX, mouseY, NULL );
if( hitObject != NULL )
{
2012-11-26 18:58:24 +00:00
mouseObject = hitObject;
mouseObject->AddRef();
var = hitObject->Get( "onPress" );
if( var.IsFunction() )
{
2012-11-26 18:58:24 +00:00
idSWFParmList parms;
parms.Append( event->inputDevice );
var.GetFunction()->Call( hitObject, parms );
parms.Clear();
return true;
}
2012-11-26 18:58:24 +00:00
idSWFScriptVar var = hitObject->Get( "onDrag" );
if( var.IsFunction() )
{
2012-11-26 18:58:24 +00:00
idSWFParmList parms;
parms.Append( mouseX );
parms.Append( mouseY );
parms.Append( true );
var.GetFunction()->Call( hitObject, parms );
parms.Clear();
return true;
}
}
2012-11-26 18:58:24 +00:00
idSWFParmList parms;
parms.Append( hitObject );
Invoke( "setHitObject", parms );
}
else
{
if( mouseObject )
{
2012-11-26 18:58:24 +00:00
var = mouseObject->Get( "onRelease" );
if( var.IsFunction() )
{
2012-11-26 18:58:24 +00:00
idSWFParmList parms;
parms.Append( mouseObject ); // FIXME: Remove this
var.GetFunction()->Call( mouseObject, parms );
}
2012-11-26 18:58:24 +00:00
mouseObject->Release();
mouseObject = NULL;
}
if( hoverObject )
{
2012-11-26 18:58:24 +00:00
hoverObject->Release();
hoverObject = NULL;
}
if( var.IsFunction() )
{
2012-11-26 18:58:24 +00:00
return true;
}
}
return false;
}
const char* keyName = idKeyInput::KeyNumToString( ( keyNum_t )event->evValue );
2012-11-26 18:58:24 +00:00
idSWFScriptVar var = shortcutKeys->Get( keyName );
// anything more than 32 levels of indirection we can be pretty sure is an infinite loop
for( int runaway = 0; runaway < 32; runaway++ )
{
2012-11-26 18:58:24 +00:00
idSWFParmList eventParms;
eventParms.Clear();
eventParms.Append( event->inputDevice );
if( var.IsString() )
{
2012-11-26 18:58:24 +00:00
// alias to another key
var = shortcutKeys->Get( var.ToString() );
continue;
}
else if( var.IsObject() )
{
2012-11-26 18:58:24 +00:00
// if this object is a sprite, send fake mouse events to it
idSWFScriptObject* object = var.GetObject();
2012-11-26 18:58:24 +00:00
// make sure we don't send an onRelease event unless we have already sent that object an onPress
bool wasPressed = object->Get( "_pressed" ).ToBool();
object->Set( "_pressed", event->evValue2 );
if( event->evValue2 )
{
2012-11-26 18:58:24 +00:00
var = object->Get( "onPress" );
}
else if( wasPressed )
{
2012-11-26 18:58:24 +00:00
var = object->Get( "onRelease" );
}
if( var.IsFunction() )
{
2012-11-26 18:58:24 +00:00
var.GetFunction()->Call( object, eventParms );
return true;
}
}
else if( var.IsFunction() )
{
if( event->evValue2 )
{
2012-11-26 18:58:24 +00:00
// anonymous functions only respond to key down events
var.GetFunction()->Call( NULL, eventParms );
return true;
}
return false;
}
2012-11-26 18:58:24 +00:00
idSWFScriptVar useFunction = globals->Get( "useFunction" );
if( useFunction.IsFunction() && event->evValue2 )
{
const char* action = idKeyInput::GetBinding( event->evValue );
if( idStr::Cmp( "_use", action ) == 0 )
{
2012-11-26 18:58:24 +00:00
useFunction.GetFunction()->Call( NULL, idSWFParmList() );
}
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
idSWFScriptVar waitInput = globals->Get( "waitInput" );
if( waitInput.IsFunction() )
{
2012-11-26 18:58:24 +00:00
useMouse = false;
if( event->evValue2 )
{
2012-11-26 18:58:24 +00:00
idSWFParmList waitParms;
waitParms.Append( event->evValue );
waitInput.GetFunction()->Call( NULL, waitParms );
}
}
else
{
2012-11-26 18:58:24 +00:00
useMouse = true;
}
2012-11-26 18:58:24 +00:00
idSWFScriptVar focusWindow = globals->Get( "focusWindow" );
if( focusWindow.IsObject() )
{
2012-11-26 18:58:24 +00:00
idSWFScriptVar onKey = focusWindow.GetObject()->Get( "onKey" );
if( onKey.IsFunction() )
{
2012-11-26 18:58:24 +00:00
// make sure we don't send an onRelease event unless we have already sent that object an onPress
idSWFScriptObject* object = focusWindow.GetObject();
2012-11-26 18:58:24 +00:00
bool wasPressed = object->Get( "_kpressed" ).ToBool();
object->Set( "_kpressed", event->evValue2 );
if( event->evValue2 || wasPressed )
{
2012-11-26 18:58:24 +00:00
idSWFParmList parms;
parms.Append( event->evValue );
parms.Append( event->evValue2 );
onKey.GetFunction()->Call( focusWindow.GetObject(), parms ).ToBool();
return true;
}
else if( event->evValue == K_LSHIFT || event->evValue == K_RSHIFT )
{
2012-11-26 18:58:24 +00:00
idSWFParmList parms;
parms.Append( event->evValue );
parms.Append( event->evValue2 );
onKey.GetFunction()->Call( focusWindow.GetObject(), parms ).ToBool();
}
}
}
return false;
}
idLib::Warning( "Circular reference in %s shortcutKeys.%s", filename.c_str(), keyName );
}
else if( event->evType == SE_CHAR )
{
2012-11-26 18:58:24 +00:00
idSWFScriptVar focusWindow = globals->Get( "focusWindow" );
if( focusWindow.IsObject() )
{
2012-11-26 18:58:24 +00:00
idSWFScriptVar onChar = focusWindow.GetObject()->Get( "onChar" );
if( onChar.IsFunction() )
{
2012-11-26 18:58:24 +00:00
idSWFParmList parms;
parms.Append( event->evValue );
parms.Append( idKeyInput::KeyNumToString( ( keyNum_t )event->evValue ) );
2012-11-26 18:58:24 +00:00
onChar.GetFunction()->Call( focusWindow.GetObject(), parms ).ToBool();
return true;
}
}
}
else if( event->evType == SE_MOUSE_ABSOLUTE || event->evType == SE_MOUSE )
{
2012-11-26 18:58:24 +00:00
mouseEnabled = true;
isMouseInClientArea = true;
2012-11-26 18:58:24 +00:00
// Mouse position in screen space needs to be converted to SWF space
if( event->evType == SE_MOUSE_ABSOLUTE )
{
2012-11-26 18:58:24 +00:00
const float pixelAspect = renderSystem->GetPixelAspect();
const float sysWidth = renderSystem->GetWidth() * ( pixelAspect > 1.0f ? pixelAspect : 1.0f );
const float sysHeight = renderSystem->GetHeight() / ( pixelAspect < 1.0f ? pixelAspect : 1.0f );
float scale = swfScale * sysHeight / ( float )frameHeight;
2012-11-26 18:58:24 +00:00
float invScale = 1.0f / scale;
float tx = 0.5f * ( sysWidth - ( frameWidth * scale ) );
float ty = 0.5f * ( sysHeight - ( frameHeight * scale ) );
2012-11-26 18:58:24 +00:00
mouseX = idMath::Ftoi( ( static_cast<float>( event->evValue ) - tx ) * invScale );
mouseY = idMath::Ftoi( ( static_cast<float>( event->evValue2 ) - ty ) * invScale );
}
else
{
2012-11-26 18:58:24 +00:00
mouseX += event->evValue;
mouseY += event->evValue2;
2012-11-26 18:58:24 +00:00
mouseX = Max( Min( mouseX, idMath::Ftoi( frameWidth + renderBorder ) ), idMath::Ftoi( 0.0f - renderBorder ) );
mouseY = Max( Min( mouseY, idMath::Ftoi( frameHeight ) ), 0 );
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
bool retVal = false;
idSWFScriptObject* hitObject = HitTest( mainspriteInstance, swfRenderState_t(), mouseX, mouseY, NULL );
if( hitObject != NULL )
{
2012-11-26 18:58:24 +00:00
hasHitObject = true;
}
else
{
2012-11-26 18:58:24 +00:00
hasHitObject = false;
}
if( hitObject != hoverObject )
{
2012-11-26 18:58:24 +00:00
// First check to see if we should call onRollOut on our previous hoverObject
if( hoverObject != NULL )
{
2012-11-26 18:58:24 +00:00
idSWFScriptVar var = hoverObject->Get( "onRollOut" );
if( var.IsFunction() )
{
2012-11-26 18:58:24 +00:00
var.GetFunction()->Call( hoverObject, idSWFParmList() );
retVal = true;
}
hoverObject->Release();
hoverObject = NULL;
}
// Then call onRollOver on our hitObject
if( hitObject != NULL )
{
2012-11-26 18:58:24 +00:00
hoverObject = hitObject;
hoverObject->AddRef();
idSWFScriptVar var = hitObject->Get( "onRollOver" );
if( var.IsFunction() )
{
2012-11-26 18:58:24 +00:00
var.GetFunction()->Call( hitObject, idSWFParmList() );
retVal = true;
}
}
}
if( mouseObject != NULL )
{
2012-11-26 18:58:24 +00:00
idSWFScriptVar var = mouseObject->Get( "onDrag" );
if( var.IsFunction() )
{
2012-11-26 18:58:24 +00:00
idSWFParmList parms;
parms.Append( mouseX );
parms.Append( mouseY );
parms.Append( false );
var.GetFunction()->Call( mouseObject, parms );
return true;
}
}
return retVal;
}
else if( event->evType == SE_MOUSE_LEAVE )
{
2012-11-26 18:58:24 +00:00
isMouseInClientArea = false;
}
else if( event->evType == SE_JOYSTICK )
{
2012-11-26 18:58:24 +00:00
idSWFParmList parms;
parms.Append( event->evValue );
parms.Append( event->evValue2 / 32.0f );
Invoke( "onJoystick", parms );
}
return false;
}