No Description

mainwindow.cpp 33KB

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