scriptObjects.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. #include "platform/platform.h"
  23. #include "console/simBase.h"
  24. #include "console/consoleTypes.h"
  25. #include "console/scriptObjects.h"
  26. #include "console/simBase.h"
  27. #include "console/engineAPI.h"
  28. IMPLEMENT_CONOBJECT(ScriptObject);
  29. ConsoleDocClass( ScriptObject,
  30. "@brief A script-level OOP object which allows binding of a class, superClass and arguments along with declaration of methods.\n\n"
  31. "ScriptObjects are extrodinarily powerful objects that allow defining of any type of data required. They can optionally have\n"
  32. "a class and a superclass defined for added control of multiple ScriptObjects through a simple class definition.\n\n"
  33. "@tsexample\n"
  34. "new ScriptObject(Game)\n"
  35. "{\n"
  36. " class = \"DeathMatchGame\";\n"
  37. " superClass = GameCore;\n"
  38. " genre = \"Action FPS\"; // Note the new, non-Torque variable\n"
  39. "};\n"
  40. "@endtsexample\n"
  41. "@see SimObject\n"
  42. "@ingroup Console\n"
  43. "@ingroup Scripting"
  44. );
  45. IMPLEMENT_CALLBACK( ScriptObject, onAdd, void, ( SimObjectId ID ), ( ID ),
  46. "Called when this ScriptObject is added to the system.\n"
  47. "@param ID Unique object ID assigned when created (%this in script).\n"
  48. );
  49. IMPLEMENT_CALLBACK( ScriptObject, onRemove, void, ( SimObjectId ID ), ( ID ),
  50. "Called when this ScriptObject is removed from the system.\n"
  51. "@param ID Unique object ID assigned when created (%this in script).\n"
  52. );
  53. ScriptObject::ScriptObject()
  54. {
  55. }
  56. bool ScriptObject::onAdd()
  57. {
  58. if (!Parent::onAdd())
  59. return false;
  60. // Call onAdd in script!
  61. onAdd_callback(getId());
  62. return true;
  63. }
  64. void ScriptObject::onRemove()
  65. {
  66. // We call this on this objects namespace so we unlink them after. - jdd
  67. //
  68. // Call onRemove in script!
  69. onRemove_callback(getId());
  70. Parent::onRemove();
  71. }
  72. //-----------------------------------------------------------------------------
  73. // Script group placeholder
  74. //-----------------------------------------------------------------------------
  75. class ScriptGroup : public SimGroup
  76. {
  77. typedef SimGroup Parent;
  78. public:
  79. ScriptGroup();
  80. bool onAdd();
  81. void onRemove();
  82. DECLARE_CONOBJECT(ScriptGroup);
  83. DECLARE_CALLBACK(void, onAdd, (SimObjectId ID) );
  84. DECLARE_CALLBACK(void, onRemove, (SimObjectId ID));
  85. };
  86. IMPLEMENT_CONOBJECT(ScriptGroup);
  87. ConsoleDocClass( ScriptGroup,
  88. "@brief Essentially a SimGroup, but with onAdd and onRemove script callbacks.\n\n"
  89. "@tsexample\n"
  90. "// First container, SimGroup containing a ScriptGroup\n"
  91. "new SimGroup(Scenes)\n"
  92. "{\n"
  93. " // Subcontainer, ScriptGroup containing variables\n"
  94. " // related to a cut scene and a starting WayPoint\n"
  95. " new ScriptGroup(WelcomeScene)\n"
  96. " {\n"
  97. " class = \"Scene\";\n"
  98. " pathName = \"Pathx\";\n"
  99. " description = \"A small orc village set in the Hardesty mountains. This town and its surroundings will be used to illustrate some the Torque Game Engine\'s features.\";\n"
  100. " pathTime = \"0\";\n"
  101. " title = \"Welcome to Orc Town\";\n\n"
  102. " new WayPoint(start)\n"
  103. " {\n"
  104. " position = \"163.873 -103.82 208.354\";\n"
  105. " rotation = \"0.136165 -0.0544916 0.989186 44.0527\";\n"
  106. " scale = \"1 1 1\";\n"
  107. " dataBlock = \"WayPointMarker\";\n"
  108. " team = \"0\";\n"
  109. " };\n"
  110. " };\n"
  111. "};\n"
  112. "@endtsexample\n\n"
  113. "@see SimGroup\n"
  114. "@ingroup Console\n"
  115. "@ingroup Scripting"
  116. );
  117. ScriptGroup::ScriptGroup()
  118. {
  119. }
  120. IMPLEMENT_CALLBACK( ScriptGroup, onAdd, void, ( SimObjectId ID ), ( ID ),
  121. "Called when this ScriptGroup is added to the system.\n"
  122. "@param ID Unique object ID assigned when created (%this in script).\n"
  123. );
  124. IMPLEMENT_CALLBACK( ScriptGroup, onRemove, void, ( SimObjectId ID ), ( ID ),
  125. "Called when this ScriptObject is removed from the system.\n"
  126. "@param ID Unique object ID assigned when created (%this in script).\n"
  127. );
  128. bool ScriptGroup::onAdd()
  129. {
  130. if (!Parent::onAdd())
  131. return false;
  132. // Call onAdd in script!
  133. //Con::executef(this, "onAdd", Con::getIntArg(getId()));
  134. onAdd_callback(getId());
  135. return true;
  136. }
  137. void ScriptGroup::onRemove()
  138. {
  139. // Call onRemove in script!
  140. //Con::executef(this, "onRemove", Con::getIntArg(getId()));
  141. onRemove_callback(getId());
  142. Parent::onRemove();
  143. }