Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1046 lines
25 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "stdafx.h"
  8. // NOTE: This must be the last file included!!!
  9. #include "tier0/memdbgon.h"
  10. using namespace Scaleform::GFx;
  11. using namespace Scaleform::Render;
  12. /*********************************************
  13. * We're going to prime the value caches with some statically allocated
  14. * values and value arrays. We'll start with VALUE_BANK_SIZE individual
  15. * Values, and NUMBER_OF_PRECACHED_x_ELEMENT_ARRAYS for the first few
  16. * arrays
  17. *
  18. * we need to be a little careful when we clean up these caches so that
  19. * we don't try to delete these statically allocated Values.
  20. */
  21. const int VALUE_BANK_SIZE = 32;
  22. const int NUMBER_OF_PRECACHED_2_ELEMENT_ARRAYS = 1;
  23. const int NUMBER_OF_PRECACHED_3_ELEMENT_ARRAYS = 1;
  24. const int NUMBER_OF_PRECACHED_4_ELEMENT_ARRAYS = 1;
  25. const int VALUEARRAY_BANK_SIZE = NUMBER_OF_PRECACHED_2_ELEMENT_ARRAYS * 2 + NUMBER_OF_PRECACHED_3_ELEMENT_ARRAYS * 3 + NUMBER_OF_PRECACHED_4_ELEMENT_ARRAYS * 4;
  26. static Value *VALUE_BANK = NULL;
  27. static Value *VALUE_ARRAY_BANK = NULL;
  28. inline int IndexFromArrayLength( int length )
  29. {
  30. return length - 2;
  31. }
  32. inline int ArrayLengthFromIndex( int index )
  33. {
  34. return index + 2;
  35. }
  36. /*****************************
  37. * internal functions
  38. */
  39. inline bool PtrInValueBank( Value* ptr )
  40. {
  41. return ( ( ptr >= &VALUE_BANK[0] ) && ( ptr < &VALUE_BANK[VALUE_BANK_SIZE] ) );
  42. }
  43. inline bool PtrInValueArrayBank( Value* ptr )
  44. {
  45. return ( ( ptr >= &VALUE_ARRAY_BANK[0] ) && ( ptr < &VALUE_ARRAY_BANK[VALUEARRAY_BANK_SIZE] ) );
  46. }
  47. void ScaleformUIImpl::InitValueImpl( void )
  48. {
  49. VALUE_BANK = new Value[VALUE_BANK_SIZE];
  50. VALUE_ARRAY_BANK = new Value[VALUEARRAY_BANK_SIZE];
  51. for ( int i = 0; i < VALUE_BANK_SIZE; i++ )
  52. {
  53. m_ValueCache.AddToTail( &VALUE_BANK[i] );
  54. }
  55. // initialize the 2, 3, and 4, element array caches with one
  56. // array each.
  57. int element = 0;
  58. for ( int i = 0; i <= 2; i++ )
  59. {
  60. m_ValueArrayCaches[i].AddToTail( &VALUE_ARRAY_BANK[element] );
  61. element += ArrayLengthFromIndex( i );
  62. }
  63. m_pFunctionAdapter = *new ScaleformFunctionHandlerAdapter();
  64. }
  65. void ScaleformUIImpl::ShutdownValueImpl( void )
  66. {
  67. int j = m_ValueCache.Count();
  68. while( j-- )
  69. {
  70. if ( !PtrInValueBank( m_ValueCache[j] ) )
  71. {
  72. delete m_ValueCache[j];
  73. }
  74. }
  75. m_ValueCache.Purge();
  76. for ( int i = 0; i < NUM_VALUEARRAY_SLOTS; i++ )
  77. {
  78. j = m_ValueArrayCaches[i].Count();
  79. while( j-- )
  80. {
  81. if ( !PtrInValueArrayBank( m_ValueArrayCaches[i][j] ) )
  82. {
  83. delete[] m_ValueArrayCaches[i][j];
  84. }
  85. }
  86. m_ValueArrayCaches[i].Purge();
  87. }
  88. m_pFunctionAdapter = NULL;
  89. #if defined( _DEBUG )
  90. bool showBlankLine = true;
  91. if ( m_ValuesInUse.Count() )
  92. {
  93. showBlankLine = false;
  94. LogPrintf( "\nUnreleased Value* handles!\n" );
  95. LogPrintf( " handles (there are %i):\n", m_ValuesInUse.Count() );
  96. // print dangling error
  97. int i = m_ValuesInUse.Count();
  98. while( i-- )
  99. {
  100. LogPrintf( " 0x%p\n", m_ValuesInUse[i] );
  101. if ( !PtrInValueBank( m_ValuesInUse[i] ) )
  102. {
  103. delete m_ValuesInUse[i];
  104. }
  105. }
  106. LogPrintf( "\n" );
  107. m_ValuesInUse.Purge();
  108. }
  109. bool showedHeader = false;
  110. for ( int i = 0; i < NUM_VALUEARRAY_SLOTS; i++ )
  111. {
  112. if ( m_ValueArraysInUse[i].Count() )
  113. {
  114. if ( !showedHeader )
  115. {
  116. showedHeader = true;
  117. if ( showBlankLine )
  118. {
  119. showBlankLine = false;
  120. LogPrintf( "\n" );
  121. }
  122. LogPrintf( "Unreleased SFVALUEARRAY handles!\n" );
  123. }
  124. LogPrintf( " %i element handles (there are %i):\n", ArrayLengthFromIndex( i ), m_ValueArraysInUse[i].Count() );
  125. // print dangling error
  126. int j = m_ValueArraysInUse[i].Count();
  127. while( j-- )
  128. {
  129. LogPrintf( " 0x%p\n", m_ValueArraysInUse[i][j] );
  130. if ( !PtrInValueArrayBank( m_ValueArraysInUse[i][j] ) )
  131. {
  132. delete[] m_ValueArraysInUse[i][j];
  133. }
  134. }
  135. m_ValueArraysInUse[i].Purge();
  136. }
  137. }
  138. if ( showedHeader )
  139. {
  140. LogPrintf( "\n" );
  141. }
  142. #endif
  143. // Intentionally leak VALUE_BANK and VALUE_ARRAY_BANK to workaround crash on Mac @Value::ObjectInterface::ObjectRelease(Value*, void*)
  144. //delete [] VALUE_BANK;
  145. //delete [] VALUE_ARRAY_BANK;
  146. VALUE_BANK = NULL;
  147. VALUE_ARRAY_BANK = NULL;
  148. }
  149. SFVALUE ScaleformUIImpl::CreateGFxValue( SFVALUE value )
  150. {
  151. MEM_ALLOC_CREDIT();
  152. Value* pResult;
  153. Value *pValue = FromSFVALUE( value );
  154. if ( m_ValueCache.Count() )
  155. {
  156. pResult = m_ValueCache.Tail();
  157. m_ValueCache.FastRemove( m_ValueCache.Count()-1 );
  158. if ( pValue )
  159. {
  160. *pResult = *pValue;
  161. }
  162. }
  163. else
  164. {
  165. pResult = ( pValue ) ? new Value( *pValue ) : new Value();
  166. }
  167. #if defined( _DEBUG )
  168. m_ValuesInUse.AddToTail( pResult );
  169. #endif
  170. return ToSFVALUE( pResult );
  171. }
  172. void ScaleformUIImpl::ReleaseGFxValue( SFVALUE value )
  173. {
  174. Value *pValue = FromSFVALUE( value );
  175. pValue->SetNull();
  176. m_ValueCache.AddToTail( pValue );
  177. #if defined( _DEBUG )
  178. m_ValuesInUse.FindAndFastRemove( pValue );
  179. #endif
  180. }
  181. /**********************************
  182. * value creation and release
  183. */
  184. void ScaleformUIImpl::ReleaseValue( SFVALUE value )
  185. {
  186. if ( value != NULL )
  187. {
  188. ReleaseGFxValue( value );
  189. }
  190. }
  191. SFVALUE ScaleformUIImpl::CreateValue( SFVALUE value )
  192. {
  193. return CreateGFxValue( value );
  194. }
  195. SFVALUE ScaleformUIImpl::CreateValue( int value )
  196. {
  197. Value* pResult = FromSFVALUE( CreateGFxValue() );
  198. pResult->SetNumber( ( SF::Double ) value );
  199. return ToSFVALUE( pResult );
  200. }
  201. SFVALUE ScaleformUIImpl::CreateValue( float value )
  202. {
  203. Value* pResult = FromSFVALUE( CreateGFxValue() );
  204. pResult->SetNumber( ( SF::Double ) value );
  205. return ToSFVALUE( pResult );
  206. }
  207. SFVALUE ScaleformUIImpl::CreateValue( bool value )
  208. {
  209. Value* pResult = FromSFVALUE( CreateGFxValue() );
  210. pResult->SetBoolean( value );
  211. return ToSFVALUE( pResult );
  212. }
  213. SFVALUE ScaleformUIImpl::CreateValue( const char* value )
  214. {
  215. Value* pResult = FromSFVALUE( CreateGFxValue() );
  216. pResult->SetString( value );
  217. return ToSFVALUE( pResult );
  218. }
  219. SFVALUE ScaleformUIImpl::CreateValue( const wchar_t* value )
  220. {
  221. Value* pResult = FromSFVALUE( CreateGFxValue() );
  222. pResult->SetStringW( value );
  223. return ToSFVALUE( pResult );
  224. }
  225. void ScaleformUIImpl::Value_SetValue( SFVALUE obj, SFVALUE value )
  226. {
  227. DebugBreakIfNotLocked();
  228. *FromSFVALUE( obj ) = *FromSFVALUE( value );
  229. }
  230. void ScaleformUIImpl::Value_SetValue( SFVALUE obj, int value )
  231. {
  232. DebugBreakIfNotLocked();
  233. FromSFVALUE( obj )->SetNumber( ( SF::Double ) value );
  234. }
  235. void ScaleformUIImpl::Value_SetValue( SFVALUE obj, float value )
  236. {
  237. DebugBreakIfNotLocked();
  238. FromSFVALUE( obj )->SetNumber( ( SF::Double ) value );
  239. }
  240. void ScaleformUIImpl::Value_SetValue( SFVALUE obj, bool value )
  241. {
  242. DebugBreakIfNotLocked();
  243. FromSFVALUE( obj )->SetBoolean( value );
  244. }
  245. void ScaleformUIImpl::Value_SetValue( SFVALUE obj, const char* value )
  246. {
  247. DebugBreakIfNotLocked();
  248. FromSFVALUE( obj )->SetString( value );
  249. }
  250. void ScaleformUIImpl::Value_SetValue( SFVALUE obj, const wchar_t* value )
  251. {
  252. DebugBreakIfNotLocked();
  253. FromSFVALUE( obj )->SetStringW( value );
  254. }
  255. void ScaleformUIImpl::Value_SetText( SFVALUE obj, const char* value )
  256. {
  257. DebugBreakIfNotLocked();
  258. FromSFVALUE( obj )->SetText( value );
  259. }
  260. void ScaleformUIImpl::Value_SetText( SFVALUE obj, const wchar_t* value )
  261. {
  262. DebugBreakIfNotLocked();
  263. FromSFVALUE( obj )->SetText( value );
  264. }
  265. void ScaleformUIImpl::Value_SetTextHTML( SFVALUE obj, const char* value )
  266. {
  267. DebugBreakIfNotLocked();
  268. FromSFVALUE( obj )->SetTextHTML( value );
  269. }
  270. void ScaleformUIImpl::Value_SetTextHTML( SFVALUE obj, const wchar_t* value )
  271. {
  272. DebugBreakIfNotLocked();
  273. FromSFVALUE( obj )->SetTextHTML( value );
  274. }
  275. int ScaleformUIImpl::Value_SetFormattedText( SFVALUE obj, const char* pFormat, ... )
  276. {
  277. DebugBreakIfNotLocked();
  278. va_list marker;
  279. va_start( marker, pFormat );
  280. int len = V_vsnprintf( m_cTemporaryBuffer, TEMPORARY_BUFFER_SIZE, pFormat, marker );
  281. va_end( marker );
  282. Value_SetTextHTML( obj, m_cTemporaryBuffer );
  283. return len;
  284. }
  285. void ScaleformUIImpl::Value_SetColor( SFVALUE obj, int color )
  286. {
  287. Value_SetColor( obj, ( color >> 16 ) & 0xFF, ( color >> 8 ) & 0xFF, ( color ) & 0xFF, ( color >> 24 ) & 0xFF );
  288. }
  289. void ScaleformUIImpl::Value_SetColor( SFVALUE obj, float r, float g, float b, float a )
  290. {
  291. DebugBreakIfNotLocked();
  292. Value *pValue = FromSFVALUE( obj );
  293. Cxform cxform;
  294. cxform.M[0][0] = 0;
  295. cxform.M[0][1] = 0;
  296. cxform.M[0][2] = 0;
  297. cxform.M[0][3] = 0;
  298. cxform.M[1][0] = r * 255.0f;
  299. cxform.M[1][1] = g * 255.0f;
  300. cxform.M[1][2] = b * 255.0f;
  301. cxform.M[1][3] = a * 255.0f;
  302. pValue->SetColorTransform( cxform );
  303. }
  304. void ScaleformUIImpl::Value_SetTint( SFVALUE obj, int color )
  305. {
  306. DebugBreakIfNotLocked();
  307. Value_SetTint( obj,
  308. ( ( color >> 16 ) & 0xFF ) / 255.0f,
  309. ( ( color >> 8 ) & 0xFF ) / 255.0f,
  310. ( color & 0xFF ) / 255.0f,
  311. ( ( color >> 24 ) & 0xFF ) / 255.0f );
  312. }
  313. void ScaleformUIImpl::Value_SetTint( SFVALUE obj, float r, float g, float b, float a )
  314. {
  315. DebugBreakIfNotLocked();
  316. Value *pValue = FromSFVALUE( obj );
  317. Cxform cxform;
  318. cxform.M[0][0] = r;
  319. cxform.M[0][1] = g;
  320. cxform.M[0][2] = b;
  321. cxform.M[0][3] = a;
  322. cxform.M[1][0] = 0;
  323. cxform.M[1][1] = 0;
  324. cxform.M[1][2] = 0;
  325. cxform.M[1][3] = 0;
  326. pValue->SetColorTransform( cxform );
  327. }
  328. void ScaleformUIImpl::Value_SetColorTransform( SFVALUE obj, int colorMultiply, int colorAdd )
  329. {
  330. DebugBreakIfNotLocked();
  331. Cxform cxform;
  332. Value_SetColorTransform( obj,
  333. ( ( colorMultiply >> 16 ) & 0xFF ) / 255.0f,
  334. ( ( colorMultiply >> 8 ) & 0xFF ) / 255.0f,
  335. ( colorMultiply & 0xFF ) / 255.0f,
  336. ( ( colorMultiply >> 24 ) & 0xFF ) / 255.0f,
  337. colorAdd ) ;
  338. }
  339. void ScaleformUIImpl::Value_SetColorTransform( SFVALUE obj, float r, float g, float b, float a, int colorAdd )
  340. {
  341. DebugBreakIfNotLocked();
  342. Cxform cxform;
  343. cxform.M[0][0] = r;
  344. cxform.M[0][1] = g;
  345. cxform.M[0][2] = b;
  346. cxform.M[0][3] = a;
  347. cxform.M[1][0] = ( colorAdd >> 16 ) & 0xFF;
  348. cxform.M[1][1] = ( colorAdd >> 8 )& 0xFF;
  349. cxform.M[1][2] = colorAdd& 0xFF;
  350. cxform.M[1][3] = ( colorAdd >> 24 ) & 0xFF;
  351. FromSFVALUE( obj )->SetColorTransform( cxform );
  352. }
  353. /**********************************
  354. * array manipulation
  355. */
  356. void ScaleformUIImpl::CreateValueArray( SFVALUEARRAY& valueArray, int length )
  357. {
  358. Value* pValues = NULL;
  359. if ( length > 0 && length <= MAX_VALUES_IN_ARRAY )
  360. {
  361. if ( length == 1 )
  362. {
  363. pValues = FromSFVALUE( CreateGFxValue() );
  364. }
  365. else
  366. {
  367. int index = IndexFromArrayLength( length );
  368. int count = m_ValueArrayCaches[index].Count();
  369. if ( count )
  370. {
  371. pValues = m_ValueArrayCaches[index].Tail();
  372. m_ValueArrayCaches[index].RemoveMultipleFromTail( 1 );
  373. }
  374. else
  375. pValues = new Value[length];
  376. #if _DEBUG
  377. // we don't have to do this if the length was 1
  378. // because it will be taken care of by the single
  379. // value allocator.
  380. m_ValueArraysInUse[index].AddToTail( pValues );
  381. #endif
  382. for (int i = 0; i < length; i++ )
  383. {
  384. pValues[i].SetNull();
  385. }
  386. }
  387. }
  388. valueArray.SetValues( length, ToSFVALUE( pValues ) );
  389. }
  390. SFVALUEARRAY ScaleformUIImpl::CreateValueArray( int length )
  391. {
  392. Value* pValues = NULL;
  393. if ( length > 0 && length <= MAX_VALUES_IN_ARRAY )
  394. {
  395. if ( length == 1 )
  396. {
  397. pValues = FromSFVALUE( CreateGFxValue() );
  398. }
  399. else
  400. {
  401. int index = IndexFromArrayLength( length );
  402. int count = m_ValueArrayCaches[index].Count();
  403. if ( count )
  404. {
  405. pValues = m_ValueArrayCaches[index].Tail();
  406. m_ValueArrayCaches[index].RemoveMultipleFromTail( 1 );
  407. }
  408. else
  409. pValues = new Value[length];
  410. #if _DEBUG
  411. // we don't have to do this if the length was 1
  412. // because it will be taken care of by the single
  413. // value allocator.
  414. m_ValueArraysInUse[index].AddToTail( pValues );
  415. #endif
  416. for (int i = 0; i < length; i++ )
  417. {
  418. pValues[i].SetNull();
  419. }
  420. }
  421. }
  422. return SFVALUEARRAY( length, ToSFVALUE( pValues ) );
  423. }
  424. void ScaleformUIImpl::ReleaseValueArray( SFVALUEARRAY& valueArray )
  425. {
  426. if ( valueArray.GetValues() == NULL )
  427. return;
  428. if ( valueArray.Count() == 1 )
  429. {
  430. ReleaseGFxValue( valueArray[0] );
  431. }
  432. else
  433. {
  434. for (int i = 0; i < valueArray.Count(); i++ )
  435. {
  436. FromSFVALUE( valueArray[i] )->SetNull();
  437. }
  438. int index = IndexFromArrayLength( valueArray.Count() );
  439. m_ValueArrayCaches[index].AddToTail( FromSFVALUE( valueArray.GetValues() ) );
  440. #if _DEBUG
  441. // we don't have to do this if the length is 1 because the single value
  442. // releaser will handle that case.
  443. m_ValueArraysInUse[index].FindAndFastRemove( FromSFVALUE( valueArray[0] ) );
  444. #endif
  445. }
  446. valueArray.SetValues(0, NULL);
  447. }
  448. void ScaleformUIImpl::ReleaseValueArray( SFVALUEARRAY& valueArray, int count )
  449. {
  450. Assert(count == valueArray.Count());
  451. ReleaseValueArray(valueArray);
  452. }
  453. SFVALUE ScaleformUIImpl::ValueArray_GetElement( SFVALUEARRAY valueArray, int index )
  454. {
  455. return valueArray[index];
  456. }
  457. IScaleformUI::_ValueType ScaleformUIImpl::ValueArray_GetType( SFVALUEARRAY valueArray, int index )
  458. {
  459. return ValueType_SDK_to_SFUI( FromSFVALUE( valueArray[index] )->GetType() );
  460. }
  461. double ScaleformUIImpl::ValueArray_GetNumber( SFVALUEARRAY valueArray, int index )
  462. {
  463. return FromSFVALUE(valueArray[index])->GetNumber();
  464. }
  465. bool ScaleformUIImpl::ValueArray_GetBool( SFVALUEARRAY valueArray, int index )
  466. {
  467. return FromSFVALUE(valueArray[index])->GetBool();
  468. }
  469. const char* ScaleformUIImpl::ValueArray_GetString( SFVALUEARRAY valueArray, int index )
  470. {
  471. return FromSFVALUE(valueArray[index])->GetString();
  472. }
  473. const wchar_t* ScaleformUIImpl::ValueArray_GetStringW( SFVALUEARRAY valueArray, int index )
  474. {
  475. return FromSFVALUE(valueArray[index])->GetStringW();
  476. }
  477. void ScaleformUIImpl::ValueArray_SetElement( SFVALUEARRAY valueArray, int index, SFVALUE value )
  478. {
  479. *FromSFVALUE(valueArray[index]) = *FromSFVALUE(value);
  480. }
  481. void ScaleformUIImpl::ValueArray_SetElement( SFVALUEARRAY valueArray, int index, int value )
  482. {
  483. FromSFVALUE(valueArray[index])->SetNumber( ( SF::Double ) value );
  484. }
  485. void ScaleformUIImpl::ValueArray_SetElement( SFVALUEARRAY valueArray, int index, float value )
  486. {
  487. FromSFVALUE(valueArray[index])->SetNumber( ( SF::Double ) value );
  488. }
  489. void ScaleformUIImpl::ValueArray_SetElement( SFVALUEARRAY valueArray, int index, bool value )
  490. {
  491. FromSFVALUE(valueArray[index])->SetBoolean( value );
  492. }
  493. void ScaleformUIImpl::ValueArray_SetElement( SFVALUEARRAY valueArray, int index, const char* value )
  494. {
  495. FromSFVALUE(valueArray[index])->SetString( value );
  496. }
  497. void ScaleformUIImpl::ValueArray_SetElement( SFVALUEARRAY valueArray, int index, const wchar_t* value )
  498. {
  499. FromSFVALUE(valueArray[index])->SetStringW( value );
  500. }
  501. void ScaleformUIImpl::ValueArray_SetElementText( SFVALUEARRAY valueArray, int index, const char* value )
  502. {
  503. FromSFVALUE(valueArray[index])->SetText( value );
  504. }
  505. void ScaleformUIImpl::ValueArray_SetElementText( SFVALUEARRAY valueArray, int index, const wchar_t* value )
  506. {
  507. FromSFVALUE(valueArray[index])->SetText( value );
  508. }
  509. void ScaleformUIImpl::ValueArray_SetElementTextHTML( SFVALUEARRAY valueArray, int index, const char* value )
  510. {
  511. FromSFVALUE(valueArray[index])->SetTextHTML( value );
  512. }
  513. void ScaleformUIImpl::ValueArray_SetElementTextHTML( SFVALUEARRAY valueArray, int index, const wchar_t* value )
  514. {
  515. FromSFVALUE(valueArray[index])->SetTextHTML( value );
  516. }
  517. /**********************************
  518. * value manipulation
  519. */
  520. bool ScaleformUIImpl::Value_HasMember( SFVALUE value, const char* name )
  521. {
  522. return FromSFVALUE( value )->HasMember( name );
  523. }
  524. SFVALUE ScaleformUIImpl::Value_GetMember( SFVALUE value, const char* name )
  525. {
  526. Value newValue;
  527. Value* pResult = NULL;
  528. bool worked = FromSFVALUE( value )->GetMember( name, &newValue );
  529. if ( worked )
  530. {
  531. pResult = FromSFVALUE( CreateGFxValue( ToSFVALUE( &newValue ) ) );
  532. }
  533. return ToSFVALUE( pResult );
  534. }
  535. bool ScaleformUIImpl::Value_SetMember( SFVALUE obj, const char *name, SFVALUE value )
  536. {
  537. DebugBreakIfNotLocked();
  538. return FromSFVALUE( obj )->SetMember( name, *FromSFVALUE(value) );
  539. }
  540. bool ScaleformUIImpl::Value_SetMember( SFVALUE obj, const char *name, int value )
  541. {
  542. DebugBreakIfNotLocked();
  543. Value gfxv( ( double ) value );
  544. return FromSFVALUE( obj )->SetMember( name, gfxv );
  545. }
  546. bool ScaleformUIImpl::Value_SetMember( SFVALUE obj, const char *name, float value )
  547. {
  548. DebugBreakIfNotLocked();
  549. Value gfxv( ( double ) value );
  550. return FromSFVALUE( obj )->SetMember( name, gfxv );
  551. }
  552. bool ScaleformUIImpl::Value_SetMember( SFVALUE obj, const char *name, bool value )
  553. {
  554. DebugBreakIfNotLocked();
  555. Value gfxv( value );
  556. return FromSFVALUE( obj )->SetMember( name, gfxv );
  557. }
  558. bool ScaleformUIImpl::Value_SetMember( SFVALUE obj, const char *name, const char* value )
  559. {
  560. DebugBreakIfNotLocked();
  561. Value gfxv( value );
  562. return FromSFVALUE( obj )->SetMember( name, gfxv );
  563. }
  564. bool ScaleformUIImpl::Value_SetMember( SFVALUE obj, const char *name, const wchar_t* value )
  565. {
  566. DebugBreakIfNotLocked();
  567. Value gfxv( value );
  568. return FromSFVALUE( obj )->SetMember( name, gfxv );
  569. }
  570. bool InvokeHelper( Value* pObj, const char* methodName, Value* pArgs, unsigned int numArgs, Value* pResult )
  571. {
  572. MEM_ALLOC_CREDIT();
  573. if ( pArgs && numArgs )
  574. {
  575. return pObj->Invoke( methodName, pResult, pArgs, numArgs );
  576. }
  577. else
  578. {
  579. return pObj->Invoke( methodName, pResult );
  580. }
  581. }
  582. bool ScaleformUIImpl::Value_InvokeWithoutReturn( SFVALUE obj, const char* methodName, const SFVALUEARRAY& args )
  583. {
  584. DebugBreakIfNotLocked();
  585. if ( !obj )
  586. return false;
  587. return InvokeHelper( FromSFVALUE( obj ), methodName, FromSFVALUE( args.GetValues() ), args.Count(), NULL );
  588. }
  589. bool ScaleformUIImpl::Value_InvokeWithoutReturn( SFVALUE obj, const char* methodName, const SFVALUEARRAY& args, int numArgs )
  590. {
  591. Assert(numArgs == args.Count());
  592. if ( numArgs != args.Count() )
  593. {
  594. Warning("Value_Invoke(%s) called with numArgs=%i but SFVALUEARRAY size=%i\n", methodName, numArgs, args.Count());
  595. }
  596. return Value_InvokeWithoutReturn(obj, methodName, args);
  597. }
  598. bool ScaleformUIImpl::Value_InvokeWithoutReturn( SFVALUE obj, const char* methodName, SFVALUE arg, int numArgs )
  599. {
  600. DebugBreakIfNotLocked();
  601. Assert( (numArgs == 0 && arg == NULL ) || (numArgs == 1 && arg != NULL) );
  602. if ( !obj )
  603. return false;
  604. return InvokeHelper( FromSFVALUE( obj ), methodName, FromSFVALUE( arg ), numArgs, NULL );
  605. }
  606. SFVALUE ScaleformUIImpl::Value_Invoke( SFVALUE obj, const char* methodName, const SFVALUEARRAY& args )
  607. {
  608. DebugBreakIfNotLocked();
  609. Value newValue;
  610. Value* pResult = NULL;
  611. bool worked = InvokeHelper( FromSFVALUE( obj ), methodName, FromSFVALUE( args.GetValues() ), args.Count(), &newValue );
  612. if ( worked )
  613. {
  614. pResult = FromSFVALUE( CreateGFxValue( ToSFVALUE( &newValue ) ) );
  615. }
  616. return ToSFVALUE( pResult );
  617. }
  618. SFVALUE ScaleformUIImpl::Value_Invoke( SFVALUE obj, const char* methodName, const SFVALUEARRAY& args, int numArgs )
  619. {
  620. Assert(numArgs == args.Count());
  621. if ( numArgs != args.Count() )
  622. {
  623. Warning("Value_Invoke(%s) called with numArgs=%i but SFVALUEARRAY size=%i\n", methodName, numArgs, args.Count());
  624. }
  625. return Value_Invoke(obj, methodName, args);
  626. }
  627. SFVALUE ScaleformUIImpl::Value_Invoke( SFVALUE obj, const char* methodName, SFVALUE arg, int numArgs )
  628. {
  629. DebugBreakIfNotLocked();
  630. Assert( (numArgs == 0 && arg == NULL ) || (numArgs == 1 && arg != NULL) );
  631. Value newValue;
  632. Value* pResult = NULL;
  633. bool worked = InvokeHelper( FromSFVALUE( obj ), methodName, FromSFVALUE( arg ), numArgs, &newValue );
  634. if ( worked )
  635. {
  636. pResult = FromSFVALUE( CreateGFxValue( ToSFVALUE( &newValue ) ) );
  637. }
  638. return ToSFVALUE( pResult );
  639. }
  640. void ScaleformUIImpl::AddAPIFunctionToObject( SFVALUE pApi, SFMOVIE pMovie, ScaleformUIFunctionHandlerObject* object, const ScaleformUIFunctionHandlerDefinition* pFunctionDef )
  641. {
  642. Value func;
  643. ScaleformCallbackHolder* holder = new ScaleformCallbackHolder( object, pFunctionDef->m_pHandler );
  644. FromSFMOVIE( pMovie )->CreateFunction( &func, m_pFunctionAdapter, ( void* )holder );
  645. func.SetUserData( holder );
  646. FromSFVALUE( pApi )->SetMember( pFunctionDef->m_pName, func );
  647. }
  648. void ScaleformUIImpl::DebugBreakIfNotLocked( void )
  649. {
  650. #if defined ( WIN32 )
  651. // gurjeets - locks commented out, left here for reference, see comment in LockSlotPtr
  652. //if ( !OwnsAtLeastOneMutex() )
  653. //{
  654. // // DebuggerBreakIfDebugging();
  655. // Warning( "WARNING: Some code is calling a scaleform method without any locked mutex! Run with a debugger attached to catch this callstack.\n" );
  656. //}
  657. #endif
  658. }
  659. bool ScaleformUIImpl::OwnsAtLeastOneMutex( void )
  660. {
  661. #if defined ( WIN32 )
  662. // gurjeets - locks commented out, left here for reference, see comment in LockSlotPtr
  663. //for ( int i = 0; i < MAX_SLOTS; i++ )
  664. //{
  665. // if ( m_SlotMutexes[i].IsOwnedByCurrentThread_DebugOnly() )
  666. return true;
  667. //}
  668. #endif
  669. return false;
  670. }
  671. void ScaleformUIImpl::SetVisible( SFVALUE pgfx, bool visible )
  672. {
  673. Value::DisplayInfo info( visible );
  674. FromSFVALUE( pgfx )->SetDisplayInfo( info );
  675. }
  676. void ScaleformUIImpl::Value_SetVisible( SFVALUE obj, bool visible )
  677. {
  678. SetVisible( obj, visible );
  679. }
  680. void ScaleformUIImpl::Value_GetDisplayInfo( SFVALUE obj, ScaleformDisplayInfo* dinfo )
  681. {
  682. Value* pVal = FromSFVALUE( obj );
  683. if ( pVal->GetType() == VT_Null )
  684. {
  685. AssertMsg( 0, "ScaleformUIImpl::Value_GetDisplayInfo: Passed a null typed Value*!" );
  686. Warning( "ScaleformUIImpl::Value_GetDisplayInfo: Passed a null typed Value*!" );
  687. dinfo->Clear();
  688. return;
  689. }
  690. Value::DisplayInfo info;
  691. if ( ( pVal )->GetDisplayInfo( &info ) )
  692. {
  693. dinfo->SetX( info.GetX() );
  694. dinfo->SetY( info.GetY() );
  695. dinfo->SetRotation( info.GetRotation() );
  696. dinfo->SetAlpha( info.GetAlpha() );
  697. dinfo->SetVisibility( info.GetVisible() );
  698. dinfo->SetXScale( info.GetXScale() );
  699. dinfo->SetYScale( info.GetYScale() );
  700. }
  701. dinfo->Clear();
  702. }
  703. void ScaleformUIImpl::Value_SetDisplayInfo( SFVALUE obj, const ScaleformDisplayInfo* dinfo )
  704. {
  705. Value::DisplayInfo info;
  706. if ( dinfo->IsXSet() )
  707. {
  708. info.SetX( dinfo->GetX() );
  709. }
  710. if ( dinfo->IsYSet() )
  711. {
  712. info.SetY( dinfo->GetY() );
  713. }
  714. if ( dinfo->IsRotationSet() )
  715. {
  716. info.SetRotation( dinfo->GetRotation() );
  717. }
  718. if ( dinfo->IsAlphaSet() )
  719. {
  720. info.SetAlpha( dinfo->GetAlpha() );
  721. }
  722. if ( dinfo->IsVisibilitySet() )
  723. {
  724. info.SetVisible( dinfo->GetVisibility() );
  725. }
  726. if ( dinfo->IsXScaleSet() )
  727. {
  728. info.SetXScale( dinfo->GetXScale() );
  729. }
  730. if ( dinfo->IsYScaleSet() )
  731. {
  732. info.SetYScale( dinfo->GetYScale() );
  733. }
  734. Value *pVal = FromSFVALUE( obj );
  735. if ( pVal->GetType() == VT_Null )
  736. {
  737. AssertMsg( 0, "ScaleformUIImpl::Value_SetDisplayInfo: Passed a null typed Value*!" );
  738. Warning( "ScaleformUIImpl::Value_SetDisplayInfo: Passed a null typed Value*!" );
  739. }
  740. else
  741. {
  742. pVal->SetDisplayInfo( info );
  743. }
  744. }
  745. IScaleformUI::_ValueType ScaleformUIImpl::Value_GetType( SFVALUE obj )
  746. {
  747. return ValueType_SDK_to_SFUI( FromSFVALUE( obj )->GetType() );
  748. }
  749. double ScaleformUIImpl::Value_GetNumber( SFVALUE obj )
  750. {
  751. return FromSFVALUE( obj )->GetNumber();
  752. }
  753. bool ScaleformUIImpl::Value_GetBool( SFVALUE obj )
  754. {
  755. return FromSFVALUE( obj )->GetBool();
  756. }
  757. const char* ScaleformUIImpl::Value_GetString( SFVALUE obj )
  758. {
  759. return FromSFVALUE( obj )->GetString();
  760. }
  761. const wchar_t* ScaleformUIImpl::Value_GetStringW( SFVALUE obj )
  762. {
  763. return FromSFVALUE( obj )->GetStringW();
  764. }
  765. SFVALUE ScaleformUIImpl::Value_GetText( SFVALUE obj )
  766. {
  767. Value* pResult = FromSFVALUE( CreateGFxValue() );
  768. FromSFVALUE( obj )->GetText( pResult );
  769. return ToSFVALUE( pResult );
  770. }
  771. SFVALUE ScaleformUIImpl::Value_GetTextHTML( SFVALUE obj )
  772. {
  773. Value* pResult = FromSFVALUE( CreateGFxValue() );
  774. FromSFVALUE( obj )->GetTextHTML( pResult );
  775. return ToSFVALUE( pResult );
  776. }
  777. void ScaleformUIImpl::Value_SetArraySize( SFVALUE obj, int size )
  778. {
  779. FromSFVALUE( obj )->SetArraySize( size );
  780. }
  781. int ScaleformUIImpl::Value_GetArraySize( SFVALUE obj )
  782. {
  783. return ( int )FromSFVALUE( obj )->GetArraySize();
  784. }
  785. void ScaleformUIImpl::Value_ClearArrayElements( SFVALUE obj )
  786. {
  787. FromSFVALUE( obj )->ClearElements();
  788. }
  789. void ScaleformUIImpl::Value_RemoveArrayElement( SFVALUE obj, int index )
  790. {
  791. FromSFVALUE( obj )->RemoveElement( index );
  792. }
  793. void ScaleformUIImpl::Value_RemoveArrayElements( SFVALUE obj, int index, int count )
  794. {
  795. FromSFVALUE( obj )->RemoveElements( index, count );
  796. }
  797. SFVALUE ScaleformUIImpl::Value_GetArrayElement( SFVALUE obj, int index )
  798. {
  799. Value* pResult = FromSFVALUE( CreateGFxValue() );
  800. FromSFVALUE( obj )->GetElement( index, pResult );
  801. return ToSFVALUE( pResult );
  802. }
  803. void ScaleformUIImpl::Value_SetArrayElement( SFVALUE obj, int index, SFVALUE value )
  804. {
  805. Assert( OwnsAtLeastOneMutex() );
  806. FromSFVALUE( obj )->SetElement( index, *FromSFVALUE( value ) );
  807. }
  808. void ScaleformUIImpl::Value_SetArrayElement( SFVALUE obj, int index, int value )
  809. {
  810. Assert( OwnsAtLeastOneMutex() );
  811. Value gv( ( double ) value );
  812. FromSFVALUE( obj )->SetElement( index, gv );
  813. }
  814. void ScaleformUIImpl::Value_SetArrayElement( SFVALUE obj, int index, float value )
  815. {
  816. Assert( OwnsAtLeastOneMutex() );
  817. Value gv( ( double ) value );
  818. FromSFVALUE( obj )->SetElement( index, gv );
  819. }
  820. void ScaleformUIImpl::Value_SetArrayElement( SFVALUE obj, int index, bool value )
  821. {
  822. Assert( OwnsAtLeastOneMutex() );
  823. Value gv( value );
  824. FromSFVALUE( obj )->SetElement( index, gv );
  825. }
  826. void ScaleformUIImpl::Value_SetArrayElement( SFVALUE obj, int index, const char* value )
  827. {
  828. Assert( OwnsAtLeastOneMutex() );
  829. Value gv( value );
  830. FromSFVALUE( obj )->SetElement( index, gv );
  831. }
  832. void ScaleformUIImpl::Value_SetArrayElement( SFVALUE obj, int index, const wchar_t* value )
  833. {
  834. Assert( OwnsAtLeastOneMutex() );
  835. Value gv( value );
  836. FromSFVALUE( obj )->SetElement( index, gv );
  837. }
  838. SFVALUE SFVALUEARRAY::operator[]( int index )
  839. {
  840. Assert(index >= 0 && index < m_count);
  841. if ( index < 0 || index >= m_count )
  842. {
  843. Error("Index out of range for SFVALUEARRAY\n");
  844. }
  845. Value* pValues = FromSFVALUE(m_pValues);
  846. return ToSFVALUE( &pValues[index] );
  847. }