-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It is meant to incorporate the functionality of mapview_common needed to draw the map. Right now it delegates most (all) of its work to mapview_common.
- Loading branch information
1 parent
4f5fe2b
commit 4f85b9a
Showing
6 changed files
with
148 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2022 Louis Moureaux <[email protected]> | ||
* | ||
* SPDX-License-Identifier: GPLv3-or-later | ||
*/ | ||
|
||
#include "renderer.h" | ||
|
||
#include "mapview_common.h" | ||
|
||
namespace freeciv { | ||
|
||
/** | ||
* @class renderer | ||
* @brief Renders the map on widgets | ||
* | ||
* This class is used to draw the map. It can handle zoom via the @ref scale | ||
* property. | ||
* | ||
* @property scale By how much the map is scaled before being drawn (a scale | ||
* of 2 means that everything is 2x bigger) | ||
*/ | ||
|
||
/** | ||
* Constructor. | ||
*/ | ||
renderer::renderer(QObject *parent) : QObject(parent) {} | ||
|
||
/** | ||
* Changes the scale of the rendering (zooms in or out). | ||
*/ | ||
void renderer::set_scale(double scale) | ||
{ | ||
m_scale = scale; | ||
|
||
// When zoomed in, we pretend that the canvas is smaller than it actually | ||
// is. This makes text look bad, but everything else is drawn correctly. | ||
map_canvas_resized(m_viewport_size.width() / m_scale, | ||
m_viewport_size.height() / m_scale); | ||
} | ||
|
||
/** | ||
* Instructs the renderer to draw a viewport with a different size. | ||
*/ | ||
void renderer::set_viewport_size(const QSize &size) | ||
{ | ||
m_viewport_size = size; | ||
|
||
// When zoomed in, we pretend that the canvas is smaller than it actually | ||
// is. This makes text look bad, but everything else is drawn correctly. | ||
map_canvas_resized(m_viewport_size.width() / m_scale, | ||
m_viewport_size.height() / m_scale); | ||
} | ||
|
||
/** | ||
* Renders the specified region of the visible portion of the map on @c | ||
* painter. | ||
* @see @ref render(QPainter&, const QRect&) | ||
*/ | ||
void renderer::render(QPainter &painter, const QRegion ®ion) const | ||
{ | ||
for (const auto &rect : region) { | ||
render(painter, rect); | ||
} | ||
} | ||
|
||
/** | ||
* Renders the specified area of the visible portion of the map on @c | ||
* painter. This is meant to be used directly from @c paintEvent, so the | ||
* position of | ||
* @c area is relative to the @ref viewport. | ||
*/ | ||
void renderer::render(QPainter &painter, const QRect &area) const | ||
{ | ||
if (scale() != 1) { | ||
painter.setRenderHint(QPainter::SmoothPixmapTransform); | ||
} | ||
|
||
auto mapview_rect = | ||
QRectF(area.left() / scale(), area.top() / scale(), | ||
area.width() / scale(), area.height() / scale()); | ||
painter.drawPixmap(area, *mapview.store, mapview_rect); | ||
} | ||
|
||
} // namespace freeciv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2022 Louis Moureaux <[email protected]> | ||
* | ||
* SPDX-License-Identifier: GPLv3-or-later | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <QObject> | ||
#include <QPainter> | ||
#include <QRect> | ||
#include <QRegion> | ||
#include <QSize> | ||
|
||
namespace freeciv { | ||
|
||
class renderer : public QObject { | ||
Q_OBJECT | ||
Q_PROPERTY(double scale READ scale WRITE set_scale); | ||
|
||
public: | ||
explicit renderer(QObject *parent = nullptr); | ||
virtual ~renderer() = default; | ||
|
||
/// The scale (zoom) at which rendering is performed | ||
double scale() const { return m_scale; } | ||
void set_scale(double scale); | ||
|
||
/// The current dimensions of the viewport | ||
QSize viewport_size() const { return m_viewport_size; } | ||
void set_viewport_size(const QSize &size); | ||
|
||
void render(QPainter &painter, const QRegion ®ion) const; | ||
void render(QPainter &painter, const QRect &area) const; | ||
|
||
private: | ||
double m_scale = 1.0; | ||
QSize m_viewport_size; | ||
}; | ||
|
||
} // namespace freeciv |