using DownKyi.Core.Settings; using DownKyi.Core.Utils.Validator; using DownKyi.Events; using DownKyi.Utils; using Prism.Commands; using Prism.Events; using Prism.Regions; using System.Collections.Generic; using System.Drawing.Text; namespace DownKyi.ViewModels.Settings { public class ViewDanmakuViewModel : BaseViewModel { public const string Tag = "PageSettingsDanmaku"; private bool isOnNavigatedTo; #region 页面属性申明 private bool topFilter; public bool TopFilter { get { return topFilter; } set { SetProperty(ref topFilter, value); } } private bool bottomFilter; public bool BottomFilter { get { return bottomFilter; } set { SetProperty(ref bottomFilter, value); } } private bool scrollFilter; public bool ScrollFilter { get { return scrollFilter; } set { SetProperty(ref scrollFilter, value); } } private int screenWidth; public int ScreenWidth { get { return screenWidth; } set { SetProperty(ref screenWidth, value); } } private int screenHeight; public int ScreenHeight { get { return screenHeight; } set { SetProperty(ref screenHeight, value); } } private List fonts; public List Fonts { get { return fonts; } set { SetProperty(ref fonts, value); } } private string selectedFont; public string SelectedFont { get { return selectedFont; } set { SetProperty(ref selectedFont, value); } } private int fontSize; public int FontSize { get { return fontSize; } set { SetProperty(ref fontSize, value); } } private int lineCount; public int LineCount { get { return lineCount; } set { SetProperty(ref lineCount, value); } } private bool sync; public bool Sync { get { return sync; } set { SetProperty(ref sync, value); } } private bool async; public bool Async { get { return async; } set { SetProperty(ref async, value); } } #endregion public ViewDanmakuViewModel(IEventAggregator eventAggregator) : base(eventAggregator) { #region 属性初始化 // 弹幕字体 Fonts = new List(); var fontCollection = new InstalledFontCollection(); foreach (var font in fontCollection.Families) { Fonts.Add(font.Name); } #endregion } /// /// 导航到VideoDetail页面时执行 /// /// public override void OnNavigatedTo(NavigationContext navigationContext) { base.OnNavigatedTo(navigationContext); isOnNavigatedTo = true; // 屏蔽顶部弹幕 AllowStatus danmakuTopFilter = SettingsManager.GetInstance().GetDanmakuTopFilter(); TopFilter = danmakuTopFilter == AllowStatus.YES; // 屏蔽底部弹幕 AllowStatus danmakuBottomFilter = SettingsManager.GetInstance().GetDanmakuBottomFilter(); BottomFilter = danmakuBottomFilter == AllowStatus.YES; // 屏蔽滚动弹幕 AllowStatus danmakuScrollFilter = SettingsManager.GetInstance().GetDanmakuScrollFilter(); ScrollFilter = danmakuScrollFilter == AllowStatus.YES; // 分辨率-宽 ScreenWidth = SettingsManager.GetInstance().GetDanmakuScreenWidth(); // 分辨率-高 ScreenHeight = SettingsManager.GetInstance().GetDanmakuScreenHeight(); // 弹幕字体 string danmakuFont = SettingsManager.GetInstance().GetDanmakuFontName(); if (danmakuFont != null && Fonts.Contains(danmakuFont)) { // 只有系统中存在当前设置的字体,才能显示 SelectedFont = danmakuFont; } // 弹幕字体大小 FontSize = SettingsManager.GetInstance().GetDanmakuFontSize(); // 弹幕限制行数 LineCount = SettingsManager.GetInstance().GetDanmakuLineCount(); // 弹幕布局算法 DanmakuLayoutAlgorithm layoutAlgorithm = SettingsManager.GetInstance().GetDanmakuLayoutAlgorithm(); SetLayoutAlgorithm(layoutAlgorithm); isOnNavigatedTo = false; } #region 命令申明 // 屏蔽顶部弹幕事件 private DelegateCommand topFilterCommand; public DelegateCommand TopFilterCommand => topFilterCommand ?? (topFilterCommand = new DelegateCommand(ExecuteTopFilterCommand)); /// /// 屏蔽顶部弹幕事件 /// private void ExecuteTopFilterCommand() { AllowStatus isTopFilter = TopFilter ? AllowStatus.YES : AllowStatus.NO; bool isSucceed = SettingsManager.GetInstance().SetDanmakuTopFilter(isTopFilter); PublishTip(isSucceed); } // 屏蔽底部弹幕事件 private DelegateCommand bottomFilterCommand; public DelegateCommand BottomFilterCommand => bottomFilterCommand ?? (bottomFilterCommand = new DelegateCommand(ExecuteBottomFilterCommand)); /// /// 屏蔽底部弹幕事件 /// private void ExecuteBottomFilterCommand() { AllowStatus isBottomFilter = BottomFilter ? AllowStatus.YES : AllowStatus.NO; bool isSucceed = SettingsManager.GetInstance().SetDanmakuBottomFilter(isBottomFilter); PublishTip(isSucceed); } // 屏蔽滚动弹幕事件 private DelegateCommand scrollFilterCommand; public DelegateCommand ScrollFilterCommand => scrollFilterCommand ?? (scrollFilterCommand = new DelegateCommand(ExecuteScrollFilterCommand)); /// /// 屏蔽滚动弹幕事件 /// private void ExecuteScrollFilterCommand() { AllowStatus isScrollFilter = ScrollFilter ? AllowStatus.YES : AllowStatus.NO; bool isSucceed = SettingsManager.GetInstance().SetDanmakuScrollFilter(isScrollFilter); PublishTip(isSucceed); } // 设置分辨率-宽事件 private DelegateCommand screenWidthCommand; public DelegateCommand ScreenWidthCommand => screenWidthCommand ?? (screenWidthCommand = new DelegateCommand(ExecuteScreenWidthCommand)); /// /// 设置分辨率-宽事件 /// /// private void ExecuteScreenWidthCommand(string parameter) { int width = (int)Number.GetInt(parameter); ScreenWidth = width; bool isSucceed = SettingsManager.GetInstance().SetDanmakuScreenWidth(ScreenWidth); PublishTip(isSucceed); } // 设置分辨率-高事件 private DelegateCommand screenHeightCommand; public DelegateCommand ScreenHeightCommand => screenHeightCommand ?? (screenHeightCommand = new DelegateCommand(ExecuteScreenHeightCommand)); /// /// 设置分辨率-高事件 /// /// private void ExecuteScreenHeightCommand(string parameter) { int height = (int)Number.GetInt(parameter); ScreenHeight = height; bool isSucceed = SettingsManager.GetInstance().SetDanmakuScreenHeight(ScreenHeight); PublishTip(isSucceed); } // 弹幕字体选择事件 private DelegateCommand fontSelectCommand; public DelegateCommand FontSelectCommand => fontSelectCommand ?? (fontSelectCommand = new DelegateCommand(ExecuteFontSelectCommand)); /// /// 弹幕字体选择事件 /// /// private void ExecuteFontSelectCommand(string parameter) { bool isSucceed = SettingsManager.GetInstance().SetDanmakuFontName(parameter); PublishTip(isSucceed); } // 弹幕字体大小事件 private DelegateCommand fontSizeCommand; public DelegateCommand FontSizeCommand => fontSizeCommand ?? (fontSizeCommand = new DelegateCommand(ExecuteFontSizeCommand)); /// /// 弹幕字体大小事件 /// /// private void ExecuteFontSizeCommand(string parameter) { int fontSize = (int)Number.GetInt(parameter); FontSize = fontSize; bool isSucceed = SettingsManager.GetInstance().SetDanmakuFontSize(FontSize); PublishTip(isSucceed); } // 弹幕限制行数事件 private DelegateCommand lineCountCommand; public DelegateCommand LineCountCommand => lineCountCommand ?? (lineCountCommand = new DelegateCommand(ExecuteLineCountCommand)); /// /// 弹幕限制行数事件 /// /// private void ExecuteLineCountCommand(string parameter) { int lineCount = (int)Number.GetInt(parameter); LineCount = lineCount; bool isSucceed = SettingsManager.GetInstance().SetDanmakuLineCount(LineCount); PublishTip(isSucceed); } // 弹幕布局算法事件 private DelegateCommand layoutAlgorithmCommand; public DelegateCommand LayoutAlgorithmCommand => layoutAlgorithmCommand ?? (layoutAlgorithmCommand = new DelegateCommand(ExecuteLayoutAlgorithmCommand)); /// /// 弹幕布局算法事件 /// /// private void ExecuteLayoutAlgorithmCommand(string parameter) { DanmakuLayoutAlgorithm layoutAlgorithm; switch (parameter) { case "Sync": layoutAlgorithm = DanmakuLayoutAlgorithm.SYNC; break; case "Async": layoutAlgorithm = DanmakuLayoutAlgorithm.ASYNC; break; default: layoutAlgorithm = DanmakuLayoutAlgorithm.SYNC; break; } bool isSucceed = SettingsManager.GetInstance().SetDanmakuLayoutAlgorithm(layoutAlgorithm); PublishTip(isSucceed); if (isSucceed) { SetLayoutAlgorithm(layoutAlgorithm); } } #endregion /// /// 设置弹幕同步算法 /// /// private void SetLayoutAlgorithm(DanmakuLayoutAlgorithm layoutAlgorithm) { switch (layoutAlgorithm) { case DanmakuLayoutAlgorithm.SYNC: Sync = true; Async = false; break; case DanmakuLayoutAlgorithm.ASYNC: Sync = false; Async = true; break; case DanmakuLayoutAlgorithm.NONE: Sync = false; Async = false; break; default: break; } } /// /// 发送需要显示的tip /// /// private void PublishTip(bool isSucceed) { if (isOnNavigatedTo) { return; } if (isSucceed) { eventAggregator.GetEvent().Publish(DictionaryResource.GetString("TipSettingUpdated")); } else { eventAggregator.GetEvent().Publish(DictionaryResource.GetString("TipSettingFailed")); } } } }