12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619 |
- #include "editor.h"
- #include "event.h"
- #include <QCheckBox>
- #include <QPainter>
- #include <QMouseEvent>
-
- bool selectingEvent = false;
-
- Editor::Editor(Ui::MainWindow* ui)
- {
- this->ui = ui;
- selected_events = new QList<DraggablePixmapItem*>;
- }
-
- void Editor::saveProject() {
- if (project) {
- project->saveAllMaps();
- project->saveAllDataStructures();
- }
- }
-
- void Editor::save() {
- if (project && map) {
- project->saveMap(map);
- project->saveAllDataStructures();
- }
- }
-
- void Editor::undo() {
- if (current_view) {
- map->undo();
- map_item->draw();
- }
- }
-
- void Editor::redo() {
- if (current_view) {
- map->redo();
- map_item->draw();
- }
- }
-
- void Editor::setEditingMap() {
- current_view = map_item;
- if (map_item) {
- displayMapConnections();
- map_item->draw();
- map_item->setVisible(true);
- map_item->setEnabled(true);
- setConnectionsVisibility(true);
- }
- if (collision_item) {
- collision_item->setVisible(false);
- }
- if (events_group) {
- events_group->setVisible(false);
- }
- setBorderItemsVisible(true);
- setConnectionItemsVisible(false);
- }
-
- void Editor::setEditingCollision() {
- current_view = collision_item;
- if (collision_item) {
- displayMapConnections();
- collision_item->draw();
- collision_item->setVisible(true);
- setConnectionsVisibility(true);
- }
- if (map_item) {
- map_item->setVisible(false);
- }
- if (events_group) {
- events_group->setVisible(false);
- }
- setBorderItemsVisible(true);
- setConnectionItemsVisible(false);
- }
-
- void Editor::setEditingObjects() {
- current_view = map_item;
- if (events_group) {
- events_group->setVisible(true);
- }
- if (map_item) {
- map_item->setVisible(true);
- map_item->setEnabled(false);
- setConnectionsVisibility(true);
- }
- if (collision_item) {
- collision_item->setVisible(false);
- }
- setBorderItemsVisible(true);
- setConnectionItemsVisible(false);
- }
-
- void Editor::setEditingConnections() {
- current_view = map_item;
- if (map_item) {
- map_item->draw();
- map_item->setVisible(true);
- map_item->setEnabled(false);
- populateConnectionMapPickers();
- ui->label_NumConnections->setText(QString::number(map->connections.length()));
- setConnectionsVisibility(false);
- setDiveEmergeControls();
- setConnectionEditControlsEnabled(selected_connection_item != NULL);
- if (selected_connection_item) {
- onConnectionOffsetChanged(selected_connection_item->connection->offset.toInt());
- setConnectionMap(selected_connection_item->connection->map_name);
- setCurrentConnectionDirection(selected_connection_item->connection->direction);
- }
- }
- if (collision_item) {
- collision_item->setVisible(false);
- }
- if (events_group) {
- events_group->setVisible(false);
- }
- setBorderItemsVisible(true, 0.4);
- setConnectionItemsVisible(true);
- }
-
- void Editor::setDiveEmergeControls() {
- ui->comboBox_DiveMap->blockSignals(true);
- ui->comboBox_EmergeMap->blockSignals(true);
- ui->comboBox_DiveMap->setCurrentText("");
- ui->comboBox_EmergeMap->setCurrentText("");
- for (Connection* connection : map->connections) {
- if (connection->direction == "dive") {
- ui->comboBox_DiveMap->setCurrentText(connection->map_name);
- } else if (connection->direction == "emerge") {
- ui->comboBox_EmergeMap->setCurrentText(connection->map_name);
- }
- }
- ui->comboBox_DiveMap->blockSignals(false);
- ui->comboBox_EmergeMap->blockSignals(false);
- }
-
- void Editor::populateConnectionMapPickers() {
- ui->comboBox_ConnectedMap->blockSignals(true);
- ui->comboBox_DiveMap->blockSignals(true);
- ui->comboBox_EmergeMap->blockSignals(true);
-
- ui->comboBox_ConnectedMap->clear();
- ui->comboBox_ConnectedMap->addItems(*project->mapNames);
- ui->comboBox_DiveMap->clear();
- ui->comboBox_DiveMap->addItems(*project->mapNames);
- ui->comboBox_EmergeMap->clear();
- ui->comboBox_EmergeMap->addItems(*project->mapNames);
-
- ui->comboBox_ConnectedMap->blockSignals(false);
- ui->comboBox_DiveMap->blockSignals(true);
- ui->comboBox_EmergeMap->blockSignals(true);
- }
-
- void Editor::setConnectionItemsVisible(bool visible) {
- for (ConnectionPixmapItem* item : connection_edit_items) {
- item->setVisible(visible);
- item->setEnabled(visible);
- }
- }
-
- void Editor::setBorderItemsVisible(bool visible, qreal opacity) {
- for (QGraphicsPixmapItem* item : borderItems) {
- item->setVisible(visible);
- item->setOpacity(opacity);
- }
- }
-
- void Editor::setCurrentConnectionDirection(QString curDirection) {
- if (!selected_connection_item)
- return;
-
- selected_connection_item->connection->direction = curDirection;
-
- Map *connected_map = project->getMap(selected_connection_item->connection->map_name);
- QPixmap pixmap = connected_map->renderConnection(*selected_connection_item->connection);
- int offset = selected_connection_item->connection->offset.toInt(nullptr, 0);
- selected_connection_item->initialOffset = offset;
- int x = 0, y = 0;
- if (selected_connection_item->connection->direction == "up") {
- x = offset * 16;
- y = -pixmap.height();
- } else if (selected_connection_item->connection->direction == "down") {
- x = offset * 16;
- y = map->getHeight() * 16;
- } else if (selected_connection_item->connection->direction == "left") {
- x = -pixmap.width();
- y = offset * 16;
- } else if (selected_connection_item->connection->direction == "right") {
- x = map->getWidth() * 16;
- y = offset * 16;
- }
-
- selected_connection_item->basePixmap = pixmap;
- QPainter painter(&pixmap);
- painter.setPen(QColor(255, 0, 255));
- painter.drawRect(0, 0, pixmap.width() - 1, pixmap.height() - 1);
- painter.end();
- selected_connection_item->setPixmap(pixmap);
- selected_connection_item->initialX = x;
- selected_connection_item->initialY = y;
- selected_connection_item->blockSignals(true);
- selected_connection_item->setX(x);
- selected_connection_item->setY(y);
- selected_connection_item->setZValue(-1);
- selected_connection_item->blockSignals(false);
-
- setConnectionEditControlValues(selected_connection_item->connection);
- }
-
- void Editor::updateCurrentConnectionDirection(QString curDirection) {
- if (!selected_connection_item)
- return;
-
- QString originalDirection = selected_connection_item->connection->direction;
- setCurrentConnectionDirection(curDirection);
- updateMirroredConnectionDirection(selected_connection_item->connection, originalDirection);
- }
-
- void Editor::onConnectionMoved(Connection* connection) {
- updateMirroredConnectionOffset(connection);
- onConnectionOffsetChanged(connection->offset.toInt());
- }
-
- void Editor::onConnectionOffsetChanged(int newOffset) {
- ui->spinBox_ConnectionOffset->blockSignals(true);
- ui->spinBox_ConnectionOffset->setValue(newOffset);
- ui->spinBox_ConnectionOffset->blockSignals(false);
-
- }
-
- void Editor::setConnectionEditControlValues(Connection* connection) {
- QString mapName = connection ? connection->map_name : "";
- QString direction = connection ? connection->direction : "";
- int offset = connection ? connection->offset.toInt() : 0;
-
- ui->comboBox_ConnectedMap->blockSignals(true);
- ui->comboBox_ConnectionDirection->blockSignals(true);
- ui->spinBox_ConnectionOffset->blockSignals(true);
-
- ui->comboBox_ConnectedMap->setCurrentText(mapName);
- ui->comboBox_ConnectionDirection->setCurrentText(direction);
- ui->spinBox_ConnectionOffset->setValue(offset);
-
- ui->comboBox_ConnectedMap->blockSignals(false);
- ui->comboBox_ConnectionDirection->blockSignals(false);
- ui->spinBox_ConnectionOffset->blockSignals(false);
- }
-
- void Editor::setConnectionEditControlsEnabled(bool enabled) {
- ui->comboBox_ConnectionDirection->setEnabled(enabled);
- ui->comboBox_ConnectedMap->setEnabled(enabled);
- ui->spinBox_ConnectionOffset->setEnabled(enabled);
-
- if (!enabled) {
- setConnectionEditControlValues(false);
- }
- }
-
- void Editor::onConnectionItemSelected(ConnectionPixmapItem* connectionItem) {
- if (!connectionItem)
- return;
-
- for (ConnectionPixmapItem* item : connection_edit_items) {
- bool isSelectedItem = item == connectionItem;
- int zValue = isSelectedItem ? 0 : -1;
- qreal opacity = isSelectedItem ? 1 : 0.75;
- item->setZValue(zValue);
- item->render(opacity);
- if (isSelectedItem) {
- QPixmap pixmap = item->pixmap();
- QPainter painter(&pixmap);
- painter.setPen(QColor(255, 0, 255));
- painter.drawRect(0, 0, pixmap.width() - 1, pixmap.height() - 1);
- painter.end();
- item->setPixmap(pixmap);
- }
- }
- selected_connection_item = connectionItem;
- setConnectionEditControlsEnabled(true);
- setConnectionEditControlValues(selected_connection_item->connection);
- ui->spinBox_ConnectionOffset->setMaximum(selected_connection_item->getMaxOffset());
- ui->spinBox_ConnectionOffset->setMinimum(selected_connection_item->getMinOffset());
- onConnectionOffsetChanged(selected_connection_item->connection->offset.toInt());
- }
-
- void Editor::setSelectedConnectionFromMap(QString mapName) {
- // Search for the first connection that connects to the given map map.
- for (ConnectionPixmapItem* item : connection_edit_items) {
- if (item->connection->map_name == mapName) {
- onConnectionItemSelected(item);
- break;
- }
- }
- }
-
- void Editor::onConnectionItemDoubleClicked(ConnectionPixmapItem* connectionItem) {
- emit loadMapRequested(connectionItem->connection->map_name, map->name);
- }
-
- void Editor::onConnectionDirectionChanged(QString newDirection) {
- ui->comboBox_ConnectionDirection->blockSignals(true);
- ui->comboBox_ConnectionDirection->setCurrentText(newDirection);
- ui->comboBox_ConnectionDirection->blockSignals(false);
- }
-
- void Editor::onBorderMetatilesChanged() {
- displayMapBorder();
- }
-
- void Editor::setConnectionsVisibility(bool visible) {
- for (QGraphicsPixmapItem* item : connection_items) {
- item->setVisible(visible);
- item->setActive(visible);
- }
- }
-
- void Editor::setMap(QString map_name) {
- if (map_name.isNull()) {
- return;
- }
- if (project) {
- map = project->loadMap(map_name);
- selected_events->clear();
- displayMap();
- updateSelectedEvents();
- }
- }
-
- void Editor::mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item) {
- if (map_edit_mode == "paint") {
- item->paint(event);
- } else if (map_edit_mode == "fill") {
- item->floodFill(event);
- } else if (map_edit_mode == "pick") {
- item->pick(event);
- } else if (map_edit_mode == "select") {
- item->select(event);
- }
- }
- void Editor::mouseEvent_collision(QGraphicsSceneMouseEvent *event, CollisionPixmapItem *item) {
- if (map_edit_mode == "paint") {
- item->paint(event);
- } else if (map_edit_mode == "fill") {
- item->floodFill(event);
- } else if (map_edit_mode == "pick") {
- item->pick(event);
- } else if (map_edit_mode == "select") {
- item->select(event);
- }
- }
-
- void Editor::displayMap() {
- if (!scene)
- scene = new QGraphicsScene;
-
- if (map_item && scene) {
- scene->removeItem(map_item);
- delete map_item;
- }
- map_item = new MapPixmapItem(map);
- connect(map_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*,MapPixmapItem*)),
- this, SLOT(mouseEvent_map(QGraphicsSceneMouseEvent*,MapPixmapItem*)));
-
- map_item->draw(true);
- scene->addItem(map_item);
-
- if (collision_item && scene) {
- scene->removeItem(collision_item);
- delete collision_item;
- }
- collision_item = new CollisionPixmapItem(map);
- connect(collision_item, SIGNAL(mouseEvent(QGraphicsSceneMouseEvent*,CollisionPixmapItem*)),
- this, SLOT(mouseEvent_collision(QGraphicsSceneMouseEvent*,CollisionPixmapItem*)));
-
- collision_item->draw(true);
- scene->addItem(collision_item);
-
- int tw = 16;
- int th = 16;
- scene->setSceneRect(
- -6 * tw,
- -6 * th,
- map_item->pixmap().width() + 12 * tw,
- map_item->pixmap().height() + 12 * th
- );
-
- displayMetatiles();
- displayBorderMetatiles();
- displayCollisionMetatiles();
- displayElevationMetatiles();
- displayMapEvents();
- displayMapConnections();
- displayMapBorder();
- displayMapGrid();
-
- if (map_item) {
- map_item->setVisible(false);
- }
- if (collision_item) {
- collision_item->setVisible(false);
- }
- if (events_group) {
- events_group->setVisible(false);
- }
- }
-
- void Editor::displayMetatiles() {
- if (metatiles_item && metatiles_item->scene()) {
- metatiles_item->scene()->removeItem(metatiles_item);
- delete metatiles_item;
- }
-
- scene_metatiles = new QGraphicsScene;
- metatiles_item = new MetatilesPixmapItem(map);
- metatiles_item->draw();
- scene_metatiles->addItem(metatiles_item);
- }
-
- void Editor::displayBorderMetatiles() {
- if (selected_border_metatiles_item && selected_border_metatiles_item->scene()) {
- selected_border_metatiles_item->scene()->removeItem(selected_border_metatiles_item);
- delete selected_border_metatiles_item;
- }
-
- scene_selected_border_metatiles = new QGraphicsScene;
- selected_border_metatiles_item = new BorderMetatilesPixmapItem(map);
- selected_border_metatiles_item->draw();
- scene_selected_border_metatiles->addItem(selected_border_metatiles_item);
-
- connect(selected_border_metatiles_item, SIGNAL(borderMetatilesChanged()), this, SLOT(onBorderMetatilesChanged()));
- }
-
- void Editor::displayCollisionMetatiles() {
- if (collision_metatiles_item && collision_metatiles_item->scene()) {
- collision_metatiles_item->scene()->removeItem(collision_metatiles_item);
- delete collision_metatiles_item;
- }
-
- scene_collision_metatiles = new QGraphicsScene;
- collision_metatiles_item = new CollisionMetatilesPixmapItem(map);
- collision_metatiles_item->draw();
- scene_collision_metatiles->addItem(collision_metatiles_item);
- }
-
- void Editor::displayElevationMetatiles() {
- if (elevation_metatiles_item && elevation_metatiles_item->scene()) {
- elevation_metatiles_item->scene()->removeItem(elevation_metatiles_item);
- delete elevation_metatiles_item;
- }
-
- scene_elevation_metatiles = new QGraphicsScene;
- elevation_metatiles_item = new ElevationMetatilesPixmapItem(map);
- elevation_metatiles_item->draw();
- scene_elevation_metatiles->addItem(elevation_metatiles_item);
- }
-
- void Editor::displayMapEvents() {
- if (events_group) {
- for (QGraphicsItem *child : events_group->childItems()) {
- events_group->removeFromGroup(child);
- delete child;
- }
-
- if (events_group->scene()) {
- events_group->scene()->removeItem(events_group);
- }
-
- delete events_group;
- }
-
- selected_events->clear();
-
- events_group = new EventGroup;
- scene->addItem(events_group);
-
- QList<Event *> events = map->getAllEvents();
- project->loadEventPixmaps(events);
- for (Event *event : events) {
- addMapEvent(event);
- }
- //objects_group->setFiltersChildEvents(false);
- events_group->setHandlesChildEvents(false);
-
- emit objectsChanged();
- }
-
- DraggablePixmapItem *Editor::addMapEvent(Event *event) {
- DraggablePixmapItem *object = new DraggablePixmapItem(event, this);
- events_group->addToGroup(object);
- return object;
- }
-
- void Editor::displayMapConnections() {
- for (QGraphicsPixmapItem* item : connection_items) {
- if (item->scene()) {
- item->scene()->removeItem(item);
- }
- delete item;
- }
- connection_items.clear();
-
- for (ConnectionPixmapItem* item : connection_edit_items) {
- if (item->scene()) {
- item->scene()->removeItem(item);
- }
- delete item;
- }
- selected_connection_item = NULL;
- connection_edit_items.clear();
-
- for (Connection *connection : map->connections) {
- if (connection->direction == "dive" || connection->direction == "emerge") {
- continue;
- }
- createConnectionItem(connection, false);
- }
-
- if (!connection_edit_items.empty()) {
- onConnectionItemSelected(connection_edit_items.first());
- }
- }
-
- void Editor::createConnectionItem(Connection* connection, bool hide) {
- Map *connected_map = project->getMap(connection->map_name);
- QPixmap pixmap = connected_map->renderConnection(*connection);
- int offset = connection->offset.toInt(nullptr, 0);
- int x = 0, y = 0;
- if (connection->direction == "up") {
- x = offset * 16;
- y = -pixmap.height();
- } else if (connection->direction == "down") {
- x = offset * 16;
- y = map->getHeight() * 16;
- } else if (connection->direction == "left") {
- x = -pixmap.width();
- y = offset * 16;
- } else if (connection->direction == "right") {
- x = map->getWidth() * 16;
- y = offset * 16;
- }
-
- QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
- item->setZValue(-1);
- item->setX(x);
- item->setY(y);
- scene->addItem(item);
- connection_items.append(item);
- item->setVisible(!hide);
-
- ConnectionPixmapItem *connection_edit_item = new ConnectionPixmapItem(pixmap, connection, x, y, map->getWidth(), map->getHeight());
- connection_edit_item->setX(x);
- connection_edit_item->setY(y);
- connection_edit_item->setZValue(-1);
- scene->addItem(connection_edit_item);
- connect(connection_edit_item, SIGNAL(connectionMoved(Connection*)), this, SLOT(onConnectionMoved(Connection*)));
- connect(connection_edit_item, SIGNAL(connectionItemSelected(ConnectionPixmapItem*)), this, SLOT(onConnectionItemSelected(ConnectionPixmapItem*)));
- connect(connection_edit_item, SIGNAL(connectionItemDoubleClicked(ConnectionPixmapItem*)), this, SLOT(onConnectionItemDoubleClicked(ConnectionPixmapItem*)));
- connection_edit_items.append(connection_edit_item);
- }
-
- void Editor::displayMapBorder() {
- for (QGraphicsPixmapItem* item : borderItems) {
- if (item->scene()) {
- item->scene()->removeItem(item);
- }
- delete item;
- }
- borderItems.clear();
-
- QPixmap pixmap = map->renderBorder();
- for (int y = -6; y < map->getHeight() + 6; y += 2)
- for (int x = -6; x < map->getWidth() + 6; x += 2) {
- QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
- item->setX(x * 16);
- item->setY(y * 16);
- item->setZValue(-2);
- scene->addItem(item);
- borderItems.append(item);
- }
- }
-
- void Editor::displayMapGrid() {
- for (QGraphicsLineItem* item : gridLines) {
- if (item && item->scene()) {
- item->scene()->removeItem(item);
- }
- delete item;
- }
- gridLines.clear();
- ui->checkBox_ToggleGrid->disconnect();
-
- int pixelWidth = map->getWidth() * 16;
- int pixelHeight = map->getHeight() * 16;
- for (int i = 0; i <= map->getWidth(); i++) {
- int x = i * 16;
- QGraphicsLineItem *line = scene->addLine(x, 0, x, pixelHeight);
- line->setVisible(ui->checkBox_ToggleGrid->isChecked());
- gridLines.append(line);
- connect(ui->checkBox_ToggleGrid, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);});
- }
- for (int j = 0; j <= map->getHeight(); j++) {
- int y = j * 16;
- QGraphicsLineItem *line = scene->addLine(0, y, pixelWidth, y);
- line->setVisible(ui->checkBox_ToggleGrid->isChecked());
- gridLines.append(line);
- connect(ui->checkBox_ToggleGrid, &QCheckBox::toggled, [=](bool checked){line->setVisible(checked);});
- }
- }
-
- void Editor::updateConnectionOffset(int offset) {
- if (!selected_connection_item)
- return;
-
- selected_connection_item->blockSignals(true);
- offset = qMin(offset, selected_connection_item->getMaxOffset());
- offset = qMax(offset, selected_connection_item->getMinOffset());
- selected_connection_item->connection->offset = QString::number(offset);
- if (selected_connection_item->connection->direction == "up" || selected_connection_item->connection->direction == "down") {
- selected_connection_item->setX(selected_connection_item->initialX + (offset - selected_connection_item->initialOffset) * 16);
- } else if (selected_connection_item->connection->direction == "left" || selected_connection_item->connection->direction == "right") {
- selected_connection_item->setY(selected_connection_item->initialY + (offset - selected_connection_item->initialOffset) * 16);
- }
- selected_connection_item->blockSignals(false);
- updateMirroredConnectionOffset(selected_connection_item->connection);
- }
-
- void Editor::setConnectionMap(QString mapName) {
- if (!mapName.isEmpty() && !project->mapNames->contains(mapName)) {
- qDebug() << "Invalid map name " << mapName << " specified for connection.";
- return;
- }
- if (!selected_connection_item)
- return;
-
- if (mapName.isEmpty()) {
- removeCurrentConnection();
- return;
- }
-
- QString originalMapName = selected_connection_item->connection->map_name;
- setConnectionEditControlsEnabled(true);
- selected_connection_item->connection->map_name = mapName;
- setCurrentConnectionDirection(selected_connection_item->connection->direction);
- updateMirroredConnectionMap(selected_connection_item->connection, originalMapName);
- }
-
- void Editor::addNewConnection() {
- // Find direction with least number of connections.
- QMap<QString, int> directionCounts = QMap<QString, int>({{"up", 0}, {"right", 0}, {"down", 0}, {"left", 0}});
- for (Connection* connection : map->connections) {
- directionCounts[connection->direction]++;
- }
- QString minDirection = "up";
- int minCount = INT_MAX;
- for (QString direction : directionCounts.keys()) {
- if (directionCounts[direction] < minCount) {
- minDirection = direction;
- minCount = directionCounts[direction];
- }
- }
-
- // Don't connect the map to itself.
- QString defaultMapName = project->mapNames->first();
- if (defaultMapName == map->name) {
- defaultMapName = project->mapNames->value(1);
- }
-
- Connection* newConnection = new Connection;
- newConnection->direction = minDirection;
- newConnection->offset = "0";
- newConnection->map_name = defaultMapName;
- map->connections.append(newConnection);
- createConnectionItem(newConnection, true);
- onConnectionItemSelected(connection_edit_items.last());
- ui->label_NumConnections->setText(QString::number(map->connections.length()));
-
- updateMirroredConnection(newConnection, newConnection->direction, newConnection->map_name);
- }
-
- void Editor::updateMirroredConnectionOffset(Connection* connection) {
- updateMirroredConnection(connection, connection->direction, connection->map_name);
- }
- void Editor::updateMirroredConnectionDirection(Connection* connection, QString originalDirection) {
- updateMirroredConnection(connection, originalDirection, connection->map_name);
- }
- void Editor::updateMirroredConnectionMap(Connection* connection, QString originalMapName) {
- updateMirroredConnection(connection, connection->direction, originalMapName);
- }
- void Editor::removeMirroredConnection(Connection* connection) {
- updateMirroredConnection(connection, connection->direction, connection->map_name, true);
- }
- void Editor::updateMirroredConnection(Connection* connection, QString originalDirection, QString originalMapName, bool isDelete) {
- if (!ui->checkBox_MirrorConnections->isChecked())
- return;
-
- static QMap<QString, QString> oppositeDirections = QMap<QString, QString>({
- {"up", "down"}, {"right", "left"},
- {"down", "up"}, {"left", "right"},
- {"dive", "emerge"},{"emerge", "dive"}});
- QString oppositeDirection = oppositeDirections.value(originalDirection);
-
- // Find the matching connection in the connected map.
- QMap<QString, Map*> *mapcache = project->map_cache;
- Connection* mirrorConnection = NULL;
- Map* otherMap = project->getMap(originalMapName);
- for (Connection* conn : otherMap->connections) {
- if (conn->direction == oppositeDirection && conn->map_name == map->name) {
- mirrorConnection = conn;
- }
- }
-
- if (isDelete) {
- if (mirrorConnection) {
- otherMap->connections.removeOne(mirrorConnection);
- delete mirrorConnection;
- }
- return;
- }
-
- if (connection->direction != originalDirection || connection->map_name != originalMapName) {
- if (mirrorConnection) {
- otherMap->connections.removeOne(mirrorConnection);
- delete mirrorConnection;
- mirrorConnection = NULL;
- otherMap = project->getMap(connection->map_name);
- }
- }
-
- // Create a new mirrored connection, if a matching one doesn't already exist.
- if (!mirrorConnection) {
- mirrorConnection = new Connection;
- mirrorConnection->direction = oppositeDirections.value(connection->direction);
- mirrorConnection->map_name = map->name;
- otherMap->connections.append(mirrorConnection);
- }
-
- mirrorConnection->offset = QString::number(-connection->offset.toInt());
- }
-
- void Editor::removeCurrentConnection() {
- if (!selected_connection_item)
- return;
-
- map->connections.removeOne(selected_connection_item->connection);
- connection_edit_items.removeOne(selected_connection_item);
- removeMirroredConnection(selected_connection_item->connection);
-
- if (selected_connection_item && selected_connection_item->scene()) {
- selected_connection_item->scene()->removeItem(selected_connection_item);
- delete selected_connection_item;
- }
-
- selected_connection_item = NULL;
- setConnectionEditControlsEnabled(false);
- ui->spinBox_ConnectionOffset->setValue(0);
- ui->label_NumConnections->setText(QString::number(map->connections.length()));
-
- if (connection_edit_items.length() > 0) {
- onConnectionItemSelected(connection_edit_items.last());
- }
- }
-
- void Editor::updateDiveMap(QString mapName) {
- updateDiveEmergeMap(mapName, "dive");
- }
-
- void Editor::updateEmergeMap(QString mapName) {
- updateDiveEmergeMap(mapName, "emerge");
- }
-
- void Editor::updateDiveEmergeMap(QString mapName, QString direction) {
- if (!mapName.isEmpty() && !project->mapNamesToMapConstants->contains(mapName)) {
- qDebug() << "Invalid " << direction << " map connection: " << mapName;
- return;
- }
-
- Connection* connection = NULL;
- for (Connection* conn : map->connections) {
- if (conn->direction == direction) {
- connection = conn;
- break;
- }
- }
-
- if (mapName.isEmpty()) {
- // Remove dive/emerge connection
- if (connection) {
- map->connections.removeOne(connection);
- removeMirroredConnection(connection);
- }
- } else {
- if (!connection) {
- connection = new Connection;
- connection->direction = direction;
- connection->offset = "0";
- connection->map_name = mapName;
- map->connections.append(connection);
- updateMirroredConnection(connection, connection->direction, connection->map_name);
- } else {
- QString originalMapName = connection->map_name;
- connection->map_name = mapName;
- updateMirroredConnectionMap(connection, originalMapName);
- }
- }
-
- ui->label_NumConnections->setText(QString::number(map->connections.length()));
- }
-
- void Editor::updatePrimaryTileset(QString tilesetLabel)
- {
- if (map->layout->tileset_primary_label != tilesetLabel)
- {
- map->layout->tileset_primary_label = tilesetLabel;
- map->layout->tileset_primary = project->getTileset(tilesetLabel);
- emit tilesetChanged(map->name);
- }
- }
-
- void Editor::updateSecondaryTileset(QString tilesetLabel)
- {
- if (map->layout->tileset_secondary_label != tilesetLabel)
- {
- map->layout->tileset_secondary_label = tilesetLabel;
- map->layout->tileset_secondary = project->getTileset(tilesetLabel);
- emit tilesetChanged(map->name);
- }
- }
-
- void MetatilesPixmapItem::paintTileChanged(Map *map) {
- draw();
- }
-
- void MetatilesPixmapItem::draw() {
- setPixmap(map->renderMetatiles());
- }
-
- void MetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
- int x = ((int)pos.x()) / 16;
- int y = ((int)pos.y()) / 16;
- int width = pixmap().width() / 16;
- int height = pixmap().height() / 16;
- if (x < 0 || x >= width || y < 0 || y >= height) {
- map->clearHoveredMetatile();
- } else {
- int block = y * width + x;
- map->hoveredMetatileChanged(block);
- }
- }
-
- void MetatilesPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
- updateCurHoveredMetatile(event->pos());
- }
- void MetatilesPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
- map->clearHoveredMetatile();
- }
- void MetatilesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
- QPointF pos = event->pos();
- int x = ((int)pos.x()) / 16;
- int y = ((int)pos.y()) / 16;
- map->paint_metatile_initial_x = x;
- map->paint_metatile_initial_y = y;
- updateSelection(event->pos(), event->button());
- }
- void MetatilesPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
- updateCurHoveredMetatile(event->pos());
- Qt::MouseButton button = event->button();
- if (button == Qt::MouseButton::NoButton) {
- Qt::MouseButtons heldButtons = event->buttons();
- if (heldButtons & Qt::RightButton) {
- button = Qt::RightButton;
- } else if (heldButtons & Qt::LeftButton) {
- button = Qt::LeftButton;
- }
- }
- updateSelection(event->pos(), button);
- }
- void MetatilesPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
- updateSelection(event->pos(), event->button());
- }
- void MetatilesPixmapItem::updateSelection(QPointF pos, Qt::MouseButton button) {
- int x = ((int)pos.x()) / 16;
- int y = ((int)pos.y()) / 16;
- int width = pixmap().width() / 16;
- int height = pixmap().height() / 16;
- if ((x >= 0 && x < width) && (y >=0 && y < height)) {
- int baseTileX = x < map->paint_metatile_initial_x ? x : map->paint_metatile_initial_x;
- int baseTileY = y < map->paint_metatile_initial_y ? y : map->paint_metatile_initial_y;
- map->paint_tile_index = baseTileY * 8 + baseTileX;
- map->paint_tile_width = abs(map->paint_metatile_initial_x - x) + 1;
- map->paint_tile_height = abs(map->paint_metatile_initial_y - y) + 1;
- emit map->paintTileChanged(map);
- }
- }
-
- void BorderMetatilesPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
- QPointF pos = event->pos();
- int x = ((int)pos.x()) / 16;
- int y = ((int)pos.y()) / 16;
-
- for (int i = 0; i < map->paint_tile_width && (i + x) < 2; i++) {
- for (int j = 0; j < map->paint_tile_height && (j + y) < 2; j++) {
- int blockIndex = (j + y) * 2 + (i + x);
- int tile = map->getSelectedBlockIndex(map->paint_tile_index + i + (j * 8));
- (*map->layout->border->blocks)[blockIndex].tile = tile;
- }
- }
-
- draw();
- emit borderMetatilesChanged();
- }
-
- void BorderMetatilesPixmapItem::draw() {
- QImage image(32, 32, QImage::Format_RGBA8888);
- QPainter painter(&image);
- QList<Block> *blocks = map->layout->border->blocks;
-
- for (int i = 0; i < 2; i++)
- for (int j = 0; j < 2; j++)
- {
- int x = i * 16;
- int y = j * 16;
- int index = j * 2 + i;
- QImage metatile_image = Metatile::getMetatileImage(blocks->value(index).tile, map->layout->tileset_primary, map->layout->tileset_secondary);
- QPoint metatile_origin = QPoint(x, y);
- painter.drawImage(metatile_origin, metatile_image);
- }
-
- painter.end();
- setPixmap(QPixmap::fromImage(image));
- }
-
- void MovementPermissionsPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {
- QPointF pos = event->pos();
- int x = ((int)pos.x()) / 16;
- int y = ((int)pos.y()) / 16;
- int width = pixmap().width() / 16;
- int height = pixmap().height() / 16;
- if ((x >= 0 && x < width) && (y >=0 && y < height)) {
- pick(y * width + x);
- }
- }
- void MovementPermissionsPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event) {
- updateCurHoveredMetatile(event->pos());
- mousePressEvent(event);
- }
- void MovementPermissionsPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) {
- mousePressEvent(event);
- }
-
- void CollisionMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
- int x = ((int)pos.x()) / 16;
- int y = ((int)pos.y()) / 16;
- int width = pixmap().width() / 16;
- int height = pixmap().height() / 16;
- if (x < 0 || x >= width || y < 0 || y >= height) {
- map->clearHoveredCollisionTile();
- } else {
- int collision = y * width + x;
- map->hoveredCollisionTileChanged(collision);
- }
- }
-
- int ConnectionPixmapItem::getMinOffset() {
- if (connection->direction == "up" || connection->direction == "down")
- return 1 - (this->pixmap().width() / 16);
- else
- return 1 - (this->pixmap().height() / 16);
- }
- int ConnectionPixmapItem::getMaxOffset() {
- if (connection->direction == "up" || connection->direction == "down")
- return baseMapWidth - 1;
- else
- return baseMapHeight - 1;
- }
- QVariant ConnectionPixmapItem::itemChange(GraphicsItemChange change, const QVariant &value)
- {
- if (change == ItemPositionChange) {
- QPointF newPos = value.toPointF();
-
- qreal x, y;
- int newOffset = initialOffset;
- if (connection->direction == "up" || connection->direction == "down") {
- x = round(newPos.x() / 16) * 16;
- newOffset += (x - initialX) / 16;
- newOffset = qMin(newOffset, this->getMaxOffset());
- newOffset = qMax(newOffset, this->getMinOffset());
- x = newOffset * 16;
- }
- else {
- x = initialX;
- }
-
- if (connection->direction == "right" || connection->direction == "left") {
- y = round(newPos.y() / 16) * 16;
- newOffset += (y - initialY) / 16;
- newOffset = qMin(newOffset, this->getMaxOffset());
- newOffset = qMax(newOffset, this->getMinOffset());
- y = newOffset * 16;
- }
- else {
- y = initialY;
- }
-
- connection->offset = QString::number(newOffset);
- emit connectionMoved(connection);
- return QPointF(x, y);
- }
- else {
- return QGraphicsItem::itemChange(change, value);
- }
- }
- void ConnectionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {
- emit connectionItemSelected(this);
- }
- void ConnectionPixmapItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent*) {
- emit connectionItemDoubleClicked(this);
- }
-
- void ElevationMetatilesPixmapItem::updateCurHoveredMetatile(QPointF pos) {
- int x = ((int)pos.x()) / 16;
- int y = ((int)pos.y()) / 16;
- int width = pixmap().width() / 16;
- int height = pixmap().height() / 16;
- if (x < 0 || x >= width || y < 0 || y >= height) {
- map->clearHoveredElevationTile();
- } else {
- int elevation = y * width + x;
- map->hoveredElevationTileChanged(elevation);
- }
- }
-
- void MapPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
- if (map) {
- QPointF pos = event->pos();
- int x = (int)(pos.x()) / 16;
- int y = (int)(pos.y()) / 16;
-
- if (map->smart_paths_enabled && map->paint_tile_width == 3 && map->paint_tile_height == 3) {
- paintSmartPath(x, y);
- } else {
- paintNormal(x, y);
- }
-
- if (event->type() == QEvent::GraphicsSceneMouseRelease) {
- map->commit();
- }
- draw();
- }
- }
-
- void MapPixmapItem::paintNormal(int x, int y) {
- // Snap the selected position to the top-left of the block boundary.
- // This allows painting via dragging the mouse to tile the painted region.
- int xDiff = x - map->paint_tile_initial_x;
- int yDiff = y - map->paint_tile_initial_y;
- if (xDiff < 0 && xDiff % map->paint_tile_width != 0) xDiff -= map->paint_tile_width;
- if (yDiff < 0 && yDiff % map->paint_tile_height != 0) yDiff -= map->paint_tile_height;
-
- x = map->paint_tile_initial_x + (xDiff / map->paint_tile_width) * map->paint_tile_width;
- y = map->paint_tile_initial_y + (yDiff / map->paint_tile_height) * map->paint_tile_height;
- for (int i = 0; i < map->paint_tile_width && i + x < map->getWidth(); i++)
- for (int j = 0; j < map->paint_tile_height && j + y < map->getHeight(); j++) {
- int actualX = i + x;
- int actualY = j + y;
- Block *block = map->getBlock(actualX, actualY);
- if (block) {
- block->tile = map->getSelectedBlockIndex(map->paint_tile_index + i + (j * 8));
- map->_setBlock(actualX, actualY, *block);
- }
- }
- }
-
- // These are tile offsets from the top-left tile in the 3x3 smart path selection.
- // Each entry is for one possibility from the marching squares value for a tile.
- // (Marching Squares: https://en.wikipedia.org/wiki/Marching_squares)
- QList<int> MapPixmapItem::smartPathTable = QList<int>({
- 8 + 1, // 0000
- 8 + 1, // 0001
- 8 + 1, // 0010
- 16 + 0, // 0011
- 8 + 1, // 0100
- 8 + 1, // 0101
- 0 + 0, // 0110
- 8 + 0, // 0111
- 8 + 1, // 1000
- 16 + 2, // 1001
- 8 + 1, // 1010
- 16 + 1, // 1011
- 0 + 2, // 1100
- 8 + 2, // 1101
- 0 + 1, // 1110
- 8 + 1, // 1111
- });
-
- #define IS_SMART_PATH_TILE(block) ((map->getDisplayedBlockIndex(block->tile) >= map->paint_tile_index && map->getDisplayedBlockIndex(block->tile) < map->paint_tile_index + 3) \
- || (map->getDisplayedBlockIndex(block->tile) >= map->paint_tile_index + 8 && map->getDisplayedBlockIndex(block->tile) < map->paint_tile_index + 11) \
- || (map->getDisplayedBlockIndex(block->tile) >= map->paint_tile_index + 16 && map->getDisplayedBlockIndex(block->tile) < map->paint_tile_index + 19))
-
- void MapPixmapItem::paintSmartPath(int x, int y) {
- // Smart path should never be enabled without a 3x3 block selection.
- if (map->paint_tile_width != 3 || map->paint_tile_height != 3) return;
-
- // Shift to the middle tile of the smart path selection.
- int openTile = map->paint_tile_index + 8 + 1;
-
- // Fill the region with the open tile.
- for (int i = -1; i <= 1; i++)
- for (int j = -1; j <= 1; j++) {
- // Check if in map bounds.
- if (!(i + x < map->getWidth() && i + x >= 0 && j + y < map->getHeight() && j + y >= 0))
- continue;
- int actualX = i + x;
- int actualY = j + y;
- Block *block = map->getBlock(actualX, actualY);
- if (block) {
- block->tile = map->getSelectedBlockIndex(openTile);
- map->_setBlock(actualX, actualY, *block);
- }
- }
-
- // Go back and resolve the edge tiles
- for (int i = -2; i <= 2; i++)
- for (int j = -2; j <= 2; j++) {
- // Check if in map bounds.
- if (!(i + x < map->getWidth() && i + x >= 0 && j + y < map->getHeight() && j + y >= 0))
- continue;
- // Ignore the corners, which can't possible be affected by the smart path.
- if ((i == -2 && j == -2) || (i == 2 && j == -2) ||
- (i == -2 && j == 2) || (i == 2 && j == 2))
- continue;
-
- // Ignore tiles that aren't part of the smart path set.
- int actualX = i + x;
- int actualY = j + y;
- Block *block = map->getBlock(actualX, actualY);
- if (!block || !IS_SMART_PATH_TILE(block)) {
- continue;
- }
-
- int id = 0;
- Block *top = map->getBlock(actualX, actualY - 1);
- Block *right = map->getBlock(actualX + 1, actualY);
- Block *bottom = map->getBlock(actualX, actualY + 1);
- Block *left = map->getBlock(actualX - 1, actualY);
-
- // Get marching squares value, to determine which tile to use.
- if (top && IS_SMART_PATH_TILE(top))
- id += 1;
- if (right && IS_SMART_PATH_TILE(right))
- id += 2;
- if (bottom && IS_SMART_PATH_TILE(bottom))
- id += 4;
- if (left && IS_SMART_PATH_TILE(left))
- id += 8;
-
- block->tile = map->getSelectedBlockIndex(map->paint_tile_index + smartPathTable[id]);
- map->_setBlock(actualX, actualY, *block);
- }
- }
-
- void MapPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
- if (map) {
- QPointF pos = event->pos();
- int x = (int)(pos.x()) / 16;
- int y = (int)(pos.y()) / 16;
- Block *block = map->getBlock(x, y);
- int tile = map->getSelectedBlockIndex(map->paint_tile_index);
- if (block && block->tile != tile) {
- if (map->smart_paths_enabled && map->paint_tile_width == 3 && map->paint_tile_height == 3)
- this->_floodFillSmartPath(x, y);
- else
- this->_floodFill(x, y);
- }
-
- if (event->type() == QEvent::GraphicsSceneMouseRelease) {
- map->commit();
- }
- draw();
- }
- }
-
- void MapPixmapItem::_floodFill(int initialX, int initialY) {
- QList<QPoint> todo;
- todo.append(QPoint(initialX, initialY));
- while (todo.length()) {
- QPoint point = todo.takeAt(0);
- int x = point.x();
- int y = point.y();
-
- Block *block = map->getBlock(x, y);
- if (block == NULL) {
- continue;
- }
-
- int xDiff = x - initialX;
- int yDiff = y - initialY;
- int i = xDiff % map->paint_tile_width;
- int j = yDiff % map->paint_tile_height;
- if (i < 0) i = map->paint_tile_width + i;
- if (j < 0) j = map->paint_tile_height + j;
- int tile = map->getSelectedBlockIndex(map->paint_tile_index + i + (j * 8));
- uint old_tile = block->tile;
- if (old_tile == tile) {
- continue;
- }
-
- block->tile = tile;
- map->_setBlock(x, y, *block);
- if ((block = map->getBlock(x + 1, y)) && block->tile == old_tile) {
- todo.append(QPoint(x + 1, y));
- }
- if ((block = map->getBlock(x - 1, y)) && block->tile == old_tile) {
- todo.append(QPoint(x - 1, y));
- }
- if ((block = map->getBlock(x, y + 1)) && block->tile == old_tile) {
- todo.append(QPoint(x, y + 1));
- }
- if ((block = map->getBlock(x, y - 1)) && block->tile == old_tile) {
- todo.append(QPoint(x, y - 1));
- }
- }
- }
-
- void MapPixmapItem::_floodFillSmartPath(int initialX, int initialY) {
- // Smart path should never be enabled without a 3x3 block selection.
- if (map->paint_tile_width != 3 || map->paint_tile_height != 3) return;
-
- // Shift to the middle tile of the smart path selection.
- int openTile = map->paint_tile_index + 8 + 1;
-
- // Flood fill the region with the open tile.
- QList<QPoint> todo;
- todo.append(QPoint(initialX, initialY));
- while (todo.length()) {
- QPoint point = todo.takeAt(0);
- int x = point.x();
- int y = point.y();
-
- Block *block = map->getBlock(x, y);
- if (block == NULL) {
- continue;
- }
-
- int tile = map->getSelectedBlockIndex(openTile);
- uint old_tile = block->tile;
- if (old_tile == tile) {
- continue;
- }
-
- block->tile = tile;
- map->_setBlock(x, y, *block);
- if ((block = map->getBlock(x + 1, y)) && block->tile == old_tile) {
- todo.append(QPoint(x + 1, y));
- }
- if ((block = map->getBlock(x - 1, y)) && block->tile == old_tile) {
- todo.append(QPoint(x - 1, y));
- }
- if ((block = map->getBlock(x, y + 1)) && block->tile == old_tile) {
- todo.append(QPoint(x, y + 1));
- }
- if ((block = map->getBlock(x, y - 1)) && block->tile == old_tile) {
- todo.append(QPoint(x, y - 1));
- }
- }
-
- // Go back and resolve the flood-filled edge tiles.
- // Mark tiles as visited while we go.
- bool visited[map->getWidth() * map->getHeight()];
- for (int i = 0; i < sizeof visited; i++)
- visited[i] = false;
-
- todo.append(QPoint(initialX, initialY));
- while (todo.length()) {
- QPoint point = todo.takeAt(0);
- int x = point.x();
- int y = point.y();
- visited[x + y * map->getWidth()] = true;
-
- Block *block = map->getBlock(x, y);
- if (block == NULL) {
- continue;
- }
-
- int id = 0;
- Block *top = map->getBlock(x, y - 1);
- Block *right = map->getBlock(x + 1, y);
- Block *bottom = map->getBlock(x, y + 1);
- Block *left = map->getBlock(x - 1, y);
-
- // Get marching squares value, to determine which tile to use.
- if (top && IS_SMART_PATH_TILE(top))
- id += 1;
- if (right && IS_SMART_PATH_TILE(right))
- id += 2;
- if (bottom && IS_SMART_PATH_TILE(bottom))
- id += 4;
- if (left && IS_SMART_PATH_TILE(left))
- id += 8;
-
- block->tile = map->getSelectedBlockIndex(map->paint_tile_index + smartPathTable[id]);
- map->_setBlock(x, y, *block);
-
- // Visit neighbors if they are smart-path tiles, and don't revisit any.
- if (!visited[x + 1 + y * map->getWidth()] && (block = map->getBlock(x + 1, y)) && IS_SMART_PATH_TILE(block)) {
- todo.append(QPoint(x + 1, y));
- visited[x + 1 + y * map->getWidth()] = true;
- }
- if (!visited[x - 1 + y * map->getWidth()] && (block = map->getBlock(x - 1, y)) && IS_SMART_PATH_TILE(block)) {
- todo.append(QPoint(x - 1, y));
- visited[x - 1 + y * map->getWidth()] = true;
- }
- if (!visited[x + (y + 1) * map->getWidth()] && (block = map->getBlock(x, y + 1)) && IS_SMART_PATH_TILE(block)) {
- todo.append(QPoint(x, y + 1));
- visited[x + (y + 1) * map->getWidth()] = true;
- }
- if (!visited[x + (y - 1) * map->getWidth()] && (block = map->getBlock(x, y - 1)) && IS_SMART_PATH_TILE(block)) {
- todo.append(QPoint(x, y - 1));
- visited[x + (y - 1) * map->getWidth()] = true;
- }
- }
- }
-
- void MapPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
- QPointF pos = event->pos();
- int x = (int)(pos.x()) / 16;
- int y = (int)(pos.y()) / 16;
- Block *block = map->getBlock(x, y);
- if (block) {
- map->paint_tile_index = map->getDisplayedBlockIndex(block->tile);
- map->paint_tile_width = 1;
- map->paint_tile_height = 1;
- emit map->paintTileChanged(map);
- }
- }
-
- #define SWAP(a, b) do { if (a != b) { a ^= b; b ^= a; a ^= b; } } while (0)
-
- void MapPixmapItem::select(QGraphicsSceneMouseEvent *event) {
- QPointF pos = event->pos();
- int x = (int)(pos.x()) / 16;
- int y = (int)(pos.y()) / 16;
- if (event->type() == QEvent::GraphicsSceneMousePress) {
- selection_origin = QPoint(x, y);
- selection.clear();
- } else if (event->type() == QEvent::GraphicsSceneMouseMove) {
- if (event->buttons() & Qt::LeftButton) {
- selection.clear();
- selection.append(QPoint(x, y));
- }
- } else if (event->type() == QEvent::GraphicsSceneMouseRelease) {
- if (!selection.isEmpty()) {
- QPoint pos = selection.last();
- int x1 = selection_origin.x();
- int y1 = selection_origin.y();
- int x2 = pos.x();
- int y2 = pos.y();
- if (x1 > x2) SWAP(x1, x2);
- if (y1 > y2) SWAP(y1, y2);
- selection.clear();
- for (int y = y1; y <= y2; y++) {
- for (int x = x1; x <= x2; x++) {
- selection.append(QPoint(x, y));
- }
- }
- qDebug() << QString("selected (%1, %2) -> (%3, %4)").arg(x1).arg(y1).arg(x2).arg(y2);
- }
- }
- }
-
- void MapPixmapItem::draw(bool ignoreCache) {
- if (map) {
- setPixmap(map->render(ignoreCache));
- }
- }
-
- void MapPixmapItem::updateCurHoveredTile(QPointF pos) {
- int x = ((int)pos.x()) / 16;
- int y = ((int)pos.y()) / 16;
- int blockIndex = y * map->getWidth() + x;
- if (x < 0 || x >= map->getWidth() || y < 0 || y >= map->getHeight()) {
- map->clearHoveredTile();
- } else {
- int tile = map->layout->blockdata->blocks->at(blockIndex).tile;
- map->hoveredTileChanged(x, y, tile);
- }
- }
-
- void MapPixmapItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
- updateCurHoveredTile(event->pos());
- }
- void MapPixmapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
- map->clearHoveredTile();
- }
- void MapPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
- QPointF pos = event->pos();
- int x = ((int)pos.x()) / 16;
- int y = ((int)pos.y()) / 16;
- map->paint_tile_initial_x = x;
- map->paint_tile_initial_y = y;
- emit mouseEvent(event, this);
- }
- void MapPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
- updateCurHoveredTile(event->pos());
- emit mouseEvent(event, this);
- }
- void MapPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
- emit mouseEvent(event, this);
- }
-
- void CollisionPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
- emit mouseEvent(event, this);
- }
- void CollisionPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
- emit mouseEvent(event, this);
- }
- void CollisionPixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
- emit mouseEvent(event, this);
- }
-
- void CollisionPixmapItem::draw(bool ignoreCache) {
- if (map) {
- setPixmap(map->renderCollision(ignoreCache));
- }
- }
-
- void CollisionPixmapItem::paint(QGraphicsSceneMouseEvent *event) {
- if (map) {
- QPointF pos = event->pos();
- int x = (int)(pos.x()) / 16;
- int y = (int)(pos.y()) / 16;
- Block *block = map->getBlock(x, y);
- if (block) {
- if (map->paint_collision >= 0) {
- block->collision = map->paint_collision;
- }
- if (map->paint_elevation >= 0) {
- block->elevation = map->paint_elevation;
- }
- map->_setBlock(x, y, *block);
- }
- if (event->type() == QEvent::GraphicsSceneMouseRelease) {
- map->commit();
- }
- draw();
- }
- }
-
- void CollisionPixmapItem::floodFill(QGraphicsSceneMouseEvent *event) {
- if (map) {
- QPointF pos = event->pos();
- int x = (int)(pos.x()) / 16;
- int y = (int)(pos.y()) / 16;
- bool collision = map->paint_collision >= 0;
- bool elevation = map->paint_elevation >= 0;
- if (collision && elevation) {
- map->floodFillCollisionElevation(x, y, map->paint_collision, map->paint_elevation);
- } else if (collision) {
- map->floodFillCollision(x, y, map->paint_collision);
- } else if (elevation) {
- map->floodFillElevation(x, y, map->paint_elevation);
- }
- draw();
- }
- }
-
- void CollisionPixmapItem::pick(QGraphicsSceneMouseEvent *event) {
- QPointF pos = event->pos();
- int x = (int)(pos.x()) / 16;
- int y = (int)(pos.y()) / 16;
- Block *block = map->getBlock(x, y);
- if (block) {
- map->paint_collision = block->collision;
- map->paint_elevation = block->elevation;
- emit map->paintCollisionChanged(map);
- }
- }
-
- void DraggablePixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *mouse) {
- active = true;
- last_x = (mouse->pos().x() + this->pos().x()) / 16;
- last_y = (mouse->pos().y() + this->pos().y()) / 16;
- this->editor->selectMapEvent(this, mouse->modifiers() & Qt::ControlModifier);
- this->editor->updateSelectedEvents();
- selectingEvent = true;
- //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("y"));
- }
-
- void DraggablePixmapItem::move(int x, int y) {
- event->setX(event->x() + x);
- event->setY(event->y() + y);
- updatePosition();
- emitPositionChanged();
- }
-
- void DraggablePixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent *mouse) {
- if (active) {
- int x = (mouse->pos().x() + this->pos().x()) / 16;
- int y = (mouse->pos().y() + this->pos().y()) / 16;
- if (x != last_x || y != last_y) {
- if (editor->selected_events->contains(this)) {
- for (DraggablePixmapItem *item : *editor->selected_events) {
- item->move(x - last_x, y - last_y);
- }
- } else {
- move(x - last_x, y - last_y);
- }
- last_x = x;
- last_y = y;
- //qDebug() << QString("(%1, %2)").arg(event->get("x")).arg(event->get("x"));
- }
- }
- }
-
- void DraggablePixmapItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouse) {
- active = false;
- }
-
- void DraggablePixmapItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouse) {
- if (this->event->get("event_type") == EventType::Warp) {
- emit editor->warpEventDoubleClicked(this->event->get("destination_map_name"), this->event->get("destination_warp"));
- }
- }
-
- QList<DraggablePixmapItem *> *Editor::getObjects() {
- QList<DraggablePixmapItem *> *list = new QList<DraggablePixmapItem *>;
- for (Event *event : map->getAllEvents()) {
- for (QGraphicsItem *child : events_group->childItems()) {
- DraggablePixmapItem *item = (DraggablePixmapItem *)child;
- if (item->event == event) {
- list->append(item);
- break;
- }
- }
- }
- return list;
- }
-
- void Editor::redrawObject(DraggablePixmapItem *item) {
- if (item) {
- item->setPixmap(item->event->pixmap);
- if (selected_events && selected_events->contains(item)) {
- QImage image = item->pixmap().toImage();
- QPainter painter(&image);
- painter.setPen(QColor(250, 0, 255));
- painter.drawRect(0, 0, image.width() - 1, image.height() - 1);
- painter.end();
- item->setPixmap(QPixmap::fromImage(image));
- }
- }
- }
-
- void Editor::updateSelectedEvents() {
- for (DraggablePixmapItem *item : *(getObjects())) {
- redrawObject(item);
- }
- emit selectedObjectsChanged();
- }
-
- void Editor::selectMapEvent(DraggablePixmapItem *object) {
- selectMapEvent(object, false);
- }
-
- void Editor::selectMapEvent(DraggablePixmapItem *object, bool toggle) {
- if (selected_events && object) {
- if (selected_events->contains(object)) {
- if (toggle) {
- selected_events->removeOne(object);
- }
- } else {
- if (!toggle) {
- selected_events->clear();
- }
- selected_events->append(object);
- }
- updateSelectedEvents();
- }
- }
-
- DraggablePixmapItem* Editor::addNewEvent(QString event_type) {
- if (project && map) {
- Event *event = Event::createNewEvent(event_type, map->name);
- event->put("map_name", map->name);
- map->addEvent(event);
- project->loadEventPixmaps(map->getAllEvents());
- DraggablePixmapItem *object = addMapEvent(event);
-
- return object;
- }
- return NULL;
- }
-
- void Editor::deleteEvent(Event *event) {
- Map *map = project->getMap(event->get("map_name"));
- if (map) {
- map->removeEvent(event);
- }
- //selected_events->removeAll(event);
- //updateSelectedObjects();
- }
-
- // It doesn't seem to be possible to prevent the mousePress event
- // from triggering both event's DraggablePixmapItem and the background mousePress.
- // Since the DraggablePixmapItem's event fires first, we can set a temp
- // variable "selectingEvent" so that we can detect whether or not the user
- // is clicking on the background instead of an event.
- void Editor::objectsView_onMousePress(QMouseEvent *event) {
- bool multiSelect = event->modifiers() & Qt::ControlModifier;
- if (!selectingEvent && !multiSelect && selected_events->length() > 1) {
- DraggablePixmapItem *first = selected_events->first();
- selected_events->clear();
- selected_events->append(first);
- updateSelectedEvents();
- }
-
- selectingEvent = false;
- }
|