Skip to content

Commit

Permalink
ui中增加“打开新文件夹”、“新建播放列表”、“播放我喜欢的音乐”按钮
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongyang219 committed Jul 26, 2024
1 parent c82153b commit 7be16cb
Show file tree
Hide file tree
Showing 11 changed files with 335 additions and 209 deletions.
105 changes: 66 additions & 39 deletions MusicPlayer2/CPlayerUIBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "SongInfoHelper.h"
#include "UIElement.h"
#include "MediaLibPlaylistMgr.h"
#include "MusicPlayerCmdHelper.h"

CPlayerUIBase::CPlayerUIBase(UIData& ui_data, CWnd* pMainWnd)
: m_ui_data(ui_data), m_pMainWnd(pMainWnd)
Expand Down Expand Up @@ -509,6 +510,23 @@ bool CPlayerUIBase::LButtonUp(CPoint point)
SwitchStackElement();
return true;

case BTN_OPEN_FOLDER:
theApp.m_pMainWnd->SendMessage(WM_COMMAND, ID_FILE_OPEN_FOLDER);
return true;

case BTN_NEW_PLAYLIST:
{
CMusicPlayerCmdHelper helper;
helper.OnNewPlaylist();
return true;
}
case BTN_PLAY_MY_FAVOURITE:
{
CMusicPlayerCmdHelper helper;
helper.OnPlayMyFavourite();
return true;
}

//菜单
case MENU_FILE:
showMenu(btn.second.rect, theApp.m_menu_mgr.GetMenu(MenuMgr::MainFileMenu));
Expand Down Expand Up @@ -771,12 +789,60 @@ IconMgr::IconType CPlayerUIBase::GetBtnIconType(BtnKey key)
return IconMgr::IconType::IT_Setting;
case MENU_HELP:
return IconMgr::IconType::IT_Help;
case BTN_OPEN_FOLDER:
return IconMgr::IconType::IT_Folder;
case BTN_NEW_PLAYLIST:
return IconMgr::IconType::IT_Add;
case BTN_PLAY_MY_FAVOURITE:
return IconMgr::IconType::IT_Play;
default:
ASSERT(FALSE);
return IconMgr::IconType::IT_NO_ICON;
}
}

std::wstring CPlayerUIBase::GetButtonText(BtnKey key_type)
{
switch (key_type)
{
case CPlayerUIBase::BTN_REPETEMODE:
switch (CPlayer::GetInstance().GetRepeatMode())
{
case RM_PLAY_ORDER: return theApp.m_str_table.LoadText(L"UI_TIP_REPEAT_ORDER");
case RM_PLAY_SHUFFLE: return theApp.m_str_table.LoadText(L"UI_TIP_REPEAT_SHUFFLE");
case RM_PLAY_RANDOM: return theApp.m_str_table.LoadText(L"UI_TIP_REPEAT_RANDOM");
case RM_LOOP_PLAYLIST: return theApp.m_str_table.LoadText(L"UI_TIP_REPEAT_PLAYLIST");
case RM_LOOP_TRACK: return theApp.m_str_table.LoadText(L"UI_TIP_REPEAT_TRACK");
case RM_PLAY_TRACK: return theApp.m_str_table.LoadText(L"UI_TIP_REPEAT_ONCE");
}
break;
case CPlayerUIBase::BTN_SKIN: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_SWITCH_UI");
case CPlayerUIBase::BTN_EQ: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_SOUND_EFFECT_SETTING");
case CPlayerUIBase::BTN_SETTING: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_OPTION_SETTING");
case CPlayerUIBase::BTN_MINI: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_MINIMODE");
case CPlayerUIBase::BTN_INFO: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_PROPERTY");
case CPlayerUIBase::BTN_FIND: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_FIND_SONGS");
case CPlayerUIBase::BTN_STOP: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_STOP");
case CPlayerUIBase::BTN_PREVIOUS: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_STOP");
case CPlayerUIBase::BTN_PLAY_PAUSE: return theApp.m_str_table.LoadText(CPlayer::GetInstance().IsPlaying() ? L"UI_TIP_BTN_PAUSE" : L"UI_TIP_BTN_PLAY");
case CPlayerUIBase::BTN_NEXT: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_NEXT");
case CPlayerUIBase::BTN_SHOW_PLAYLIST: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_PLAYLIST_SHOW_HIDE");
case CPlayerUIBase::BTN_MEDIA_LIB: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_MEDIA_LIB");
case CPlayerUIBase::BTN_FULL_SCREEN: return theApp.m_str_table.LoadText(m_ui_data.full_screen ? L"UI_TIP_BTN_FULL_SCREEN_EXIT" : L"UI_TIP_BTN_FULL_SCREEN");
case CPlayerUIBase::BTN_MENU: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_MAIN_MENU");
case CPlayerUIBase::BTN_FAVOURITE: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_FAVOURITE");
case CPlayerUIBase::BTN_ADD_TO_PLAYLIST: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_ADD_TO_PLAYLIST");
case CPlayerUIBase::BTN_SWITCH_DISPLAY: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_SWITCH_DISPLAY");
case CPlayerUIBase::BTN_DARK_LIGHT: return theApp.m_str_table.LoadText(theApp.m_app_setting_data.dark_mode ? L"UI_TIP_BTN_DARK_LIGHT_TO_LIGHT_MODE" : L"UI_TIP_BTN_DARK_LIGHT_TO_DARK_MODE");
case CPlayerUIBase::BTN_LOCATE_TO_CURRENT:return theApp.m_str_table.LoadText(L"UI_TIP_BTN_LOCATE_TO_CURRENT");
case CPlayerUIBase::BTN_OPEN_FOLDER: return theApp.m_str_table.LoadText(L"UI_TXT_BTN_OPEN_FOLDER");
case CPlayerUIBase::BTN_NEW_PLAYLIST: return theApp.m_str_table.LoadText(L"UI_TXT_BTN_NEW_PLAYLIST");
case CPlayerUIBase::BTN_PLAY_MY_FAVOURITE: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_PLAY");
}

return std::wstring();
}

void CPlayerUIBase::PreDrawInfo()
{
//设置颜色
Expand Down Expand Up @@ -3051,45 +3117,6 @@ void CPlayerUIBase::DrawNavigationBar(CRect rect, UiElement::NavigationBar* tab_
ResetDrawArea();
}

std::wstring CPlayerUIBase::GetButtonText(BtnKey key_type)
{
switch (key_type)
{
case CPlayerUIBase::BTN_REPETEMODE:
switch (CPlayer::GetInstance().GetRepeatMode())
{
case RM_PLAY_ORDER: return theApp.m_str_table.LoadText(L"UI_TIP_REPEAT_ORDER");
case RM_PLAY_SHUFFLE: return theApp.m_str_table.LoadText(L"UI_TIP_REPEAT_SHUFFLE");
case RM_PLAY_RANDOM: return theApp.m_str_table.LoadText(L"UI_TIP_REPEAT_RANDOM");
case RM_LOOP_PLAYLIST: return theApp.m_str_table.LoadText(L"UI_TIP_REPEAT_PLAYLIST");
case RM_LOOP_TRACK: return theApp.m_str_table.LoadText(L"UI_TIP_REPEAT_TRACK");
case RM_PLAY_TRACK: return theApp.m_str_table.LoadText(L"UI_TIP_REPEAT_ONCE");
}
break;
case CPlayerUIBase::BTN_SKIN: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_SWITCH_UI");
case CPlayerUIBase::BTN_EQ: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_SOUND_EFFECT_SETTING");
case CPlayerUIBase::BTN_SETTING: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_OPTION_SETTING");
case CPlayerUIBase::BTN_MINI: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_MINIMODE");
case CPlayerUIBase::BTN_INFO: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_PROPERTY");
case CPlayerUIBase::BTN_FIND: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_FIND_SONGS");
case CPlayerUIBase::BTN_STOP: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_STOP");
case CPlayerUIBase::BTN_PREVIOUS: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_STOP");
case CPlayerUIBase::BTN_PLAY_PAUSE: return theApp.m_str_table.LoadText(CPlayer::GetInstance().IsPlaying() ? L"UI_TIP_BTN_PAUSE" : L"UI_TIP_BTN_PLAY");
case CPlayerUIBase::BTN_NEXT: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_NEXT");
case CPlayerUIBase::BTN_SHOW_PLAYLIST: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_PLAYLIST_SHOW_HIDE");
case CPlayerUIBase::BTN_MEDIA_LIB: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_MEDIA_LIB");
case CPlayerUIBase::BTN_FULL_SCREEN: return theApp.m_str_table.LoadText(m_ui_data.full_screen ? L"UI_TIP_BTN_FULL_SCREEN_EXIT" : L"UI_TIP_BTN_FULL_SCREEN");
case CPlayerUIBase::BTN_MENU: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_MAIN_MENU");
case CPlayerUIBase::BTN_FAVOURITE: return theApp.m_str_table.LoadText(L"UI_TIP_BTN_FAVOURITE");
case CPlayerUIBase::BTN_ADD_TO_PLAYLIST:return theApp.m_str_table.LoadText(L"UI_TIP_BTN_ADD_TO_PLAYLIST");
case CPlayerUIBase::BTN_SWITCH_DISPLAY:return theApp.m_str_table.LoadText(L"UI_TIP_BTN_SWITCH_DISPLAY");
case CPlayerUIBase::BTN_DARK_LIGHT:return theApp.m_str_table.LoadText(theApp.m_app_setting_data.dark_mode ? L"UI_TIP_BTN_DARK_LIGHT_TO_LIGHT_MODE" : L"UI_TIP_BTN_DARK_LIGHT_TO_DARK_MODE");
case CPlayerUIBase::BTN_LOCATE_TO_CURRENT:return theApp.m_str_table.LoadText(L"UI_TIP_BTN_LOCATE_TO_CURRENT");
}

return std::wstring();
}

void CPlayerUIBase::DrawUiIcon(const CRect& rect, IconMgr::IconType icon_type, IconMgr::IconStyle icon_style, IconMgr::IconSize icon_size)
{
// style为IS_Auto时根据深色模式设置向IconMgr要求深色/浅色图标,没有对应风格图标时IconMgr会自行fallback
Expand Down
8 changes: 6 additions & 2 deletions MusicPlayer2/CPlayerUIBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ class CPlayerUIBase : public IPlayerUI
BTN_SWITCH_DISPLAY, //切换界面中的stackElement
BTN_DARK_LIGHT, //切换深色/浅色模式
BTN_LOCATE_TO_CURRENT, //播放列表定位到当前播放
BTN_OPEN_FOLDER, //打开文件夹
BTN_NEW_PLAYLIST, //新建播放列表
BTN_PLAY_MY_FAVOURITE, //播放“我喜欢的音乐”

//菜单栏
MENU_FILE,
Expand All @@ -207,6 +210,9 @@ class CPlayerUIBase : public IPlayerUI
// 将BtnKey枚举和当前状态组合映射为IconMgr::IconType枚举
IconMgr::IconType GetBtnIconType(BtnKey key);

//获取按钮的文本
std::wstring GetButtonText(BtnKey key_type);

protected:
struct DrawData
{
Expand Down Expand Up @@ -250,8 +256,6 @@ class CPlayerUIBase : public IPlayerUI
void DrawUiMenuBar(CRect rect);
void DrawNavigationBar(CRect rect, UiElement::NavigationBar* tab_element);

std::wstring GetButtonText(BtnKey key_type);

// 实际绘制一个图标
void DrawUiIcon(const CRect& rect, IconMgr::IconType icon_type, IconMgr::IconStyle icon_style = IconMgr::IconStyle::IS_Auto, IconMgr::IconSize icon_size = IconMgr::IconSize::IS_DPI_16);
// 绘制一个UI按钮 (使用GetBtnIconType取得的图标)
Expand Down
16 changes: 16 additions & 0 deletions MusicPlayer2/MusicPlayerCmdHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,22 @@ bool CMusicPlayerCmdHelper::OnRemoveFromPlaylist(const std::vector<SongInfo>& so
return false;
}

void CMusicPlayerCmdHelper::OnPlayMyFavourite()
{
//已经在播放“我喜欢的音乐”
if (CPlayer::GetInstance().IsPlaylistMode() && CPlaylistMgr::Instance().GetCurPlaylistType() == PT_FAVOURITE)
{
//不在播放状态时执行播放命令
if (!CPlayer::GetInstance().IsPlaying())
theApp.m_pMainWnd->SendMessage(WM_COMMAND, ID_PLAY);
}
//没有播放“我喜欢的音乐”的情况下,播放“我喜欢的音乐”
else
{
OnPlaylistSelected(CPlaylistMgr::Instance().GetFavouritePlaylist(), true);
}
}

void CMusicPlayerCmdHelper::AddToPlaylist(const std::vector<SongInfo>& songs, const std::wstring& playlist_path)
{
CMusicPlayerDlg* pPlayerDlg = CMusicPlayerDlg::GetInstance();
Expand Down
2 changes: 2 additions & 0 deletions MusicPlayer2/MusicPlayerCmdHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class CMusicPlayerCmdHelper
//从播放列表中移除
bool OnRemoveFromPlaylist(const std::vector<SongInfo>& songs, const std::wstring& playlist_path);

void OnPlayMyFavourite();

protected:
void AddToPlaylist(const std::vector<SongInfo>& songs, const std::wstring& playlist_path);

Expand Down
37 changes: 32 additions & 5 deletions MusicPlayer2/UIElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ void UiElement::Button::Draw()
ui->DrawABRepeatButton(rect);
break;
default:
ui->DrawUIButton(rect, key, big_icon, show_text);
ui->DrawUIButton(rect, key, big_icon, show_text, font_size);
break;
}
ui->ResetDrawArea();
Expand Down Expand Up @@ -694,18 +694,39 @@ void UiElement::Button::FromString(const std::string& key_type)
key = CPlayerUIBase::BTN_DARK_LIGHT;
else if (key_type == "locateTrack")
key = CPlayerUIBase::BTN_LOCATE_TO_CURRENT;
else if (key_type == "openFolder")
key = CPlayerUIBase::BTN_OPEN_FOLDER;
else if (key_type == "newPlaylist")
key = CPlayerUIBase::BTN_NEW_PLAYLIST;
else if (key_type == "playMyFavourite")
key = CPlayerUIBase::BTN_PLAY_MY_FAVOURITE;
else
key = CPlayerUIBase::BTN_INVALID;
}

int UiElement::Button::GetMaxWidth(CRect parent_rect) const
{
//显示文本时跟随文本宽度
if (show_text)
//显示文本,并且没有指定宽度时时跟随文本宽度
if (show_text && !width.IsValid())
{
std::wstring text = ui->GetButtonText(key);
int right_space = (rect.Height() - ui->DPI(16)) / 2;
int width_text{ ui->m_draw.GetTextExtent(text.c_str()).cx + right_space + rect.Height() };
//第一次执行到这里时,由于rect还没有从layout元素中计算出来,因此这里做一下判断,如果高度为0,则直接获取height的值
int btn_height = rect.Height();
if (btn_height == 0)
btn_height = Element::height.GetValue(parent_rect);
int right_space = (btn_height - ui->DPI(16)) / 2;

//计算文本宽度前先设置一下字体
CFont* old_font{}; //原先的字体
bool big_font{ ui->m_ui_data.full_screen && ui->IsDrawLargeIcon() };
old_font = ui->m_draw.SetFont(&theApp.m_font_set.GetFontBySize(font_size).GetFont(big_font));

int width_text{ ui->m_draw.GetTextExtent(text.c_str()).cx + right_space + btn_height };

//恢复原来的字体
if (old_font != nullptr)
ui->m_draw.SetFont(old_font);

int width_max{ max_width.IsValid() ? max_width.GetValue(parent_rect) : INT_MAX };
return min(width_text, width_max);
}
Expand All @@ -715,6 +736,12 @@ int UiElement::Button::GetMaxWidth(CRect parent_rect) const
}
}

void UiElement::Button::ClearRect()
{
Element::ClearRect();
ui->m_buttons[key].rect = CRect();
}

void UiElement::Text::Draw()
{
CalculateRect();
Expand Down
1 change: 1 addition & 0 deletions MusicPlayer2/UIElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ namespace UiElement
virtual void Draw() override;
void FromString(const std::string& key_type);
virtual int GetMaxWidth(CRect parent_rect) const override;
virtual void ClearRect() override;
};

//文本
Expand Down
2 changes: 2 additions & 0 deletions MusicPlayer2/language/English.ini
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ UI_TXT_LYRICS = "Lyrics"
UI_TXT_PLAY_QUEUE = "Paly queue"
UI_TXT_BTN_MEDIA_LIB = "Media Library..."
UI_TXT_BTN_OPEN_FOLDER = "Open New Folder"
UI_TXT_BTN_NEW_PLAYLIST = "New Playlist"
UI_LYRIC_EMPTY_LINE = "..."
UI_LYRIC_EMPTY_LINE_2 = "Music ..."
Expand Down
2 changes: 2 additions & 0 deletions MusicPlayer2/language/Simplified_Chinese.ini
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ UI_TXT_LYRICS = "歌词"
UI_TXT_PLAY_QUEUE = "播放队列"
UI_TXT_BTN_MEDIA_LIB = "媒体库..."
UI_TXT_BTN_OPEN_FOLDER = "打开新文件夹"
UI_TXT_BTN_NEW_PLAYLIST = "新建播放列表"
UI_LYRIC_EMPTY_LINE = "……"
UI_LYRIC_EMPTY_LINE_2 = "Music …"
Expand Down
35 changes: 25 additions & 10 deletions MusicPlayer2/skins/02_grooveMusic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
<horizontalLayout>
<verticalLayout width="134">
<navigationBar orientation="vertical" icon_type="icon_and_text" item_height="40" item_list="now_playing,play_queue,recently_played,folder,playlist,my_favourite,media_lib"/>
<horizontalLayout height="40">
<button key="settings" width="48" height="40"/>
<placeHolder/>
</horizontalLayout>
<button key="settings" height="40" show_text="true"/>
</verticalLayout>
<stackElement>
<!--正在播放-->
Expand Down Expand Up @@ -43,17 +40,26 @@
</verticalLayout>
<!--文件夹-->
<verticalLayout>
<text type="userDefine" text="%(TXT_FOLDER)" height="56" font_size="12" margin-left="8"/>
<horizontalLayout height="56" margin-left="8" margin-right="8">
<text type="userDefine" text="%(TXT_FOLDER)" font_size="12"/>
<button key="openFolder" height="32" show_text="true"/>
</horizontalLayout>
<mediaLibFolder item_height="30"/>
</verticalLayout>
<!--播放列表-->
<verticalLayout>
<text type="userDefine" text="%(TXT_PLAYLIST)" height="56" font_size="12" margin-left="8"/>
<horizontalLayout height="56" margin-left="8" margin-right="8">
<text type="userDefine" text="%(TXT_PLAYLIST)" font_size="12"/>
<button key="newPlaylist" height="32" show_text="true"/>
</horizontalLayout>
<mediaLibPlaylist item_height="30"/>
</verticalLayout>
<!--我喜欢的音乐-->
<verticalLayout>
<text type="userDefine" text="%(TXT_MY_FAVOURITE)" height="56" font_size="12" margin-left="8"/>
<horizontalLayout height="56" margin-left="8" margin-right="8">
<text type="userDefine" text="%(TXT_MY_FAVOURITE)" font_size="12"/>
<button key="playMyFavourite" height="32" show_text="true"/>
</horizontalLayout>
<myFavouriteList item_height="30"/>
</verticalLayout>
<!--媒体库-->
Expand Down Expand Up @@ -138,17 +144,26 @@
</verticalLayout>
<!--文件夹-->
<verticalLayout>
<text type="userDefine" text="%(TXT_FOLDER)" height="56" font_size="12" margin-left="8"/>
<horizontalLayout height="56" margin-left="8" margin-right="8">
<text type="userDefine" text="%(TXT_FOLDER)" font_size="12"/>
<button key="openFolder" height="32" show_text="true"/>
</horizontalLayout>
<mediaLibFolder item_height="30"/>
</verticalLayout>
<!--播放列表-->
<verticalLayout>
<text type="userDefine" text="%(TXT_PLAYLIST)" height="56" font_size="12" margin-left="8"/>
<horizontalLayout height="56" margin-left="8" margin-right="8">
<text type="userDefine" text="%(TXT_PLAYLIST)" font_size="12"/>
<button key="newPlaylist" height="32" show_text="true"/>
</horizontalLayout>
<mediaLibPlaylist item_height="30"/>
</verticalLayout>
<!--我喜欢的音乐-->
<verticalLayout>
<text type="userDefine" text="%(TXT_MY_FAVOURITE)" height="56" font_size="12" margin-left="8"/>
<horizontalLayout height="56" margin-left="8" margin-right="8">
<text type="userDefine" text="%(TXT_MY_FAVOURITE)" font_size="12"/>
<button key="playMyFavourite" height="32" show_text="true"/>
</horizontalLayout>
<myFavouriteList item_height="30"/>
</verticalLayout>
<!--媒体库-->
Expand Down
Loading

0 comments on commit 7be16cb

Please sign in to comment.