diff options
author | Stijn Buys <ingar@osirion.org> | 2012-02-19 14:50:24 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2012-02-19 14:50:24 +0000 |
commit | 7a9c504720e494ee07d16a6234b77c500af4da93 (patch) | |
tree | f7e66125b2c4c39f2007823c27dcc9b13d2b927b | |
parent | 46f48143ed59eddafa86f2711f518792f362a46c (diff) |
Renamed class MapEntity to EntityWidget, added entity dragging.
-rw-r--r-- | src/Makefile.am | 6 | ||||
-rw-r--r-- | src/editorwindow.cc | 6 | ||||
-rw-r--r-- | src/entitywidget.cc (renamed from src/mapentity.cc) | 44 | ||||
-rw-r--r-- | src/entitywidget.h (renamed from src/mapentity.h) | 34 | ||||
-rw-r--r-- | src/mapwidget.cc | 69 | ||||
-rw-r--r-- | src/mapwidget.h | 25 | ||||
-rw-r--r-- | src/sidebar.cc | 6 | ||||
-rw-r--r-- | src/sidebar.h | 6 |
8 files changed, 143 insertions, 53 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index d556a38..aa2355c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -5,9 +5,9 @@ bin_PROGRAMS = editor editor_SOURCES = \ editor.cc \ editorwindow.cc \ + entitywidget.cc \ inistream.cc \ mainwindow.cc \ - mapentity.cc \ mapwidget.cc \ sidebar.cc @@ -15,11 +15,11 @@ editor_SOURCES = \ # its mocced name, moc_editor.cpp. nodist_editor_SOURCES = \ moc_editorwindow.cc \ + moc_entitywidget.cc \ moc_mainwindow.cc \ - moc_mapentity.cc \ moc_mapwidget.cc \ moc_sidebar.cc - + # This rule lets GNU make create any moc_*.cpp from the equivalent *.h moc_%.cc: %.h moc $< -o $@ diff --git a/src/editorwindow.cc b/src/editorwindow.cc index 3f1dce7..ec7ebe2 100644 --- a/src/editorwindow.cc +++ b/src/editorwindow.cc @@ -6,9 +6,9 @@ */ #include "editorwindow.h" +#include "entitywidget.h" #include "inistream.h" #include "mapwidget.h" -#include "mapentity.h" #include "sidebar.h" #include <QtGui> @@ -24,7 +24,7 @@ EditorWindow::EditorWindow(QWidget *parent) : QWidget(parent) editorwindow_mapwidget = new MapWidget(this); editorwindow_sidebar = new SideBar(this); - connect(editorwindow_mapwidget, SIGNAL(selected(MapEntity *)), editorwindow_sidebar, SLOT(setEntity(MapEntity *))); + connect(editorwindow_mapwidget, SIGNAL(selected(EntityWidget *)), editorwindow_sidebar, SLOT(setEntity(EntityWidget *))); connect(editorwindow_sidebar, SIGNAL(entityChanged()), editorwindow_mapwidget, SLOT(resizeChildren())); } @@ -53,7 +53,7 @@ bool EditorWindow::loadFile(const QString &filename) IniStream ini; - MapEntity *entity = 0; + EntityWidget *entity = 0; bool in_entity = false; float x, y, z; float f; diff --git a/src/mapentity.cc b/src/entitywidget.cc index 0a17e63..f04c177 100644 --- a/src/mapentity.cc +++ b/src/entitywidget.cc @@ -1,11 +1,11 @@ /* - mapentity.cc + entitywidget.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 <mapentity.h> +#include <entitywidget.h> #include <QPainter> #include <QMouseEvent> @@ -14,47 +14,48 @@ namespace editor { -MapEntity::MapEntity(QWidget *parent) : QWidget(parent) +EntityWidget::EntityWidget(QWidget *parent) : QWidget(parent) { is_selected = false; + is_dragging = false; entity_radius = 0; } -void MapEntity::set_selected(const bool selected) +void EntityWidget::set_selected(const bool selected) { is_selected = selected; update(); } -void MapEntity::set_label(const QString &label) +void EntityWidget::set_label(const QString &label) { entity_label = label; } -void MapEntity::set_name(const QString &name) +void EntityWidget::set_name(const QString &name) { entity_name = name; } -void MapEntity::set_radius(const float radius) +void EntityWidget::set_radius(const float radius) { entity_radius = radius; } -void MapEntity::set_location(const float x, const float y, const float z) +void EntityWidget::set_location(const float x, const float y, const float z) { entity_location[0] = x; entity_location[1] = y; entity_location[2] = z; } -void MapEntity::set_properties(const QString &properties) +void EntityWidget::set_properties(const QString &properties) { entity_properties = properties; } -void MapEntity::add_property(const QString &key, const QString &value) +void EntityWidget::add_property(const QString &key, const QString &value) { if (entity_properties.size()) { entity_properties += '\n'; @@ -64,7 +65,7 @@ void MapEntity::add_property(const QString &key, const QString &value) entity_properties += value; } -void MapEntity::paintEvent(QPaintEvent *event) +void EntityWidget::paintEvent(QPaintEvent *event) { QPen pen(Qt::black, 1, Qt::SolidLine); QPainter painter(this); @@ -78,13 +79,30 @@ void MapEntity::paintEvent(QPaintEvent *event) painter.drawEllipse(0, 0, width() - 1 , height() - 1); } -void MapEntity::mousePressEvent(QMouseEvent *event) +void EntityWidget::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { //qDebug() << "clicked entity " << name(); event->accept(); - + is_dragging = true; emit clicked(this); + } else { + event->ignore(); + is_dragging = false; + } +} + +void EntityWidget::mouseReleaseEvent(QMouseEvent *event) +{ + if (event->button() == Qt::LeftButton) { + is_dragging = false; + } +} + +void EntityWidget::mouseMoveEvent(QMouseEvent *event) +{ + if (is_dragging) { + emit dragged(this, event->pos().x(), event->pos().y()); } } diff --git a/src/mapentity.h b/src/entitywidget.h index a7b5a3a..04a925b 100644 --- a/src/mapentity.h +++ b/src/entitywidget.h @@ -1,12 +1,12 @@ /* - mapentity.h + entitywidget.h 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 */ -#ifndef __INCLUDED_EDITOR_MAPENTITY__ -#define __INCLUDED_EDITOR_MAPENTITY__ +#ifndef __INCLUDED_EDITOR_ENTITYWIDGET__ +#define __INCLUDED_EDITOR_ENTITYWIDGET__ #include <QWidget> #include <QList> @@ -16,14 +16,14 @@ namespace editor { /** - * @brief MapEntity is an entity on the map + * @brief a Widget resembling an entity on the map * */ -class MapEntity : public QWidget +class EntityWidget : public QWidget { Q_OBJECT public: - MapEntity(QWidget *parent = 0); + EntityWidget(QWidget *parent = 0); /** * @brief returns the entity label @@ -62,9 +62,14 @@ public: signals: /** - * @brief the clicked() signal is emitted if the entity is clicked with the left mouse button + * @brief this signal is emitted if the entity is clicked with the left mouse button * */ - void clicked(MapEntity *entity); + void clicked(EntityWidget *entity); + + /** + * @brief this signal is emitted if the entity is dragged + * */ + void dragged(EntityWidget *entity, int x, int y); public slots: @@ -113,9 +118,20 @@ protected: * @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); private: bool is_selected; + bool is_dragging; QString entity_label; QString entity_name; @@ -131,4 +147,4 @@ private: } -#endif // __INCLUDED_EDITOR_MAPENTITY__ +#endif // __INCLUDED_EDITOR_ENTITYWIDGET__ 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; } } diff --git a/src/mapwidget.h b/src/mapwidget.h index 5bc3cb2..531eb4d 100644 --- a/src/mapwidget.h +++ b/src/mapwidget.h @@ -14,7 +14,7 @@ namespace editor { -class MapEntity; +class EntityWidget; /** * @brief MapWidget shows the zone map with the blue grid line @@ -29,7 +29,7 @@ public: /** * @brief add an entity to the map * */ - MapEntity *addEntity(); + EntityWidget *addEntity(); protected: @@ -62,12 +62,17 @@ protected: * @brief handle mousewheel events * */ virtual void wheelEvent(QWheelEvent *event); + + /** + * @brief handle keypress events + * */ + virtual void keyPressEvent(QKeyEvent *event); signals: /** * @brief the selected() signal is emitted if an entity on the map is selected * */ - void selected(MapEntity *entity); + void selected(EntityWidget *entity); public slots: /** @@ -80,7 +85,17 @@ private slots: /** * @brief called when an entity on the map has been clicked * */ - void select(MapEntity *entity); + void select(EntityWidget *entity); + + /** + * @brief clear current selection + * */ + void deselect(); + + /** + * @brief entity is dragged + * */ + void dragEntity(EntityWidget *entity, int x, int y); private: int mapwidget_zoom; @@ -92,7 +107,7 @@ private: bool is_dragging; - QList<MapEntity *> mapwidget_enties; + QList<EntityWidget *> mapwidget_enties; }; } diff --git a/src/sidebar.cc b/src/sidebar.cc index 65fffb4..6298589 100644 --- a/src/sidebar.cc +++ b/src/sidebar.cc @@ -11,7 +11,7 @@ #include <QTextEdit> #include "sidebar.h" -#include "mapentity.h" +#include "entitywidget.h" namespace editor { @@ -69,7 +69,7 @@ SideBar::SideBar(QWidget *parent) : QWidget(parent) setLayout(box_global); - last_selected = 0; + setEntity(0); } void SideBar::updateEntityLocationX(const QString &value) @@ -108,7 +108,7 @@ void SideBar::updateEntityLocationZ(const QString &value) } } -void SideBar::setEntity(MapEntity *entity) +void SideBar::setEntity(EntityWidget *entity) { QString value; diff --git a/src/sidebar.h b/src/sidebar.h index 78a62f2..48fcafe 100644 --- a/src/sidebar.h +++ b/src/sidebar.h @@ -17,7 +17,7 @@ class QTextEdit; namespace editor { -class MapEntity; +class EntityWidget; /** * @brief Sidebar is the EditorWindow sidebar @@ -32,7 +32,7 @@ public: SideBar(QWidget *parent = 0); public slots: - void setEntity(MapEntity *entity); + void setEntity(EntityWidget *entity); void setZoneName(const QString &name); @@ -54,7 +54,7 @@ private: QLineEdit *edit_entitylocation_z; QTextEdit *text_entityproperties; - MapEntity *last_selected; + EntityWidget *last_selected; }; } |