using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using Dominion.NET_WPF.Controls.GameLog; namespace Dominion.NET_WPF.Controls { /// /// Interaction logic for ucGameLog.xaml /// public partial class ucGameLog : UserControl { private LogSection _currentPlayerTurn; private LogSection _currentGameTurn; public string LogFile = string.Empty; private readonly Dictionary> _playerBrushes = new Dictionary>(); public ucGameLog() { InitializeComponent(); } public static DependencyProperty VerticalScrollBarVisibilityProperty = DependencyProperty.Register("VerticalScrollBarVisibility", typeof(ScrollBarVisibility), typeof(ucGameLog)); public static DependencyProperty HorizontalScrollBarVisibilityProperty = DependencyProperty.Register("HorizontalScrollBarVisibility", typeof(ScrollBarVisibility), typeof(ucGameLog)); public ScrollBarVisibility VerticalScrollBarVisibility { get { return (ScrollBarVisibility)GetValue(VerticalScrollBarVisibilityProperty); } set { SetValue(VerticalScrollBarVisibilityProperty, value); } } public ScrollBarVisibility HorizontalScrollBarVisibility { get { return (ScrollBarVisibility)GetValue(HorizontalScrollBarVisibilityProperty); } set { SetValue(HorizontalScrollBarVisibilityProperty, value); } } public void TearDown() { _currentPlayerTurn = null; foreach (var ls in spArea.Children.OfType()) ls.TearDown(); _playerBrushes.Clear(); spArea.Children.Clear(); } public void Clear() { _currentPlayerTurn = null; foreach (var ls in spArea.Children.OfType()) ls.Dispose(); _playerBrushes.Clear(); spArea.Children.Clear(); svArea.ScrollToTop(); if (System.IO.File.Exists(LogFile)) Utilities.LogClear(LogFile); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); } public void Push() { _currentPlayerTurn?.Push(); } public void Pop() { _currentPlayerTurn?.Pop(); } public void NewSection(string title) { Utilities.Log(LogFile, "-------------------------------------------------------"); _currentPlayerTurn?.End(); _currentPlayerTurn = new ucGameMessage { LogFile = LogFile }; _currentPlayerTurn.New(title); spArea.Children.Add(_currentPlayerTurn); svArea.ScrollToBottom(); svArea.ScrollToLeftEnd(); } public void NewTurn(int turnNumber) { Utilities.Log(LogFile, string.Format("=======================================================", turnNumber)); Utilities.Log(LogFile, $"---------------------- Turn #{turnNumber} {(new StringBuilder()).Insert(0, "-", 3 - (int)Math.Log10(turnNumber))}---------------------"); _currentGameTurn?.End(); _currentGameTurn = new ucGameTurn { LogFile = LogFile }; ((ucGameTurn)_currentGameTurn).New(turnNumber); spArea.Children.Add(_currentGameTurn); svArea.ScrollToBottom(); svArea.ScrollToLeftEnd(); } public void NewTurn(DominionBase.IPlayer player, DominionBase.IDisplayable grantedBy) { Utilities.Log(LogFile, "-------------------------------------------------------"); _currentPlayerTurn?.End(); _currentPlayerTurn = new ucPlayerTurn { LogFile = LogFile }; if (player != null) _currentPlayerTurn.New(player, _playerBrushes[player.Name], grantedBy); if (_currentGameTurn != null) { (_currentGameTurn as ucGameTurn)?.Add((ucPlayerTurn)_currentPlayerTurn); if (WMain.Settings.AutoCollapseOldTurns) { var gameTurns = spArea.Children.OfType().ToList(); if (gameTurns.Count > 1) { var gtOld = gameTurns.ElementAt(gameTurns.Count - 2); foreach (var pt in gtOld.GetChildren(player)) pt.IsExpanded = false; if (!gtOld.IsAnyExpanded) { gtOld.IsAllExpanded = true; gtOld.IsExpanded = false; } } } } else { spArea.Children.Add(_currentPlayerTurn); } svArea.ScrollToBottom(); svArea.ScrollToLeftEnd(); } public void Log(DominionBase.IPlayer player, params object[] items) { if (_currentPlayerTurn == null) NewTurn(null, null); _currentPlayerTurn?.Log(new DominionBase.Visual.VisualPlayer(player), _playerBrushes[player.Name], items); svArea.ScrollToBottom(); svArea.ScrollToLeftEnd(); } public void Log(DominionBase.Visual.VisualPlayer player, params object[] items) { if (_currentPlayerTurn == null) NewTurn(null, null); _currentPlayerTurn?.Log(player, _playerBrushes[player.Name], items); svArea.ScrollToBottom(); svArea.ScrollToLeftEnd(); } internal void Log(params object[] items) { if (_currentPlayerTurn == null) NewSection(string.Empty); _currentPlayerTurn?.Log(items); svArea.ScrollToBottom(); svArea.ScrollToLeftEnd(); } private void miCollapseAll_Click(object sender, RoutedEventArgs e) { foreach (var ls in spArea.Children.OfType()) ls.IsExpanded = false; } private void miExpandAll_Click(object sender, RoutedEventArgs e) { foreach (var ls in spArea.Children.OfType()) ls.IsExpanded = true; } internal void AddPlayerColor(string player, Color color) { var hlsValue = HlsColor.RgbToHls(color); _playerBrushes[player] = new List { new SolidColorBrush(HlsColor.HlsToRgb(hlsValue.H, hlsValue.L*1.1, hlsValue.S, hlsValue.A)), new SolidColorBrush(HlsColor.HlsToRgb(hlsValue.H, hlsValue.L*1.125, hlsValue.S*0.95, hlsValue.A)), new SolidColorBrush(HlsColor.HlsToRgb(hlsValue.H, hlsValue.L*0.25, hlsValue.S, hlsValue.A)) }; _playerBrushes[player].ForEach(b => b.Freeze()); } private void CurrentGame_ViewGameLog_Click(object sender, RoutedEventArgs e) { if (System.IO.File.Exists(LogFile)) System.Diagnostics.Process.Start(LogFile); } private void ContextMenu_Opened(object sender, RoutedEventArgs e) { miViewGameLog.IsEnabled = System.IO.File.Exists(LogFile); } } }