1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-02-03 13:01:33 +02:00

Implement lasso brush

This commit is contained in:
nordsoft 2022-12-28 01:14:10 +04:00
parent 37d1e1e748
commit 954ea9d5a9
5 changed files with 82 additions and 2 deletions

BIN
mapeditor/icons/brush-3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -839,6 +839,22 @@ void MainWindow::on_toolArea_clicked(bool checked)
ui->tabWidget->setCurrentIndex(0);
}
void MainWindow::on_toolLasso_clicked(bool checked)
{
ui->toolBrush->setChecked(false);
ui->toolBrush2->setChecked(false);
ui->toolBrush4->setChecked(false);
ui->toolArea->setChecked(false);
//ui->toolLasso->setChecked(false);
if(checked)
ui->mapView->selectionTool = MapView::SelectionTool::Lasso;
else
ui->mapView->selectionTool = MapView::SelectionTool::None;
ui->tabWidget->setCurrentIndex(0);
}
void MainWindow::on_actionErase_triggered()
{
on_toolErase_clicked();

View File

@ -87,6 +87,8 @@ private slots:
void on_toolBrush2_clicked(bool checked);
void on_toolBrush4_clicked(bool checked);
void on_toolLasso_clicked(bool checked);
void on_inspectorWidget_itemChanged(QTableWidgetItem *item);

View File

@ -642,7 +642,7 @@
<item row="2" column="0">
<widget class="QPushButton" name="toolLasso">
<property name="enabled">
<bool>false</bool>
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
@ -663,7 +663,11 @@
</size>
</property>
<property name="text">
<string>O</string>
<string/>
</property>
<property name="icon">
<iconset>
<normaloff>icons:brush-3.png</normaloff>icons:brush-3.png</iconset>
</property>
<property name="checkable">
<bool>true</bool>

View File

@ -155,6 +155,14 @@ void MapView::mouseMoveEvent(QMouseEvent *mouseEvent)
}
sc->selectionTerrainView.draw();
break;
case MapView::SelectionTool::Lasso:
if(mouseEvent->buttons() == Qt::LeftButton)
{
sc->selectionTerrainView.select(tile);
sc->selectionTerrainView.draw();
}
break;
case MapView::SelectionTool::None:
if(mouseEvent->buttons() & Qt::RightButton)
@ -247,6 +255,7 @@ void MapView::mousePressEvent(QMouseEvent *event)
break;
case MapView::SelectionTool::Area:
case MapView::SelectionTool::Lasso:
if(event->button() == Qt::RightButton)
break;
@ -329,6 +338,55 @@ void MapView::mouseReleaseEvent(QMouseEvent *event)
switch(selectionTool)
{
case MapView::SelectionTool::Lasso: {
if(event->button() == Qt::RightButton)
break;
//key: y position of tile
//value.first: x position of left tile
//value.second: x postiion of right tile
std::map<int, std::pair<int, int>> selectionRangeMapX, selectionRangeMapY;
for(auto & t : sc->selectionTerrainView.selection())
{
auto pairIter = selectionRangeMapX.find(t.y);
if(pairIter == selectionRangeMapX.end())
selectionRangeMapX[t.y] = std::make_pair(t.x, t.x);
else
{
pairIter->second.first = std::min(pairIter->second.first, t.x);
pairIter->second.second = std::max(pairIter->second.second, t.x);
}
pairIter = selectionRangeMapY.find(t.x);
if(pairIter == selectionRangeMapY.end())
selectionRangeMapY[t.x] = std::make_pair(t.y, t.y);
else
{
pairIter->second.first = std::min(pairIter->second.first, t.y);
pairIter->second.second = std::max(pairIter->second.second, t.y);
}
}
std::set<int3> selectionByX, selectionByY;
std::vector<int3> finalSelection;
for(auto & selectionRange : selectionRangeMapX)
{
for(int i = selectionRange.second.first; i < selectionRange.second.second; ++i)
selectionByX.insert(int3(i, selectionRange.first, sc->level));
}
for(auto & selectionRange : selectionRangeMapY)
{
for(int i = selectionRange.second.first; i < selectionRange.second.second; ++i)
selectionByY.insert(int3(selectionRange.first, i, sc->level));
}
std::set_intersection(selectionByX.begin(), selectionByX.end(), selectionByY.begin(), selectionByY.end(), std::back_inserter(finalSelection));
for(auto & lassoTile : finalSelection)
sc->selectionTerrainView.select(lassoTile);
sc->selectionTerrainView.draw();
break;
}
case MapView::SelectionTool::None:
if(event->button() == Qt::RightButton)
break;