livelcreator added

This commit is contained in:
Vladislav Khorev 2013-11-04 18:06:35 +00:00
parent 6e23ac1eeb
commit 6191258856
9 changed files with 798 additions and 0 deletions

View File

@ -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

View File

@ -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());
}
}

View File

@ -0,0 +1,30 @@
#ifndef CUSTOMLABEL_H
#define CUSTOMLABEL_H
#include <QLabel>
#include <QMouseEvent>
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

View File

@ -0,0 +1,173 @@
#include "leveldata.h"
#include "mainwindow.h"
#include <QPainter>
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();
}

View File

@ -0,0 +1,52 @@
#ifndef LEVELDATA_H
#define LEVELDATA_H
#include <vector>
#include <QObject>
#include <QPoint>
class MainWindow;
struct TPoly
{
std::vector<QPoint> pointArr;
};
class LevelData : public QObject
{
Q_OBJECT
public:
std::vector<QPoint> tempPointArr;
std::vector<TPoly> 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

View File

@ -0,0 +1,13 @@
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
w.InitSlots();
return a.exec();
}

View File

@ -0,0 +1,166 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
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();
}
}

View File

@ -0,0 +1,56 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#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

View File

@ -0,0 +1,254 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>720</width>
<height>453</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="sizeConstraint">
<enum>QLayout::SetMinAndMaxSize</enum>
</property>
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QFrame" name="frame">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>150</width>
<height>400</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>150</width>
<height>16777215</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<widget class="QRadioButton" name="radioButtonModifyPolys">
<property name="geometry">
<rect>
<x>10</x>
<y>62</y>
<width>83</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string>Modify polys</string>
</property>
</widget>
<widget class="QRadioButton" name="radioButtonAddPolys">
<property name="geometry">
<rect>
<x>10</x>
<y>39</y>
<width>70</width>
<height>17</height>
</rect>
</property>
<property name="text">
<string>Add polys</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
<widget class="QPushButton" name="LoadBackgroundButton">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>131</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Load background</string>
</property>
</widget>
<widget class="QPushButton" name="SaveLevelButton">
<property name="geometry">
<rect>
<x>10</x>
<y>100</y>
<width>131</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Save level</string>
</property>
</widget>
<widget class="QPushButton" name="LoadLevelButton">
<property name="geometry">
<rect>
<x>10</x>
<y>130</y>
<width>131</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Load level</string>
</property>
</widget>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Minimum</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QScrollArea" name="scrollArea">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOn</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOn</enum>
</property>
<property name="widgetResizable">
<bool>true</bool>
</property>
<property name="alignment">
<set>Qt::AlignJustify|Qt::AlignVCenter</set>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>978</width>
<height>658</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="CustomLabel" name="Image">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>960</width>
<height>640</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>720</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>CustomLabel</class>
<extends>QLabel</extends>
<header>customlabel.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>