afxXM_MountedImageNode.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. docsURL;
  84. addField("imageSlot", TypeS32, Offset(image_slot, afxXM_MountedImageNodeData),
  85. "...");
  86. addField("nodeName", TypeString, Offset(node_name, afxXM_MountedImageNodeData),
  87. "...");
  88. Parent::initPersistFields();
  89. }
  90. void afxXM_MountedImageNodeData::packData(BitStream* stream)
  91. {
  92. Parent::packData(stream);
  93. stream->write(image_slot);
  94. stream->writeString(node_name);
  95. }
  96. void afxXM_MountedImageNodeData::unpackData(BitStream* stream)
  97. {
  98. Parent::unpackData(stream);
  99. stream->read(&image_slot);
  100. node_name = stream->readSTString();
  101. }
  102. bool afxXM_MountedImageNodeData::onAdd()
  103. {
  104. if (Parent::onAdd() == false)
  105. return false;
  106. if (image_slot >= ShapeBase::MaxMountedImages)
  107. {
  108. // datablock will not be added if imageSlot is out-of-bounds
  109. Con::errorf(ConsoleLogEntry::General,
  110. "afxXM_MountedImageNodeData(%s): imageSlot (%u) >= MaxMountedImages (%d)",
  111. getName(),
  112. image_slot,
  113. ShapeBase::MaxMountedImages);
  114. return false;
  115. }
  116. return true;
  117. }
  118. afxXM_Base* afxXM_MountedImageNodeData::create(afxEffectWrapper* fx, bool on_server)
  119. {
  120. afxXM_MountedImageNodeData* datablock = this;
  121. if (getSubstitutionCount() > 0)
  122. {
  123. datablock = new afxXM_MountedImageNodeData(*this, true);
  124. this->performSubstitutions(datablock, fx->getChoreographer(), fx->getGroupIndex());
  125. }
  126. return new afxXM_MountedImageNode(datablock, fx);
  127. }
  128. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  129. afxXM_MountedImageNode::afxXM_MountedImageNode(afxXM_MountedImageNodeData* db, afxEffectWrapper* fxw)
  130. : afxXM_Base(db, fxw)
  131. {
  132. mImage_slot = db->image_slot;
  133. mNode_name = db->node_name;
  134. mCons = 0;
  135. mNode_ID = -1;
  136. mShape = 0;
  137. }
  138. // find the first constraint with a shape by checking pos
  139. // then orient constraints in that order.
  140. afxConstraint* afxXM_MountedImageNode::find_constraint()
  141. {
  142. afxConstraint* cons = fx_wrapper->getOrientConstraint();
  143. if (cons && dynamic_cast<ShapeBase*>(cons->getSceneObject()))
  144. return cons;
  145. cons = fx_wrapper->getPosConstraint();
  146. if (cons && dynamic_cast<ShapeBase*>(cons->getSceneObject()))
  147. return cons;
  148. return 0;
  149. }
  150. void afxXM_MountedImageNode::start(F32 timestamp)
  151. {
  152. // constraint won't change over the modifier's
  153. // lifetime so we find it here in start().
  154. mCons = find_constraint();
  155. if (!mCons)
  156. Con::errorf(ConsoleLogEntry::General,
  157. "afxXM_MountedImageNode: failed to find a ShapeBase derived constraint source.");
  158. }
  159. void afxXM_MountedImageNode::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  160. {
  161. if (!mCons)
  162. return;
  163. // validate shape
  164. // The shape must be validated in case it gets deleted
  165. // of goes out scope.
  166. SceneObject* scene_object = mCons->getSceneObject();
  167. if (scene_object != (SceneObject*)mShape)
  168. {
  169. mShape = dynamic_cast<ShapeBase*>(scene_object);
  170. if (mShape && mNode_name != ST_NULLSTRING)
  171. {
  172. mNode_ID = mShape->getNodeIndex(mImage_slot, mNode_name);
  173. if (mNode_ID < 0)
  174. {
  175. Con::errorf(ConsoleLogEntry::General,
  176. "afxXM_MountedImageNode: failed to find nodeName, \"%s\".",
  177. mNode_name);
  178. }
  179. }
  180. else
  181. mNode_ID = -1;
  182. }
  183. if (mShape)
  184. {
  185. mShape->getImageTransform(mImage_slot, mNode_ID, &params.ori);
  186. params.pos = params.ori.getPosition();
  187. }
  188. }
  189. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//