xref: /MusicPlayer2/MusicPlayer2/CUIDrawer.cpp (revision ed4a559c3ee49bf03b919e20bf10573dada4b52f)
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_app_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_ui_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_ui_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_ui_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_ui_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_ui_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_ui_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         CRect lyric_rect = rect;
206         Time time = CPlayer::GetInstance().GetCurrentPosition();
207         CLyrics::Lyric current_lyric{ CPlayer::GetInstance().m_Lyrics.GetLyric(time, 0) };	//获取当歌词
208         if (current_lyric.text.empty())		//如果当前歌词为空白,就显示为省略号
209             current_lyric.text = CCommon::LoadText(IDS_DEFAULT_LYRIC_TEXT);
210         int progress{ CPlayer::GetInstance().m_Lyrics.GetLyricProgress(time) };		//获取当前歌词进度(范围为0~1000)
211 
212         //双行显示歌词
213         if (double_line && (!CPlayer::GetInstance().m_Lyrics.IsTranslated() || !theApp.m_ui_data.show_translate) && rect.Height() > static_cast<int>(GetLyricTextHeight() * 1.73))
214         {
215             wstring next_lyric_text = CPlayer::GetInstance().m_Lyrics.GetLyric(time, 1).text;
216             if (next_lyric_text.empty())
217                 next_lyric_text = CCommon::LoadText(IDS_DEFAULT_LYRIC_TEXT);
218             //这里实现文本从非高亮缓慢变化到高亮效果
219             int last_time_span = time - current_lyric.time;     //当前播放的歌词已持续的时间
220             int fade_percent = last_time_span / 8;         //计算颜色高亮变化的百分比,除数越大则持续时间越长,10则为1秒
221             DrawLyricDoubleLine(lyric_rect, current_lyric.text.c_str(), next_lyric_text.c_str(), progress, fade_percent);
222         }
223         else
224         {
225             if (theApp.m_ui_data.show_translate && !current_lyric.translate.empty() && rect.Height() > static_cast<int>(GetLyricTextHeight() * 1.73))
226             {
227                 lyric_rect.bottom = lyric_rect.top + rect.Height() / 2;
228                 CRect translate_rect = lyric_rect;
229                 translate_rect.MoveToY(lyric_rect.bottom);
230 
231                 SetLyricFontTranslated();
232                 DrawWindowText(translate_rect, current_lyric.translate.c_str(), m_colors.color_text, m_colors.color_text, progress, align, true);
233             }
234 
235             SetLyricFont();
236             if (theApp.m_lyric_setting_data.lyric_karaoke_disp)
237                 DrawWindowText(lyric_rect, current_lyric.text.c_str(), m_colors.color_text, m_colors.color_text_2, progress, align, true);
238             else
239                 DrawWindowText(lyric_rect, current_lyric.text.c_str(), m_colors.color_text, m_colors.color_text, progress, align, true);
240         }
241     }
242 
243     SetFont(pOldFont);
244 }
245 
246 void CUIDrawer::DrawSpectrum(CRect rect, SpectrumCol col, bool draw_reflex /*= false*/, bool low_freq_in_center)
247 {
248     int cols;		//要显示的频谱柱形的数量
249     switch (col)
250     {
251     case CUIDrawer::SC_64:
252         cols = 64;
253         break;
254     case CUIDrawer::SC_32:
255         cols = 32;
256         break;
257     case CUIDrawer::SC_16:
258         cols = 16;
259         break;
260     case CUIDrawer::SC_8:
261         cols = 8;
262         break;
263     default:
264         cols = SPECTRUM_COL;
265         break;
266     }
267     int gap_width{ rect.Width() * (SPECTRUM_COL / cols) / 168 };		//频谱柱形间隙宽度
268     if (theApp.m_ui_data.full_screen)
269         gap_width *= CONSTVAL::FULL_SCREEN_ZOOM_FACTOR;
270     if (gap_width < 1)
271         gap_width = 1;
272     int width = (rect.Width() - (cols - 1) * gap_width) / (cols - 1);
273 
274     DrawSpectrum(rect, width, gap_width, cols, m_colors.color_spectrum, draw_reflex, low_freq_in_center);
275 }
276 
277 void CUIDrawer::DrawSpectrum(CRect rect, int col_width, int gap_width, int cols, COLORREF color, bool draw_reflex /*= false*/, bool low_freq_in_center)
278 {
279     CRect rc_spectrum_top = rect;
280     if (draw_reflex)     //如果要绘制倒影,则倒影占总高度的1/3
281         rc_spectrum_top.bottom = rect.top + (rect.Height() * 2 / 3);
282 
283     CRect rects[SPECTRUM_COL];
284     rects[0] = rc_spectrum_top;
285     rects[0].right = rects[0].left + col_width;
286     for (int i{ 1 }; i < cols; i++)
287     {
288         rects[i] = rects[0];
289         rects[i].left += (i * (col_width + gap_width));
290         rects[i].right += (i * (col_width + gap_width));
291     }
292     for (int i{}; i < cols; i++)
293     {
294         int index;
295         if (low_freq_in_center)
296         {
297             if (i < cols / 2)
298                 index = (-i + cols / 2) * 2 - 1;
299             else
300                 index = (i - cols / 2) * 2;
301         }
302         else
303         {
304             index = i;
305         }
306         if (index >= cols)
307             index = cols;
308         if (index < 0)
309             index = 0;
310         float spetral_data = CPlayer::GetInstance().GetSpectralData()[index * (SPECTRUM_COL / cols)];
311         float peak_data = CPlayer::GetInstance().GetSpectralPeakData()[index * (SPECTRUM_COL / cols)];
312 
313         CRect rect_tmp{ rects[i] };
314         int spetral_height = static_cast<int>(spetral_data * rects[0].Height() / 30 * theApp.m_app_setting_data.sprctrum_height / 100);
315         int peak_height = static_cast<int>(peak_data * rects[0].Height() / 30 * theApp.m_app_setting_data.sprctrum_height / 100);
316         if (spetral_height < 0 || CPlayer::GetInstance().IsError()) spetral_height = 0;		//如果播放出错,不显示频谱
317         if (peak_height < 0 || CPlayer::GetInstance().IsError()) peak_height = 0;
318 
319         int peak_rect_height = max(theApp.DPIRound(1.1), gap_width / 2);        //顶端矩形的高度
320         spetral_height += peak_rect_height;                                     //频谱至少和顶端矩形一样高
321         peak_height += peak_rect_height;
322 
323         rect_tmp.top = rect_tmp.bottom - spetral_height;
324         if (rect_tmp.top < rects[0].top) rect_tmp.top = rects[0].top;
325         FillRect(rect_tmp, color, true);
326         //绘制倒影
327         if (draw_reflex)
328         {
329             CRect rc_invert = rect_tmp;
330             rc_invert.bottom = rect_tmp.top + rect_tmp.Height() * 2 / 3;
331             rc_invert.MoveToY(rect_tmp.bottom + gap_width);
332             FillAlphaRect(rc_invert, color, 96, true);
333         }
334 
335         //绘制顶端
336         CRect rect_peak{ rect_tmp };
337         rect_peak.bottom = rect_tmp.bottom - peak_height - gap_width;
338         rect_peak.top = rect_peak.bottom - peak_rect_height;
339         FillRect(rect_peak, color, true);
340         ////绘制顶端倒影
341         //CRect rc_peak_invert = rect_peak;
342         //rc_peak_invert.MoveToY(rc_invert.top + peak_height + theApp.DPIRound(1.1));
343         //FillAlphaRect(rc_peak_invert, color, 96);
344     }
345 
346 }
347 
348 void CUIDrawer::DrawLyricDoubleLine(CRect rect, LPCTSTR lyric, LPCTSTR next_lyric, int progress, int fade_percent)
349 {
350     CFont* pOldFont = SetLyricFont();
351     static bool swap;
352     static int last_progress;
353     if (last_progress > progress)		//如果当前的歌词进度比上次的小,说明歌词切换到了下一句
354     {
355         swap = !swap;
356     }
357     last_progress = progress;
358 
359 
360     CRect up_rect{ rect }, down_rect{ rect };		//上半部分和下半部分歌词的矩形区域
361     up_rect.bottom = up_rect.top + (up_rect.Height() / 2);
362     down_rect.top = down_rect.bottom - (down_rect.Height() / 2);
363     //根据下一句歌词的文本计算需要的宽度,从而实现下一行歌词右对齐
364     //GetDC()->SelectObject(&theApp.m_font_set.lyric.GetFont(theApp.m_ui_data.full_screen));
365     int width;
366     if (!swap)
367         width = GetTextExtent(next_lyric).cx;
368     else
369         width = GetTextExtent(lyric).cx;
370     if (width < rect.Width())
371         down_rect.left = down_rect.right - width;
372 
373     COLORREF color1, color2;
374     if (theApp.m_lyric_setting_data.lyric_karaoke_disp)
375     {
376         color1 = m_colors.color_text;
377         color2 = m_colors.color_text_2;
378     }
379     else
380     {
381         color1 = color2 = CColorConvert::GetGradientColor(m_colors.color_text_2, m_colors.color_text, fade_percent);
382     }
383 
384     if (!swap)
385     {
386         DrawWindowText(up_rect, lyric, color1, color2, progress);
387         DrawWindowText(down_rect, next_lyric, m_colors.color_text_2);
388     }
389     else
390     {
391         DrawWindowText(up_rect, next_lyric, m_colors.color_text_2);
392         DrawWindowText(down_rect, lyric, color1, color2, progress);
393     }
394     SetFont(pOldFont);
395 }
396 
397 CFont* CUIDrawer::SetLyricFont()
398 {
399     if (!m_for_cortana_lyric)
400         return SetFont(&theApp.m_font_set.lyric.GetFont(theApp.m_ui_data.full_screen));
401     else
402         return SetFont(&theApp.m_font_set.cortana.GetFont());
403 }
404 
405 CFont* CUIDrawer::SetLyricFontTranslated()
406 {
407     if (!m_for_cortana_lyric)
408         return SetFont(&theApp.m_font_set.lyric_translate.GetFont(theApp.m_ui_data.full_screen));
409     else
410         return SetFont(&theApp.m_font_set.cortana_translate.GetFont());
411 }
412