-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMainWindow.cpp
2052 lines (1796 loc) · 62 KB
/
MainWindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "ZoomableGraphicsView.h"
#include "SheetEditorView.h"
#include <QFileDialog>
#include <QDesktopWidget>
#include <QFile>
#include <QMessageBox>
#include <QSettings>
#include <QDebug>
#include <QFontDialog>
#include <QFontMetrics>
#include <QBuffer>
#include "FreeImage.h"
#include <string.h>
#include <QListView>
#include <QTreeView>
#include "BatchRenderer.h"
#include <QThreadPool>
#include "undo/FontColorStep.h"
#include "undo/FrameBgColorStep.h"
#include "undo/SheetBgColorStep.h"
#include "undo/SheetBgTransparentStep.h"
#include "undo/FrameBgTransparentStep.h"
#include "undo/SheetFontStep.h"
#include "undo/YSpacingStep.h"
#include "undo/XSpacingStep.h"
#include "undo/MinimizeWidthCheckboxStep.h"
#include "undo/SheetWidthStep.h"
#include "undo/ReverseAnimStep.h"
#include "undo/AnimNameStep.h"
#include "undo/NameVisibleStep.h"
#include "undo/BalanceAnimStep.h"
#include "undo/RemoveDuplicateStep.h"
#include "undo/DragDropStep.h"
#include "undo/DeleteStep.h"
#include "undo/AddImagesStep.h"
#define SELECT_RECT_THICKNESS 5
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
sheet = NULL;
animationWrap = WRAP;
//Create UI, removing help icons as needed
ui->setupUi(this);
mImportWindow = new ImportDialog(this);
mImportWindow->setWindowFlags(mImportWindow->windowFlags() & ~Qt::WindowContextHelpButtonHint);
mBalanceWindow = new BalanceSheetDialog(this);
mBalanceWindow->setWindowFlags(mBalanceWindow->windowFlags() & ~Qt::WindowContextHelpButtonHint);
mIconExportWindow = new IconExportDialog(this);
mIconExportWindow->setWindowFlags(mIconExportWindow->windowFlags() & ~Qt::WindowContextHelpButtonHint);
mRecentDocuments = new RecentDocuments(this);
mRecentDocuments->init(ui->menuFile, ui->actionQuit);
//TODO Implement cut/copy/paste
ui->cutCopyPasteSeparator->setVisible(false);
ui->cutButton->setVisible(false);
ui->copyButton->setVisible(false);
ui->pasteButton->setVisible(false);
ui->actionCut->setVisible(false);
ui->actionCopy->setVisible(false);
ui->actionPaste->setVisible(false);
//Connect all our signals & slots up
QObject::connect(mImportWindow, SIGNAL(importOK(QImage, int, int, bool, bool)), this, SLOT(importNext(QImage, int, int, bool, bool)));
QObject::connect(mImportWindow, SIGNAL(importAll(QImage, int, int, bool, bool)), this, SLOT(importAll(QImage, int, int, bool, bool)));
QObject::connect(this, SIGNAL(setImportImg(QImage)), mImportWindow, SLOT(setPreviewImage(QImage)));
QObject::connect(ui->sheetPreview, SIGNAL(mouseMoved(int,int)), this, SLOT(mouseCursorPos(int, int)));
QObject::connect(ui->sheetPreview, SIGNAL(mousePressed(int,int)), this, SLOT(mouseDown(int, int)));
QObject::connect(ui->sheetPreview, SIGNAL(mouseReleased(int,int)), this, SLOT(mouseUp(int, int)));
QObject::connect(ui->actionNew, SIGNAL(triggered()), this, SLOT(newFile()));
QObject::connect(ui->actionSaveAs, SIGNAL(triggered()), this, SLOT(saveFileAs()));
QObject::connect(ui->actionImport_WIP_Sheet, SIGNAL(triggered(bool)), this, SLOT(loadSheet()));
QObject::connect(ui->actionUndo, SIGNAL(triggered(bool)), this, SLOT(undo()));
QObject::connect(ui->actionRedo, SIGNAL(triggered(bool)), this, SLOT(redo()));
QObject::connect(ui->actionCut, SIGNAL(triggered(bool)), this, SLOT(cut()));
QObject::connect(ui->actionCopy, SIGNAL(triggered(bool)), this, SLOT(copy()));
QObject::connect(ui->actionPaste, SIGNAL(triggered(bool)), this, SLOT(paste()));
QObject::connect(ui->sheetPreview, SIGNAL(droppedFiles(QStringList)), this, SLOT(addImages(QStringList)));
QObject::connect(ui->sheetPreview, SIGNAL(droppedFolders(QStringList)), this, SLOT(addFolders(QStringList)));
QObject::connect(mBalanceWindow, SIGNAL(balance(int,int,BalancePos::Pos,BalancePos::Pos)), this, SLOT(balance(int,int,BalancePos::Pos,BalancePos::Pos)));
QObject::connect(this, SIGNAL(setBalanceDefWH(int,int)), mBalanceWindow, SLOT(defaultWH(int,int)));
QObject::connect(this, SIGNAL(setIconImage(QImage)), mIconExportWindow, SLOT(setImage(QImage)));
QObject::connect(mRecentDocuments, SIGNAL(openFile(QString)), this, SLOT(loadSheet(QString)));
animItem = NULL;
progressBar = NULL;
mAnimFrame = 0;
clicked = selected = lastSelected = NULL;
transparentBg = QImage("://bg");
bUIMutex = false;
userEditingWidth = true;
wEditing = 1000;
bDraggingSheetW = false;
m_bDraggingSelected = false;
m_bSetDraggingCursor = false;
setSaved();
m_rLastDragHighlight.setCoords(0,0,0,0);
m_bLastDragInAnim = false;
sCurFilename = UNTITLED_IMAGE_STR;
animUpdateTimer = new QTimer(this);
QObject::connect(animUpdateTimer, SIGNAL(timeout()), this, SLOT(animUpdate()));
//Create gfx stuff for drawing to image contexts
animScene = new QGraphicsScene();
msheetScene = new QGraphicsScene();
ui->animationPreview->setScene(animScene);
ui->sheetPreview->setScene(msheetScene);
ui->animationPreview->show();
ui->sheetPreview->show();
ui->animationNameEditor->installEventFilter(this);
ui->splitter->setStretchFactor(0, 1);
ui->splitter->setStretchFactor(1, 0);
mSheetZoom = new ZoomableGraphicsView(ui->sheetPreview);
mAnimationZoom = new ZoomableGraphicsView(ui->animationPreview);
animHighlightCol = QColor(128, 0, 0, 255);
//Create animation sheet
sheet = new Sheet(msheetScene, ui->sheetPreview, transparentBg, DRAG_HANDLE_SIZE);
if(ui->sheetPreview->isHidden())
ui->sheetPreview->show();
QPen linePen(QColor(0,0,255), SELECT_RECT_THICKNESS);
curSelectedRect = msheetScene->addRect(QRect(0,0,0,0), linePen);
curSelectedRect->setZValue(2); //Above most everything
curSelectedRect->setVisible(false);
curDragLine = msheetScene->addLine(0, 0, 10, 10, linePen);
curDragLine->setZValue(2);
curDragLine->setVisible(false);
curSelectedAnimRect = msheetScene->addRect(0,0,0,0,QPen(animHighlightCol, SELECT_RECT_THICKNESS/2), QBrush(QColor(128,0,0,100)));
curSelectedAnimRect->setZValue(-3);
curSelectedAnimRect->setVisible(true);
//Read in settings here
loadSettings();
//Set color icons to proper color
setColorButtonIcons();
updateWindowTitle();
//Store initial undo state
//pushUndo();
updateUndoRedoMenu();
msheetScene->setSceneRect(0,0,0,0);
}
MainWindow::~MainWindow()
{
clearUndo();
clearRedo();
if(animItem)
delete animItem;
delete sheet;
if(animScene)
delete animScene;
if(msheetScene)
delete msheetScene;
delete mIconExportWindow;
delete animUpdateTimer;
delete mImportWindow;
delete mBalanceWindow;
delete mSheetZoom;
delete mAnimationZoom;
delete ui;
}
void MainWindow::addImages(QStringList l)
{
mOpenFiles = l;
openImportDiag();
}
void MainWindow::importImageList(QStringList& fileList, QString prepend, QString animName)
{
if(fileList.size())
{
QVector<QImage> animation;
foreach(QString s1, fileList)
{
QString imgPath = prepend + s1;
QImage image = loadImageFI(imgPath);
if(!image.isNull())
animation.append(image);
else
qDebug() << "Unable to open image " << imgPath << endl;
}
insertAnimHelper(animation, animName);
checkMinWidth();
if(ui->minWidthCheckbox->isChecked())
minimizeSheetWidth();
drawAnimation();
updateSelectedAnim();
}
}
QStringList MainWindow::supportedFileFormats()
{
QStringList fileFormats;
//FreeImage supported file formats from http://freeimage.sourceforge.net/features.html
fileFormats
<< "*.bmp"
<< "*.dds"
<< "*.exr"
<< "*.gif"
<< "*.hdr"
<< "*.ico"
<< "*.iff"
<< "*.jng"
<< "*.jpeg"
<< "*.jpg"
<< "*.mng"
<< "*.pcx"
<< "*.pbm"
<< "*.pgm"
<< "*.ppm"
<< "*.pfm"
<< "*.png"
<< "*.pict"
<< "*.psd"
<< "*.raw"
<< "*.ras"
<< "*.sgi"
<< "*.tga"
<< "*.tif"
<< "*.tiff"
<< "*.wbmp"
<< "*.xbm"
<< "*.xpm";
return fileFormats;
}
void MainWindow::addFolders(QStringList l)
{
foreach(QString s, l)
{
QDir folder(s);
//Filter out only image files
QStringList fileFilters = supportedFileFormats();
QStringList files = folder.entryList(fileFilters, QDir::Files, QDir::Name); //Get list of all files in this folder
importImageList(files, s + '/', folder.dirName());
}
//if(l.size())
//genUndoState();
}
void MainWindow::on_openImagesButton_clicked()
{
QStringList files = QFileDialog::getOpenFileNames(this, "Import Image as Animation Sheet", lastOpenDir, "All Files (*.*)");
if(files.size())
{
QString s = (*files.begin());
QFileInfo inf(s);
lastOpenDir = inf.absoluteDir().absolutePath();
}
addImages(files);
}
void MainWindow::on_openStripButton_clicked()
{
mOpenFiles = QFileDialog::getOpenFileNames(this, "Import Frame Sequence", lastOpenDir, "All Files (*.*)");
if(mOpenFiles.size())
{
QString s = (*mOpenFiles.begin());
QFileInfo inf(s);
lastOpenDir = inf.absoluteDir().absolutePath();
importImageList(mOpenFiles);
//genUndoState();
}
}
void MainWindow::importNext(QImage img, int numx, int numy, bool bVert, bool bSplit)
{
importImageAsSheet(img, curImportImage, numx, numy, bVert, bSplit);
openImportDiag(); //Next one
}
void MainWindow::importAll(QImage img, int numx, int numy, bool bVert, bool bSplit)
{
importImageAsSheet(img, curImportImage, numx, numy, bVert, bSplit);
foreach(QString s, mOpenFiles)
importImageAsSheet(s, numx, numy, bVert, bSplit);
curImportImage = "";
mOpenFiles.clear();
}
void MainWindow::openImportDiag()
{
if(!mOpenFiles.size())
return;
curImportImage = mOpenFiles.takeFirst(); //Pop first filename and use it
//Test for/filter out animated .gif
if(curImportImage.endsWith(".gif", Qt::CaseInsensitive))
{
if(loadAnimatedGIF(curImportImage)) //If this fails, fall through and treat this image as per normal
{
openImportDiag(); //Recursively call so we get the next image
return; //Break out here so we don't add it accidentally
}
}
//Strip the file path from the image name
int last = curImportImage.lastIndexOf("/");
if(last == -1)
last = curImportImage.lastIndexOf("\\");
QString s = curImportImage;
if(last != -1)
s.remove(0, last+1);
QString windowTitle = "Animation for image " + s;
mImportWindow->setModal(true);
mImportWindow->setWindowTitle(windowTitle);
if(setImportImg(loadImageFI(curImportImage)))
{
mImportWindow->show();
//Center on parent
centerParent(this, mImportWindow);
}
}
void MainWindow::insertAnimHelper(QVector<QImage> imgList, QString name)
{
if(imgList.size())
addUndoStep(new AddImagesStep(this, imgList, name));
}
void MainWindow::importImageAsSheet(QString imgFilename, int numxframes, int numyframes, bool bVert, bool bSplit)
{
QImage image = loadImageFI(imgFilename);
if(!image.isNull())
{
QMessageBox::information(this, "Image Import", "Error opening image " + imgFilename);
return;
}
importImageAsSheet(image, imgFilename, numxframes, numyframes, bVert, bSplit);
}
void MainWindow::importImageAsSheet(QImage image, QString imgFilename, int numxframes, int numyframes, bool bVert, bool bSplit)
{
QString fileName = QFileInfo(imgFilename).baseName();
//Find image dimensions
int iXFrameSize = image.width() / numxframes;
int iYFrameSize = image.height() / numyframes;
//Grab all the frames out
QVector<QImage> imgList;
if(!bVert)
{
for(int y = 0; y < numyframes; y++)
{
for(int x = 0; x < numxframes; x++)
{
imgList.push_back(image.copy(x*iXFrameSize, y*iYFrameSize, iXFrameSize, iYFrameSize));
}
if(bSplit)
{
insertAnimHelper(imgList, fileName + '_' + QString::number(y));
imgList.clear();
}
}
}
else
{
for(int x = 0; x < numxframes; x++)
{
for(int y = 0; y < numyframes; y++)
{
imgList.push_back(image.copy(x*iXFrameSize, y*iYFrameSize, iXFrameSize, iYFrameSize));
}
if(bSplit)
{
insertAnimHelper(imgList, fileName + '_' + QString::number(x));
imgList.clear();
}
}
}
if(!bSplit)
insertAnimHelper(imgList, fileName);
checkMinWidth();
drawAnimation();
}
//Example from http://www.qtforum.org/article/28852/center-any-child-window-center-parent.html
void MainWindow::centerParent(QWidget* parent, QWidget* child)
{
QPoint centerparent(
parent->x() + ((parent->frameGeometry().width() - child->frameGeometry().width()) /2),
parent->y() + ((parent->frameGeometry().height() - child->frameGeometry().height()) /2));
QDesktopWidget * pDesktop = QApplication::desktop();
QRect sgRect = pDesktop->screenGeometry(pDesktop->screenNumber(parent));
QRect childFrame = child->frameGeometry();
if(centerparent.x() < sgRect.left())
centerparent.setX(sgRect.left());
else if((centerparent.x() + childFrame.width()) > sgRect.right())
centerparent.setX(sgRect.right() - childFrame.width());
if(centerparent.y() < sgRect.top())
centerparent.setY(sgRect.top());
else if((centerparent.y() + childFrame.height()) > sgRect.bottom())
centerparent.setY(sgRect.bottom() - childFrame.height());
child->move(centerparent);
}
void MainWindow::on_xSpacingBox_valueChanged(int arg1)
{
sheet->setXSpacing(arg1);
int minW = sheet->getSmallestPossibleWidth();
if(minW > ui->sheetWidthBox->value())
{
userEditingWidth = false;
ui->sheetWidthBox->setValue(minW);
}
updateSelectedAnim();
}
void MainWindow::on_ySpacingBox_valueChanged(int arg1)
{
sheet->setYSpacing(arg1);
sheet->refresh();
sheet->updateSceneBounds();
updateSelectedAnim();
}
void MainWindow::genericSave(QString saveFilename)
{
if(saveFilename.length())
{
//Set cursor to saving cursor
QApplication::setOverrideCursor(Qt::WaitCursor);
QApplication::processEvents();
statusBar()->showMessage("Saving " + saveFilename + "...");
//Create image and save
lastSaveStr = saveFilename;
if(saveFilename.contains(".sheet", Qt::CaseInsensitive))
{
saveSheet(saveFilename);
QFileInfo fi(saveFilename);
sCurFilename = fi.fileName();
setSaved();
updateWindowTitle();
}
else
{
if(!sheet->render(saveFilename))
{
QMessageBox::information(this,"Image Export","Error saving image " + saveFilename);
}
else
{
QFileInfo fi(saveFilename);
sCurFilename = fi.fileName();
setSaved();
updateWindowTitle();
}
}
//Set cursor back
QApplication::restoreOverrideCursor();
statusBar()->showMessage(saveFilename + " saved!");
}
}
QString MainWindow::getSaveFilename(const char* title)
{
QString sSel;
if(lastSaveStr.contains(".sheet", Qt::CaseInsensitive))
sSel = "Sprite Sheet (*.sheet)";
else if(lastSaveStr.contains(".bmp", Qt::CaseInsensitive))
sSel = "Windows Bitmap (*.bmp)";
else if(lastSaveStr.contains(".tiff", Qt::CaseInsensitive))
sSel = "TIFF Image (*.tiff)";
else
sSel = "PNG Image (*.png)"; //Default to PNG the first time they use
return QFileDialog::getSaveFileName(this,
tr(title),
lastSaveStr,
tr("PNG Image (*.png);;Windows Bitmap (*.bmp);;TIFF Image (*.tiff);;Sprite Sheet (*.sheet)"),
&sSel);
}
//Save file
void MainWindow::saveFile()
{
if(!sheet || !sheet->size())
return;
if(!isModified())
return; //Don't bother saving if we already have
QString saveFilename;
if(sCurFilename == UNTITLED_IMAGE_STR) //Haven't saved this yet
saveFilename = getSaveFilename("Save Sheet");
else
saveFilename = lastSaveStr;
genericSave(saveFilename);
}
void MainWindow::saveFileAs()
{
if(!sheet || !sheet->size())
return;
genericSave(getSaveFilename("Save Sheet As"));
}
void MainWindow::on_animationNameEditor_textChanged(const QString& arg1)
{
if(sheet->size())
{
Animation* anim = sheet->getAnimation(sheet->getCurSelected());
anim->setName(arg1);
sheet->refresh();
sheet->updateSceneBounds();
updateSelectedAnim(false);
}
}
void MainWindow::drawAnimation()
{
mCurFrame = QImage();
if(sheet->size())
{
Animation* anim = sheet->getAnimation(sheet->getCurSelected());
QVector<Frame*> frames = anim->getFrames();
if(frames.size())
{
if(mAnimFrame > frames.size()-1)
mAnimFrame = frames.size()-1;
mCurFrame = frames.at(mAnimFrame)->getImage();
}
}
if(mCurFrame.isNull())
{
if(animItem)
animItem->hide();
ui->curFrameLabel->setText("Current Frame: 0/0");
return;
}
//Draw image and bg
QImage animFrame(mCurFrame.width(), mCurFrame.height(), QImage::Format_ARGB32);
QPainter painter(&animFrame);
painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
if(ui->frameBgTransparent->isChecked())
{
QBrush bgTexBrush(transparentBg);
painter.fillRect(0, 0, mCurFrame.width(), mCurFrame.height(), bgTexBrush);
}
else
animFrame.fill(sheet->getFrameBgCol());
painter.drawImage(0, 0, mCurFrame);
if(animItem == NULL)
{
animItem = new QGraphicsPixmapItem(QPixmap::fromImage(animFrame));
animScene->addItem(animItem);
}
else
animItem->setPixmap(QPixmap::fromImage(animFrame));
animItem->show();
animScene->setSceneRect(0, 0, animFrame.width(), animFrame.height());
int numFramesTotal = 0;
if(sheet->getCurSelected() >= 0 && sheet->getCurSelected() < (int)sheet->size())
numFramesTotal = sheet->getAnimation(sheet->getCurSelected())->getFrames().size();
ui->curFrameLabel->setText("Current Frame: " + QString::number(mAnimFrame+1) + "/" + QString::number(numFramesTotal));
}
void MainWindow::animUpdate()
{
if(sheet->size())
{
Animation* anim = sheet->getAnimation(sheet->getCurSelected());
QVector<Frame*> frames = anim->getFrames();
if(frames.size())
{
if(frames.size() > 1)
{
if(animationWrap != PINGPONG_BACK)
mAnimFrame++;
else
{
if(!mAnimFrame)
{
animationWrap = PINGPONG;
mAnimFrame++;
}
else
mAnimFrame--;
}
if(mAnimFrame > frames.size()-1)
{
switch(animationWrap)
{
case WRAP:
default:
mAnimFrame = 0;
break;
case PINGPONG:
mAnimFrame = frames.size()-2;
animationWrap = PINGPONG_BACK;
break;
case STOP:
on_animStopButton_clicked();
mAnimFrame = frames.size()-1; //Reset to last frame
break;
}
}
}
else
mAnimFrame = 0;
}
}
drawAnimation();
}
void MainWindow::on_animationSpeedSpinbox_valueChanged(int arg1)
{
int iInterval = 1000/arg1;
animUpdateTimer->stop();
animUpdateTimer->start(iInterval);
updatePlayIcon();
}
void MainWindow::on_animPlayButton_clicked()
{
if(!animUpdateTimer->isActive())
{
int iInterval = 1000/ui->animationSpeedSpinbox->value();
animUpdateTimer->start(iInterval);
}
else
animUpdateTimer->stop();
//Set to first frame if we're on stop anim playback on last frame
if(animationWrap == STOP && sheet->size())
{
Animation* anim = sheet->getAnimation(sheet->getCurSelected());
QVector<Frame*> frames = anim->getFrames();
if(mAnimFrame >= frames.size()-1)
{
mAnimFrame = 0;
drawAnimation();
}
}
updatePlayIcon();
}
void MainWindow::on_animStopButton_clicked()
{
animUpdateTimer->stop();
mAnimFrame = 0;
drawAnimation();
updatePlayIcon();
}
void MainWindow::on_animPrevFrameButton_clicked()
{
if(animUpdateTimer->isActive())
animUpdateTimer->stop();
if(sheet->size())
{
Animation* anim = sheet->getAnimation(sheet->getCurSelected());
QVector<Frame*> frames = anim->getFrames();
if(frames.size())
{
mAnimFrame--;
if(mAnimFrame > frames.size()-1)
mAnimFrame = frames.size()-1;
if(mAnimFrame < 0)
mAnimFrame = frames.size()-1;
}
}
drawAnimation();
updatePlayIcon();
}
void MainWindow::on_animNextFrameButton_clicked()
{
if(animUpdateTimer->isActive())
animUpdateTimer->stop();
if(sheet->size())
{
Animation* anim = sheet->getAnimation(sheet->getCurSelected());
QVector<Frame*> frames = anim->getFrames();
if(frames.size())
{
mAnimFrame++;
if(mAnimFrame > frames.size()-1)
mAnimFrame = 0;
}
}
drawAnimation();
updatePlayIcon();
}
bool MainWindow::isMouseOverDragArea(int x, int y)
{
if(x < 0 || y < 0)
return false;
unsigned int ux = x;
unsigned int uy = y;
return ux >= sheet->getWidth() &&
ux <= sheet->getWidth() + DRAG_HANDLE_SIZE &&
uy <= sheet->getHeight() &&
uy >= 0;
}
QGraphicsItem* MainWindow::isItemUnderCursor(int x, int y)
{
//Get the list of all items under this position
QList<QGraphicsItem *> items = msheetScene->items(
x,
y,
1,
1,
Qt::IntersectsItemBoundingRect, //Intersect the bounding rectangle of the item (because transparency)
Qt::AscendingOrder, //No idea if this is what we want
mSheetZoom->getView()->transform());
//Grab the topmost animation frame image
foreach(QGraphicsItem* item, items)
{
if(!item->zValue()) //our animation frames are at a z of 0
return item;
}
return NULL;
}
void MainWindow::mouseCursorPos(int x, int y)
{
bool bSelectRectVisible = false;
if(sheet)
{
//Update cursor if need be
if(isMouseOverDragArea(x, y) && !selected)
ui->sheetPreview->setCursor(Qt::SizeHorCursor);
else if(!bDraggingSheetW && !m_bDraggingSelected)
ui->sheetPreview->setCursor(Qt::ArrowCursor);
//If dragging, update sheet width (set the box value directly so that it updates properly)
if(bDraggingSheetW)
{
userEditingWidth = false;
ui->sheetWidthBox->setValue(mStartSheetW - (xStartDragSheetW - x));
if(ui->minWidthCheckbox->isChecked())
minimizeSheetWidth();
}
else
{
if(!selected)
{
QGraphicsItem* it = isItemUnderCursor(x, y);
if(it != NULL)
{
//Draw box around currently-highlighted image
curSelectedRect->setRect(it->boundingRect());
curSelectedRect->setPos(it->x(), it->y());
bSelectRectVisible = true;
}
}
else
{
QLine pos = sheet->getDragPos(x, y);
//qDebug() << "Drag pos" << pos;
if(pos.x1() >= 0 && pos.y1() >= 0)
{
//qDebug() << "set line";
curDragLine->setLine(pos);
curDragLine->setVisible(true);
}
else
curDragLine->setVisible(false);
}
}
}
curSelectedRect->setVisible(bSelectRectVisible);
//Show the mouse cursor pos
statusBar()->showMessage(QString::number(x) + ", " + QString::number(y));
curMouseY = y;
curMouseX = x;
}
void MainWindow::mouseDown(int x, int y)
{
selected = NULL;
if(sheet)
{
//We're starting to drag the sheet size handle
if(isMouseOverDragArea(x, y))
{
bDraggingSheetW = true;
mStartSheetW = sheet->getWidth();
xStartDragSheetW = x;
}
else
{
clicked = isItemUnderCursor(x, y);
if(clicked)
{
if(sheet->selected(clicked))
selected = clicked;
}
}
}
}
void MainWindow::mouseUp(int x, int y)
{
if(sheet)
{
if(bDraggingSheetW)
{
bDraggingSheetW = false;
sheet->updateSceneBounds();
addUndoStep(new SheetWidthStep(this, lastSheetW, ui->sheetWidthBox->value()));
lastSheetW = ui->sheetWidthBox->value();
}
else
{
QGraphicsItem* itemUnder = isItemUnderCursor(x, y);
if(clicked && itemUnder == clicked) //Selecting things
{
//Shift-click to select line
if(lastSelected && QGuiApplication::keyboardModifiers() & Qt::ShiftModifier)
{
sheet->selectLine(clicked, lastSelected);
sheet->clicked(x, y, clicked); //Deselect so it can be reselected again (FSMs are hard)
}
//Ctrl-click to select multiple
else if(!(QGuiApplication::keyboardModifiers() & Qt::ControlModifier))
sheet->deselectAll();
if(sheet->clicked(x, y, clicked))
lastSelected = clicked;
sheet->selectAnimation(sheet->getSelected(x, y));
}
else if(selected) //Drag-dropping frames
{
sheet->selectAnimation(sheet->getSelected(x, y));
addUndoStep(new DragDropStep(this, x, y));
}
else if(!itemUnder) //Deselecting everything
{
int selectedAnim = sheet->getSelected(x, y);
sheet->selectAnimation(selectedAnim);
sheet->deselectAll();
if(x > 0 && x < (int)sheet->getWidth() && y > 0 && y < (int)sheet->getHeight())
{
Animation* anim = sheet->getAnimation(selectedAnim);
if(anim != NULL)
anim->selectAll();
}
}
updateSelectedAnim();
}
}
selected = NULL;
curDragLine->setVisible(false);
}
void MainWindow::closeEvent(QCloseEvent *event)
{
//Make sure user has saved before closing
if(isModified())
{
QMessageBox::StandardButton dialog;
dialog = QMessageBox::warning(this, "Save Changes",
"Do you want to save changes to \"" + sCurFilename + "\"?",
QMessageBox::Discard | QMessageBox::Save | QMessageBox::Cancel);
if(dialog == QMessageBox::Save)
{
saveFile();
if(isModified()) //If they still haven't saved...
{
event->ignore();
return;
}
}
else if(dialog != QMessageBox::Discard)
{
event->ignore();
return;
}
}
saveSettings();
QMainWindow::closeEvent(event);
}
void MainWindow::saveSettings()
{
QSettings settings("DaxarDev", "SpriteSheeter");
//Save window geometry
settings.setValue("geometry", saveGeometry());
settings.setValue("windowState", saveState());
//Save other GUI settings
settings.setValue("xSpacing", ui->xSpacingBox->value());
settings.setValue("ySpacing", ui->ySpacingBox->value());
settings.setValue("sheetWidth", ui->sheetWidthBox->value());
settings.setValue("animationSpeed", ui->animationSpeedSpinbox->value());
settings.setValue("FrameBgTransparent", ui->frameBgTransparent->isChecked());
settings.setValue("SheetBgTransparent", ui->sheetBgTransparent->isChecked());
settings.setValue("sheetBgColr", sheet->getBgCol().red());
settings.setValue("sheetBgColg", sheet->getBgCol().green());
settings.setValue("sheetBgColb", sheet->getBgCol().blue());
settings.setValue("frameBgColr", sheet->getFrameBgCol().red());
settings.setValue("frameBgColg", sheet->getFrameBgCol().green());
settings.setValue("frameBgColb", sheet->getFrameBgCol().blue());
QColor fontColor = sheet->getFontColor();
settings.setValue("fontColr", fontColor.red());
settings.setValue("fontColg", fontColor.green());
settings.setValue("fontColb", fontColor.blue());
settings.setValue("lastSaveStr", lastSaveStr);
settings.setValue("lastIconStr", lastIconStr);
settings.setValue("lastOpenDir", lastOpenDir);
settings.setValue("lastImportExportStr", lastImportExportStr);
settings.setValue("sheetFont", sheet->getFont().toString());
settings.setValue("animNames", ui->animNameEnabled->isChecked());
settings.setValue("lastGIFStr", lastGIFStr);
settings.setValue("minimizeSheetWidth", ui->minWidthCheckbox->isChecked());
//Save version number (useful when loading settings from an older version of the program)
settings.setValue("major", MAJOR_VERSION);
settings.setValue("minor", MINOR_VERSION);
settings.setValue("rev", REV_VERSION);
WrapType saveType = animationWrap;
if(saveType == PINGPONG_BACK)
saveType = PINGPONG;
settings.setValue("animationWrap", (uint)saveType);
//settings.setValue("", );
}
void MainWindow::loadSettings()
{
//Read in settings from config (registry or wherever it is)
bUIMutex = true;
QSettings settings("DaxarDev", "SpriteSheeter");
if(settings.value("xSpacing", -1).toInt() == -1) //No settings are here
return;
restoreGeometry(settings.value("geometry").toByteArray());
restoreState(settings.value("windowState").toByteArray());
ui->xSpacingBox->setValue(settings.value("xSpacing").toInt());
ui->ySpacingBox->setValue(settings.value("ySpacing").toInt());
userEditingWidth = false;
ui->sheetWidthBox->setValue(settings.value("sheetWidth").toInt());
ui->animationSpeedSpinbox->setValue(settings.value("animationSpeed").toInt());
ui->frameBgTransparent->setChecked(settings.value("FrameBgTransparent").toBool());
ui->sheetBgTransparent->setChecked(settings.value("SheetBgTransparent").toBool());
QColor sheetBgCol;
sheetBgCol.setRed(settings.value("sheetBgColr").toInt());
sheetBgCol.setGreen(settings.value("sheetBgColg").toInt());
sheetBgCol.setBlue(settings.value("sheetBgColb").toInt());
QColor frameBgCol;
frameBgCol.setRed(settings.value("frameBgColr").toInt());
frameBgCol.setGreen(settings.value("frameBgColg").toInt());
frameBgCol.setBlue(settings.value("frameBgColb").toInt());
QColor fontColor;
if(settings.value("fontColr", -1).toInt() != -1)
{
fontColor.setRed(settings.value("fontColr").toInt());
fontColor.setGreen(settings.value("fontColg").toInt());
fontColor.setBlue(settings.value("fontColb").toInt());