Нет описания

mainwindow.cpp 32KB

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