diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2020-09-21 15:38:11 +0200 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2020-09-21 15:38:11 +0200 |
| commit | d3e70eb68019f7d4e866dbda6ee8463ec5ad901b (patch) | |
| tree | c42f4ab3f1f8651febdaa1102172544c591d5fbd /labb3/tiles | |
| parent | aa9d7f6543982618e0b4c72cfbfd3ebfeda293c1 (diff) | |
| download | tddd86-d3e70eb68019f7d4e866dbda6ee8463ec5ad901b.tar.gz | |
Given files L3 tiles
Diffstat (limited to 'labb3/tiles')
| -rw-r--r-- | labb3/tiles/Tile.cpp | 31 | ||||
| -rw-r--r-- | labb3/tiles/Tile.h | 57 | ||||
| -rw-r--r-- | labb3/tiles/TileList.cpp | 52 | ||||
| -rw-r--r-- | labb3/tiles/TileList.h | 29 | ||||
| -rw-r--r-- | labb3/tiles/Tiles.pro | 13 | ||||
| -rw-r--r-- | labb3/tiles/mainwindow.cpp | 129 | ||||
| -rw-r--r-- | labb3/tiles/mainwindow.h | 35 |
7 files changed, 346 insertions, 0 deletions
diff --git a/labb3/tiles/Tile.cpp b/labb3/tiles/Tile.cpp new file mode 100644 index 0000000..dc16cf6 --- /dev/null +++ b/labb3/tiles/Tile.cpp @@ -0,0 +1,31 @@ +/* + * TDDD86 Tiles + * This file contains the implementation of the Tile structure. + * See Tile.h for comments about each member. + * Your code should work properly with an unmodified version of this file. + */ + +#include <QGraphicsRectItem> +#include "Tile.h" + +bool Tile::contains(int x, int y) const { + return this->x <= x && x < this->x + this->width && + this->y <= y && y < this->y + this->height; +} + +void Tile::draw(QGraphicsScene *scene) const { + QGraphicsRectItem *item = new QGraphicsRectItem(x, y, width, height); + item->setBrush(QBrush(QColor(r, g, b))); + scene->addItem(item); +} + +string Tile::toString() const { + return "Tile{x=" + to_string(x) + ",y=" + to_string(y) + + ",width=" + to_string(width) + ",height=" + to_string(height) + + ",r=" + to_string(r) + ",g=" +to_string (g) + ",b=" + to_string(b) + "}"; +} + +ostream& operator<<(ostream& out, const Tile& tile) { + out << tile.toString(); + return out; +} diff --git a/labb3/tiles/Tile.h b/labb3/tiles/Tile.h new file mode 100644 index 0000000..5705bf1 --- /dev/null +++ b/labb3/tiles/Tile.h @@ -0,0 +1,57 @@ +/* + * TDDD86 Tiles + * This file contains the declaration of the Tile structure. + * See Tile.cpp for implementation of each member. + * Your code should work properly with an unmodified version of this file. + */ + +#ifndef TILE_H +#define TILE_H + +#include <iostream> +#include <string> +#include <QGraphicsScene> +using namespace std; + +/* + * Each Tile structure represents a single rectangular tile in the simulation. + * A tile knows its position, size, and color. + * It contains members that can draw itself, tell you whether it touches a + * given x/y pixel, and print it on the console for debugging. + */ +struct Tile { +public: + int x = 0; // x position of tile's top-left corner + int y = 0; // y position of tile's top-left corner + int width = 0; // width of tile in pixels + int height = 0; // height of tile in pixels + int r = 0; // R component of tile's color + int g = 0; // G component of tile's color + int b = 0; // B component of tile's color + + /* + * Returns true if this Tile touches the given x/y pixel. + * For example, if (x, y) is (10, 20) and width x height are 5 x 30, + * the tile returns true for (10 <= x <= 14, 20 <= y <= 49) inclusive. + */ + bool contains(int x, int y) const; + + /* + * Draws the given tile onto the given QGraphicsScene. + */ + void draw(QGraphicsScene* scene) const; + + /* + * Returns a string representation of the tile, such as + * "Tile{x=10,y=20,width=5,height=30,r=255,g=255,b=255}". + */ + string toString() const; +}; + +/* + * Outputs the given tile to the given output stream (e.g. cout) in the same + * format as returned by its toString member function. + */ +ostream& operator<<(ostream& out, const Tile& tile); + +#endif // TILE_H diff --git a/labb3/tiles/TileList.cpp b/labb3/tiles/TileList.cpp new file mode 100644 index 0000000..29a309b --- /dev/null +++ b/labb3/tiles/TileList.cpp @@ -0,0 +1,52 @@ +// This is the .cpp file you will edit and turn in. +// We have provided a skeleton for you, +// but you must finish it as described in the spec. +// Also remove these comments here and add your own. +// TODO: remove this comment header + +#include "TileList.h" + +TileList::TileList() +{ + // TODO: write this member +} + +TileList::~TileList() +{ + // TODO: write this member +} + +void TileList::addTile(Tile tile) +{ + // TODO: write this member +} + +void TileList::drawAll(QGraphicsScene* scene) +{ + // TODO: write this member +} + +int TileList::indexOfTopTile(int x, int y) +{ + // TODO: write this member +} + +void TileList::raise(int x, int y) +{ + // TODO: write this member +} + +void TileList::lower(int x, int y) +{ + // TODO: write this member +} + +void TileList::remove(int x, int y) +{ + // TODO: write this member +} + +void TileList::removeAll(int x, int y) +{ + // TODO: write this member +} diff --git a/labb3/tiles/TileList.h b/labb3/tiles/TileList.h new file mode 100644 index 0000000..a4b6ce6 --- /dev/null +++ b/labb3/tiles/TileList.h @@ -0,0 +1,29 @@ +// This is the .h file you will edit and turn in. +// We have provided a skeleton for you, +// but you must finish it as described in the spec. +// Also remove these comments here and add your own, as well as on the members. +// TODO: remove this comment header + +#ifndef TILELIST_H +#define TILELIST_H + +#include <QGraphicsScene> +#include "Tile.h" + +class TileList { +public: + TileList(); + ~TileList(); + void addTile(Tile tile); + void drawAll(QGraphicsScene* scene); + int indexOfTopTile(int x, int y); + void lower(int x, int y); + void raise(int x, int y); + void remove(int x, int y); + void removeAll(int x, int y); + +private: + +}; + +#endif // TILELIST_H diff --git a/labb3/tiles/Tiles.pro b/labb3/tiles/Tiles.pro new file mode 100644 index 0000000..ca4cdac --- /dev/null +++ b/labb3/tiles/Tiles.pro @@ -0,0 +1,13 @@ +TEMPLATE = app +QT += widgets +HEADERS += mainwindow.h \ + Tile.h \ + TileList.h +SOURCES += mainwindow.cpp \ + tilemain.cpp \ + Tile.cpp \ + TileList.cpp +QMAKE_CXXFLAGS += -std=c++11 +macx { + cache() +} diff --git a/labb3/tiles/mainwindow.cpp b/labb3/tiles/mainwindow.cpp new file mode 100644 index 0000000..461d84a --- /dev/null +++ b/labb3/tiles/mainwindow.cpp @@ -0,0 +1,129 @@ +/* + * TDDD86 Tiles + * This file contains the implementation of the mainwindow class. + * See mainwindow.h for comments about each member. + */ + +#include <QPointF> +#include <random> +#include "mainwindow.h" +#include "Tile.h" +#include "TileList.h" + +// constants +static const int SCENE_WIDTH = 600; +static const int SCENE_HEIGHT = 400; +static const int MIN_SIZE = 20; +static const int MAX_SIZE = 100; +static const int MIN_COLOR = 50; +static const int MAX_COLOR = 255; +static const int TILE_COUNT = 50; +static const bool RANDOM = false; // set to false for repeatable output + +MainWindow::MainWindow(QWidget *parent) : + QGraphicsView(parent) +{ + // setup graphical window + scene = new QGraphicsScene(); + this->setScene(scene); + this->setSceneRect(0, 0, SCENE_WIDTH, SCENE_HEIGHT); + this->setFixedSize(sizeHint()); + + // place several random tiles on the screen + this->setWindowTitle("Tiles"); + for (int i = 0; i < TILE_COUNT; ++i) { + addRandomTile(tlist); + } + tlist.drawAll(scene); +} + +/* + * Listens to mouse press events from the graphical subsystem, + * and handles the events appropriately: + * - A left-click is a 'raise' action. + * - A shift-left-click is a 'lower' action. + * - A right-click (or ctrl-click) is a 'delete' action. + * - A shift-right-click (or shift-ctrl-click) is a 'delete all' action. + */ +void MainWindow::mousePressEvent(QMouseEvent *e) +{ + QPointF pt = mapToScene(e->pos()); + bool modified = false; + switch (e->button()) { + case Qt::LeftButton: + switch (e->modifiers()) { + case Qt::NoModifier: + tlist.raise(pt.x(), pt.y()); + modified = true; + break; + case Qt::ShiftModifier: + tlist.lower(pt.x(), pt.y()); + modified = true; + break; + } + break; + case Qt::RightButton: + switch (e->modifiers()) { + case Qt::NoModifier: + tlist.remove(pt.x(), pt.y()); + modified = true; + break; + case Qt::ShiftModifier: + tlist.removeAll(pt.x(), pt.y()); + modified = true; + break; + } + break; + default: + QGraphicsView::mousePressEvent(e); + } + if (modified) { + scene->clear(); + tlist.drawAll(scene); + } +} + +/* + * Listens to key press events from the graphical subsystem, + * and handles the events appropriately: + * - 'N' creates a new random tile. + */ +void MainWindow::keyPressEvent(QKeyEvent *e) +{ + switch (e->key()) { + case Qt::Key_N: + addRandomTile(tlist); + scene->clear(); + tlist.drawAll(scene); + break; + default: + QGraphicsView::keyPressEvent(e); + } +} + + +/* + * Creates a new tile with a random x/y position, width, height, and color, + * and adds it to the given tile list. + */ +void MainWindow::addRandomTile(TileList &tlist) +{ + Tile tile; + std::random_device rd; + + // possibly use the same random numbers every time for testing + static std::default_random_engine e(RANDOM ? rd() : 42); + + std::uniform_int_distribution<unsigned> u1(MIN_COLOR, MAX_COLOR); + std::uniform_int_distribution<unsigned> u2(MIN_SIZE, MAX_SIZE); + tile.width = u2(e); + tile.height = u2(e); + std::uniform_int_distribution<unsigned> u3(0, SCENE_WIDTH - tile.width - 1); + std::uniform_int_distribution<unsigned> u4(0, SCENE_HEIGHT - tile.height - 1); + tile.x = u3(e); + tile.y = u4(e); + tile.r = u1(e); + tile.g = u1(e); + tile.b = u1(e); + tlist.addTile(tile); +} diff --git a/labb3/tiles/mainwindow.h b/labb3/tiles/mainwindow.h new file mode 100644 index 0000000..cbecb7c --- /dev/null +++ b/labb3/tiles/mainwindow.h @@ -0,0 +1,35 @@ +/* + * TDDD86 Tiles + * This file contains the declaration of the mainwindow class. + * See mainwindow.cpp for implementation of each member. + * Your code should work properly with an unmodified version of this file. + */ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include <QGraphicsView> +#include <QGraphicsScene> +#include <QMouseEvent> +#include "TileList.h" + +class MainWindow : public QGraphicsView +{ + Q_OBJECT +public: + explicit MainWindow(QWidget *parent = 0); + +signals: + +public slots: + void mousePressEvent(QMouseEvent *e); + void keyPressEvent(QKeyEvent *k); + +private: + void addRandomTile(TileList& tlist); + + QGraphicsScene* scene; + TileList tlist; +}; + +#endif // MAINWINDOW_H |
