[ADDITION] Added databinding to the chatbox function.
Tried to broadcast the message, doesn't work yet.
This commit is contained in:
@@ -65,7 +65,6 @@ namespace Client
|
||||
}
|
||||
|
||||
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete), null);
|
||||
|
||||
}
|
||||
|
||||
private void handleData(byte[] message)
|
||||
@@ -84,16 +83,18 @@ namespace Client
|
||||
string textUsername = combo.Item1;
|
||||
string textMsg = combo.Item2;
|
||||
//TODO display username and message in chat window
|
||||
|
||||
Debug.WriteLine("Message username: {0}\t Message: {1}", textUsername, textMsg);
|
||||
break;
|
||||
|
||||
case JSONConvert.LOBBY:
|
||||
// lobby data
|
||||
//TODO fill lobby with the data received
|
||||
break;
|
||||
|
||||
case JSONConvert.CANVAS:
|
||||
// canvas data
|
||||
break;
|
||||
|
||||
default:
|
||||
Debug.WriteLine("[CLIENT] Received weird identifier: " + id);
|
||||
break;
|
||||
|
||||
@@ -1,10 +1,69 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using GalaSoft.MvvmLight.Command;
|
||||
using SharedClientServer;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace Client.ViewModels
|
||||
{
|
||||
class ViewModelGame : INotifyPropertyChanged
|
||||
{
|
||||
ClientData data = ClientData.Instance;
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
public ObservableCollection<string> Messages { get; } = new ObservableCollection<string>();
|
||||
|
||||
private dynamic _payload;
|
||||
|
||||
private string _username;
|
||||
|
||||
private string _message;
|
||||
public string Message
|
||||
{
|
||||
get
|
||||
{
|
||||
return _message;
|
||||
}
|
||||
set
|
||||
{
|
||||
_message = value;
|
||||
}
|
||||
}
|
||||
public ICommand OnKeyDown { get; set; }
|
||||
|
||||
public ViewModelGame()
|
||||
{
|
||||
if (_payload == null)
|
||||
{
|
||||
_message = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
_message = _payload.message;
|
||||
_username = _payload.username;
|
||||
}
|
||||
OnKeyDown = new RelayCommand(ChatBox_KeyDown);
|
||||
}
|
||||
|
||||
private void ChatBox_KeyDown()
|
||||
{
|
||||
//if enter then clear textbox and send message.
|
||||
if (Message != string.Empty) AddMessage(Message);
|
||||
Message = string.Empty;
|
||||
}
|
||||
|
||||
internal void AddMessage(string message)
|
||||
{
|
||||
Messages.Add($"{data.User.Username}: {message}");
|
||||
|
||||
_payload = new
|
||||
{
|
||||
username = data.User.Username,
|
||||
message = message
|
||||
};
|
||||
|
||||
//Broadcast the message after adding it to the list!
|
||||
data.Client.SendMessage(JSONConvert.GetMessageToSend(JSONConvert.MESSAGE, _payload));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,8 +48,13 @@
|
||||
</Border>
|
||||
|
||||
<Grid Grid.Column="2" Grid.Row="1">
|
||||
<TextBox Name="SentMessage" IsReadOnly="True" Margin="0,0,0,50"/>
|
||||
<TextBox x:Name="ChatBox" Keyboard.KeyDown="ChatBox_KeyDown" Width="200" Height="50" ToolTip="Message goes here" ToolTipService.IsEnabled="True" VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
|
||||
<ListBox Name ="TextBox" ItemsSource="{Binding Path=Messages}" Margin="0,0,0,69"/>
|
||||
|
||||
<TextBox Name="ChatBox" Text="{Binding Message, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,465,0,0">
|
||||
<TextBox.InputBindings>
|
||||
<KeyBinding Key="Return" Command="{Binding OnKeyDown}"/>
|
||||
</TextBox.InputBindings>
|
||||
</TextBox>
|
||||
</Grid>
|
||||
|
||||
</Grid>
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace Client.Views
|
||||
/// </summary>
|
||||
public partial class GameWindow : Window
|
||||
{
|
||||
ClientData data = ClientData.Instance;
|
||||
|
||||
public GameWindow()
|
||||
{
|
||||
DataContext = new ViewModelGame();
|
||||
@@ -82,7 +82,6 @@ namespace Client.Views
|
||||
// var deepCopy = System.Windows.Markup.XamlReader.Parse(xaml) as UIElement;
|
||||
// TEST.Children.Add(deepCopy);
|
||||
//}
|
||||
|
||||
}
|
||||
|
||||
private void ClrPcker_Background_SelectedColorChanged_1(object sender, RoutedPropertyChangedEventArgs<Color?> e)
|
||||
@@ -95,23 +94,14 @@ namespace Client.Views
|
||||
color = colorSelected;
|
||||
}
|
||||
|
||||
private void ChatBox_KeyDown(object sender, KeyEventArgs e)
|
||||
{
|
||||
//if enter then clear textbox and send message.
|
||||
if (e.Key.Equals(Key.Enter))
|
||||
{
|
||||
WriteToChat(ChatBox.Text);
|
||||
ChatBox.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Writes the current client's message to the chatbox.
|
||||
*/
|
||||
private void WriteToChat(string message)
|
||||
{
|
||||
string user = data.User.Username;
|
||||
SentMessage.AppendText($"{user}: {message}\n");
|
||||
}
|
||||
///*
|
||||
// * Writes the current client's message to the chatbox.
|
||||
// */
|
||||
//private void WriteToChat(string message)
|
||||
//{
|
||||
// string user = data.User.Username;
|
||||
// SentMessage.AppendText($"{user}: {message}\n");
|
||||
// data.User.Message = message;
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,6 @@ namespace Client
|
||||
|
||||
private void Button_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
|
||||
Lobby lobbySelected = LobbyList.SelectedItem as Lobby;
|
||||
if(lobbySelected != null)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user