From 619125885620076bf9b56dceb24d4ca63e2c6949 Mon Sep 17 00:00:00 2001 From: Vladislav Khorev Date: Mon, 4 Nov 2013 18:06:35 +0000 Subject: [PATCH] livelcreator added --- .../Black_okospace_levelcreator.pro | 22 ++ Black_okospace_levelcreator/customlabel.cpp | 32 +++ Black_okospace_levelcreator/customlabel.h | 30 +++ Black_okospace_levelcreator/leveldata.cpp | 173 ++++++++++++ Black_okospace_levelcreator/leveldata.h | 52 ++++ Black_okospace_levelcreator/main.cpp | 13 + Black_okospace_levelcreator/mainwindow.cpp | 166 ++++++++++++ Black_okospace_levelcreator/mainwindow.h | 56 ++++ Black_okospace_levelcreator/mainwindow.ui | 254 ++++++++++++++++++ 9 files changed, 798 insertions(+) create mode 100644 Black_okospace_levelcreator/Black_okospace_levelcreator.pro create mode 100644 Black_okospace_levelcreator/customlabel.cpp create mode 100644 Black_okospace_levelcreator/customlabel.h create mode 100644 Black_okospace_levelcreator/leveldata.cpp create mode 100644 Black_okospace_levelcreator/leveldata.h create mode 100644 Black_okospace_levelcreator/main.cpp create mode 100644 Black_okospace_levelcreator/mainwindow.cpp create mode 100644 Black_okospace_levelcreator/mainwindow.h create mode 100644 Black_okospace_levelcreator/mainwindow.ui diff --git a/Black_okospace_levelcreator/Black_okospace_levelcreator.pro b/Black_okospace_levelcreator/Black_okospace_levelcreator.pro new file mode 100644 index 0000000..25bfd63 --- /dev/null +++ b/Black_okospace_levelcreator/Black_okospace_levelcreator.pro @@ -0,0 +1,22 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2013-11-04T12:10:54 +# +#------------------------------------------------- + +QT += core gui + +TARGET = Black_okospace_levelcreator +TEMPLATE = app + + +SOURCES += main.cpp\ + mainwindow.cpp \ + leveldata.cpp \ + customlabel.cpp + +HEADERS += mainwindow.h \ + leveldata.h \ + customlabel.h + +FORMS += mainwindow.ui diff --git a/Black_okospace_levelcreator/customlabel.cpp b/Black_okospace_levelcreator/customlabel.cpp new file mode 100644 index 0000000..44a1c9d --- /dev/null +++ b/Black_okospace_levelcreator/customlabel.cpp @@ -0,0 +1,32 @@ +#include "customlabel.h" + +CustomLabel::CustomLabel(QWidget *parent) : + QLabel(parent) +{ +} + +void CustomLabel::mousePressEvent(QMouseEvent *ev) +{ + emit buttonPressed(ev->x(), ev->y()); +} + +void CustomLabel::mouseReleaseEvent(QMouseEvent * ev) +{ + + if (ev->button() == Qt::LeftButton) + { + emit leftButtonReleased(ev->x(), ev->y()); + } + if (ev->button() == Qt::RightButton) + { + emit rightButtonReleased(ev->x(), ev->y()); + } +} + +void CustomLabel::mouseMoveEvent(QMouseEvent *ev) +{ + if (ev->buttons() & Qt::RightButton) + { + emit mouseMoveWithRightButtonPressed(ev->x(), ev->y()); + } +} diff --git a/Black_okospace_levelcreator/customlabel.h b/Black_okospace_levelcreator/customlabel.h new file mode 100644 index 0000000..278e876 --- /dev/null +++ b/Black_okospace_levelcreator/customlabel.h @@ -0,0 +1,30 @@ +#ifndef CUSTOMLABEL_H +#define CUSTOMLABEL_H + +#include +#include + +class CustomLabel : public QLabel +{ + Q_OBJECT +public: + explicit CustomLabel(QWidget *parent = 0); + +signals: + void buttonPressed(int x, int y); + + void leftButtonReleased(int x, int y); + void rightButtonReleased(int x, int y); + void mouseMoveWithRightButtonPressed(int x, int y); + + +public slots: + +protected: + virtual void mousePressEvent(QMouseEvent *ev); + virtual void mouseReleaseEvent(QMouseEvent * ev); + virtual void mouseMoveEvent(QMouseEvent *ev); + +}; + +#endif // CUSTOMLABEL_H diff --git a/Black_okospace_levelcreator/leveldata.cpp b/Black_okospace_levelcreator/leveldata.cpp new file mode 100644 index 0000000..fae657d --- /dev/null +++ b/Black_okospace_levelcreator/leveldata.cpp @@ -0,0 +1,173 @@ +#include "leveldata.h" + +#include "mainwindow.h" + + #include + +const int CONST_SELECTION_DISTANCE = 10; + +LevelData::LevelData(QObject *parent) + : QObject(parent) +{ + ResetSelected(); +} + +LevelData::~LevelData() +{ + +} + + + +void LevelData::OnSelectPoint(int x, int y) +{ + selectedPoly = -1; + selectedPoint = -1; + + for (int i = 0; i < PolyArr.size(); i++) + { + for (int j=0; j < PolyArr[i].pointArr.size(); j++) + { + if (abs(PolyArr[i].pointArr[j].x() - x) < CONST_SELECTION_DISTANCE && abs(PolyArr[i].pointArr[j].y() - y) < CONST_SELECTION_DISTANCE) + { + selectedPoly = i; + selectedPoint = j; + } + + } + } + RefreshPixmap(); +} + +void LevelData::OnMovePoint(int x, int y) +{ + QPoint shift = QPoint(x, y) - LastMouseDownPos; + //QPoint shift = QPoint(x, y); + + if (selectedPoly != -1 && selectedPoint != -1) + { + PolyArr[selectedPoly].pointArr[selectedPoint] += shift; + } + + LastMouseDownPos = QPoint(x, y); + + RefreshPixmap(); +} + +void LevelData::OnAddPoint(int x, int y) +{ + tempPointArr.push_back(QPoint(x,y)); + RefreshPixmap(); +} + +void LevelData::RefreshPixmap() +{ + mainWindow->Pixmap->convertFromImage(mainWindow->bkgImage); + + QPainter pixPaint(mainWindow->Pixmap); + + for (size_t i = 0; i < tempPointArr.size(); i++) + { + QBrush brush(Qt::red); + QPen pen(Qt::red); + pixPaint.setBrush(brush); + pixPaint.setPen(pen); + pixPaint.drawEllipse(tempPointArr[i], 3, 3); + } + + for (size_t i = 0; i < PolyArr.size(); i++) + { + QBrush brush(QColor::fromRgb(255, 0, 255)); + QPen pen(QColor::fromRgb(255, 0, 255)); + pixPaint.setBrush(brush); + pixPaint.setPen(pen); + + QPoint point1; + QPoint point2; + + for (size_t j=0; j< PolyArr[i].pointArr.size()-1; j++) + { + point1 = PolyArr[i].pointArr[j]; + point2 = PolyArr[i].pointArr[j+1]; + pixPaint.drawLine(point1, point2); + } + + point1 = PolyArr[i].pointArr[PolyArr[i].pointArr.size()-1]; + point2 = PolyArr[i].pointArr[0]; + pixPaint.drawLine(point1, point2); + } + + if (selectedPoly != -1 && selectedPoint != -1) + { + QBrush brush(QColor::fromRgb(255, 0, 255)); + QPen pen(QColor::fromRgb(255, 0, 255)); + pixPaint.setBrush(brush); + pixPaint.setPen(pen); + + pixPaint.drawEllipse(PolyArr[selectedPoly].pointArr[selectedPoint], 3, 3); + } + + + mainWindow->SetImagePixmap(mainWindow->Pixmap); +} + +void LevelData::OnApplyPointList(int x, int y) +{ + if (tempPointArr.size() >= 3) + { + TPoly poly; + poly.pointArr = tempPointArr; + PolyArr.push_back(poly); + tempPointArr.clear(); + } + RefreshPixmap(); +} + +void LevelData::ResetSelected() +{ + selectedPoly = -1; + selectedPoint = -1; +} + + +void LevelData::ResetTempPoints() +{ + tempPointArr.clear(); +} + +void LevelData::Clear() +{ + tempPointArr.clear(); + PolyArr.clear(); +} + +void LevelData::OnMouseDown(int x, int y) +{ + LastMouseDownPos = QPoint(x, y); +} + +void LevelData::DeleteSelectedPoint() +{ + if (selectedPoly != -1 && selectedPoint != -1) + { + + PolyArr[selectedPoly].pointArr.erase(PolyArr[selectedPoly].pointArr.begin() + selectedPoint); + + if (PolyArr[selectedPoly].pointArr.size() < 3) + { + PolyArr.erase(PolyArr.begin() + selectedPoly); + } + } + + ResetSelected(); + RefreshPixmap(); +} + +void LevelData::DeleteLastTempPoint() +{ + if (tempPointArr.size() > 0) + { + tempPointArr.erase(tempPointArr.end()-1); + } + RefreshPixmap(); +} diff --git a/Black_okospace_levelcreator/leveldata.h b/Black_okospace_levelcreator/leveldata.h new file mode 100644 index 0000000..4ef7b76 --- /dev/null +++ b/Black_okospace_levelcreator/leveldata.h @@ -0,0 +1,52 @@ +#ifndef LEVELDATA_H +#define LEVELDATA_H + +#include +#include +#include + +class MainWindow; + +struct TPoly +{ + std::vector pointArr; +}; + +class LevelData : public QObject +{ + Q_OBJECT +public: + std::vector tempPointArr; + std::vector PolyArr; + + int selectedPoly; + int selectedPoint; + QPoint LastMouseDownPos; + + MainWindow* mainWindow; + + explicit LevelData(QObject *parent = 0); + + ~LevelData(); + + void RefreshPixmap(); + void ResetSelected(); + void ResetTempPoints(); + + void Clear(); + +public slots: + void OnSelectPoint(int x, int y); + void OnMovePoint(int x, int y); + + void OnAddPoint(int x, int y); + void OnApplyPointList(int x, int y); + + void OnMouseDown(int x, int y); + + void DeleteLastTempPoint(); + void DeleteSelectedPoint(); + +}; + +#endif // LEVELDATA_H diff --git a/Black_okospace_levelcreator/main.cpp b/Black_okospace_levelcreator/main.cpp new file mode 100644 index 0000000..339dd94 --- /dev/null +++ b/Black_okospace_levelcreator/main.cpp @@ -0,0 +1,13 @@ +#include +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + + w.InitSlots(); + + return a.exec(); +} diff --git a/Black_okospace_levelcreator/mainwindow.cpp b/Black_okospace_levelcreator/mainwindow.cpp new file mode 100644 index 0000000..bfe1852 --- /dev/null +++ b/Black_okospace_levelcreator/mainwindow.cpp @@ -0,0 +1,166 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" +#include + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); + + Pixmap = new QPixmap(960, 640); + levelData = new LevelData; + levelData->mainWindow = this; + + CurrentMode = CM_ADD_POLYS; + +} + +MainWindow::~MainWindow() +{ + delete Pixmap; + delete levelData; + delete ui; +} + +void MainWindow::InitSlots() +{ + + QObject::connect(ui->LoadBackgroundButton, SIGNAL(pressed()), this, SLOT(LoadBackgroundSlot())); + QObject::connect(ui->radioButtonAddPolys, SIGNAL(pressed()), this, SLOT(RadioButtonAddPolysSelected())); + QObject::connect(ui->radioButtonModifyPolys, SIGNAL(pressed()), this, SLOT(RadioButtonModifyPolysSelected())); + QObject::connect(ui->Image, SIGNAL(buttonPressed(int, int)), levelData, SLOT(OnMouseDown(int, int))); + + QObject::connect(ui->SaveLevelButton, SIGNAL(pressed()), this, SLOT(OnSaveButtonPressed())); + QObject::connect(ui->LoadLevelButton, SIGNAL(pressed()), this, SLOT(OnLoadButtonPressed())); + + InitSlotsForAddPolys(); +} + +void MainWindow::InitSlotsForAddPolys() +{ + QObject::disconnect(ui->Image, SIGNAL(leftButtonReleased(int, int)), levelData, SLOT(OnSelectPoint(int, int))); + QObject::disconnect(ui->Image, SIGNAL(mouseMoveWithRightButtonPressed(int, int)), levelData, SLOT(OnMovePoint(int, int))); + QObject::disconnect(this, SIGNAL(deleteButtonPressed()), levelData, SLOT(DeleteSelectedPoint())); + + + QObject::connect(ui->Image, SIGNAL(leftButtonReleased(int, int)), levelData, SLOT(OnAddPoint(int, int))); + QObject::connect(ui->Image, SIGNAL(rightButtonReleased(int, int)), levelData, SLOT(OnApplyPointList(int, int))); + QObject::connect(this, SIGNAL(deleteButtonPressed()), levelData, SLOT(DeleteLastTempPoint())); + +} + +void MainWindow::InitSlotsForModifyPolys() +{ + QObject::disconnect(ui->Image, SIGNAL(leftButtonReleased(int, int)), levelData, SLOT(OnAddPoint(int, int))); + QObject::disconnect(ui->Image, SIGNAL(rightButtonReleased(int, int)), levelData, SLOT(OnApplyPointList(int, int))); + QObject::disconnect(this, SIGNAL(deleteButtonPressed()), levelData, SLOT(DeleteLastTempPoint())); + + QObject::connect(ui->Image, SIGNAL(leftButtonReleased(int, int)), levelData, SLOT(OnSelectPoint(int, int))); + QObject::connect(ui->Image, SIGNAL(mouseMoveWithRightButtonPressed(int, int)), levelData, SLOT(OnMovePoint(int, int))); + QObject::connect(this, SIGNAL(deleteButtonPressed()), levelData, SLOT(DeleteSelectedPoint())); + +} + +void MainWindow::LoadBackgroundSlot() +{ + QFileDialog dialog(this); + + dialog.setNameFilter(tr("PNG (*.png)")); + + dialog.setFileMode(QFileDialog::ExistingFile); + + if (dialog.exec()) + { + QString filename = (dialog.selectedFiles())[0]; + bkgImage = QImage(filename); + + levelData->RefreshPixmap(); + //ui->Image->setPixmap(*Pixmap); + } +} + +void MainWindow::RadioButtonAddPolysSelected() +{ + CurrentMode = CM_ADD_POLYS; + InitSlotsForAddPolys(); + levelData->ResetSelected(); + levelData->RefreshPixmap(); +} + +void MainWindow::RadioButtonModifyPolysSelected() +{ + CurrentMode = CM_MODIFY_POLYS; + InitSlotsForModifyPolys(); + levelData->ResetTempPoints(); + levelData->RefreshPixmap(); +} + +void MainWindow::OnSaveButtonPressed() +{ + QFileDialog dialog(this); + + dialog.setNameFilter(tr("xml (*.xml)")); + + dialog.setFileMode(QFileDialog::AnyFile); + + if (dialog.exec()) + { + QString filename = (dialog.selectedFiles())[0]; + + QString xmlOut; + + QXmlStreamWriter stream(&xmlOut); + stream.setAutoFormatting(true); + stream.writeStartDocument(); + + stream.writeStartElement("LevelPolyArr"); + + for (size_t i = 0; i < levelData->PolyArr.size(); i++) + { + stream.writeStartElement("LevelPoly"); + + for (size_t j = 0; j < levelData->PolyArr[i].pointArr.size(); j++) + { + stream.writeStartElement("Point"); + stream.writeAttribute("x", QString::number(levelData->PolyArr[i].pointArr[j].x())); + stream.writeAttribute("y", QString::number(levelData->PolyArr[i].pointArr[j].y())); + stream.writeEndElement(); + } + + stream.writeEndElement(); + } + + stream.writeEndElement(); + + stream.writeEndDocument(); + + QFile file(filename); + if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) + return; + + QTextStream out(&file); + out << xmlOut; + + file.close(); + } + +} + +void MainWindow::OnLoadButtonPressed() +{ + +} + +void MainWindow::SetImagePixmap(QPixmap* p) +{ + ui->Image->setPixmap(*p); +} + +void MainWindow::keyReleaseEvent(QKeyEvent * qev) +{ + if (qev->key() == Qt::Key_Delete) + { + emit deleteButtonPressed(); + } +} diff --git a/Black_okospace_levelcreator/mainwindow.h b/Black_okospace_levelcreator/mainwindow.h new file mode 100644 index 0000000..4e96b59 --- /dev/null +++ b/Black_okospace_levelcreator/mainwindow.h @@ -0,0 +1,56 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include "leveldata.h" + +namespace Ui { +class MainWindow; +} + +enum TCurrentMode +{ + CM_ADD_POLYS, + CM_MODIFY_POLYS, +}; + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + TCurrentMode CurrentMode; + + QPixmap* Pixmap; + QImage bkgImage; + + LevelData* levelData; + + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + + + void InitSlots(); + void InitSlotsForAddPolys(); + void InitSlotsForModifyPolys(); + + void SetImagePixmap(QPixmap* p); + +signals: + void deleteButtonPressed(); + +public slots: + void LoadBackgroundSlot(); + void RadioButtonAddPolysSelected(); + void RadioButtonModifyPolysSelected(); + + void OnSaveButtonPressed(); + void OnLoadButtonPressed(); + +private: + Ui::MainWindow *ui; +protected: + void keyReleaseEvent(QKeyEvent* event); +}; + +#endif // MAINWINDOW_H diff --git a/Black_okospace_levelcreator/mainwindow.ui b/Black_okospace_levelcreator/mainwindow.ui new file mode 100644 index 0000000..6cd6b42 --- /dev/null +++ b/Black_okospace_levelcreator/mainwindow.ui @@ -0,0 +1,254 @@ + + + MainWindow + + + + 0 + 0 + 720 + 453 + + + + MainWindow + + + + + 0 + 0 + + + + + 0 + 0 + + + + Qt::LeftToRight + + + + 0 + + + QLayout::SetMinAndMaxSize + + + 0 + + + + + + 0 + 0 + + + + + 150 + 400 + + + + + 150 + 16777215 + + + + QFrame::StyledPanel + + + QFrame::Raised + + + + + 10 + 62 + 83 + 17 + + + + Modify polys + + + + + + 10 + 39 + 70 + 17 + + + + Add polys + + + true + + + + + + 10 + 10 + 131 + 23 + + + + Load background + + + + + + 10 + 100 + 131 + 23 + + + + Save level + + + + + + 10 + 130 + 131 + 23 + + + + Load level + + + + + + + + Qt::Vertical + + + QSizePolicy::Minimum + + + + 20 + 40 + + + + + + + + + 0 + 0 + + + + + 0 + 0 + + + + Qt::ScrollBarAlwaysOn + + + Qt::ScrollBarAlwaysOn + + + true + + + Qt::AlignJustify|Qt::AlignVCenter + + + + + 0 + 0 + 978 + 658 + + + + + 0 + 0 + + + + + + + + 0 + 0 + + + + + 960 + 640 + + + + QFrame::Box + + + + + + + + + + + + + + + + 0 + 0 + 720 + 21 + + + + + + TopToolBarArea + + + false + + + + + + + + CustomLabel + QLabel +
customlabel.h
+
+
+ + +