afxEA_GuiText.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 <typeinfo>
  25. #include "afx/arcaneFX.h"
  26. #include "afx/afxChoreographer.h"
  27. #include "afx/afxEffectDefs.h"
  28. #include "afx/afxEffectWrapper.h"
  29. #include "afx/ce/afxGuiText.h"
  30. #include "afx/ui/afxGuiTextHud.h"
  31. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  32. // afxEA_GuiText
  33. class afxEA_GuiText : public afxEffectWrapper
  34. {
  35. typedef afxEffectWrapper Parent;
  36. enum {
  37. UNDEFINED,
  38. USER_TEXT,
  39. SHAPE_NAME
  40. };
  41. afxGuiTextData* text_data;
  42. S32 text_src;
  43. LinearColorF text_clr;
  44. void do_runtime_substitutions();
  45. public:
  46. /*C*/ afxEA_GuiText();
  47. virtual void ea_set_datablock(SimDataBlock*);
  48. virtual bool ea_start();
  49. virtual bool ea_update(F32 dt);
  50. };
  51. //~~~~~~~~~~~~~~~~~~~~//
  52. afxEA_GuiText::afxEA_GuiText()
  53. {
  54. text_data = 0;
  55. text_src = UNDEFINED;
  56. text_clr.set(1,1,1,1);
  57. }
  58. void afxEA_GuiText::ea_set_datablock(SimDataBlock* db)
  59. {
  60. text_data = dynamic_cast<afxGuiTextData*>(db);
  61. }
  62. bool afxEA_GuiText::ea_start()
  63. {
  64. if (!text_data)
  65. {
  66. Con::errorf("afxEA_GuiText::ea_start() -- missing or incompatible datablock.");
  67. return false;
  68. }
  69. do_runtime_substitutions();
  70. if (text_data->text_str == ST_NULLSTRING || text_data->text_str[0] == '\0')
  71. {
  72. Con::errorf("afxEA_GuiText::ea_start() -- empty text string.");
  73. return false;
  74. }
  75. text_clr = text_data->text_clr;
  76. if (dStricmp("#shapeName", text_data->text_str) == 0)
  77. text_src = SHAPE_NAME;
  78. else
  79. text_src = USER_TEXT;
  80. return true;
  81. }
  82. bool afxEA_GuiText::ea_update(F32 dt)
  83. {
  84. switch (text_src)
  85. {
  86. case USER_TEXT:
  87. {
  88. LinearColorF temp_clr = text_clr;
  89. if (mDo_fades)
  90. temp_clr.alpha = mFade_value;
  91. afxGuiTextHud::addTextItem(mUpdated_pos, text_data->text_str, temp_clr);
  92. }
  93. break;
  94. case SHAPE_NAME:
  95. {
  96. const char* name = 0;
  97. SceneObject* cons_obj = 0;
  98. afxConstraint* pos_cons = getPosConstraint();
  99. if (pos_cons)
  100. {
  101. ShapeBase* shape = dynamic_cast<ShapeBase*>(pos_cons->getSceneObject());
  102. if (shape)
  103. {
  104. name = shape->getShapeName();
  105. cons_obj = shape;
  106. }
  107. }
  108. if (name && name[0] != '\0')
  109. {
  110. LinearColorF temp_clr = text_clr;
  111. if (mDo_fades)
  112. temp_clr.alpha = mFade_value;
  113. afxGuiTextHud::addTextItem(mUpdated_pos, name, temp_clr, cons_obj);
  114. }
  115. }
  116. break;
  117. }
  118. return true;
  119. }
  120. void afxEA_GuiText::do_runtime_substitutions()
  121. {
  122. // only clone the datablock if there are substitutions
  123. if (text_data->getSubstitutionCount() > 0)
  124. {
  125. // clone the datablock and perform substitutions
  126. afxGuiTextData* orig_db = text_data;
  127. text_data = new afxGuiTextData(*orig_db, true);
  128. orig_db->performSubstitutions(text_data, mChoreographer, mGroup_index);
  129. }
  130. }
  131. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  132. class afxEA_GuiTextDesc : public afxEffectAdapterDesc, public afxEffectDefs
  133. {
  134. static afxEA_GuiTextDesc desc;
  135. public:
  136. virtual bool testEffectType(const SimDataBlock*) const;
  137. virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const;
  138. virtual bool runsOnServer(const afxEffectWrapperData*) const { return false; }
  139. virtual bool runsOnClient(const afxEffectWrapperData*) const { return true; }
  140. virtual afxEffectWrapper* create() const { return new afxEA_GuiText; }
  141. };
  142. afxEA_GuiTextDesc afxEA_GuiTextDesc::desc;
  143. bool afxEA_GuiTextDesc::testEffectType(const SimDataBlock* db) const
  144. {
  145. return (typeid(afxGuiTextData) == typeid(*db));
  146. }
  147. bool afxEA_GuiTextDesc::requiresStop(const afxEffectWrapperData* ew, const afxEffectTimingData& timing) const
  148. {
  149. return (timing.lifetime < 0);
  150. }
  151. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//