Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save less state in minimap_view #2409

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};
Loading