Geen omschrijving

mainwindow.cpp 26KB

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