using DominionBase.Cards; using DominionBase.Enums; using System; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Input; using System.Windows.Media; using Orientation = System.Windows.Controls.Orientation; namespace Dominion.NET_WPF.Controls { /// /// Interaction logic for ucICardCheckBox.xaml /// public partial class ucICardCheckBox : UserControl, IDisposable { public static readonly DependencyProperty CardProperty = DependencyProperty.Register("Card", typeof(DominionBase.IDisplayable), typeof(ucICardCheckBox), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure, OnCardChanged, CoerceCard)); private static void OnCardChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var ctrl = d as ucICardCheckBox; if (ctrl != null) ctrl.Card = (DominionBase.IDisplayable)e.NewValue; } private static object CoerceCard(DependencyObject d, object value) { return value; } public static readonly DependencyProperty IsCheckableProperty = DependencyProperty.Register("IsCheckable", typeof(bool), typeof(ucICardCheckBox), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure, OnIsCheckableChanged, CoerceIsCheckable)); private static void OnIsCheckableChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var ctrl = d as ucICardCheckBox; if (ctrl != null) ctrl.IsCheckable = (bool)e.NewValue; } private static object CoerceIsCheckable(DependencyObject d, object value) { return value; } public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register("Orientation", typeof(Orientation), typeof(ucICardCheckBox), new FrameworkPropertyMetadata(Orientation.Horizontal, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure, OnOrientationChanged, CoerceOrientation)); private static void OnOrientationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var ctrl = d as ucICardCheckBox; if (ctrl != null) ctrl.Orientation = (Orientation)e.NewValue; } private static object CoerceOrientation(DependencyObject d, object value) { return value; } public static readonly DependencyProperty CardVisibilityProperty = DependencyProperty.Register("CardVisibility", typeof(DominionBase.Piles.Visibility), typeof(ucICardCheckBox), new FrameworkPropertyMetadata(DominionBase.Piles.Visibility.All, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure, OnCardVisibilityChanged, CoerceCardVisibility)); private static void OnCardVisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var ctrl = d as ucICardCheckBox; if (ctrl != null) ctrl.CardVisibility = (DominionBase.Piles.Visibility)e.NewValue; } private static object CoerceCardVisibility(DependencyObject d, object value) { return value; } public static readonly RoutedEvent ICardCheckBoxCheckedEvent = EventManager.RegisterRoutedEvent( "ICardCheckBoxChecked", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ucICardCheckBox)); public event RoutedEventHandler ICardCheckBoxChecked { add { AddHandler(ICardCheckBoxCheckedEvent, value); } remove { RemoveHandler(ICardCheckBoxCheckedEvent, value); } } public ucICardCheckBox() { InitializeComponent(); if (WMain.Settings != null) { WMain.Settings.SettingsChanged += Settings_SettingsChanged; Settings_SettingsChanged(WMain.Settings, null); } } public void Dispose() { if (WMain.Settings != null) { WMain.Settings.SettingsChanged -= Settings_SettingsChanged; } } private void Settings_SettingsChanged(object sender, SettingsChangedEventArgs e) { var settings = sender as Settings; if (settings != null && settings.ToolTipShowDuration == ToolTipShowDuration.Off) ToolTipService.SetIsEnabled(this, false); else { ToolTipService.SetIsEnabled(this, true); ToolTipService.SetShowDuration(this, (int)settings.ToolTipShowDuration); } } public bool? IsChecked { get { return cbSelected.IsChecked; } set { cbSelected.IsChecked = value; } } public bool IsCheckable { get { return (bool)GetValue(IsCheckableProperty); } set { SetValue(IsCheckableProperty, value); cbSelected.Visibility = value ? Visibility.Visible : Visibility.Collapsed; } } public Orientation Orientation { get { return (Orientation)GetValue(OrientationProperty); } set { SetValue(OrientationProperty, value); if (value == Orientation.Horizontal) { Padding = new Thickness(3, 0, 3, 0); spMain.Orientation = Orientation.Horizontal; spMain.Margin = new Thickness(2); var rotateTransform = lName.LayoutTransform as RotateTransform; if (rotateTransform != null) rotateTransform.Angle = 0; } else { Padding = new Thickness(0, 0, 1, 1); spMain.Orientation = Orientation.Vertical; spMain.Margin = new Thickness(0); var rotateTransform = lName.LayoutTransform as RotateTransform; if (rotateTransform != null) rotateTransform.Angle = -90; } } } public DominionBase.Piles.Visibility CardVisibility { get { return (DominionBase.Piles.Visibility)GetValue(CardVisibilityProperty); } set { SetValue(CardVisibilityProperty, value); switch (value) { case DominionBase.Piles.Visibility.All: tbName.Visibility = Visibility.Visible; imName.Visibility = Visibility.Collapsed; break; case DominionBase.Piles.Visibility.None: lName.BorderThickness = new Thickness(0); tbName.Visibility = Visibility.Collapsed; imName.Visibility = Visibility.Visible; break; } UpdateCardDisplay(); } } public DominionBase.IDisplayable Card { get { return (DominionBase.IDisplayable)GetValue(CardProperty); } set { SetValue(CardProperty, value); UpdateCardDisplay(); } } public string Title { get { return tbName.Text; } private set { tbName.Text = value; lName.Visibility = string.IsNullOrEmpty(value) ? Visibility.Hidden : Visibility.Visible; } } private void UpdateCardDisplay() { ttcCard.Card = null; if (Card != null) { if (Card is DominionBase.Cards.Universal.Blank) { lName.Background = Brushes.BlueViolet; lName.MinWidth = 10; Title = " "; tbName.Text = string.Empty; ttcCard.Card = null; return; } if (CardVisibility == DominionBase.Piles.Visibility.All) { var category = Card.Category; var card = Card as Card; if (card != null) category = card.PhysicalCategory; lName.Background = Caching.BrushRepository.GetBackgroundBrush(category); lName.Foreground = Caching.BrushRepository.GetForegroundBrush(category); // Flip the foreground/background for Events if (category.HasFlag(Categories.Event)) { var t = lName.Foreground; lName.Foreground = lName.Background; lName.Background = t; } if (category.HasFlag(Categories.Reaction)) tbName.Effect = Caching.DropShadowRepository.GetDSE(8, Colors.White, 1d); Title = Card.Name; ttcCard.Card = Card; } else { lName.Background = Brushes.Transparent; var repo = Caching.ImageRepository.Acquire(); if (repo != null && imName != null) { switch (Card.CardBack) { case CardBack.Standard: imName.Source = repo.GetBitmapImage("back", "small"); break; case CardBack.Red: imName.Source = repo.GetBitmapImage("back_red", "small"); break; } } Caching.ImageRepository.Release(); if (imName != null) { imName.Width = lName.MinWidth; imName.Height = 12; } } } } private void cbSelected_Checked(object sender, RoutedEventArgs e) { var toggleButton = sender as ToggleButton; if (toggleButton != null) toggleButton.IsEnabled = false; RaiseEvent(new RoutedEventArgs(ICardCheckBoxCheckedEvent)); if (toggleButton != null) toggleButton.IsEnabled = true; } private void UserControl_MouseDown(object sender, MouseButtonEventArgs e) { if (WMain.Settings != null && ((ToolTip as ToolTip)?.Content as ToolTipIDisplayable)?.Card != null && (((ToolTip) ToolTip).Content as ToolTipIDisplayable)?.Card.Type != DominionBase.Cards.Universal.TypeClass.Dummy && (((ToolTip) ToolTip).Content as ToolTipIDisplayable)?.Card.Type != DominionBase.Cards.Universal.TypeClass.DummyRed && (((ToolTip) ToolTip).Content as ToolTipIDisplayable)?.Card.Type != DominionBase.Cards.Universal.TypeClass.Blank && WMain.Settings.ShowToolTipOnRightClick && e.ChangedButton == MouseButton.Right && e.ButtonState == MouseButtonState.Pressed) { CaptureMouse(); ((ToolTip) ToolTip).IsOpen = true; } } private void UserControl_MouseUp(object sender, MouseButtonEventArgs e) { if (WMain.Settings != null && ToolTip != null && WMain.Settings.ShowToolTipOnRightClick && e.ChangedButton == MouseButton.Right && e.ButtonState == MouseButtonState.Released) { ReleaseMouseCapture(); var toolTip = ToolTip as ToolTip; if (toolTip != null) toolTip.IsOpen = false; } } private void UserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { var toolTip = ToolTip as ToolTip; if (toolTip != null && toolTip.IsOpen) ((ToolTip) ToolTip).IsOpen = false; } } }