No Description

mainwindow.cpp 38KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include "project.h"
  4. #include "editor.h"
  5. #include "objectpropertiesframe.h"
  6. #include "ui_objectpropertiesframe.h"
  7. #include <QDebug>
  8. #include <QFileDialog>
  9. #include <QStandardItemModel>
  10. #include <QShortcut>
  11. #include <QSettings>
  12. #include <QSpinBox>
  13. #include <QTextEdit>
  14. #include <QSpacerItem>
  15. #include <QFont>
  16. #include <QScrollBar>
  17. #include <QMessageBox>
  18. #include <QDialogButtonBox>
  19. MainWindow::MainWindow(QWidget *parent) :
  20. QMainWindow(parent),
  21. ui(new Ui::MainWindow)
  22. {
  23. QCoreApplication::setOrganizationName("pret");
  24. QCoreApplication::setApplicationName("pretmap");
  25. ui->setupUi(this);
  26. ui->newEventToolButton->initButton();
  27. connect(ui->newEventToolButton, SIGNAL(newEventAdded(QString)), this, SLOT(addNewEvent(QString)));
  28. new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Z), this, SLOT(redo()));
  29. editor = new Editor(ui);
  30. connect(editor, SIGNAL(objectsChanged()), this, SLOT(updateSelectedObjects()));
  31. connect(editor, SIGNAL(selectedObjectsChanged()), this, SLOT(updateSelectedObjects()));
  32. connect(editor, SIGNAL(loadMapRequested(QString, QString)), this, SLOT(onLoadMapRequested(QString, QString)));
  33. connect(editor, SIGNAL(tilesetChanged(QString)), this, SLOT(onTilesetChanged(QString)));
  34. connect(editor, SIGNAL(warpEventDoubleClicked(QString,QString)), this, SLOT(openWarpMap(QString,QString)));
  35. on_toolButton_Paint_clicked();
  36. QSettings settings;
  37. QString key = "recent_projects";
  38. if (settings.contains(key)) {
  39. QString default_dir = settings.value(key).toStringList().last();
  40. if (!default_dir.isNull()) {
  41. qDebug() << QString("default_dir: '%1'").arg(default_dir);
  42. openProject(default_dir);
  43. }
  44. }
  45. }
  46. MainWindow::~MainWindow()
  47. {
  48. delete ui;
  49. }
  50. void MainWindow::setStatusBarMessage(QString message, int timeout/* = 0*/) {
  51. statusBar()->showMessage(message, timeout);
  52. }
  53. void MainWindow::openProject(QString dir) {
  54. if (dir.isNull()) {
  55. return;
  56. }
  57. setStatusBarMessage(QString("Opening project %1").arg(dir));
  58. bool already_open = (
  59. (editor != NULL && editor != nullptr)
  60. && (editor->project != NULL && editor->project != nullptr)
  61. && (editor->project->root == dir)
  62. );
  63. if (!already_open) {
  64. editor->project = new Project;
  65. editor->project->root = dir;
  66. setWindowTitle(editor->project->getProjectTitle() + " - pretmap");
  67. loadDataStructures();
  68. populateMapList();
  69. setMap(getDefaultMap());
  70. } else {
  71. setWindowTitle(editor->project->getProjectTitle() + " - pretmap");
  72. loadDataStructures();
  73. populateMapList();
  74. }
  75. setStatusBarMessage(QString("Opened project %1").arg(dir));
  76. }
  77. QString MainWindow::getDefaultMap() {
  78. if (editor && editor->project) {
  79. QList<QStringList> names = editor->project->groupedMapNames;
  80. if (!names.isEmpty()) {
  81. QSettings settings;
  82. QString key = "project:" + editor->project->root;
  83. if (settings.contains(key)) {
  84. QMap<QString, QVariant> qmap = settings.value(key).toMap();
  85. if (qmap.contains("recent_map")) {
  86. QString map_name = qmap.value("recent_map").toString();
  87. for (int i = 0; i < names.length(); i++) {
  88. if (names.value(i).contains(map_name)) {
  89. return map_name;
  90. }
  91. }
  92. }
  93. }
  94. // Failing that, just get the first map in the list.
  95. for (int i = 0; i < names.length(); i++) {
  96. QStringList list = names.value(i);
  97. if (list.length()) {
  98. return list.value(0);
  99. }
  100. }
  101. }
  102. }
  103. return QString();
  104. }
  105. QString MainWindow::getExistingDirectory(QString dir) {
  106. return QFileDialog::getExistingDirectory(this, "Open Directory", dir, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
  107. }
  108. void MainWindow::on_action_Open_Project_triggered()
  109. {
  110. QSettings settings;
  111. QString key = "recent_projects";
  112. QString recent = ".";
  113. if (settings.contains(key)) {
  114. recent = settings.value(key).toStringList().last();
  115. }
  116. QString dir = getExistingDirectory(recent);
  117. if (!dir.isEmpty()) {
  118. QStringList recents;
  119. if (settings.contains(key)) {
  120. recents = settings.value(key).toStringList();
  121. }
  122. recents.removeAll(dir);
  123. recents.append(dir);
  124. settings.setValue(key, recents);
  125. openProject(dir);
  126. }
  127. }
  128. void MainWindow::setMap(QString map_name) {
  129. qDebug() << QString("setMap(%1)").arg(map_name);
  130. if (map_name.isNull()) {
  131. return;
  132. }
  133. editor->setMap(map_name);
  134. redrawMapScene();
  135. displayMapProperties();
  136. setWindowTitle(map_name + " - " + editor->project->getProjectTitle() + " - pretmap");
  137. connect(editor->map, SIGNAL(mapChanged(Map*)), this, SLOT(onMapChanged(Map *)));
  138. connect(editor->map, SIGNAL(mapNeedsRedrawing(Map*)), this, SLOT(onMapNeedsRedrawing(Map *)));
  139. connect(editor->map, SIGNAL(statusBarMessage(QString)), this, SLOT(setStatusBarMessage(QString)));
  140. setRecentMap(map_name);
  141. updateMapList();
  142. }
  143. void MainWindow::redrawMapScene()
  144. {
  145. editor->displayMap();
  146. on_tabWidget_currentChanged(ui->tabWidget->currentIndex());
  147. ui->graphicsView_Map->setScene(editor->scene);
  148. ui->graphicsView_Map->setSceneRect(editor->scene->sceneRect());
  149. ui->graphicsView_Map->setFixedSize(editor->scene->width() + 2, editor->scene->height() + 2);
  150. ui->graphicsView_Objects_Map->setScene(editor->scene);
  151. ui->graphicsView_Objects_Map->setSceneRect(editor->scene->sceneRect());
  152. ui->graphicsView_Objects_Map->setFixedSize(editor->scene->width() + 2, editor->scene->height() + 2);
  153. ui->graphicsView_Objects_Map->editor = editor;
  154. ui->graphicsView_Connections->setScene(editor->scene);
  155. ui->graphicsView_Connections->setSceneRect(editor->scene->sceneRect());
  156. ui->graphicsView_Connections->setFixedSize(editor->scene->width() + 2, editor->scene->height() + 2);
  157. ui->graphicsView_Metatiles->setScene(editor->scene_metatiles);
  158. //ui->graphicsView_Metatiles->setSceneRect(editor->scene_metatiles->sceneRect());
  159. ui->graphicsView_Metatiles->setFixedSize(editor->metatiles_item->pixmap().width() + 2, editor->metatiles_item->pixmap().height() + 2);
  160. ui->graphicsView_BorderMetatile->setScene(editor->scene_selected_border_metatiles);
  161. ui->graphicsView_BorderMetatile->setFixedSize(editor->selected_border_metatiles_item->pixmap().width() + 2, editor->selected_border_metatiles_item->pixmap().height() + 2);
  162. ui->graphicsView_Collision->setScene(editor->scene_collision_metatiles);
  163. //ui->graphicsView_Collision->setSceneRect(editor->scene_collision_metatiles->sceneRect());
  164. ui->graphicsView_Collision->setFixedSize(editor->collision_metatiles_item->pixmap().width() + 2, editor->collision_metatiles_item->pixmap().height() + 2);
  165. ui->graphicsView_Elevation->setScene(editor->scene_elevation_metatiles);
  166. //ui->graphicsView_Elevation->setSceneRect(editor->scene_elevation_metatiles->sceneRect());
  167. ui->graphicsView_Elevation->setFixedSize(editor->elevation_metatiles_item->pixmap().width() + 2, editor->elevation_metatiles_item->pixmap().height() + 2);
  168. }
  169. void MainWindow::openWarpMap(QString map_name, QString warp_num) {
  170. // Ensure valid destination map name.
  171. if (!editor->project->mapNames->contains(map_name)) {
  172. qDebug() << QString("Invalid warp destination map name '%1'").arg(map_name);
  173. return;
  174. }
  175. // Ensure valid destination warp number.
  176. bool ok;
  177. int warpNum = warp_num.toInt(&ok, 0);
  178. if (!ok) {
  179. qDebug() << QString("Invalid warp number '%1' for destination map '%2'").arg(warp_num).arg(map_name);
  180. return;
  181. }
  182. // Open the destination map, and select the target warp event.
  183. setMap(map_name);
  184. QList<Event*> warp_events = editor->map->events["warp_event_group"];
  185. if (warp_events.length() > warpNum) {
  186. Event *warp_event = warp_events.at(warpNum);
  187. QList<DraggablePixmapItem *> *all_events = editor->getObjects();
  188. for (DraggablePixmapItem *item : *all_events) {
  189. if (item->event == warp_event) {
  190. editor->selected_events->clear();
  191. editor->selected_events->append(item);
  192. editor->updateSelectedEvents();
  193. }
  194. }
  195. delete all_events;
  196. }
  197. }
  198. void MainWindow::setRecentMap(QString map_name) {
  199. QSettings settings;
  200. QString key = "project:" + editor->project->root;
  201. QMap<QString, QVariant> qmap;
  202. if (settings.contains(key)) {
  203. qmap = settings.value(key).toMap();
  204. }
  205. qmap.insert("recent_map", map_name);
  206. settings.setValue(key, qmap);
  207. }
  208. void MainWindow::displayMapProperties() {
  209. ui->comboBox_Song->clear();
  210. ui->comboBox_Location->clear();
  211. ui->checkBox_Visibility->setChecked(false);
  212. ui->comboBox_Weather->clear();
  213. ui->comboBox_Type->clear();
  214. ui->comboBox_BattleScene->clear();
  215. ui->comboBox_PrimaryTileset->clear();
  216. ui->comboBox_SecondaryTileset->clear();
  217. ui->checkBox_ShowLocation->setChecked(false);
  218. if (!editor || !editor->map || !editor->project) {
  219. ui->frame_3->setEnabled(false);
  220. return;
  221. }
  222. ui->frame_3->setEnabled(true);
  223. Map *map = editor->map;
  224. Project *project = editor->project;
  225. QStringList songs = project->getSongNames();
  226. ui->comboBox_Song->addItems(songs);
  227. ui->comboBox_Song->setCurrentText(map->song);
  228. ui->comboBox_Location->addItems(*project->regionMapSections);
  229. ui->comboBox_Location->setCurrentText(map->location);
  230. QMap<QString, QStringList> tilesets = project->getTilesets();
  231. ui->comboBox_PrimaryTileset->addItems(tilesets.value("primary"));
  232. ui->comboBox_PrimaryTileset->setCurrentText(map->layout->tileset_primary_label);
  233. ui->comboBox_SecondaryTileset->addItems(tilesets.value("secondary"));
  234. ui->comboBox_SecondaryTileset->setCurrentText(map->layout->tileset_secondary_label);
  235. ui->checkBox_Visibility->setChecked(map->requiresFlash.toInt() > 0 || map->requiresFlash == "TRUE");
  236. ui->comboBox_Weather->addItems(*project->weatherNames);
  237. ui->comboBox_Weather->setCurrentText(map->weather);
  238. ui->comboBox_Type->addItems(*project->mapTypes);
  239. ui->comboBox_Type->setCurrentText(map->type);
  240. ui->comboBox_BattleScene->addItems(*project->mapBattleScenes);
  241. ui->comboBox_BattleScene->setCurrentText(map->battle_scene);
  242. ui->checkBox_ShowLocation->setChecked(map->show_location.toInt() > 0 || map->show_location == "TRUE");
  243. }
  244. void MainWindow::on_comboBox_Song_activated(const QString &song)
  245. {
  246. if (editor && editor->map) {
  247. editor->map->song = song;
  248. }
  249. }
  250. void MainWindow::on_comboBox_Location_activated(const QString &location)
  251. {
  252. if (editor && editor->map) {
  253. editor->map->location = location;
  254. }
  255. }
  256. void MainWindow::on_comboBox_Visibility_activated(const QString &requiresFlash)
  257. {
  258. if (editor && editor->map) {
  259. editor->map->requiresFlash = requiresFlash;
  260. }
  261. }
  262. void MainWindow::on_comboBox_Weather_activated(const QString &weather)
  263. {
  264. if (editor && editor->map) {
  265. editor->map->weather = weather;
  266. }
  267. }
  268. void MainWindow::on_comboBox_Type_activated(const QString &type)
  269. {
  270. if (editor && editor->map) {
  271. editor->map->type = type;
  272. }
  273. }
  274. void MainWindow::on_comboBox_BattleScene_activated(const QString &battle_scene)
  275. {
  276. if (editor && editor->map) {
  277. editor->map->battle_scene = battle_scene;
  278. }
  279. }
  280. void MainWindow::on_checkBox_Visibility_clicked(bool checked)
  281. {
  282. if (editor && editor->map) {
  283. if (checked) {
  284. editor->map->requiresFlash = "TRUE";
  285. } else {
  286. editor->map->requiresFlash = "FALSE";
  287. }
  288. }
  289. }
  290. void MainWindow::on_checkBox_ShowLocation_clicked(bool checked)
  291. {
  292. if (editor && editor->map) {
  293. if (checked) {
  294. editor->map->show_location = "TRUE";
  295. } else {
  296. editor->map->show_location = "FALSE";
  297. }
  298. }
  299. }
  300. void MainWindow::loadDataStructures() {
  301. Project *project = editor->project;
  302. project->readMapLayoutsTable();
  303. project->readAllMapLayouts();
  304. project->readRegionMapSections();
  305. project->readItemNames();
  306. project->readFlagNames();
  307. project->readVarNames();
  308. project->readMovementTypes();
  309. project->readMapTypes();
  310. project->readMapBattleScenes();
  311. project->readWeatherNames();
  312. project->readCoordEventWeatherNames();
  313. project->readSecretBaseIds();
  314. project->readBgEventFacingDirections();
  315. project->readMapsWithConnections();
  316. }
  317. void MainWindow::populateMapList() {
  318. Project *project = editor->project;
  319. QIcon mapFolderIcon;
  320. mapFolderIcon.addFile(QStringLiteral(":/icons/folder_closed_map.ico"), QSize(), QIcon::Normal, QIcon::Off);
  321. mapFolderIcon.addFile(QStringLiteral(":/icons/folder_map.ico"), QSize(), QIcon::Normal, QIcon::On);
  322. QIcon folderIcon;
  323. folderIcon.addFile(QStringLiteral(":/icons/folder_closed.ico"), QSize(), QIcon::Normal, QIcon::Off);
  324. mapIcon = new QIcon;
  325. mapIcon->addFile(QStringLiteral(":/icons/map.ico"), QSize(), QIcon::Normal, QIcon::Off);
  326. mapIcon->addFile(QStringLiteral(":/icons/image.ico"), QSize(), QIcon::Normal, QIcon::On);
  327. mapListModel = new QStandardItemModel;
  328. mapGroupsModel = new QList<QStandardItem*>;
  329. QStandardItem *entry = new QStandardItem;
  330. entry->setText(project->getProjectTitle());
  331. entry->setIcon(folderIcon);
  332. entry->setEditable(false);
  333. mapListModel->appendRow(entry);
  334. QStandardItem *maps = new QStandardItem;
  335. maps->setText("maps");
  336. maps->setIcon(folderIcon);
  337. maps->setEditable(false);
  338. entry->appendRow(maps);
  339. project->readMapGroups();
  340. for (int i = 0; i < project->groupNames->length(); i++) {
  341. QString group_name = project->groupNames->value(i);
  342. QStandardItem *group = new QStandardItem;
  343. group->setText(group_name);
  344. group->setIcon(mapFolderIcon);
  345. group->setEditable(false);
  346. group->setData(group_name, Qt::UserRole);
  347. group->setData("map_group", MapListUserRoles::TypeRole);
  348. group->setData(i, MapListUserRoles::GroupRole);
  349. maps->appendRow(group);
  350. mapGroupsModel->append(group);
  351. QStringList names = project->groupedMapNames.value(i);
  352. for (int j = 0; j < names.length(); j++) {
  353. QString map_name = names.value(j);
  354. QStandardItem *map = createMapItem(map_name, i, j);
  355. group->appendRow(map);
  356. }
  357. }
  358. // Right-clicking on items in the map list tree view brings up a context menu.
  359. ui->mapList->setContextMenuPolicy(Qt::CustomContextMenu);
  360. connect(ui->mapList, SIGNAL(customContextMenuRequested(const QPoint &)),
  361. this, SLOT(onOpenMapListContextMenu(const QPoint &)));
  362. ui->mapList->setModel(mapListModel);
  363. ui->mapList->setUpdatesEnabled(true);
  364. ui->mapList->expandToDepth(2);
  365. ui->mapList->repaint();
  366. }
  367. QStandardItem* MainWindow::createMapItem(QString mapName, int groupNum, int inGroupNum) {
  368. QStandardItem *map = new QStandardItem;
  369. map->setText(QString("[%1.%2] ").arg(groupNum).arg(inGroupNum, 2, 10, QLatin1Char('0')) + mapName);
  370. map->setIcon(*mapIcon);
  371. map->setEditable(false);
  372. map->setData(mapName, Qt::UserRole);
  373. map->setData("map_name", MapListUserRoles::TypeRole);
  374. return map;
  375. }
  376. void MainWindow::onOpenMapListContextMenu(const QPoint &point)
  377. {
  378. QModelIndex index = ui->mapList->indexAt(point);
  379. if (!index.isValid()) {
  380. return;
  381. }
  382. QStandardItem *selectedItem = mapListModel->itemFromIndex(index);
  383. QVariant itemType = selectedItem->data(MapListUserRoles::TypeRole);
  384. if (!itemType.isValid()) {
  385. return;
  386. }
  387. // Build custom context menu depending on which type of item was selected (map group, map name, etc.)
  388. if (itemType == "map_group") {
  389. QString groupName = selectedItem->data(Qt::UserRole).toString();
  390. int groupNum = selectedItem->data(MapListUserRoles::GroupRole).toInt();
  391. QMenu* menu = new QMenu();
  392. QActionGroup* actions = new QActionGroup(menu);
  393. actions->addAction(menu->addAction("Add New Map to Group"))->setData(groupNum);
  394. connect(actions, SIGNAL(triggered(QAction*)), this, SLOT(onAddNewMapToGroupClick(QAction*)));
  395. menu->exec(QCursor::pos());
  396. }
  397. }
  398. void MainWindow::onAddNewMapToGroupClick(QAction* triggeredAction)
  399. {
  400. int groupNum = triggeredAction->data().toInt();
  401. QStandardItem* groupItem = mapGroupsModel->at(groupNum);
  402. QString newMapName = editor->project->getNewMapName();
  403. Map* newMap = editor->project->addNewMapToGroup(newMapName, groupNum);
  404. editor->project->saveMap(newMap);
  405. editor->project->saveAllDataStructures();
  406. int numMapsInGroup = groupItem->rowCount();
  407. QStandardItem *newMapItem = createMapItem(newMapName, groupNum, numMapsInGroup);
  408. groupItem->appendRow(newMapItem);
  409. setMap(newMapName);
  410. }
  411. void MainWindow::onTilesetChanged(QString mapName)
  412. {
  413. setMap(mapName);
  414. }
  415. void MainWindow::on_mapList_activated(const QModelIndex &index)
  416. {
  417. QVariant data = index.data(Qt::UserRole);
  418. if (!data.isNull()) {
  419. setMap(data.toString());
  420. }
  421. //updateMapList();
  422. }
  423. void MainWindow::markAllEdited(QAbstractItemModel *model) {
  424. QList<QModelIndex> list;
  425. list.append(QModelIndex());
  426. while (list.length()) {
  427. QModelIndex parent = list.takeFirst();
  428. for (int i = 0; i < model->rowCount(parent); i++) {
  429. QModelIndex index = model->index(i, 0, parent);
  430. if (model->hasChildren(index)) {
  431. list.append(index);
  432. }
  433. markEdited(index);
  434. }
  435. }
  436. }
  437. void MainWindow::markEdited(QModelIndex index) {
  438. QVariant data = index.data(Qt::UserRole);
  439. if (!data.isNull()) {
  440. QString map_name = data.toString();
  441. if (editor->project) {
  442. if (editor->project->map_cache->contains(map_name)) {
  443. // Just mark anything that's been opened for now.
  444. // TODO if (project->getMap()->saved)
  445. //ui->mapList->setExpanded(index, true);
  446. ui->mapList->setExpanded(index, editor->project->map_cache->value(map_name)->hasUnsavedChanges());
  447. }
  448. }
  449. }
  450. }
  451. void MainWindow::updateMapList() {
  452. QAbstractItemModel *model = ui->mapList->model();
  453. markAllEdited(model);
  454. }
  455. void MainWindow::on_action_Save_Project_triggered()
  456. {
  457. editor->saveProject();
  458. updateMapList();
  459. }
  460. void MainWindow::undo() {
  461. editor->undo();
  462. }
  463. void MainWindow::redo() {
  464. editor->redo();
  465. }
  466. void MainWindow::on_action_Save_triggered() {
  467. editor->save();
  468. updateMapList();
  469. }
  470. void MainWindow::on_tabWidget_2_currentChanged(int index)
  471. {
  472. if (index == 0) {
  473. editor->setEditingMap();
  474. } else if (index == 1) {
  475. editor->setEditingCollision();
  476. }
  477. }
  478. void MainWindow::on_action_Exit_triggered()
  479. {
  480. QApplication::quit();
  481. }
  482. void MainWindow::on_tabWidget_currentChanged(int index)
  483. {
  484. if (index == 0) {
  485. on_tabWidget_2_currentChanged(ui->tabWidget_2->currentIndex());
  486. } else if (index == 1) {
  487. editor->setEditingObjects();
  488. } else if (index == 3) {
  489. editor->setEditingConnections();
  490. }
  491. }
  492. void MainWindow::on_actionUndo_triggered()
  493. {
  494. undo();
  495. }
  496. void MainWindow::on_actionRedo_triggered()
  497. {
  498. redo();
  499. }
  500. void MainWindow::addNewEvent(QString event_type)
  501. {
  502. if (editor) {
  503. DraggablePixmapItem *object = editor->addNewEvent(event_type);
  504. if (object) {
  505. editor->selectMapEvent(object, false);
  506. }
  507. updateSelectedObjects();
  508. }
  509. }
  510. // Should probably just pass layout and let the editor work it out
  511. void MainWindow::updateSelectedObjects() {
  512. QList<DraggablePixmapItem *> *all_events = editor->getObjects();
  513. QList<DraggablePixmapItem *> *events = NULL;
  514. if (editor->selected_events && editor->selected_events->length()) {
  515. events = editor->selected_events;
  516. } else {
  517. events = new QList<DraggablePixmapItem*>;
  518. if (all_events && all_events->length()) {
  519. DraggablePixmapItem *selectedEvent = all_events->first();
  520. editor->selected_events->append(selectedEvent);
  521. editor->redrawObject(selectedEvent);
  522. events->append(selectedEvent);
  523. }
  524. }
  525. QMap<QString, int> event_obj_gfx_constants = editor->project->getEventObjGfxConstants();
  526. QList<ObjectPropertiesFrame *> frames;
  527. for (DraggablePixmapItem *item : *events) {
  528. ObjectPropertiesFrame *frame = new ObjectPropertiesFrame;
  529. // frame->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
  530. QSpinBox *x = frame->ui->spinBox_x;
  531. QSpinBox *y = frame->ui->spinBox_y;
  532. QSpinBox *z = frame->ui->spinBox_z;
  533. x->setValue(item->event->x());
  534. connect(x, SIGNAL(valueChanged(QString)), item, SLOT(set_x(QString)));
  535. connect(item, SIGNAL(xChanged(int)), x, SLOT(setValue(int)));
  536. y->setValue(item->event->y());
  537. connect(y, SIGNAL(valueChanged(QString)), item, SLOT(set_y(QString)));
  538. connect(item, SIGNAL(yChanged(int)), y, SLOT(setValue(int)));
  539. z->setValue(item->event->elevation());
  540. connect(z, SIGNAL(valueChanged(QString)), item, SLOT(set_elevation(QString)));
  541. connect(item, SIGNAL(elevationChanged(int)), z, SLOT(setValue(int)));
  542. QFont font;
  543. font.setCapitalization(QFont::Capitalize);
  544. frame->ui->label_name->setFont(font);
  545. QString event_type = item->event->get("event_type");
  546. QString event_group_type = item->event->get("event_group_type");
  547. QString map_name = item->event->get("map_name");
  548. frame->ui->label_name->setText(
  549. QString("%1: %2 %3")
  550. .arg(editor->project->getMap(map_name)->events.value(event_group_type).indexOf(item->event) + 1)
  551. .arg(map_name)
  552. .arg(event_type)
  553. );
  554. frame->ui->label_spritePixmap->setPixmap(item->event->pixmap);
  555. connect(item, SIGNAL(spriteChanged(QPixmap)), frame->ui->label_spritePixmap, SLOT(setPixmap(QPixmap)));
  556. frame->ui->sprite->setVisible(false);
  557. QMap<QString, QString> field_labels;
  558. field_labels["script_label"] = "Script";
  559. field_labels["event_flag"] = "Event Flag";
  560. field_labels["movement_type"] = "Movement";
  561. field_labels["radius_x"] = "Movement Radius X";
  562. field_labels["radius_y"] = "Movement Radius Y";
  563. field_labels["is_trainer"] = "Trainer";
  564. field_labels["sight_radius_tree_id"] = "Sight Radius / Berry Tree ID";
  565. field_labels["destination_warp"] = "Destination Warp";
  566. field_labels["destination_map_name"] = "Destination Map";
  567. field_labels["script_var"] = "Var";
  568. field_labels["script_var_value"] = "Var Value";
  569. field_labels["player_facing_direction"] = "Player Facing Direction";
  570. field_labels["item"] = "Item";
  571. field_labels["item_unknown5"] = "Unknown 5";
  572. field_labels["item_unknown6"] = "Unknown 6";
  573. field_labels["weather"] = "Weather";
  574. field_labels["flag"] = "Flag";
  575. field_labels["secret_base_id"] = "Secret Base Id";
  576. QStringList fields;
  577. if (event_type == EventType::Object) {
  578. frame->ui->sprite->setVisible(true);
  579. frame->ui->comboBox_sprite->addItems(event_obj_gfx_constants.keys());
  580. frame->ui->comboBox_sprite->setCurrentText(item->event->get("sprite"));
  581. connect(frame->ui->comboBox_sprite, SIGNAL(activated(QString)), item, SLOT(set_sprite(QString)));
  582. /*
  583. frame->ui->script->setVisible(true);
  584. frame->ui->comboBox_script->addItem(item->event->get("script_label"));
  585. frame->ui->comboBox_script->setCurrentText(item->event->get("script_label"));
  586. //item->bind(frame->ui->comboBox_script, "script_label");
  587. connect(frame->ui->comboBox_script, SIGNAL(activated(QString)), item, SLOT(set_script(QString)));
  588. //connect(frame->ui->comboBox_script, static_cast<void (QComboBox::*)(const QString&)>(&QComboBox::activated), item, [item](QString script_label){ item->event->put("script_label", script_label); });
  589. //connect(item, SIGNAL(scriptChanged(QString)), frame->ui->comboBox_script, SLOT(setValue(QString)));
  590. */
  591. fields << "movement_type";
  592. fields << "radius_x";
  593. fields << "radius_y";
  594. fields << "script_label";
  595. fields << "event_flag";
  596. fields << "is_trainer";
  597. fields << "sight_radius_tree_id";
  598. }
  599. else if (event_type == EventType::Warp) {
  600. fields << "destination_warp";
  601. fields << "destination_map_name";
  602. }
  603. else if (event_type == EventType::CoordScript) {
  604. fields << "script_label";
  605. fields << "script_var";
  606. fields << "script_var_value";
  607. }
  608. else if (event_type == EventType::CoordWeather) {
  609. fields << "weather";
  610. }
  611. else if (event_type == EventType::Sign) {
  612. fields << "player_facing_direction";
  613. fields << "script_label";
  614. }
  615. else if (event_type == EventType::HiddenItem) {
  616. fields << "item";
  617. fields << "flag";
  618. }
  619. else if (event_type == EventType::SecretBase) {
  620. fields << "secret_base_id";
  621. }
  622. for (QString key : fields) {
  623. QString value = item->event->get(key);
  624. QWidget *widget = new QWidget(frame);
  625. QFormLayout *fl = new QFormLayout(widget);
  626. fl->setContentsMargins(9, 0, 9, 0);
  627. // is_trainer is the only non-combobox item.
  628. if (key == "is_trainer") {
  629. QCheckBox *checkbox = new QCheckBox(widget);
  630. checkbox->setEnabled(true);
  631. checkbox->setChecked(value.toInt() != 0 && value != "FALSE");
  632. checkbox->setToolTip("Whether or not this object is trainer.");
  633. fl->addRow(new QLabel(field_labels[key], widget), checkbox);
  634. widget->setLayout(fl);
  635. frame->layout()->addWidget(widget);
  636. connect(checkbox, &QCheckBox::stateChanged, [=](int state) {
  637. QString isTrainer = state == Qt::Checked ? "TRUE" : "FALSE";
  638. item->event->put("is_trainer", isTrainer);
  639. });
  640. continue;
  641. }
  642. NoScrollComboBox *combo = new NoScrollComboBox(widget);
  643. combo->setEditable(true);
  644. if (key == "destination_map_name") {
  645. if (!editor->project->mapNames->contains(value)) {
  646. combo->addItem(value);
  647. }
  648. combo->addItems(*editor->project->mapNames);
  649. combo->setToolTip("The destination map name of the warp.");
  650. } else if (key == "destination_warp") {
  651. combo->setToolTip("The warp id on the destination map.");
  652. } else if (key == "item") {
  653. if (!editor->project->itemNames->contains(value)) {
  654. combo->addItem(value);
  655. }
  656. combo->addItems(*editor->project->itemNames);
  657. } else if (key == "flag" || key == "event_flag") {
  658. if (!editor->project->flagNames->contains(value)) {
  659. combo->addItem(value);
  660. }
  661. combo->addItems(*editor->project->flagNames);
  662. if (key == "flag")
  663. combo->setToolTip("The flag which is set when the hidden item is picked up.");
  664. else if (key == "event_flag")
  665. combo->setToolTip("The flag which hides the object when set.");
  666. } else if (key == "script_var") {
  667. if (!editor->project->varNames->contains(value)) {
  668. combo->addItem(value);
  669. }
  670. combo->addItems(*editor->project->varNames);
  671. combo->setToolTip("The variable by which the script is triggered. The script is triggered when this variable's value matches 'Var Value'.");
  672. } else if (key == "script_var_value") {
  673. combo->setToolTip("The variable's value which triggers the script.");
  674. } else if (key == "movement_type") {
  675. if (!editor->project->movementTypes->contains(value)) {
  676. combo->addItem(value);
  677. }
  678. combo->addItems(*editor->project->movementTypes);
  679. combo->setToolTip("The object's natural movement behavior when the player is not interacting with it.");
  680. } else if (key == "weather") {
  681. if (!editor->project->coordEventWeatherNames->contains(value)) {
  682. combo->addItem(value);
  683. }
  684. combo->addItems(*editor->project->coordEventWeatherNames);
  685. combo->setToolTip("The weather that starts when the player steps on this spot.");
  686. } else if (key == "secret_base_id") {
  687. if (!editor->project->secretBaseIds->contains(value)) {
  688. combo->addItem(value);
  689. }
  690. combo->addItems(*editor->project->secretBaseIds);
  691. combo->setToolTip("The secret base id which is inside this secret base entrance. Secret base ids are meant to be unique to each and every secret base entrance.");
  692. } else if (key == "player_facing_direction") {
  693. if (!editor->project->bgEventFacingDirections->contains(value)) {
  694. combo->addItem(value);
  695. }
  696. combo->addItems(*editor->project->bgEventFacingDirections);
  697. combo->setToolTip("The direction which the player must be facing to be able to interact with this event.");
  698. } else if (key == "radius_x") {
  699. combo->setToolTip("The maximum number of metatiles this object is allowed to move left or right during its normal movement behavior actions.");
  700. } else if (key == "radius_y") {
  701. combo->setToolTip("The maximum number of metatiles this object is allowed to move up or down during its normal movement behavior actions.");
  702. } else if (key == "script_label") {
  703. combo->setToolTip("The script which is executed with this event.");
  704. } else if (key == "sight_radius_tree_id") {
  705. combo->setToolTip("The maximum sight range of a trainer, OR the unique id of the berry tree.");
  706. } else {
  707. combo->addItem(value);
  708. }
  709. combo->setCurrentText(value);
  710. fl->addRow(new QLabel(field_labels[key], widget), combo);
  711. widget->setLayout(fl);
  712. frame->layout()->addWidget(widget);
  713. item->bind(combo, key);
  714. }
  715. frames.append(frame);
  716. }
  717. //int scroll = ui->scrollArea_4->verticalScrollBar()->value();
  718. QWidget *target = ui->scrollAreaWidgetContents;
  719. if (target->children().length()) {
  720. qDeleteAll(target->children());
  721. }
  722. QVBoxLayout *layout = new QVBoxLayout(target);
  723. target->setLayout(layout);
  724. ui->scrollArea_4->setWidgetResizable(true);
  725. ui->scrollArea_4->setWidget(target);
  726. for (ObjectPropertiesFrame *frame : frames) {
  727. layout->addWidget(frame);
  728. }
  729. layout->addStretch(1);
  730. // doesn't work
  731. //QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  732. //ui->scrollArea_4->ensureVisible(0, scroll);
  733. }
  734. void MainWindow::on_toolButton_deleteObject_clicked()
  735. {
  736. if (editor && editor->selected_events) {
  737. if (editor->selected_events->length()) {
  738. for (DraggablePixmapItem *item : *editor->selected_events) {
  739. editor->deleteEvent(item->event);
  740. if (editor->scene->items().contains(item)) {
  741. editor->scene->removeItem(item);
  742. }
  743. editor->selected_events->removeOne(item);
  744. }
  745. updateSelectedObjects();
  746. }
  747. }
  748. }
  749. void MainWindow::on_toolButton_Paint_clicked()
  750. {
  751. editor->map_edit_mode = "paint";
  752. checkToolButtons();
  753. }
  754. void MainWindow::on_toolButton_Select_clicked()
  755. {
  756. editor->map_edit_mode = "select";
  757. checkToolButtons();
  758. }
  759. void MainWindow::on_toolButton_Fill_clicked()
  760. {
  761. editor->map_edit_mode = "fill";
  762. checkToolButtons();
  763. }
  764. void MainWindow::on_toolButton_Dropper_clicked()
  765. {
  766. editor->map_edit_mode = "pick";
  767. checkToolButtons();
  768. }
  769. void MainWindow::checkToolButtons() {
  770. ui->toolButton_Paint->setChecked(editor->map_edit_mode == "paint");
  771. ui->toolButton_Select->setChecked(editor->map_edit_mode == "select");
  772. ui->toolButton_Fill->setChecked(editor->map_edit_mode == "fill");
  773. ui->toolButton_Dropper->setChecked(editor->map_edit_mode == "pick");
  774. }
  775. void MainWindow::onLoadMapRequested(QString mapName, QString fromMapName) {
  776. setMap(mapName);
  777. editor->setSelectedConnectionFromMap(fromMapName);
  778. }
  779. void MainWindow::onMapChanged(Map *map) {
  780. map->layout->has_unsaved_changes = true;
  781. updateMapList();
  782. }
  783. void MainWindow::onMapNeedsRedrawing(Map *map) {
  784. redrawMapScene();
  785. }
  786. void MainWindow::on_action_Export_Map_Image_triggered()
  787. {
  788. QString defaultFilepath = QString("%1/%2.png").arg(editor->project->root).arg(editor->map->name);
  789. QString filepath = QFileDialog::getSaveFileName(this, "Export Map Image", defaultFilepath, "Image Files (*.png *.jpg *.bmp)");
  790. if (!filepath.isEmpty()) {
  791. editor->map_item->pixmap().save(filepath);
  792. }
  793. }
  794. void MainWindow::on_comboBox_ConnectionDirection_currentIndexChanged(const QString &direction)
  795. {
  796. editor->updateCurrentConnectionDirection(direction);
  797. }
  798. void MainWindow::on_spinBox_ConnectionOffset_valueChanged(int offset)
  799. {
  800. editor->updateConnectionOffset(offset);
  801. }
  802. void MainWindow::on_comboBox_ConnectedMap_currentTextChanged(const QString &mapName)
  803. {
  804. editor->setConnectionMap(mapName);
  805. }
  806. void MainWindow::on_pushButton_AddConnection_clicked()
  807. {
  808. editor->addNewConnection();
  809. }
  810. void MainWindow::on_pushButton_RemoveConnection_clicked()
  811. {
  812. editor->removeCurrentConnection();
  813. }
  814. void MainWindow::on_comboBox_DiveMap_currentTextChanged(const QString &mapName)
  815. {
  816. editor->updateDiveMap(mapName);
  817. }
  818. void MainWindow::on_comboBox_EmergeMap_currentTextChanged(const QString &mapName)
  819. {
  820. editor->updateEmergeMap(mapName);
  821. }
  822. void MainWindow::on_comboBox_PrimaryTileset_activated(const QString &tilesetLabel)
  823. {
  824. editor->updatePrimaryTileset(tilesetLabel);
  825. }
  826. void MainWindow::on_comboBox_SecondaryTileset_activated(const QString &tilesetLabel)
  827. {
  828. editor->updateSecondaryTileset(tilesetLabel);
  829. }
  830. void MainWindow::on_pushButton_clicked()
  831. {
  832. QDialog dialog(this, Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
  833. dialog.setWindowTitle("Change Map Dimensions");
  834. dialog.setWindowModality(Qt::NonModal);
  835. QFormLayout form(&dialog);
  836. QSpinBox *widthSpinBox = new QSpinBox();
  837. QSpinBox *heightSpinBox = new QSpinBox();
  838. widthSpinBox->setMinimum(1);
  839. heightSpinBox->setMinimum(1);
  840. // See below for explanation of maximum map dimensions
  841. widthSpinBox->setMaximum(0x1E7);
  842. heightSpinBox->setMaximum(0x1D1);
  843. widthSpinBox->setValue(editor->map->getWidth());
  844. heightSpinBox->setValue(editor->map->getHeight());
  845. form.addRow(new QLabel("Width"), widthSpinBox);
  846. form.addRow(new QLabel("Height"), heightSpinBox);
  847. QLabel *errorLabel = new QLabel();
  848. QPalette errorPalette;
  849. errorPalette.setColor(QPalette::WindowText, Qt::red);
  850. errorLabel->setPalette(errorPalette);
  851. errorLabel->setVisible(false);
  852. QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &dialog);
  853. form.addRow(&buttonBox);
  854. connect(&buttonBox, &QDialogButtonBox::accepted, [&dialog, &widthSpinBox, &heightSpinBox, &errorLabel](){
  855. // Ensure width and height are an acceptable size.
  856. // The maximum number of metatiles in a map is the following:
  857. // max = (width + 15) * (height + 14)
  858. // This limit can be found in fieldmap.c in pokeruby/pokeemerald.
  859. int realWidth = widthSpinBox->value() + 15;
  860. int realHeight = heightSpinBox->value() + 14;
  861. int numMetatiles = realWidth * realHeight;
  862. if (numMetatiles <= 0x2800) {
  863. dialog.accept();
  864. } else {
  865. QString errorText = QString("Error: The specified width and height are too large.\n"
  866. "The maximum width and height is the following: (width + 15) * (height + 14) <= 10240\n"
  867. "The specified width and height was: (%1 + 15) * (%2 + 14) = %3")
  868. .arg(widthSpinBox->value())
  869. .arg(heightSpinBox->value())
  870. .arg(numMetatiles);
  871. errorLabel->setText(errorText);
  872. errorLabel->setVisible(true);
  873. }
  874. });
  875. connect(&buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));
  876. form.addRow(errorLabel);
  877. if (dialog.exec() == QDialog::Accepted) {
  878. editor->map->setDimensions(widthSpinBox->value(), heightSpinBox->value());
  879. editor->map->commit();
  880. onMapNeedsRedrawing(editor->map);
  881. }
  882. }
  883. void MainWindow::on_checkBox_smartPaths_stateChanged(int selected)
  884. {
  885. editor->map->smart_paths_enabled = selected == Qt::Checked;
  886. }