FxGameDelegate.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**********************************************************************
  2. Filename : FxGameDelegate.h
  3. Content : Communication logic for CLIK GameDelegate
  4. Created :
  5. Authors : Prasad Silva
  6. Copyright : (c) 2005-2009 Scaleform Corp. All Rights Reserved.
  7. This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
  8. THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR ANY PURPOSE.
  9. **********************************************************************/
  10. #ifndef INC_FxGameDelegateHandler_H
  11. #define INC_FxGameDelegateHandler_H
  12. #include "GFxPlayer.h"
  13. #include "GMemory.h"
  14. class FxDelegate;
  15. class FxDelegateArgs;
  16. //
  17. // Interface implemented by all callback handlers. These handlers register
  18. // callbacks with the FxGameDelegate.
  19. //
  20. class FxDelegateHandler : public GRefCountBase<FxDelegateHandler, GStat_Default_Mem>
  21. {
  22. public:
  23. virtual ~FxDelegateHandler() {}
  24. //
  25. // All callback methods must have the following signature. To produce a response
  26. // to the callback, push parameters to the game delegate.
  27. //
  28. typedef void (*CallbackFn)(const FxDelegateArgs& params);
  29. //
  30. // Interface implemented by callback registrars. The handler should
  31. // pass the appropriate parameters to the Visit method.
  32. //
  33. class CallbackProcessor
  34. {
  35. public:
  36. virtual ~CallbackProcessor() {}
  37. virtual void Process(const GString& methodName, CallbackFn method) = 0;
  38. };
  39. //
  40. // Callback registrar visitor method
  41. // Implementations are expected to call the registrar's Process method
  42. // for all callbacks.
  43. //
  44. virtual void Accept(CallbackProcessor* cbreg) = 0;
  45. };
  46. //////////////////////////////////////////////////////////////////////////
  47. //
  48. // Callback response parameters
  49. // The
  50. //
  51. class FxResponseArgsBase
  52. {
  53. public:
  54. virtual ~FxResponseArgsBase() {}
  55. virtual UInt GetValues(GFxValue** pparams) = 0;
  56. };
  57. //
  58. // Callback response that uses stack based storage
  59. //
  60. template <int N>
  61. class FxResponseArgs : public FxResponseArgsBase
  62. {
  63. public:
  64. FxResponseArgs() : Index(1) {}
  65. void Add(const GFxValue& v)
  66. {
  67. if (Index > N)
  68. {
  69. GFC_DEBUG_WARNING(1, "Adding parameter out of bounds!");
  70. return;
  71. }
  72. Values[Index++] = v;
  73. }
  74. UInt GetValues(GFxValue** pparams) { *pparams = Values; return Index; }
  75. private:
  76. GFxValue Values[N+1]; // Space for response data
  77. UInt Index;
  78. };
  79. //
  80. // Callback response that uses dynamically allocated storage
  81. //
  82. class FxResponseArgsList : public FxResponseArgsBase
  83. {
  84. public:
  85. FxResponseArgsList() { Args.PushBack(GFxValue::VT_Null); } // Space for response data
  86. void Add(const GFxValue& v) { Args.PushBack(v); }
  87. UInt GetValues(GFxValue** pparams) { *pparams = &Args[0]; return (UInt)Args.GetSize(); }
  88. private:
  89. GArray<GFxValue> Args;
  90. };
  91. //////////////////////////////////////////////////////////////////////////
  92. //
  93. // Parameters passed to the callback handler
  94. //
  95. class FxDelegateArgs
  96. {
  97. public:
  98. FxDelegateArgs(GFxValue responseid, FxDelegateHandler* pthis, GFxMovieView* pmovie,
  99. const GFxValue* vals, UInt nargs) : ResponseID(responseid), pThis(pthis),
  100. pMovieView(pmovie), Args(vals), NArgs(nargs) {}
  101. void Respond(FxResponseArgsBase& params) const;
  102. FxDelegateHandler* GetHandler() const { return pThis; }
  103. GFxMovieView* GetMovie() const { return pMovieView; }
  104. const GFxValue& operator[](UPInt i) const
  105. {
  106. GASSERT(i >= 0 && i < NArgs);
  107. return Args[i];
  108. }
  109. UInt GetArgCount() const { return NArgs; }
  110. private:
  111. GFxValue ResponseID;
  112. FxDelegateHandler* pThis;
  113. GFxMovieView* pMovieView;
  114. const GFxValue* Args;
  115. UInt NArgs;
  116. };
  117. //////////////////////////////////////////////////////////////////////////
  118. //
  119. // Callback manager that marshals calls from ActionScript
  120. //
  121. class FxDelegate : public GFxExternalInterface
  122. {
  123. public:
  124. //
  125. // Callback target
  126. //
  127. struct CallbackDefn
  128. {
  129. GPtr<FxDelegateHandler> pThis;
  130. FxDelegateHandler::CallbackFn pCallback;
  131. };
  132. //
  133. // Callback hash
  134. //
  135. struct CallbackHashFunctor
  136. {
  137. UPInt operator()(const char* data) const
  138. {
  139. UPInt size = G_strlen(data);
  140. return GString::BernsteinHashFunction(data, size);
  141. }
  142. };
  143. typedef GHash<GString, CallbackDefn, CallbackHashFunctor> CallbackHash;
  144. FxDelegate();
  145. //
  146. // Install and uninstall callbacks
  147. //
  148. void RegisterHandler(FxDelegateHandler* callback);
  149. void UnregisterHandler(FxDelegateHandler* callback);
  150. //
  151. // Call a method registered with the AS2 GameDelegate instance
  152. //
  153. static void Invoke(GFxMovieView* pmovieView, const char* methodName,
  154. FxResponseArgsBase& args);
  155. // KevinJ: Workaround interface changes
  156. static void Invoke2(GFxMovieView* pmovieView, const char* methodName,
  157. FxResponseArgsBase& args);
  158. //
  159. // ExternalInterface callback entry point
  160. //
  161. void Callback(GFxMovieView* pmovieView, const char* methodName,
  162. const GFxValue* args, UInt argCount);
  163. private:
  164. //
  165. // Callbacks installed with the game delegate
  166. //
  167. CallbackHash Callbacks;
  168. };
  169. #endif // INC_FxGameDelegateHandler_H
粤ICP备19079148号