Нема описа

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