This repository has been archived on 2025-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
Karaoke-Kingpin/Controller/MainViewModel.cs

203 lines
6.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO; // 添加這行
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Karaoke_Kingpin.Controller
{
public class MainViewModel : INotifyPropertyChanged
{
private string _cacheImage;
private string _filmImage;
private string _bugImage;
private string _userImage;
private string _imagesImage;
private ObservableCollection<string> _marquee1Items;
private ObservableCollection<string> _marquee2Items;
private string selectedRoom;
private string selectedColor;
private string contentText;
public string CacheImage
{
get { return _cacheImage; }
set
{
_cacheImage = value;
OnPropertyChanged(nameof(CacheImage));
}
}
public string FilmImage
{
get { return _filmImage; }
set
{
_filmImage = value;
OnPropertyChanged(nameof(FilmImage));
}
}
public string BugImage
{
get { return _bugImage; }
set
{
_bugImage = value;
OnPropertyChanged(nameof(BugImage));
}
}
public string UserImage
{
get { return _userImage; }
set
{
_userImage = value;
OnPropertyChanged(nameof(UserImage));
}
}
public string ImagesImage
{
get { return _imagesImage; }
set
{
_imagesImage = value;
OnPropertyChanged(nameof(ImagesImage));
}
}
public ObservableCollection<string> Marquee1Items
{
get { return _marquee1Items; }
set
{
_marquee1Items = value;
OnPropertyChanged(nameof(Marquee1Items));
}
}
public ObservableCollection<string> Marquee2Items
{
get { return _marquee2Items; }
set
{
_marquee2Items = value;
OnPropertyChanged(nameof(Marquee2Items));
}
}
public string SelectedRoom
{
get => selectedRoom;
set
{
if (selectedRoom != value)
{
selectedRoom = value;
OnPropertyChanged();
UpdateContentTextRoom();
}
}
}
public string SelectedColor
{
get => selectedColor;
set
{
if (selectedColor != value)
{
selectedColor = value;
OnPropertyChanged();
UpdateContentTextColor();
}
}
}
public string ContentText
{
get => contentText;
set
{
if (contentText != value)
{
contentText = value;
OnPropertyChanged();
}
}
}
public MainViewModel()
{
// Initialize with the image filename
CacheImage = @"outputfile\Imagers\cache.jpg";
FilmImage = @"outputfile\Imagers\film.jpg";
BugImage = @"outputfile\Imagers\bug.jpg";
UserImage = @"outputfile\Imagers\user.jpg";
ImagesImage = @"outputfile\Imagers\images.jpg";
// Initialize Marquee Items
Marquee1Items = new ObservableCollection<string>(File.ReadAllLines(@"txt\marquee1_items.txt"));
Marquee2Items = new ObservableCollection<string>(File.ReadAllLines(@"txt\marquee2_items.txt"));
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void UpdateContentTextColor()
{
if (!string.IsNullOrEmpty(ContentText) && ContentText.Length >= 5)
{
// 正则表达式模式,匹配"全部"或4位数字 + 颜色 + "-"
string pattern = @"^(全部|\d{4})\((白色|紅色|綠色|黑色|藍色)\)-";
Match match = Regex.Match(ContentText, pattern);
if (match.Success)
{
// 提取颜色部分
string colorName = selectedColor;
// 构建新的 ContentText只在右边添加括号
ContentText = ContentText.Substring(0, match.Groups[1].Length + 1) +
colorName +
")" +
ContentText.Substring(match.Groups[1].Length + 1 + colorName.Length + 1);
// 通知 ContentText 属性已更新
OnPropertyChanged(nameof(ContentText));
}
}
}
private void UpdateContentTextRoom()
{
if (!string.IsNullOrEmpty(ContentText) && !string.IsNullOrEmpty(SelectedRoom))
{
// 正则表达式模式,匹配"全部"或4位数字 + 颜色 + "-"
string pattern = @"^(全部|\d{4})\((白色|紅色|綠色|黑色|藍色)\)-";
Match match = Regex.Match(ContentText, pattern);
if (match.Success)
{
// 构建新的 ContentText将 "全部" 或房号替换为 SelectedRoom
ContentText = SelectedRoom + ContentText.Substring(match.Groups[1].Length);
// 通知 ContentText 属性已更新
OnPropertyChanged(nameof(ContentText));
}
}
}
}
}