暂无描述

editor.cpp 41KB

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