afxEA_GuiController.cpp
Engine/source/afx/ea/afxEA_GuiController.cpp
Classes:
Detailed Description
1 2 3//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 4// Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames 5// Copyright (C) 2015 Faust Logic, Inc. 6// 7// Permission is hereby granted, free of charge, to any person obtaining a copy 8// of this software and associated documentation files (the "Software"), to 9// deal in the Software without restriction, including without limitation the 10// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 11// sell copies of the Software, and to permit persons to whom the Software is 12// furnished to do so, subject to the following conditions: 13// 14// The above copyright notice and this permission notice shall be included in 15// all copies or substantial portions of the Software. 16// 17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23// IN THE SOFTWARE. 24// 25//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 26 27#include <typeinfo> 28#include "afx/arcaneFX.h" 29 30#include "gui/core/guiControl.h" 31#include "gui/3d/guiTSControl.h" 32#include "T3D/gameBase/gameConnection.h" 33#include "gui/game/guiProgressCtrl.h" 34 35#include "afx/afxChoreographer.h" 36#include "afx/afxEffectDefs.h" 37#include "afx/afxEffectWrapper.h" 38#include "afx/ce/afxGuiController.h" 39#include "afx/ui/afxProgressBase.h" 40 41//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 42// afxEA_GuiController 43 44class afxEA_GuiController : public afxEffectWrapper 45{ 46 typedef afxEffectWrapper Parent; 47 48 afxGuiControllerData* controller_data; 49 GuiControl* gui_control; 50 GuiTSCtrl* ts_ctrl; 51 afxProgressBase* progress_base; 52 GuiProgressCtrl* progress_ctrl; 53 54 void do_runtime_substitutions(); 55 56public: 57 /*C*/ afxEA_GuiController(); 58 59 virtual void ea_set_datablock(SimDataBlock*); 60 virtual bool ea_start(); 61 virtual bool ea_update(F32 dt); 62 virtual void ea_finish(bool was_stopped); 63}; 64 65//~~~~~~~~~~~~~~~~~~~~// 66 67afxEA_GuiController::afxEA_GuiController() 68{ 69 controller_data = 0; 70 gui_control = 0; 71 ts_ctrl = 0; 72 progress_base = 0; 73 progress_ctrl = 0; 74} 75 76void afxEA_GuiController::ea_set_datablock(SimDataBlock* db) 77{ 78 controller_data = dynamic_cast<afxGuiControllerData*>(db); 79} 80 81bool afxEA_GuiController::ea_start() 82{ 83 if (!controller_data) 84 { 85 Con::errorf("afxEA_GuiController::ea_start() -- missing or incompatible datablock."); 86 return false; 87 } 88 89 do_runtime_substitutions(); 90 91 if (controller_data->ctrl_client_only) 92 { 93 afxConstraint* pos_cons = getPosConstraint(); 94 if (!pos_cons) 95 return false; // not an error condition 96 GameBase* gamebase_obj = dynamic_cast<GameBase*>(pos_cons->getSceneObject()); 97 if (!gamebase_obj || !gamebase_obj->getControllingClient()) 98 return false; // not an error condition 99 } 100 101 if (controller_data->control_name == ST_NULLSTRING || controller_data->control_name[0] == '\0') 102 { 103 Con::errorf("afxEA_GuiController::ea_start() -- empty control name."); 104 return false; 105 } 106 107 gui_control = dynamic_cast<GuiControl*>(Sim::findObject(controller_data->control_name)); 108 if (!gui_control) 109 { 110 Con::errorf("afxEA_GuiController::ea_start() -- failed to find control \"%s\".", controller_data->control_name); 111 return false; 112 } 113 114 gui_control->setVisible(true); 115 116 progress_base = dynamic_cast<afxProgressBase*>(gui_control); 117 if (progress_base) 118 { 119 progress_base->setProgress(0.0f); 120 progress_ctrl = 0; 121 } 122 else 123 { 124 progress_ctrl = (GuiProgressCtrl*)gui_control; 125 if (progress_ctrl) 126 { 127 progress_ctrl->setScriptValue(0); 128 progress_base = 0; 129 } 130 } 131 132 ts_ctrl = 0; 133 for (GuiControl* ctrl = gui_control->getParent(); ctrl != 0; ctrl->getParent()) 134 { 135 if (dynamic_cast<GuiTSCtrl*>(ctrl)) 136 { 137 ts_ctrl = (GuiTSCtrl*) ctrl; 138 break; 139 } 140 } 141 142 return true; 143} 144 145bool afxEA_GuiController::ea_update(F32 dt) 146{ 147 if (ts_ctrl && !controller_data->preserve_pos) 148 { 149 Point3F screen_pos; 150 if (ts_ctrl->project(mUpdated_pos, &screen_pos)) 151 { 152 const Point2I ext = gui_control->getExtent(); 153 Point2I newpos(screen_pos.x - ext.x/2, screen_pos.y - ext.y/2); 154 gui_control->setPosition(newpos); 155 } 156 } 157 158 if (progress_base) 159 progress_base->setProgress((mEW_timing.lifetime > 0.0) ? mLife_elapsed / mEW_timing.lifetime : 0.0f); 160 else if (progress_ctrl) 161 progress_ctrl->setScriptValue((mEW_timing.lifetime > 0.0) ? avar("%g", mLife_elapsed / mEW_timing.lifetime) : 0); 162 163 if (mDo_fades) 164 gui_control->setFadeAmount(mFade_value); 165 166 return true; 167} 168 169void afxEA_GuiController::ea_finish(bool was_stopped) 170{ 171 if (progress_base) 172 progress_base->setProgress(1.0f); 173 else if (progress_ctrl) 174 progress_ctrl->setScriptValue("1"); 175 gui_control->setVisible(false); 176} 177 178void afxEA_GuiController::do_runtime_substitutions() 179{ 180 // only clone the datablock if there are substitutions 181 if (controller_data->getSubstitutionCount() > 0) 182 { 183 // clone the datablock and perform substitutions 184 afxGuiControllerData* orig_db = controller_data; 185 controller_data = new afxGuiControllerData(*orig_db, true); 186 orig_db->performSubstitutions(controller_data, mChoreographer, mGroup_index); 187 } 188} 189 190//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 191 192class afxEA_GuiControllerDesc : public afxEffectAdapterDesc, public afxEffectDefs 193{ 194 static afxEA_GuiControllerDesc desc; 195 196public: 197 virtual bool testEffectType(const SimDataBlock*) const; 198 virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const; 199 virtual bool runsOnServer(const afxEffectWrapperData*) const { return false; } 200 virtual bool runsOnClient(const afxEffectWrapperData*) const { return true; } 201 202 virtual afxEffectWrapper* create() const { return new afxEA_GuiController; } 203}; 204 205afxEA_GuiControllerDesc afxEA_GuiControllerDesc::desc; 206 207bool afxEA_GuiControllerDesc::testEffectType(const SimDataBlock* db) const 208{ 209 return (typeid(afxGuiControllerData) == typeid(*db)); 210} 211 212bool afxEA_GuiControllerDesc::requiresStop(const afxEffectWrapperData* ew, const afxEffectTimingData& timing) const 213{ 214 return (timing.lifetime < 0); 215} 216 217//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~// 218