/* mapwidget.cc This file is part of the Project::OSiRiON world editor and is distributed under the terms and conditions of the GNU General Public License version 2 */ #include "entitywidget.h" #include "mapwidget.h" #include #include #include #include #include namespace editor { MapWidget::MapWidget(QWidget *parent) : QWidget(parent) { //setMinimumSize(256,256); // zoom sucks but it works mapwidget_zoom = 16; // 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) { if (event->orientation() == Qt::Vertical) { int oldzoom = mapwidget_zoom; mapwidget_zoom -= event->delta() / 120; 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(); } } void MapWidget::resizeChildren() { 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->radius() * scale); if (radius < 6) radius = 6; int x = width() / 2 - (int) (entity->location(1) * scale) - radius; int y = height() / 2 - (int)(entity->location(0) * scale) - radius; entity->setGeometry(center_x + x, center_y + y, radius * 2 , radius * 2); //qDebug() << "Moving entity to " << x << " " << y; } } 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) { EntityWidget *entitywidget = mapwidget_enties.at(i); if (entity == entitywidget) { entity->set_selected(true); //qDebug() << "selected entity " << entity->name(); emit selected(entity); } else { entitywidget->set_selected(false); } } update(); } void MapWidget::keyPressEvent(QKeyEvent *event) { if (event->key() == Qt::Key_Escape) { deselect(); event->accept(); } else { event->ignore(); } } void MapWidget::resizeEvent(QResizeEvent *event) { resizeChildren(); } void MapWidget::paintEvent(QPaintEvent *event) { const QColor gridlinecolor(0, 0, 128); const QColor axislinecolor(128, 0, 0); QPen pen(gridlinecolor, 1, Qt::SolidLine); QPainter painter(this); painter.setPen(pen); // mapwidget_zoom is used to scale from game units to screen coorinates // 1 grid square is 256 game units // gridsize is the size of one grid square in screen coordinates int gridsize = width() / mapwidget_zoom; int x = center_x + width() / 2; pen.setColor(axislinecolor); painter.setPen(pen); painter.drawLine(x, 0, x, height()); x -= gridsize; pen.setColor(gridlinecolor); painter.setPen(pen); while (x > 0) { painter.drawLine(x, 0, x, height()); x -= gridsize; } x = center_x + width() / 2 + gridsize; while (x <= width()) { painter.drawLine(x, 0, x, height()); x += gridsize; } int y = center_y + height() / 2; pen.setColor(axislinecolor); painter.setPen(pen); painter.drawLine(0, y, width(), y); y -= gridsize; pen.setColor(gridlinecolor); painter.setPen(pen); while (y > 0) { painter.drawLine(0, y, width(), y); y -= gridsize; } y = center_y + height() / 2 + gridsize; while (y <= height()) { painter.drawLine(0, y, width(), y); y += gridsize; } QWidget::paintEvent(event); } EntityWidget *MapWidget::addEntity() { EntityWidget *entitywidget = new EntityWidget(this); mapwidget_enties.append(entitywidget); // 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))); entitywidget->show(); resizeChildren(); update(); return entitywidget; } }