Skip to content

Commit

Permalink
Comment main.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
maddymakesgames committed Mar 22, 2024
1 parent a067ebf commit 1238f96
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
7 changes: 3 additions & 4 deletions gui/src/editor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::sync::Arc;

use celeste_rs::saves::{
everest::LevelSetStats,
ops::DeError,
util::{EntityID, FileTime},
SaveData,
};
Expand Down Expand Up @@ -46,7 +45,7 @@ pub struct EditorScreen {
}

impl EditorScreen {
pub fn new(file_name: String, save: SaveData) -> Result<EditorScreen, DeError> {
pub fn new(file_name: String, save: SaveData) -> EditorScreen {
let vanilla_level_set = LevelSetStats {
name: "Celeste".to_owned(),
areas: save.areas.clone(),
Expand All @@ -55,7 +54,7 @@ impl EditorScreen {
total_strawberries: save.total_strawberries,
};

Ok(EditorScreen {
EditorScreen {
file_name,
save,
safety_off: false,
Expand All @@ -66,7 +65,7 @@ impl EditorScreen {
session_add_strawb_buff: String::new(),
session_add_key_buf: String::new(),
session_add_dnl_buf: String::new(),
})
}
}

pub fn display(&mut self, ui: &mut Ui, rt: &Runtime, popups: &Arc<Mutex<Vec<PopupWindow>>>) {
Expand Down
34 changes: 18 additions & 16 deletions gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ use eframe::{
Color32,
Context,
FontFamily,
FontFamily::Proportional,
FontId,
RichText,
TextStyle::*,
Ui,
WidgetText,
Window,
Expand Down Expand Up @@ -75,6 +77,9 @@ struct SaveEditor {
impl SaveEditor {
fn new(cc: &CreationContext) -> SaveEditor {
// expects are fine since if this fails theres nothing we can do
// We create a multithreading runtime on native, and a single-threaded runtime on wasm.
// We don't *really* use the runtime on wasm but it'd be too annoying for it to not exist
// We also can rewrite the code easily to actually use the runtime so /shrug
#[cfg(not(target_family = "wasm"))]
let runtime = tokio::runtime::Runtime::new().expect("Error creating tokio runtime");
#[cfg(target_family = "wasm")]
Expand All @@ -83,10 +88,10 @@ impl SaveEditor {
.build()
.expect("Error creating tokio runtime");

let mut style = (*cc.egui_ctx.style()).clone();

use eframe::egui::{FontFamily::Proportional, TextStyle::*};
let mut style = (*cc.egui_ctx.style()).clone();

// Modify the text_styles to include our own font sizes
style.text_styles = [
(Heading, FontId::new(32.0, Proportional)),
(Name("header2".into()), FontId::new(26.0, Proportional)),
Expand All @@ -110,19 +115,21 @@ impl SaveEditor {

impl App for SaveEditor {
fn update(&mut self, ctx: &eframe::egui::Context, _frame: &mut eframe::Frame) {
// Show the main window contents
CentralPanel::default().show(ctx, |ui| {
self.screen.update(ui, &self.runtime, &self.popups)
});

let mut popup_guard = self.popups.blocking_lock();
let mut to_remove = None;
// Loop over any open popups and display them
for (i, popup) in popup_guard.iter().enumerate() {
match popup.show(ctx) {
PopupResult::ClosePopup => to_remove = Some(i),
PopupResult::Nothing => {}
PopupResult::CloseApp => {
// unwraps are safe cause window will always exist and I don't think reload can fail
#[cfg(target_family = "wasm")]
// unwraps are safe cause window will always exist and I don't think reload can fail
web_sys::window().unwrap().location().reload().unwrap();
#[cfg(not(target_family = "wasm"))]
{
Expand All @@ -133,6 +140,7 @@ impl App for SaveEditor {
}
}

// If the user closed a popup remove it from the list
if let Some(to_remove) = to_remove {
popup_guard.remove(to_remove);
}
Expand All @@ -149,23 +157,15 @@ enum ScreenState {
impl ScreenState {
fn update(&mut self, ui: &mut Ui, rt: &Runtime, popups: &Arc<Mutex<Vec<PopupWindow>>>) {
match self {
// Startup just immediately transitions to the main menu
ScreenState::Startup => *self = ScreenState::Menu(MainMenu::default()),
ScreenState::Menu(m) =>
// The main menu displays until a file has been opened
// In which case we transition to the editor
if let Some((file_name, save)) = m.display(ui, rt, popups) {
match EditorScreen::new(file_name, save) {
Ok(e) => *self = ScreenState::Editor(e),
Err(e) => {
let mut popups = popups.blocking_lock();
popups.push(PopupWindow::new(
ErrorSeverity::Error,
format!(
"Error reading savefile: {e:?}.\nMake sure this actually is a \
save file."
),
))
}
}
*self = ScreenState::Editor(EditorScreen::new(file_name, save))
},
// The editor (current) doesn't ever transition out
ScreenState::Editor(e) => {
e.display(ui, rt, popups);
}
Expand Down Expand Up @@ -200,7 +200,9 @@ fn celeste_save_dir() -> Option<PathBuf> {
}
}

/// An error popup window
struct PopupWindow {
/// The severity of the message
severity: ErrorSeverity,
/// The text displayed on the popup
text: String,
Expand Down

0 comments on commit 1238f96

Please sign in to comment.