afxEffectGroup.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 "console/engineAPI.h"
  26. #include "afx/afxEffectGroup.h"
  27. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  28. // afxEffectGroupData::egValidator
  29. //
  30. // When an effect is added using "addEffect", this validator intercepts the value
  31. // and adds it to the dynamic effects list.
  32. //
  33. void afxEffectGroupData::egValidator::validateType(SimObject* object, void* typePtr)
  34. {
  35. afxEffectGroupData* eff_data = dynamic_cast<afxEffectGroupData*>(object);
  36. afxEffectBaseData** ew = (afxEffectBaseData**)(typePtr);
  37. if (eff_data && ew)
  38. {
  39. eff_data->fx_list.push_back(*ew);
  40. *ew = 0;
  41. }
  42. }
  43. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  44. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  45. // afxEffectGroupData
  46. IMPLEMENT_CO_DATABLOCK_V1(afxEffectGroupData);
  47. ConsoleDocClass( afxEffectGroupData,
  48. "@brief A datablock that describes an Effect Group.\n\n"
  49. "afxEffectGroupData provides a way for adding several effects to a choreographer as a "
  50. "group and can be used wherever an afxEffectWrapperData is used. Basically, an "
  51. "effect-group is a simple list of effect-wrappers. When an effect-group is added to a "
  52. "choreographer, the end result is almost the same as adding all of the group's "
  53. "effect-wrappers directly to the choreographer. The main difference is that the "
  54. "grouped effects can be turned on and off collectively and created in multiples. "
  55. "Effect-groups can also contain other effect-groups, forming a hierarchy of effects.\n\n"
  56. "A great strength of effect-groups is that they have a count setting that multiplies "
  57. "the number of times the effects in the group are added to the owning choreographer "
  58. "and this doesn't happen until the choreographer instance is created and launched. "
  59. "This makes a big difference for certain kinds of effects, such as fireworks, that "
  60. "tend to consist of small groupings of effects that are repeated many times with "
  61. "slight variations. With groups, an effect like this has a very compact representation "
  62. "for transmitting from server to clients, that only expands when actually used.\n\n"
  63. "Effect-groups with a count greater than one are extremely useful when some of the "
  64. "effects use field substitutions. When an effect-group is expanded, it essentially runs "
  65. "through a for-loop from 0 to count-1 and creates a new set of effect instances each "
  66. "time through the loop. For each new set of effects, their group-index is set to the "
  67. "index of this for-loop, which in turn replaces the ## token used in any field "
  68. "substitutions in the child effects. In essence, the for-loop index becomes a parameter "
  69. "of the child effects which can be used to vary the effects created in each loop.\n\n"
  70. "@see afxEffectBaseData\n\n"
  71. "@see afxEffectWrapperData\n\n"
  72. "@ingroup afxEffects\n"
  73. "@ingroup AFX\n"
  74. "@ingroup Datablocks\n"
  75. );
  76. afxEffectGroupData::afxEffectGroupData()
  77. {
  78. group_enabled = true;
  79. group_count = 1;
  80. idx_offset = 0;
  81. assign_idx = false;
  82. // dummy entry holds effect-wrapper pointer while a special validator
  83. // grabs it and adds it to an appropriate effects list
  84. dummy_fx_entry = NULL;
  85. // marked true if datablock ids need to
  86. // be converted into pointers
  87. do_id_convert = false;
  88. }
  89. afxEffectGroupData::afxEffectGroupData(const afxEffectGroupData& other, bool temp_clone) : afxEffectBaseData(other, temp_clone)
  90. {
  91. group_enabled = other.group_enabled;
  92. group_count = other.group_count;
  93. idx_offset = other.idx_offset;
  94. assign_idx = other.assign_idx;
  95. timing = other.timing;
  96. dummy_fx_entry = other.dummy_fx_entry;
  97. do_id_convert = other.do_id_convert; // --
  98. fx_list = other.fx_list; // --
  99. }
  100. void afxEffectGroupData::reloadReset()
  101. {
  102. fx_list.clear();
  103. }
  104. void afxEffectGroupData::pack_fx(BitStream* stream, const afxEffectList& fx, bool packed)
  105. {
  106. stream->writeInt(fx.size(), EFFECTS_PER_PHRASE_BITS);
  107. for (int i = 0; i < fx.size(); i++)
  108. writeDatablockID(stream, fx[i], packed);
  109. }
  110. void afxEffectGroupData::unpack_fx(BitStream* stream, afxEffectList& fx)
  111. {
  112. fx.clear();
  113. S32 n_fx = stream->readInt(EFFECTS_PER_PHRASE_BITS);
  114. for (int i = 0; i < n_fx; i++)
  115. fx.push_back((afxEffectWrapperData*)(uintptr_t)readDatablockID(stream));
  116. }
  117. #define myOffset(field) Offset(field, afxEffectGroupData)
  118. void afxEffectGroupData::initPersistFields()
  119. {
  120. docsURL;
  121. addField("groupEnabled", TypeBool, myOffset(group_enabled),
  122. "...");
  123. addField("count", TypeS32, myOffset(group_count),
  124. "...");
  125. addField("indexOffset", TypeS8, myOffset(idx_offset),
  126. "...");
  127. addField("assignIndices", TypeBool, myOffset(assign_idx),
  128. "...");
  129. addField("delay", TypeF32, myOffset(timing.delay),
  130. "...");
  131. addField("lifetime", TypeF32, myOffset(timing.lifetime),
  132. "...");
  133. addField("fadeInTime", TypeF32, myOffset(timing.fade_in_time),
  134. "...");
  135. addField("fadeOutTime", TypeF32, myOffset(timing.fade_out_time),
  136. "...");
  137. // effect lists
  138. // for each of these, dummy_fx_entry is set and then a validator adds it to the appropriate effects list
  139. static egValidator emptyValidator(0);
  140. addFieldV("addEffect", TYPEID<afxEffectBaseData>(), myOffset(dummy_fx_entry), &emptyValidator,
  141. "...");
  142. Parent::initPersistFields();
  143. // disallow some field substitutions
  144. disableFieldSubstitutions("addEffect");
  145. }
  146. void afxEffectGroupData::packData(BitStream* stream)
  147. {
  148. Parent::packData(stream);
  149. stream->writeFlag(group_enabled);
  150. stream->write(group_count);
  151. stream->write(idx_offset);
  152. stream->writeFlag(assign_idx);
  153. stream->write(timing.delay);
  154. stream->write(timing.lifetime);
  155. stream->write(timing.fade_in_time);
  156. stream->write(timing.fade_out_time);
  157. pack_fx(stream, fx_list, mPacked);
  158. }
  159. void afxEffectGroupData::unpackData(BitStream* stream)
  160. {
  161. Parent::unpackData(stream);
  162. group_enabled = stream->readFlag();
  163. stream->read(&group_count);
  164. stream->read(&idx_offset);
  165. assign_idx = stream->readFlag();
  166. stream->read(&timing.delay);
  167. stream->read(&timing.lifetime);
  168. stream->read(&timing.fade_in_time);
  169. stream->read(&timing.fade_out_time);
  170. do_id_convert = true;
  171. unpack_fx(stream, fx_list);
  172. }
  173. bool afxEffectGroupData::preload(bool server, String &errorStr)
  174. {
  175. if (!Parent::preload(server, errorStr))
  176. return false;
  177. // Resolve objects transmitted from server
  178. if (!server)
  179. {
  180. if (do_id_convert)
  181. {
  182. for (S32 i = 0; i < fx_list.size(); i++)
  183. {
  184. SimObjectId db_id = SimObjectId((uintptr_t)fx_list[i]);
  185. if (db_id != 0)
  186. {
  187. // try to convert id to pointer
  188. if (!Sim::findObject(db_id, fx_list[i]))
  189. {
  190. Con::errorf(ConsoleLogEntry::General,
  191. "afxEffectGroupData::preload() -- bad datablockId: 0x%x",
  192. db_id);
  193. }
  194. }
  195. }
  196. do_id_convert = false;
  197. }
  198. }
  199. return true;
  200. }
  201. void afxEffectGroupData::gather_cons_defs(Vector<afxConstraintDef>& defs)
  202. {
  203. for (S32 i = 0; i < fx_list.size(); i++)
  204. {
  205. if (fx_list[i])
  206. fx_list[i]->gather_cons_defs(defs);
  207. }
  208. }
  209. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  210. DefineEngineMethod(afxEffectGroupData, reset, void, (),,
  211. "Resets an effect-group datablock during reload.\n\n"
  212. "@ingroup AFX")
  213. {
  214. object->reloadReset();
  215. }
  216. DefineEngineMethod(afxEffectGroupData, pushEffect, void, (afxEffectBaseData* effect),,
  217. "Adds an effect (wrapper or group) to an effect-group.\n\n"
  218. "@ingroup AFX")
  219. {
  220. if (!effect)
  221. {
  222. Con::errorf("afxEffectGroupData::pushEffect() -- missing afxEffectWrapperData.");
  223. return;
  224. }
  225. object->fx_list.push_back(effect);
  226. }
  227. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//