gameMode.cpp 12 KB

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