Editor.cpp 3.5 KB

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