Skip to content

Commit

Permalink
migrate network to newgui
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Feb 14, 2024
1 parent a364bee commit 386a59a
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 130 deletions.
4 changes: 0 additions & 4 deletions native_app/src/gui/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use crate::uiworld::UiWorld;
use simulation::Simulation;

pub mod debug;
#[cfg(feature = "multiplayer")]
pub mod network;

pub trait GUIWindow: Send + Sync {
fn render_window(
Expand Down Expand Up @@ -51,8 +49,6 @@ impl Default for OldGUIWindows {
opened: vec![],
};
s.insert("Debug", debug::debug, false);
#[cfg(feature = "multiplayer")]
s.insert("Network", network::network, false);
s
}
}
Expand Down
124 changes: 0 additions & 124 deletions native_app/src/gui/windows/network.rs

This file was deleted.

2 changes: 1 addition & 1 deletion native_app/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn init() {
simulation::init::init();
register_resource::<Settings>("settings");
#[cfg(feature = "multiplayer")]
register_resource::<crate::gui::windows::network::NetworkConnectionInfo>("netinfo");
register_resource::<crate::newgui::windows::network::NetworkConnectionInfo>("netinfo");
register_resource::<LotBrushResource>("lot_brush");
register_resource::<Bindings>("bindings");

Expand Down
2 changes: 1 addition & 1 deletion native_app/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ fn handle_replay(
#[cfg(feature = "multiplayer")]
mod inner {
use crate::game_loop::{State, Timings, VERSION};
use crate::gui::windows::network::NetworkConnectionInfo;
use crate::network::handle_replay;
use crate::newgui::windows::network::NetworkConnectionInfo;
use crate::uiworld::{ReceivedCommands, SaveLoadState};
use common::timestep::Timestep;
use networking::{
Expand Down
13 changes: 13 additions & 0 deletions native_app/src/newgui/hud/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ use crate::uiworld::UiWorld;
use goryak::button_primary;
use simulation::Simulation;

#[cfg(feature = "multiplayer")]
pub mod network;

#[derive(Default)]
pub struct GUIWindows {
economy_open: bool,
settings_open: bool,
load_open: bool,
#[cfg(feature = "multiplayer")]
network_open: bool,
}

impl GUIWindows {
Expand All @@ -27,6 +32,11 @@ impl GUIWindows {
if button_primary("Load").show().clicked {
self.load_open ^= true;
}

#[cfg(feature = "multiplayer")]
if button_primary("Network").show().clicked {
self.network_open ^= true;
}
}

pub fn render(&mut self, uiworld: &UiWorld, sim: &Simulation) {
Expand All @@ -42,5 +52,8 @@ impl GUIWindows {
economy::economy(uiworld, sim, &mut self.economy_open);
settings::settings(uiworld, sim, &mut self.settings_open);
load::load(uiworld, sim, &mut self.load_open);

#[cfg(feature = "multiplayer")]
network::network(uiworld, sim, &mut self.network_open);
}
}
129 changes: 129 additions & 0 deletions native_app/src/newgui/hud/windows/network.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
use std::borrow::Cow;
use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};
use yakui::widgets::{Pad, TextBox};
use yakui::{constrained, divider, Color, Constraints, Vec2};

use common::saveload::Encoder;
use goryak::{
button_primary, checkbox_value, error, on_secondary_container, outline, textc, Window,
};
use simulation::Simulation;

use crate::network::NetworkState;
use crate::uiworld::UiWorld;

#[derive(Default, Serialize, Deserialize)]
pub struct NetworkConnectionInfo {
pub name: String,
pub ip: String,
#[serde(skip)]
pub error: String,
#[serde(skip)]
show_hashes: bool,
#[serde(skip)]
hashes: BTreeMap<String, u64>,
#[serde(skip)]
hashes_tick: u64,
}

fn label(x: impl Into<Cow<'static, str>>) {
textc(on_secondary_container(), x);
}

fn text_edit(x: &mut String, placeholder: &str) {
constrained(
Constraints {
min: Vec2::new(100.0, 20.0),
max: Vec2::new(f32::INFINITY, f32::INFINITY),
},
|| {
let mut text = TextBox::new(x.clone());
text.placeholder = placeholder.to_string();
text.fill = Some(Color::rgba(0, 0, 0, 50));
if let Some(changed) = text.show().into_inner().text {
*x = changed;
}
},
);
}

/// Network window
/// Allows to connect to a server or start a server
pub fn network(uiworld: &UiWorld, sim: &Simulation, opened: &mut bool) {
Window {
title: "Network".into(),
opened,
pad: Pad::all(10.0),
radius: 10.0,
child_spacing: 10.0,
}
.show(|| {
let mut state = uiworld.write::<NetworkState>();
let mut info = uiworld.write::<NetworkConnectionInfo>();
common::saveload::JSONPretty::save_silent(&*info, "netinfo");

match *state {
NetworkState::Singleplayer(_) => {
if !info.error.is_empty() {
textc(error(), info.error.clone());
divider(outline(), 5.0, 1.0);
}

text_edit(&mut info.name, "Name");

if info.name.is_empty() {
label("please enter your name");
return;
}

if button_primary("Start server").show().clicked {
if let Some(server) = crate::network::start_server(&mut info, sim) {
*state = NetworkState::Server(server);
}
}

divider(outline(), 5.0, 1.0);

text_edit(&mut info.ip, "IP");

if button_primary("Connect").show().clicked {
if let Some(c) = crate::network::start_client(&mut info) {
*state = NetworkState::Client(c);
}
}
}
NetworkState::Client(ref client) => {
label(client.lock().unwrap().describe());
show_hashes(sim, &mut info);
}
NetworkState::Server(ref server) => {
label("Running server");
label(server.lock().unwrap().describe());
show_hashes(sim, &mut info);
}
}
});
}

fn show_hashes(sim: &Simulation, info: &mut NetworkConnectionInfo) {
checkbox_value(
&mut info.show_hashes,
on_secondary_container(),
"show hashes",
);
if !info.show_hashes {
return;
}

if sim.get_tick() % 100 == 0 || info.hashes.is_empty() {
info.hashes = sim.hashes();
info.hashes_tick = sim.get_tick();
}

label(format!("hashes for tick {}", info.hashes_tick));
for (name, hash) in &info.hashes {
label(format!("{name}: {hash}"));
}
}

0 comments on commit 386a59a

Please sign in to comment.