afxEA_GuiController.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "gui/core/guiControl.h"
  27. #include "gui/3d/guiTSControl.h"
  28. #include "T3D/gameBase/gameConnection.h"
  29. #include "gui/game/guiProgressCtrl.h"
  30. #include "afx/afxChoreographer.h"
  31. #include "afx/afxEffectDefs.h"
  32. #include "afx/afxEffectWrapper.h"
  33. #include "afx/ce/afxGuiController.h"
  34. #include "afx/ui/afxProgressBase.h"
  35. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  36. // afxEA_GuiController
  37. class afxEA_GuiController : public afxEffectWrapper
  38. {
  39. typedef afxEffectWrapper Parent;
  40. afxGuiControllerData* controller_data;
  41. GuiControl* gui_control;
  42. GuiTSCtrl* ts_ctrl;
  43. afxProgressBase* progress_base;
  44. GuiProgressCtrl* progress_ctrl;
  45. void do_runtime_substitutions();
  46. public:
  47. /*C*/ afxEA_GuiController();
  48. virtual void ea_set_datablock(SimDataBlock*);
  49. virtual bool ea_start();
  50. virtual bool ea_update(F32 dt);
  51. virtual void ea_finish(bool was_stopped);
  52. };
  53. //~~~~~~~~~~~~~~~~~~~~//
  54. afxEA_GuiController::afxEA_GuiController()
  55. {
  56. controller_data = 0;
  57. gui_control = 0;
  58. ts_ctrl = 0;
  59. progress_base = 0;
  60. progress_ctrl = 0;
  61. }
  62. void afxEA_GuiController::ea_set_datablock(SimDataBlock* db)
  63. {
  64. controller_data = dynamic_cast<afxGuiControllerData*>(db);
  65. }
  66. bool afxEA_GuiController::ea_start()
  67. {
  68. if (!controller_data)
  69. {
  70. Con::errorf("afxEA_GuiController::ea_start() -- missing or incompatible datablock.");
  71. return false;
  72. }
  73. do_runtime_substitutions();
  74. if (controller_data->ctrl_client_only)
  75. {
  76. afxConstraint* pos_cons = getPosConstraint();
  77. if (!pos_cons)
  78. return false; // not an error condition
  79. GameBase* gamebase_obj = dynamic_cast<GameBase*>(pos_cons->getSceneObject());
  80. if (!gamebase_obj || !gamebase_obj->getControllingClient())
  81. return false; // not an error condition
  82. }
  83. if (controller_data->control_name == ST_NULLSTRING || controller_data->control_name[0] == '\0')
  84. {
  85. Con::errorf("afxEA_GuiController::ea_start() -- empty control name.");
  86. return false;
  87. }
  88. gui_control = dynamic_cast<GuiControl*>(Sim::findObject(controller_data->control_name));
  89. if (!gui_control)
  90. {
  91. Con::errorf("afxEA_GuiController::ea_start() -- failed to find control \"%s\".", controller_data->control_name);
  92. return false;
  93. }
  94. gui_control->setVisible(true);
  95. progress_base = dynamic_cast<afxProgressBase*>(gui_control);
  96. if (progress_base)
  97. {
  98. progress_base->setProgress(0.0f);
  99. progress_ctrl = 0;
  100. }
  101. else
  102. {
  103. progress_ctrl = (GuiProgressCtrl*)gui_control;
  104. if (progress_ctrl)
  105. {
  106. progress_ctrl->setScriptValue(0);
  107. progress_base = 0;
  108. }
  109. }
  110. ts_ctrl = 0;
  111. for (GuiControl* ctrl = gui_control->getParent(); ctrl != 0; ctrl->getParent())
  112. {
  113. if (dynamic_cast<GuiTSCtrl*>(ctrl))
  114. {
  115. ts_ctrl = (GuiTSCtrl*) ctrl;
  116. break;
  117. }
  118. }
  119. return true;
  120. }
  121. bool afxEA_GuiController::ea_update(F32 dt)
  122. {
  123. if (ts_ctrl && !controller_data->preserve_pos)
  124. {
  125. Point3F screen_pos;
  126. if (ts_ctrl->project(mUpdated_pos, &screen_pos))
  127. {
  128. const Point2I ext = gui_control->getExtent();
  129. Point2I newpos(screen_pos.x - ext.x/2, screen_pos.y - ext.y/2);
  130. gui_control->setPosition(newpos);
  131. }
  132. }
  133. if (progress_base)
  134. progress_base->setProgress((mEW_timing.lifetime > 0.0) ? mLife_elapsed / mEW_timing.lifetime : 0.0f);
  135. else if (progress_ctrl)
  136. progress_ctrl->setScriptValue((mEW_timing.lifetime > 0.0) ? avar("%g", mLife_elapsed / mEW_timing.lifetime) : 0);
  137. if (mDo_fades)
  138. gui_control->setFadeAmount(mFade_value);
  139. return true;
  140. }
  141. void afxEA_GuiController::ea_finish(bool was_stopped)
  142. {
  143. if (progress_base)
  144. progress_base->setProgress(1.0f);
  145. else if (progress_ctrl)
  146. progress_ctrl->setScriptValue("1");
  147. gui_control->setVisible(false);
  148. }
  149. void afxEA_GuiController::do_runtime_substitutions()
  150. {
  151. // only clone the datablock if there are substitutions
  152. if (controller_data->getSubstitutionCount() > 0)
  153. {
  154. // clone the datablock and perform substitutions
  155. afxGuiControllerData* orig_db = controller_data;
  156. controller_data = new afxGuiControllerData(*orig_db, true);
  157. orig_db->performSubstitutions(controller_data, mChoreographer, mGroup_index);
  158. }
  159. }
  160. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  161. class afxEA_GuiControllerDesc : public afxEffectAdapterDesc, public afxEffectDefs
  162. {
  163. static afxEA_GuiControllerDesc desc;
  164. public:
  165. virtual bool testEffectType(const SimDataBlock*) const;
  166. virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const;
  167. virtual bool runsOnServer(const afxEffectWrapperData*) const { return false; }
  168. virtual bool runsOnClient(const afxEffectWrapperData*) const { return true; }
  169. virtual afxEffectWrapper* create() const { return new afxEA_GuiController; }
  170. };
  171. afxEA_GuiControllerDesc afxEA_GuiControllerDesc::desc;
  172. bool afxEA_GuiControllerDesc::testEffectType(const SimDataBlock* db) const
  173. {
  174. return (typeid(afxGuiControllerData) == typeid(*db));
  175. }
  176. bool afxEA_GuiControllerDesc::requiresStop(const afxEffectWrapperData* ew, const afxEffectTimingData& timing) const
  177. {
  178. return (timing.lifetime < 0);
  179. }
  180. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//