diff options
Diffstat (limited to 'src/mapwidget.cc')
-rw-r--r-- | src/mapwidget.cc | 69 |
1 files changed, 55 insertions, 14 deletions
diff --git a/src/mapwidget.cc b/src/mapwidget.cc index febf6ac..78c413d 100644 --- a/src/mapwidget.cc +++ b/src/mapwidget.cc @@ -5,8 +5,8 @@ the GNU General Public License version 2 */ +#include "entitywidget.h" #include "mapwidget.h" -#include "mapentity.h" #include <QPainter> #include <QMouseEvent> @@ -27,6 +27,11 @@ MapWidget::MapWidget(QWidget *parent) : QWidget(parent) // center of the map (used for dragging) center_x = 0; center_y = 0; + + dragstart_x = 0; + dragstart_y = 0; + + setFocusPolicy(Qt::ClickFocus); } void MapWidget::wheelEvent(QWheelEvent *event) @@ -95,7 +100,7 @@ void MapWidget::resizeChildren() const float scale = (float) width() / (float) (mapwidget_zoom * 256); for (int i = 0; i < mapwidget_enties.size(); ++i) { - MapEntity * entity = mapwidget_enties.at(i); + EntityWidget * entity = mapwidget_enties.at(i); int radius = (int) (entity->radius() * scale); if (radius < 6) radius = 6; @@ -109,13 +114,37 @@ void MapWidget::resizeChildren() } -void MapWidget::select(MapEntity *entity) +void MapWidget::deselect() +{ + for (int i = 0; i < mapwidget_enties.size(); ++i) { + EntityWidget *entitywidget = mapwidget_enties.at(i); + entitywidget->set_selected(false); + } + emit selected(0); +} + +void MapWidget::dragEntity(EntityWidget *entity, int x, int y) +{ + const float scale = (float) (mapwidget_zoom * 256) / (float) width(); + int radius = (int) (entity->radius() / scale); + if (radius < 6) + radius = 6; + + entity->set_location( + entity->location(0) - (int) ((float) (y - radius) * scale), + entity->location(1) - (int) ((float) (x - radius) * scale), + entity->location(2) + ); + emit selected(entity); + resizeChildren(); +} +void MapWidget::select(EntityWidget *entity) { for (int i = 0; i < mapwidget_enties.size(); ++i) { - MapEntity *mapentity = mapwidget_enties.at(i); + EntityWidget *entitywidget = mapwidget_enties.at(i); - if (entity == mapentity) { + if (entity == entitywidget) { entity->set_selected(true); //qDebug() << "selected entity " << entity->name(); @@ -123,13 +152,24 @@ void MapWidget::select(MapEntity *entity) } else { - mapentity->set_selected(false); + entitywidget->set_selected(false); } } update(); } -void MapWidget::resizeEvent (QResizeEvent *event) +void MapWidget::keyPressEvent(QKeyEvent *event) +{ + if (event->key() == Qt::Key_Escape) { + deselect(); + event->accept(); + } else { + event->ignore(); + } + +} + +void MapWidget::resizeEvent(QResizeEvent *event) { resizeChildren(); } @@ -191,20 +231,21 @@ void MapWidget::paintEvent(QPaintEvent *event) QWidget::paintEvent(event); } -MapEntity *MapWidget::addEntity() +EntityWidget *MapWidget::addEntity() { - MapEntity *mapentity = new MapEntity(this); - mapwidget_enties.append(mapentity); + EntityWidget *entitywidget = new EntityWidget(this); + mapwidget_enties.append(entitywidget); - // connect the MapEntity::clicked() signal to the MapWidget::selected() slot - connect(mapentity, SIGNAL(clicked(MapEntity *)), this, SLOT(select(MapEntity *))); + // connect the EntityWidget::clicked() signal to the MapWidget::selected() slot + connect(entitywidget, SIGNAL(clicked(EntityWidget *)), this, SLOT(select(EntityWidget *))); + connect(entitywidget, SIGNAL(dragged(EntityWidget *, int, int)), this, SLOT(dragEntity(EntityWidget *, int, int))); - mapentity->show(); + entitywidget->show(); resizeChildren(); update(); - return mapentity; + return entitywidget; } } |