eventManager_ScriptBinding.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. ConsoleMethodGroupBeginWithDocs(EventManager, SimObject)
  23. /*!
  24. Register an event with the event manager.
  25. @param event The event to register.
  26. @return Whether or not the event was registered successfully.
  27. */
  28. ConsoleMethodWithDocs( EventManager, registerEvent, ConsoleBool, 3, 3, ( String event ))
  29. {
  30. return object->registerEvent( argv[2] );
  31. }
  32. /*!
  33. Remove an event from the EventManager.
  34. @param event The event to remove.
  35. */
  36. ConsoleMethodWithDocs( EventManager, unregisterEvent, ConsoleVoid, 3, 3, ( String event ))
  37. {
  38. object->unregisterEvent( argv[2] );
  39. }
  40. /*!
  41. Check if an event is registered or not.
  42. @param event The event to check.
  43. @return Whether or not the event exists.
  44. */
  45. ConsoleMethodWithDocs( EventManager, isRegisteredEvent, ConsoleBool, 3, 3, ( String event ))
  46. {
  47. return object->isRegisteredEvent( argv[2] );
  48. }
  49. /*!
  50. Trigger an event.
  51. @param event The event to trigger.
  52. @param data The data associated with the event.
  53. @return Whether or not the event was dispatched successfully.
  54. */
  55. ConsoleMethodWithDocs( EventManager, postEvent, ConsoleBool, 3, 4, ( String event, String data ))
  56. {
  57. return object->postEvent( argv[2], argc > 3 ? argv[3] : "" );
  58. }
  59. /*!
  60. Subscribe a listener to an event.
  61. @param listener The listener to subscribe.
  62. @param event The event to subscribe to.
  63. @param callback Optional method name to receive the event notification. If this is not specified, \on[event]\ will be used.
  64. @return Whether or not the subscription was successful.
  65. */
  66. ConsoleMethodWithDocs( EventManager, subscribe, ConsoleBool, 4, 5, ( SimObject listener, String event, String callback ))
  67. {
  68. // Find the listener object.
  69. SimObject *cbObj = dynamic_cast<SimObject *>(Sim::findObject(argv[2]));
  70. if( cbObj == NULL )
  71. {
  72. Con::warnf( "EventManager::subscribe - Invalid listener." );
  73. return false;
  74. }
  75. return object->subscribe( cbObj, argv[3], argc > 4 ? argv[4] : NULL );
  76. }
  77. /*!
  78. Remove a listener from an event.
  79. @param listener The listener to remove.
  80. @param event The event to be removed from.
  81. */
  82. ConsoleMethodWithDocs( EventManager, remove, ConsoleVoid, 4, 4, ( SimObject listener, String event ))
  83. {
  84. // Find the listener object.
  85. SimObject * listener = dynamic_cast< SimObject * >( Sim::findObject( argv[2] ) );
  86. if( listener )
  87. object->remove( listener, argv[3] );
  88. }
  89. /*!
  90. Print all registered events to the console.
  91. */
  92. ConsoleMethodWithDocs( EventManager, dumpEvents, ConsoleVoid, 2, 2, ())
  93. {
  94. object->dumpEvents();
  95. }
  96. /*!
  97. Print all subscribers to an event to the console.
  98. @param event The event whose subscribers are to be printed. If this parameter isn't specified, all events will be dumped.
  99. */
  100. ConsoleMethodWithDocs( EventManager, dumpSubscribers, ConsoleVoid, 2, 3, ( String event ))
  101. {
  102. if( argc > 2 )
  103. object->dumpSubscribers( argv[2] );
  104. else
  105. object->dumpSubscribers();
  106. }
  107. ConsoleMethodGroupEndWithDocs(EventManager)