Skip to content

Commit

Permalink
Chat is now implemented with ItemsControl rather than simple textbloc…
Browse files Browse the repository at this point in the history
…k to allow styling messages
  • Loading branch information
Arcidev committed Aug 13, 2024
1 parent cca1e99 commit e8831ae
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Client.UI/Controls/AchievementControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<RowDefinition Height="70" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid >
<Grid>
<Grid.Style>
<Style TargetType="Grid">
<Style.Triggers>
Expand Down
9 changes: 9 additions & 0 deletions Client.UI/Enums/ChatMessageType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

namespace Client.UI.Enums
{
public enum ChatMessageType
{
Message = 0,
System
}
}
9 changes: 9 additions & 0 deletions Client.UI/ViewModels/MainGame/ChatMessageViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Client.UI.Enums;

namespace Client.UI.ViewModels.MainGame
{
public record ChatMessageViewModel(string Author, string Message, ChatMessageType Type = ChatMessageType.Message)
{
public bool ShowAuthor => Type == ChatMessageType.Message;
}
}
46 changes: 30 additions & 16 deletions Client.UI/ViewModels/MainGame/ChatWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using Client.UI.Controls;
using Client.UI.Resources;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Client.UI.ViewModels.MainGame
{
Expand Down Expand Up @@ -33,7 +35,6 @@ public class ChatWindowViewModel : NotifyPropertyViewModel
];

private readonly ChatViewModel parent;
private readonly StringBuilder text;
private string message;
private string picture;
private bool pendingMessages;
Expand All @@ -44,7 +45,7 @@ public class ChatWindowViewModel : NotifyPropertyViewModel

public AsyncCommandHandler HandleChatCommandCmd { get; }

public string Text => text.ToString();
public ObservableCollection<ChatMessageViewModel> Messages { get; } = [];

public bool PendingMessages
{
Expand Down Expand Up @@ -91,14 +92,15 @@ public ChatWindowViewModel(string name, ChatType chatType, ChatViewModel parent)
ChatType = chatType;
this.parent = parent;

text = new StringBuilder();
HandleChatCommandCmd = new (HandleChatCommand);
}

public void Write(string name, string msg)
{
text.Append(name).Append("> ").AppendLine(msg);
OnPropertyChanged(nameof(Text));
Application.Current.Dispatcher.Invoke(() =>
{
Messages.Add(new ChatMessageViewModel(name, msg));
});
}

private async Task HandleChatCommand()
Expand Down Expand Up @@ -155,8 +157,7 @@ private async Task HandleCommand(string command)
await HandleBlockCommand(command[commandDelimiter..].Trim(), false);
break;
case clear:
text.Clear();
OnPropertyChanged(nameof(Text));
Messages.Clear();
break;
default:
HandleInvalidCommand();
Expand All @@ -166,25 +167,36 @@ private async Task HandleCommand(string command)

private void ListCommands()
{
text.Append(Texts.PossibleCommands).AppendLine(":");
var commandsStr = new StringBuilder();
commandsStr.Append(Texts.PossibleCommands).Append(':');
foreach (var command in commands)
text.AppendLine(command);
{
commandsStr.AppendLine();
commandsStr.Append(command);
}

OnPropertyChanged(nameof(Text));
Application.Current.Dispatcher.Invoke(() =>
{
Messages.Add(new ChatMessageViewModel(string.Empty, commandsStr.ToString(), Enums.ChatMessageType.System));
});
}

private void HandleInvalidCommand()
{
text.AppendLine(Texts.InvalidCommand);
OnPropertyChanged(nameof(Text));
Application.Current.Dispatcher.Invoke(() =>
{
Messages.Add(new ChatMessageViewModel(string.Empty, Texts.InvalidCommand, Enums.ChatMessageType.System));
});
}

private async Task HandleWhisperCommand(string arg)
{
void FormatSyntaxError()
{
text.AppendLine(string.Format(Texts.InvalidSyntax, $"/{whisper} [name] [message]"));
OnPropertyChanged(nameof(Text));
Application.Current.Dispatcher.Invoke(() =>
{
Messages.Add(new ChatMessageViewModel(string.Empty, string.Format(Texts.InvalidSyntax, $"/{whisper} [name] [message]"), Enums.ChatMessageType.System));
});
}

int commandDelimiter = arg.IndexOf(' ');
Expand Down Expand Up @@ -216,8 +228,10 @@ private async Task HandleFriendCommand(string arg)

void FormatSyntaxError()
{
text.AppendLine(string.Format(Texts.InvalidSyntax, $"/{friend} [{addFriend}/{acceptFriend}/{denyFriend}/{removeFriend}] [name]"));
OnPropertyChanged(nameof(Text));
Application.Current.Dispatcher.Invoke(() =>
{
Messages.Add(new ChatMessageViewModel(string.Empty, string.Format(Texts.InvalidSyntax, $"/{friend} [{addFriend}/{acceptFriend}/{denyFriend}/{removeFriend}] [name]"), Enums.ChatMessageType.System));
});
}

int commandDelimiter = arg.IndexOf(' ');
Expand Down
24 changes: 23 additions & 1 deletion Client.UI/Views/Game/Chat.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:resx="clr-namespace:Client.UI.Resources"
xmlns:enums="clr-namespace:Client.UI.Enums"
xmlns:viewmodels="clr-namespace:Client.UI.ViewModels.MainGame"
mc:Ignorable="d"
Title="{x:Static resx:Texts.Chat}"
Expand Down Expand Up @@ -33,7 +34,28 @@
<RowDefinition />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Text, Mode=OneWay}" TextWrapping="Wrap" />
<ItemsControl ItemsSource="{Binding Messages}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel.Style>
<Style TargetType="StackPanel">
<Style.Triggers>
<DataTrigger Binding="{Binding Type}" Value="{x:Static enums:ChatMessageType.System}">
<Setter Property="TextElement.Foreground" Value="OrangeRed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
<StackPanel Orientation="Horizontal" Visibility="{Binding ShowAuthor, Converter={StaticResource BooleanToVisibilityConverter}}">
<TextBlock Text="{Binding Author, Mode=OneWay}" />
<TextBlock Text=">&#160;" />
</StackPanel>
<TextBlock Text="{Binding Message, Mode=OneWay}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition />
Expand Down

0 comments on commit e8831ae

Please sign in to comment.