using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Media.Animation; using DominionBase; namespace Dominion.NET_WPF { /// /// Interaction logic for ToolTipIDisplayable.xaml /// public partial class ToolTipIDisplayable : UserControl { private bool _isSetup; public ToolTipIDisplayable() { InitializeComponent(); } private void SetupDisplay() { _isSetup = true; if (Card == null) return; ttContainer.Children.Clear(); var cardIndex = 0; var newWidth = 0d; var newHeight = 0d; var displayAll = false; var toDisplay = CardList; if (CardList.DistinctBy(c => c.Name).Count() > 1) { toDisplay = toDisplay.Reverse(); displayAll = true; _isSetup = false; } var s = new Storyboard(); var duration = new Duration(TimeSpan.FromMilliseconds(150)); foreach (var card in toDisplay) { UserControl newTT = null; if (card is DominionBase.Cards.Card) newTT = new ToolTipCard { Card = card }; else if (card is DominionBase.Cards.Event) newTT = new ToolTipEvent { Card = card }; else if (card is DominionBase.Cards.Landmark) newTT = new ToolTipLandmark { Card = card }; else if (card is DominionBase.Cards.Boon) newTT = new ToolTipBoon { Card = card }; else if (card is DominionBase.Cards.State) newTT = new ToolTipState { Card = card }; else if (card is DominionBase.Cards.Hex) newTT = new ToolTipHex { Card = card }; else if (card is DominionBase.Cards.Artifact) newTT = new ToolTipArtifact { Card = card }; else if (card is DominionBase.Cards.Project) newTT = new ToolTipProject { Card = card }; else if (card is DominionBase.Cards.Way) newTT = new ToolTipWay { Card = card }; if (newTT != null) { var newMargin = new Thickness(25 * cardIndex, 25 * cardIndex, 0, 0); newTT.HorizontalAlignment = HorizontalAlignment.Left; newTT.VerticalAlignment = VerticalAlignment.Top; newWidth = Math.Max(newWidth, newTT.Width + newMargin.Left); newHeight = Math.Max(newHeight, newTT.Height + newMargin.Top); ttContainer.Children.Add(newTT); var a = new ThicknessAnimation(newMargin, duration); Storyboard.SetTarget(a, newTT); Storyboard.SetTargetProperty(a, new PropertyPath(MarginProperty)); s.Children.Add(a); } if (!displayAll) break; cardIndex++; } if (Visibility != Visibility.Visible) { Width = newWidth; Height = newHeight; } else { if (Math.Abs(newWidth - Width) > 0.001) BeginAnimation(WidthProperty, new DoubleAnimation(newWidth, new Duration(TimeSpan.FromMilliseconds(50)))); if (Math.Abs(newHeight - Height) > 0.001) BeginAnimation(HeightProperty, new DoubleAnimation(newHeight, new Duration(TimeSpan.FromMilliseconds(50)))); } s.Begin(); } public static readonly DependencyProperty IDisplayableProperty = DependencyProperty.Register("Card", typeof(IDisplayable), typeof(ToolTipIDisplayable), new PropertyMetadata(null)); public IDisplayable Card { get { var cards = CardList; return cards?.FirstOrDefault(); } set { CardList = new List { value }; } } public static readonly DependencyProperty IEnumerableIDisplayableProperty = DependencyProperty.Register("CardList", typeof(IEnumerable), typeof(ToolTipIDisplayable), new PropertyMetadata(null)); public IEnumerable CardList { get { return (IEnumerable)GetValue(IEnumerableIDisplayableProperty); } set { if (CardList == null || value == null || CardList != value) _isSetup = false; SetValue(IEnumerableIDisplayableProperty, value); if (IsVisible) SetupDisplay(); } } private delegate void IsVisibleChanged_Delegate(object sender, DependencyPropertyChangedEventArgs e); private void ToolTipIDisplayable_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { if (Dispatcher.CheckAccess()) { if ((bool)e.NewValue && !_isSetup) SetupDisplay(); } else { Dispatcher.BeginInvoke(new IsVisibleChanged_Delegate(ToolTipIDisplayable_IsVisibleChanged), System.Windows.Threading.DispatcherPriority.Normal, sender, e); } } } }