using Dominion.NET_WPF.Caching; using DominionBase.Players; using System; using System.Collections; using System.ComponentModel; using System.Globalization; using System.Linq; using System.Reflection; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Media; using System.Windows.Media.Imaging; namespace Dominion.NET_WPF.Controls { /// /// Interaction logic for ucPlayerSettings.xaml /// public partial class ucPlayerSettings : UserControl { private PlayerType _playerType = PlayerType.Computer; private IEnumerable _aiTypes; private PlayerSettings _playerSettings; public IEnumerable AITypes { get { return _aiTypes; } set { cbAISelection.ItemsSource = _aiTypes = value; } } public PlayerSettings PlayerSettings { get { return _playerSettings; } set { _playerSettings = value; tbName.Text = value.Name; if (scpTint.AvailableColors.Contains(value.UIColor)) scpTint.SelectedColor = value.UIColor; string brushLocation = null; if ((value.BackgroundBrush as ImageBrush)?.ImageSource is BitmapImage image) brushLocation = image.SourcePath(); Brush bgBrush = null; if (brushLocation != null) { bgBrush = sbpBackground.AvailableBrushes.FirstOrDefault(b => (b as ImageBrush)?.ImageSource is BitmapImage im && im.SourcePath() == brushLocation); if (bgBrush != null) sbpBackground.SelectedItem = bgBrush; } if (bgBrush == null && sbpBackground.AvailableBrushes.Any()) sbpBackground.SelectedItem = sbpBackground.AvailableBrushes.First(); foreach (var type in cbAISelection.Items.OfType()) { if (type != value.AIClassType) continue; cbAISelection.SelectedItem = type; break; } } } public int PlayerNumber { get { if (!(lNameNumber.Content is int)) lNameNumber.Content = 0; return (int)lNameNumber.Content; } set { lNameNumber.Content = value; tbName.TabIndex = (value + 2) * 10; scpTint.TabIndex = (value + 2) * 10 + 1; sbpBackground.TabIndex = (value + 2) * 10 + 2; cbAISelection.TabIndex = (value + 2) * 10 + 3; } } public PlayerType PlayerType { get { return _playerType; } set { _playerType = value; switch (value) { case PlayerType.Human: iType.Source = (BitmapImage)Resources["imHuman"]; iType.ToolTip = "Human player"; lAISelect.Visibility = cbAISelection.Visibility = Visibility.Hidden; break; case PlayerType.Computer: iType.Source = (BitmapImage)Resources["imComputer"]; iType.ToolTip = "Computer player"; lAISelect.Visibility = cbAISelection.Visibility = Visibility.Visible; break; } } } public ucPlayerSettings() { InitializeComponent(); scpTint.Clear(); for (var h = 0; h < 15; h++) scpTint.AddColor(HlsColor.HlsToRgb(24 * h, 0.85, 1, 1)); scpTint.AddColor(Colors.Transparent); sbpBackground.Clear(); if (!DesignerProperties.GetIsInDesignMode(this)) { var iRepo = ImageRepository.Acquire(); var path = iRepo.GetImagePath("background"); if (System.IO.Directory.Exists(path)) { foreach (var filename in System.IO.Directory.EnumerateFiles(path)) { try { var bgImage = iRepo.GetBitmapImage(filename, "background"); if (bgImage == null) continue; var bg = new ImageBrush(bgImage) { Stretch = Stretch.Fill, TileMode = TileMode.Tile, ViewportUnits = BrushMappingMode.Absolute }; bg.Viewport = new Rect(0, 0, bg.ImageSource.Height / 2, bg.ImageSource.Width / 2); sbpBackground.AddBrush(bg); } catch { } } } ImageRepository.Release(); } } private void CbAISelection_SelectionChanged(object sender, SelectionChangedEventArgs e) { PlayerSettings.AIClassType = (Type)(sender as ComboBox)?.SelectedItem; } private void TbName_TextChanged(object sender, TextChangedEventArgs e) { PlayerSettings.Name = (sender as TextBox)?.Text; } private void TbName_GotFocus(object sender, RoutedEventArgs e) { (sender as TextBox)?.SelectAll(); } private void ScpTint_ColorChanged(object sender, RoutedPropertyChangedEventArgs e) { var ucSmallColorPicker = sender as ucSmallColorPicker; if (ucSmallColorPicker != null) PlayerSettings.UIColor = ucSmallColorPicker.SelectedColor; } private void SbpBackground_BrushChanged(object sender, RoutedPropertyChangedEventArgs e) { var selectedBrush = (Brush)(sender as ucSmallBrushPicker)?.SelectedItem; dpName.Background = selectedBrush; if (PlayerSettings != null) PlayerSettings.BackgroundBrush = selectedBrush; } } public class AITypeConverterName : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var type = value as Type; if (type == null) return value.ToString(); if (type == typeof(DominionBase.Players.AI.Basic) || type.IsSubclassOf(typeof(DominionBase.Players.AI.Basic))) { return (string)type.GetProperty("AIName", BindingFlags.Static | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.FlattenHierarchy).GetValue(null, null); } if (type == typeof(DominionBase.Players.AI.RandomAI) || type.IsSubclassOf(typeof(DominionBase.Players.AI.RandomAI))) { return (string)type.GetProperty("AIName", BindingFlags.Static | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.FlattenHierarchy).GetValue(null, null); } return value.ToString(); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return null; } } public class AITypeConverterDescription : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var type = value as Type; if (type == null) return value.ToString(); if (type == typeof(DominionBase.Players.AI.Basic) || type.IsSubclassOf(typeof(DominionBase.Players.AI.Basic))) { return (string)type.GetProperty("AIDescription", BindingFlags.Static | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.FlattenHierarchy).GetValue(null, null); } if (type == typeof(DominionBase.Players.AI.RandomAI) || type.IsSubclassOf(typeof(DominionBase.Players.AI.RandomAI))) { return (string)type.GetProperty("AIDescription", BindingFlags.Static | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.FlattenHierarchy).GetValue(null, null); } return value.ToString(); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return null; } } }