From e3af85ab1a06742ba23c815b80887fdbf32fe36c Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 18 Mar 2012 11:01:29 +0000 Subject: Made MapWidget::zoom() independent of the size of the widget. --- src/editorwindow.cc | 2 +- src/mapwidget.cc | 87 +++++++++++++++++++++++++++-------------------------- src/mapwidget.h | 15 ++++++--- 3 files changed, 56 insertions(+), 48 deletions(-) (limited to 'src') diff --git a/src/editorwindow.cc b/src/editorwindow.cc index fc183c6..ee71577 100644 --- a/src/editorwindow.cc +++ b/src/editorwindow.cc @@ -254,7 +254,7 @@ void EditorWindow::deleteSelected() void EditorWindow::showZoneProperties() { - QMessageBox::information(this, tr("Zone properties"), tr("Not yet iplemented")); + QMessageBox::information(this, tr("Zone properties"), tr("Needs to be implemented")); } } diff --git a/src/mapwidget.cc b/src/mapwidget.cc index f5dd595..2ce1def 100644 --- a/src/mapwidget.cc +++ b/src/mapwidget.cc @@ -15,6 +15,8 @@ #include #include +#include + namespace editor { @@ -23,16 +25,12 @@ MapWidget::MapWidget(QWidget *parent) : QWidget(parent) //setMinimumSize(256,256); mapwidget_manipulator = new Manipulator(this); - // zoom sucks but it works - mapwidget_zoom = 16; - - // center of the map (used for dragging) - center_x = 0; - center_y = 0; + // zoom factor + mapwidget_zoomfactor = 0; + // reset dragging dragstart_x = 0; - dragstart_y = 0; - + dragstart_y = 0; is_dragging = false; mapwidget_selected = 0; @@ -40,21 +38,23 @@ MapWidget::MapWidget(QWidget *parent) : QWidget(parent) setFocusPolicy(Qt::ClickFocus); } +float MapWidget::zoom() const +{ + return ::pow(2.0f, (float) mapwidget_zoomfactor); +} + void MapWidget::wheelEvent(QWheelEvent *event) { if (event->orientation() == Qt::Vertical) { - int oldzoom = mapwidget_zoom; + + mapwidget_zoomfactor += event->delta() / 120; - mapwidget_zoom -= event->delta() / 120; - if (mapwidget_zoom < 1) - mapwidget_zoom = 1; - else if (mapwidget_zoom > 64) { - mapwidget_zoom = 64; + if (mapwidget_zoomfactor < - 64) { + mapwidget_zoomfactor = -64; + } else if (mapwidget_zoomfactor > 64) { + mapwidget_zoomfactor = 64; } - - center_x = center_x * oldzoom / mapwidget_zoom; - center_y = center_y * oldzoom / mapwidget_zoom; - + resizeChildren(); update(); event->accept(); @@ -83,14 +83,13 @@ void MapWidget::mouseReleaseEvent(QMouseEvent *event) 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; + float delta_x = event->pos().x() - dragstart_x; + float delta_y = event->pos().y() - dragstart_y; + // move map center, mapwidget_center is in world coordinates + mapwidget_center[0] += delta_y / zoom(); + mapwidget_center[1] += delta_x / zoom(); + dragstart_x = event->pos().x(); dragstart_y = event->pos().y(); @@ -107,19 +106,20 @@ void MapWidget::resizeChildren() if ((width() < 16) || (height() < 16)) return; - const float scale = (float) width() / (float) (mapwidget_zoom * 256); - for (int i = 0; i < mapwidget_enties.size(); ++i) { + EntityWidget * entity = mapwidget_enties.at(i); - int radius = (int) (entity->properties()->radius() * scale); - if (radius < 6) + + int radius = (int) (entity->properties()->radius() * zoom()); + if (radius < 6) { radius = 6; + } - int x = width() / 2 - (int) (entity->properties()->location()[1] * scale) - radius; - int y = height() / 2 - (int)(entity->properties()->location()[0] * scale) - radius; + int x = width() / 2 - (int)((entity->properties()->location()[1] - mapwidget_center[1]) * zoom()) - radius; + int y = height() / 2 - (int)((entity->properties()->location()[0] - mapwidget_center[0]) * zoom()) - 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; if (entity->selected()) { mapwidget_manipulator->setSize(entity); mapwidget_manipulator->setProperties(entity->properties()); @@ -144,14 +144,13 @@ void MapWidget::deselect() void MapWidget::dragEntity(EntityWidget *entity, int x, int y) { - const float scale = (float) (mapwidget_zoom * 256) / (float) width(); - int radius = (int) (entity->properties()->radius() / scale); + int radius = (int) (entity->properties()->radius() * zoom()); if (radius < 6) radius = 6; entity->properties()->set_location( - entity->properties()->location()[0] - (int) ((float) (y - radius) * scale), - entity->properties()->location()[1] - (int) ((float) (x - radius) * scale), + entity->properties()->location()[0] - (int) ((float) (y - radius) / zoom()), + entity->properties()->location()[1] - (int) ((float) (x - radius) / zoom()), entity->properties()->location()[2] ); @@ -217,11 +216,13 @@ void MapWidget::paintEvent(QPaintEvent *event) painter.setPen(pen); // mapwidget_zoom is used to scale from game units to screen coorinates - // 1 grid square is 256 game units + // 1 grid square is 128 game units // gridsize is the size of one grid square in screen coordinates - int gridsize = width() / mapwidget_zoom; + int gridsize = (int) (128.0f * zoom()); + if (gridsize <= 0) + return; - int x = center_x + width() / 2; + int x = (int)(mapwidget_center.y() * zoom()) + width() / 2; pen.setColor(axislinecolor); painter.setPen(pen); painter.drawLine(x, 0, x, height()); @@ -233,14 +234,14 @@ void MapWidget::paintEvent(QPaintEvent *event) painter.drawLine(x, 0, x, height()); x -= gridsize; } - x = center_x + width() / 2 + gridsize; + x = (int) (mapwidget_center.y() * zoom()) + width() / 2 + gridsize; while (x <= width()) { painter.drawLine(x, 0, x, height()); x += gridsize; } - int y = center_y + height() / 2; + int y = (int) (mapwidget_center.x() * zoom()) + height() / 2; pen.setColor(axislinecolor); painter.setPen(pen); @@ -253,7 +254,7 @@ void MapWidget::paintEvent(QPaintEvent *event) painter.drawLine(0, y, width(), y); y -= gridsize; } - y = center_y + height() / 2 + gridsize; + y = (int) (mapwidget_center.x() * zoom()) + 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 2dde34f..d418602 100644 --- a/src/mapwidget.h +++ b/src/mapwidget.h @@ -42,6 +42,11 @@ public: inline EntityWidget *selected() { return mapwidget_selected; } + + /** + * @return the current map zoom factor + * */ + float zoom() const; protected: @@ -122,14 +127,16 @@ private slots: private: typedef QList Entities; - int mapwidget_zoom; int dragstart_x; int dragstart_y; - int center_x; - int center_y; - bool is_dragging; + + // zoom factor, as a power of 2 + int mapwidget_zoomfactor; + + // center of the map, in world coordinates + Vector3f mapwidget_center; Entities mapwidget_enties; EntityWidget *mapwidget_selected; -- cgit v1.2.3