Editor.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "StdInc.h"
  2. #include "Editor.h"
  3. #include "../lib/VCMI_Lib.h"
  4. #include "../lib/VCMIDirs.h"
  5. #include "../lib/filesystem/CResourceLoader.h"
  6. #include "../lib/CGeneralTextHandler.h"
  7. #include "../lib/mapping/CMap.h"
  8. #include "../lib/mapping/CMapService.h"
  9. #include "../lib/logging/CBasicLogConfigurator.h"
  10. Editor::Editor(QWidget *parent)
  11. : QMainWindow(parent)
  12. {
  13. // Setup default logging(enough for now)
  14. console = new CConsoleHandler;
  15. CBasicLogConfigurator logConfig(VCMIDirs::get().localPath() + "/VCMI_Editor_log.txt", console);
  16. logConfig.configureDefault();
  17. preinitDLL(console);
  18. loadDLLClasses();
  19. VLC->generaltexth->readToVector("DATA/EDITOR", txtEditor);
  20. VLC->generaltexth->readToVector("DATA/EDITRCMD", txtEditorCmd);
  21. ui.setupUi(this);
  22. createMenus();
  23. }
  24. Editor::~Editor()
  25. {
  26. }
  27. void Editor::createMenus()
  28. {
  29. std::map<std::string, std::function<void()> > actions; //connect these to actions
  30. enum MenuName {FILE=0, EDIT, VIEW, TOOLS, PLAYER, HELP, //main level
  31. TERRAIN=0, RIVER, ROADS, ERASE, OBSTACLES, OBJECTS}; //tools submenus
  32. //txts are in wrong order
  33. std::swap(txtEditor[793], txtEditor[797]);
  34. std::swap(txtEditor[794], txtEditor[797]);
  35. std::swap(txtEditor[795], txtEditor[797]);
  36. std::swap(txtEditor[796], txtEditor[797]);
  37. //setting up actions
  38. actions["file|1"] = [&]()
  39. {
  40. QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
  41. "",
  42. tr("Files (*.h3m)"));
  43. std::ifstream is;
  44. is.open (fileName.toStdString().c_str(), std::ios::binary );
  45. // get length of file
  46. is.seekg (0, std::ios::end);
  47. int length = is.tellg();
  48. is.seekg (0, std::ios::beg);
  49. char* buffer = new char [length];
  50. is.read (buffer, length);
  51. is.close();
  52. map = CMapService::loadMap((ui8*)buffer, length);
  53. };
  54. //setting up menus
  55. QMenu * menus[6];
  56. for(int i=0; i<6; ++i)
  57. menus[i] = menuBar()->addMenu(tr(txtEditor[751+i].c_str()));
  58. auto addMenu = [&](QMenu ** menuList, int txtBegin, int count, std::string actionBase, MenuName mn, const std::vector<int> & separators)
  59. {
  60. for(int i=0; i<count; ++i)
  61. {
  62. if(vstd::contains(separators, i))
  63. menuList[mn]->addSeparator();
  64. QAction * qa = new QAction(tr(txtEditor[txtBegin+i].c_str()), menus[mn]);
  65. std::string actionName = actionBase + "|" + boost::lexical_cast<std::string>(i);
  66. if(vstd::contains(actions, actionName))
  67. {
  68. QObject::connect(qa, &QAction::triggered, actions[actionName]);
  69. }
  70. menuList[mn]->addAction(qa);
  71. }
  72. };
  73. //terrain submenus
  74. QMenu* toolMenus[6];
  75. for(int i=0; i<6; ++i)
  76. toolMenus[i] = menus[TOOLS]->addMenu(tr(txtEditor[789+i].c_str()));
  77. using namespace boost::assign;
  78. std::vector<int> seps;
  79. seps += 4;
  80. addMenu(menus, 758, 6, "file", FILE, seps);
  81. seps.clear(); seps += 2, 6, 8;
  82. addMenu(menus, 860, 10, "edit", EDIT, seps);
  83. seps.clear(); seps += 2, 3, 7;
  84. addMenu(menus, 778, 10, "view", VIEW, seps);
  85. seps.clear(); seps += 0, 2;
  86. addMenu(menus, 795, 3, "tools", TOOLS, seps);
  87. seps.clear();
  88. addMenu(menus, 846, 9, "player", PLAYER, seps);
  89. seps.clear(); seps += 1;
  90. addMenu(menus, 856, 2, "help", HELP, seps);
  91. seps.clear(); seps += 4;
  92. addMenu(toolMenus, 799, 14, "tools|terrain", TERRAIN, seps);
  93. seps.clear();;
  94. addMenu(toolMenus, 814, 5, "tools|rivers", RIVER, seps);
  95. seps.clear();
  96. addMenu(toolMenus, 820, 4, "tools|roads", ROADS, seps);
  97. seps.clear();
  98. addMenu(toolMenus, 825, 4, "tools|erase", ERASE, seps);
  99. seps.clear(); seps += 16;
  100. addMenu(toolMenus, 872, 17, "tools|obstacles", OBSTACLES, seps);
  101. seps.clear();
  102. addMenu(toolMenus, 830, 15, "tools|objects", OBJECTS, seps);
  103. }