mirror of
https://github.com/huderlem/porymap.git
synced 2026-09-26 01:35:38 -05:00
Fix more minor map connection issues
This commit is contained in:
@@ -9,36 +9,32 @@ const QMap<QString, QString> MapConnection::oppositeDirections = {
|
||||
{"dive", "emerge"}, {"emerge", "dive"}
|
||||
};
|
||||
|
||||
MapConnection::MapConnection(const QString &direction, const QString &hostMapName, const QString &targetMapName, int offset) {
|
||||
m_direction = direction;
|
||||
m_hostMapName = hostMapName;
|
||||
MapConnection::MapConnection(const QString &targetMapName, const QString &direction, int offset) {
|
||||
m_parentMap = nullptr;
|
||||
m_targetMapName = targetMapName;
|
||||
m_direction = direction;
|
||||
m_offset = offset;
|
||||
m_ignoreMirror = false;
|
||||
}
|
||||
|
||||
bool MapConnection::isMirror(const MapConnection* other) {
|
||||
if (!other)
|
||||
if (!other || !other->m_parentMap)
|
||||
return false;
|
||||
|
||||
return m_hostMapName == other->m_targetMapName
|
||||
&& m_targetMapName == other->m_hostMapName
|
||||
return parentMapName() == other->m_targetMapName
|
||||
&& m_targetMapName == other->parentMapName()
|
||||
&& m_offset == -other->m_offset
|
||||
&& m_direction == oppositeDirection(other->m_direction);
|
||||
}
|
||||
|
||||
MapConnection* MapConnection::findMirror() {
|
||||
if (!porymapConfig.mirrorConnectingMaps || !project || m_ignoreMirror)
|
||||
return nullptr;
|
||||
|
||||
Map *connectedMap = project->getMap(m_targetMapName);
|
||||
if (!connectedMap)
|
||||
auto map = targetMap();
|
||||
if (!map)
|
||||
return nullptr;
|
||||
|
||||
// Find the matching connection in the connected map.
|
||||
// Note: There is no strict source -> mirror pairing, i.e. we are not guaranteed
|
||||
// to always get the same MapConnection if there are multiple identical copies.
|
||||
for (auto connection : connectedMap->connections) {
|
||||
for (auto connection : map->getConnections()) {
|
||||
if (this != connection && this->isMirror(connection))
|
||||
return connection;
|
||||
}
|
||||
@@ -46,122 +42,114 @@ MapConnection* MapConnection::findMirror() {
|
||||
}
|
||||
|
||||
MapConnection * MapConnection::createMirror() {
|
||||
if (!porymapConfig.mirrorConnectingMaps)
|
||||
return nullptr;
|
||||
return new MapConnection(oppositeDirection(m_direction), m_targetMapName, m_hostMapName, -m_offset);
|
||||
auto mirror = new MapConnection(parentMapName(), oppositeDirection(m_direction), -m_offset);
|
||||
mirror->setParentMap(targetMap(), false);
|
||||
return mirror;
|
||||
}
|
||||
|
||||
void MapConnection::markMapEdited() {
|
||||
if (project) {
|
||||
Map * map = project->getMap(m_hostMapName);
|
||||
if (map) map->modify();
|
||||
}
|
||||
if (m_parentMap)
|
||||
m_parentMap->modify();
|
||||
}
|
||||
|
||||
Map* MapConnection::getMap(const QString& mapName) const {
|
||||
return project ? project->getMap(mapName) : nullptr;
|
||||
}
|
||||
|
||||
Map* MapConnection::targetMap() const {
|
||||
return getMap(m_targetMapName);
|
||||
}
|
||||
|
||||
QPixmap MapConnection::getPixmap() {
|
||||
if (!project)
|
||||
auto map = targetMap();
|
||||
if (!map)
|
||||
return QPixmap();
|
||||
|
||||
Map *hostMap = project->getMap(m_hostMapName);
|
||||
Map *targetMap = project->getMap(m_targetMapName);
|
||||
if (!hostMap || !targetMap)
|
||||
return QPixmap();
|
||||
|
||||
return targetMap->renderConnection(m_direction, hostMap->layout);
|
||||
return map->renderConnection(m_direction, m_parentMap ? m_parentMap->layout : nullptr);
|
||||
}
|
||||
|
||||
void MapConnection::setDirection(const QString &direction) {
|
||||
if (direction == m_direction)
|
||||
void MapConnection::setParentMap(Map* map, bool mirror) {
|
||||
if (map == m_parentMap)
|
||||
return;
|
||||
|
||||
mirrorDirection(direction);
|
||||
auto before = m_direction;
|
||||
m_direction = direction;
|
||||
emit directionChanged(before, m_direction);
|
||||
markMapEdited();
|
||||
}
|
||||
|
||||
void MapConnection::mirrorDirection(const QString &direction) {
|
||||
MapConnection * mirror = findMirror();
|
||||
if (!mirror)
|
||||
return;
|
||||
mirror->m_ignoreMirror = true;
|
||||
mirror->setDirection(oppositeDirection(direction));
|
||||
mirror->m_ignoreMirror = false;
|
||||
}
|
||||
|
||||
void MapConnection::setHostMapName(const QString &hostMapName) {
|
||||
if (hostMapName == m_hostMapName)
|
||||
return;
|
||||
|
||||
mirrorHostMapName(hostMapName);
|
||||
auto before = m_hostMapName;
|
||||
m_hostMapName = hostMapName;
|
||||
|
||||
if (project) {
|
||||
// TODO: This is probably unexpected design, esp because creating a MapConnection doesn't insert to host map
|
||||
// Reassign connection to new host map
|
||||
Map * oldMap = project->getMap(before);
|
||||
Map * newMap = project->getMap(m_hostMapName);
|
||||
if (oldMap) oldMap->removeConnection(this);
|
||||
if (newMap) newMap->addConnection(this);
|
||||
if (mirror && porymapConfig.mirrorConnectingMaps) {
|
||||
auto connection = findMirror();
|
||||
if (connection)
|
||||
connection->setTargetMapName(map ? map->name : QString(), false);
|
||||
}
|
||||
emit hostMapNameChanged(before, m_hostMapName);
|
||||
|
||||
if (m_parentMap)
|
||||
m_parentMap->removeConnection(this);
|
||||
|
||||
auto before = m_parentMap;
|
||||
m_parentMap = map;
|
||||
|
||||
if (m_parentMap)
|
||||
m_parentMap->addConnection(this);
|
||||
|
||||
emit parentMapChanged(before, m_parentMap);
|
||||
}
|
||||
|
||||
void MapConnection::mirrorHostMapName(const QString &hostMapName) {
|
||||
MapConnection * mirror = findMirror();
|
||||
if (!mirror)
|
||||
return;
|
||||
mirror->m_ignoreMirror = true;
|
||||
mirror->setTargetMapName(hostMapName);
|
||||
mirror->m_ignoreMirror = false;
|
||||
QString MapConnection::parentMapName() const {
|
||||
return m_parentMap ? m_parentMap->name : QString();
|
||||
}
|
||||
|
||||
void MapConnection::setTargetMapName(const QString &targetMapName) {
|
||||
void MapConnection::setTargetMapName(const QString &targetMapName, bool mirror) {
|
||||
if (targetMapName == m_targetMapName)
|
||||
return;
|
||||
|
||||
mirrorTargetMapName(targetMapName);
|
||||
if (mirror && porymapConfig.mirrorConnectingMaps) {
|
||||
auto connection = findMirror();
|
||||
if (connection)
|
||||
connection->setParentMap(getMap(targetMapName), false);
|
||||
}
|
||||
|
||||
auto before = m_targetMapName;
|
||||
m_targetMapName = targetMapName;
|
||||
emit targetMapNameChanged(before, m_targetMapName);
|
||||
markMapEdited();
|
||||
}
|
||||
|
||||
void MapConnection::mirrorTargetMapName(const QString &targetMapName) {
|
||||
MapConnection * mirror = findMirror();
|
||||
if (!mirror)
|
||||
void MapConnection::setDirection(const QString &direction, bool mirror) {
|
||||
if (direction == m_direction)
|
||||
return;
|
||||
mirror->m_ignoreMirror = true;
|
||||
mirror->setHostMapName(targetMapName);
|
||||
mirror->m_ignoreMirror = false;
|
||||
|
||||
if (mirror && porymapConfig.mirrorConnectingMaps) {
|
||||
auto connection = findMirror();
|
||||
if (connection)
|
||||
connection->setDirection(oppositeDirection(direction), false);
|
||||
}
|
||||
|
||||
auto before = m_direction;
|
||||
m_direction = direction;
|
||||
emit directionChanged(before, m_direction);
|
||||
markMapEdited();
|
||||
}
|
||||
|
||||
void MapConnection::setOffset(int offset) {
|
||||
void MapConnection::setOffset(int offset, bool mirror) {
|
||||
if (offset == m_offset)
|
||||
return;
|
||||
|
||||
mirrorOffset(offset);
|
||||
if (mirror && porymapConfig.mirrorConnectingMaps) {
|
||||
auto connection = findMirror();
|
||||
if (connection)
|
||||
connection->setOffset(-offset, false);
|
||||
}
|
||||
|
||||
auto before = m_offset;
|
||||
m_offset = offset;
|
||||
emit offsetChanged(before, m_offset);
|
||||
markMapEdited();
|
||||
}
|
||||
|
||||
void MapConnection::mirrorOffset(int offset) {
|
||||
MapConnection * mirror = findMirror();
|
||||
if (!mirror)
|
||||
return;
|
||||
mirror->m_ignoreMirror = true;
|
||||
mirror->setOffset(-offset);
|
||||
mirror->m_ignoreMirror = false;
|
||||
}
|
||||
|
||||
const QStringList MapConnection::cardinalDirections = {
|
||||
"up", "down", "left", "right"
|
||||
};
|
||||
|
||||
bool MapConnection::isCardinal(const QString &direction) {
|
||||
return cardinalDirections.contains(direction);
|
||||
}
|
||||
|
||||
bool MapConnection::isHorizontal(const QString &direction) {
|
||||
return direction == "left" || direction == "right";
|
||||
}
|
||||
@@ -170,6 +158,6 @@ bool MapConnection::isVertical(const QString &direction) {
|
||||
return direction == "up" || direction == "down";
|
||||
}
|
||||
|
||||
bool MapConnection::isCardinal(const QString &direction) {
|
||||
return cardinalDirections.contains(direction);
|
||||
bool MapConnection::isDiving(const QString &direction) {
|
||||
return direction == "dive" || direction == "emerge";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user