GameObjectAsset.cpp
Engine/source/T3D/assets/GameObjectAsset.cpp
Public Functions
ConsoleDocClass(GuiInspectorTypeGameObjectAssetPtr , "@brief Inspector field type <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> Game <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Objects\n\n</a>" "Editor use <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">only.\n\n</a>" "@internal" )
ConsoleSetType(TypeGameObjectAssetPtr )
ConsoleType(GameObjectAssetPtr , TypeGameObjectAssetPtr , GameObjectAsset , ASSET_ID_FIELD_PREFIX )
DefineEngineMethod(GameObjectAsset , createObject , const char * , () , "Creates an instance of the given GameObject given the asset <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">definition.\n</a>" "@return The GameObject entity created from the asset." )
Detailed Description
Public Functions
ConsoleDocClass(GuiInspectorTypeGameObjectAssetPtr , "@brief Inspector field type <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> Game <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Objects\n\n</a>" "Editor use <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">only.\n\n</a>" "@internal" )
ConsoleSetType(TypeGameObjectAssetPtr )
ConsoleType(GameObjectAssetPtr , TypeGameObjectAssetPtr , GameObjectAsset , ASSET_ID_FIELD_PREFIX )
DefineEngineMethod(GameObjectAsset , createObject , const char * , () , "Creates an instance of the given GameObject given the asset <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">definition.\n</a>" "@return The GameObject entity created from the asset." )
IMPLEMENT_CONOBJECT(GameObjectAsset )
IMPLEMENT_CONOBJECT(GuiInspectorTypeGameObjectAssetPtr )
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2013 GarageGames, LLC 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#ifndef GAME_OBJECT_ASSET_H 25#include "GameObjectAsset.h" 26#endif 27 28#ifndef _ASSET_MANAGER_H_ 29#include "assets/assetManager.h" 30#endif 31 32#ifndef _CONSOLETYPES_H_ 33#include "console/consoleTypes.h" 34#endif 35 36#ifndef _TAML_ 37#include "persistence/taml/taml.h" 38#endif 39 40#ifndef _ASSET_PTR_H_ 41#include "assets/assetPtr.h" 42#endif 43 44// Debug Profiling. 45#include "platform/profiler.h" 46 47//----------------------------------------------------------------------------- 48 49IMPLEMENT_CONOBJECT(GameObjectAsset); 50 51ConsoleType(GameObjectAssetPtr, TypeGameObjectAssetPtr, GameObjectAsset, ASSET_ID_FIELD_PREFIX) 52 53//----------------------------------------------------------------------------- 54 55ConsoleGetType(TypeGameObjectAssetPtr) 56{ 57 // Fetch asset Id. 58 return (*((AssetPtr<GameObjectAsset>*)dptr)).getAssetId(); 59} 60 61//----------------------------------------------------------------------------- 62 63ConsoleSetType(TypeGameObjectAssetPtr) 64{ 65 // Was a single argument specified? 66 if (argc == 1) 67 { 68 // Yes, so fetch field value. 69 const char* pFieldValue = argv[0]; 70 71 // Fetch asset pointer. 72 AssetPtr<GameObjectAsset>* pAssetPtr = dynamic_cast<AssetPtr<GameObjectAsset>*>((AssetPtrBase*)(dptr)); 73 74 // Is the asset pointer the correct type? 75 if (pAssetPtr == NULL) 76 { 77 // No, so fail. 78 //Con::warnf("(TypeGameObjectAssetPtr) - Failed to set asset Id '%d'.", pFieldValue); 79 return; 80 } 81 82 // Set asset. 83 pAssetPtr->setAssetId(pFieldValue); 84 85 return; 86 } 87 88 // Warn. 89 Con::warnf("(TypeGameObjectAssetPtr) - Cannot set multiple args to a single asset."); 90} 91 92//----------------------------------------------------------------------------- 93 94GameObjectAsset::GameObjectAsset() 95{ 96 mGameObjectName = StringTable->EmptyString(); 97 mScriptFile = StringTable->EmptyString(); 98 mTAMLFile = StringTable->EmptyString(); 99 mScriptPath = StringTable->EmptyString(); 100 mTAMLPath = StringTable->EmptyString(); 101} 102 103//----------------------------------------------------------------------------- 104 105GameObjectAsset::~GameObjectAsset() 106{ 107} 108 109//----------------------------------------------------------------------------- 110 111void GameObjectAsset::initPersistFields() 112{ 113 // Call parent. 114 Parent::initPersistFields(); 115 116 addField("gameObjectName", TypeString, Offset(mGameObjectName, GameObjectAsset), "Name of the game object. Defines the created object's class."); 117 118 addProtectedField("scriptFile", TypeAssetLooseFilePath, Offset(mScriptFile, GameObjectAsset), 119 &setScriptFile, &getScriptFile, "Path to the script file for the GameObject's script code."); 120 addProtectedField("TAMLFile", TypeAssetLooseFilePath, Offset(mTAMLFile, GameObjectAsset), 121 &setTAMLFile, &getTAMLFile, "Path to the taml file for the GameObject's heirarchy."); 122} 123 124//------------------------------------------------------------------------------ 125 126void GameObjectAsset::copyTo(SimObject* object) 127{ 128 // Call to parent. 129 Parent::copyTo(object); 130} 131 132void GameObjectAsset::initializeAsset() 133{ 134 //Ensure we have an expanded filepath 135 mScriptPath = getOwned() ? expandAssetFilePath(mScriptFile) : mScriptPath; 136 137 if (Platform::isFile(mScriptPath)) 138 Con::executeFile(mScriptPath, false, false); 139 140 mTAMLPath = getOwned() ? expandAssetFilePath(mTAMLFile) : mTAMLPath; 141} 142 143void GameObjectAsset::onAssetRefresh() 144{ 145 //Ensure we have an expanded filepath 146 mScriptPath = getOwned() ? expandAssetFilePath(mScriptFile) : mScriptPath; 147 148 if (Platform::isFile(mScriptPath)) 149 Con::executeFile(mScriptPath, false, false); 150 151 mTAMLPath = getOwned() ? expandAssetFilePath(mTAMLFile) : mTAMLPath; 152} 153 154void GameObjectAsset::setScriptFile(const char* pScriptFile) 155{ 156 // Sanity! 157 AssertFatal(pScriptFile != NULL, "Cannot use a NULL script file."); 158 159 // Fetch image file. 160 pScriptFile = StringTable->insert(pScriptFile); 161 162 // Ignore no change, 163 if (pScriptFile == mScriptFile) 164 return; 165 166 // Update. 167 mScriptFile = getOwned() ? expandAssetFilePath(pScriptFile) : pScriptFile; 168 169 // Refresh the asset. 170 refreshAsset(); 171} 172 173 174void GameObjectAsset::setTAMLFile(const char* pTAMLFile) 175{ 176 // Sanity! 177 AssertFatal(pTAMLFile != NULL, "Cannot use a NULL TAML file."); 178 179 // Fetch image file. 180 pTAMLFile = StringTable->insert(pTAMLFile); 181 182 // Ignore no change, 183 if (pTAMLFile == mTAMLFile) 184 return; 185 186 // Update. 187 mTAMLFile = getOwned() ? expandAssetFilePath(pTAMLFile) : pTAMLFile; 188 189 // Refresh the asset. 190 refreshAsset(); 191} 192 193 194const char* GameObjectAsset::create() 195{ 196 if (!Platform::isFile(mTAMLFile)) 197 return ""; 198 199 // Set the format mode. 200 Taml taml; 201 202 // Yes, so set it. 203 taml.setFormatMode(Taml::getFormatModeEnum("xml")); 204 205 // Turn-off auto-formatting. 206 taml.setAutoFormat(false); 207 208 // Read object. 209 SimObject* pSimObject = taml.read(mTAMLFile); 210 211 // Did we find the object? 212 if (pSimObject == NULL) 213 { 214 // No, so warn. 215 Con::warnf("GameObjectAsset::create() - Could not read object from file '%s'.", mTAMLFile); 216 return ""; 217 } 218 219 //Flag it so we know where it came from 220 //Entity* e = dynamic_cast<Entity*>(pSimObject); 221 //e->_setGameObject(getAssetId()); 222 223 pSimObject->setDataField(StringTable->insert("GameObject"), nullptr, getAssetId()); 224 225 return pSimObject->getIdString(); 226} 227 228DefineEngineMethod(GameObjectAsset, createObject, const char*, (),, 229 "Creates an instance of the given GameObject given the asset definition.\n" 230 "@return The GameObject entity created from the asset.") 231{ 232 return object->create(); 233} 234 235//----------------------------------------------------------------------------- 236// GuiInspectorTypeAssetId 237//----------------------------------------------------------------------------- 238 239IMPLEMENT_CONOBJECT(GuiInspectorTypeGameObjectAssetPtr); 240 241ConsoleDocClass(GuiInspectorTypeGameObjectAssetPtr, 242 "@brief Inspector field type for Game Objects\n\n" 243 "Editor use only.\n\n" 244 "@internal" 245); 246 247void GuiInspectorTypeGameObjectAssetPtr::consoleInit() 248{ 249 Parent::consoleInit(); 250 251 ConsoleBaseType::getType(TypeGameObjectAssetPtr)->setInspectorFieldType("GuiInspectorTypeGameObjectAssetPtr"); 252} 253 254GuiControl* GuiInspectorTypeGameObjectAssetPtr::constructEditControl() 255{ 256 // Create "Open in ShapeEditor" button 257 mGameObjectEditButton = new GuiButtonCtrl(); 258 259 // Change filespec 260 char szBuffer[512]; 261 dSprintf(szBuffer, sizeof(szBuffer), "%d.onClick(%s);", this->getId(), mCaption); 262 mGameObjectEditButton->setField("Command", szBuffer); 263 264 mGameObjectEditButton->setDataField(StringTable->insert("Profile"), NULL, "ToolsGuiButtonProfile"); 265 mGameObjectEditButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "ToolsGuiToolTipProfile"); 266 mGameObjectEditButton->setDataField(StringTable->insert("hovertime"), NULL, "1000"); 267 268 const char* assetId = getData(); 269 270 if (dStrEqual(assetId, "")) 271 { 272 mGameObjectEditButton->setText("Create Game Object"); 273 274 mGameObjectEditButton->setDataField(StringTable->insert("tooltip"), NULL, "Convert this object into a reusable Game Object asset."); 275 } 276 else 277 { 278 GameObjectAsset* goAsset = AssetDatabase.acquireAsset< GameObjectAsset>(assetId); 279 280 if (goAsset) 281 { 282 mGameObjectEditButton->setText("Edit Game Object"); 283 284 mGameObjectEditButton->setDataField(StringTable->insert("tooltip"), NULL, "Edit this object instance or Game Object asset."); 285 } 286 else 287 { 288 mGameObjectEditButton->setText("Create Game Object"); 289 290 mGameObjectEditButton->setDataField(StringTable->insert("tooltip"), NULL, "Convert this object into a reusable Game Object asset."); 291 } 292 } 293 294 //mGameObjectEditButton->registerObject(); 295 _registerEditControl(mGameObjectEditButton); 296 297 addObject(mGameObjectEditButton); 298 299 return mGameObjectEditButton; 300} 301 302bool GuiInspectorTypeGameObjectAssetPtr::updateRects() 303{ 304 S32 dividerPos, dividerMargin; 305 mInspector->getDivider(dividerPos, dividerMargin); 306 Point2I fieldExtent = getExtent(); 307 Point2I fieldPos = getPosition(); 308 309 mCaptionRect.set(0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y); 310 mEditCtrlRect.set(fieldExtent.x - dividerPos + dividerMargin, 1, dividerPos - dividerMargin, fieldExtent.y); 311 312 bool resized = mEdit->resize(mEditCtrlRect.point, mEditCtrlRect.extent); 313 if (mGameObjectEditButton != NULL) 314 { 315 resized |= mGameObjectEditButton->resize(mEditCtrlRect.point, mEditCtrlRect.extent); 316 } 317 318 return resized; 319} 320