Skip to content

Commit

Permalink
migrate load window
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Feb 14, 2024
1 parent 625d176 commit a364bee
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 109 deletions.
104 changes: 0 additions & 104 deletions native_app/src/gui/windows/load.rs

This file was deleted.

2 changes: 0 additions & 2 deletions native_app/src/gui/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::uiworld::UiWorld;
use simulation::Simulation;

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

Expand Down Expand Up @@ -54,7 +53,6 @@ impl Default for OldGUIWindows {
s.insert("Debug", debug::debug, false);
#[cfg(feature = "multiplayer")]
s.insert("Network", network::network, false);
s.insert("Load", load::load, false);
s
}
}
Expand Down
7 changes: 4 additions & 3 deletions native_app/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ use crate::newgui::specialbuilding::SpecialBuildingResource;
use crate::newgui::terraforming::TerraformingResource;
use crate::newgui::toolbox::building::BuildingIcons;
use crate::newgui::windows::economy::EconomyState;
use crate::newgui::windows::load::LoadState;
use crate::newgui::windows::settings::{Settings, SettingsState};
use crate::newgui::zoneedit::ZoneEditState;
use crate::newgui::{
ErrorTooltip, ExitState, GuiState, InspectedBuilding, InspectedEntity, PotentialCommands,
TimeAlways, Tool,
};
use crate::rendering::immediate::{ImmediateDraw, ImmediateSound};
use crate::uiworld::{ReceivedCommands, UiWorld};
use crate::uiworld::{ReceivedCommands, SaveLoadState, UiWorld};
use common::saveload::Encoder;
use serde::de::DeserializeOwned;
use serde::Serialize;
Expand Down Expand Up @@ -61,8 +62,8 @@ pub fn init() {
register_resource_noserialize::<Timings>();
register_resource_noserialize::<Tool>();
register_resource_noserialize::<WorldCommands>();
register_resource_noserialize::<crate::gui::windows::load::LoadState>();
register_resource_noserialize::<crate::uiworld::SaveLoadState>();
register_resource_noserialize::<LoadState>();
register_resource_noserialize::<SaveLoadState>();
register_resource_noserialize::<EconomyState>();
register_resource_noserialize::<SettingsState>();
register_resource_noserialize::<BuildingIcons>();
Expand Down
125 changes: 125 additions & 0 deletions native_app/src/newgui/hud/windows/load.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#![allow(unused)]
use crate::uiworld::{SaveLoadState, UiWorld};
use egui::{Color32, DroppedFile, Widget};
use goryak::{
button_primary, error, minrow, on_primary, on_secondary_container, primary, textc, ProgressBar,
Window,
};
use simulation::utils::scheduler::SeqSchedule;
use simulation::Simulation;
use std::path::PathBuf;
use yakui::widgets::Pad;
use yakui::{Color, Vec2};

pub struct LoadState {
curpath: Option<PathBuf>,
load_fail: String,
has_save: bool,
}

impl Default for LoadState {
fn default() -> Self {
Self {
curpath: None,
load_fail: String::new(),
has_save: std::fs::metadata("world/world_replay.json").is_ok(),
}
}
}

/// Load window
/// Allows to load a replay from disk and play it
pub fn load(uiw: &UiWorld, _: &Simulation, opened: &mut bool) {
Window {
title: "Load".into(),
pad: Pad::all(10.0),
radius: 10.0,
opened,
child_spacing: 10.0,
}
.show(|| {
let mut state = uiw.write::<LoadState>();

if button_primary("New Game").show().clicked {
uiw.write::<SaveLoadState>().please_load_sim = Some(Simulation::new(true));
}

if state.has_save {
if button_primary("Load world/world_replay.json")
.show()
.clicked
{
let replay = Simulation::load_replay_from_disk("world");

if let Some(replay) = replay {
let (mut sim, mut loader) = Simulation::from_replay(replay);
let mut s = SeqSchedule::default();
loader.advance_tick(&mut sim, &mut s); // advance by one tick to get the initial state (like map size info)

uiw.write::<SaveLoadState>().please_load = Some(loader);
uiw.write::<SaveLoadState>().please_load_sim = Some(sim);
} else {
state.load_fail = "Failed to load replay".to_string();
}
}
} else {
textc(
on_secondary_container(),
"No replay found in world/world_replay.json",
);
}

if let Some(ref mut loading) = uiw.write::<SaveLoadState>().please_load {
let ticks_done = loading.pastt.0;
let ticks_total = loading.replay.commands.last().map(|c| c.0 .0).unwrap_or(0);
ProgressBar {
value: ticks_done as f32 / ticks_total as f32,
size: Vec2::new(400.0, 25.0),
color: primary().adjust(0.7),
}
.show_children(|| {
textc(
on_secondary_container(),
format!("Loading replay: {ticks_done}/{ticks_total}"),
);
});

minrow(5.0, || {
if button_primary("||").show().clicked {
loading.speed = 0;
}

if button_primary(">").show().clicked {
loading.speed = 1;
}
if button_primary(">>>").show().clicked {
loading.speed = 100;
}
if button_primary("max").show().clicked {
loading.speed = 10000;
}

if button_primary("1").show().clicked {
loading.speed = 0;
loading.advance_n_ticks = 1;
}
if button_primary("10").show().clicked {
loading.speed = 0;
loading.advance_n_ticks = 10;
}
if button_primary("100").show().clicked {
loading.speed = 0;
loading.advance_n_ticks = 100;
}
if button_primary("1000").show().clicked {
loading.speed = 0;
loading.advance_n_ticks = 1000;
}
});
}

if !state.load_fail.is_empty() {
textc(error(), state.load_fail.clone());
}
});
}
7 changes: 7 additions & 0 deletions native_app/src/newgui/hud/windows/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod economy;
pub mod load;
pub mod settings;

use crate::inputmap::{InputAction, InputMap};
Expand All @@ -10,6 +11,7 @@ use simulation::Simulation;
pub struct GUIWindows {
economy_open: bool,
settings_open: bool,
load_open: bool,
}

impl GUIWindows {
Expand All @@ -21,6 +23,10 @@ impl GUIWindows {
if button_primary("Settings").show().clicked {
self.settings_open ^= true;
}

if button_primary("Load").show().clicked {
self.load_open ^= true;
}
}

pub fn render(&mut self, uiworld: &UiWorld, sim: &Simulation) {
Expand All @@ -35,5 +41,6 @@ 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);
}
}

0 comments on commit a364bee

Please sign in to comment.