No Description

mainwindow.cpp 27KB

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