QGraphicsView实现简易地图4『局部加载-地图漫游』

前文链接:QGraphicsView实现简易地图3『局部加载-地图缩放』
当鼠标拖动地图移动时,需要实时增补和删减瓦片地图,大致思路是计算地图从各方向移动时进出视口的瓦片坐标值,根据变化后的瓦片坐标值来增减地图瓦片,以下将提供实现此需求的核心代码。
1、动态演示效果


2、静态展示图片
在这里插入图片描述

核心代码

void MapView::moveScene()
{
	QString appPath = QApplication::applicationDirPath();
	QString dirPath = QString("%1/MapData/GaoDeMap/Map/MapPng/L0%2").arg(appPath).arg(m_curLevel + 1);
	// 视口宽度和高度
	int vw = viewport()->width();
	int vh = viewport()->height();

	// 计算呈现的瓦片地图左上角的场景坐标和视口坐标、呈现的瓦片地图右下角的场景坐标和视口坐标
	QPoint topLeftScenePos(m_topLeftTileCoord.x * PIXMAP_SIZE, m_topLeftTileCoord.y * PIXMAP_SIZE);
	QPointF topLeftViewPos = mapFromScene(topLeftScenePos);
	QPoint bottomRightScenePos(m_bottomRightTileCoord.x * PIXMAP_SIZE, m_bottomRightTileCoord.y * PIXMAP_SIZE);
	QPointF bottomRightViewPos = mapFromScene(bottomRightScenePos);

	int mapSideCount = pow(2, m_curLevel);
	
	if (MapUtility::sceneSize(m_curLevel) > vw)
	{
		// 1、水平瓦片坐标控制:判断最左侧瓦片是否完全进入视口、最右侧瓦片是否完全离开视口
		if (topLeftViewPos.x() > 0)
		{
			int count = qCeil(topLeftViewPos.x() / PIXMAP_SIZE);	// 左侧进入视口瓦片数量
			int oldLeftTileCoordX = m_topLeftTileCoord.x;			// 保存原左侧瓦片坐标X
			m_topLeftTileCoord.x -= count;							// 更新现左侧瓦片坐标X
			if (m_topLeftTileCoord.x < 0)
				m_topLeftTileCoord.x = 0;

			// 增加从左侧进入视口的图片
			for (int row = m_topLeftTileCoord.y; row <= m_bottomRightTileCoord.y; ++row)
			{
				for (int col = m_topLeftTileCoord.x; col < oldLeftTileCoordX; ++col)
				{
					QString fileName = QString("%1/Map_%2-%3.png").arg(dirPath)
						.arg(QString::number(row + 1).rightJustified(2, '0')).arg(QString::number(col + 1).rightJustified(2, '0'));
					QPixmap pixmap(fileName);
					QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
					item->setPos(PIXMAP_SIZE * col, PIXMAP_SIZE * row);
					m_scene->addItem(item);
					m_mapItems[row][col] = item;
				}
			}
		}
		if (bottomRightViewPos.x() > vw)
		{
			int count = qFloor((bottomRightViewPos.x() - vw) / PIXMAP_SIZE) + 1;	// 右侧离开视口瓦片数量
			int oldRightTileCoordX = m_bottomRightTileCoord.x;					// 保存原右侧瓦片坐标X
			m_bottomRightTileCoord.x -= count;									// 更新现右侧瓦片坐标X
			if (m_bottomRightTileCoord.x < 0)
				m_bottomRightTileCoord.x = 0;

			// 删除从右侧离开视口的图片
			for (int row = m_topLeftTileCoord.y; row <= m_bottomRightTileCoord.y; ++row)
			{
				for (int col = oldRightTileCoordX; col > m_bottomRightTileCoord.x; --col)
				{
					QGraphicsPixmapItem *item = m_mapItems[row][col];
					m_scene->removeItem(item);
					m_mapItems[row].remove(col);
					delete item;
				}
			}
		}

		// 2、水平瓦片坐标控制:判断最右侧瓦片是否完全进入视口、最左侧瓦片是否完全离开视口
		if (bottomRightViewPos.x() + 255 < vw)
		{
			int count = qCeil((vw - (bottomRightViewPos.x() + 255)) / PIXMAP_SIZE);	// 右侧进入视口瓦片数量
			int oldRightTileCoordX = m_bottomRightTileCoord.x;						// 保存原右侧瓦片坐标X
			m_bottomRightTileCoord.x += count;										// 保存现右侧瓦片坐标X
			if (m_bottomRightTileCoord.x >= mapSideCount)
				m_bottomRightTileCoord.x = mapSideCount - 1;

			// 增加从右侧进入视口的图片
			for (int row = m_topLeftTileCoord.y; row <= m_bottomRightTileCoord.y; ++row)
			{
				for (int col = m_bottomRightTileCoord.x; col > oldRightTileCoordX; --col)
				{
					QString fileName = QString("%1/Map_%2-%3.png").arg(dirPath)
						.arg(QString::number(row + 1).rightJustified(2, '0')).arg(QString::number(col + 1).rightJustified(2, '0'));
					QPixmap pixmap(fileName);
					QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
					item->setPos(PIXMAP_SIZE * col, PIXMAP_SIZE * row);
					m_scene->addItem(item);
					m_mapItems[row][col] = item;
				}
			}
		}

		if (topLeftViewPos.x() + 255 < 0)
		{
			int count = qFloor(fabs(topLeftViewPos.x()) / PIXMAP_SIZE);	// 左侧离开视口瓦片数量
			int oldLeftTileCoordX = m_topLeftTileCoord.x;				// 保存原左侧瓦片坐标X
			m_topLeftTileCoord.x += count;								// 保存现左侧瓦片坐标X
			if (m_topLeftTileCoord.x >= mapSideCount)
				m_topLeftTileCoord.x = mapSideCount - 1;

			// 删除从左侧离开视口的图片
			for (int row = m_topLeftTileCoord.y; row <= m_bottomRightTileCoord.y; ++row)
			{
				for (int col = oldLeftTileCoordX; col < m_topLeftTileCoord.x; ++col)
				{
					QGraphicsPixmapItem *item = m_mapItems[row][col];
					m_scene->removeItem(item);
					m_mapItems[row].remove(col);
					delete item;
				}
			}
		}
	}
	
	if (MapUtility::sceneSize(m_curLevel) > vh)
	{
		// 3、垂直瓦片坐标控制:判断最上侧瓦片是否完全进入视口,最下侧瓦片是否完全离开视口
		if (topLeftViewPos.y() > 0)
		{
			int count = qCeil(topLeftViewPos.y() / PIXMAP_SIZE);	// 上侧进入视口瓦片数量
			int oldTopTileCoordY = m_topLeftTileCoord.y;			// 保存原上侧瓦片坐标Y
			m_topLeftTileCoord.y -= count;							// 保存现上侧瓦片坐标Y
			if (m_topLeftTileCoord.y < 0)
				m_topLeftTileCoord.y = 0;

			// 增加从上侧进入视口的图片
			for (int row = m_topLeftTileCoord.y; row < oldTopTileCoordY; ++row)
			{
				for (int col = m_topLeftTileCoord.x; col <= m_bottomRightTileCoord.x; ++col)
				{
					QString fileName = QString("%1/Map_%2-%3.png").arg(dirPath)
						.arg(QString::number(row + 1).rightJustified(2, '0')).arg(QString::number(col + 1).rightJustified(2, '0'));
					QPixmap pixmap(fileName);
					QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
					item->setPos(PIXMAP_SIZE * col, PIXMAP_SIZE * row);
					m_scene->addItem(item);
					m_mapItems[row][col] = item;
				}
			}
		}

		if (bottomRightViewPos.y() > vh)
		{
			int count = qFloor((bottomRightViewPos.y() - vh) / PIXMAP_SIZE) + 1;	// 下侧离开视口瓦片数量
			int oldBottomTileCoordY = m_bottomRightTileCoord.y;					// 保存原下侧瓦片坐标Y
			m_bottomRightTileCoord.y -= count;									// 保存现下侧瓦片坐标Y
			if (m_bottomRightTileCoord.y < 0)
				m_bottomRightTileCoord.y = 0;

			// 删除从下侧离开视口的图片
			for (int row = oldBottomTileCoordY; row > m_bottomRightTileCoord.y; --row)
			{
				for (int col = m_topLeftTileCoord.x; col <= m_bottomRightTileCoord.x; ++col)
				{
					QGraphicsPixmapItem *item = m_mapItems[row][col];
					m_scene->removeItem(item);
					m_mapItems[row].remove(col);
					delete item;
				}
			}
		}

		// 4、垂直瓦片坐标控制:判断最下侧瓦片是否完全进入视口,最上侧瓦片是否完全离开视口
		if (bottomRightViewPos.y() + 255 < vh)
		{
			int count = qCeil((vh - (bottomRightViewPos.y() + 255)) / PIXMAP_SIZE);	// 下侧进入视口瓦片数量
			int oldBottomTileCoordY = m_bottomRightTileCoord.y;						// 保存原下侧瓦片坐标Y
			m_bottomRightTileCoord.y += count;										// 保存现下侧瓦片坐标Y
			if (m_bottomRightTileCoord.y >= mapSideCount)
				m_bottomRightTileCoord.y = mapSideCount - 1;

			// 增加从下侧进入视口的图片
			for (int row = m_bottomRightTileCoord.y; row > oldBottomTileCoordY; --row)
			{
				for (int col = m_topLeftTileCoord.x; col <= m_bottomRightTileCoord.x; ++col)
				{
					QString fileName = QString("%1/Map_%2-%3.png").arg(dirPath)
						.arg(QString::number(row + 1).rightJustified(2, '0')).arg(QString::number(col + 1).rightJustified(2, '0'));
					QPixmap pixmap(fileName);
					QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
					item->setPos(PIXMAP_SIZE * col, PIXMAP_SIZE * row);
					m_scene->addItem(item);
					m_mapItems[row][col] = item;
				}
			}
		}

		if (topLeftViewPos.y() + 255 < 0)
		{
			int count = qFloor(fabs(topLeftViewPos.y()) / PIXMAP_SIZE);	// 上侧离开视口瓦片数量
			int oldTopTileCoordY = m_topLeftTileCoord.y;				// 保存原上侧瓦片坐标Y
			m_topLeftTileCoord.y += count;								// 保存现上侧瓦片坐标Y
			if (m_topLeftTileCoord.y >= mapSideCount)
				m_topLeftTileCoord.y = mapSideCount - 1;

			// 删除从上侧离开视口的图片
			for (int row = oldTopTileCoordY; row < m_topLeftTileCoord.y; ++row)
			{
				for (int col = m_topLeftTileCoord.x; col <= m_bottomRightTileCoord.x; ++col)
				{
					QGraphicsPixmapItem *item = m_mapItems[row][col];
					m_scene->removeItem(item);
					m_mapItems[row].remove(col);
					delete item;
				}
			}
		}
	}
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浮生卍流年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值