using DominionBase;
using DominionBase.Cards;
using DominionBase.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;
namespace Dominion.NET_WPF
{
///
/// Interaction logic for ToolTipEvent.xaml
///
public partial class ToolTipEvent : UserControl
{
private bool _isSetup;
public ToolTipEvent()
{
InitializeComponent();
}
private void SetupDisplay()
{
if (Card == null)
return;
var fullCardFound = false;
if (WMain.Settings.UseCustomToolTips)
{
iFullCard.Visibility = Visibility.Visible;
dpCardFace.Visibility = Visibility.Hidden;
var repo = Caching.ImageRepository.Acquire();
var im = repo.GetBitmapImage(Card.ImageName, "full");
if (im != null)
{
iFullCard.Source = im;
fullCardFound = true;
}
Caching.ImageRepository.Release();
}
if (!fullCardFound)
{
iFullCard.Visibility = Visibility.Hidden;
dpCardFace.Visibility = Visibility.Visible;
tbCardName.Text = string.Empty;
Run rName = null;
foreach (var letter in Card.Name)
{
if (rName == null || char.IsUpper(rName.Text[0]) != char.IsUpper(letter))
{
if (rName != null)
tbCardName.Inlines.Add(rName);
rName = new Run {FontSize = char.IsUpper(letter) ? 16 : 12};
}
rName.Text += char.ToUpper(letter);
}
if (rName != null)
tbCardName.Inlines.Add(rName);
var repo = Caching.ImageRepository.Acquire();
var im = repo.GetBitmapImage(Card.ImageName, "medium");
imCardLarge.Source = im;
if (Card.Edition == Edition.Second)
im = repo.GetBitmapImage($"{Card.Source}-{Card.Edition}", "set") ??
repo.GetBitmapImage(Card.Source.ToString(), "set");
else
im = (repo.GetBitmapImage(Card.ImageName.Replace(" ", "").Replace("'", ""), "set") ??
repo.GetBitmapImage($"{Card.Source}-{Card.Edition}", "set")) ??
repo.GetBitmapImage(Card.Source.ToString(), "set");
imSource.Source = im;
Caching.ImageRepository.Release();
var categories = new List();
if (Card.Category.HasFlag(Categories.Event))
categories.Add(DominionBase.ResourcesHelper.Get(Categories.Event));
tbCardCost.Inlines.Clear();
spCardText.Children.Clear();
var tbTemp = (TextBlock)Utilities.RenderText(Card.BaseCost.ToString(), NET_WPF.RenderSize.Medium, false)[0];
while (tbTemp.Inlines.Any())
tbCardCost.Inlines.Add(tbTemp.Inlines.ElementAt(0));
if (Card.BaseCost.Special)
{
var special = new Run("*") {FontWeight = FontWeights.Bold};
tbCardCost.Inlines.Add(special);
}
if (Card.BaseCost.CanOverpay)
{
var canOverpay = new Run("+") {FontWeight = FontWeights.Bold};
tbCardCost.Inlines.Add(canOverpay);
}
tbType.Text = string.Empty;
Run rType = null;
foreach (var letter in string.Join(" - ", categories.ToArray()))
{
if (rType == null || char.IsUpper(rType.Text[0]) != char.IsUpper(letter))
{
if (rType != null)
tbType.Inlines.Add(rType);
rType = new Run {FontSize = char.IsUpper(letter) ? 16 : 14};
}
rType.Text += char.ToUpper(letter);
}
if (rType != null)
tbType.Inlines.Add(rType);
var category = Card.Category;
if (Card is Card card)
category = card.PhysicalCategory;
lblCardName.Background = pEventBorder.Fill = rBackground.Fill = Caching.BrushRepository.GetBackgroundBrush(category);
lblCardName.Foreground = lblType.Foreground = Caching.BrushRepository.GetForegroundBrush(category);
if (category.HasFlag(Categories.Reaction))
tbCardName.Effect = tbType.Effect = Caching.DropShadowRepository.GetDSE(8, Colors.White, 1d);
if (lblCardName.Foreground.CanFreeze)
lblCardName.Foreground.Freeze();
if (lblType.Foreground.CanFreeze)
lblType.Foreground.Freeze();
if (lblCardCost.Foreground.CanFreeze)
lblCardCost.Foreground.Freeze();
var text = Card.Text.Split(new[] { "
" }, StringSplitOptions.RemoveEmptyEntries);
for (var index = 0; index < text.Length; index++)
{
if (text[index].StartsWith(Environment.NewLine))
text[index] = text[index].Substring(Environment.NewLine.Length);
if (index > 0)
{
var newLine = new Line
{
Stretch = Stretch.Fill,
Stroke = Brushes.Black,
StrokeThickness = 2,
X2 = 1,
Margin = new Thickness(20, 6, 20, 6)
};
spCardText.Children.Add(newLine);
}
var t = text[index];
var elements = Utilities.RenderText(t, (Card.Location == Location.General ? NET_WPF.RenderSize.ExtraLarge : NET_WPF.RenderSize.Small), false);
foreach (var tb in elements.OfType())
{
tb.HorizontalAlignment = HorizontalAlignment.Center;
tb.VerticalAlignment = VerticalAlignment.Center;
tb.Margin = new Thickness(10, 0, 10, 0);
tb.Padding = new Thickness(0);
tb.TextWrapping = TextWrapping.Wrap;
tb.TextAlignment = TextAlignment.Center;
}
elements.ForEach(e => spCardText.Children.Add(e));
}
}
_isSetup = true;
}
public static readonly DependencyProperty CardProperty =
DependencyProperty.Register("Card", typeof(IDisplayable), typeof(ToolTipEvent),
new PropertyMetadata(null));
public IDisplayable Card
{
get { return (IDisplayable)GetValue(CardProperty); }
set
{
if (((Card == null || value == null) && Card != value) ||
(Card != null && value != null && Card.Name != value.Name))
_isSetup = false;
SetValue(CardProperty, value);
if (IsVisible)
SetupDisplay();
}
}
private delegate void IsVisibleChanged_Delegate(object sender, DependencyPropertyChangedEventArgs e);
private void ToolTipEvent_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (Dispatcher.CheckAccess())
{
if ((bool)e.NewValue && !_isSetup)
SetupDisplay();
}
else
{
Dispatcher.BeginInvoke(new IsVisibleChanged_Delegate(ToolTipEvent_IsVisibleChanged), System.Windows.Threading.DispatcherPriority.Normal, sender, e);
}
}
}
}