Support adding/removing connections via buttons
This commit is contained in:
parent
9a6132314c
commit
5301b299e2
5 changed files with 203 additions and 44 deletions
70
editor.cpp
70
editor.cpp
|
@ -97,6 +97,7 @@ void Editor::setEditingConnections() {
|
|||
ui->comboBox_ConnectedMap->clear();
|
||||
ui->comboBox_ConnectedMap->addItems(*project->mapNames);
|
||||
ui->comboBox_ConnectedMap->blockSignals(false);
|
||||
ui->label_NumConnections->setText(QString::number(map->connections.length()));
|
||||
setConnectionsVisibility(false);
|
||||
if (current_connection_edit_item) {
|
||||
onConnectionOffsetChanged(current_connection_edit_item->connection->offset.toInt());
|
||||
|
@ -195,6 +196,9 @@ void Editor::setConnectionEditControlsEnabled(bool enabled) {
|
|||
}
|
||||
|
||||
void Editor::onConnectionItemSelected(ConnectionPixmapItem* connectionItem) {
|
||||
if (!connectionItem)
|
||||
return;
|
||||
|
||||
for (ConnectionPixmapItem* item : connection_edit_items) {
|
||||
bool isSelectedItem = item == connectionItem;
|
||||
int zValue = isSelectedItem ? 0 : -1;
|
||||
|
@ -372,6 +376,15 @@ void Editor::displayMapConnections() {
|
|||
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);
|
||||
|
@ -396,6 +409,7 @@ void Editor::displayMapConnections() {
|
|||
item->setY(y);
|
||||
scene->addItem(item);
|
||||
map->connection_items.append(item);
|
||||
item->setVisible(!hide);
|
||||
|
||||
ConnectionPixmapItem *connection_edit_item = new ConnectionPixmapItem(pixmap, connection, x, y);
|
||||
connection_edit_item->setX(x);
|
||||
|
@ -405,11 +419,6 @@ void Editor::displayMapConnections() {
|
|||
connect(connection_edit_item, SIGNAL(connectionMoved(int)), this, SLOT(onConnectionOffsetChanged(int)));
|
||||
connect(connection_edit_item, SIGNAL(connectionItemSelected(ConnectionPixmapItem*)), this, SLOT(onConnectionItemSelected(ConnectionPixmapItem*)));
|
||||
connection_edit_items.append(connection_edit_item);
|
||||
}
|
||||
|
||||
if (!connection_edit_items.empty()) {
|
||||
onConnectionItemSelected(connection_edit_items.first());
|
||||
}
|
||||
}
|
||||
|
||||
void Editor::displayMapBorder() {
|
||||
|
@ -465,13 +474,7 @@ void Editor::updateConnectionMap(QString mapName, QString direction) {
|
|||
return;
|
||||
|
||||
if (mapName.isEmpty()) {
|
||||
map->connections.removeOne(current_connection_edit_item->connection);
|
||||
connection_edit_items.removeOne(current_connection_edit_item);
|
||||
scene->removeItem(current_connection_edit_item);
|
||||
delete current_connection_edit_item;
|
||||
current_connection_edit_item = NULL;
|
||||
setConnectionEditControlsEnabled(false);
|
||||
ui->spinBox_ConnectionOffset->setValue(0);
|
||||
removeCurrentConnection();
|
||||
return;
|
||||
} else {
|
||||
setConnectionEditControlsEnabled(true);
|
||||
|
@ -481,6 +484,49 @@ void Editor::updateConnectionMap(QString mapName, QString direction) {
|
|||
setCurrentConnectionDirection(direction);
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
Connection* newConnection = new Connection;
|
||||
newConnection->direction = minDirection;
|
||||
newConnection->offset = "0";
|
||||
newConnection->map_name = project->mapNames->first();
|
||||
map->connections.append(newConnection);
|
||||
createConnectionItem(newConnection, true);
|
||||
onConnectionItemSelected(connection_edit_items.last());
|
||||
ui->label_NumConnections->setText(QString::number(map->connections.length()));
|
||||
}
|
||||
|
||||
void Editor::removeCurrentConnection() {
|
||||
if (!current_connection_edit_item)
|
||||
return;
|
||||
|
||||
map->connections.removeOne(current_connection_edit_item->connection);
|
||||
connection_edit_items.removeOne(current_connection_edit_item);
|
||||
scene->removeItem(current_connection_edit_item);
|
||||
delete current_connection_edit_item;
|
||||
current_connection_edit_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 MetatilesPixmapItem::paintTileChanged(Map *map) {
|
||||
draw();
|
||||
}
|
||||
|
|
3
editor.h
3
editor.h
|
@ -51,6 +51,8 @@ public:
|
|||
void setConnectionsVisibility(bool visible);
|
||||
void updateConnectionOffset(int offset);
|
||||
void updateConnectionMap(QString mapName, QString direction);
|
||||
void addNewConnection();
|
||||
void removeCurrentConnection();
|
||||
|
||||
DraggablePixmapItem *addMapObject(Event *event);
|
||||
void selectMapObject(DraggablePixmapItem *object);
|
||||
|
@ -92,6 +94,7 @@ private:
|
|||
void setBorderItemsVisible(bool, qreal = 1);
|
||||
void setConnectionEditControlValues(Connection*);
|
||||
void setConnectionEditControlsEnabled(bool);
|
||||
void createConnectionItem(Connection* connection, bool hide);
|
||||
|
||||
private slots:
|
||||
void mouseEvent_map(QGraphicsSceneMouseEvent *event, MapPixmapItem *item);
|
||||
|
|
|
@ -780,3 +780,13 @@ void MainWindow::on_comboBox_ConnectedMap_currentTextChanged(const QString &mapN
|
|||
{
|
||||
editor->updateConnectionMap(mapName, ui->comboBox_ConnectionDirection->currentText().toLower());
|
||||
}
|
||||
|
||||
void MainWindow::on_pushButton_AddConnection_clicked()
|
||||
{
|
||||
editor->addNewConnection();
|
||||
}
|
||||
|
||||
void MainWindow::on_pushButton_RemoveConnection_clicked()
|
||||
{
|
||||
editor->removeCurrentConnection();
|
||||
}
|
||||
|
|
|
@ -80,6 +80,10 @@ private slots:
|
|||
|
||||
void on_comboBox_ConnectedMap_currentTextChanged(const QString &mapName);
|
||||
|
||||
void on_pushButton_AddConnection_clicked();
|
||||
|
||||
void on_pushButton_RemoveConnection_clicked();
|
||||
|
||||
private:
|
||||
Ui::MainWindow *ui;
|
||||
QStandardItemModel *mapListModel;
|
||||
|
|
104
mainwindow.ui
104
mainwindow.ui
|
@ -60,7 +60,7 @@
|
|||
</sizepolicy>
|
||||
</property>
|
||||
<property name="currentIndex">
|
||||
<number>0</number>
|
||||
<number>3</number>
|
||||
</property>
|
||||
<property name="tabsClosable">
|
||||
<bool>false</bool>
|
||||
|
@ -1246,7 +1246,7 @@
|
|||
<property name="spacing">
|
||||
<number>0</number>
|
||||
</property>
|
||||
<item row="0" column="0">
|
||||
<item row="1" column="0">
|
||||
<widget class="QFrame" name="horizontalFrame">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
|
||||
|
@ -1369,7 +1369,7 @@
|
|||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<item row="2" column="0">
|
||||
<widget class="QFrame" name="gridFrame">
|
||||
<property name="frameShape">
|
||||
<enum>QFrame::StyledPanel</enum>
|
||||
|
@ -1410,7 +1410,7 @@
|
|||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>826</width>
|
||||
<height>621</height>
|
||||
<height>587</height>
|
||||
</rect>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout_14">
|
||||
|
@ -1486,6 +1486,102 @@
|
|||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="0">
|
||||
<widget class="QFrame" name="horizontalFrame">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>0</width>
|
||||
<height>32</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="frameShape">
|
||||
<enum>QFrame::StyledPanel</enum>
|
||||
</property>
|
||||
<property name="frameShadow">
|
||||
<enum>QFrame::Raised</enum>
|
||||
</property>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_4">
|
||||
<property name="spacing">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="leftMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="topMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="rightMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<property name="bottomMargin">
|
||||
<number>4</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButton_AddConnection">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset>
|
||||
<activeon>:/icons/add.ico</activeon>
|
||||
</iconset>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="pushButton_RemoveConnection">
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset>
|
||||
<activeon>:/icons/delete.ico</activeon>
|
||||
</iconset>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="label_13">
|
||||
<property name="text">
|
||||
<string>Number of Connections:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="label_NumConnections">
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="horizontalSpacer_9">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
|
|
Loading…
Reference in a new issue