Няма описание

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