1 | /* |
2 | Copyright 2003 Russell Steffen <rsteffen@bayarea.net> |
3 | Copyright 2003 Stephan Zehetner <s.zehetner@nevox.org> |
4 | Copyright 2006 Dmitry Suzdalev <dimsuz@gmail.com> |
5 | Copyright 2006 Inge Wallin <inge@lysator.liu.se> |
6 | Copyright 2006 Pierre Ducroquet <pinaraf@gmail.com> |
7 | Copyright Sean D'Epagnier <geckosenator@gmail.com> |
8 | |
9 | This program is free software; you can redistribute it and/or modify |
10 | it under the terms of the GNU General Public License as published by |
11 | the Free Software Foundation; either version 2 of the License, or |
12 | (at your option) any later version. |
13 | |
14 | This program is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | GNU General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU General Public License |
20 | along with this program; if not, write to the Free Software |
21 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
22 | */ |
23 | |
24 | #include "minimapview.h" |
25 | |
26 | #include <QMouseEvent> |
27 | #include <QPainter> |
28 | #include <QDebug> |
29 | |
30 | #include "planet.h" |
31 | #include "players/player.h" |
32 | |
33 | |
34 | MiniMapView::MiniMapView(QWidget *parent) : |
35 | QWidget(parent), |
36 | m_map(0), |
37 | m_selection(-1, -1) |
38 | { |
39 | QPalette pal = palette(); |
40 | pal.setColor(backgroundRole(), Qt::black); |
41 | setPalette(pal); |
42 | |
43 | setMinimumSize(100, 100); |
44 | setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
45 | } |
46 | |
47 | |
48 | MiniMapView::~MiniMapView() |
49 | { |
50 | } |
51 | |
52 | |
53 | void |
54 | MiniMapView::setMap(Map *map) |
55 | { |
56 | m_map = map; |
57 | connect(m_map, SIGNAL(update()), this, SLOT(update())); |
58 | } |
59 | |
60 | |
61 | void |
62 | MiniMapView::CalculateOffsets(float §orSize, float &woffset, float &hoffset) |
63 | { |
64 | sectorSize = ((float)width())/m_map->columns(); |
65 | if (height()/m_map->rows() < sectorSize) |
66 | sectorSize = ((float)height())/m_map->rows(); |
67 | |
68 | woffset = ((float)width() - m_map->columns()*sectorSize)/2; |
69 | hoffset = ((float)height() - m_map->rows()*sectorSize)/2; |
70 | } |
71 | |
72 | |
73 | void |
74 | MiniMapView::mousePressEvent(QMouseEvent *event) |
75 | { |
76 | float sectorSize, woffset, hoffset; |
77 | CalculateOffsets(sectorSize, woffset, hoffset); |
78 | |
79 | m_selection = Coordinate((event->x() - woffset) / sectorSize, |
80 | (event->y() - hoffset) / sectorSize); |
81 | |
82 | const Sector *sector = m_map->sector(m_selection); |
83 | if (sector && !sector->hasPlanet()) |
84 | m_selection = Coordinate(-1, -1); |
85 | |
86 | emit sectorSelected(m_selection); |
87 | } |
88 | |
89 | |
90 | void |
91 | MiniMapView::paintEvent(QPaintEvent * /*event*/) |
92 | { |
93 | QPainter painter(this); |
94 | |
95 | float sectorSize, woffset, hoffset; |
96 | CalculateOffsets(sectorSize, woffset, hoffset); |
97 | |
98 | // Draw the black background |
99 | painter.setRenderHint(QPainter::Antialiasing); |
100 | painter.setPen(Qt::black); |
101 | painter.setBrush(Qt::black); |
102 | painter.drawRect(QRectF(woffset, hoffset, m_map->columns() * sectorSize, m_map->rows() * sectorSize)); |
103 | |
104 | // Draw selection |
105 | if (hasSelection()) { |
106 | painter.setBrush(Qt::cyan); |
107 | painter.drawRect(QRectF( |
108 | woffset + m_selection.x() * sectorSize, |
109 | hoffset + m_selection.y() * sectorSize, |
110 | sectorSize, |
111 | sectorSize |
112 | )); |
113 | } |
114 | |
115 | // Now draw the planets... |
116 | for (int col = 0 ; col < m_map->columns() ; col++) { |
117 | for (int row = 0 ; row < m_map->rows() ; row++) { |
118 | QPoint pt(col, row); |
119 | Planet *planet = m_map->sector(pt)->planet(); |
120 | if (planet) { |
121 | Player *player = planet->player(); |
122 | if (player) { |
123 | painter.setBrush(player->color()); |
124 | |
125 | // Draw a circle in the planets color to show the planet. |
126 | painter.drawEllipse(QRectF( |
127 | woffset + col * sectorSize, |
128 | hoffset + row * sectorSize, |
129 | sectorSize, |
130 | sectorSize |
131 | )); |
132 | } |
133 | } |
134 | } |
135 | } |
136 | } |
137 | |