worldsmenu.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #ifdef GAME_TEMPLATE
  2. [EditorAttribute("box", "GameLib/Scripted", "Worlds menu", "-0.25 -0.25 -0.25", "0.25 0.25 0.25", "255 0 0 255")]
  3. class WorldsMenuClass
  4. {
  5. }
  6. WorldsMenuClass WorldsMenuSource;
  7. class WorldsMenu: GenericEntity
  8. {
  9. int m_DbgListSelection = 0;
  10. int m_WorldsCount = 0;
  11. string DEFAULT_WORLD = "worlds/default.ent";
  12. ref array<string> m_DbgOptions = {};
  13. ref ImageWidget m_MouseWidget;
  14. void WorldsMenu(IEntitySource src, IEntity parent)
  15. {
  16. SetFlags(EntityFlags.ACTIVE, false);
  17. SetEventMask(EntityEvent.POSTFRAME);
  18. Class.CastTo(m_MouseWidget, GetGame().GetWorkspace().CreateWidgets("gui/layouts/mouse.layout"));
  19. m_MouseWidget.SetSort(1024);
  20. SetCursorWidget(m_MouseWidget);
  21. LoadWorlds();
  22. }
  23. void ~WorldsMenu()
  24. {
  25. delete m_MouseWidget;
  26. }
  27. array<string> GetWorldList()
  28. {
  29. return m_DbgOptions;
  30. }
  31. override void EOnPostFrame(IEntity other, int extra) //EntityEvent.POSTFRAME
  32. {
  33. InputManager im = GetGame().GetInputManager();
  34. im.ActivateContext("MenuContext");
  35. bool menuSelect = im.GetActionTriggered("MenuSelect");
  36. bool menuBack = im.GetActionTriggered("MenuBack");
  37. DbgUI.Begin("Load world", 400, 100);
  38. DbgUI.Text("Select world to load from worlds directory");
  39. if (m_DbgOptions.Count() > 0)
  40. {
  41. DbgUI.List("Worlds", m_DbgListSelection, m_DbgOptions);
  42. if (DbgUI.Button("Start") || menuSelect)
  43. {
  44. string worldToLoad = m_DbgOptions.Get(m_DbgListSelection);
  45. GetGame().SetWorldFile(worldToLoad, true);
  46. }
  47. }
  48. if (DbgUI.Button("Exit") || menuBack)
  49. {
  50. GetGame().RequestClose();
  51. }
  52. DbgUI.End();
  53. }
  54. void LoadWorlds()
  55. {
  56. string fileName;
  57. FileAttr fileAttr;
  58. FindFileHandle worlds = FindFile("worlds/*.ent", fileName, fileAttr, 0);
  59. if (!worlds)
  60. return;
  61. InsertWorldToList(fileName);
  62. while(FindNextFile(worlds, fileName, fileAttr))
  63. {
  64. InsertWorldToList(fileName);
  65. }
  66. CloseFindFile(worlds);
  67. m_WorldsCount = m_DbgOptions.Count();
  68. }
  69. void InsertWorldToList(string fileName)
  70. {
  71. string path = String("worlds/" + fileName);
  72. if (path != DEFAULT_WORLD)
  73. m_DbgOptions.Insert(String(path));
  74. }
  75. }
  76. #endif