Sin descripción

editor.cpp 38KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. #include "editor.h"
  2. #include <QCheckBox>
  3. #include <QPainter>
  4. #include <QMouseEvent>
  5. Editor::Editor(Ui::MainWindow* ui)
  6. {
  7. this->ui = ui;
  8. selected_events = new QList<DraggablePixmapItem*>;
  9. }
  10. void Editor::saveProject() {
  11. if (project) {
  12. project->saveAllMaps();
  13. project->saveAllDataStructures();
  14. }
  15. }
  16. void Editor::save() {
  17. if (project && map) {
  18. project->saveMap(map);
  19. project->saveAllDataStructures();
  20. }
  21. }
  22. void Editor::undo() {
  23. if (current_view) {
  24. ((MapPixmapItem*)current_view)->undo();
  25. }
  26. }
  27. void Editor::redo() {
  28. if (current_view) {
  29. ((MapPixmapItem*)current_view)->redo();
  30. }
  31. }
  32. void Editor::setEditingMap() {
  33. current_view = map_item;
  34. if (map_item) {
  35. displayMapConnections();
  36. map_item->draw();
  37. map_item->setVisible(true);
  38. map_item->setEnabled(true);
  39. setConnectionsVisibility(true);
  40. }
  41. if (collision_item) {
  42. collision_item->setVisible(false);
  43. }
  44. if (objects_group) {
  45. objects_group->setVisible(false);
  46. }
  47. setBorderItemsVisible(true);
  48. setConnectionItemsVisible(false);
  49. }
  50. void Editor::setEditingCollision() {
  51. current_view = collision_item;
  52. if (collision_item) {
  53. collision_item->setVisible(true);
  54. setConnectionsVisibility(true);
  55. }
  56. if (map_item) {
  57. map_item->setVisible(false);
  58. }
  59. if (objects_group) {
  60. objects_group->setVisible(false);
  61. }
  62. setBorderItemsVisible(true);
  63. setConnectionItemsVisible(false);
  64. }
  65. void Editor::setEditingObjects() {
  66. current_view = map_item;
  67. if (objects_group) {
  68. objects_group->setVisible(true);
  69. }
  70. if (map_item) {
  71. map_item->setVisible(true);
  72. map_item->setEnabled(false);
  73. setConnectionsVisibility(true);
  74. }
  75. if (collision_item) {
  76. collision_item->setVisible(false);
  77. }
  78. setBorderItemsVisible(true);
  79. setConnectionItemsVisible(false);
  80. }
  81. void Editor::setEditingConnections() {
  82. current_view = map_item;
  83. if (map_item) {
  84. map_item->draw();
  85. map_item->setVisible(true);
  86. map_item->setEnabled(false);
  87. ui->comboBox_ConnectedMap->blockSignals(true);
  88. ui->comboBox_ConnectedMap->clear();
  89. ui->comboBox_ConnectedMap->addItems(*project->mapNames);
  90. ui->comboBox_ConnectedMap->blockSignals(false);
  91. ui->label_NumConnections->setText(QString::number(map->connections.length()));
  92. setConnectionsVisibility(false);
  93. if (current_connection_edit_item) {
  94. onConnectionOffsetChanged(current_connection_edit_item->connection->offset.toInt());
  95. updateConnectionMap(current_connection_edit_item->connection->map_name, current_connection_edit_item->connection->direction);
  96. }
  97. }
  98. if (collision_item) {
  99. collision_item->setVisible(false);
  100. }
  101. if (objects_group) {
  102. objects_group->setVisible(false);
  103. }
  104. setBorderItemsVisible(true, 0.4);
  105. setConnectionItemsVisible(true);
  106. }
  107. void Editor::setConnectionItemsVisible(bool visible) {
  108. for (ConnectionPixmapItem* item : connection_edit_items) {
  109. item->setVisible(visible);
  110. item->setEnabled(visible);
  111. }
  112. }
  113. void Editor::setBorderItemsVisible(bool visible, qreal opacity) {
  114. for (QGraphicsPixmapItem* item : borderItems) {
  115. item->setVisible(visible);
  116. item->setOpacity(opacity);
  117. }
  118. }
  119. void Editor::setCurrentConnectionDirection(QString curDirection) {
  120. if (!current_connection_edit_item)
  121. return;
  122. current_connection_edit_item->connection->direction = curDirection;
  123. Map *connected_map = project->getMap(current_connection_edit_item->connection->map_name);
  124. QPixmap pixmap = connected_map->renderConnection(*current_connection_edit_item->connection);
  125. int offset = current_connection_edit_item->connection->offset.toInt(nullptr, 0);
  126. current_connection_edit_item->initialOffset = offset;
  127. int x = 0, y = 0;
  128. if (current_connection_edit_item->connection->direction == "up") {
  129. x = offset * 16;
  130. y = -pixmap.height();
  131. } else if (current_connection_edit_item->connection->direction == "down") {
  132. x = offset * 16;
  133. y = map->getHeight() * 16;
  134. } else if (current_connection_edit_item->connection->direction == "left") {
  135. x = -pixmap.width();
  136. y = offset * 16;
  137. } else if (current_connection_edit_item->connection->direction == "right") {
  138. x = map->getWidth() * 16;
  139. y = offset * 16;
  140. }
  141. current_connection_edit_item->basePixmap = pixmap;
  142. QPainter painter(&pixmap);
  143. painter.setPen(QColor(255, 0, 255));
  144. painter.drawRect(0, 0, pixmap.width() - 1, pixmap.height() - 1);
  145. painter.end();
  146. current_connection_edit_item->setPixmap(pixmap);
  147. current_connection_edit_item->initialX = x;
  148. current_connection_edit_item->initialY = y;
  149. current_connection_edit_item->blockSignals(true);
  150. current_connection_edit_item->setX(x);
  151. current_connection_edit_item->setY(y);
  152. current_connection_edit_item->setZValue(-1);
  153. current_connection_edit_item->blockSignals(false);
  154. setConnectionEditControlValues(current_connection_edit_item->connection);
  155. }
  156. void Editor::onConnectionOffsetChanged(int newOffset) {
  157. ui->spinBox_ConnectionOffset->blockSignals(true);
  158. ui->spinBox_ConnectionOffset->setValue(newOffset);
  159. ui->spinBox_ConnectionOffset->blockSignals(false);
  160. }
  161. void Editor::setConnectionEditControlValues(Connection* connection) {
  162. ui->comboBox_ConnectedMap->blockSignals(true);
  163. ui->comboBox_ConnectionDirection->blockSignals(true);
  164. ui->spinBox_ConnectionOffset->blockSignals(true);
  165. ui->comboBox_ConnectedMap->setCurrentText(connection->map_name);
  166. ui->comboBox_ConnectionDirection->setCurrentText(connection->direction);
  167. ui->spinBox_ConnectionOffset->setValue(connection->offset.toInt());
  168. ui->comboBox_ConnectedMap->blockSignals(false);
  169. ui->comboBox_ConnectionDirection->blockSignals(false);
  170. ui->spinBox_ConnectionOffset->blockSignals(false);
  171. }
  172. void Editor::setConnectionEditControlsEnabled(bool enabled) {
  173. ui->comboBox_ConnectionDirection->setEnabled(enabled);
  174. ui->comboBox_ConnectedMap->setEnabled(enabled);
  175. ui->spinBox_ConnectionOffset->setEnabled(enabled);
  176. }
  177. void Editor::onConnectionItemSelected(ConnectionPixmapItem* connectionItem) {
  178. if (!connectionItem)
  179. return;
  180. for (ConnectionPixmapItem* item : connection_edit_items) {
  181. bool isSelectedItem = item == connectionItem;
  182. int zValue = isSelectedItem ? 0 : -1;
  183. qreal opacity = isSelectedItem ? 1 : 0.75;
  184. item->setZValue(zValue);
  185. item->render(opacity);
  186. if (isSelectedItem) {
  187. QPixmap pixmap = item->pixmap();
  188. QPainter painter(&pixmap);
  189. painter.setPen(QColor(255, 0, 255));
  190. painter.drawRect(0, 0, pixmap.width() - 1, pixmap.height() - 1);
  191. painter.end();
  192. item->setPixmap(pixmap);
  193. }
  194. }
  195. current_connection_edit_item = connectionItem;
  196. current_connection_edit_item->setZValue(0);
  197. setConnectionEditControlsEnabled(true);
  198. setConnectionEditControlValues(current_connection_edit_item->connection);
  199. }
  200. void Editor::onConnectionDirectionChanged(QString newDirection) {
  201. ui->comboBox_ConnectionDirection->blockSignals(true);
  202. ui->comboBox_ConnectionDirection->setCurrentText(newDirection);
  203. ui->comboBox_ConnectionDirection->blockSignals(false);
  204. }
  205. void Editor::setConnectionsVisibility(bool visible) {
  206. for (QGraphicsPixmapItem* item : map->connection_items) {
  207. item->setVisible(visible);
  208. item->setActive(visible);
  209. }
  210. }
  211. void Editor::setMap(QString map_name) {
  212. if (map_name.isNull()) {
  213. return;
  214. }
  215. if (project) {
  216. map = project->loadMap(map_name);
  217. displayMap();
  218. selected_events->clear();
  219. updateSelectedObjects();
  220. }
  221. }
  222. void Editor::mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item) {
  223. if (map_edit_mode == "paint") {
  224. item->paint(event);
  225. } else if (map_edit_mode == "fill") {
  226. item->floodFill(event);
  227. } else if (map_edit_mode == "pick") {
  228. item->pick(event);
  229. } else if (map_edit_mode == "select") {
  230. item->select(event);
  231. }
  232. }
  233. void Editor::mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixmapItem *item) {
  234. if (map_edit_mode == "paint") {
  235. item->paint(event);
  236. } else if (map_edit_mode == "fill") {
  237. item->floodFill(event);
  238. } else if (map_edit_mode == "pick") {
  239. item->pick(event);
  240. } else if (map_edit_mode == "select") {
  241. item->select(event);
  242. }
  243. }
  244. void Editor::displayMap() {
  245. scene = new QGraphicsScene;
  246. map_item = new MapPixmapItem(map);
  247. connect(map_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*,MapPixmapItem*)),
  248. this, SLOT(mouseEvent_map(QGraphicsSceneMouseEvent*,MapPixmapItem*)));
  249. map_item->draw();
  250. scene->addItem(map_item);
  251. collision_item = new CollisionPixmapItem(map);
  252. connect(collision_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*,CollisionPixmapItem*)),
  253. this, SLOT(mouseEvent_collision(QGraphicsSceneMouseEvent*,CollisionPixmapItem*)));
  254. collision_item->draw();
  255. scene->addItem(collision_item);
  256. objects_group = new EventGroup;
  257. scene->addItem(objects_group);
  258. if (map_item) {
  259. map_item->setVisible(false);
  260. }
  261. if (collision_item) {
  262. collision_item->setVisible(false);
  263. }
  264. if (objects_group) {
  265. objects_group->setVisible(false);
  266. }
  267. int tw = 16;
  268. int th = 16;
  269. scene->setSceneRect(
  270. -6 * tw,
  271. -6 * th,
  272. map_item->pixmap().width() + 12 * tw,
  273. map_item->pixmap().height() + 12 * th
  274. );
  275. displayMetatiles();
  276. displayCollisionMetatiles();
  277. displayElevationMetatiles();
  278. displayMapObjects();
  279. displayMapConnections();
  280. displayMapBorder();
  281. displayMapGrid();
  282. }
  283. void Editor::displayMetatiles() {
  284. scene_metatiles = new QGraphicsScene;
  285. metatiles_item = new MetatilesPixmapItem(map);
  286. metatiles_item->draw();
  287. scene_metatiles->addItem(metatiles_item);
  288. }
  289. void Editor::displayCollisionMetatiles() {
  290. scene_collision_metatiles = new QGraphicsScene;
  291. collision_metatiles_item = new CollisionMetatilesPixmapItem(map);
  292. collision_metatiles_item->draw();
  293. scene_collision_metatiles->addItem(collision_metatiles_item);
  294. }
  295. void Editor::displayElevationMetatiles() {
  296. scene_elevation_metatiles = new QGraphicsScene;
  297. elevation_metatiles_item = new ElevationMetatilesPixmapItem(map);
  298. elevation_metatiles_item->draw();
  299. scene_elevation_metatiles->addItem(elevation_metatiles_item);
  300. }
  301. void Editor::displayMapObjects() {
  302. for (QGraphicsItem *child : objects_group->childItems()) {
  303. objects_group->removeFromGroup(child);
  304. }
  305. QList<Event *> events = map->getAllEvents();
  306. project->loadObjectPixmaps(events);
  307. for (Event *event : events) {
  308. addMapObject(event);
  309. }
  310. //objects_group->setFiltersChildEvents(false);
  311. objects_group->setHandlesChildEvents(false);
  312. emit objectsChanged();
  313. }
  314. DraggablePixmapItem *Editor::addMapObject(Event *event) {
  315. DraggablePixmapItem *object = new DraggablePixmapItem(event);
  316. object->editor = this;
  317. objects_group->addToGroup(object);
  318. return object;
  319. }
  320. void Editor::displayMapConnections() {
  321. for (QGraphicsPixmapItem* item : map->connection_items) {
  322. scene->removeItem(item);
  323. delete item;
  324. }
  325. map->connection_items.clear();
  326. for (ConnectionPixmapItem* item : connection_edit_items) {
  327. delete item;
  328. }
  329. connection_edit_items.clear();
  330. for (Connection *connection : map->connections) {
  331. if (connection->direction == "dive" || connection->direction == "emerge") {
  332. continue;
  333. }
  334. createConnectionItem(connection, false);
  335. }
  336. if (!connection_edit_items.empty()) {
  337. onConnectionItemSelected(connection_edit_items.first());
  338. }
  339. }
  340. void Editor::createConnectionItem(Connection* connection, bool hide) {
  341. Map *connected_map = project->getMap(connection->map_name);
  342. QPixmap pixmap = connected_map->renderConnection(*connection);
  343. int offset = connection->offset.toInt(nullptr, 0);
  344. int x = 0, y = 0;
  345. if (connection->direction == "up") {
  346. x = offset * 16;
  347. y = -pixmap.height();
  348. } else if (connection->direction == "down") {
  349. x = offset * 16;
  350. y = map->getHeight() * 16;
  351. } else if (connection->direction == "left") {
  352. x = -pixmap.width();
  353. y = offset * 16;
  354. } else if (connection->direction == "right") {
  355. x = map->getWidth() * 16;
  356. y = offset * 16;
  357. }
  358. QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
  359. item->setZValue(-1);
  360. item->setX(x);
  361. item->setY(y);
  362. scene->addItem(item);
  363. map->connection_items.append(item);
  364. item->setVisible(!hide);
  365. ConnectionPixmapItem *connection_edit_item = new ConnectionPixmapItem(pixmap, connection, x, y);
  366. connection_edit_item->setX(x);
  367. connection_edit_item->setY(y);
  368. connection_edit_item->setZValue(-1);
  369. scene->addItem(connection_edit_item);
  370. connect(connection_edit_item, SIGNAL(connectionMoved(int)), this, SLOT(onConnectionOffsetChanged(int)));
  371. connect(connection_edit_item, SIGNAL(connectionItemSelected(ConnectionPixmapItem*)), this, SLOT(onConnectionItemSelected(ConnectionPixmapItem*)));
  372. connection_edit_items.append(connection_edit_item);
  373. }
  374. void Editor::displayMapBorder() {
  375. QPixmap pixmap = map->renderBorder();
  376. for (int y = -6; y < map->getHeight() + 6; y += 2)
  377. for (int x = -6; x < map->getWidth() + 6; x += 2) {
  378. QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
  379. item->setX(x * 16);
  380. item->setY(y * 16);
  381. item->setZValue(-2);
  382. scene->addItem(item);
  383. borderItems.append(item);
  384. }
  385. }
  386. void Editor::displayMapGrid() {
  387. int pixelWidth = map->getWidth() * 16;
  388. int pixelHeight = map->getHeight() * 16;
  389. for (int i = 0; i <= map->getWidth(); i++) {
  390. int x = i * 16;
  391. QGraphicsLineItem *line = scene->addLine(x, 0, x, pixelHeight);
  392. line->setVisible(ui->checkBox_ToggleGrid->isChecked());
  393. connect(ui->checkBox_ToggleGrid, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);});
  394. }
  395. for (int j = 0; j <= map->getHeight(); j++) {
  396. int y = j * 16;
  397. QGraphicsLineItem *line = scene->addLine(0, y, pixelWidth, y);
  398. line->setVisible(ui->checkBox_ToggleGrid->isChecked());
  399. connect(ui->checkBox_ToggleGrid, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);});
  400. }
  401. }
  402. void Editor::updateConnectionOffset(int offset) {
  403. if (!current_connection_edit_item)
  404. return;
  405. current_connection_edit_item->blockSignals(true);
  406. current_connection_edit_item->connection->offset = QString::number(offset);
  407. if (current_connection_edit_item->connection->direction == "up" || current_connection_edit_item->connection->direction == "down") {
  408. current_connection_edit_item->setX(current_connection_edit_item->initialX + (offset - current_connection_edit_item->initialOffset) * 16);
  409. } else {
  410. current_connection_edit_item->setY(current_connection_edit_item->initialY + (offset - current_connection_edit_item->initialOffset) * 16);
  411. }
  412. current_connection_edit_item->blockSignals(false);
  413. }
  414. void Editor::updateConnectionMap(QString mapName, QString direction) {
  415. if (!mapName.isEmpty() && !project->mapNames->contains(mapName)) {
  416. qDebug() << "Invalid map name " << mapName << " specified for connection.";
  417. return;
  418. }
  419. if (!current_connection_edit_item)
  420. return;
  421. if (mapName.isEmpty()) {
  422. removeCurrentConnection();
  423. return;
  424. } else {
  425. setConnectionEditControlsEnabled(true);
  426. }
  427. current_connection_edit_item->connection->map_name = mapName;
  428. setCurrentConnectionDirection(direction);
  429. }
  430. void Editor::addNewConnection() {
  431. // Find direction with least number of connections.
  432. QMap<QString, int> directionCounts = QMap<QString, int>({{"up", 0}, {"right", 0}, {"down", 0}, {"left", 0}});
  433. for (Connection* connection : map->connections) {
  434. directionCounts[connection->direction]++;
  435. }
  436. QString minDirection = "up";
  437. int minCount = INT_MAX;
  438. for (QString direction : directionCounts.keys()) {
  439. if (directionCounts[direction] < minCount) {
  440. minDirection = direction;
  441. minCount = directionCounts[direction];
  442. }
  443. }
  444. Connection* newConnection = new Connection;
  445. newConnection->direction = minDirection;
  446. newConnection->offset = "0";
  447. newConnection->map_name = project->mapNames->first();
  448. map->connections.append(newConnection);
  449. createConnectionItem(newConnection, true);
  450. onConnectionItemSelected(connection_edit_items.last());
  451. ui->label_NumConnections->setText(QString::number(map->connections.length()));
  452. }
  453. void Editor::removeCurrentConnection() {
  454. if (!current_connection_edit_item)
  455. return;
  456. map->connections.removeOne(current_connection_edit_item->connection);
  457. connection_edit_items.removeOne(current_connection_edit_item);
  458. scene->removeItem(current_connection_edit_item);
  459. delete current_connection_edit_item;
  460. current_connection_edit_item = NULL;
  461. setConnectionEditControlsEnabled(false);
  462. ui->spinBox_ConnectionOffset->setValue(0);
  463. ui->label_NumConnections->setText(QString::number(map->connections.length()));
  464. if (connection_edit_items.length() > 0) {
  465. onConnectionItemSelected(connection_edit_items.last());
  466. }
  467. }
  468. void MetatilesPixmapItem::paintTileChanged(Map *map) {
  469. draw();
  470. }
  471. void MetatilesPixmapItem::draw() {
  472. setPixmap(map->renderMetatiles());
  473. }
  474. void MetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
  475. int x = ((int)pos.x()) / 16;
  476. int y = ((int)pos.y()) / 16;
  477. int width = pixmap().width() / 16;
  478. int height = pixmap().height() / 16;
  479. if (x < 0 || x >= width || y < 0 || y >= height) {
  480. map->clearHoveredMetatile();
  481. } else {
  482. int block = y * width + x;
  483. map->hoveredMetatileChanged(block);
  484. }
  485. }
  486. void MetatilesPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
  487. updateCurHoveredMetatile(event->pos());
  488. }
  489. void MetatilesPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
  490. map->clearHoveredMetatile();
  491. }
  492. void MetatilesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  493. QPointF pos = event->pos();
  494. int x = ((int)pos.x()) / 16;
  495. int y = ((int)pos.y()) / 16;
  496. map->paint_metatile_initial_x = x;
  497. map->paint_metatile_initial_y = y;
  498. updateSelection(event->pos(), event->button());
  499. }
  500. void MetatilesPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  501. updateCurHoveredMetatile(event->pos());
  502. Qt::MouseButton button = event->button();
  503. if (button == Qt::MouseButton::NoButton) {
  504. Qt::MouseButtons heldButtons = event->buttons();
  505. if (heldButtons & Qt::RightButton) {
  506. button = Qt::RightButton;
  507. } else if (heldButtons & Qt::LeftButton) {
  508. button = Qt::LeftButton;
  509. }
  510. }
  511. updateSelection(event->pos(), button);
  512. }
  513. void MetatilesPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  514. updateSelection(event->pos(), event->button());
  515. }
  516. void MetatilesPixmapItem::updateSelection(QPointF pos, Qt::MouseButton button) {
  517. int x = ((int)pos.x()) / 16;
  518. int y = ((int)pos.y()) / 16;
  519. int width = pixmap().width() / 16;
  520. int height = pixmap().height() / 16;
  521. if ((x >= 0 && x < width) && (y >=0 && y < height)) {
  522. int baseTileX = x < map->paint_metatile_initial_x ? x : map->paint_metatile_initial_x;
  523. int baseTileY = y < map->paint_metatile_initial_y ? y : map->paint_metatile_initial_y;
  524. map->paint_tile = baseTileY * 8 + baseTileX;
  525. map->paint_tile_width = abs(map->paint_metatile_initial_x - x) + 1;
  526. map->paint_tile_height = abs(map->paint_metatile_initial_y - y) + 1;
  527. map->smart_paths_enabled = button == Qt::RightButton
  528. && map->paint_tile_width == 3
  529. && map->paint_tile_height == 3;
  530. emit map->paintTileChanged(map);
  531. }
  532. }
  533. void CollisionMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
  534. int x = ((int)pos.x()) / 16;
  535. int y = ((int)pos.y()) / 16;
  536. int width = pixmap().width() / 16;
  537. int height = pixmap().height() / 16;
  538. if (x < 0 || x >= width || y < 0 || y >= height) {
  539. map->clearHoveredCollisionTile();
  540. } else {
  541. int collision = y * width + x;
  542. map->hoveredCollisionTileChanged(collision);
  543. }
  544. }
  545. QVariant ConnectionPixmapItem::itemChange(GraphicsItemChange change, const QVariant &value)
  546. {
  547. if (change == ItemPositionChange) {
  548. QPointF newPos = value.toPointF();
  549. qreal x, y;
  550. int newOffset = initialOffset;
  551. if (connection->direction == "up" || connection->direction == "down") {
  552. x = round(newPos.x() / 16) * 16;
  553. newOffset += (x - initialX) / 16;
  554. }
  555. else {
  556. x = initialX;
  557. }
  558. if (connection->direction == "right" || connection->direction == "left") {
  559. y = round(newPos.y() / 16) * 16;
  560. newOffset += (y - initialY) / 16;
  561. }
  562. else {
  563. y = initialY;
  564. }
  565. emit connectionMoved(newOffset);
  566. connection->offset = QString::number(newOffset);
  567. return QPointF(x, y);
  568. }
  569. else {
  570. return QGraphicsItem::itemChange(change, value);
  571. }
  572. }
  573. void ConnectionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {
  574. emit connectionItemSelected(this);
  575. }
  576. void ElevationMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
  577. int x = ((int)pos.x()) / 16;
  578. int y = ((int)pos.y()) / 16;
  579. int width = pixmap().width() / 16;
  580. int height = pixmap().height() / 16;
  581. if (x < 0 || x >= width || y < 0 || y >= height) {
  582. map->clearHoveredElevationTile();
  583. } else {
  584. int elevation = y * width + x;
  585. map->hoveredElevationTileChanged(elevation);
  586. }
  587. }
  588. void MapPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
  589. if (map) {
  590. QPointF pos = event->pos();
  591. int x = (int)(pos.x()) / 16;
  592. int y = (int)(pos.y()) / 16;
  593. if (map->smart_paths_enabled) {
  594. paintSmartPath(x, y);
  595. } else {
  596. paintNormal(x, y);
  597. }
  598. if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  599. map->commit();
  600. }
  601. draw();
  602. }
  603. }
  604. void MapPixmapItem::paintNormal(int x, int y) {
  605. // Snap the selected position to the top-left of the block boundary.
  606. // This allows painting via dragging the mouse to tile the painted region.
  607. int xDiff = x - map->paint_tile_initial_x;
  608. int yDiff = y - map->paint_tile_initial_y;
  609. if (xDiff < 0 && xDiff % map->paint_tile_width != 0) xDiff -= map->paint_tile_width;
  610. if (yDiff < 0 && yDiff % map->paint_tile_height != 0) yDiff -= map->paint_tile_height;
  611. x = map->paint_tile_initial_x + (xDiff / map->paint_tile_width) * map->paint_tile_width;
  612. y = map->paint_tile_initial_y + (yDiff / map->paint_tile_height) * map->paint_tile_height;
  613. for (int i = 0; i < map->paint_tile_width && i + x < map->getWidth(); i++)
  614. for (int j = 0; j < map->paint_tile_height && j + y < map->getHeight(); j++) {
  615. int actualX = i + x;
  616. int actualY = j + y;
  617. Block *block = map->getBlock(actualX, actualY);
  618. if (block) {
  619. block->tile = map->paint_tile + i + (j * 8);
  620. map->_setBlock(actualX, actualY, *block);
  621. }
  622. }
  623. }
  624. // These are tile offsets from the top-left tile in the 3x3 smart path selection.
  625. // Each entry is for one possibility from the marching squares value for a tile.
  626. // (Marching Squares: https://en.wikipedia.org/wiki/Marching_squares)
  627. QList<int> MapPixmapItem::smartPathTable = QList<int>({
  628. 8 + 1, // 0000
  629. 8 + 1, // 0001
  630. 8 + 1, // 0010
  631. 16 + 0, // 0011
  632. 8 + 1, // 0100
  633. 8 + 1, // 0101
  634. 0 + 0, // 0110
  635. 8 + 0, // 0111
  636. 8 + 1, // 1000
  637. 16 + 2, // 1001
  638. 8 + 1, // 1010
  639. 16 + 1, // 1011
  640. 0 + 2, // 1100
  641. 8 + 2, // 1101
  642. 0 + 1, // 1110
  643. 8 + 1, // 1111
  644. });
  645. #define IS_SMART_PATH_TILE(block) ((block->tile >= map->paint_tile && block->tile < map->paint_tile + 3) \
  646. || (block->tile >= map->paint_tile + 8 && block->tile < map->paint_tile + 11) \
  647. || (block->tile >= map->paint_tile + 16 && block->tile < map->paint_tile + 19))
  648. void MapPixmapItem::paintSmartPath(int x, int y) {
  649. // Smart path should never be enabled without a 3x3 block selection.
  650. if (map->paint_tile_width != 3 || map->paint_tile_height != 3) return;
  651. // Shift to the middle tile of the smart path selection.
  652. int openTile = map->paint_tile + 8 + 1;
  653. // Fill the region with the open tile.
  654. for (int i = -1; i <= 1 && i + x < map->getWidth() && i + x >= 0; i++)
  655. for (int j = -1; j <= 1 && j + y < map->getHeight() && j + y >= 0; j++) {
  656. int actualX = i + x;
  657. int actualY = j + y;
  658. Block *block = map->getBlock(actualX, actualY);
  659. if (block) {
  660. block->tile = openTile;
  661. map->_setBlock(actualX, actualY, *block);
  662. }
  663. }
  664. // Go back and resolve the edge tiles
  665. for (int i = -2; i <= 2 && i + x < map->getWidth() && i + x >= 0; i++)
  666. for (int j = -2; j <= 2 && j + y < map->getHeight() && j + y >= 0; j++) {
  667. // Ignore the corners, which can't possible be affected by the smart path.
  668. if ((i == -2 && j == -2) || (i == 2 && j == -2) ||
  669. (i == -2 && j == 2) || (i == 2 && j == 2))
  670. continue;
  671. // Ignore tiles that aren't part of the smart path set.
  672. int actualX = i + x;
  673. int actualY = j + y;
  674. Block *block = map->getBlock(actualX, actualY);
  675. if (!block || !IS_SMART_PATH_TILE(block)) {
  676. continue;
  677. }
  678. int id = 0;
  679. Block *top = map->getBlock(actualX, actualY - 1);
  680. Block *right = map->getBlock(actualX + 1, actualY);
  681. Block *bottom = map->getBlock(actualX, actualY + 1);
  682. Block *left = map->getBlock(actualX - 1, actualY);
  683. // Get marching squares value, to determine which tile to use.
  684. if (top && IS_SMART_PATH_TILE(top))
  685. id += 1;
  686. if (right && IS_SMART_PATH_TILE(right))
  687. id += 2;
  688. if (bottom && IS_SMART_PATH_TILE(bottom))
  689. id += 4;
  690. if (left && IS_SMART_PATH_TILE(left))
  691. id += 8;
  692. block->tile = map->paint_tile + smartPathTable[id];;
  693. map->_setBlock(actualX, actualY, *block);
  694. }
  695. }
  696. void MapPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
  697. if (map) {
  698. QPointF pos = event->pos();
  699. int x = (int)(pos.x()) / 16;
  700. int y = (int)(pos.y()) / 16;
  701. map->floodFill(x, y, map->paint_tile);
  702. draw();
  703. }
  704. }
  705. void MapPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
  706. QPointF pos = event->pos();
  707. int x = (int)(pos.x()) / 16;
  708. int y = (int)(pos.y()) / 16;
  709. Block *block = map->getBlock(x, y);
  710. if (block) {
  711. map->paint_tile = block->tile;
  712. map->paint_tile_width = 1;
  713. map->paint_tile_height = 1;
  714. emit map->paintTileChanged(map);
  715. }
  716. }
  717. #define SWAP(a, b) do { if (a != b) { a ^= b; b ^= a; a ^= b; } } while (0)
  718. void MapPixmapItem::select(QGraphicsSceneMouseEvent *event) {
  719. QPointF pos = event->pos();
  720. int x = (int)(pos.x()) / 16;
  721. int y = (int)(pos.y()) / 16;
  722. if (event->type() == QEvent::GraphicsSceneMousePress) {
  723. selection_origin = QPoint(x, y);
  724. selection.clear();
  725. } else if (event->type() == QEvent::GraphicsSceneMouseMove) {
  726. if (event->buttons() & Qt::LeftButton) {
  727. selection.clear();
  728. selection.append(QPoint(x, y));
  729. }
  730. } else if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  731. if (!selection.isEmpty()) {
  732. QPoint pos = selection.last();
  733. int x1 = selection_origin.x();
  734. int y1 = selection_origin.y();
  735. int x2 = pos.x();
  736. int y2 = pos.y();
  737. if (x1 > x2) SWAP(x1, x2);
  738. if (y1 > y2) SWAP(y1, y2);
  739. selection.clear();
  740. for (int y = y1; y <= y2; y++) {
  741. for (int x = x1; x <= x2; x++) {
  742. selection.append(QPoint(x, y));
  743. }
  744. }
  745. qDebug() << QString("selected (%1, %2) -> (%3, %4)").arg(x1).arg(y1).arg(x2).arg(y2);
  746. }
  747. }
  748. }
  749. void MapPixmapItem::draw() {
  750. if (map) {
  751. setPixmap(map->render());
  752. }
  753. }
  754. void MapPixmapItem::undo() {
  755. if (map) {
  756. map->undo();
  757. draw();
  758. }
  759. }
  760. void MapPixmapItem::redo() {
  761. if (map) {
  762. map->redo();
  763. draw();
  764. }
  765. }
  766. void MapPixmapItem::updateCurHoveredTile(QPointF pos) {
  767. int x = ((int)pos.x()) / 16;
  768. int y = ((int)pos.y()) / 16;
  769. int blockIndex = y * map->getWidth() + x;
  770. if (x < 0 || x >= map->getWidth() || y < 0 || y >= map->getHeight()) {
  771. map->clearHoveredTile();
  772. } else {
  773. int tile = map->blockdata->blocks->at(blockIndex).tile;
  774. map->hoveredTileChanged(x, y, tile);
  775. }
  776. }
  777. void MapPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
  778. updateCurHoveredTile(event->pos());
  779. }
  780. void MapPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
  781. map->clearHoveredTile();
  782. }
  783. void MapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  784. QPointF pos = event->pos();
  785. int x = ((int)pos.x()) / 16;
  786. int y = ((int)pos.y()) / 16;
  787. map->paint_tile_initial_x = x;
  788. map->paint_tile_initial_y = y;
  789. emit mouseEvent(event, this);
  790. }
  791. void MapPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  792. updateCurHoveredTile(event->pos());
  793. emit mouseEvent(event, this);
  794. }
  795. void MapPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  796. emit mouseEvent(event, this);
  797. }
  798. void CollisionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  799. emit mouseEvent(event, this);
  800. }
  801. void CollisionPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  802. emit mouseEvent(event, this);
  803. }
  804. void CollisionPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  805. emit mouseEvent(event, this);
  806. }
  807. void CollisionPixmapItem::draw() {
  808. if (map) {
  809. setPixmap(map->renderCollision());
  810. }
  811. }
  812. void CollisionPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
  813. if (map) {
  814. QPointF pos = event->pos();
  815. int x = (int)(pos.x()) / 16;
  816. int y = (int)(pos.y()) / 16;
  817. Block *block = map->getBlock(x, y);
  818. if (block) {
  819. if (map->paint_collision >= 0) {
  820. block->collision = map->paint_collision;
  821. }
  822. if (map->paint_elevation >= 0) {
  823. block->elevation = map->paint_elevation;
  824. }
  825. map->_setBlock(x, y, *block);
  826. }
  827. if (event->type() == QEvent::GraphicsSceneMouseRelease) {
  828. map->commit();
  829. }
  830. draw();
  831. }
  832. }
  833. void CollisionPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
  834. if (map) {
  835. QPointF pos = event->pos();
  836. int x = (int)(pos.x()) / 16;
  837. int y = (int)(pos.y()) / 16;
  838. bool collision = map->paint_collision >= 0;
  839. bool elevation = map->paint_elevation >= 0;
  840. if (collision && elevation) {
  841. map->floodFillCollisionElevation(x, y, map->paint_collision, map->paint_elevation);
  842. } else if (collision) {
  843. map->floodFillCollision(x, y, map->paint_collision);
  844. } else if (elevation) {
  845. map->floodFillElevation(x, y, map->paint_elevation);
  846. }
  847. draw();
  848. }
  849. }
  850. void CollisionPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
  851. QPointF pos = event->pos();
  852. int x = (int)(pos.x()) / 16;
  853. int y = (int)(pos.y()) / 16;
  854. Block *block = map->getBlock(x, y);
  855. if (block) {
  856. map->paint_collision = block->collision;
  857. map->paint_elevation = block->elevation;
  858. emit map->paintCollisionChanged(map);
  859. }
  860. }
  861. void DraggablePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *mouse) {
  862. active = true;
  863. clicking = true;
  864. last_x = (mouse->pos().x() + this->pos().x()) / 16;
  865. last_y = (mouse->pos().y() + this->pos().y()) / 16;
  866. //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("y"));
  867. }
  868. void DraggablePixmapItem::move(int x, int y) {
  869. event->setX(event->x() + x);
  870. event->setY(event->y() + y);
  871. updatePosition();
  872. emitPositionChanged();
  873. }
  874. void DraggablePixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouse) {
  875. if (active) {
  876. int x = (mouse->pos().x() + this->pos().x()) / 16;
  877. int y = (mouse->pos().y() + this->pos().y()) / 16;
  878. if (x != last_x || y != last_y) {
  879. clicking = false;
  880. if (editor->selected_events->contains(this)) {
  881. for (DraggablePixmapItem *item : *editor->selected_events) {
  882. item->move(x - last_x, y - last_y);
  883. }
  884. } else {
  885. move(x - last_x, y - last_y);
  886. }
  887. last_x = x;
  888. last_y = y;
  889. //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("x"));
  890. }
  891. }
  892. }
  893. void DraggablePixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouse) {
  894. if (clicking) {
  895. this->editor->selectMapObject(this, mouse->modifiers() & Qt::ControlModifier);
  896. this->editor->updateSelectedObjects();
  897. }
  898. active = false;
  899. }
  900. QList<DraggablePixmapItem *> *Editor::getObjects() {
  901. QList<DraggablePixmapItem *> *list = new QList<DraggablePixmapItem *>;
  902. for (Event *event : map->getAllEvents()) {
  903. for (QGraphicsItem *child : objects_group->childItems()) {
  904. DraggablePixmapItem *item = (DraggablePixmapItem *)child;
  905. if (item->event == event) {
  906. list->append(item);
  907. break;
  908. }
  909. }
  910. }
  911. return list;
  912. }
  913. void Editor::redrawObject(DraggablePixmapItem *item) {
  914. if (item) {
  915. item->setPixmap(item->event->pixmap);
  916. if (selected_events && selected_events->contains(item)) {
  917. QImage image = item->pixmap().toImage();
  918. QPainter painter(&image);
  919. painter.setPen(QColor(250, 100, 25));
  920. painter.drawRect(0, 0, image.width() - 1, image.height() - 1);
  921. painter.end();
  922. item->setPixmap(QPixmap::fromImage(image));
  923. }
  924. }
  925. }
  926. void Editor::updateSelectedObjects() {
  927. for (DraggablePixmapItem *item : *(getObjects())) {
  928. redrawObject(item);
  929. }
  930. emit selectedObjectsChanged();
  931. }
  932. void Editor::selectMapObject(DraggablePixmapItem *object) {
  933. selectMapObject(object, false);
  934. }
  935. void Editor::selectMapObject(DraggablePixmapItem *object, bool toggle) {
  936. if (selected_events && object) {
  937. if (selected_events->contains(object)) {
  938. if (toggle) {
  939. selected_events->removeOne(object);
  940. }
  941. } else {
  942. if (!toggle) {
  943. selected_events->clear();
  944. }
  945. selected_events->append(object);
  946. }
  947. updateSelectedObjects();
  948. }
  949. }
  950. DraggablePixmapItem* Editor::addNewEvent() {
  951. return addNewEvent("object");
  952. }
  953. DraggablePixmapItem* Editor::addNewEvent(QString event_type) {
  954. if (project && map) {
  955. Event *event = new Event;
  956. event->put("map_name", map->name);
  957. event->put("event_type", event_type);
  958. map->addEvent(event);
  959. project->loadObjectPixmaps(map->getAllEvents());
  960. DraggablePixmapItem *object = addMapObject(event);
  961. return object;
  962. }
  963. return NULL;
  964. }
  965. void Editor::deleteEvent(Event *event) {
  966. Map *map = project->getMap(event->get("map_name"));
  967. if (map) {
  968. map->removeEvent(event);
  969. }
  970. //selected_events->removeAll(event);
  971. //updateSelectedObjects();
  972. }
  973. // dunno how to detect bubbling. QMouseEvent::isAccepted seems to always be true
  974. // check if selected_events changed instead. this has the side effect of deselecting
  975. // when you click on a selected event, since selected_events doesn't change.
  976. QList<DraggablePixmapItem *> selected_events_test;
  977. bool clicking = false;
  978. void Editor::objectsView_onMousePress(QMouseEvent *event) {
  979. clicking = true;
  980. selected_events_test = *selected_events;
  981. }
  982. void Editor::objectsView_onMouseMove(QMouseEvent *event) {
  983. clicking = false;
  984. }
  985. void Editor::objectsView_onMouseRelease(QMouseEvent *event) {
  986. if (clicking) {
  987. if (selected_events_test.length()) {
  988. if (selected_events_test.length() == selected_events->length()) {
  989. bool deselect = true;
  990. for (int i = 0; i < selected_events_test.length(); i++) {
  991. if (selected_events_test.at(i) != selected_events->at(i)) {
  992. deselect = false;
  993. break;
  994. }
  995. }
  996. if (deselect) {
  997. selected_events->clear();
  998. updateSelectedObjects();
  999. }
  1000. }
  1001. }
  1002. clicking = false;
  1003. }
  1004. }