#include "Animation.h" #include "mainwindow.h" TAnimation::TAnimation(QObject *parent) : QObject(parent) { } void TAnimation::SetMainWindow(MainWindow* window) { Window = window; } void TAnimation::AddFrame(const TFrame& frame) { FrameList.push_back(frame); } TFrame& TAnimation::GetFrame(int i) { return FrameList[i]; } size_t TAnimation::GetFrameCount() { return FrameList.size(); } int TAnimation::GetMaxWidth() { int r = FrameList[0].Pixmap->width() + FrameList[0].ShiftX; for (size_t i = 1; i < FrameList.size(); i++) { if (r < FrameList[i].Pixmap->width() + FrameList[i].ShiftX) { r = FrameList[i].Pixmap->width() + FrameList[i].ShiftX; } } return r; } int TAnimation::GetMaxHeight() { int r = FrameList[0].Pixmap->height() + FrameList[0].ShiftY; for (size_t i = 1; i < FrameList.size(); i++) { if (r < FrameList[i].Pixmap->height() + FrameList[i].ShiftY) { r = FrameList[i].Pixmap->height() + FrameList[i].ShiftY; } } return r; } void TAnimation::FillSheet(boost::shared_ptr sheetPixmap, std::vector& frameDataArr) { int maxFrameWidth = GetMaxWidth(); int maxFrameHeight = GetMaxHeight(); int cursorX = 0; int cursorY = 0; QPainter p; p.begin(&(*sheetPixmap)); for (size_t i=0; i(cursorX) / sheetPixmap->width(); frameData.TexCoordFromY = 1.f - static_cast(cursorY + maxFrameHeight) / sheetPixmap->height(); frameData.TexCoordToX = static_cast(cursorX + maxFrameWidth) / sheetPixmap->width(); frameData.TexCoordToY = 1.f - static_cast(cursorY) / sheetPixmap->height(); frameData.TimeToPlay = FrameList[i].TimeToPlay; frameDataArr.push_back(frameData); QPixmap& pixmapToDraw = *(FrameList[i].Pixmap); p.drawPixmap(cursorX + shiftX, cursorY + shiftY, pixmapToDraw); cursorX += maxFrameWidth; if (cursorX + maxFrameWidth >= sheetPixmap->width()) { cursorX = 0; cursorY += maxFrameHeight; } } p.end(); } void TAnimation::LoadFrame() { QFileDialog dialog(Window); dialog.setFileMode(QFileDialog::ExistingFiles); dialog.setNameFilter("PNG images (*.png)"); if (dialog.exec()) { QStringList fileNames = dialog.selectedFiles(); for (int i = 0; i < fileNames.count(); i++) { boost::shared_ptr pixmap(new QPixmap(fileNames[i], "PNG")); QFileInfo q; q.setFile(fileNames[i]); QString fn = q.fileName(); std::string fileName = fn.toUtf8().constData(); AddFrame(TFrame(pixmap, 0, fileName)); Window->LoadFrame(fileName); } //FontFileName = (dialog.selectedFiles())[0]; //emit SetFontNameTextSignal(FontFileName); //LoadFontFile(); } } void TAnimation::Clear() { FrameList.clear(); }