qt/Black_okospace_levelcreator/leveldata.cpp

174 lines
3.7 KiB
C++

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