File indexing completed on 2024-12-08 03:47:13
0001 /* 0002 SPDX-FileCopyrightText: 2003 Russell Steffen <rsteffen@bayarea.net> 0003 SPDX-FileCopyrightText: 2003 Stephan Zehetner <s.zehetner@nevox.org> 0004 SPDX-FileCopyrightText: 2006 Dmitry Suzdalev <dimsuz@gmail.com> 0005 SPDX-FileCopyrightText: 2006 Inge Wallin <inge@lysator.liu.se> 0006 SPDX-FileCopyrightText: 2006 Pierre Ducroquet <pinaraf@gmail.com> 0007 Copyright Sean D'Epagnier <geckosenator@gmail.com> 0008 0009 SPDX-License-Identifier: GPL-2.0-or-later 0010 */ 0011 0012 #include "minimapview.h" 0013 0014 #include <QMouseEvent> 0015 #include <QPainter> 0016 0017 #include "planet.h" 0018 #include "players/player.h" 0019 0020 0021 MiniMapView::MiniMapView(QWidget *parent) : 0022 QWidget(parent), 0023 m_map(nullptr), 0024 m_selection(-1, -1) 0025 { 0026 QPalette pal = palette(); 0027 pal.setColor(backgroundRole(), Qt::black); 0028 setPalette(pal); 0029 0030 setMinimumSize(100, 100); 0031 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); 0032 } 0033 0034 0035 MiniMapView::~MiniMapView() 0036 { 0037 } 0038 0039 0040 void 0041 MiniMapView::setMap(Map *map) 0042 { 0043 m_map = map; 0044 connect(m_map, &Map::update, this, static_cast<void (QWidget::*)(void)>(&QWidget::update)); 0045 } 0046 0047 0048 void 0049 MiniMapView::CalculateOffsets(float §orSize, float &woffset, float &hoffset) 0050 { 0051 sectorSize = ((float)width())/m_map->columns(); 0052 if (height()/m_map->rows() < sectorSize) 0053 sectorSize = ((float)height())/m_map->rows(); 0054 0055 woffset = ((float)width() - m_map->columns()*sectorSize)/2; 0056 hoffset = ((float)height() - m_map->rows()*sectorSize)/2; 0057 } 0058 0059 0060 void 0061 MiniMapView::mousePressEvent(QMouseEvent *event) 0062 { 0063 float sectorSize, woffset, hoffset; 0064 CalculateOffsets(sectorSize, woffset, hoffset); 0065 0066 const QPoint mousePos = event->position().toPoint(); 0067 m_selection = Coordinate((mousePos.x() - woffset) / sectorSize, 0068 (mousePos.y() - hoffset) / sectorSize); 0069 0070 const Sector *sector = m_map->sector(m_selection); 0071 if (sector && !sector->hasPlanet()) 0072 m_selection = Coordinate(-1, -1); 0073 0074 Q_EMIT sectorSelected(m_selection); 0075 } 0076 0077 0078 void 0079 MiniMapView::paintEvent(QPaintEvent * /*event*/) 0080 { 0081 QPainter painter(this); 0082 0083 float sectorSize, woffset, hoffset; 0084 CalculateOffsets(sectorSize, woffset, hoffset); 0085 0086 // Draw the black background 0087 painter.setRenderHint(QPainter::Antialiasing); 0088 painter.setPen(Qt::black); 0089 painter.setBrush(Qt::black); 0090 painter.drawRect(QRectF(woffset, hoffset, m_map->columns() * sectorSize, m_map->rows() * sectorSize)); 0091 0092 // Draw selection 0093 if (hasSelection()) { 0094 painter.setBrush(Qt::cyan); 0095 painter.drawRect(QRectF( 0096 woffset + m_selection.x() * sectorSize, 0097 hoffset + m_selection.y() * sectorSize, 0098 sectorSize, 0099 sectorSize 0100 )); 0101 } 0102 0103 // Now draw the planets... 0104 for (int col = 0 ; col < m_map->columns() ; col++) { 0105 for (int row = 0 ; row < m_map->rows() ; row++) { 0106 QPoint pt(col, row); 0107 Planet *planet = m_map->sector(pt)->planet(); 0108 if (planet) { 0109 Player *player = planet->player(); 0110 if (player) { 0111 painter.setBrush(player->color()); 0112 0113 // Draw a circle in the planets color to show the planet. 0114 painter.drawEllipse(QRectF( 0115 woffset + col * sectorSize, 0116 hoffset + row * sectorSize, 0117 sectorSize, 0118 sectorSize 0119 )); 0120 } 0121 } 0122 } 0123 } 0124 } 0125 0126 #include "moc_minimapview.cpp"