FxGameDelegate.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**********************************************************************
  2. Filename : FxGameDelegate.cpp
  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. #include "FxGameDelegate.h"
  11. //
  12. // Visitor to register callbacks
  13. //
  14. class AddCallbackVisitor : public FxDelegateHandler::CallbackProcessor
  15. {
  16. public:
  17. AddCallbackVisitor(FxDelegateHandler* pthis, FxDelegate::CallbackHash* phash)
  18. : pThis(pthis), pHash(phash) {}
  19. void Process(const GString& methodName, FxDelegateHandler::CallbackFn method)
  20. {
  21. FxDelegate::CallbackDefn cbt;
  22. cbt.pThis = pThis;
  23. cbt.pCallback = method;
  24. pHash->Add(methodName, cbt);
  25. }
  26. private:
  27. FxDelegateHandler* pThis;
  28. FxDelegate::CallbackHash* pHash;
  29. };
  30. //
  31. // Visitor to unregister callbacks
  32. //
  33. class RemoveCallbackVisitor : public FxDelegateHandler::CallbackProcessor
  34. {
  35. public:
  36. RemoveCallbackVisitor(FxDelegateHandler* pthis, FxDelegate::CallbackHash* phash)
  37. : pThis(pthis), pHash(phash) {}
  38. void Process(const GString& methodName, FxDelegateHandler::CallbackFn method)
  39. {
  40. GUNUSED(method);
  41. pHash->Remove(methodName);
  42. }
  43. private:
  44. FxDelegateHandler* pThis;
  45. FxDelegate::CallbackHash* pHash;
  46. };
  47. //////////////////////////////////////////////////////////////////////////
  48. FxDelegate::FxDelegate()
  49. {
  50. }
  51. void FxDelegate::RegisterHandler(FxDelegateHandler* callback)
  52. {
  53. AddCallbackVisitor reg(callback, &Callbacks);
  54. callback->Accept(&reg);
  55. }
  56. void FxDelegate::UnregisterHandler(FxDelegateHandler* callback)
  57. {
  58. RemoveCallbackVisitor reg(callback, &Callbacks);
  59. callback->Accept(&reg);
  60. }
  61. void FxDelegate::Invoke(GFxMovieView* pmovieView, const char* methodName,
  62. FxResponseArgsBase& args)
  63. {
  64. GFxValue* pv = NULL;
  65. UInt nv = args.GetValues(&pv);
  66. pv[0] = methodName;
  67. pmovieView->Invoke("call", pv, nv);
  68. }
  69. void FxDelegate::Invoke2(GFxMovieView* pmovieView, const char* methodName,
  70. FxResponseArgsBase& args)
  71. {
  72. GFxValue* pv = NULL;
  73. UInt nv = args.GetValues(&pv);
  74. pmovieView->Invoke(methodName, NULL, pv+1, nv-1);
  75. }
  76. void FxDelegate::Callback(GFxMovieView* pmovieView, const char* methodName, const GFxValue* args, UInt argCount)
  77. {
  78. // KevinJ: With calling ExternalInterface from flash, this is apparently obsolete now
  79. // GASSERT(argCount > 0); // Must at least have a uid parameter
  80. CallbackDefn* pcb = Callbacks.GetAlt(methodName);
  81. if (pcb != NULL)
  82. {
  83. // KevinJ: With calling ExternalInterface from flash, this is apparently obsolete now
  84. // FxDelegateArgs params(args[0],
  85. // pcb->pThis,
  86. // pmovieView,
  87. // &args[1],
  88. // argCount - 1);
  89. FxDelegateArgs params(GFxValue(),
  90. pcb->pThis,
  91. pmovieView,
  92. argCount>=1 ? &args[0] : NULL,
  93. argCount);
  94. pcb->pCallback(params);
  95. }
  96. }
  97. //////////////////////////////////////////////////////////////////////////
  98. void FxDelegateArgs::Respond(FxResponseArgsBase& params) const
  99. {
  100. GFxValue* pv = NULL;
  101. UInt nv = params.GetValues(&pv);
  102. pv[0] = ResponseID;
  103. pMovieView->Invoke("respond", pv, nv);
  104. }
粤ICP备19079148号