Bind host and join to NetworkManager via signals
This commit is contained in:
parent
947b606b44
commit
ecaa5537a2
@ -2,8 +2,8 @@ use std::net::SocketAddr;
|
||||
|
||||
use godot::prelude::*;
|
||||
|
||||
mod network_manager;
|
||||
mod ui;
|
||||
pub mod network_manager;
|
||||
pub mod ui;
|
||||
|
||||
struct MyExtension;
|
||||
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use godot::prelude::*;
|
||||
use godot::{prelude::*, tools::get_autoload_by_name};
|
||||
|
||||
use crate::ui::cli::{CLI_GLOBAL_NAME, CliColor, CommandLinePanel};
|
||||
|
||||
#[derive(GodotClass)]
|
||||
#[class(base=Node)]
|
||||
@ -15,14 +17,31 @@ impl INode for NetworkManager {
|
||||
fn init(base: Base<Node>) -> Self {
|
||||
NetworkManager { base }
|
||||
}
|
||||
|
||||
fn ready(&mut self) {
|
||||
let mut cli = get_autoload_by_name::<CommandLinePanel>(CLI_GLOBAL_NAME);
|
||||
|
||||
cli.bind_mut()
|
||||
.signals()
|
||||
.on_host()
|
||||
.connect_other(self, |s, port| s.host(port));
|
||||
cli.bind_mut()
|
||||
.signals()
|
||||
.on_join()
|
||||
.connect_other(self, |s, addr| s.join(addr.parse().unwrap()));
|
||||
}
|
||||
}
|
||||
|
||||
impl NetworkManager {
|
||||
pub fn host(&mut self, port: u32) {
|
||||
godot_print!("Hosting server at {}", port);
|
||||
get_autoload_by_name::<CommandLinePanel>(CLI_GLOBAL_NAME)
|
||||
.bind_mut()
|
||||
.publish_message(format!("Hosting server at {}", port), CliColor::Info);
|
||||
}
|
||||
|
||||
pub fn join(&mut self, addr: SocketAddr) {
|
||||
godot_print!("Joining server at {:?}", addr);
|
||||
get_autoload_by_name::<CommandLinePanel>(CLI_GLOBAL_NAME)
|
||||
.bind_mut()
|
||||
.publish_message(format!("Joining server at {}", addr), CliColor::Info);
|
||||
}
|
||||
}
|
||||
|
||||
@ -27,9 +27,11 @@ impl CliColor {
|
||||
}
|
||||
}
|
||||
|
||||
pub const CLI_GLOBAL_NAME: &str = "CommandLinePanelGlobal";
|
||||
|
||||
#[derive(GodotClass)]
|
||||
#[class(base=Panel)]
|
||||
struct CommandLinePanel {
|
||||
pub struct CommandLinePanel {
|
||||
#[export]
|
||||
pub input_field: Option<Gd<TextEdit>>,
|
||||
#[export]
|
||||
@ -80,7 +82,13 @@ impl IPanel for CommandLinePanel {
|
||||
}
|
||||
}
|
||||
|
||||
#[godot_api]
|
||||
impl CommandLinePanel {
|
||||
#[signal]
|
||||
pub fn on_host(port: u32);
|
||||
#[signal]
|
||||
pub fn on_join(port: String);
|
||||
|
||||
pub fn input_command(&mut self, command: String) {
|
||||
self.publish_message(format!("> {}", command), CliColor::Command);
|
||||
if let Some(input) = &mut self.input_field {
|
||||
@ -150,10 +158,7 @@ impl Command for HostCommand {
|
||||
fn execute(&self, cli: &mut CommandLinePanel, params: &[&str]) {
|
||||
if let Some(port_str) = params.first() {
|
||||
if let Ok(port) = u32::from_str_radix(*port_str, 10) {
|
||||
cli.publish_message(format!("Server hosted at {}", port), CliColor::Info);
|
||||
get_autoload_by_name::<NetworkManager>(NETWORK_SINGLETON_NAME)
|
||||
.bind_mut()
|
||||
.host(port);
|
||||
cli.signals().on_host().emit(port);
|
||||
} else {
|
||||
cli.publish_message(format!("Invalid port: {}", port_str), CliColor::Error);
|
||||
}
|
||||
@ -172,11 +177,8 @@ struct JoinCommand;
|
||||
impl Command for JoinCommand {
|
||||
fn execute(&self, cli: &mut CommandLinePanel, params: &[&str]) {
|
||||
if let Some(address_str) = params.first() {
|
||||
if let Ok(address) = address_str.parse::<SocketAddr>() {
|
||||
cli.publish_message(format!("Joining server at {}", address), CliColor::Info);
|
||||
get_autoload_by_name::<NetworkManager>(NETWORK_SINGLETON_NAME)
|
||||
.bind_mut()
|
||||
.join(address);
|
||||
if let Ok(_) = address_str.parse::<SocketAddr>() {
|
||||
cli.signals().on_join().emit(address_str.to_string());
|
||||
} else {
|
||||
cli.publish_message(format!("Invalid address: {}", address_str), CliColor::Error);
|
||||
}
|
||||
|
||||
@ -1 +1 @@
|
||||
mod cli;
|
||||
pub mod cli;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user