Nav apraksta

mainwindow.cpp 25KB

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