afxXM_MountedImageNode.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  3. // Copyright (C) 2015 Faust Logic, Inc.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //
  23. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  24. #include "afx/arcaneFX.h"
  25. #include "afx/afxEffectWrapper.h"
  26. #include "afx/afxChoreographer.h"
  27. #include "afx/xm/afxXfmMod.h"
  28. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  29. class afxXM_MountedImageNodeData : public afxXM_BaseData
  30. {
  31. typedef afxXM_BaseData Parent;
  32. public:
  33. StringTableEntry node_name; // name of a mounted-image node
  34. U32 image_slot; // which mounted-image?
  35. // (0 <= image_slot < MaxMountedImages)
  36. public:
  37. /*C*/ afxXM_MountedImageNodeData();
  38. /*C*/ afxXM_MountedImageNodeData(const afxXM_MountedImageNodeData&, bool = false);
  39. void packData(BitStream* stream);
  40. void unpackData(BitStream* stream);
  41. bool onAdd();
  42. virtual bool allowSubstitutions() const { return true; }
  43. static void initPersistFields();
  44. afxXM_Base* create(afxEffectWrapper* fx, bool on_server);
  45. DECLARE_CONOBJECT(afxXM_MountedImageNodeData);
  46. DECLARE_CATEGORY("AFX");
  47. };
  48. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  49. class afxXM_MountedImageNode : public afxXM_Base
  50. {
  51. typedef afxXM_Base Parent;
  52. StringTableEntry mNode_name;
  53. U32 mImage_slot;
  54. S32 mNode_ID;
  55. ShapeBase* mShape;
  56. afxConstraint* mCons;
  57. afxConstraint* find_constraint();
  58. public:
  59. /*C*/ afxXM_MountedImageNode(afxXM_MountedImageNodeData*, afxEffectWrapper*);
  60. virtual void start(F32 timestamp);
  61. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  62. };
  63. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  64. IMPLEMENT_CO_DATABLOCK_V1(afxXM_MountedImageNodeData);
  65. ConsoleDocClass( afxXM_MountedImageNodeData,
  66. "@brief An xmod datablock.\n\n"
  67. "@ingroup afxXMods\n"
  68. "@ingroup AFX\n"
  69. "@ingroup Datablocks\n"
  70. );
  71. afxXM_MountedImageNodeData::afxXM_MountedImageNodeData()
  72. {
  73. image_slot = 0;
  74. node_name = ST_NULLSTRING;
  75. }
  76. afxXM_MountedImageNodeData::afxXM_MountedImageNodeData(const afxXM_MountedImageNodeData& other, bool temp_clone) : afxXM_BaseData(other, temp_clone)
  77. {
  78. image_slot = other.image_slot;
  79. node_name = other.node_name;
  80. }
  81. void afxXM_MountedImageNodeData::initPersistFields()
  82. {
  83. addField("imageSlot", TypeS32, Offset(image_slot, afxXM_MountedImageNodeData),
  84. "...");
  85. addField("nodeName", TypeString, Offset(node_name, afxXM_MountedImageNodeData),
  86. "...");
  87. Parent::initPersistFields();
  88. }
  89. void afxXM_MountedImageNodeData::packData(BitStream* stream)
  90. {
  91. Parent::packData(stream);
  92. stream->write(image_slot);
  93. stream->writeString(node_name);
  94. }
  95. void afxXM_MountedImageNodeData::unpackData(BitStream* stream)
  96. {
  97. Parent::unpackData(stream);
  98. stream->read(&image_slot);
  99. node_name = stream->readSTString();
  100. }
  101. bool afxXM_MountedImageNodeData::onAdd()
  102. {
  103. if (Parent::onAdd() == false)
  104. return false;
  105. if (image_slot >= ShapeBase::MaxMountedImages)
  106. {
  107. // datablock will not be added if imageSlot is out-of-bounds
  108. Con::errorf(ConsoleLogEntry::General,
  109. "afxXM_MountedImageNodeData(%s): imageSlot (%u) >= MaxMountedImages (%d)",
  110. getName(),
  111. image_slot,
  112. ShapeBase::MaxMountedImages);
  113. return false;
  114. }
  115. return true;
  116. }
  117. afxXM_Base* afxXM_MountedImageNodeData::create(afxEffectWrapper* fx, bool on_server)
  118. {
  119. afxXM_MountedImageNodeData* datablock = this;
  120. if (getSubstitutionCount() > 0)
  121. {
  122. datablock = new afxXM_MountedImageNodeData(*this, true);
  123. this->performSubstitutions(datablock, fx->getChoreographer(), fx->getGroupIndex());
  124. }
  125. return new afxXM_MountedImageNode(datablock, fx);
  126. }
  127. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  128. afxXM_MountedImageNode::afxXM_MountedImageNode(afxXM_MountedImageNodeData* db, afxEffectWrapper* fxw)
  129. : afxXM_Base(db, fxw)
  130. {
  131. mImage_slot = db->image_slot;
  132. mNode_name = db->node_name;
  133. mCons = 0;
  134. mNode_ID = -1;
  135. mShape = 0;
  136. }
  137. // find the first constraint with a shape by checking pos
  138. // then orient constraints in that order.
  139. afxConstraint* afxXM_MountedImageNode::find_constraint()
  140. {
  141. afxConstraint* cons = fx_wrapper->getOrientConstraint();
  142. if (cons && dynamic_cast<ShapeBase*>(cons->getSceneObject()))
  143. return cons;
  144. cons = fx_wrapper->getPosConstraint();
  145. if (cons && dynamic_cast<ShapeBase*>(cons->getSceneObject()))
  146. return cons;
  147. return 0;
  148. }
  149. void afxXM_MountedImageNode::start(F32 timestamp)
  150. {
  151. // constraint won't change over the modifier's
  152. // lifetime so we find it here in start().
  153. mCons = find_constraint();
  154. if (!mCons)
  155. Con::errorf(ConsoleLogEntry::General,
  156. "afxXM_MountedImageNode: failed to find a ShapeBase derived constraint source.");
  157. }
  158. void afxXM_MountedImageNode::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  159. {
  160. if (!mCons)
  161. return;
  162. // validate shape
  163. // The shape must be validated in case it gets deleted
  164. // of goes out scope.
  165. SceneObject* scene_object = mCons->getSceneObject();
  166. if (scene_object != (SceneObject*)mShape)
  167. {
  168. mShape = dynamic_cast<ShapeBase*>(scene_object);
  169. if (mShape && mNode_name != ST_NULLSTRING)
  170. {
  171. mNode_ID = mShape->getNodeIndex(mImage_slot, mNode_name);
  172. if (mNode_ID < 0)
  173. {
  174. Con::errorf(ConsoleLogEntry::General,
  175. "afxXM_MountedImageNode: failed to find nodeName, \"%s\".",
  176. mNode_name);
  177. }
  178. }
  179. else
  180. mNode_ID = -1;
  181. }
  182. if (mShape)
  183. {
  184. mShape->getImageTransform(mImage_slot, mNode_ID, &params.ori);
  185. params.pos = params.ori.getPosition();
  186. }
  187. }
  188. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//