-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimerManager.cpp
More file actions
2520 lines (2133 loc) · 72.8 KB
/
Copy pathTimerManager.cpp
File metadata and controls
2520 lines (2133 loc) · 72.8 KB
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 "TimerManager.h"
#include "SDF.h"
#include <sstream>
#include <iomanip>
#include <cmath>
using namespace std;
//自定义计时器对话框实现
CustomTimerDialog::CustomTimerDialog()
{
dialog_width = 350;
dialog_height = 260;
dialog_x = (520 - dialog_width) / 2;
dialog_y = (680 - dialog_height) / 2;
hours = 0;
minutes = 0;
seconds = 0;
label_input = L"";
ringtone_name = L"做个文明中国人";
current_focus = TIMER_FOCUS_NONE;
is_editing_label = false;
hour_input_buffer = -1;
minute_input_buffer = -1;
second_input_buffer = -1;
last_input_time = 0;
show_ringtone_menu = false;
ringtone_menu_y = 0;
ringtone_scroll_offset = 0;
ringtone_hovered_index = -1;
is_visible = false;
is_confirmed = false;
AlarmSound::get_all_ringtones(&ringtone_list, ringtone_count);
}
CustomTimerDialog::~CustomTimerDialog()
{
if (ringtone_list != nullptr)
{
delete[] ringtone_list;
}
}
void CustomTimerDialog::show()
{
is_visible = true;
is_confirmed = false;
hours = 0;
minutes = 0;
seconds = 0;
label_input = L"";
ringtone_name = L"做个文明中国人";
current_focus = TIMER_FOCUS_NONE;
is_editing_label = false;
show_ringtone_menu = false;
}
void CustomTimerDialog::hide()
{
is_visible = false;
show_ringtone_menu = false;
}
void CustomTimerDialog::draw()
{
if (!is_visible) return;
//绘制半透明背景
setfillcolor(RGB(0, 0, 0));
for (int y = 0; y < 680; y += 2)
{
for (int x = 0; x < 520; x += 2)
{
putpixel(x, y, RGB(0, 0, 0));
}
}
//绘制对话框背景
setfillcolor(RGB(45, 45, 45));
solidroundrect(dialog_x, dialog_y, dialog_x + dialog_width, dialog_y + dialog_height, 15, 15);
setlinecolor(RGB(80, 80, 80));
setlinestyle(PS_SOLID, 1);
roundrect(dialog_x, dialog_y, dialog_x + dialog_width, dialog_y + dialog_height, 15, 15);
draw_time_section();
draw_label_section();
draw_ringtone_section();
draw_buttons();
if (show_ringtone_menu)
{
draw_ringtone_menu();
}
}
void CustomTimerDialog::draw_time_section()
{
int time_y = dialog_y + 25;
//绘制小时分钟秒标签
settextstyle(12, 0, L"微软雅黑");
settextcolor(RGB(154, 154, 154));
setbkmode(TRANSPARENT);
int digit_width = 45;
int colon_width = 15;
int total_width = digit_width * 3 + colon_width * 2;
int start_x = dialog_x + (dialog_width - total_width) / 2;
outtextxy(start_x + digit_width / 2 - 12, time_y, L"小时");
outtextxy(start_x + digit_width + colon_width + digit_width / 2 - 12, time_y, L"分钟");
outtextxy(start_x + digit_width * 2 + colon_width * 2 + digit_width / 2 - 6, time_y, L"秒");
//绘制时间数字
int digits_y = time_y + 20;
settextstyle(40, 0, L"微软雅黑");
wstringstream wss;
wss << setfill(L'0') << setw(2) << hours;
wstring hour_str = wss.str();
wss.str(L"");
wss << setfill(L'0') << setw(2) << minutes;
wstring min_str = wss.str();
wss.str(L"");
wss << setfill(L'0') << setw(2) << seconds;
wstring sec_str = wss.str();
//小时背景
if (current_focus == TIMER_FOCUS_HOUR)
{
setfillcolor(RGB(192, 123, 62));
solidroundrect(start_x - 2, digits_y - 2, start_x + digit_width + 2, digits_y + 42, 5, 5);
}
settextcolor(RGB(221, 221, 221));
outtextxy(start_x + 5, digits_y, hour_str.c_str());
//冒号
outtextxy(start_x + digit_width + 2, digits_y, L":");
//分钟背景
if (current_focus == TIMER_FOCUS_MINUTE)
{
setfillcolor(RGB(192, 123, 62));
solidroundrect(start_x + digit_width + colon_width - 2, digits_y - 2, start_x + digit_width * 2 + colon_width + 2, digits_y + 42, 5, 5);
}
settextcolor(RGB(221, 221, 221));
outtextxy(start_x + digit_width + colon_width + 5, digits_y, min_str.c_str());
//冒号
outtextxy(start_x + digit_width * 2 + colon_width + 2, digits_y, L":");
//秒背景
if (current_focus == TIMER_FOCUS_SECOND)
{
setfillcolor(RGB(192, 123, 62));
solidroundrect(start_x + digit_width * 2 + colon_width * 2 - 2, digits_y - 2,start_x + digit_width * 3 + colon_width * 2 + 2, digits_y + 42, 5, 5);
}
settextcolor(RGB(221, 221, 221));
outtextxy(start_x + digit_width * 2 + colon_width * 2 + 5, digits_y, sec_str.c_str());
}
void CustomTimerDialog::draw_label_section()
{
int label_y = dialog_y + 100;
int label_x = dialog_x + 20;
int input_x = dialog_x + 100;
int input_width = dialog_width - 120;
int input_height = 28;
//标签文字
settextstyle(14, 0, L"微软雅黑");
settextcolor(RGB(180, 180, 180));
setbkmode(TRANSPARENT);
outtextxy(label_x, label_y + 6, L"标签:");
//输入框
setfillcolor(RGB(60, 60, 60));
solidroundrect(input_x, label_y, input_x + input_width, label_y + input_height, 5, 5);
if (is_editing_label)
{
setlinecolor(RGB(192, 123, 62));
setlinestyle(PS_SOLID, 1);
roundrect(input_x, label_y, input_x + input_width, label_y + input_height, 5, 5);
}
if (label_input.empty())
{
settextcolor(RGB(86, 86, 86));
outtextxy(input_x + 8, label_y + 6, L"计时器");
}
else
{
settextcolor(RGB(221, 221, 221));
outtextxy(input_x + 8, label_y + 6, label_input.c_str());
}
}
void CustomTimerDialog::draw_ringtone_section()
{
int ringtone_y = dialog_y + 140;
int label_x = dialog_x + 20;
int input_x = dialog_x + 100;
int input_width = dialog_width - 120;
int input_height = 28;
//标签文字(与"标签:"的冒号对齐)
settextstyle(14, 0, L"微软雅黑");
settextcolor(RGB(180, 180, 180));
setbkmode(TRANSPARENT);
outtextxy(label_x - 56, ringtone_y + 6, L"计时结束时启用:");
//选择框
setfillcolor(RGB(60, 60, 60));
solidroundrect(input_x, ringtone_y, input_x + input_width, ringtone_y + input_height, 5, 5);
settextcolor(RGB(221, 221, 221));
outtextxy(input_x + 8, ringtone_y + 6, ringtone_name.c_str());
settextcolor(RGB(150, 150, 150));
outtextxy(input_x + input_width - 20, ringtone_y + 6, L"▼");
ringtone_menu_y = ringtone_y + input_height + 5;
}
void CustomTimerDialog::draw_buttons()
{
int button_y = dialog_y + dialog_height - 50;
int button_width = 100;
int button_height = 32;
int spacing = 20;
int cancel_x = dialog_x + dialog_width / 2 - button_width - spacing / 2;
int start_x = dialog_x + dialog_width / 2 + spacing / 2;
//取消按钮
setfillcolor(RGB(39, 39, 39));
solidroundrect(cancel_x, button_y, cancel_x + button_width, button_y + button_height,
button_height / 2, button_height / 2);
settextstyle(14, 0, L"微软雅黑");
settextcolor(RGB(92, 92, 92));
setbkmode(TRANSPARENT);
wstring cancel_text = L"取消";
int cancel_text_width = textwidth(cancel_text.c_str());
outtextxy(cancel_x + (button_width - cancel_text_width) / 2, button_y + 8, cancel_text.c_str());
//开始计时按钮
bool can_start = (hours > 0 || minutes > 0 || seconds > 0);
if (can_start)
{
setfillcolor(RGB(104, 206, 103));
settextcolor(RGB(255, 255, 255));
}
else
{
setfillcolor(RGB(34, 39, 34));
settextcolor(RGB(92, 92, 92));
}
solidroundrect(start_x, button_y, start_x + button_width, button_y + button_height,
button_height / 2, button_height / 2);
wstring start_text = L"开始计时";
int start_text_width = textwidth(start_text.c_str());
outtextxy(start_x + (button_width - start_text_width) / 2, button_y + 8, start_text.c_str());
}
void CustomTimerDialog::draw_ringtone_menu()
{
int menu_x = dialog_x + 100;
int menu_width = dialog_width - 120;
int item_height = 30;
int menu_height = min(ringtone_count * item_height, 150);
setfillcolor(RGB(60, 60, 60));
solidroundrect(menu_x, ringtone_menu_y, menu_x + menu_width, ringtone_menu_y + menu_height, 5, 5);
setlinecolor(RGB(100, 100, 100));
setlinestyle(PS_SOLID, 1);
roundrect(menu_x, ringtone_menu_y, menu_x + menu_width, ringtone_menu_y + menu_height, 5, 5);
HDC hdc = GetImageHDC(NULL);
HRGN clip_region = CreateRectRgn(menu_x, ringtone_menu_y, menu_x + menu_width, ringtone_menu_y + menu_height);
SelectClipRgn(hdc, clip_region);
for (int i = 0; i < ringtone_count; i++)
{
int item_y = ringtone_menu_y + i * item_height + ringtone_scroll_offset;
if (item_y + item_height < ringtone_menu_y || item_y > ringtone_menu_y + menu_height)
continue;
if (i == ringtone_hovered_index)
{
setfillcolor(RGB(80, 80, 80));
solidrectangle(menu_x + 2, item_y + 2, menu_x + menu_width - 2, item_y + item_height - 2);
}
settextcolor(WHITE);
settextstyle(14, 0, L"微软雅黑");
setbkmode(TRANSPARENT);
outtextxy(menu_x + 10, item_y + 7, ringtone_list[i].display_name.c_str());
}
SelectClipRgn(hdc, NULL);
DeleteObject(clip_region);
}
bool CustomTimerDialog::handle_mouse_click(int mouse_x, int mouse_y)
{
if (!is_visible) return false;
//处理铃声菜单点击
if (show_ringtone_menu)
{
int menu_x = dialog_x + 100;
int menu_width = dialog_width - 120;
int item_height = 30;
int menu_height = min(ringtone_count * item_height, 150);
if (mouse_x >= menu_x && mouse_x <= menu_x + menu_width && mouse_y >= ringtone_menu_y && mouse_y <= ringtone_menu_y + menu_height)
{
int relative_y = mouse_y - ringtone_menu_y - ringtone_scroll_offset;
int index = relative_y / item_height;
if (index >= 0 && index < ringtone_count)
{
ringtone_name = ringtone_list[index].display_name;
show_ringtone_menu = false;
return true;
}
}
else
{
show_ringtone_menu = false;
return true;
}
}
//检查对话框外点击
if (mouse_x < dialog_x || mouse_x > dialog_x + dialog_width || mouse_y < dialog_y || mouse_y > dialog_y + dialog_height)
{
hide();
return true;
}
//检查时间数字点击
int time_y = dialog_y + 25;
int digits_y = time_y + 20;
int digit_width = 45;
int colon_width = 15;
int total_width = digit_width * 3 + colon_width * 2;
int start_x = dialog_x + (dialog_width - total_width) / 2;
if (mouse_y >= digits_y - 2 && mouse_y <= digits_y + 42)
{
if (mouse_x >= start_x - 2 && mouse_x <= start_x + digit_width + 2)
{
current_focus = TIMER_FOCUS_HOUR;
is_editing_label = false;
hour_input_buffer = -1;
return true;
}
else if (mouse_x >= start_x + digit_width + colon_width - 2 && mouse_x <= start_x + digit_width * 2 + colon_width + 2)
{
current_focus = TIMER_FOCUS_MINUTE;
is_editing_label = false;
minute_input_buffer = -1;
return true;
}
else if (mouse_x >= start_x + digit_width * 2 + colon_width * 2 - 2 && mouse_x <= start_x + digit_width * 3 + colon_width * 2 + 2)
{
current_focus = TIMER_FOCUS_SECOND;
is_editing_label = false;
second_input_buffer = -1;
return true;
}
}
//检查标签输入框点击
int label_y = dialog_y + 100;
int input_x = dialog_x + 100;
int input_width = dialog_width - 120;
int input_height = 28;
if (mouse_x >= input_x && mouse_x <= input_x + input_width && mouse_y >= label_y && mouse_y <= label_y + input_height)
{
is_editing_label = true;
current_focus = TIMER_FOCUS_NONE;
return true;
}
else
{
is_editing_label = false;
}
//检查铃声选择框点击
int ringtone_y = dialog_y + 140;
if (mouse_x >= input_x && mouse_x <= input_x + input_width && mouse_y >= ringtone_y && mouse_y <= ringtone_y + input_height)
{
show_ringtone_menu = !show_ringtone_menu;
ringtone_scroll_offset = 0;
return true;
}
//检查按钮点击
int button_y = dialog_y + dialog_height - 50;
int button_width = 100;
int button_height = 32;
int spacing = 20;
int cancel_x = dialog_x + dialog_width / 2 - button_width - spacing / 2;
int start_btn_x = dialog_x + dialog_width / 2 + spacing / 2;
if (mouse_y >= button_y && mouse_y <= button_y + button_height)
{
if (mouse_x >= cancel_x && mouse_x <= cancel_x + button_width)
{
hide();
return true;
}
else if (mouse_x >= start_btn_x && mouse_x <= start_btn_x + button_width)
{
bool can_start = (hours > 0 || minutes > 0 || seconds > 0);
if (can_start)
{
is_confirmed = true;
result_timer.hours = hours;
result_timer.minutes = minutes;
result_timer.seconds = seconds;
result_timer.label = label_input.empty() ? L"计时器" : label_input;
result_timer.ringtone_name = ringtone_name;
hide();
return true;
}
}
}
return true;
}
void CustomTimerDialog::handle_mouse_move(int mouse_x, int mouse_y)
{
if (!is_visible || !show_ringtone_menu) return;
int menu_x = dialog_x + 100;
int menu_width = dialog_width - 120;
int item_height = 30;
int menu_height = min(ringtone_count * item_height, 150);
ringtone_hovered_index = -1;
if (mouse_x >= menu_x && mouse_x <= menu_x + menu_width &&
mouse_y >= ringtone_menu_y && mouse_y <= ringtone_menu_y + menu_height)
{
int relative_y = mouse_y - ringtone_menu_y - ringtone_scroll_offset;
ringtone_hovered_index = relative_y / item_height;
if (ringtone_hovered_index < 0 || ringtone_hovered_index >= ringtone_count)
{
ringtone_hovered_index = -1;
}
}
}
void CustomTimerDialog::handle_mouse_wheel(int delta)
{
if (!is_visible || !show_ringtone_menu) return;
int item_height = 30;
int menu_height = min(ringtone_count * item_height, 150);
int max_scroll = ringtone_count * item_height - menu_height;
ringtone_scroll_offset += delta / 2;
if (ringtone_scroll_offset > 0) ringtone_scroll_offset = 0;
if (ringtone_scroll_offset < -max_scroll) ringtone_scroll_offset = -max_scroll;
}
void CustomTimerDialog::process_virtual_key(int vk)
{
if (!is_visible) return;
if (vk == VK_ESCAPE)
{
hide();
return;
}
if (vk == VK_BACK)
{
if (is_editing_label && !label_input.empty())
{
label_input.pop_back();
}
return;
}
if ((vk == VK_UP || vk == VK_DOWN) && current_focus != TIMER_FOCUS_NONE && !is_editing_label)
{
bool is_up = (vk == VK_UP);
if (current_focus == TIMER_FOCUS_HOUR)
{
if (is_up) { hours++; if (hours > 99) hours = 0; }
else { hours--; if (hours < 0) hours = 99; }
hour_input_buffer = -1;
}
else if (current_focus == TIMER_FOCUS_MINUTE)
{
if (is_up) { minutes++; if (minutes > 59) minutes = 0; }
else { minutes--; if (minutes < 0) minutes = 59; }
minute_input_buffer = -1;
}
else if (current_focus == TIMER_FOCUS_SECOND)
{
if (is_up) { seconds++; if (seconds > 59) seconds = 0; }
else { seconds--; if (seconds < 0) seconds = 59; }
second_input_buffer = -1;
}
return;
}
int digit = -1;
if (vk >= '0' && vk <= '9') digit = vk - '0';
else if (vk >= VK_NUMPAD0 && vk <= VK_NUMPAD9) digit = vk - VK_NUMPAD0;
if (digit >= 0 && current_focus != TIMER_FOCUS_NONE && !is_editing_label)
{
time_t now = time(0);
if (current_focus == TIMER_FOCUS_HOUR)
{
if (hour_input_buffer == -1 || (now - last_input_time) >= 1)
{
hour_input_buffer = digit;
last_input_time = now;
hours = digit;
}
else
{
int combined = hour_input_buffer * 10 + digit;
if (combined >= 0 && combined <= 99)
{
hours = combined;
hour_input_buffer = -1;
}
else
{
hour_input_buffer = digit;
last_input_time = now;
hours = digit;
}
}
}
else if (current_focus == TIMER_FOCUS_MINUTE)
{
if (minute_input_buffer == -1 || (now - last_input_time) >= 1)
{
minute_input_buffer = digit;
last_input_time = now;
minutes = digit;
}
else
{
int combined = minute_input_buffer * 10 + digit;
if (combined >= 0 && combined <= 59)
{
minutes = combined;
minute_input_buffer = -1;
}
else
{
minute_input_buffer = digit;
last_input_time = now;
minutes = digit;
}
}
}
else if (current_focus == TIMER_FOCUS_SECOND)
{
if (second_input_buffer == -1 || (now - last_input_time) >= 1)
{
second_input_buffer = digit;
last_input_time = now;
seconds = digit;
}
else
{
int combined = second_input_buffer * 10 + digit;
if (combined >= 0 && combined <= 59)
{
seconds = combined;
second_input_buffer = -1;
}
else
{
second_input_buffer = digit;
last_input_time = now;
seconds = digit;
}
}
}
return;
}
if (vk == VK_SPACE && is_editing_label)
{
label_input += L' ';
return;
}
if (is_editing_label && vk >= 'A' && vk <= 'Z')
{
bool shift = (GetAsyncKeyState(VK_SHIFT) & 0x8000) != 0;
wchar_t ch = (wchar_t)(shift ? vk : (vk - 'A' + 'a'));
label_input += ch;
return;
}
}
Timer CustomTimerDialog::get_timer()
{
return result_timer;
}
//到552左右是构造函数和铃声菜单
//铃声选择菜单实现
TimerRingtoneMenu::TimerRingtoneMenu()
{
menu_width = 250;
item_height = 40;
hovered_index = -1;
scroll_offset = 0;
AlarmSound::get_all_ringtones(&ringtone_list, ringtone_count);
int max_height = (680 - 60) * 3 / 4;
int full_height = ringtone_count * item_height;
if (full_height > max_height)
{
menu_height = max_height;
can_scroll = true;
}
else
{
menu_height = full_height;
can_scroll = false;
}
}
TimerRingtoneMenu::~TimerRingtoneMenu()
{
if (ringtone_list != nullptr)
{
delete[] ringtone_list;
}
}
void TimerRingtoneMenu::show(int x, int y)
{
menu_x = x;
menu_y = y;
if (menu_x + menu_width > 520)
{
menu_x = 520 - menu_width;
}
if (menu_y + menu_height > 680)
{
menu_y = 680 - menu_height;
}
}
void TimerRingtoneMenu::draw()
{
setfillcolor(RGB(60, 60, 60));
solidroundrect(menu_x, menu_y, menu_x + menu_width, menu_y + menu_height, 5, 5);
setlinecolor(RGB(100, 100, 100));
setlinestyle(PS_SOLID, 1); //确保边框细
roundrect(menu_x, menu_y, menu_x + menu_width, menu_y + menu_height, 5, 5);
HDC hdc = GetImageHDC(NULL);
HRGN clip_region = CreateRectRgn(menu_x, menu_y, menu_x + menu_width, menu_y + menu_height);
SelectClipRgn(hdc, clip_region);
for (int i = 0; i < ringtone_count; i++)
{
int item_y = menu_y + i * item_height + scroll_offset;
if (item_y + item_height < menu_y || item_y > menu_y + menu_height)
{
continue;
}
if (i == hovered_index)
{
setfillcolor(RGB(80, 80, 80));
solidrectangle(menu_x + 2, item_y + 2, menu_x + menu_width - 2, item_y + item_height - 2);
}
settextcolor(WHITE);
settextstyle(16, 0, L"微软雅黑");
setbkmode(TRANSPARENT);
outtextxy(menu_x + 15, item_y + 10, ringtone_list[i].display_name.c_str());
}
SelectClipRgn(hdc, NULL);
DeleteObject(clip_region);
}
void TimerRingtoneMenu::update_hover(int mouse_x, int mouse_y)
{
hovered_index = -1;
if (mouse_x < menu_x || mouse_x > menu_x + menu_width || mouse_y < menu_y || mouse_y > menu_y + menu_height)
{
return;
}
int relative_y = mouse_y - menu_y - scroll_offset;
hovered_index = relative_y / item_height;
if (hovered_index < 0 || hovered_index >= ringtone_count)
{
hovered_index = -1;
}
}
int TimerRingtoneMenu::check_click(int mouse_x, int mouse_y)
{
if (mouse_x < menu_x || mouse_x > menu_x + menu_width || mouse_y < menu_y || mouse_y > menu_y + menu_height)
{
return -1;
}
int relative_y = mouse_y - menu_y - scroll_offset;
int index = relative_y / item_height;
if (index >= 0 && index < ringtone_count)
{
return index;
}
return -1;
}
bool TimerRingtoneMenu::is_outside_click(int mouse_x, int mouse_y)
{
return (mouse_x < menu_x || mouse_x > menu_x + menu_width || mouse_y < menu_y || mouse_y > menu_y + menu_height);
}
void TimerRingtoneMenu::scroll(int delta)
{
if (!can_scroll) return;
scroll_offset += delta;
int max_scroll = 0;
int min_scroll = -(ringtone_count * item_height - menu_height);
if (scroll_offset > max_scroll) scroll_offset = max_scroll;
if (scroll_offset < min_scroll) scroll_offset = min_scroll;
}
//加号菜单实现
TimerAddMenu::TimerAddMenu()
{
menu_width = 180;
item_height = 40;
hovered_index = -1;
show_preset_submenu = false;
preset_hovered_index = -1;
//初始化预置项
preset_items.push_back(PresetMenuItem(L"1分钟", 1));
preset_items.push_back(PresetMenuItem(L"2分钟", 2));
preset_items.push_back(PresetMenuItem(L"3分钟", 3));
preset_items.push_back(PresetMenuItem(L"4分钟", 4));
preset_items.push_back(PresetMenuItem(L"5分钟", 5));
preset_items.push_back(PresetMenuItem(L"10分钟", 10));
preset_items.push_back(PresetMenuItem(L"15分钟", 15));
preset_items.push_back(PresetMenuItem(L"20分钟", 20));
preset_items.push_back(PresetMenuItem(L"30分钟", 30));
preset_items.push_back(PresetMenuItem(L"45分钟", 45));
preset_items.push_back(PresetMenuItem(L"1小时", 60));
preset_items.push_back(PresetMenuItem(L"2小时", 120));
preset_menu_width = 120;
preset_menu_height = preset_items.size() * item_height;
}
TimerAddMenu::~TimerAddMenu()
{
}
void TimerAddMenu::show(int x, int y, const vector<Timer>& recent_timers)
{
menu_x = x;
menu_y = y;
//清空菜单项
menu_items.clear();
//添加"自定义计时器"
AddMenuItem custom_item;
custom_item.type = MENU_CUSTOM;
custom_item.text = L"自定义计时器";
custom_item.menu_index = 0;
menu_items.push_back(custom_item);
//添加分隔线
AddMenuItem sep1;
sep1.type = MENU_SEPARATOR;
sep1.menu_index = 1;
menu_items.push_back(sep1);
//添加"预置"
AddMenuItem preset_item;
preset_item.type = MENU_PRESET_TITLE;
preset_item.text = L"预置";
preset_item.menu_index = 2;
menu_items.push_back(preset_item);
//如果有最近使用,添加标题和项
if (!recent_timers.empty())
{
AddMenuItem recent_title;
recent_title.type = MENU_RECENT_TITLE;
recent_title.text = L"最近使用";
recent_title.menu_index = 3;
menu_items.push_back(recent_title);
for (size_t i = 0; i < recent_timers.size(); i++)
{
AddMenuItem recent_item;
recent_item.type = MENU_RECENT_ITEM;
recent_item.timer = recent_timers[i];
recent_item.text = recent_timers[i].get_time_string();
recent_item.menu_index = 4 + i;
menu_items.push_back(recent_item);
}
}
//计算菜单高度
menu_height = 0;
for (size_t i = 0; i < menu_items.size(); i++)
{
if (menu_items[i].type == MENU_SEPARATOR)
{
menu_height += 1;
}
else if (menu_items[i].type == MENU_RECENT_TITLE)
{
menu_height += 30; //最近使用标题更小
}
else
{
menu_height += item_height;
}
}
//调整位置
if (menu_x + menu_width > 520)
{
menu_x = 520 - menu_width - 5;
}
if (menu_y + menu_height > 680)
{
menu_y = 680 - menu_height;
}
}
void TimerAddMenu::draw()
{
//绘制主菜单背景
setfillcolor(RGB(60, 60, 60));
solidroundrect(menu_x, menu_y, menu_x + menu_width, menu_y + menu_height, 5, 5);
setlinecolor(RGB(100, 100, 100));
setlinestyle(PS_SOLID, 1); //确保边框细
roundrect(menu_x, menu_y, menu_x + menu_width, menu_y + menu_height, 5, 5);
//绘制菜单项
int current_y = menu_y;
for (size_t i = 0; i < menu_items.size(); i++)
{
if (menu_items[i].type == MENU_SEPARATOR)
{
//绘制分隔线
setlinecolor(RGB(100, 100, 100));
setlinestyle(PS_SOLID, 1);
line(menu_x + 10, current_y, menu_x + menu_width - 10, current_y);
current_y += 1;
}
else if (menu_items[i].type == MENU_RECENT_TITLE)
{
//在最近使用标题前绘制分隔线(预置和最近使用之间)
setlinecolor(RGB(100, 100, 100));
setlinestyle(PS_SOLID, 1);
line(menu_x + 10, current_y, menu_x + menu_width - 10, current_y);
current_y += 1;
//绘制"最近使用"标题(更小的字体,灰色,居左)
settextcolor(RGB(84, 84, 84));
settextstyle(12, 0, L"微软雅黑");
setbkmode(TRANSPARENT);
outtextxy(menu_x + 15, current_y + 8, menu_items[i].text.c_str());
current_y += 30;
}
else
{
int item_height_actual = item_height;
//绘制悬停背景
if (menu_items[i].is_hovered)
{
setfillcolor(RGB(233, 150, 91));
solidroundrect(menu_x + 2, current_y + 2, menu_x + menu_width - 2, current_y + item_height_actual - 2, 5, 5);
}
//绘制文字
settextcolor(WHITE);
settextstyle(16, 0, L"微软雅黑");
setbkmode(TRANSPARENT);
outtextxy(menu_x + 15, current_y + 10, menu_items[i].text.c_str());
//如果是预置项,绘制箭头
if (menu_items[i].type == MENU_PRESET_TITLE)
{
settextcolor(RGB(150, 150, 150));
outtextxy(menu_x + menu_width - 25, current_y + 10, L"▶");
}
current_y += item_height_actual;
}
}
//绘制预置子菜单
if (show_preset_submenu)
{
setfillcolor(RGB(60, 60, 60));
solidroundrect(preset_menu_x, preset_menu_y, preset_menu_x + preset_menu_width, preset_menu_y + preset_menu_height, 5, 5);
setlinecolor(RGB(100, 100, 100));
setlinestyle(PS_SOLID, 1); //确保边框细
roundrect(preset_menu_x, preset_menu_y, preset_menu_x + preset_menu_width, preset_menu_y + preset_menu_height, 5, 5);
for (size_t i = 0; i < preset_items.size(); i++)
{
int item_y = preset_menu_y + i * item_height;
if (preset_items[i].is_hovered)
{
setfillcolor(RGB(233, 150, 91));
solidroundrect(preset_menu_x + 2, item_y + 2, preset_menu_x + preset_menu_width - 2, item_y + item_height - 2, 5, 5);
}
settextcolor(WHITE);
settextstyle(16, 0, L"微软雅黑");
setbkmode(TRANSPARENT);
outtextxy(preset_menu_x + 15, item_y + 10, preset_items[i].text.c_str());
}
}
}
void TimerAddMenu::update_hover(int mouse_x, int mouse_y)
{
//重置所有悬停状态
for (size_t i = 0; i < menu_items.size(); i++)
{
menu_items[i].is_hovered = false;
}
//重置所有预置项悬停状态
for (size_t i = 0; i < preset_items.size(); i++)
{
preset_items[i].is_hovered = false;
}
hovered_index = -1;
preset_hovered_index = -1;
//检查预置子菜单悬停
if (show_preset_submenu)
{