From 7a9c504720e494ee07d16a6234b77c500af4da93 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 19 Feb 2012 14:50:24 +0000 Subject: Renamed class MapEntity to EntityWidget, added entity dragging. --- src/Makefile.am | 6 +-- src/editorwindow.cc | 6 +-- src/entitywidget.cc | 109 ++++++++++++++++++++++++++++++++++++++ src/entitywidget.h | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/mapentity.cc | 91 ------------------------------- src/mapentity.h | 134 ---------------------------------------------- src/mapwidget.cc | 69 +++++++++++++++++++----- src/mapwidget.h | 25 +++++++-- src/sidebar.cc | 6 +-- src/sidebar.h | 6 +-- 10 files changed, 346 insertions(+), 256 deletions(-) create mode 100644 src/entitywidget.cc create mode 100644 src/entitywidget.h delete mode 100644 src/mapentity.cc delete mode 100644 src/mapentity.h 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 @@ -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/entitywidget.cc b/src/entitywidget.cc new file mode 100644 index 0000000..f04c177 --- /dev/null +++ b/src/entitywidget.cc @@ -0,0 +1,109 @@ +/* + 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 + +#include +#include +#include + +namespace editor +{ + +EntityWidget::EntityWidget(QWidget *parent) : QWidget(parent) +{ + is_selected = false; + is_dragging = false; + + entity_radius = 0; +} + +void EntityWidget::set_selected(const bool selected) +{ + is_selected = selected; + update(); +} + +void EntityWidget::set_label(const QString &label) +{ + entity_label = label; +} + +void EntityWidget::set_name(const QString &name) +{ + entity_name = name; +} + +void EntityWidget::set_radius(const float radius) +{ + entity_radius = radius; + +} + +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 EntityWidget::set_properties(const QString &properties) +{ + entity_properties = properties; +} +void EntityWidget::add_property(const QString &key, const QString &value) +{ + if (entity_properties.size()) { + entity_properties += '\n'; + } + entity_properties += key; + entity_properties += '='; + entity_properties += value; +} + +void EntityWidget::paintEvent(QPaintEvent *event) +{ + QPen pen(Qt::black, 1, Qt::SolidLine); + QPainter painter(this); + + if (is_selected) { + pen.setColor(Qt::red); + painter.setPen(pen); + } + + painter.setPen(pen); + painter.drawEllipse(0, 0, width() - 1 , height() - 1); +} + +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/entitywidget.h b/src/entitywidget.h new file mode 100644 index 0000000..04a925b --- /dev/null +++ b/src/entitywidget.h @@ -0,0 +1,150 @@ +/* + 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_ENTITYWIDGET__ +#define __INCLUDED_EDITOR_ENTITYWIDGET__ + +#include +#include +#include + +namespace editor +{ + +/** + * @brief a Widget resembling an entity on the map + * */ +class EntityWidget : public QWidget +{ + Q_OBJECT + +public: + EntityWidget(QWidget *parent = 0); + + /** + * @brief returns the entity label + * */ + inline const QString &label() const { + return entity_label; + } + + /** + * @brief returns the entity name + * */ + inline const QString &name() const { + return entity_name; + } + + /** + * @brief returns the entity radius + * */ + inline const float radius() const { + return entity_radius; + } + + /** + * @brief returns the x, y, or z coordinate of the entity location + * */ + inline const float location(int index) const { + return entity_location[index]; + } + + /** + * @brief returns the properties string + * */ + inline const QString &properties() const { + return entity_properties; + } + +signals: + /** + * @brief this signal is emitted if the entity is clicked with the left mouse button + * */ + void clicked(EntityWidget *entity); + + /** + * @brief this signal is emitted if the entity is dragged + * */ + void dragged(EntityWidget *entity, int x, int y); + +public slots: + + /** + * @brief set the entity name + * */ + void set_label(const QString &label); + + /** + * @brief set the entity label + * */ + void set_name(const QString &name); + + /** + * @brief set the entity radius + * */ + void set_radius(const float radius); + + /** + * @brief set the entity location + * */ + void set_location(const float x, const float y, const float z); + + /** + * @brief set the entity properties string + * */ + void set_properties(const QString &properties); + + /** + * @brief add a property + * */ + void add_property(const QString &key, const QString &value); + + /** + * @brief set the selected state + * */ + void set_selected(const bool selected); + +protected: + /** + * @brief handle draw events + * */ + virtual void paintEvent(QPaintEvent *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); + +private: + bool is_selected; + bool is_dragging; + + QString entity_label; + QString entity_name; + QString entity_type; + + QString entity_properties; + + float entity_location[3]; + float entity_radius; + + QColor entity_color; +}; + +} + +#endif // __INCLUDED_EDITOR_ENTITYWIDGET__ diff --git a/src/mapentity.cc b/src/mapentity.cc deleted file mode 100644 index 0a17e63..0000000 --- a/src/mapentity.cc +++ /dev/null @@ -1,91 +0,0 @@ -/* - mapentity.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 - -#include -#include -#include - -namespace editor -{ - -MapEntity::MapEntity(QWidget *parent) : QWidget(parent) -{ - is_selected = false; - - entity_radius = 0; -} - -void MapEntity::set_selected(const bool selected) -{ - is_selected = selected; - update(); -} - -void MapEntity::set_label(const QString &label) -{ - entity_label = label; -} - -void MapEntity::set_name(const QString &name) -{ - entity_name = name; -} - -void MapEntity::set_radius(const float radius) -{ - entity_radius = radius; - -} - -void MapEntity::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) -{ - entity_properties = properties; -} -void MapEntity::add_property(const QString &key, const QString &value) -{ - if (entity_properties.size()) { - entity_properties += '\n'; - } - entity_properties += key; - entity_properties += '='; - entity_properties += value; -} - -void MapEntity::paintEvent(QPaintEvent *event) -{ - QPen pen(Qt::black, 1, Qt::SolidLine); - QPainter painter(this); - - if (is_selected) { - pen.setColor(Qt::red); - painter.setPen(pen); - } - - painter.setPen(pen); - painter.drawEllipse(0, 0, width() - 1 , height() - 1); -} - -void MapEntity::mousePressEvent(QMouseEvent *event) -{ - if (event->button() == Qt::LeftButton) { - //qDebug() << "clicked entity " << name(); - event->accept(); - - emit clicked(this); - } -} - -} diff --git a/src/mapentity.h b/src/mapentity.h deleted file mode 100644 index a7b5a3a..0000000 --- a/src/mapentity.h +++ /dev/null @@ -1,134 +0,0 @@ -/* - mapentity.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__ - -#include -#include -#include - -namespace editor -{ - -/** - * @brief MapEntity is an entity on the map - * */ -class MapEntity : public QWidget -{ - Q_OBJECT - -public: - MapEntity(QWidget *parent = 0); - - /** - * @brief returns the entity label - * */ - inline const QString &label() const { - return entity_label; - } - - /** - * @brief returns the entity name - * */ - inline const QString &name() const { - return entity_name; - } - - /** - * @brief returns the entity radius - * */ - inline const float radius() const { - return entity_radius; - } - - /** - * @brief returns the x, y, or z coordinate of the entity location - * */ - inline const float location(int index) const { - return entity_location[index]; - } - - /** - * @brief returns the properties string - * */ - inline const QString &properties() const { - return entity_properties; - } - -signals: - /** - * @brief the clicked() signal is emitted if the entity is clicked with the left mouse button - * */ - void clicked(MapEntity *entity); - -public slots: - - /** - * @brief set the entity name - * */ - void set_label(const QString &label); - - /** - * @brief set the entity label - * */ - void set_name(const QString &name); - - /** - * @brief set the entity radius - * */ - void set_radius(const float radius); - - /** - * @brief set the entity location - * */ - void set_location(const float x, const float y, const float z); - - /** - * @brief set the entity properties string - * */ - void set_properties(const QString &properties); - - /** - * @brief add a property - * */ - void add_property(const QString &key, const QString &value); - - /** - * @brief set the selected state - * */ - void set_selected(const bool selected); - -protected: - /** - * @brief handle draw events - * */ - virtual void paintEvent(QPaintEvent *event); - - /** - * @brief handle mouse button press events - * */ - virtual void mousePressEvent(QMouseEvent *event); - -private: - bool is_selected; - - QString entity_label; - QString entity_name; - QString entity_type; - - QString entity_properties; - - float entity_location[3]; - float entity_radius; - - QColor entity_color; -}; - -} - -#endif // __INCLUDED_EDITOR_MAPENTITY__ 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 #include @@ -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 mapwidget_enties; + QList 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 #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; }; } -- cgit v1.2.3