فهرست منبع

Implement lasso brush

nordsoft 2 سال پیش
والد
کامیت
954ea9d5a9
5فایلهای تغییر یافته به همراه82 افزوده شده و 2 حذف شده
  1. BIN
      mapeditor/icons/brush-3.png
  2. 16 0
      mapeditor/mainwindow.cpp
  3. 2 0
      mapeditor/mainwindow.h
  4. 6 2
      mapeditor/mainwindow.ui
  5. 58 0
      mapeditor/mapview.cpp

BIN
mapeditor/icons/brush-3.png


+ 16 - 0
mapeditor/mainwindow.cpp

@@ -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();

+ 2 - 0
mapeditor/mainwindow.h

@@ -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);
 

+ 6 - 2
mapeditor/mainwindow.ui

@@ -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>

+ 58 - 0
mapeditor/mapview.cpp

@@ -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;