No Description

mainwindow.cpp 28KB

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