Keine Beschreibung

mainwindow.cpp 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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->comboBox_Visibility->clear();
  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->getLocations());
  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->comboBox_Visibility->addItems(project->getVisibilities());
  236. ui->comboBox_Visibility->setCurrentText(map->visibility);
  237. ui->comboBox_Weather->addItems(project->getWeathers());
  238. ui->comboBox_Weather->setCurrentText(map->weather);
  239. ui->comboBox_Type->addItems(project->getMapTypes());
  240. ui->comboBox_Type->setCurrentText(map->type);
  241. ui->comboBox_BattleScene->addItems(project->getBattleScenes());
  242. ui->comboBox_BattleScene->setCurrentText(map->battle_scene);
  243. ui->checkBox_ShowLocation->setChecked(map->show_location.toInt() > 0 || map->show_location == "TRUE");
  244. }
  245. void MainWindow::on_comboBox_Song_activated(const QString &song)
  246. {
  247. if (editor && editor->map) {
  248. editor->map->song = song;
  249. }
  250. }
  251. void MainWindow::on_comboBox_Location_activated(const QString &location)
  252. {
  253. if (editor && editor->map) {
  254. editor->map->location = location;
  255. }
  256. }
  257. void MainWindow::on_comboBox_Visibility_activated(const QString &visibility)
  258. {
  259. if (editor && editor->map) {
  260. editor->map->visibility = visibility;
  261. }
  262. }
  263. void MainWindow::on_comboBox_Weather_activated(const QString &weather)
  264. {
  265. if (editor && editor->map) {
  266. editor->map->weather = weather;
  267. }
  268. }
  269. void MainWindow::on_comboBox_Type_activated(const QString &type)
  270. {
  271. if (editor && editor->map) {
  272. editor->map->type = type;
  273. }
  274. }
  275. void MainWindow::on_comboBox_BattleScene_activated(const QString &battle_scene)
  276. {
  277. if (editor && editor->map) {
  278. editor->map->battle_scene = battle_scene;
  279. }
  280. }
  281. void MainWindow::on_checkBox_ShowLocation_clicked(bool checked)
  282. {
  283. if (editor && editor->map) {
  284. if (checked) {
  285. editor->map->show_location = "TRUE";
  286. } else {
  287. editor->map->show_location = "FALSE";
  288. }
  289. }
  290. }
  291. void MainWindow::loadDataStructures() {
  292. Project *project = editor->project;
  293. project->readMapLayoutsTable();
  294. project->readAllMapLayouts();
  295. project->readItemNames();
  296. project->readFlagNames();
  297. project->readVarNames();
  298. project->readMovementTypes();
  299. project->readCoordEventWeatherNames();
  300. project->readSecretBaseIds();
  301. project->readBgEventFacingDirections();
  302. project->readMapsWithConnections();
  303. }
  304. void MainWindow::populateMapList() {
  305. Project *project = editor->project;
  306. QIcon mapFolderIcon;
  307. mapFolderIcon.addFile(QStringLiteral(":/icons/folder_closed_map.ico"), QSize(), QIcon::Normal, QIcon::Off);
  308. mapFolderIcon.addFile(QStringLiteral(":/icons/folder_map.ico"), QSize(), QIcon::Normal, QIcon::On);
  309. QIcon folderIcon;
  310. folderIcon.addFile(QStringLiteral(":/icons/folder_closed.ico"), QSize(), QIcon::Normal, QIcon::Off);
  311. mapIcon = new QIcon;
  312. mapIcon->addFile(QStringLiteral(":/icons/map.ico"), QSize(), QIcon::Normal, QIcon::Off);
  313. mapIcon->addFile(QStringLiteral(":/icons/image.ico"), QSize(), QIcon::Normal, QIcon::On);
  314. mapListModel = new QStandardItemModel;
  315. mapGroupsModel = new QList<QStandardItem*>;
  316. QStandardItem *entry = new QStandardItem;
  317. entry->setText(project->getProjectTitle());
  318. entry->setIcon(folderIcon);
  319. entry->setEditable(false);
  320. mapListModel->appendRow(entry);
  321. QStandardItem *maps = new QStandardItem;
  322. maps->setText("maps");
  323. maps->setIcon(folderIcon);
  324. maps->setEditable(false);
  325. entry->appendRow(maps);
  326. project->readMapGroups();
  327. for (int i = 0; i < project->groupNames->length(); i++) {
  328. QString group_name = project->groupNames->value(i);
  329. QStandardItem *group = new QStandardItem;
  330. group->setText(group_name);
  331. group->setIcon(mapFolderIcon);
  332. group->setEditable(false);
  333. group->setData(group_name, Qt::UserRole);
  334. group->setData("map_group", MapListUserRoles::TypeRole);
  335. group->setData(i, MapListUserRoles::GroupRole);
  336. maps->appendRow(group);
  337. mapGroupsModel->append(group);
  338. QStringList names = project->groupedMapNames.value(i);
  339. for (int j = 0; j < names.length(); j++) {
  340. QString map_name = names.value(j);
  341. QStandardItem *map = createMapItem(map_name, i, j);
  342. group->appendRow(map);
  343. }
  344. }
  345. // Right-clicking on items in the map list tree view brings up a context menu.
  346. ui->mapList->setContextMenuPolicy(Qt::CustomContextMenu);
  347. connect(ui->mapList, SIGNAL(customContextMenuRequested(const QPoint &)),
  348. this, SLOT(onOpenMapListContextMenu(const QPoint &)));
  349. ui->mapList->setModel(mapListModel);
  350. ui->mapList->setUpdatesEnabled(true);
  351. ui->mapList->expandToDepth(2);
  352. ui->mapList->repaint();
  353. }
  354. QStandardItem* MainWindow::createMapItem(QString mapName, int groupNum, int inGroupNum) {
  355. QStandardItem *map = new QStandardItem;
  356. map->setText(QString("[%1.%2] ").arg(groupNum).arg(inGroupNum, 2, 10, QLatin1Char('0')) + mapName);
  357. map->setIcon(*mapIcon);
  358. map->setEditable(false);
  359. map->setData(mapName, Qt::UserRole);
  360. map->setData("map_name", MapListUserRoles::TypeRole);
  361. return map;
  362. }
  363. void MainWindow::onOpenMapListContextMenu(const QPoint &point)
  364. {
  365. QModelIndex index = ui->mapList->indexAt(point);
  366. if (!index.isValid()) {
  367. return;
  368. }
  369. QStandardItem *selectedItem = mapListModel->itemFromIndex(index);
  370. QVariant itemType = selectedItem->data(MapListUserRoles::TypeRole);
  371. if (!itemType.isValid()) {
  372. return;
  373. }
  374. // Build custom context menu depending on which type of item was selected (map group, map name, etc.)
  375. if (itemType == "map_group") {
  376. QString groupName = selectedItem->data(Qt::UserRole).toString();
  377. int groupNum = selectedItem->data(MapListUserRoles::GroupRole).toInt();
  378. QMenu* menu = new QMenu();
  379. QActionGroup* actions = new QActionGroup(menu);
  380. actions->addAction(menu->addAction("Add New Map to Group"))->setData(groupNum);
  381. connect(actions, SIGNAL(triggered(QAction*)), this, SLOT(onAddNewMapToGroupClick(QAction*)));
  382. menu->exec(QCursor::pos());
  383. }
  384. }
  385. void MainWindow::onAddNewMapToGroupClick(QAction* triggeredAction)
  386. {
  387. int groupNum = triggeredAction->data().toInt();
  388. QStandardItem* groupItem = mapGroupsModel->at(groupNum);
  389. QString newMapName = editor->project->getNewMapName();
  390. Map* newMap = editor->project->addNewMapToGroup(newMapName, groupNum);
  391. editor->project->saveMap(newMap);
  392. editor->project->saveAllDataStructures();
  393. int numMapsInGroup = groupItem->rowCount();
  394. QStandardItem *newMapItem = createMapItem(newMapName, groupNum, numMapsInGroup);
  395. groupItem->appendRow(newMapItem);
  396. setMap(newMapName);
  397. }
  398. void MainWindow::onTilesetChanged(QString mapName)
  399. {
  400. setMap(mapName);
  401. }
  402. void MainWindow::on_mapList_activated(const QModelIndex &index)
  403. {
  404. QVariant data = index.data(Qt::UserRole);
  405. if (!data.isNull()) {
  406. setMap(data.toString());
  407. }
  408. //updateMapList();
  409. }
  410. void MainWindow::markAllEdited(QAbstractItemModel *model) {
  411. QList<QModelIndex> list;
  412. list.append(QModelIndex());
  413. while (list.length()) {
  414. QModelIndex parent = list.takeFirst();
  415. for (int i = 0; i < model->rowCount(parent); i++) {
  416. QModelIndex index = model->index(i, 0, parent);
  417. if (model->hasChildren(index)) {
  418. list.append(index);
  419. }
  420. markEdited(index);
  421. }
  422. }
  423. }
  424. void MainWindow::markEdited(QModelIndex index) {
  425. QVariant data = index.data(Qt::UserRole);
  426. if (!data.isNull()) {
  427. QString map_name = data.toString();
  428. if (editor->project) {
  429. if (editor->project->map_cache->contains(map_name)) {
  430. // Just mark anything that's been opened for now.
  431. // TODO if (project->getMap()->saved)
  432. //ui->mapList->setExpanded(index, true);
  433. ui->mapList->setExpanded(index, editor->project->map_cache->value(map_name)->hasUnsavedChanges());
  434. }
  435. }
  436. }
  437. }
  438. void MainWindow::updateMapList() {
  439. QAbstractItemModel *model = ui->mapList->model();
  440. markAllEdited(model);
  441. }
  442. void MainWindow::on_action_Save_Project_triggered()
  443. {
  444. editor->saveProject();
  445. updateMapList();
  446. }
  447. void MainWindow::undo() {
  448. editor->undo();
  449. }
  450. void MainWindow::redo() {
  451. editor->redo();
  452. }
  453. void MainWindow::on_action_Save_triggered() {
  454. editor->save();
  455. updateMapList();
  456. }
  457. void MainWindow::on_tabWidget_2_currentChanged(int index)
  458. {
  459. if (index == 0) {
  460. editor->setEditingMap();
  461. } else if (index == 1) {
  462. editor->setEditingCollision();
  463. }
  464. }
  465. void MainWindow::on_action_Exit_triggered()
  466. {
  467. QApplication::quit();
  468. }
  469. void MainWindow::on_tabWidget_currentChanged(int index)
  470. {
  471. if (index == 0) {
  472. on_tabWidget_2_currentChanged(ui->tabWidget_2->currentIndex());
  473. } else if (index == 1) {
  474. editor->setEditingObjects();
  475. } else if (index == 3) {
  476. editor->setEditingConnections();
  477. }
  478. }
  479. void MainWindow::on_actionUndo_triggered()
  480. {
  481. undo();
  482. }
  483. void MainWindow::on_actionRedo_triggered()
  484. {
  485. redo();
  486. }
  487. void MainWindow::addNewEvent(QString event_type)
  488. {
  489. if (editor) {
  490. DraggablePixmapItem *object = editor->addNewEvent(event_type);
  491. if (object) {
  492. editor->selectMapEvent(object, false);
  493. }
  494. updateSelectedObjects();
  495. }
  496. }
  497. // Should probably just pass layout and let the editor work it out
  498. void MainWindow::updateSelectedObjects() {
  499. QList<DraggablePixmapItem *> *all_events = editor->getObjects();
  500. QList<DraggablePixmapItem *> *events = NULL;
  501. if (editor->selected_events && editor->selected_events->length()) {
  502. events = editor->selected_events;
  503. } else {
  504. events = new QList<DraggablePixmapItem*>;
  505. if (all_events && all_events->length()) {
  506. DraggablePixmapItem *selectedEvent = all_events->first();
  507. editor->selected_events->append(selectedEvent);
  508. editor->redrawObject(selectedEvent);
  509. events->append(selectedEvent);
  510. }
  511. }
  512. QMap<QString, int> event_obj_gfx_constants = editor->project->getEventObjGfxConstants();
  513. QList<ObjectPropertiesFrame *> frames;
  514. for (DraggablePixmapItem *item : *events) {
  515. ObjectPropertiesFrame *frame = new ObjectPropertiesFrame;
  516. // frame->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
  517. QSpinBox *x = frame->ui->spinBox_x;
  518. QSpinBox *y = frame->ui->spinBox_y;
  519. QSpinBox *z = frame->ui->spinBox_z;
  520. x->setValue(item->event->x());
  521. connect(x, SIGNAL(valueChanged(QString)), item, SLOT(set_x(QString)));
  522. connect(item, SIGNAL(xChanged(int)), x, SLOT(setValue(int)));
  523. y->setValue(item->event->y());
  524. connect(y, SIGNAL(valueChanged(QString)), item, SLOT(set_y(QString)));
  525. connect(item, SIGNAL(yChanged(int)), y, SLOT(setValue(int)));
  526. z->setValue(item->event->elevation());
  527. connect(z, SIGNAL(valueChanged(QString)), item, SLOT(set_elevation(QString)));
  528. connect(item, SIGNAL(elevationChanged(int)), z, SLOT(setValue(int)));
  529. QFont font;
  530. font.setCapitalization(QFont::Capitalize);
  531. frame->ui->label_name->setFont(font);
  532. QString event_type = item->event->get("event_type");
  533. QString event_group_type = item->event->get("event_group_type");
  534. QString map_name = item->event->get("map_name");
  535. frame->ui->label_name->setText(
  536. QString("%1: %2 %3")
  537. .arg(editor->project->getMap(map_name)->events.value(event_group_type).indexOf(item->event) + 1)
  538. .arg(map_name)
  539. .arg(event_type)
  540. );
  541. frame->ui->label_spritePixmap->setPixmap(item->event->pixmap);
  542. connect(item, SIGNAL(spriteChanged(QPixmap)), frame->ui->label_spritePixmap, SLOT(setPixmap(QPixmap)));
  543. frame->ui->sprite->setVisible(false);
  544. QMap<QString, QString> field_labels;
  545. field_labels["script_label"] = "Script";
  546. field_labels["event_flag"] = "Event Flag";
  547. field_labels["replacement"] = "Replacement";
  548. field_labels["movement_type"] = "Movement";
  549. field_labels["radius_x"] = "Movement Radius X";
  550. field_labels["radius_y"] = "Movement Radius Y";
  551. field_labels["trainer_see_type"] = "Trainer See Type";
  552. field_labels["sight_radius_tree_id"] = "Sight Radius / Berry Tree ID";
  553. field_labels["destination_warp"] = "Destination Warp";
  554. field_labels["destination_map_name"] = "Destination Map";
  555. field_labels["script_var"] = "Var";
  556. field_labels["script_var_value"] = "Var Value";
  557. field_labels["player_facing_direction"] = "Player Facing Direction";
  558. field_labels["item"] = "Item";
  559. field_labels["item_unknown5"] = "Unknown 5";
  560. field_labels["item_unknown6"] = "Unknown 6";
  561. field_labels["weather"] = "Weather";
  562. field_labels["flag"] = "Flag";
  563. field_labels["secret_base_id"] = "Secret Base Id";
  564. QStringList fields;
  565. if (event_type == EventType::Object) {
  566. frame->ui->sprite->setVisible(true);
  567. frame->ui->comboBox_sprite->addItems(event_obj_gfx_constants.keys());
  568. frame->ui->comboBox_sprite->setCurrentText(item->event->get("sprite"));
  569. connect(frame->ui->comboBox_sprite, SIGNAL(activated(QString)), item, SLOT(set_sprite(QString)));
  570. /*
  571. frame->ui->script->setVisible(true);
  572. frame->ui->comboBox_script->addItem(item->event->get("script_label"));
  573. frame->ui->comboBox_script->setCurrentText(item->event->get("script_label"));
  574. //item->bind(frame->ui->comboBox_script, "script_label");
  575. connect(frame->ui->comboBox_script, SIGNAL(activated(QString)), item, SLOT(set_script(QString)));
  576. //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); });
  577. //connect(item, SIGNAL(scriptChanged(QString)), frame->ui->comboBox_script, SLOT(setValue(QString)));
  578. */
  579. fields << "movement_type";
  580. fields << "radius_x";
  581. fields << "radius_y";
  582. fields << "script_label";
  583. fields << "event_flag";
  584. fields << "replacement";
  585. fields << "trainer_see_type";
  586. fields << "sight_radius_tree_id";
  587. }
  588. else if (event_type == EventType::Warp) {
  589. fields << "destination_warp";
  590. fields << "destination_map_name";
  591. }
  592. else if (event_type == EventType::CoordScript) {
  593. fields << "script_label";
  594. fields << "script_var";
  595. fields << "script_var_value";
  596. }
  597. else if (event_type == EventType::CoordWeather) {
  598. fields << "weather";
  599. }
  600. else if (event_type == EventType::Sign) {
  601. fields << "player_facing_direction";
  602. fields << "script_label";
  603. }
  604. else if (event_type == EventType::HiddenItem) {
  605. fields << "item";
  606. fields << "flag";
  607. }
  608. else if (event_type == EventType::SecretBase) {
  609. fields << "secret_base_id";
  610. }
  611. for (QString key : fields) {
  612. QWidget *widget = new QWidget(frame);
  613. QFormLayout *fl = new QFormLayout(widget);
  614. fl->setContentsMargins(9, 0, 9, 0);
  615. NoScrollComboBox *combo = new NoScrollComboBox(widget);
  616. combo->setEditable(true);
  617. QString value = item->event->get(key);
  618. if (key == "destination_map_name") {
  619. if (!editor->project->mapNames->contains(value)) {
  620. combo->addItem(value);
  621. }
  622. combo->addItems(*editor->project->mapNames);
  623. } else if (key == "item") {
  624. if (!editor->project->itemNames->contains(value)) {
  625. combo->addItem(value);
  626. }
  627. combo->addItems(*editor->project->itemNames);
  628. } else if (key == "flag" || key == "event_flag") {
  629. if (!editor->project->flagNames->contains(value)) {
  630. combo->addItem(value);
  631. }
  632. combo->addItems(*editor->project->flagNames);
  633. } else if (key == "script_var") {
  634. if (!editor->project->varNames->contains(value)) {
  635. combo->addItem(value);
  636. }
  637. combo->addItems(*editor->project->varNames);
  638. } else if (key == "movement_type") {
  639. if (!editor->project->movementTypes->contains(value)) {
  640. combo->addItem(value);
  641. }
  642. combo->addItems(*editor->project->movementTypes);
  643. } else if (key == "weather") {
  644. if (!editor->project->coordEventWeatherNames->contains(value)) {
  645. combo->addItem(value);
  646. }
  647. combo->addItems(*editor->project->coordEventWeatherNames);
  648. } else if (key == "secret_base_id") {
  649. if (!editor->project->secretBaseIds->contains(value)) {
  650. combo->addItem(value);
  651. }
  652. combo->addItems(*editor->project->secretBaseIds);
  653. } else if (key == "player_facing_direction") {
  654. if (!editor->project->bgEventFacingDirections->contains(value)) {
  655. combo->addItem(value);
  656. }
  657. combo->addItems(*editor->project->bgEventFacingDirections);
  658. } else {
  659. combo->addItem(value);
  660. }
  661. combo->setCurrentText(value);
  662. fl->addRow(new QLabel(field_labels[key], widget), combo);
  663. widget->setLayout(fl);
  664. frame->layout()->addWidget(widget);
  665. item->bind(combo, key);
  666. }
  667. frames.append(frame);
  668. }
  669. //int scroll = ui->scrollArea_4->verticalScrollBar()->value();
  670. QWidget *target = ui->scrollAreaWidgetContents;
  671. if (target->children().length()) {
  672. qDeleteAll(target->children());
  673. }
  674. QVBoxLayout *layout = new QVBoxLayout(target);
  675. target->setLayout(layout);
  676. ui->scrollArea_4->setWidgetResizable(true);
  677. ui->scrollArea_4->setWidget(target);
  678. for (ObjectPropertiesFrame *frame : frames) {
  679. layout->addWidget(frame);
  680. }
  681. layout->addStretch(1);
  682. // doesn't work
  683. //QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
  684. //ui->scrollArea_4->ensureVisible(0, scroll);
  685. }
  686. void MainWindow::on_toolButton_deleteObject_clicked()
  687. {
  688. if (editor && editor->selected_events) {
  689. if (editor->selected_events->length()) {
  690. for (DraggablePixmapItem *item : *editor->selected_events) {
  691. editor->deleteEvent(item->event);
  692. if (editor->scene->items().contains(item)) {
  693. editor->scene->removeItem(item);
  694. }
  695. editor->selected_events->removeOne(item);
  696. }
  697. updateSelectedObjects();
  698. }
  699. }
  700. }
  701. void MainWindow::on_toolButton_Paint_clicked()
  702. {
  703. editor->map_edit_mode = "paint";
  704. checkToolButtons();
  705. }
  706. void MainWindow::on_toolButton_Select_clicked()
  707. {
  708. editor->map_edit_mode = "select";
  709. checkToolButtons();
  710. }
  711. void MainWindow::on_toolButton_Fill_clicked()
  712. {
  713. editor->map_edit_mode = "fill";
  714. checkToolButtons();
  715. }
  716. void MainWindow::on_toolButton_Dropper_clicked()
  717. {
  718. editor->map_edit_mode = "pick";
  719. checkToolButtons();
  720. }
  721. void MainWindow::checkToolButtons() {
  722. ui->toolButton_Paint->setChecked(editor->map_edit_mode == "paint");
  723. ui->toolButton_Select->setChecked(editor->map_edit_mode == "select");
  724. ui->toolButton_Fill->setChecked(editor->map_edit_mode == "fill");
  725. ui->toolButton_Dropper->setChecked(editor->map_edit_mode == "pick");
  726. }
  727. void MainWindow::onLoadMapRequested(QString mapName, QString fromMapName) {
  728. setMap(mapName);
  729. editor->setSelectedConnectionFromMap(fromMapName);
  730. }
  731. void MainWindow::onMapChanged(Map *map) {
  732. map->layout->has_unsaved_changes = true;
  733. updateMapList();
  734. }
  735. void MainWindow::onMapNeedsRedrawing(Map *map) {
  736. redrawMapScene();
  737. }
  738. void MainWindow::on_action_Export_Map_Image_triggered()
  739. {
  740. QString defaultFilepath = QString("%1/%2.png").arg(editor->project->root).arg(editor->map->name);
  741. QString filepath = QFileDialog::getSaveFileName(this, "Export Map Image", defaultFilepath, "Image Files (*.png *.jpg *.bmp)");
  742. if (!filepath.isEmpty()) {
  743. editor->map_item->pixmap().save(filepath);
  744. }
  745. }
  746. void MainWindow::on_comboBox_ConnectionDirection_currentIndexChanged(const QString &direction)
  747. {
  748. editor->updateCurrentConnectionDirection(direction);
  749. }
  750. void MainWindow::on_spinBox_ConnectionOffset_valueChanged(int offset)
  751. {
  752. editor->updateConnectionOffset(offset);
  753. }
  754. void MainWindow::on_comboBox_ConnectedMap_currentTextChanged(const QString &mapName)
  755. {
  756. editor->setConnectionMap(mapName);
  757. }
  758. void MainWindow::on_pushButton_AddConnection_clicked()
  759. {
  760. editor->addNewConnection();
  761. }
  762. void MainWindow::on_pushButton_RemoveConnection_clicked()
  763. {
  764. editor->removeCurrentConnection();
  765. }
  766. void MainWindow::on_comboBox_DiveMap_currentTextChanged(const QString &mapName)
  767. {
  768. editor->updateDiveMap(mapName);
  769. }
  770. void MainWindow::on_comboBox_EmergeMap_currentTextChanged(const QString &mapName)
  771. {
  772. editor->updateEmergeMap(mapName);
  773. }
  774. void MainWindow::on_comboBox_PrimaryTileset_activated(const QString &tilesetLabel)
  775. {
  776. editor->updatePrimaryTileset(tilesetLabel);
  777. }
  778. void MainWindow::on_comboBox_SecondaryTileset_activated(const QString &tilesetLabel)
  779. {
  780. editor->updateSecondaryTileset(tilesetLabel);
  781. }
  782. void MainWindow::on_pushButton_clicked()
  783. {
  784. QDialog dialog(this, Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
  785. dialog.setWindowTitle("Change Map Dimensions");
  786. dialog.setWindowModality(Qt::NonModal);
  787. QFormLayout form(&dialog);
  788. QSpinBox *widthSpinBox = new QSpinBox();
  789. QSpinBox *heightSpinBox = new QSpinBox();
  790. widthSpinBox->setMinimum(1);
  791. heightSpinBox->setMinimum(1);
  792. // See below for explanation of maximum map dimensions
  793. widthSpinBox->setMaximum(0x1E7);
  794. heightSpinBox->setMaximum(0x1D1);
  795. widthSpinBox->setValue(editor->map->getWidth());
  796. heightSpinBox->setValue(editor->map->getHeight());
  797. form.addRow(new QLabel("Width"), widthSpinBox);
  798. form.addRow(new QLabel("Height"), heightSpinBox);
  799. QLabel *errorLabel = new QLabel();
  800. QPalette errorPalette;
  801. errorPalette.setColor(QPalette::WindowText, Qt::red);
  802. errorLabel->setPalette(errorPalette);
  803. errorLabel->setVisible(false);
  804. QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &dialog);
  805. form.addRow(&buttonBox);
  806. connect(&buttonBox, &QDialogButtonBox::accepted, [&dialog, &widthSpinBox, &heightSpinBox, &errorLabel](){
  807. // Ensure width and height are an acceptable size.
  808. // The maximum number of metatiles in a map is the following:
  809. // max = (width + 15) * (height + 14)
  810. // This limit can be found in fieldmap.c in pokeruby/pokeemerald.
  811. int realWidth = widthSpinBox->value() + 15;
  812. int realHeight = heightSpinBox->value() + 14;
  813. int numMetatiles = realWidth * realHeight;
  814. if (numMetatiles <= 0x2800) {
  815. dialog.accept();
  816. } else {
  817. QString errorText = QString("Error: The specified width and height are too large.\n"
  818. "The maximum width and height is the following: (width + 15) * (height + 14) <= 10240\n"
  819. "The specified width and height was: (%1 + 15) * (%2 + 14) = %3")
  820. .arg(widthSpinBox->value())
  821. .arg(heightSpinBox->value())
  822. .arg(numMetatiles);
  823. errorLabel->setText(errorText);
  824. errorLabel->setVisible(true);
  825. }
  826. });
  827. connect(&buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));
  828. form.addRow(errorLabel);
  829. if (dialog.exec() == QDialog::Accepted) {
  830. editor->map->setDimensions(widthSpinBox->value(), heightSpinBox->value());
  831. editor->map->commit();
  832. onMapNeedsRedrawing(editor->map);
  833. }
  834. }
  835. void MainWindow::on_checkBox_smartPaths_stateChanged(int selected)
  836. {
  837. editor->map->smart_paths_enabled = selected == Qt::Checked;
  838. }