1 #include "stdafx.h" 2 #include "CUIDrawer.h" 3 #include "MusicPlayer2.h" 4 5 CUIDrawer::CUIDrawer(UIColors& colors) 6 : m_colors(colors) 7 { 8 } 9 10 11 CUIDrawer::~CUIDrawer() 12 { 13 } 14 15 void CUIDrawer::DrawLryicCommon(CRect rect, Alignment align) 16 { 17 SetDrawArea(rect); 18 static int flag{}; 19 if (!IsDrawMultiLine(rect.Height())) 20 DrawLyricTextSingleLine(rect, flag, true, align); 21 else 22 DrawLyricTextMultiLine(rect, align); 23 } 24 25 int CUIDrawer::GetLyricTextHeight() const 26 { 27 //计算文本高度 28 if (!m_for_cortana_lyric) 29 m_pDC->SelectObject(&theApp.m_font_set.lyric.GetFont(theApp.m_ui_data.full_screen)); 30 else 31 m_pDC->SelectObject(&theApp.m_font_set.cortana.GetFont()); 32 return m_pDC->GetTextExtent(L"文").cy; //根据当前的字体设置计算文本的高度 33 } 34 35 void CUIDrawer::Create(CDC* pDC, CWnd* pMainWnd) 36 { 37 CDrawCommon::Create(pDC, pMainWnd); 38 } 39 40 bool CUIDrawer::IsDrawMultiLine(int height) const 41 { 42 return height >= static_cast<int>(GetLyricTextHeight() * 3.5); 43 } 44 45 void CUIDrawer::SetForCortanaLyric(bool for_cortana_lyric) 46 { 47 m_for_cortana_lyric = for_cortana_lyric; 48 } 49 50 void CUIDrawer::DrawLyricTextMultiLine(CRect lyric_area, Alignment align) 51 { 52 // AUTO时多行歌词居中显示 53 if (align == Alignment::AUTO) align = Alignment::CENTER; 54 55 int line_space{}; 56 if (m_for_cortana_lyric) 57 { 58 line_space = theApp.DPI(4); 59 } 60 else 61 { 62 line_space = theApp.m_lyric_setting_data.lyric_line_space; 63 if (theApp.m_ui_data.full_screen) 64 line_space = static_cast<int>(line_space * CONSTVAL::FULL_SCREEN_ZOOM_FACTOR); 65 } 66 67 int lyric_height = GetLyricTextHeight() + line_space; //文本高度加上行间距 68 int lyric_height2 = lyric_height * 2 + line_space; //包含翻译的歌词高度 69 70 CFont* pOldFont = SetLyricFont(); 71 if (CPlayer::GetInstance().IsPlaylistEmpty()) //当前播放为空时在歌词区域显示播放提示 72 { 73 CFont* font = SetFont(&theApp.m_font_set.font10.GetFont()); 74 CString no_track_tip_str{ CCommon::LoadTextFormat(IDS_NO_TRACKS_TIP_INFO, { 75 theApp.m_accelerator_res.GetShortcutDescriptionById(ID_SHOW_PLAYLIST), 76 theApp.m_accelerator_res.GetShortcutDescriptionById(ID_FILE_OPEN), 77 theApp.m_accelerator_res.GetShortcutDescriptionById(ID_FILE_OPEN_FOLDER), 78 theApp.m_accelerator_res.GetShortcutDescriptionById(ID_SET_PATH)})}; 79 DrawWindowText(lyric_area, no_track_tip_str, m_colors.color_text_2, Alignment::LEFT, false, true); 80 SetFont(font); 81 } 82 else if (CPlayerUIHelper::IsMidiLyric()) 83 { 84 wstring current_lyric{ CPlayer::GetInstance().GetMidiLyric() }; 85 DrawWindowText(lyric_area, current_lyric.c_str(), m_colors.color_text, Alignment::CENTER, false, true); 86 } 87 else if (CPlayer::GetInstance().m_Lyrics.IsEmpty()) 88 { 89 //没有歌词时显示歌曲信息 90 if (theApp.m_lyric_setting_data.show_song_info_if_lyric_not_exist) 91 { 92 CString song_info_str; 93 const SongInfo& cur_song{ CPlayer::GetInstance().GetCurrentSongInfo() }; 94 std::wstring artist{ cur_song.album_artist }; 95 if (artist.empty()) 96 artist = cur_song.GetArtist(); 97 song_info_str.Format(_T("%s - %s\r\n%s"), artist.c_str(), cur_song.GetAlbum().c_str(), cur_song.GetTitle().c_str()); 98 DrawWindowText(lyric_area, song_info_str, m_colors.color_text, align, false, true); 99 } 100 //显示“当前歌曲没有歌词” 101 else 102 { 103 DrawWindowText(lyric_area, CCommon::LoadText(IDS_NO_LYRIC_INFO), m_colors.color_text_2, Alignment::CENTER); 104 } 105 } 106 else 107 { 108 //CRect arect{ lyric_area }; //一行歌词的矩形区域 109 //arect.bottom = arect.top + lyric_height; 110 //vector<CRect> rects(CPlayer::GetInstance().m_Lyrics.GetLyricCount() + 1, arect); 111 //为每一句歌词创建一个矩形,保存在容器里 112 vector<CRect> rects; 113 int lyric_count = CPlayer::GetInstance().m_Lyrics.GetLyricCount() + 1; //获取歌词数量(由于第一行歌词需要显示标题,所以这里要+1) 114 for (int i{}; i < lyric_count; i++) 115 { 116 CRect arect{ lyric_area }; 117 if (!CPlayer::GetInstance().m_Lyrics.GetLyric(i - 1).translate.empty() && theApp.m_lyric_setting_data.show_translate) 118 arect.bottom = arect.top + lyric_height2; 119 else 120 arect.bottom = arect.top + lyric_height; 121 rects.push_back(arect); 122 } 123 int center_pos = (lyric_area.top + lyric_area.bottom) / 2; //歌词区域的中心y坐标 124 Time time{ CPlayer::GetInstance().GetCurrentPosition() }; //当前播放时间 125 int lyric_index = CPlayer::GetInstance().m_Lyrics.GetLyricIndex(time); // 当前歌词的序号 126 int progress{ CPlayer::GetInstance().m_Lyrics.GetLyricProgress(time, false, false, [this](const wstring& str) { return GetTextExtent(str.c_str()).cx; }) }; // 当前歌词进度(范围为0~1000),多行歌词使用的进度不含进度符号 127 int y_progress; //当前歌词在y轴上的进度 128 if (!CPlayer::GetInstance().m_Lyrics.GetLyric(lyric_index).translate.empty() && theApp.m_lyric_setting_data.show_translate) 129 y_progress = progress * lyric_height2 / 1000; 130 else 131 y_progress = progress * lyric_height / 1000; 132 //int start_pos = center_pos - y_progress - (lyric_index + 1)*lyric_height; //第1句歌词的起始y坐标 133 //计算第1句歌词的起始y坐标 134 //由于当前歌词需要显示在歌词区域的中心位置,因此从中心位置开始,减去当前歌词在Y轴上的进度 135 //再依次减去之前每一句歌词的高度,即得到了第一句歌词的起始位置 136 int start_pos; 137 start_pos = center_pos - y_progress; 138 for (int i{ lyric_index - 1 }; i >= -1; i--) 139 { 140 if (theApp.m_lyric_setting_data.show_translate && !CPlayer::GetInstance().m_Lyrics.GetLyric(i).translate.empty()) 141 start_pos -= lyric_height2; 142 else 143 start_pos -= lyric_height; 144 } 145 146 //依次绘制每一句歌词 147 for (int i{ -1 }; i < static_cast<int>(rects.size()) - 1; i++) 148 { 149 //计算每一句歌词的位置 150 if (i == -1) 151 rects[i + 1].MoveToY(start_pos); 152 else 153 rects[i + 1].MoveToY(rects[i].bottom); 154 //绘制歌词文本 155 if (!(rects[i + 1] & lyric_area).IsRectEmpty()) //只有当一句歌词的矩形区域和歌词区域的矩形有交集时,才绘制歌词 156 { 157 //设置歌词文本和翻译文本的矩形区域 158 CRect rect_text{ rects[i + 1] }; 159 CRect rect_translate; 160 const CLyrics::Lyric& lyric_i = CPlayer::GetInstance().m_Lyrics.GetLyric(i); 161 if (!lyric_i.translate.empty() && theApp.m_lyric_setting_data.show_translate) 162 { 163 rect_text.MoveToY(rect_text.top + line_space); 164 rect_text.bottom = rect_text.top + GetLyricTextHeight(); 165 rect_translate = rect_text; 166 rect_translate.MoveToY(rect_text.bottom + line_space); 167 } 168 169 if (i == lyric_index && progress < 1000) //绘制正在播放的歌词(处于逐渐过度到高亮过程中的歌词) 170 { 171 //这里实现文本从非高亮缓慢变化到高亮效果 172 int last_time_span = time - lyric_i.time_start; //当前播放的歌词已持续的时间 173 int fade_percent = last_time_span / 8; //计算颜色高亮变化的百分比,除数越大则持续时间越长,10则为1秒 174 COLORREF text_color = CColorConvert::GetGradientColor(m_colors.color_text_2, m_colors.color_text, fade_percent); 175 //绘制歌词文本 176 SetLyricFont(); 177 if (theApp.m_lyric_setting_data.lyric_karaoke_disp) 178 DrawWindowText(rect_text, lyric_i.text.c_str(), m_colors.color_text, m_colors.color_text_2, progress, align, true); 179 else 180 DrawWindowText(rect_text, lyric_i.text.c_str(), text_color, text_color, progress, align, true); 181 //绘制翻译文本 182 if (!lyric_i.translate.empty() && theApp.m_lyric_setting_data.show_translate) 183 { 184 SetLyricFontTranslated(); 185 DrawWindowText(rect_translate, lyric_i.translate.c_str(), text_color, text_color, progress, align, true); 186 } 187 } 188 else //绘制非正在播放的歌词 189 { 190 SetLyricFont(); 191 COLORREF text_color; 192 if (i == lyric_index - 1 || (i == lyric_index && progress == 1000)) // 绘制正在取消高亮的歌词(这里实现一句歌词颜色从高亮缓慢变化到非高亮效果),逐字歌词最后一句在此处取消高亮 193 { 194 int last_time_span = time - (lyric_i.time_start + lyric_i.time_span); // 引入逐字歌词后上句歌词结束后时长不等于当前播放的歌词已持续的时间,此处应当使用上句歌词结束时间 195 int fade_percent = last_time_span / 20; //计算颜色高亮变化的百分比,当持续时间为2000毫秒时为100%,即颜色缓慢变化的时间为2秒 196 text_color = CColorConvert::GetGradientColor(m_colors.color_text, m_colors.color_text_2, fade_percent); 197 } 198 else 199 { 200 text_color = m_colors.color_text_2; 201 } 202 //绘制歌词文本 203 DrawWindowText(rect_text, lyric_i.text.c_str(), text_color, align, true); 204 //绘制翻译文本 205 if (!lyric_i.translate.empty() && theApp.m_lyric_setting_data.show_translate) 206 { 207 SetLyricFontTranslated(); 208 DrawWindowText(rect_translate, lyric_i.translate.c_str(), text_color, align, true); 209 } 210 } 211 } 212 } 213 } 214 SetFont(pOldFont); 215 } 216 217 void CUIDrawer::DrawLyricTextSingleLine(CRect rect, int& flag, bool double_line, Alignment align) 218 { 219 CFont* pOldFont = SetLyricFont(); 220 221 if (CPlayerUIHelper::IsMidiLyric()) 222 { 223 wstring current_lyric{ CPlayer::GetInstance().GetMidiLyric() }; 224 DrawWindowText(rect, current_lyric.c_str(), m_colors.color_text, Alignment::CENTER, false, true); 225 } 226 else if (CPlayer::GetInstance().m_Lyrics.IsEmpty()) 227 { 228 //没有歌词时显示歌曲信息 229 if (theApp.m_lyric_setting_data.show_song_info_if_lyric_not_exist) 230 { 231 CString song_info_str; 232 const SongInfo& cur_song{ CPlayer::GetInstance().GetCurrentSongInfo() }; 233 song_info_str.Format(_T("%s - %s"), cur_song.GetArtist().c_str(), cur_song.GetTitle().c_str()); 234 static CDrawCommon::ScrollInfo lyric_scroll_info; 235 DrawScrollText(rect, song_info_str, m_colors.color_text, CPlayerUIHelper::GetScrollTextPixel(), theApp.m_lyric_setting_data.lyric_align != Alignment::LEFT, lyric_scroll_info); 236 } 237 //显示“当前歌曲没有歌词” 238 else 239 { 240 DrawWindowText(rect, CCommon::LoadText(IDS_NO_LYRIC_INFO), m_colors.color_text_2, Alignment::CENTER); 241 } 242 } 243 else 244 { 245 SetDrawArea(rect); 246 CRect lyric_rect = rect; 247 248 const bool karaoke{ theApp.m_lyric_setting_data.lyric_karaoke_disp }; 249 const bool ignore_blank{ theApp.m_lyric_setting_data.donot_show_blank_lines}; 250 auto& now_lyrics{ CPlayer::GetInstance().m_Lyrics }; 251 Time time{ CPlayer::GetInstance().GetCurrentPosition() }; 252 CLyrics::Lyric current_lyric{ now_lyrics.GetLyric(time, false, ignore_blank, karaoke) }; 253 int progress{ now_lyrics.GetLyricProgress(time, ignore_blank, karaoke, [this](const wstring& str) { return GetTextExtent(str.c_str()).cx; }) }; 254 bool switch_flag{ flag > 5000 }; 255 switch_flag ^= (flag % 5000) > progress; 256 flag = switch_flag ? 10000 + progress : progress; 257 258 if (current_lyric.text.empty()) 259 current_lyric.text = CCommon::LoadText(IDS_DEFAULT_LYRIC_TEXT); 260 //双行显示歌词 261 if (double_line && (current_lyric.translate.empty() || !theApp.m_lyric_setting_data.show_translate) && rect.Height() > static_cast<int>(GetLyricTextHeight() * 1.73)) 262 { 263 wstring next_lyric_text; 264 next_lyric_text = now_lyrics.GetLyric(time, true, ignore_blank, karaoke).text; 265 if (next_lyric_text.empty()) 266 next_lyric_text = CCommon::LoadText(IDS_DEFAULT_LYRIC_TEXT); 267 //这里实现文本从非高亮缓慢变化到高亮效果 268 int last_time_span = time - current_lyric.time_start; //当前播放的歌词已持续的时间 269 int fade_percent = last_time_span / 8; //计算颜色高亮变化的百分比,除数越大则持续时间越长,10则为1秒 270 if (progress == 1000) fade_percent = 0; // 进度为1000时当前歌词“已完成”不再高亮 271 // 这里的fade_percent当合并空行开启时可能为负,在颜色渐变处规范取值,此处不再处理 272 DrawLyricDoubleLine(lyric_rect, current_lyric.text.c_str(), next_lyric_text.c_str(), align, progress, switch_flag, fade_percent); 273 } 274 else 275 { 276 // AUTO时单行歌词居中显示 277 if (align == Alignment::AUTO) align = Alignment::CENTER; 278 // 单行歌词在这里显示翻译,同时更新歌词区域为单行有翻译时的位置 279 if (theApp.m_lyric_setting_data.show_translate && !current_lyric.translate.empty() && rect.Height() > static_cast<int>(GetLyricTextHeight() * 1.73)) 280 { 281 lyric_rect.bottom = lyric_rect.top + rect.Height() / 2; 282 CRect translate_rect = lyric_rect; 283 translate_rect.MoveToY(lyric_rect.bottom); 284 285 SetLyricFontTranslated(); 286 DrawWindowText(translate_rect, current_lyric.translate.c_str(), m_colors.color_text, m_colors.color_text, progress, align, true); 287 } 288 // 绘制单行歌词 289 SetLyricFont(); 290 if (theApp.m_lyric_setting_data.lyric_karaoke_disp) 291 DrawWindowText(lyric_rect, current_lyric.text.c_str(), m_colors.color_text, m_colors.color_text_2, progress, align, true); 292 else if (0 < progress && progress < 1000) // 仅高亮“正在进行”的歌词 293 DrawWindowText(lyric_rect, current_lyric.text.c_str(), m_colors.color_text, m_colors.color_text, progress, align, true); 294 else 295 DrawWindowText(lyric_rect, current_lyric.text.c_str(), m_colors.color_text_2, m_colors.color_text_2, progress, align, true); 296 } 297 } 298 299 SetFont(pOldFont); 300 } 301 302 void CUIDrawer::DrawSpectrum(CRect rect, SpectrumCol col, bool draw_reflex /*= false*/, bool low_freq_in_center, bool fixed_width, Alignment alignment) 303 { 304 int cols; //要显示的频谱柱形的数量 305 switch (col) 306 { 307 case CUIDrawer::SC_64: 308 cols = 64; 309 break; 310 case CUIDrawer::SC_32: 311 cols = 32; 312 break; 313 case CUIDrawer::SC_16: 314 cols = 16; 315 break; 316 case CUIDrawer::SC_8: 317 cols = 8; 318 break; 319 default: 320 cols = SPECTRUM_COL; 321 break; 322 } 323 int max_width{ rect.Width() }; 324 if (fixed_width) 325 { 326 if (col == SC_64) 327 { 328 max_width = DPI(280); 329 } 330 } 331 int gap_width{ max_width * (SPECTRUM_COL / cols) / 168 }; //频谱柱形间隙宽度 332 if (theApp.m_ui_data.full_screen && !m_for_cortana_lyric) 333 gap_width = static_cast<int>(gap_width * CONSTVAL::FULL_SCREEN_ZOOM_FACTOR); 334 int width = (max_width - (cols - 1) * gap_width) / (cols - 1); 335 if (gap_width < 1) 336 gap_width = 1; 337 if (width < 1) 338 width = 1; 339 340 if (fixed_width) 341 SetDrawArea(rect); 342 DrawSpectrum(rect, width, gap_width, cols, m_colors.color_spectrum, draw_reflex, low_freq_in_center, alignment); 343 } 344 345 void CUIDrawer::DrawSpectrum(CRect rect, int col_width, int gap_width, int cols, COLORREF color, bool draw_reflex /*= false*/, bool low_freq_in_center, Alignment alignment) 346 { 347 CRect rc_spectrum_top = rect; 348 if (draw_reflex) //如果要绘制倒影,则倒影占总高度的1/3 349 rc_spectrum_top.bottom = rect.top + (rect.Height() * 2 / 3); 350 351 CRect rects[SPECTRUM_COL]; 352 rects[0] = rc_spectrum_top; 353 rects[0].right = rects[0].left + col_width; 354 355 //频谱的实际宽度 356 int width_actrual{ col_width * cols + gap_width * (cols - 1) }; 357 //如果频谱的实际宽度小于矩形的宽度,则让根据alignment的值让频谱居中或右对齐显示 358 if ((width_actrual < rect.Width() && alignment == Alignment::CENTER) || (width_actrual >= rect.Width() && theApp.m_app_setting_data.spectrum_low_freq_in_center)) 359 rects[0].MoveToX(rects[0].left + (rect.Width() - width_actrual) / 2); 360 else if (width_actrual < rect.Width() && alignment == Alignment::RIGHT) 361 rects[0].MoveToX(rects[0].left + (rect.Width() - width_actrual)); 362 363 for (int i{ 1 }; i < cols; i++) 364 { 365 rects[i] = rects[0]; 366 rects[i].left += (i * (col_width + gap_width)); 367 rects[i].right += (i * (col_width + gap_width)); 368 } 369 for (int i{}; i < cols; i++) 370 { 371 int index; 372 if (low_freq_in_center) 373 { 374 if (i < cols / 2) 375 index = (-i + cols / 2) * 2 - 1; 376 else 377 index = (i - cols / 2) * 2; 378 } 379 else 380 { 381 index = i; 382 } 383 if (index >= cols) 384 index = cols; 385 if (index < 0) 386 index = 0; 387 float spetral_data = CPlayer::GetInstance().GetSpectralData()[index * (SPECTRUM_COL / cols)]; 388 float peak_data = CPlayer::GetInstance().GetSpectralPeakData()[index * (SPECTRUM_COL / cols)]; 389 390 CRect rect_tmp{ rects[i] }; 391 int spetral_height = static_cast<int>(spetral_data * rects[0].Height() / 30 * theApp.m_app_setting_data.sprctrum_height / 100); 392 int peak_height = static_cast<int>(peak_data * rects[0].Height() / 30 * theApp.m_app_setting_data.sprctrum_height / 100); 393 if (spetral_height < 0 || CPlayer::GetInstance().IsError()) spetral_height = 0; //如果播放出错,不显示频谱 394 if (peak_height < 0 || CPlayer::GetInstance().IsError()) peak_height = 0; 395 396 int peak_rect_height = max(theApp.DPIRound(1.1), gap_width / 2); //顶端矩形的高度 397 spetral_height += peak_rect_height; //频谱至少和顶端矩形一样高 398 peak_height += peak_rect_height; 399 400 rect_tmp.top = rect_tmp.bottom - spetral_height; 401 if (rect_tmp.top < rects[0].top) rect_tmp.top = rects[0].top; 402 FillRect(rect_tmp, color, true); 403 //绘制倒影 404 if (draw_reflex) 405 { 406 CRect rc_invert = rect_tmp; 407 rc_invert.bottom = rect_tmp.top + rect_tmp.Height() * 2 / 3; 408 rc_invert.MoveToY(rect_tmp.bottom + gap_width); 409 FillAlphaRect(rc_invert, color, 96, true); 410 } 411 412 //绘制顶端 413 CRect rect_peak{ rect_tmp }; 414 rect_peak.bottom = rect_tmp.bottom - peak_height - gap_width; 415 rect_peak.top = rect_peak.bottom - peak_rect_height; 416 FillRect(rect_peak, color, true); 417 ////绘制顶端倒影 418 //CRect rc_peak_invert = rect_peak; 419 //rc_peak_invert.MoveToY(rc_invert.top + peak_height + theApp.DPIRound(1.1)); 420 //FillAlphaRect(rc_peak_invert, color, 96); 421 } 422 423 } 424 425 int CUIDrawer::DPI(int pixel) 426 { 427 if (theApp.m_ui_data.full_screen && !m_for_cortana_lyric) 428 return theApp.DPI(pixel * CONSTVAL::FULL_SCREEN_ZOOM_FACTOR); 429 else 430 return theApp.DPI(pixel); 431 } 432 433 void CUIDrawer::DrawLyricDoubleLine(CRect rect, LPCTSTR lyric, LPCTSTR next_lyric, Alignment align, int progress, bool switch_flag, int fade_percent) 434 { 435 CFont* pOldFont = SetLyricFont(); 436 437 CRect up_rect{ rect }, down_rect{ rect }; //上半部分和下半部分歌词的矩形区域 438 up_rect.bottom = up_rect.top + (up_rect.Height() / 2); 439 down_rect.top = down_rect.bottom - (down_rect.Height() / 2); 440 441 // 对齐方式为AUTO时使用上左下右的卡拉OK对齐方式 442 Alignment up_align{ Alignment::LEFT }, down_align{ Alignment::RIGHT }; 443 if (align != Alignment::AUTO) 444 up_align = down_align = align; 445 446 COLORREF color1, color2; 447 if (theApp.m_lyric_setting_data.lyric_karaoke_disp) 448 { 449 color1 = m_colors.color_text; 450 color2 = m_colors.color_text_2; 451 } 452 else 453 { 454 color1 = color2 = CColorConvert::GetGradientColor(m_colors.color_text_2, m_colors.color_text, fade_percent); 455 } 456 457 // 绘制当前歌词 458 if (!switch_flag) 459 { 460 DrawWindowText(up_rect, lyric, color1, color2, progress, up_align); 461 DrawWindowText(down_rect, next_lyric, m_colors.color_text_2, down_align); 462 } 463 else 464 { 465 DrawWindowText(up_rect, next_lyric, m_colors.color_text_2, up_align); 466 DrawWindowText(down_rect, lyric, color1, color2, progress, down_align); 467 } 468 SetFont(pOldFont); 469 } 470 471 CFont* CUIDrawer::SetLyricFont() 472 { 473 if (!m_for_cortana_lyric) 474 return SetFont(&theApp.m_font_set.lyric.GetFont(theApp.m_ui_data.full_screen)); 475 else 476 return SetFont(&theApp.m_font_set.cortana.GetFont()); 477 } 478 479 CFont* CUIDrawer::SetLyricFontTranslated() 480 { 481 if (!m_for_cortana_lyric) 482 return SetFont(&theApp.m_font_set.lyric_translate.GetFont(theApp.m_ui_data.full_screen)); 483 else 484 return SetFont(&theApp.m_font_set.cortana_translate.GetFont()); 485 } 486