using System; using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace Dominion.NET_WPF.Controls { /// /// Interaction logic for ucCardSettings.xaml /// public partial class ucCardSettings : UserControl, IDisposable { public static readonly DependencyProperty CardsSettingsProperty = DependencyProperty.Register("CardsSettings", typeof(DominionBase.Cards.CardsSettings), typeof(ucCardSettings), new PropertyMetadata(null)); public DominionBase.Cards.CardsSettings CardsSettings { get { return (DominionBase.Cards.CardsSettings)GetValue(CardsSettingsProperty); } set { SetValue(CardsSettingsProperty, value); lName.Content = value.Name; ttcCard.Card = DisplayObjects.Cards.FirstOrDefault(c => c.Name == value.Name); foreach (var cardSetting in value.CardSettingOrdered) { if (cardSetting.Type == typeof(bool)) { var spBoolean = new StackPanel { Orientation = Orientation.Horizontal, ToolTip = cardSetting.Hint }; icCardSetting.Items.Add(spBoolean); var cbBoolean = new CheckBox { Tag = cardSetting, VerticalAlignment = VerticalAlignment.Center, IsChecked = (bool) cardSetting.Value, Margin = new Thickness(3) }; cbBoolean.Checked += cbBoolean_Checked; cbBoolean.Unchecked += cbBoolean_Checked; spBoolean.Children.Add(cbBoolean); var lBoolean = new Label { VerticalAlignment = VerticalAlignment.Center, Content = cardSetting.Text, Padding = new Thickness(0) }; cbBoolean.Content = lBoolean; } else if (cardSetting.Type == typeof(int)) { var spInt = new StackPanel { Orientation = Orientation.Horizontal, ToolTip = cardSetting.Hint }; icCardSetting.Items.Add(spInt); var lInt = new Label { Padding = new Thickness(0), VerticalAlignment = VerticalAlignment.Center, Content = $"{cardSetting.Text}: " }; spInt.Children.Add(lInt); var tbInt = new TextBox { Tag = cardSetting, VerticalAlignment = VerticalAlignment.Center, Width = 50, HorizontalContentAlignment = HorizontalAlignment.Right, Text = cardSetting.Value.ToString() }; tbInt.TextChanged += tbInt_TextChanged; spInt.Children.Add(tbInt); } else if (cardSetting.Type == typeof(DominionBase.Cards.ConstraintCollection)) { var gbConstraint = new GroupBox { Header = cardSetting.Text, ToolTip = cardSetting.Hint, HorizontalAlignment = HorizontalAlignment.Stretch }; icCardSetting.Items.Add(gbConstraint); var svConstraint = new ScrollViewer {VerticalScrollBarVisibility = ScrollBarVisibility.Auto}; gbConstraint.Content = svConstraint; var ucccConstraint = new ucCardConstraints { ConstraintCollection = (DominionBase.Cards.ConstraintCollection) cardSetting.Value }; svConstraint.Content = ucccConstraint; } else { var spString = new StackPanel { Orientation = Orientation.Horizontal, ToolTip = cardSetting.Hint }; icCardSetting.Items.Add(spString); var lString = new Label { Padding = new Thickness(0), VerticalAlignment = VerticalAlignment.Center, Content = $"{cardSetting.Text}: " }; spString.Children.Add(lString); var tbString = new TextBox { Tag = cardSetting, VerticalAlignment = VerticalAlignment.Center, Text = (string) cardSetting.Value, HorizontalAlignment = HorizontalAlignment.Stretch }; tbString.TextChanged += tbString_TextChanged; spString.Children.Add(tbString); } } } } public static readonly DependencyProperty DisplayObjectsProperty = DependencyProperty.Register("DisplayObjects", typeof(DisplayObjects), typeof(ucCardSettings), new PropertyMetadata(new DisplayObjects())); public DisplayObjects DisplayObjects { get { return (DisplayObjects)GetValue(DisplayObjectsProperty); } set { SetValue(DisplayObjectsProperty, value); } } private void tbString_TextChanged(object sender, TextChangedEventArgs e) { var cardSetting = (sender as TextBox)?.Tag as DominionBase.Cards.CardSetting; if (cardSetting != null) cardSetting.Value = ((TextBox) sender).Text; } private void tbInt_TextChanged(object sender, TextChangedEventArgs e) { int value; int.TryParse((sender as TextBox)?.Text, out value); var cardSetting = (sender as TextBox)?.Tag as DominionBase.Cards.CardSetting; if (cardSetting != null) cardSetting.Value = value; } private void cbBoolean_Checked(object sender, RoutedEventArgs e) { var cardSetting = (sender as CheckBox)?.Tag as DominionBase.Cards.CardSetting; if (cardSetting != null) cardSetting.Value = ((CheckBox) sender).IsChecked; } public ucCardSettings() { 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) { if (settings.ToolTipShowDuration == ToolTipShowDuration.Off) ToolTipService.SetIsEnabled(lName, false); else { ToolTipService.SetIsEnabled(lName, true); ToolTipService.SetShowDuration(lName, (int)settings.ToolTipShowDuration); } } } private void lName_MouseDown(object sender, MouseButtonEventArgs e) { var fe = sender as FrameworkElement; if (WMain.Settings == null || ((fe?.ToolTip as ToolTip)?.Content as ToolTipIDisplayable)?.Card == null || (((ToolTip) fe.ToolTip).Content as ToolTipIDisplayable)?.Card.Type == DominionBase.Cards.Universal.TypeClass.Dummy || (((ToolTip) fe.ToolTip).Content as ToolTipIDisplayable)?.Card.Type == DominionBase.Cards.Universal.TypeClass.DummyRed || (((ToolTip) fe.ToolTip).Content as ToolTipIDisplayable)?.Card.Type == DominionBase.Cards.Universal.TypeClass.Blank) return; if (WMain.Settings.ShowToolTipOnRightClick && e.ChangedButton == MouseButton.Right && e.ButtonState == MouseButtonState.Pressed) { fe.CaptureMouse(); var toolTip = fe.ToolTip as ToolTip; if (toolTip != null) toolTip.IsOpen = true; } } private void lName_MouseUp(object sender, MouseButtonEventArgs e) { var fe = sender as FrameworkElement; if (WMain.Settings == null || fe?.ToolTip == null) return; if (WMain.Settings.ShowToolTipOnRightClick && e.ChangedButton == MouseButton.Right && e.ButtonState == MouseButtonState.Released) { fe.ReleaseMouseCapture(); var toolTip = fe.ToolTip as ToolTip; if (toolTip != null) toolTip.IsOpen = false; } } private void UserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { var toolTip = lName.ToolTip as ToolTip; if (toolTip != null && toolTip.IsOpen) ((ToolTip) lName.ToolTip).IsOpen = false; } } }