From 8aa0a55840f02482884e29b33b770daa6028b81e Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 19 Feb 2012 12:35:12 +0000 Subject: Commented out debug messages, implemented map dragging. --- src/mapentity.cc | 2 +- src/mapwidget.cc | 65 ++++++++++++++++++++++++++++++++++++++++++++++++-------- src/mapwidget.h | 22 ++++++++++++++++++- 3 files changed, 78 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/mapentity.cc b/src/mapentity.cc index dd16379..55bb501 100644 --- a/src/mapentity.cc +++ b/src/mapentity.cc @@ -66,7 +66,7 @@ void MapEntity::paintEvent(QPaintEvent *event) void MapEntity::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { - qDebug() << "clicked entity " << name(); + //qDebug() << "clicked entity " << name(); event->accept(); emit clicked(this); diff --git a/src/mapwidget.cc b/src/mapwidget.cc index a33cf65..779bf91 100644 --- a/src/mapwidget.cc +++ b/src/mapwidget.cc @@ -25,21 +25,68 @@ MapWidget::MapWidget(QWidget *parent) : QWidget(parent) mapwidget_zoom = 16; // center of the map (used for dragging) + center_x = 0; + center_y = 0; } void MapWidget::wheelEvent(QWheelEvent *event) { if (event->orientation() == Qt::Vertical) { + int oldzoom = mapwidget_zoom; + mapwidget_zoom -= event->delta() / 120; - if (mapwidget_zoom < 4) - mapwidget_zoom = 4; + if (mapwidget_zoom < 1) + mapwidget_zoom = 1; else if (mapwidget_zoom > 64) { mapwidget_zoom = 64; } + center_x = center_x * oldzoom / mapwidget_zoom; + center_y = center_y * oldzoom / mapwidget_zoom; + + resizeChildren(); + update(); + event->accept(); + } +} + +void MapWidget::mousePressEvent(QMouseEvent *event) +{ + if (event->button() == Qt::RightButton) { + dragstart_x = event->pos().x(); + dragstart_y = event->pos().y(); + event->accept(); + is_dragging = true; + } else { + is_dragging = false; + } +} + +void MapWidget::mouseReleaseEvent(QMouseEvent *event) +{ + if (event->button() == Qt::RightButton) { + is_dragging = false; + } +} + +void MapWidget::mouseMoveEvent(QMouseEvent *event) +{ + if (is_dragging) { + int delta_x = event->pos().x() - dragstart_x; + int delta_y = event->pos().y() - dragstart_y; + + center_x += delta_x; + center_y += delta_y; + + //qDebug() << "dragging " << delta_x << " " << delta_y; + + dragstart_x = event->pos().x(); + dragstart_y = event->pos().y(); + + event->accept(); + resizeChildren(); update(); - event->accept(); } } @@ -56,7 +103,7 @@ void MapWidget::resizeChildren() int x = width() / 2 - (int) (entity->location(1) * scale) - radius; int y = height() / 2 - (int)(entity->location(0) * scale) - radius; - entity->setGeometry(x, y, radius * 2 , radius * 2); + entity->setGeometry(center_x + x, center_y + y, radius * 2 , radius * 2); //qDebug() << "Moving entity to " << x << " " << y; } @@ -71,7 +118,7 @@ void MapWidget::select(MapEntity *entity) if (entity == mapentity) { entity->set_selected(true); - qDebug() << "selected entity " << entity->name(); + //qDebug() << "selected entity " << entity->name(); emit selected(entity); } else { @@ -99,23 +146,23 @@ void MapWidget::paintEvent(QPaintEvent *event) // gridsize is the size of one grid square in screen coordinates int gridsize = width() / mapwidget_zoom; - int x = width() / 2; + int x = center_x + width() / 2; while (x > 0) { painter.drawLine(x, 0, x, height()); x -= gridsize; } - x = width() / 2 + gridsize; + x = center_x + width() / 2 + gridsize; while (x <= width()) { painter.drawLine(x, 0, x, height()); x += gridsize; } - int y = height() / 2; + int y = center_y + height() / 2; while (y > 0) { painter.drawLine(0, y, width(), y); y -= gridsize; } - y = height() / 2 + gridsize; + y = center_y + height() / 2 + gridsize; while (y <= height()) { painter.drawLine(0, y, width(), y); y += gridsize; diff --git a/src/mapwidget.h b/src/mapwidget.h index 7371ffd..4f0a1fa 100644 --- a/src/mapwidget.h +++ b/src/mapwidget.h @@ -42,7 +42,22 @@ protected: * @brief handle resize events * */ virtual void resizeEvent(QResizeEvent *event); - + + /** + * @brief handle mouse button press events + * */ + virtual void mousePressEvent(QMouseEvent *event); + + /** + * @brief handle mouse button press events + * */ + virtual void mouseReleaseEvent(QMouseEvent *event); + + /** + * @brief handle mouse move press events + * */ + virtual void mouseMoveEvent(QMouseEvent *event); + /** * @brief handle mousewheel events * */ @@ -64,9 +79,14 @@ private: void resizeChildren(); int mapwidget_zoom; + int dragstart_x; + int dragstart_y; + int center_x; int center_y; + bool is_dragging; + QList mapwidget_enties; }; -- cgit v1.2.3