gameMode.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. #include "gameMode.h"
  2. #ifdef TORQUE_TOOLS
  3. #include "gui/containers/guiDynamicCtrlArrayCtrl.h"
  4. #endif
  5. #include "console/arrayObject.h"
  6. IMPLEMENT_CONOBJECT(GameMode);
  7. IMPLEMENT_CALLBACK(GameMode, onActivated, void, (), (),
  8. "@brief Called when a gamemode is activated.\n\n");
  9. IMPLEMENT_CALLBACK(GameMode, onDeactivated, void, (), (),
  10. "@brief Called when a gamemode is deactivated.\n\n");
  11. IMPLEMENT_CALLBACK(GameMode, onSceneLoaded, void, (), (),
  12. "@brief Called when a scene has been loaded and has game mode implications.\n\n");
  13. IMPLEMENT_CALLBACK(GameMode, onSceneUnloaded, void, (), (),
  14. "@brief Called when a scene has been unloaded and has game mode implications.\n\n");
  15. IMPLEMENT_CALLBACK(GameMode, onSubsceneLoaded, void, (SubScene*), ("SubScene"),
  16. "@brief Called when a subScene has been loaded and has game mode implications.\n\n");
  17. IMPLEMENT_CALLBACK(GameMode, onSubsceneUnloaded, void, (SubScene*), ("SubScene"),
  18. "@brief Called when a subScene has been unloaded and has game mode implications.\n\n");
  19. ConsoleType(GameModeList, TypeGameModeList, const char*, "")
  20. ConsoleGetType(TypeGameModeList)
  21. {
  22. // Fetch asset Id.
  23. return *((const char**)(dptr));
  24. }
  25. //-----------------------------------------------------------------------------
  26. ConsoleSetType(TypeGameModeList)
  27. {
  28. // Was a single argument specified?
  29. if (argc == 1)
  30. {
  31. // Yes, so fetch field value.
  32. *((const char**)dptr) = StringTable->insert(argv[0]);
  33. return;
  34. }
  35. // Warn.
  36. //Con::warnf("(TypeGameModeList) - Cannot set multiple args to a single asset.");
  37. }
  38. GameMode::GameMode() :
  39. mGameModeName(StringTable->EmptyString()),
  40. mGameModeDesc(StringTable->EmptyString()),
  41. mIsActive(false),
  42. mIsAlwaysActive(false)
  43. {
  44. }
  45. void GameMode::initPersistFields()
  46. {
  47. Parent::initPersistFields();
  48. addField("gameModeName", TypeString, Offset(mGameModeName, GameMode), "Human-readable name of the gamemode");
  49. addField("description", TypeString, Offset(mGameModeDesc, GameMode), "Description of the gamemode");
  50. INITPERSISTFIELD_IMAGEASSET(PreviewImage, GameMode, "Preview Image");
  51. addField("active", TypeBool, Offset(mIsActive, GameMode), "Is the gamemode active");
  52. addField("alwaysActive", TypeBool, Offset(mIsAlwaysActive, GameMode), "Is the gamemode always active");
  53. }
  54. bool GameMode::onAdd()
  55. {
  56. if (!Parent::onAdd())
  57. return false;
  58. return true;
  59. }
  60. void GameMode::onRemove()
  61. {
  62. Parent::onRemove();
  63. }
  64. void GameMode::findGameModes(const char* gameModeList, Vector<GameMode*> *outGameModes)
  65. {
  66. if (outGameModes == nullptr)
  67. return;
  68. Vector<String> gameModeNames;
  69. U32 uCount = StringUnit::getUnitCount(gameModeList, ";");
  70. for (U32 i = 0; i < uCount; i++)
  71. {
  72. String name = StringUnit::getUnit(gameModeList, i, ";");
  73. if (!name.isEmpty())
  74. gameModeNames.push_back(name);
  75. }
  76. for (U32 i = 0; i < gameModeNames.size(); i++)
  77. {
  78. GameMode* gm;
  79. if (Sim::findObject(gameModeNames[i].c_str(), gm))
  80. {
  81. if (gm->isActive() || gm->isAlwaysActive())
  82. outGameModes->push_back(gm);
  83. }
  84. }
  85. }
  86. void GameMode::setActive(const bool& active)
  87. {
  88. mIsActive = active;
  89. if (mIsActive)
  90. onActivated_callback();
  91. else
  92. onDeactivated_callback();
  93. }
  94. void GameMode::setAlwaysActive(const bool& alwaysActive)
  95. {
  96. mIsAlwaysActive = alwaysActive;
  97. }
  98. DefineEngineMethod(GameMode, isActive, bool, (), ,
  99. "Returns if the GameMode is currently active.\n"
  100. "@return The active status of the GameMode")
  101. {
  102. return object->isActive();
  103. }
  104. DefineEngineMethod(GameMode, setActive, void, (bool active), (true),
  105. "Sets the active state of the GameMode.\n"
  106. "@param active A bool of the state the GameMode should be set to")
  107. {
  108. object->setActive(active);
  109. }
  110. DefineEngineMethod(GameMode, isALwaysActive, bool, (), ,
  111. "Returns if the GameMode is currently active.\n"
  112. "@return The active status of the GameMode")
  113. {
  114. return object->isActive();
  115. }
  116. DefineEngineMethod(GameMode, setAlwaysActive, void, (bool alwaysActive), (true),
  117. "Sets the active state of the GameMode.\n"
  118. "@param active A bool of the state the GameMode should be set to")
  119. {
  120. object->setAlwaysActive(alwaysActive);
  121. }
  122. DefineEngineFunction(getGameModesList, ArrayObject*, (), , "")
  123. {
  124. ArrayObject* dictionary = new ArrayObject();
  125. dictionary->registerObject();
  126. char activeValBuffer[16];
  127. for (SimGroup::iterator itr = Sim::getRootGroup()->begin(); itr != Sim::getRootGroup()->end(); itr++)
  128. {
  129. GameMode* gm = dynamic_cast<GameMode*>(*itr);
  130. if (gm)
  131. {
  132. dSprintf(activeValBuffer, 16, "%d", (gm->mIsActive || gm->mIsAlwaysActive));
  133. dictionary->push_back(gm->getName(), activeValBuffer);
  134. }
  135. }
  136. return dictionary;
  137. }
  138. //-----------------------------------------------------------------------------
  139. // GuiInspectorTypeAssetId
  140. //-----------------------------------------------------------------------------
  141. #ifdef TORQUE_TOOLS
  142. GuiInspectorTypeGameModeList::GuiInspectorTypeGameModeList()
  143. : mHelper(NULL),
  144. mRollout(NULL),
  145. mArrayCtrl(NULL)
  146. {
  147. }
  148. IMPLEMENT_CONOBJECT(GuiInspectorTypeGameModeList);
  149. ConsoleDocClass(GuiInspectorTypeGameModeList,
  150. "@brief Inspector field type for selecting GameModes\n\n"
  151. "Editor use only.\n\n"
  152. "@internal"
  153. );
  154. bool GuiInspectorTypeGameModeList::onAdd()
  155. {
  156. // Skip our parent because we aren't using mEditCtrl
  157. // and according to our parent that would be cause to fail onAdd.
  158. if (!Parent::Parent::onAdd())
  159. return false;
  160. if (!mInspector)
  161. return false;
  162. //build out our list of gamemodes
  163. Vector<GameMode*> gameModesList;
  164. for (SimGroup::iterator itr = Sim::getRootGroup()->begin(); itr != Sim::getRootGroup()->end(); itr++)
  165. {
  166. GameMode* gm = dynamic_cast<GameMode*>(*itr);
  167. if (gm)
  168. gameModesList.push_back(gm);
  169. }
  170. static StringTableEntry sProfile = StringTable->insert("profile");
  171. setDataField(sProfile, NULL, "GuiInspectorFieldProfile");
  172. setBounds(0, 0, 100, 18);
  173. // Allocate our children controls...
  174. mRollout = new GuiRolloutCtrl();
  175. mRollout->setMargin(14, 0, 0, 0);
  176. mRollout->setCanCollapse(false);
  177. mRollout->registerObject();
  178. addObject(mRollout);
  179. mArrayCtrl = new GuiDynamicCtrlArrayControl();
  180. mArrayCtrl->setDataField(sProfile, NULL, "GuiInspectorBitMaskArrayProfile");
  181. mArrayCtrl->setField("autoCellSize", "true");
  182. mArrayCtrl->setField("fillRowFirst", "true");
  183. mArrayCtrl->setField("dynamicSize", "true");
  184. mArrayCtrl->setField("rowSpacing", "4");
  185. mArrayCtrl->setField("colSpacing", "1");
  186. mArrayCtrl->setField("frozen", "true");
  187. mArrayCtrl->registerObject();
  188. mRollout->addObject(mArrayCtrl);
  189. GuiCheckBoxCtrl* pCheckBox = NULL;
  190. for (S32 i = 0; i < gameModesList.size(); i++)
  191. {
  192. pCheckBox = new GuiCheckBoxCtrl();
  193. pCheckBox->setText(gameModesList[i]->getName());
  194. pCheckBox->registerObject();
  195. mArrayCtrl->addObject(pCheckBox);
  196. pCheckBox->autoSize();
  197. // Override the normal script callbacks for GuiInspectorTypeCheckBox
  198. char szBuffer[512];
  199. dSprintf(szBuffer, 512, "%d.applyValue();", getId());
  200. pCheckBox->setField("Command", szBuffer);
  201. }
  202. mArrayCtrl->setField("frozen", "false");
  203. mArrayCtrl->refresh();
  204. mHelper = new GuiInspectorTypeGameModeListHelper();
  205. mHelper->init(mInspector, mParent);
  206. mHelper->mParentRollout = mRollout;
  207. mHelper->mParentField = this;
  208. mHelper->setInspectorField(mField, mCaption, mFieldArrayIndex);
  209. mHelper->registerObject();
  210. mHelper->setExtent(pCheckBox->getExtent());
  211. mHelper->setPosition(0, 0);
  212. mRollout->addObject(mHelper);
  213. mRollout->sizeToContents();
  214. mRollout->instantCollapse();
  215. updateValue();
  216. return true;
  217. }
  218. void GuiInspectorTypeGameModeList::consoleInit()
  219. {
  220. Parent::consoleInit();
  221. ConsoleBaseType::getType(TypeGameModeList)->setInspectorFieldType("GuiInspectorTypeGameModeList");
  222. }
  223. void GuiInspectorTypeGameModeList::childResized(GuiControl* child)
  224. {
  225. setExtent(mRollout->getExtent());
  226. }
  227. bool GuiInspectorTypeGameModeList::resize(const Point2I& newPosition, const Point2I& newExtent)
  228. {
  229. if (!Parent::resize(newPosition, newExtent))
  230. return false;
  231. // Hack... height of 18 is hardcoded
  232. return mHelper->resize(Point2I(0, 0), Point2I(newExtent.x, 18));
  233. }
  234. bool GuiInspectorTypeGameModeList::updateRects()
  235. {
  236. if (!mRollout)
  237. return false;
  238. bool result = mRollout->setExtent(getExtent());
  239. for (U32 i = 0; i < mArrayCtrl->size(); i++)
  240. {
  241. GuiInspectorField* pField = dynamic_cast<GuiInspectorField*>(mArrayCtrl->at(i));
  242. if (pField)
  243. if (pField->updateRects())
  244. result = true;
  245. }
  246. if (mHelper && mHelper->updateRects())
  247. result = true;
  248. return result;
  249. }
  250. StringTableEntry GuiInspectorTypeGameModeList::getValue()
  251. {
  252. if (!mRollout)
  253. return StringTable->insert("");
  254. String results = "";
  255. for (U32 i = 0; i < mArrayCtrl->size(); i++)
  256. {
  257. GuiCheckBoxCtrl* pCheckBox = dynamic_cast<GuiCheckBoxCtrl*>(mArrayCtrl->at(i));
  258. if (pCheckBox->getStateOn())
  259. results += pCheckBox->getText() + String(";");
  260. }
  261. if (!results.isEmpty())
  262. return StringTable->insert(results.c_str());
  263. else
  264. return StringTable->EmptyString();
  265. }
  266. void GuiInspectorTypeGameModeList::setValue(StringTableEntry value)
  267. {
  268. Vector<String> gameModeNames;
  269. U32 uCount = StringUnit::getUnitCount(value, ";");
  270. for (U32 i = 0; i < uCount; i++)
  271. {
  272. String name = StringUnit::getUnit(value, i, ";");
  273. if (!name.isEmpty())
  274. gameModeNames.push_back(name);
  275. }
  276. for (U32 i = 0; i < mArrayCtrl->size(); i++)
  277. {
  278. GuiCheckBoxCtrl* pCheckBox = dynamic_cast<GuiCheckBoxCtrl*>(mArrayCtrl->at(i));
  279. for (U32 m = 0; m < gameModeNames.size(); m++)
  280. {
  281. if (gameModeNames[m].equal(pCheckBox->getText()))
  282. {
  283. pCheckBox->setStateOn(true);
  284. }
  285. }
  286. }
  287. mHelper->setValue(value);
  288. }
  289. void GuiInspectorTypeGameModeList::updateData()
  290. {
  291. StringTableEntry data = getValue();
  292. setData(data);
  293. }
  294. DefineEngineMethod(GuiInspectorTypeGameModeList, applyValue, void, (), , "")
  295. {
  296. object->updateData();
  297. }
  298. GuiInspectorTypeGameModeListHelper::GuiInspectorTypeGameModeListHelper()
  299. : mButton(NULL),
  300. mParentRollout(NULL),
  301. mParentField(NULL)
  302. {
  303. }
  304. IMPLEMENT_CONOBJECT(GuiInspectorTypeGameModeListHelper);
  305. ConsoleDocClass(GuiInspectorTypeGameModeListHelper,
  306. "@brief Inspector field type support for GameModes lists.\n\n"
  307. "Editor use only.\n\n"
  308. "@internal"
  309. );
  310. GuiControl* GuiInspectorTypeGameModeListHelper::constructEditControl()
  311. {
  312. GuiControl* retCtrl = new GuiTextEditCtrl();
  313. retCtrl->setDataField(StringTable->insert("profile"), NULL, "GuiInspectorTextEditProfile");
  314. retCtrl->setField("hexDisplay", "true");
  315. _registerEditControl(retCtrl);
  316. char szBuffer[512];
  317. dSprintf(szBuffer, 512, "%d.apply(%d.getText());", mParentField->getId(), retCtrl->getId());
  318. retCtrl->setField("AltCommand", szBuffer);
  319. retCtrl->setField("Validate", szBuffer);
  320. mButton = new GuiBitmapButtonCtrl();
  321. RectI browseRect(Point2I((getLeft() + getWidth()) - 26, getTop() + 2), Point2I(20, getHeight() - 4));
  322. dSprintf(szBuffer, 512, "%d.toggleExpanded(false);", mParentRollout->getId());
  323. mButton->setField("Command", szBuffer);
  324. mButton->setField("buttonType", "ToggleButton");
  325. mButton->setDataField(StringTable->insert("Profile"), NULL, "GuiInspectorButtonProfile");
  326. mButton->setBitmap(StringTable->insert("ToolsModule:arrowBtn_N_image"));
  327. mButton->setStateOn(true);
  328. mButton->setExtent(16, 16);
  329. mButton->registerObject();
  330. addObject(mButton);
  331. mButton->resize(browseRect.point, browseRect.extent);
  332. return retCtrl;
  333. }
  334. bool GuiInspectorTypeGameModeListHelper::resize(const Point2I& newPosition, const Point2I& newExtent)
  335. {
  336. if (!Parent::resize(newPosition, newExtent))
  337. return false;
  338. if (mEdit != NULL)
  339. {
  340. return updateRects();
  341. }
  342. return false;
  343. }
  344. bool GuiInspectorTypeGameModeListHelper::updateRects()
  345. {
  346. S32 dividerPos, dividerMargin;
  347. mInspector->getDivider(dividerPos, dividerMargin);
  348. Point2I fieldExtent = getExtent();
  349. Point2I fieldPos = getPosition();
  350. mCaptionRect.set(0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y);
  351. mEditCtrlRect.set(fieldExtent.x - dividerPos + dividerMargin, 1, dividerPos - dividerMargin - 32, fieldExtent.y);
  352. bool editResize = mEdit->resize(mEditCtrlRect.point, mEditCtrlRect.extent);
  353. bool buttonResize = false;
  354. if (mButton != NULL)
  355. {
  356. mButtonRect.set(fieldExtent.x - 26, 2, 16, 16);
  357. buttonResize = mButton->resize(mButtonRect.point, mButtonRect.extent);
  358. }
  359. return (editResize || buttonResize);
  360. }
  361. void GuiInspectorTypeGameModeListHelper::setValue(StringTableEntry newValue)
  362. {
  363. GuiTextEditCtrl* edit = dynamic_cast<GuiTextEditCtrl*>(mEdit);
  364. edit->setText(newValue);
  365. }
  366. #endif