gameMode.cpp 13 KB

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