Skip to content

Commit

Permalink
Save less state in minimap_view
Browse files Browse the repository at this point in the history
The widget needs to compute horizontal and vertical scaling factors to
map between the displayed minimap (in pixel units) and the computed one
(essentially in tile units). It was computing them on resize events and
storing them, which was causing issues when switching maps. Compute them
when they are needed instead, it's just two float divisions.

Also remove a bunch of unused variables from the class and don't try to
move() it when shown (it's in a layout).

Closes #1657.
  • Loading branch information
lmoureaux authored and jwrober committed Oct 23, 2024
1 parent 5f8ebf0 commit a392316
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 44 deletions.
45 changes: 9 additions & 36 deletions client/minimap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,10 @@ const auto always_visible_margin = 15;
/**
Constructor for minimap
*/
minimap_view::minimap_view(QWidget *parent) : fcwidget()
minimap_view::minimap_view(QWidget *parent) : fcwidget(parent)
{
setParent(parent);
setAttribute(Qt::WA_OpaquePaintEvent, true);
w_ratio = 0.0;
h_ratio = 0.0;
// Dark magic: This call is required for the widget to work.
resize(0, 0);
background = QBrush(QColor(0, 0, 0));
setCursor(Qt::CrossCursor);
pix = new QPixmap;
}

/**
Minimap_view destructor
*/
minimap_view::~minimap_view()
{
delete pix;
pix = nullptr;
}

/**
Expand All @@ -74,20 +58,6 @@ void minimap_view::update_menu()
::king()->menu_bar->minimap_status->setChecked(false);
}

/**
Minimap is being moved, position is being remembered
*/
void minimap_view::moveEvent(QMoveEvent *event) { position = event->pos(); }

/**
Minimap is just unhidden, old position is restored
*/
void minimap_view::showEvent(QShowEvent *event)
{
move(position);
event->setAccepted(true);
}

namespace {

void overview_pos_nowrap(const struct tileset *t, int *ovr_x, int *ovr_y,
Expand Down Expand Up @@ -134,6 +104,10 @@ void minimap_view::draw_viewport(QPainter *painter)

painter->setPen(QColor(Qt::white));

auto w_ratio = static_cast<double>(width()) / gui_options->overview.width;
auto h_ratio =
static_cast<double>(height()) / gui_options->overview.height;

QVector<QLineF> lines;
for (int i = 0; i < 4; i++) {
lines.append(QLineF(x[i] * w_ratio, y[i] * h_ratio,
Expand Down Expand Up @@ -239,8 +213,6 @@ void minimap_view::resizeEvent(QResizeEvent *event)

if (C_S_RUNNING <= client_state() && size.width() > 0
&& size.height() > 0) {
w_ratio = static_cast<float>(width()) / gui_options->overview.width;
h_ratio = static_cast<float>(height()) / gui_options->overview.height;
king()->qt_settings.minimap_width =
static_cast<float>(size.width()) / mapview.width;
king()->qt_settings.minimap_height =
Expand All @@ -258,11 +230,12 @@ void minimap_view::resizeEvent(QResizeEvent *event)
void minimap_view::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::RightButton) {
cursor = event->pos();
auto fx = event->pos().x();
auto fy = event->pos().y();
fx = qRound(fx / w_ratio);
fy = qRound(fy / h_ratio);
fx = qRound(fx * gui_options->overview.width
/ static_cast<double>(width()));
fy = qRound(fy * gui_options->overview.height
/ static_cast<double>(height()));
fx = qMax(fx, 1);
fy = qMax(fy, 1);
fx = qMin(fx, gui_options->overview.width - 1);
Expand Down
9 changes: 1 addition & 8 deletions client/minimap.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class minimap_view : public fcwidget {

public:
minimap_view(QWidget *parent);
~minimap_view() override;

void paint(QPainter *painter, QPaintEvent *event);
void update_menu() override;
void update_image();
Expand All @@ -51,14 +51,7 @@ class minimap_view : public fcwidget {
void paintEvent(QPaintEvent *event) override;
void resizeEvent(QResizeEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void moveEvent(QMoveEvent *event) override;
void showEvent(QShowEvent *event) override;

private:
void draw_viewport(QPainter *painter);
float w_ratio, h_ratio;
QBrush background;
QPixmap *pix;
QPoint cursor;
QPoint position;
};

0 comments on commit a392316

Please sign in to comment.