forked from tiantian180/MilesEdgeworth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMilesEdgeworth.cpp
1166 lines (1104 loc) · 44.6 KB
/
MilesEdgeworth.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 "MilesEdgeworth.h"
MilesEdgeworth::MilesEdgeworth(QWidget* parent)
: QWidget(parent)
{
ui.setupUi(this);
//gif动态标签
setWindowTitle("咪酱");
ui.petLabel->setScaledContents(true);
ui.petLabel->resize(100 * scale, 100 * scale);
petMovie = new QMovie(":/gifs/special/briefcaseIn0.gif");
petMovie->setParent(this);
type = MilesEdgeworth::BRIEFCASEIN;
direction = 0;
nowScreen = QGuiApplication::primaryScreen();
QRect desktopRect = nowScreen->availableGeometry();
move(desktopRect.x() - 45 * scale, desktopRect.y() + desktopRect.height() - 90 * scale);
ui.petLabel->setMovie(petMovie);
// 设置窗口为无边框 透明 置顶
setAttribute(Qt::WA_TranslucentBackground);
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Tool);
// 提示: 如果设置为Qt::Tool, this->close()是不会退出程序的, 只会隐藏当前窗口.
// 我的解决方法是close的时候发送信号, 让main里的QApplication接收到信号时调用quit()函数退出程序.
createContextMenu(); // 创建右键菜单
createTrayIcon(); // 创建托盘图标
setContextMenuPolicy(Qt::CustomContextMenu); // 右键唤起菜单
createClickTimer(); // 创建单双击专用的计时器
createBadgeTimer(); // 创建徽章专用的计时器
createShakeTimer(); // 创建判断是否触发害怕地震动画专用的计时器
petMovie->start();
// 创建音效播放器, 并移动到独立线程
thread = new QThread();
soundEffect = new QSoundEffect(this);
soundEffect->setVolume(0.8f);
soundEffect->moveToThread(thread);
// 创建检察官徽章
prosbadge = new ProsecutorBadge();
// 连接右键菜单
connect(this, &QWidget::customContextMenuRequested, this, &MilesEdgeworth::showContextMenu);
// 帧切换: 走路/跑步要进行移动; 播放到最后一帧时判断是否要结束动作
connect(petMovie, &QMovie::frameChanged, [=](int frame) {
// 移动
if ((type == MilesEdgeworth::RUN || type == MilesEdgeworth::BRIEFCASEIN) && !actionMove->isChecked()) {
runMove();
}
else if (type == MilesEdgeworth::WALK && !actionMove->isChecked()) {
walkMove();
}
// 判断是否要结束动作
if (frame == petMovie->frameCount() - 1) { // 若此时为最后一帧
switch (type) {
case BRIEFCASEIN:
{
petMovie->setFileName(QString(":gifs/special/briefcaseStop0.gif"));
type = MilesEdgeworth::BRIEFCASESTOP;
direction = 0;
break;
}
case BRIEFCASESTOP:
{
petMovie->setFileName(QString(":gifs/stand/0.gif"));
type = MilesEdgeworth::STAND;
direction = 0;
break;
}
case MilesEdgeworth::RUN:
case MilesEdgeworth::WALK:
{
// 若当前动作为跑/走, 则有二分之一的概率继续重复, 二分之一的概率停止并切换下一动作
double randnum = QRandomGenerator::global()->generateDouble();
if (randnum <= 0.5) {
//petMovie->stop();
autoChangeGif();
}
break;
}
case MilesEdgeworth::STAND:
{
// 若当前动作为站立, 则有0.3概率继续重复, 0.7的概率停止并切换下一动作
double randnum = QRandomGenerator::global()->generateDouble();
if (randnum <= 0.7) { //0.7
//petMovie->stop();
autoChangeGif();
}
actionSleep->setEnabled(true);
break;
}
case MilesEdgeworth::TAKETHAT:
case MilesEdgeworth::ONCE:
{
// 若当前动作为单次动作, 则播放一遍后立即停止, 切换下一动作
//petMovie->stop();
autoChangeGif();
actionSleep->setEnabled(true);
break;
}
case MilesEdgeworth::SLEEP:
{
// 接持续睡觉
petMovie->setFileName(QString(":/gifs/special/sleeping%1.gif").arg(direction));
type = MilesEdgeworth::SLEEPING;
actionSleep->setText("唤醒");
actionSleep->setEnabled(true);
break;
}
case MilesEdgeworth::SLEEPING:
{
// 循环, 啥也不干.
break;
}
case MilesEdgeworth::CROUCH:
{
// 固定在最后一帧, 直到鼠标松开才站起
petMovie->stop();
}
}
}
});
// 检察官徽章被点击时消失, 触发鞠躬动画, 停止计时器
connect(prosbadge, &ProsecutorBadge::badgeClicked, [=]() {
prosbadge->close();
petMovie->stop();
specifyChangeGif(QString(":/gifs/special/bow%1.gif").arg(direction % 2),
MilesEdgeworth::ONCE, direction % 2);
// 停止用于定时close的计时器
badgeTimer->stop();
});
// 独立线程播放音频
connect(thread, &QThread::finished, soundEffect, &QObject::deleteLater);
connect(soundEffect, &QObject::destroyed, thread, &QThread::deleteLater);
connect(this, &MilesEdgeworth::soundPlay, soundEffect, &QSoundEffect::play);
connect(this, &MilesEdgeworth::soundStop, soundEffect, &QSoundEffect::stop);
thread->start();
}
MilesEdgeworth::~MilesEdgeworth()
{
prosbadge->deleteLater();
if (thread->isRunning())
{
qDebug() << "thread is Running";
thread->quit();
thread->wait();
}
}
// 重写窗口移动事件. 用于限制窗口位置
void MilesEdgeworth::moveEvent(QMoveEvent* event)
{
if (type == MilesEdgeworth::BRIEFCASEIN) return;
if (QGuiApplication::screenAt(event->pos() + QPoint(50 * scale, 50 * scale)) != nullptr) {
// 如果咪酱的中心点没被移出屏幕, 则更新咪酱的所在屏幕
nowScreen = QGuiApplication::screenAt(event->pos() + QPoint(50 * scale, 50 * scale));
}
//QRect nowScreenRect = QGuiApplication::primaryScreen()->availableGeometry();
QPoint newPos = event->pos();
// 咪酱此时的四角坐标
QPoint topLeft = newPos + QPoint(36 * scale, 10 * scale);
QPoint topRight = newPos + QPoint(63 * scale, 10 * scale);
QPoint bottomLeft = newPos + QPoint(36 * scale, 90 * scale);
QPoint bottomRight = newPos + QPoint(63 * scale, 90 * scale);
if (QGuiApplication::screenAt(topLeft) == nullptr || QGuiApplication::screenAt(topRight) == nullptr
|| QGuiApplication::screenAt(bottomLeft) == nullptr || QGuiApplication::screenAt(bottomRight) == nullptr) {
// 只要有一个角出了屏幕,那么这次的move就可能要被限制
// 窗口topLeft点的坐标限制
int xMin;
int xMax;
int yMin;
int yMax;
QRect nowScreenRect = nowScreen->geometry();
// 单屏时是四条边都做限制.
xMax = nowScreenRect.x() + nowScreenRect.width() - 63 * scale;
xMin = nowScreenRect.x() - 36 * scale;
yMax = nowScreenRect.y() + nowScreenRect.height() - 90 * scale;
yMin = nowScreenRect.y() - 10 * scale;
if (leftPrimary->isChecked()) {
// 如果主屏幕在左侧, 则不限制主屏幕右边界和第二屏幕左边界, 而是限制主屏幕的左边界和第二屏幕的右边界
// 如果是在主屏幕内, 则有xMin但没有xMax
if (QGuiApplication::screens().indexOf(nowScreen) == 0) {
// 如果是在主屏幕, 则有xMin但没有xMax
xMax = 1000000000;
}
else if (QGuiApplication::screens().indexOf(nowScreen) == 1) {
// 如果是在第二屏幕, 则有xMax但没有xMin
xMin = -1000000000;
}
}
else if (rightPrimary->isChecked()) {
// 如果主屏幕在右侧, 则不限制主屏幕左边界和第二屏幕的右边界, 而是限制主屏幕的右边界和第二屏幕的左边界
// 如果是在主屏幕内, 则有xMax但没有xMin
if (QGuiApplication::screens().indexOf(nowScreen) == 0) {
// 如果是在主屏幕, 则有xMax但没有xMin
xMin = -1000000000;
}
else if (QGuiApplication::screens().indexOf(nowScreen) == 1) {
// 如果是在第二屏幕, 则有xMin但没有xMax
xMax = 1000000000;
}
}
// 限制左上角位置
newPos.setX(qMax(xMin, newPos.x()));
newPos.setY(qMax(yMin, newPos.y()));
// 限制右下角位置
newPos.setX(qMin(xMax, newPos.x()));
newPos.setY(qMin(yMax, newPos.y()));
move(newPos);
}
}
void MilesEdgeworth::mouseMoveEvent(QMouseEvent* event)
{
if (type == MilesEdgeworth::BRIEFCASEIN) return;
if (event->buttons() & Qt::LeftButton)
{
//此段用于处理拖拽移动
if (m_dragging)
{
//delta 相对偏移量
QPoint delta = event->globalPosition().toPoint() - m_startPosition;
//新位置:窗体原始位置+偏移量
move(m_framePosition + delta);
}
// 此段用于判断是否触发害怕地震动画
if (type != MilesEdgeworth::SLEEP && type != MilesEdgeworth::SLEEPING
&& (event->globalPosition().x() - shakeX) * shakeDirect < 0) {
// 如果移动的方向改变, 则计数+1, 记录方向和横坐标
shakeCount++;
shakeDirect = -shakeDirect;
shakeX = event->globalPosition().x();
if (shakeCount == 5 && shakeTimer->isActive()) {
// 计数达到五次时, 触发害怕地震动画, 计时器停止, 计数清零
petMovie->stop();
specifyChangeGif(QString(":gifs/special/crouch%1.gif").arg(direction % 2),
MilesEdgeworth::CROUCH, direction % 2);
shakeTimer->stop();
shakeCount = 0;
}
}
}
QWidget::mouseMoveEvent(event);
}
void MilesEdgeworth::mousePressEvent(QMouseEvent* event)
{
if (type == MilesEdgeworth::BRIEFCASEIN) return;
//响应左键
if (event->button() == Qt::LeftButton)
{
// 此段用于处理拖拽移动
m_dragging = true;
m_startPosition = event->globalPosition().toPoint();
m_framePosition = frameGeometry().topLeft();
// 此段用于处理单击双击判断
if (clickTimer->isActive()) {
// 如果计时器正在运作,则说明这次press是第二次
doubleClick = true;
clickTimer->stop(); // 第二次press后关闭计时器
}
// 此段用于判断是否触发害怕地震动作
if (type != MilesEdgeworth::SLEEP && type != MilesEdgeworth::SLEEPING) {
shakeX = event->globalPosition().x();
shakeCount = 0;
shakeTimer->start();
}
}
QWidget::mousePressEvent(event);
}
void MilesEdgeworth::mouseReleaseEvent(QMouseEvent* event)
{
if (type == MilesEdgeworth::BRIEFCASEIN) return;
// 此段用于处理拖拽移动
m_dragging = false;
// 此段用于处理单双击判断
if (event->button() == Qt::LeftButton &&
event->globalPosition().toPoint() == m_startPosition) {
// 如果release时与press时鼠标位置相同, 则不是拖拽, 预备进行单双击
if (doubleClick) {
// 若这是第二次点击的release, 则执行双击事件
doubleClickEvent();
doubleClick = false;
}
else {
// 否则这是第一次点击的release, 开启计时器
m_pressPos = event->position();
clickTimer->start();
}
}
// 此段用于判断是否触发害怕地震动作
if (type != MilesEdgeworth::SLEEP && type != MilesEdgeworth::SLEEPING) {
shakeTimer->stop();
shakeCount = 0;
if (type == MilesEdgeworth::CROUCH) {
// 如果处于蹲下状态, 则切换站起动作.
if (petMovie->state() == QMovie::NotRunning) {
// 如果已经蹲下, 则切换完整的站起
specifyChangeGif(QString(":gifs/special/standup%1.gif").arg(direction),
MilesEdgeworth::ONCE, direction);
}
else {
// 如果还没蹲下, 则切换惊吓恢复站立
petMovie->stop();
specifyChangeGif(QString(":gifs/special/standup%1.gif").arg(direction + 2),
MilesEdgeworth::ONCE, direction);
}
}
}
QWidget::mouseReleaseEvent(event);
}
void MilesEdgeworth::closeEvent(QCloseEvent* event)
{
// delete所有picViewer
if (firstViewer != nullptr) {
while (firstViewer->next != nullptr) {
PicViewer* temp = firstViewer;
firstViewer = temp->next;
firstViewer->prev = nullptr;
temp->deleteLater();
}
firstViewer->deleteLater();
}
emit exitProgram();
// QWidget::closeEvent(event);
}
void MilesEdgeworth::enterEvent(QEnterEvent* event)
{
setCursor(Qt::PointingHandCursor);
}
// 创建右键菜单
void MilesEdgeworth::createContextMenu()
{
menu = new QMenu(this);
sizeMenu = menu->addMenu("调整大小");
languageMenu = menu->addMenu("语音语言");
screenMenu = menu->addMenu("双屏选项");
menu->addSeparator();
actionTop = menu->addAction("置于顶层");
actionTop->setCheckable(true); // 设置action可选中
actionTop->setChecked(true); // 默认选中
actionMove = menu->addAction("禁止走动");
actionMove->setCheckable(true); // 设置action可选中
actionMute = menu->addAction("静音");
actionMute->setCheckable(true); // 设置action可选中
menu->addSeparator();
actionTea = menu->addAction("喂食红茶");
actionSleep = menu->addAction("睡觉");
menu->addSeparator();
actionPic = menu->addAction("图片置顶查看器");
menu->addSeparator();
actionExit = menu->addAction("退出");
// 子菜单sizeMenu选项
sizeGroup = new QActionGroup(sizeMenu);
sizeGroup->setExclusive(true); // 设置为单选
miniSize = sizeMenu->addAction("迷你");
miniSize->setCheckable(true); // 设置action可选中
sizeGroup->addAction(miniSize); // 加入action组
smallSize = sizeMenu->addAction("小");
smallSize->setCheckable(true); // 设置action可选中
sizeGroup->addAction(smallSize); // 加入action组
medianSize = sizeMenu->addAction("中");
medianSize->setCheckable(true); // 设置action可选中
medianSize->setChecked(true); // 默认选中小
sizeGroup->addAction(medianSize); // 加入action组
bigSize = sizeMenu->addAction("大");
bigSize->setCheckable(true); // 设置action可选中
sizeGroup->addAction(bigSize); // 加入action组
// 子菜单languageMenu选项
lanGroup = new QActionGroup(languageMenu);
lanGroup->setExclusive(true); // 设置为单选
jpLanguage = languageMenu->addAction("日语");
jpLanguage->setCheckable(true); // 设置action可选中
jpLanguage->setChecked(true); // 默认选中日语
lanGroup->addAction(jpLanguage); // 加入action组
engLanguage = languageMenu->addAction("英语");
engLanguage->setCheckable(true); // 设置action可选中
lanGroup->addAction(engLanguage); // 加入action组
chLanguage = languageMenu->addAction("汉语");
chLanguage->setCheckable(true); // 设置action可选中
lanGroup->addAction(chLanguage); // 加入action组
// 子菜单screenMenu选项
screenGroup = new QActionGroup(screenMenu);
screenGroup->setExclusive(true); // 设置为单选
singleScreen = screenMenu->addAction("单屏");
leftPrimary = screenMenu->addAction("主屏幕在左侧");
rightPrimary = screenMenu->addAction("主屏幕在右侧");
singleScreen->setCheckable(true);
singleScreen->setChecked(true); // 默认选中单屏
if (QGuiApplication::screens().length() == 1) {
leftPrimary->setDisabled(true);
rightPrimary->setDisabled(true);
}
else {
leftPrimary->setCheckable(true);
rightPrimary->setCheckable(true);
}
screenGroup->addAction(singleScreen);
screenGroup->addAction(leftPrimary);
screenGroup->addAction(rightPrimary);
// 进行action的连接
// 调整大小
connect(miniSize, &QAction::triggered, [=](bool checked) {
if (checked) {
QPoint nowPos = frameGeometry().topLeft();
ui.petLabel->resize(100 * MINI, 100 * MINI);
scale = MINI;
move(nowPos + QPoint(1, 1)); //做这一步是为了调用moveEvent,从而防止缩放过程中咪酱被移出屏幕
}
});
connect(smallSize, &QAction::triggered, [=](bool checked) {
if (checked) {
QPoint nowPos = frameGeometry().topLeft();
ui.petLabel->resize(100 * SMALL, 100 * SMALL);
scale = SMALL;
move(nowPos + QPoint(1, 1));
}
});
connect(medianSize, &QAction::triggered, [=](bool checked) {
if (checked) {
QPoint nowPos = frameGeometry().topLeft();
ui.petLabel->resize(100 * MEDIAN, 100 * MEDIAN);
scale = MEDIAN;
move(nowPos + QPoint(1, 1));
}
});
connect(bigSize, &QAction::triggered, [=](bool checked) {
if (checked) {
QPoint nowPos = frameGeometry().topLeft();
ui.petLabel->resize(100 * BIG, 100 * BIG);
scale = BIG;
move(nowPos + QPoint(1, 1));
}
});
// 语音语言
connect(jpLanguage, &QAction::triggered, [=](bool checked) {
if (checked) {
language = 0;
}
});
connect(engLanguage, &QAction::triggered, [=](bool checked) {
if (checked) {
language = 1;
}
});
connect(chLanguage, &QAction::triggered, [=](bool checked) {
if (checked) {
language = 2;
}
});
// 置顶
connect(actionTop, &QAction::triggered, [=](bool checked) {
if (checked) {
setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint);
this->show();
}
else {
setWindowFlags(this->windowFlags() & ~Qt::WindowStaysOnTopHint);
this->show();
}
});
//静音
connect(actionMute, &QAction::triggered, [=](bool checked) {
if (checked) {
soundEffect->setVolume(0);
}
else {
soundEffect->setVolume(0.8f);
}
});
// 喂食红茶
connect(actionTea, &QAction::triggered, [=](bool checked) {
petMovie->stop();
int next_direct = direction % 2;
int randnum = QRandomGenerator::global()->bounded(2);
specifyChangeGif(QString(":/gifs/special/tea%1.gif").arg(next_direct + randnum * 2),
MilesEdgeworth::ONCE, next_direct);
});
// 睡觉
connect(actionSleep, &QAction::triggered, [=](bool checked) {
if (type == MilesEdgeworth::SLEEPING) {
// 唤醒
petMovie->stop();
specifyChangeGif(QString(":/gifs/special/wake%1.gif").arg(direction),
MilesEdgeworth::ONCE, direction);
actionSleep->setText("睡觉");
actionTea->setEnabled(true);
}
else {
petMovie->stop();
int next_direct = direction % 2;
specifyChangeGif(QString(":/gifs/special/sleep%1.gif").arg(next_direct),
MilesEdgeworth::SLEEP, next_direct);
// 将action更名为"唤醒"
// 在睡觉动画播放完之前, 两个按钮要被禁用
actionSleep->setDisabled(true); // 解除禁用在frameChanged的connect里, SLEEP最后一帧时解除禁用
actionTea->setDisabled(true); // 唤醒时解除禁用
}
});
// 置顶图查看器
connect(actionPic, &QAction::triggered, this, &MilesEdgeworth::createPicViewer);
// 退出: 要关掉所有参考图窗口, 彻底退出程序.
connect(actionExit, &QAction::triggered, this, &QWidget::close);
}
// 创建右下角托盘图标
void MilesEdgeworth::createTrayIcon()
{
tray = new QSystemTrayIcon(this);
tray->setVisible(true);
tray->setIcon(QIcon(":/icon/favicon_bar.ico"));
QMenu* menu = new QMenu(this);
menu->addAction(actionExit);
tray->setContextMenu(menu);
tray->setToolTip(windowTitle());
tray->show();
connect(tray, &QSystemTrayIcon::activated, this, &MilesEdgeworth::on_trayActivated);
}
// 创建区分单双击用的计时器
void MilesEdgeworth::createClickTimer()
{
clickTimer = new QTimer(this);
clickTimer->setSingleShot(true);
clickTimer->setInterval(300); // 300ms内算双击
connect(clickTimer, &QTimer::timeout, [=]() {
// 执行单击事件
singleClickEvent();
});
}
// 创建检察官徽章用的计时器
void MilesEdgeworth::createBadgeTimer()
{
badgeTimer = new QTimer(this);
badgeTimer->setSingleShot(true);
badgeTimer->setInterval(FLYINGTIME);
connect(badgeTimer, &QTimer::timeout, [=]() {
// 超时未点击徽章, 徽章消失并且触发捡徽章动画
prosbadge->close();
petMovie->stop();
specifyChangeGif(QString(":/gifs/special/pickup%1.gif").arg(direction),
MilesEdgeworth::ONCE, direction);
});
}
void MilesEdgeworth::createShakeTimer()
{
shakeTimer = new QTimer(this);
shakeTimer->setInterval(1000);
connect(shakeTimer, &QTimer::timeout, [=]() {
// 1s内移动方向更换不足5次则重新计数
shakeCount = 0;
});
}
void MilesEdgeworth::createPicViewer()
{
if (numViewer == MAXVIEWER) return;
if (firstViewer == nullptr) {
// 当前没有窗口
firstViewer = new PicViewer();
firstViewer->next = nullptr;
firstViewer->prev = nullptr;
connect(firstViewer, &PicViewer::isClosing, this, &MilesEdgeworth::on_viewerClosing);
firstViewer->show();
}
else {
// 当前已有窗口. 头插法加入新窗口
PicViewer* temp = new PicViewer();
temp->next = firstViewer->next;
temp->prev = firstViewer;
firstViewer->next = temp;
if(temp->next != nullptr) temp->next->prev = temp;
connect(temp, &PicViewer::isClosing, this, &MilesEdgeworth::on_viewerClosing);
temp->show();
}
numViewer++;
if (numViewer == MAXVIEWER) actionPic->setDisabled(true); // 按钮置灰
}
// 显示右键菜单
void MilesEdgeworth::showContextMenu()
{
menu->exec(QCursor::pos());
}
// 自动随机切换Gif
void MilesEdgeworth::autoChangeGif()
{
Type next_type = MilesEdgeworth::STAND; // 下一动作, 默认为STAND
int next_direct = 0; // 下一方向, 默认为向右
switch (type) {
case MilesEdgeworth::RUN:
{
double randnum = QRandomGenerator::global()->generateDouble();
if (randnum <= 0.2) { // 0.2概率接同向走路
next_type = MilesEdgeworth::WALK;
next_direct = direction;
petMovie->setFileName(QString(":/gifs/walk/%1.gif").arg(next_direct));
}
else if (randnum < 0.4) { // 0.2概率接随机跑步
next_type = MilesEdgeworth::RUN;
next_direct = QRandomGenerator::global()->bounded(RUN_NUM);
petMovie->setFileName(QString(":/gifs/run/%1.gif").arg(next_direct));
}
else { // 0.6概率接站立
next_type = MilesEdgeworth::STAND;
next_direct = direction % 2;
petMovie->setFileName(QString(":/gifs/stand/%1.gif").arg(next_direct));
}
break;
}
case MilesEdgeworth::WALK:
{
double randnum = QRandomGenerator::global()->generateDouble();
if (randnum <= 0.2) { // 0.2概率接同向跑步
next_type = MilesEdgeworth::RUN;
next_direct = direction;
petMovie->setFileName(QString(":/gifs/run/%1.gif").arg(next_direct));
}
else if (randnum < 0.5) { // 0.3概率接随机走路
next_type = MilesEdgeworth::WALK;
next_direct = QRandomGenerator::global()->bounded(WALK_NUM);
petMovie->setFileName(QString(":/gifs/walk/%1.gif").arg(next_direct));
}
else { // 0.5概率接站立
next_type = MilesEdgeworth::STAND;
next_direct = direction % 2;
petMovie->setFileName(QString(":/gifs/stand/%1.gif").arg(next_direct));
}
break;
}
case MilesEdgeworth::TAKETHAT:
case MilesEdgeworth::ONCE:
{
// 若当前动作为单次动作, 则停止后立刻切换为站立, 方向保持不变
next_type = MilesEdgeworth::STAND;
next_direct = direction % 2;
petMovie->setFileName(QString(":/gifs/stand/%1.gif").arg(next_direct));
break;
}
case MilesEdgeworth::STAND:
{
double randnum = QRandomGenerator::global()->generateDouble();
if (randnum < 0.08) {
// 0.08的概率直接切换为反向站立
next_type = MilesEdgeworth::STAND;
next_direct = 1 - direction;
petMovie->setFileName(QString(":/gifs/stand/%1.gif").arg(next_direct));
}
else if (randnum < 0.2) {
// 0.12的概率切换转身动画
next_type = MilesEdgeworth::ONCE;
next_direct = 1 - direction;
petMovie->setFileName(QString(":/gifs/special/turn%1.gif").arg(direction));
}
else if (randnum < 0.8) { //0.8
// 0.6的概率切换为单次动作, 方向不变
next_type = MilesEdgeworth::ONCE;
next_direct = direction;
int num = QRandomGenerator::global()->bounded(ONCE_NUM);
num = (direction + 2 * num) % ONCE_NUM;
petMovie->setFileName(QString(":/gifs/once/%1.gif").arg(num));
}
else if (randnum < 0.9) { //0.9
// 0.1的概率切换为走路
next_type = MilesEdgeworth::WALK;
next_direct = QRandomGenerator::global()->bounded(WALK_NUM);
petMovie->setFileName(QString(":/gifs/walk/%1.gif").arg(next_direct));
}
else {
// 0.1的概率切换为跑步
next_type = MilesEdgeworth::RUN;
next_direct = QRandomGenerator::global()->bounded(RUN_NUM);
petMovie->setFileName(QString(":/gifs/run/%1.gif").arg(next_direct));
}
break;
}
}
type = next_type;
direction = next_direct;
petMovie->start();
}
// 切换为指定的Gif. 参数为: 下一动作文件名, 下一动作类型, 下一动作方向
void MilesEdgeworth::specifyChangeGif(const QString& filename, Type next_type, int next_direct)
{
type = next_type;
direction = next_direct;
petMovie->setFileName(filename);
petMovie->start();
}
// 处理跑步时的窗口移动
void MilesEdgeworth::runMove()
{
int unit = 4;
QPoint delta = QPoint(0, 0);
switch (direction) {
case 0:
{
// 向右走
delta = (QPointF(unit, 0) * scale * 1.4).toPoint();
break;
}
case 1:
{
// 向左走
delta = (QPointF(-unit, 0) * scale * 1.4).toPoint();
break;
}
case 2:
{
// 向右上走
delta = (QPointF(unit, -unit) * scale).toPoint();
break;
}
case 3:
{
// 向左上走
delta = (QPointF(-unit, -unit) * scale).toPoint();
break;
}
case 4:
{
// 向右下走
delta = (QPointF(unit, unit) * scale).toPoint();
break;
}
case 5:
{
// 向左下走
delta = (QPointF(-unit, unit) * scale).toPoint();
break;
}
case 6:
{
// 向上走
delta = (QPointF(0, -unit) * scale).toPoint();
break;
}
case 7:
{
// 向下走
delta = (QPointF(0, unit) * scale).toPoint();
break;
}
}
if (leftPrimary->isChecked() && QGuiApplication::screens().at(1) != nullptr) {
int primaryXMax = QGuiApplication::screens().at(0)->geometry().x() + QGuiApplication::screens().at(0)->geometry().width();
int secondXMin = QGuiApplication::screens().at(1)->geometry().x();
int newPosX = pos().x() + delta.x();
if (pos().x() + 50 * scale <= primaryXMax && newPosX + 50 * scale > primaryXMax) {
// 此时为从主屏幕跨屏到第二屏幕的瞬间
newPosX = secondXMin - 40 * scale;
}
else if (pos().x() + 50 * scale >= secondXMin && newPosX + 50 * scale < secondXMin) {
// 此时为从第二屏幕跨屏到第一屏幕的瞬间
newPosX = primaryXMax - 60 * scale;
}
int newPosY = pos().y() + delta.y();
move(newPosX, newPosY);
}
else if (rightPrimary->isChecked() && QGuiApplication::screens().at(1) != nullptr) {
// 主屏幕在右侧时, 跨屏时要注意坐标
int secondXMax = QGuiApplication::screens().at(1)->geometry().x() + QGuiApplication::screens().at(1)->geometry().width();
int primaryXMin = QGuiApplication::screens().at(0)->geometry().x();
int newPosX = pos().x() + delta.x();
if (pos().x() + 50 * scale >= primaryXMin && newPosX + 50 * scale < primaryXMin) {
// 此时为从主屏幕跨屏到第二屏幕的瞬间
newPosX = secondXMax - 60 * scale;
qDebug() << "此时为从主屏幕跨屏到第二屏幕的瞬间, newPosX: " << newPosX;
}
else if (pos().x() + 50 * scale <= secondXMax && newPosX + 50 * scale > secondXMax) {
// 此时为从第二屏幕跨屏到第一屏幕的瞬间
newPosX = primaryXMin - 40 * scale;
qDebug() << "此时为第二屏幕跨屏主屏幕的瞬间, newPosX: " << newPosX;
}
int newPosY = pos().y() + delta.y();
move(newPosX, newPosY);
}
else {
// 单屏或者主屏幕在左侧时, 正常move就行
move(pos() + delta);
}
}
// 处理走路时的窗口移动
void MilesEdgeworth::walkMove()
{
int unit = 3;
QPoint delta = QPoint(0, 0);
switch (direction) {
case 0:
{
// 向右走
delta = (QPointF(unit, 0) * scale * 1.4).toPoint();
break;
}
case 1:
{
// 向左走
delta = (QPointF(-unit, 0) * scale * 1.4).toPoint();
break;
}
case 2:
{
// 向右上走
delta = (QPointF(unit, -unit) * scale).toPoint();
break;
}
case 3:
{
// 向左上走
delta = (QPointF(-unit, -unit) * scale).toPoint();
break;
}
case 4:
{
// 向右下走
delta = (QPointF(unit, unit) * scale).toPoint();
break;
}
case 5:
{
// 向左下走
delta = (QPointF(-unit, unit) * scale).toPoint();
break;
}
case 6:
{
// 向上走
delta = (QPointF(0, -unit) * scale).toPoint();
break;
}
case 7:
{
// 向下走
delta = (QPointF(0, unit) * scale).toPoint();
break;
}
}
if (leftPrimary->isChecked() && QGuiApplication::screens().at(1) != nullptr) {
int primaryXMax = QGuiApplication::primaryScreen()->geometry().x() + QGuiApplication::primaryScreen()->geometry().width();
int secondXMin = QGuiApplication::screens().at(1)->geometry().x();
int newPosX = pos().x() + delta.x();
if (pos().x() + 50 * scale <= primaryXMax && newPosX + 50 * scale > primaryXMax) {
// 此时为从主屏幕跨屏到第二屏幕的瞬间
newPosX = secondXMin - 50 * scale;
}
else if (pos().x() + 50 * scale >= secondXMin && newPosX + 50 * scale < secondXMin) {
// 此时为从第二屏幕跨屏到第一屏幕的瞬间
newPosX = primaryXMax - 50 * scale;
}
int newPosY = pos().y() + delta.y();
move(newPosX, newPosY);
}
else if (rightPrimary->isChecked() && QGuiApplication::screens().at(1) != nullptr) {
// 主屏幕在右侧时, 跨屏时要注意坐标
int secondXMax = QGuiApplication::screens().at(1)->geometry().x() + QGuiApplication::screens().at(1)->geometry().width();
int primaryXMin = QGuiApplication::screens().at(0)->geometry().x();
int newPosX = pos().x() + delta.x();
if (pos().x() + 50 * scale >= primaryXMin && newPosX + 50 * scale < primaryXMin) {
// 此时为从主屏幕跨屏到第二屏幕的瞬间
newPosX = secondXMax - 60 * scale;
qDebug() << "此时为从主屏幕跨屏到第二屏幕的瞬间, newPosX: " << newPosX;
}
else if (pos().x() + 50 * scale <= secondXMax && newPosX + 50 * scale > secondXMax) {
// 此时为从第二屏幕跨屏到第一屏幕的瞬间
newPosX = primaryXMin - 40 * scale;
qDebug() << "此时为第二屏幕跨屏主屏幕的瞬间, newPosX: " << newPosX;
}
int newPosY = pos().y() + delta.y();
move(newPosX, newPosY);
}
else {
// 单屏或者主屏幕在左侧时, 正常move就行
move(pos() + delta);
}
}
// 显示检察官徽章
void MilesEdgeworth::showProsBadge()
{
QTimer::singleShot(700, [=](){
if (type == MilesEdgeworth::TAKETHAT) {
prosbadge->setScale(scale);
if (direction == 0) {
// 向右时, 设置初始位置
prosbadge->setGeometry(this->geometry().x() + 86 * scale, this->geometry().y() + 16 * scale, 94, 94);
}
else {
// 向左时, 设置初始位置
prosbadge->setGeometry(this->geometry().x() + 1 * scale, this->geometry().y() + 16 * scale, 94, 94);
}
prosbadge->show();
prosbadge->moveAnimation(direction);
badgeTimer->start();
}
});
}
// 处理双击事件
void MilesEdgeworth::doubleClickEvent()
{
QString filename;
petMovie->stop();
if (type == MilesEdgeworth::SLEEPING) {
// 唤醒
filename = QString(":/gifs/special/wake%1.gif").arg(direction);
specifyChangeGif(filename, MilesEdgeworth::ONCE, direction);
actionSleep->setText("睡觉");
actionTea->setEnabled(true);
}
else {
Type next_type = MilesEdgeworth::ONCE;
double randnum = QRandomGenerator::global()->generateDouble();
if (randnum < 0.25) { // 等等
filename = QString(":/gifs/special/crossed%1.gif").arg(direction % 2);
soundEffect->setSource(QUrl::fromLocalFile(QString(":/audios/holdit%1.wav").arg(language)));
}
else if (randnum < 0.5) { // 看招
filename = QString(":/gifs/special/object%1.gif").arg(direction % 2);
next_type = MilesEdgeworth::TAKETHAT;
soundEffect->setSource(QUrl::fromLocalFile(QString(":/audios/takethat%1.wav").arg(language)));
// 飞出检察官徽章
showProsBadge();
}
else if (randnum < 0.75 || language == 2) { // 异议
filename = QString(":/gifs/special/object%1.gif").arg(direction % 2);
soundEffect->setSource(QUrl::fromLocalFile(QString(":/audios/objection%1.wav").arg(language)));
}
else { // koreda
filename = QString(":/gifs/special/object%1.gif").arg(direction % 2);
soundEffect->setSource(QUrl::fromLocalFile(QString(":/audios/eureka%1.wav").arg(language)));
}
// 更改动画
specifyChangeGif(filename, next_type, direction % 2);
// 播放音频
//soundEffect->play();
if (soundEffect->isPlaying()) {
emit soundStop();
}
emit soundPlay();
}
}
// 处理单击事件
void MilesEdgeworth::singleClickEvent()
{
if (type == MilesEdgeworth::SLEEPING || type == MilesEdgeworth::SLEEP) {
// 睡觉状态下, 单击无反应.
return;
}
else if (type != MilesEdgeworth::STAND) {
// 非站立也非睡觉时, 转为站立动画
petMovie->stop();
specifyChangeGif(QString(":/gifs/stand/%1.gif").arg(direction % 2),
MilesEdgeworth::STAND, direction % 2);
}
else {
// 站立时, 分区域有不同的反应
double x = m_pressPos.x();