feat: add chat window and sending messages

This commit is contained in:
2026-08-22 16:27:12 +02:00
parent 604666b471
commit 16056d0eb5

View File

@@ -1,11 +1,15 @@
use tokio::sync::mpsc::{self, Sender}; use tokio::sync::mpsc::{self, Sender};
use log::{error, info}; use log::{debug, error, info};
use macroquad::{ use macroquad::{
color::GRAY, color::{BLACK, GRAY, WHITE},
input::{KeyCode, get_keys_pressed},
math::{Rect, Vec2, vec2}, math::{Rect, Vec2, vec2},
shapes::draw_rectangle, shapes::{draw_rectangle, draw_rectangle_lines},
ui::{Ui, hash, root_ui, widgets::Checkbox}, ui::{
Ui, hash, root_ui,
widgets::{Checkbox, InputText},
},
window::{screen_height, screen_width}, window::{screen_height, screen_width},
}; };
use tokio::sync::mpsc::{Receiver, error::TryRecvError::Disconnected}; use tokio::sync::mpsc::{Receiver, error::TryRecvError::Disconnected};
@@ -35,8 +39,7 @@ pub struct GameClient {
game_padding: f32, game_padding: f32,
lobby: Lobby, lobby: Lobby,
chat: Chat,
chat_area: Rect,
} }
#[derive(Default)] #[derive(Default)]
@@ -46,6 +49,18 @@ struct Lobby {
checkbox_size: Vec2, checkbox_size: Vec2,
} }
struct Message {
sender: String,
content: String,
}
#[derive(Default)]
struct Chat {
chat_area: Rect,
messages: Vec<Message>,
message_buffer: String,
}
enum GuiCommands { enum GuiCommands {
Send(String), Send(String),
@@ -109,12 +124,7 @@ impl GameClient {
}; };
let game_padding = 25.0; let game_padding = 25.0;
let chat_area = Rect { let chat = Chat::default();
x: game_area.w,
y: 0.0,
w: screen_width() * 0.2,
h: screen_height(),
};
Some(Self { Some(Self {
cmd_sender, cmd_sender,
@@ -132,7 +142,7 @@ impl GameClient {
game_area, game_area,
game_padding, game_padding,
chat_area, chat,
}) })
} }
@@ -150,7 +160,7 @@ impl GameClient {
self.draw_summary_screen(); self.draw_summary_screen();
} }
} }
//self.draw_chat(); // TODO! uncomment this self.draw_chat();
} }
fn draw_lobby(&mut self) { fn draw_lobby(&mut self) {
@@ -192,8 +202,33 @@ impl GameClient {
todo!(); todo!();
} }
fn draw_chat(&self) { fn draw_chat(&mut self) {
todo!(); draw_rectangle(
self.chat.chat_area.x,
self.chat.chat_area.y,
self.chat.chat_area.w,
self.chat.chat_area.h,
WHITE,
);
let input_size = vec2(self.chat.chat_area.w, self.chat.chat_area.h * 0.1);
let input_pos = vec2(self.chat.chat_area.x, self.chat.chat_area.h - input_size.y);
InputText::new(hash!())
.position(input_pos)
.size(input_size)
.ui(&mut root_ui(), &mut self.chat.message_buffer);
/*
draw_rectangle_lines(
input_pos.x,
input_pos.y,
input_size.x,
input_size.y,
1.5,
BLACK,
);
*/
} }
/* === UPDATING STUFF ==== */ /* === UPDATING STUFF ==== */
@@ -207,6 +242,21 @@ impl GameClient {
ServerStage::Game => todo!(), ServerStage::Game => todo!(),
ServerStage::End => todo!(), ServerStage::End => todo!(),
} }
for key in get_keys_pressed() {
match key {
KeyCode::Enter => {
if !self.chat.message_buffer.is_empty() {
let _ = self // TODO! Check the message before sending
.cmd_sender
.try_send(GuiCommands::Send(self.chat.message_buffer.clone()));
self.chat.message_buffer.clear();
}
}
_ => {}
}
}
} }
fn update_network(&mut self) { fn update_network(&mut self) {
@@ -221,7 +271,11 @@ impl GameClient {
ServerMessage::AvailableMoves { moves } => todo!(), ServerMessage::AvailableMoves { moves } => todo!(),
ServerMessage::BroadcastBoardState { board } => todo!(), ServerMessage::BroadcastBoardState { board } => todo!(),
ServerMessage::BroadcastCurrentTurn { active_player } => todo!(), ServerMessage::BroadcastCurrentTurn { active_player } => todo!(),
ServerMessage::BroadCastTextMessage { sender, content } => todo!(), ServerMessage::BroadCastTextMessage { sender, content } => {
debug!("Got message from: {} with content: {}", sender, content);
self.chat.messages.push(Message { sender, content });
}
ServerMessage::GameEnd { result } => todo!(), ServerMessage::GameEnd { result } => todo!(),
_ => {} // ignore things that network client handled by itself _ => {} // ignore things that network client handled by itself
@@ -250,7 +304,7 @@ impl GameClient {
h: screen_height(), h: screen_height(),
}; };
self.chat_area = Rect { self.chat.chat_area = Rect {
x: self.game_area.w, x: self.game_area.w,
y: 0.0, y: 0.0,
w: screen_width() * 0.2, w: screen_width() * 0.2,