-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClock.cpp
More file actions
183 lines (149 loc) · 5.5 KB
/
Copy pathClock.cpp
File metadata and controls
183 lines (149 loc) · 5.5 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
#include "Clock.h"
#include "SDF.h"
#include <cmath>
#include <sstream>
#include <iomanip>
const double PI = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381;
Clock::Clock(int x, int y, int r)
: centerX(x), centerY(y), radius(r), hour(0), minute(0), second(0),
city_name(L"北京"), use_antialiasing(true)
{
current_timezone = TimeZone::get_timezone_by_city(L"北京");
}
Clock::~Clock()
{
}
// 根据当前时区的实际小时数判断日夜
bool Clock::is_night_mode()
{
// 获取当前时区的完整时间
time_t now = time(0);
struct tm t;
gmtime_s(&t, &now);
int month = t.tm_mon + 1;
int day = t.tm_mday;
int utc_offset = TimeZone::get_utc_offset(current_timezone, month, day);
now += utc_offset * 3600;
gmtime_s(&t, &now);
int full_hour = t.tm_hour; // 使用24小时制的完整小时数
return (full_hour >= 18 || full_hour < 6); // 18:00-6:00 为夜间
}
void Clock::draw_clock_face()
{
bool is_night = is_night_mode();
COLORREF face_color = is_night ? BLACK : WHITE;
COLORREF mark_color = is_night ? WHITE : BLACK;
if (use_antialiasing)
{
SDF::draw_aa_circle(centerX, centerY, radius, face_color);
SDF::draw_aa_ring(centerX, centerY, radius, 2, mark_color);
}
else
{
setfillcolor(face_color);
solidcircle(centerX, centerY, radius);
setlinecolor(mark_color);
setlinestyle(PS_SOLID, 2);
circle(centerX, centerY, radius);
}
//绘制12个小时数字
settextcolor(mark_color);
settextstyle(14, 0, L"Arial"); //字号14(半径56时合适)
setbkmode(TRANSPARENT);
for (int i = 1; i <= 12; i++)
{
double angle = i * PI / 6 - PI / 2;
int text_x = centerX + (int)((radius - 20) * cos(angle)); //距离边缘20px(数字靠边)
int text_y = centerY + (int)((radius - 20) * sin(angle));
TCHAR num[3];
_stprintf_s(num, _T("%d"), i);
int text_width = textwidth(num);
int text_height = textheight(num);
outtextxy(text_x - text_width / 2, text_y - text_height / 2, num);
}
//绘制整点刻度(12个)
for (int i = 0; i < 12; i++)
{
double angle = i * PI / 6 - PI / 2;
int length = 10; //刻度长度10px
int width = 2; //刻度宽度2px
int x1 = centerX + (int)((radius - length) * cos(angle));
int y1 = centerY + (int)((radius - length) * sin(angle));
int x2 = centerX + (int)((radius - 2) * cos(angle));
int y2 = centerY + (int)((radius - 2) * sin(angle));
if (use_antialiasing)
{
SDF::draw_aa_line(x1, y1, x2, y2, width, mark_color);
}
else
{
setlinecolor(mark_color);
setlinestyle(PS_SOLID, width);
line(x1, y1, x2, y2);
}
}
}
void Clock::update_time()
{
time_t now = time(0);
struct tm t;
gmtime_s(&t, &now);
int month = t.tm_mon + 1;
int day = t.tm_mday;
int utc_offset = TimeZone::get_utc_offset(current_timezone, month, day);
now += utc_offset * 3600;
gmtime_s(&t, &now);
hour = t.tm_hour % 12;
minute = t.tm_min;
second = t.tm_sec;
}
void Clock::draw_hands()
{
bool is_night = is_night_mode();
COLORREF hand_color = is_night ? WHITE : BLACK;
double second_angle = second * PI / 30 - PI / 2;
double minute_angle = (minute + second / 60.0) * PI / 30 - PI / 2;
double hour_angle = (hour + minute / 60.0) * PI / 6 - PI / 2;
if (use_antialiasing)
{
int sec_x = centerX + (int)((radius - 10) * cos(second_angle));
int sec_y = centerY + (int)((radius - 10) * sin(second_angle));
SDF::draw_aa_line(centerX, centerY, sec_x, sec_y, 1, RGB(242, 162, 90));
int min_x = centerX + (int)((radius - 15) * cos(minute_angle));
int min_y = centerY + (int)((radius - 15) * sin(minute_angle));
SDF::draw_aa_line(centerX, centerY, min_x, min_y, 2, hand_color);
int hr_x = centerX + (int)((radius - 25) * cos(hour_angle));
int hr_y = centerY + (int)((radius - 25) * sin(hour_angle));
SDF::draw_aa_line(centerX, centerY, hr_x, hr_y, 3, hand_color);
SDF::draw_aa_circle(centerX, centerY, 4, RGB(242, 162, 90));
}
else
{
int sec_x = centerX + (int)((radius - 10) * cos(second_angle));
int sec_y = centerY + (int)((radius - 10) * sin(second_angle));
setlinecolor(RGB(242, 162, 90));
setlinestyle(PS_SOLID, 1);
line(centerX, centerY, sec_x, sec_y);
int min_x = centerX + (int)((radius - 15) * cos(minute_angle));
int min_y = centerY + (int)((radius - 15) * sin(minute_angle));
setlinecolor(hand_color);
setlinestyle(PS_SOLID, 2);
line(centerX, centerY, min_x, min_y);
int hr_x = centerX + (int)((radius - 25) * cos(hour_angle));
int hr_y = centerY + (int)((radius - 25) * sin(hour_angle));
setlinecolor(hand_color);
setlinestyle(PS_SOLID, 3);
line(centerX, centerY, hr_x, hr_y);
setfillcolor(RGB(242, 162, 90));
solidcircle(centerX, centerY, 4);
}
}
void Clock::set_timezone(const wstring& city_name)
{
this->city_name = city_name;
current_timezone = TimeZone::get_timezone_by_city(city_name);
}
void Clock::set_antialiasing(bool enabled)
{
use_antialiasing = enabled;
}