generated from emilk/eframe_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
caed2dd
commit f1c973b
Showing
3 changed files
with
144 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use cavestory_save::{items::Inventory, strum::IntoEnumIterator}; | ||
use egui::Ui; | ||
|
||
const MAX_INVENTORY_NUM: usize = 31; | ||
|
||
pub fn draw_window(ui: &mut Ui, inventory_num: &mut usize, inventory: &mut [Inventory]) { | ||
ui.horizontal(|ui| { | ||
// do not set the 8th weapon, you may go into issue. | ||
if (*inventory_num == 0 | ||
|| inventory | ||
.get(*inventory_num - 1) | ||
.is_some_and(|&i| i != Inventory::None)) | ||
&& *inventory_num < MAX_INVENTORY_NUM | ||
&& ui.button(" + ").clicked() | ||
{ | ||
*inventory_num += 1 | ||
} | ||
|
||
if *inventory_num > 0 && ui.button(" - ").clicked() { | ||
*inventory_num -= 1; | ||
inventory[*inventory_num] = Default::default(); | ||
} | ||
}); | ||
|
||
ui.separator(); | ||
|
||
let chunk_size = 6; | ||
for (chunk_i, chunk) in inventory[..*inventory_num] | ||
.chunks_mut(chunk_size) | ||
.enumerate() | ||
{ | ||
ui.horizontal(|ui| { | ||
for (i, inventory) in chunk.iter_mut().enumerate() { | ||
ui.vertical(|ui| { | ||
egui::ComboBox::new(format!("weapontype-box-{}", chunk_i * chunk_size + i), "") | ||
.width(150.) | ||
.selected_text(inventory.to_string()) | ||
.show_ui(ui, |ui| { | ||
for model in Inventory::iter() { | ||
ui.selectable_value(inventory, model, model.to_string()); | ||
} | ||
}); | ||
}); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use cavestory_save::{ | ||
items::{EquipOpt, Equipment, Inventory, WeaponType}, | ||
strum::IntoEnumIterator, | ||
GameProfile, Profile, ProfileError, | ||
}; | ||
|
||
use crate::MainApp; | ||
|
||
pub trait ProfileExt { | ||
fn verify_and_init(&mut self, data: Vec<u8>) -> Result<(), ProfileError>; | ||
fn update_state(&mut self) -> Option<()>; | ||
fn detect_equip(&self) -> Option<[bool; 9]>; | ||
fn count_weapon(&self) -> Option<usize>; | ||
fn count_inventory(&self) -> Option<usize>; | ||
} | ||
|
||
impl ProfileExt for MainApp { | ||
fn verify_and_init(&mut self, data: Vec<u8>) -> Result<(), ProfileError> { | ||
match Profile::try_from(data) { | ||
Ok(profile) => { | ||
let game_profile = GameProfile::dump(&profile); | ||
self.profile = Some((profile, game_profile)); | ||
self.update_state(); | ||
Ok(()) | ||
} | ||
Err(e) => { | ||
use rfd::{AsyncMessageDialog, MessageLevel}; | ||
tokio::task::spawn(async move { | ||
AsyncMessageDialog::new() | ||
.set_level(MessageLevel::Error) | ||
.set_title("Load Error") | ||
.set_description(&e.to_string()) | ||
.show() | ||
.await; | ||
}); | ||
Err(e) | ||
} | ||
} | ||
} | ||
|
||
fn update_state(&mut self) -> Option<()> { | ||
self.weapon_num = self.count_weapon()?; | ||
self.inventory_num = self.count_inventory()?; | ||
self.equip_checked = self.detect_equip()?; | ||
Some(()) | ||
} | ||
|
||
fn detect_equip(&self) -> Option<[bool; 9]> { | ||
self.profile | ||
.as_ref() | ||
.map(|(_, GameProfile { equipment, .. })| { | ||
let mut equip_checked: [bool; 9] = Default::default(); | ||
|
||
let equip_current = equipment; | ||
for (i, equip) in Equipment::iter().enumerate() { | ||
equip_checked[i] = equip_current.check(equip); | ||
} | ||
|
||
equip_checked | ||
}) | ||
} | ||
|
||
fn count_weapon(&self) -> Option<usize> { | ||
self.profile | ||
.as_ref() | ||
.map(|(_, GameProfile { weapon, .. })| { | ||
weapon | ||
.iter() | ||
.take_while(|w| w.classification != WeaponType::None) | ||
.count() | ||
}) | ||
} | ||
|
||
fn count_inventory(&self) -> Option<usize> { | ||
self.profile | ||
.as_ref() | ||
.map(|(_, GameProfile { inventory, .. })| { | ||
inventory | ||
.iter() | ||
.take_while(|&&i| i != Inventory::None) | ||
.count() | ||
}) | ||
} | ||
} |