Sin descripción

mainwindow.cpp 29KB

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