1 #include "stdafx.h" 2 #include "CUIDrawer.h" 3 #include "MusicPlayer2.h" 4 5 6 CUIDrawer::CUIDrawer(UIColors& colors) 7 : m_colors(colors) 8 { 9 } 10 11 12 CUIDrawer::~CUIDrawer() 13 { 14 } 15 16 void CUIDrawer::DrawLryicCommon(CRect rect, Alignment align) 17 { 18 SetDrawArea(rect); 19 20 if (!IsDrawMultiLine(rect.Height())) 21 DrawLyricTextSingleLine(rect, true, align); 22 else 23 DrawLyricTextMultiLine(rect, align); 24 } 25 26 int CUIDrawer::GetLyricTextHeight() const 27 { 28 //计算文本高度 29 if (!m_for_cortana_lyric) 30 m_pDC->SelectObject(&theApp.m_font_set.lyric.GetFont(theApp.m_ui_data.full_screen)); 31 else 32 m_pDC->SelectObject(&theApp.m_font_set.cortana.GetFont()); 33 return m_pDC->GetTextExtent(L"文").cy; //根据当前的字体设置计算文本的高度 34 } 35 36 void CUIDrawer::Create(CDC* pDC, CWnd* pMainWnd) 37 { 38 CDrawCommon::Create(pDC, pMainWnd); 39 } 40 41 bool CUIDrawer::IsDrawMultiLine(int height) const 42 { 43 return height >= static_cast<int>(GetLyricTextHeight() * 3.5); 44 } 45 46 void CUIDrawer::SetForCortanaLyric(bool for_cortana_lyric) 47 { 48 m_for_cortana_lyric = for_cortana_lyric; 49 } 50 51 void CUIDrawer::DrawLyricTextMultiLine(CRect lyric_area, Alignment align) 52 { 53 int line_space{}; 54 if (m_for_cortana_lyric) 55 { 56 line_space = theApp.DPI(4); 57 } 58 else 59 { 60 line_space = theApp.m_lyric_setting_data.lyric_line_space; 61 if (theApp.m_ui_data.full_screen) 62 line_space = static_cast<int>(line_space * CONSTVAL::FULL_SCREEN_ZOOM_FACTOR); 63 } 64 65 int lyric_height = GetLyricTextHeight() + line_space; //文本高度加上行间距 66 int lyric_height2 = lyric_height * 2 + line_space; //包含翻译的歌词高度 67 68 CFont* pOldFont = SetLyricFont(); 69 if (CPlayerUIHelper::IsMidiLyric()) 70 { 71 wstring current_lyric{ CPlayer::GetInstance().GetMidiLyric() }; 72 DrawWindowText(lyric_area, current_lyric.c_str(), m_colors.color_text, Alignment::CENTER, false, true); 73 } 74 else if (CPlayer::GetInstance().m_Lyrics.IsEmpty()) 75 { 76 DrawWindowText(lyric_area, CCommon::LoadText(IDS_NO_LYRIC_INFO), m_colors.color_text_2, Alignment::CENTER); 77 } 78 else 79 { 80 //CRect arect{ lyric_area }; //一行歌词的矩形区域 81 //arect.bottom = arect.top + lyric_height; 82 //vector<CRect> rects(CPlayer::GetInstance().m_Lyrics.GetLyricCount() + 1, arect); 83 //为每一句歌词创建一个矩形,保存在容器里 84 vector<CRect> rects; 85 int lyric_count = CPlayer::GetInstance().m_Lyrics.GetLyricCount() + 1; //获取歌词数量(由于第一行歌词需要显示标题,所以这里要+1) 86 for (int i{}; i < lyric_count; i++) 87 { 88 CRect arect{ lyric_area }; 89 if (!CPlayer::GetInstance().m_Lyrics.GetLyric(i).translate.empty() && theApp.m_lyric_setting_data.show_translate) 90 arect.bottom = arect.top + lyric_height2; 91 else 92 arect.bottom = arect.top + lyric_height; 93 rects.push_back(arect); 94 } 95 int center_pos = (lyric_area.top + lyric_area.bottom) / 2; //歌词区域的中心y坐标 96 Time time{ CPlayer::GetInstance().GetCurrentPosition() }; //当前播放时间 97 int lyric_index = CPlayer::GetInstance().m_Lyrics.GetLyricIndex(time) + 1; //当前歌词的序号(歌词的第一句GetLyricIndex返回的是0,由于显示时第一句歌词要显示标题,所以这里要+1) 98 int progress = CPlayer::GetInstance().m_Lyrics.GetLyricProgress(time); //当前歌词进度(范围为0~1000) 99 int y_progress; //当前歌词在y轴上的进度 100 if (!CPlayer::GetInstance().m_Lyrics.GetLyric(lyric_index).translate.empty() && theApp.m_lyric_setting_data.show_translate) 101 y_progress = progress * lyric_height2 / 1000; 102 else 103 y_progress = progress * lyric_height / 1000; 104 //int start_pos = center_pos - y_progress - (lyric_index + 1)*lyric_height; //第1句歌词的起始y坐标 105 //计算第1句歌词的起始y坐标 106 //由于当前歌词需要显示在歌词区域的中心位置,因此从中心位置开始,减去当前歌词在Y轴上的进度 107 //再依次减去之前每一句歌词的高度,即得到了第一句歌词的起始位置 108 int start_pos; 109 start_pos = center_pos - y_progress; 110 for (int i{ lyric_index - 1 }; i >= 0; i--) 111 { 112 if (theApp.m_lyric_setting_data.show_translate && !CPlayer::GetInstance().m_Lyrics.GetLyric(i).translate.empty()) 113 start_pos -= lyric_height2; 114 else 115 start_pos -= lyric_height; 116 } 117 118 //依次绘制每一句歌词 119 for (size_t i{}; i < rects.size(); i++) 120 { 121 //计算每一句歌词的位置 122 if (i == 0) 123 rects[i].MoveToY(start_pos); 124 else 125 rects[i].MoveToY(rects[i - 1].bottom); 126 //绘制歌词文本 127 if (!(rects[i] & lyric_area).IsRectEmpty()) //只有当一句歌词的矩形区域和歌词区域的矩形有交集时,才绘制歌词 128 { 129 //设置歌词文本和翻译文本的矩形区域 130 CRect rect_text{ rects[i] }; 131 CRect rect_translate; 132 if (!CPlayer::GetInstance().m_Lyrics.GetLyric(i).translate.empty() && theApp.m_lyric_setting_data.show_translate) 133 { 134 rect_text.MoveToY(rect_text.top + line_space); 135 rect_text.bottom = rect_text.top + GetLyricTextHeight(); 136 rect_translate = rect_text; 137 rect_translate.MoveToY(rect_text.bottom + line_space); 138 } 139 140 if (i == lyric_index && progress < 1000) //绘制正在播放的歌词 141 { 142 CLyrics::Lyric lyric = CPlayer::GetInstance().m_Lyrics.GetLyric(i); 143 //这里实现文本从非高亮缓慢变化到高亮效果 144 int last_time_span = time - lyric.time; //当前播放的歌词已持续的时间 145 int fade_percent = last_time_span / 8; //计算颜色高亮变化的百分比,除数越大则持续时间越长,10则为1秒 146 COLORREF text_color = CColorConvert::GetGradientColor(m_colors.color_text_2, m_colors.color_text, fade_percent); 147 //绘制歌词文本 148 SetLyricFont(); 149 if (theApp.m_lyric_setting_data.lyric_karaoke_disp) 150 DrawWindowText(rect_text, lyric.text.c_str(), m_colors.color_text, m_colors.color_text_2, progress, align, true); 151 else 152 DrawWindowText(rect_text, lyric.text.c_str(), text_color, text_color, progress, align, true); 153 //绘制翻译文本 154 if (!lyric.translate.empty() && theApp.m_lyric_setting_data.show_translate) 155 { 156 SetLyricFontTranslated(); 157 DrawWindowText(rect_translate, CPlayer::GetInstance().m_Lyrics.GetLyric(i).translate.c_str(), text_color, text_color, progress, align, true); 158 } 159 } 160 else //绘制非正在播放的歌词 161 { 162 SetLyricFont(); 163 COLORREF text_color; 164 if (i == lyric_index - 1) //绘制上一句歌词(这里实现上一句歌词颜色从高亮缓慢变化到非高亮效果) 165 { 166 CLyrics::Lyric playing_lyric = CPlayer::GetInstance().m_Lyrics.GetLyric(lyric_index); 167 int last_time_span = time - playing_lyric.time; //当前播放的歌词已持续的时间 168 int fade_percent = last_time_span / 20; //计算颜色高亮变化的百分比,当持续时间为2000毫秒时为100%,即颜色缓慢变化的时间为2秒 169 text_color = CColorConvert::GetGradientColor(m_colors.color_text, m_colors.color_text_2, fade_percent); 170 } 171 else 172 { 173 text_color = m_colors.color_text_2; 174 } 175 //绘制歌词文本 176 DrawWindowText(rect_text, CPlayer::GetInstance().m_Lyrics.GetLyric(i).text.c_str(), text_color, align, true); 177 //绘制翻译文本 178 if (!CPlayer::GetInstance().m_Lyrics.GetLyric(i).translate.empty() && theApp.m_lyric_setting_data.show_translate) 179 { 180 SetLyricFontTranslated(); 181 DrawWindowText(rect_translate, CPlayer::GetInstance().m_Lyrics.GetLyric(i).translate.c_str(), text_color, align, true); 182 } 183 } 184 } 185 } 186 } 187 SetFont(pOldFont); 188 } 189 190 void CUIDrawer::DrawLyricTextSingleLine(CRect rect, bool double_line, Alignment align) 191 { 192 CFont* pOldFont = SetLyricFont(); 193 194 if (CPlayerUIHelper::IsMidiLyric()) 195 { 196 wstring current_lyric{ CPlayer::GetInstance().GetMidiLyric() }; 197 DrawWindowText(rect, current_lyric.c_str(), m_colors.color_text, Alignment::CENTER, false, true); 198 } 199 else if (CPlayer::GetInstance().m_Lyrics.IsEmpty()) 200 { 201 DrawWindowText(rect, CCommon::LoadText(IDS_NO_LYRIC_INFO), m_colors.color_text_2, Alignment::CENTER); 202 } 203 else 204 { 205 SetDrawArea(rect); 206 CRect lyric_rect = rect; 207 Time time = CPlayer::GetInstance().GetCurrentPosition(); 208 CLyrics::Lyric current_lyric{ CPlayer::GetInstance().m_Lyrics.GetLyric(time, 0) }; //获取当歌词 209 if (current_lyric.text.empty()) //如果当前歌词为空白,就显示为省略号 210 current_lyric.text = CCommon::LoadText(IDS_DEFAULT_LYRIC_TEXT); 211 int progress{ CPlayer::GetInstance().m_Lyrics.GetLyricProgress(time) }; //获取当前歌词进度(范围为0~1000) 212 213 //双行显示歌词 214 if (double_line && (!CPlayer::GetInstance().m_Lyrics.IsTranslated() || !theApp.m_lyric_setting_data.show_translate) && rect.Height() > static_cast<int>(GetLyricTextHeight() * 1.73)) 215 { 216 wstring next_lyric_text = CPlayer::GetInstance().m_Lyrics.GetLyric(time, 1).text; 217 if (next_lyric_text.empty()) 218 next_lyric_text = CCommon::LoadText(IDS_DEFAULT_LYRIC_TEXT); 219 //这里实现文本从非高亮缓慢变化到高亮效果 220 int last_time_span = time - current_lyric.time; //当前播放的歌词已持续的时间 221 int fade_percent = last_time_span / 8; //计算颜色高亮变化的百分比,除数越大则持续时间越长,10则为1秒 222 DrawLyricDoubleLine(lyric_rect, current_lyric.text.c_str(), next_lyric_text.c_str(), progress, fade_percent); 223 } 224 else 225 { 226 if (theApp.m_lyric_setting_data.show_translate && !current_lyric.translate.empty() && rect.Height() > static_cast<int>(GetLyricTextHeight() * 1.73)) 227 { 228 lyric_rect.bottom = lyric_rect.top + rect.Height() / 2; 229 CRect translate_rect = lyric_rect; 230 translate_rect.MoveToY(lyric_rect.bottom); 231 232 SetLyricFontTranslated(); 233 DrawWindowText(translate_rect, current_lyric.translate.c_str(), m_colors.color_text, m_colors.color_text, progress, align, true); 234 } 235 236 SetLyricFont(); 237 if (theApp.m_lyric_setting_data.lyric_karaoke_disp) 238 DrawWindowText(lyric_rect, current_lyric.text.c_str(), m_colors.color_text, m_colors.color_text_2, progress, align, true); 239 else 240 DrawWindowText(lyric_rect, current_lyric.text.c_str(), m_colors.color_text, m_colors.color_text, progress, align, true); 241 } 242 } 243 244 SetFont(pOldFont); 245 } 246 247 void CUIDrawer::DrawSpectrum(CRect rect, SpectrumCol col, bool draw_reflex /*= false*/, bool low_freq_in_center) 248 { 249 int cols; //要显示的频谱柱形的数量 250 switch (col) 251 { 252 case CUIDrawer::SC_64: 253 cols = 64; 254 break; 255 case CUIDrawer::SC_32: 256 cols = 32; 257 break; 258 case CUIDrawer::SC_16: 259 cols = 16; 260 break; 261 case CUIDrawer::SC_8: 262 cols = 8; 263 break; 264 default: 265 cols = SPECTRUM_COL; 266 break; 267 } 268 int gap_width{ rect.Width() * (SPECTRUM_COL / cols) / 168 }; //频谱柱形间隙宽度 269 if (theApp.m_ui_data.full_screen) 270 gap_width *= CONSTVAL::FULL_SCREEN_ZOOM_FACTOR; 271 if (gap_width < 1) 272 gap_width = 1; 273 int width = (rect.Width() - (cols - 1) * gap_width) / (cols - 1); 274 275 DrawSpectrum(rect, width, gap_width, cols, m_colors.color_spectrum, draw_reflex, low_freq_in_center); 276 } 277 278 void CUIDrawer::DrawSpectrum(CRect rect, int col_width, int gap_width, int cols, COLORREF color, bool draw_reflex /*= false*/, bool low_freq_in_center) 279 { 280 CRect rc_spectrum_top = rect; 281 if (draw_reflex) //如果要绘制倒影,则倒影占总高度的1/3 282 rc_spectrum_top.bottom = rect.top + (rect.Height() * 2 / 3); 283 284 CRect rects[SPECTRUM_COL]; 285 rects[0] = rc_spectrum_top; 286 rects[0].right = rects[0].left + col_width; 287 for (int i{ 1 }; i < cols; i++) 288 { 289 rects[i] = rects[0]; 290 rects[i].left += (i * (col_width + gap_width)); 291 rects[i].right += (i * (col_width + gap_width)); 292 } 293 for (int i{}; i < cols; i++) 294 { 295 int index; 296 if (low_freq_in_center) 297 { 298 if (i < cols / 2) 299 index = (-i + cols / 2) * 2 - 1; 300 else 301 index = (i - cols / 2) * 2; 302 } 303 else 304 { 305 index = i; 306 } 307 if (index >= cols) 308 index = cols; 309 if (index < 0) 310 index = 0; 311 float spetral_data = CPlayer::GetInstance().GetSpectralData()[index * (SPECTRUM_COL / cols)]; 312 float peak_data = CPlayer::GetInstance().GetSpectralPeakData()[index * (SPECTRUM_COL / cols)]; 313 314 CRect rect_tmp{ rects[i] }; 315 int spetral_height = static_cast<int>(spetral_data * rects[0].Height() / 30 * theApp.m_app_setting_data.sprctrum_height / 100); 316 int peak_height = static_cast<int>(peak_data * rects[0].Height() / 30 * theApp.m_app_setting_data.sprctrum_height / 100); 317 if (spetral_height < 0 || CPlayer::GetInstance().IsError()) spetral_height = 0; //如果播放出错,不显示频谱 318 if (peak_height < 0 || CPlayer::GetInstance().IsError()) peak_height = 0; 319 320 int peak_rect_height = max(theApp.DPIRound(1.1), gap_width / 2); //顶端矩形的高度 321 spetral_height += peak_rect_height; //频谱至少和顶端矩形一样高 322 peak_height += peak_rect_height; 323 324 rect_tmp.top = rect_tmp.bottom - spetral_height; 325 if (rect_tmp.top < rects[0].top) rect_tmp.top = rects[0].top; 326 FillRect(rect_tmp, color, true); 327 //绘制倒影 328 if (draw_reflex) 329 { 330 CRect rc_invert = rect_tmp; 331 rc_invert.bottom = rect_tmp.top + rect_tmp.Height() * 2 / 3; 332 rc_invert.MoveToY(rect_tmp.bottom + gap_width); 333 FillAlphaRect(rc_invert, color, 96, true); 334 } 335 336 //绘制顶端 337 CRect rect_peak{ rect_tmp }; 338 rect_peak.bottom = rect_tmp.bottom - peak_height - gap_width; 339 rect_peak.top = rect_peak.bottom - peak_rect_height; 340 FillRect(rect_peak, color, true); 341 ////绘制顶端倒影 342 //CRect rc_peak_invert = rect_peak; 343 //rc_peak_invert.MoveToY(rc_invert.top + peak_height + theApp.DPIRound(1.1)); 344 //FillAlphaRect(rc_peak_invert, color, 96); 345 } 346 347 } 348 349 void CUIDrawer::DrawLyricDoubleLine(CRect rect, LPCTSTR lyric, LPCTSTR next_lyric, int progress, int fade_percent) 350 { 351 CFont* pOldFont = SetLyricFont(); 352 static bool swap; 353 static int last_progress; 354 if (last_progress > progress) //如果当前的歌词进度比上次的小,说明歌词切换到了下一句 355 { 356 swap = !swap; 357 } 358 last_progress = progress; 359 360 361 CRect up_rect{ rect }, down_rect{ rect }; //上半部分和下半部分歌词的矩形区域 362 up_rect.bottom = up_rect.top + (up_rect.Height() / 2); 363 down_rect.top = down_rect.bottom - (down_rect.Height() / 2); 364 //根据下一句歌词的文本计算需要的宽度,从而实现下一行歌词右对齐 365 //GetDC()->SelectObject(&theApp.m_font_set.lyric.GetFont(theApp.m_ui_data.full_screen)); 366 int width; 367 if (!swap) 368 width = GetTextExtent(next_lyric).cx; 369 else 370 width = GetTextExtent(lyric).cx; 371 if (width < rect.Width()) 372 down_rect.left = down_rect.right - width; 373 374 COLORREF color1, color2; 375 if (theApp.m_lyric_setting_data.lyric_karaoke_disp) 376 { 377 color1 = m_colors.color_text; 378 color2 = m_colors.color_text_2; 379 } 380 else 381 { 382 color1 = color2 = CColorConvert::GetGradientColor(m_colors.color_text_2, m_colors.color_text, fade_percent); 383 } 384 385 if (!swap) 386 { 387 DrawWindowText(up_rect, lyric, color1, color2, progress); 388 DrawWindowText(down_rect, next_lyric, m_colors.color_text_2); 389 } 390 else 391 { 392 DrawWindowText(up_rect, next_lyric, m_colors.color_text_2); 393 DrawWindowText(down_rect, lyric, color1, color2, progress); 394 } 395 SetFont(pOldFont); 396 } 397 398 CFont* CUIDrawer::SetLyricFont() 399 { 400 if (!m_for_cortana_lyric) 401 return SetFont(&theApp.m_font_set.lyric.GetFont(theApp.m_ui_data.full_screen)); 402 else 403 return SetFont(&theApp.m_font_set.cortana.GetFont()); 404 } 405 406 CFont* CUIDrawer::SetLyricFontTranslated() 407 { 408 if (!m_for_cortana_lyric) 409 return SetFont(&theApp.m_font_set.lyric_translate.GetFont(theApp.m_ui_data.full_screen)); 410 else 411 return SetFont(&theApp.m_font_set.cortana_translate.GetFont()); 412 } 413