diff options
| author | Ben Boeckel <MathStuf@gmail.com> | 2009-04-02 02:48:05 -0400 |
|---|---|---|
| committer | Ben Boeckel <MathStuf@gmail.com> | 2009-04-02 02:48:05 -0400 |
| commit | 5eed3bab26059b73d2ff38522e560fef6b9d2680 (patch) | |
| tree | 2c54b18914249cb0d95bb6c04e3008add0856421 | |
| parent | b7c79190fcf43d64a72ec672f66bac09508b6e13 (diff) | |
Add world map editor
| -rw-r--r-- | sigmodr/widgets/mapeditor/WorldMapEditor.cpp | 110 | ||||
| -rw-r--r-- | sigmodr/widgets/mapeditor/WorldMapEditor.h | 77 | ||||
| -rw-r--r-- | sigmodr/widgets/mapeditor/WorldMapItem.cpp | 148 | ||||
| -rw-r--r-- | sigmodr/widgets/mapeditor/WorldMapItem.h | 87 | ||||
| -rw-r--r-- | sigmodr/widgets/mapeditor/WorldMapScene.cpp | 49 | ||||
| -rw-r--r-- | sigmodr/widgets/mapeditor/WorldMapScene.h | 58 |
6 files changed, 529 insertions, 0 deletions
diff --git a/sigmodr/widgets/mapeditor/WorldMapEditor.cpp b/sigmodr/widgets/mapeditor/WorldMapEditor.cpp new file mode 100644 index 00000000..d212e21b --- /dev/null +++ b/sigmodr/widgets/mapeditor/WorldMapEditor.cpp @@ -0,0 +1,110 @@ +/* + * Copyright 2008-2009 Ben Boeckel <MathStuf@gmail.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +// Header include +#include "WorldMapEditor.h" + +// Sigmodr widget includes +#include "MapGrid.h" +#include "WorldMapScene.h" + +// Sigmod includes +#include <sigmod/Game.h> + +// KDE includes +#include <KDoubleNumInput> +#include <KIntNumInput> + +// Qt includes +#include <QtCore/QFile> +#include <QtGui/QCheckBox> +#include <QtGui/QGraphicsView> +#include <QtGui/QVBoxLayout> +#include <QtUiTools/QUiLoader> + +using namespace Sigmod; +using namespace Sigmodr::Widgets; + +WorldMapEditor::WorldMapEditor(Game* game, QWidget* parent) : + QWidget(parent), + m_game(game), + m_scene(NULL), + m_grid(NULL) +{ + setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding); + QFile file(":/gui/worldmapeditor.ui"); + file.open(QFile::ReadOnly); + QWidget *formWidget = QUiLoader().load(&file, this); + file.close(); + ui_gridSize = formWidget->findChild<KIntNumInput*>("varGridSize"); + ui_zoom = formWidget->findChild<KDoubleNumInput*>("varZoom"); + ui_drawMasked = formWidget->findChild<QCheckBox*>("varDrawMasked"); + ui_watchCollisions = formWidget->findChild<QCheckBox*>("varWatchCollisions"); + ui_view = formWidget->findChild<QGraphicsView*>("varView"); + ui_zoom->setRange(.1, 10, .1); + connect(ui_gridSize, SIGNAL(valueChanged(int)), this, SLOT(setGridSize(int))); + connect(ui_zoom, SIGNAL(valueChanged(double)), this, SLOT(setZoom(double))); + reset(); + QVBoxLayout* layout = new QVBoxLayout; + layout->addWidget(formWidget); + setLayout(layout); +} + +void WorldMapEditor::setGame(Game* game) +{ + m_game = game; + delete m_grid; + delete m_scene; + reset(); +} + +void WorldMapEditor::reset() +{ + m_scene = new WorldMapScene(m_game, this); + m_grid = new MapGrid(0); + m_grid->setRect(m_scene->itemsBoundingRect()); + m_scene->addItem(m_grid); + ui_view->setScene(m_scene); + m_grid->setGridSize(ui_gridSize->value()); + ui_zoom->setValue(1); + ui_drawMasked->setChecked(false); + resizeGrid(m_scene->itemsBoundingRect()); + makeConnections(); +} + +void WorldMapEditor::setGridSize(const int gridSize) +{ + m_grid->setGridSize(gridSize); +} + +void WorldMapEditor::setZoom(const double zoom) +{ + ui_view->setMatrix(QMatrix().scale(zoom, zoom)); +} + +void WorldMapEditor::resizeGrid(const QRectF& rect) +{ + m_grid->setRect(rect.adjusted(5, 8, 0, 0)); +} + +void WorldMapEditor::makeConnections() +{ + connect(m_scene, SIGNAL(changed()), this, SIGNAL(changed())); + connect(m_scene, SIGNAL(sceneRectChanged(QRectF)), this, SLOT(resizeGrid(QRectF))); + connect(ui_drawMasked, SIGNAL(toggled(bool)), m_scene, SIGNAL(maskTiles(bool))); + connect(ui_watchCollisions, SIGNAL(toggled(bool)), m_scene, SIGNAL(watchCollisions(bool))); +} diff --git a/sigmodr/widgets/mapeditor/WorldMapEditor.h b/sigmodr/widgets/mapeditor/WorldMapEditor.h new file mode 100644 index 00000000..72e8185c --- /dev/null +++ b/sigmodr/widgets/mapeditor/WorldMapEditor.h @@ -0,0 +1,77 @@ +/* + * Copyright 2008-2009 Ben Boeckel <MathStuf@gmail.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef SIGMODRWIDGETS_WORLDMAPEDITOR +#define SIGMODRWIDGETS_WORLDMAPEDITOR + +// Sigmodr widget includes +#include "../Global.h" + +// Qt includes +#include <QtGui/QWidget> + +// Forward declarations +class QCheckBox; +class QGraphicsView; +class KDoubleNumInput; +class KIntNumInput; +namespace Sigmod +{ +class Game; +} + +namespace Sigmodr +{ +namespace Widgets +{ +class MapGrid; +class WorldMapScene; + +class SIGMODRWIDGETS_NO_EXPORT WorldMapEditor : public QWidget +{ + Q_OBJECT + + public: + WorldMapEditor(Sigmod::Game* game, QWidget* parent); + + void setGame(Sigmod::Game* game); + + void reset(); + signals: + void changed(); + protected slots: + void setGridSize(const int gridSize); + void setZoom(const double zoom); + + void resizeGrid(const QRectF& rect); + protected: + void makeConnections(); + private: + Sigmod::Game* m_game; + WorldMapScene* m_scene; + MapGrid* m_grid; + + KIntNumInput* ui_gridSize; + KDoubleNumInput* ui_zoom; + QCheckBox* ui_drawMasked; + QCheckBox* ui_watchCollisions; + QGraphicsView* ui_view; +}; +} +} + +#endif diff --git a/sigmodr/widgets/mapeditor/WorldMapItem.cpp b/sigmodr/widgets/mapeditor/WorldMapItem.cpp new file mode 100644 index 00000000..f0ed28ce --- /dev/null +++ b/sigmodr/widgets/mapeditor/WorldMapItem.cpp @@ -0,0 +1,148 @@ +/* + * Copyright 2008-2009 Ben Boeckel <MathStuf@gmail.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +// Header include +#include "WorldMapItem.h" + +// Sigmodr widget includes +#include "TileItem.h" + +// Sigmod includes +#include <sigmod/Game.h> +#include <sigmod/Map.h> + +// KDE includes +#include <KColorScheme> + +// Qt includes +#include <QtGui/QGraphicsScene> +#include <QtGui/QGraphicsSceneMouseEvent> +#include <QtGui/QGraphicsSimpleTextItem> +#include <QtGui/QPainter> +#include <QtGui/QStyleOptionGraphicsItem> + +using namespace Sigmod; +using namespace Sigmodr::Widgets; + +WorldMapItem::WorldMapItem(Game* game, Map* map, QGraphicsScene* parent) : + QObject(parent), + m_game(game), + m_map(map), + m_watchCollisions(false), + m_tag(new QGraphicsSimpleTextItem(this)), + m_label(new QGraphicsSimpleTextItem(this)) +{ + setAcceptHoverEvents(true); + setFlags(ItemIsMovable | ItemDoesntPropagateOpacityToChildren); + setOpacity(.5); + m_tag->setText(QString::number(m_map->id())); + m_tag->setPos(-5, -8); + m_label->hide(); + mapChanged(); +} + +WorldMapItem::~WorldMapItem() +{ + qDeleteAll(m_tiles); +} + +QRectF WorldMapItem::boundingRect() const +{ + return QRect(0, 0, m_map->width(), m_map->height()).adjusted(-1, -1, 1, 1); +} + +void WorldMapItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) +{ + Q_UNUSED(widget) + painter->setPen(QPen(Qt::black, 2)); + painter->drawRect(QRect(0, 0, m_map->width(), m_map->height())); + painter->setBrush(QBrush(Qt::yellow)); + if (option->state & QStyle::State_MouseOver) + { + painter->setPen(QPen(KStatefulBrush(KColorScheme::View, KColorScheme::HoverColor).brush(QPalette::Active), 3)); + painter->drawRect(boundingRect()); + } +} + +int WorldMapItem::type() const +{ + return Type; +} + +void WorldMapItem::watchCollisions(const bool watch) +{ + m_watchCollisions = watch; +} + +void WorldMapItem::hoverEnterEvent(QGraphicsSceneHoverEvent* event) +{ + m_label->show(); + QGraphicsItem::hoverEnterEvent(event); + update(); +} + +void WorldMapItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* event) +{ + m_label->hide(); + QGraphicsItem::hoverLeaveEvent(event); + update(); +} + +void WorldMapItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event) +{ + if (m_watchCollisions) + { + qDebug("Under development"); + setPos(m_game->mapPosition(m_map->id())); + event->ignore(); + } + else + { + QGraphicsItem::mouseMoveEvent(event); + moveTo(scenePos().toPoint()); + update(); + } +} + +void WorldMapItem::moveTo(const QPoint& point) +{ + m_game->setMapPosition(m_map->id(), point); +} + +void WorldMapItem::mapChanged() +{ + qDeleteAll(m_tiles); + m_tiles.clear(); + for (int i = 0; i < m_map->tileCount(); ++i) + { + TileItem* item = new TileItem(m_map, m_map->tile(i), NULL); + item->setAcceptHoverEvents(false); + item->setFlag(ItemIsMovable, false); + item->setFlag(ItemIsSelectable, false); + addToGroup(item); + m_tiles.append(item); + connect(this, SIGNAL(maskTiles(bool)), item, SLOT(drawCollisionMask(bool))); + } + resetLabel(); +} + +void WorldMapItem::resetLabel() +{ + m_label->setText(m_map->name()); + QSizeF size = m_label->boundingRect().size() / 2 - boundingRect().size() / 2; + m_label->setPos(-size.width(), -size.height()); +} diff --git a/sigmodr/widgets/mapeditor/WorldMapItem.h b/sigmodr/widgets/mapeditor/WorldMapItem.h new file mode 100644 index 00000000..1aef1252 --- /dev/null +++ b/sigmodr/widgets/mapeditor/WorldMapItem.h @@ -0,0 +1,87 @@ +/* + * Copyright 2008-2009 Ben Boeckel <MathStuf@gmail.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef SIGMODRWIDGETS_WORLDMAPITEM +#define SIGMODRWIDGETS_WORLDMAPITEM + +// Sigmodr widget includes +#include "../Global.h" + +// Qt includes +#include <QtCore/QObject> +#include <QtGui/QGraphicsItemGroup> + +// Forward declarations +class QGraphicsScene; +namespace Sigmod +{ +class Game; +class Map; +} + +namespace Sigmodr +{ +namespace Widgets +{ +class TileItem; + +class SIGMODRWIDGETS_NO_EXPORT WorldMapItem : public QObject, public QGraphicsItemGroup +{ + Q_OBJECT + + public: + enum + { + Type = UserType + 4 + }; + + WorldMapItem(Sigmod::Game* game, Sigmod::Map* map, QGraphicsScene* parent); + ~WorldMapItem(); + + QRectF boundingRect() const; + + void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget); + + int type() const; + public slots: + void watchCollisions(const bool watch); + signals: + void changed(); + + void maskTiles(const bool mask); + protected: + void hoverEnterEvent(QGraphicsSceneHoverEvent* event); + void hoverLeaveEvent(QGraphicsSceneHoverEvent* event); + void mouseMoveEvent(QGraphicsSceneMouseEvent* event); + + void moveTo(const QPoint& point); + protected slots: + void mapChanged(); + private: + void resetLabel(); + + Sigmod::Game* m_game; + Sigmod::Map* m_map; + bool m_watchCollisions; + QList<TileItem*> m_tiles; + QGraphicsSimpleTextItem* m_tag; + QGraphicsSimpleTextItem* m_label; +}; +} +} + +#endif diff --git a/sigmodr/widgets/mapeditor/WorldMapScene.cpp b/sigmodr/widgets/mapeditor/WorldMapScene.cpp new file mode 100644 index 00000000..89502006 --- /dev/null +++ b/sigmodr/widgets/mapeditor/WorldMapScene.cpp @@ -0,0 +1,49 @@ +/* + * Copyright 2008-2009 Ben Boeckel <MathStuf@gmail.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +// Header include +#include "WorldMapScene.h" + +// Sigmodr widget includes +#include "WorldMapItem.h" + +// Sigmod includes +#include <sigmod/Game.h> +#include <sigmod/Map.h> + +// Qt includes +#include <QtGui/QGraphicsView> + +using namespace Sigmod; +using namespace Sigmodr::Widgets; + +WorldMapScene::WorldMapScene(Game* game, QObject* parent) : + QGraphicsScene(parent), + m_game(game) +{ + for (int i = 0; i < m_game->mapCount(); ++i) + { + Map* map = m_game->map(i); + // TODO: Check if the map is accessible on its edges + WorldMapItem* item = new WorldMapItem(m_game, map, this); + connect(item, SIGNAL(changed()), this, SIGNAL(changed())); + connect(this, SIGNAL(maskTiles(bool)), item, SIGNAL(maskTiles(bool))); + connect(this, SIGNAL(watchCollisions(bool)), item, SLOT(watchCollisions(bool))); + m_maps.append(item); + addItem(item); + } +} diff --git a/sigmodr/widgets/mapeditor/WorldMapScene.h b/sigmodr/widgets/mapeditor/WorldMapScene.h new file mode 100644 index 00000000..860de121 --- /dev/null +++ b/sigmodr/widgets/mapeditor/WorldMapScene.h @@ -0,0 +1,58 @@ +/* + * Copyright 2008-2009 Ben Boeckel <MathStuf@gmail.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef SIGMODRWIDGETS_WORLDMAPSCENE +#define SIGMODRWIDGETS_WORLDMAPSCENE + +// Sigmodr widget includes +#include "../Global.h" + +// Qt includes +#include <QtCore/QMap> +#include <QtGui/QGraphicsScene> + +// Forward declarations +namespace Sigmod +{ +class Game; +} + +namespace Sigmodr +{ +namespace Widgets +{ +class WorldMapItem; + +class SIGMODRWIDGETS_NO_EXPORT WorldMapScene : public QGraphicsScene +{ + Q_OBJECT + + public: + WorldMapScene(Sigmod::Game* game, QObject* parent); + signals: + void changed(); + + void maskTiles(const bool mask); + void watchCollisions(const bool watch); + private: + Sigmod::Game* m_game; + QList<WorldMapItem*> m_maps; +}; +} +} + +#endif |
