Skip to content

Commit

Permalink
Support color coded names in party view (#135)
Browse files Browse the repository at this point in the history
* Simplify colored text binding using extension
Support color coded names in party view

* Remove commented out code
  • Loading branch information
tobibodamer authored Oct 19, 2024
1 parent fe65296 commit 38c2666
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 40 deletions.
12 changes: 6 additions & 6 deletions H2MLauncher.UI/Converters/HostNameColorConverter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
Expand All @@ -18,6 +19,8 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
return Array.Empty<Run>();
}

Style? runStyle = parameter as Style;

List<Run> runs = [];
MatchCollection matches = ColorRegex().Matches(hostname);
if (matches.Count != 0)
Expand All @@ -28,8 +31,7 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
{
Text = hostname[..matches[0].Index],
Foreground = Brushes.White,
FlowDirection = System.Windows.FlowDirection.LeftToRight,
FontFamily = Font,
Style = runStyle
});
}
foreach (Match match in matches)
Expand All @@ -52,8 +54,7 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
{
Text = match.Groups[2].Value,
Foreground = brush,
FlowDirection = System.Windows.FlowDirection.LeftToRight,
FontFamily = Font,
Style = runStyle
});
}
}
Expand All @@ -63,8 +64,7 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
{
Text = hostname,
Foreground = Brushes.White,
FlowDirection = System.Windows.FlowDirection.LeftToRight,
FontFamily = Font
Style = runStyle
});
}
return runs.ToArray();
Expand Down
34 changes: 14 additions & 20 deletions H2MLauncher.UI/Dialog/Views/QueueDialogView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:H2MLauncher.UI"
xmlns:converters="clr-namespace:H2MLauncher.UI.Converters"
xmlns:vm="clr-namespace:H2MLauncher.UI.ViewModels"
xmlns:model="clr-namespace:H2MLauncher.Core.Matchmaking.Models;assembly=H2MLauncher.Core"
Expand Down Expand Up @@ -77,26 +78,19 @@
HorizontalAlignment="Center" ClipToBounds="True" Height="25"
Visibility="{Binding CanEnterMatchmaking, Converter={StaticResource InverseBoolToVisibilityConverter}}">
<TextBlock x:Name="serverName" SizeChanged="ServerNameTextBox_SizeChanged" Canvas.Left="0"
TextWrapping="NoWrap" HorizontalAlignment="Center">
<TextBlock.Inlines>
<ItemsControl>
<ItemsControl.Style>
<Style TargetType="ItemsControl">
<Setter Property="ItemsSource" Value="{Binding ServerHostName, Converter={StaticResource HostNameColorConverter}}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding State}" Value="{x:Static model:PlayerState.Matchmaking}">
<Setter Property="ItemsSource" Value="{Binding PlaylistName, Converter={StaticResource HostNameColorConverter}}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ItemsControl.Style>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</TextBlock.Inlines>
TextWrapping="NoWrap" HorizontalAlignment="Center" FontFamily="Consolas">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="local:TextBlockExtensions.BindableInlines"
Value="{Binding ServerHostName, Converter={StaticResource HostNameColorConverter}}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding State}" Value="{x:Static model:PlayerState.Matchmaking}">
<Setter Property="local:TextBlockExtensions.BindableInlines"
Value="{Binding PlaylistName, Converter={StaticResource HostNameColorConverter}}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
<TextBlock.RenderTransform>
<TranslateTransform X="0"/>
</TextBlock.RenderTransform>
Expand Down
18 changes: 6 additions & 12 deletions H2MLauncher.UI/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -362,18 +362,12 @@
</Style>
</Image.Style>
</Image>
<ItemsControl
Focusable="False"
IsTabStop="False"
IsHitTestVisible="False"
ItemsSource="{Binding HostName, Converter={StaticResource HostNameColorConverter}}"
FlowDirection="LeftToRight">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
<TextBlock local:TextBlockExtensions.BindableInlines="{
Binding HostName,
Converter={StaticResource HostNameColorConverter}
}"
IsHitTestVisible="False"
Focusable="False"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
Expand Down
38 changes: 38 additions & 0 deletions H2MLauncher.UI/View/Behaviors/TextBlockExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

using H2MLauncher.UI.Converters;

namespace H2MLauncher.UI
{
public static class TextBlockExtensions
{
public static IEnumerable<Inline> GetBindableInlines(DependencyObject obj)
{
return (IEnumerable<Inline>)obj.GetValue(BindableInlinesProperty);
}

public static void SetBindableInlines(DependencyObject obj, IEnumerable<Inline> value)
{
obj.SetValue(BindableInlinesProperty, value);
}

public static readonly DependencyProperty BindableInlinesProperty =
DependencyProperty.RegisterAttached("BindableInlines", typeof(IEnumerable<Inline>), typeof(TextBlockExtensions), new PropertyMetadata(null, OnBindableInlinesChanged));

private static void OnBindableInlinesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is not TextBlock target)
{
return;
}

if (target != null)
{
target.Inlines.Clear();
target.Inlines.AddRange(((System.Collections.IEnumerable)e.NewValue));
}
}
}
}
6 changes: 4 additions & 2 deletions H2MLauncher.UI/View/Controls/PartyControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ui="clr-namespace:H2MLauncher.UI"
xmlns:local="clr-namespace:H2MLauncher.UI.View.Controls"
xmlns:vm="clr-namespace:H2MLauncher.UI.ViewModels"
MinWidth="150"
Expand Down Expand Up @@ -31,8 +32,9 @@
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}" Foreground="White" FontSize="15"
VerticalAlignment="Center" Margin="0,0,0,0">
<TextBlock ui:TextBlockExtensions.BindableInlines="{Binding Name, Converter={StaticResource HostNameColorConverter}}"
Foreground="White" FontSize="15"
VerticalAlignment="Center" Margin="0,0,0,0">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
Expand Down

0 comments on commit 38c2666

Please sign in to comment.