Skip to content

Commit

Permalink
fix: chapter offset adjusting (TeamFlos#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidhelium committed May 4, 2024
1 parent 28a2e7a commit 00456f5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
3 changes: 2 additions & 1 deletion phira/src/charts_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
client::Chart,
dir, get_data, get_data_mut,
icons::Icons,
page::{ChartItem, Fader, Illustration},
page::{ChartItem, ChartType, Fader, Illustration},
save_data,
scene::{render_release_to_refresh, SongScene, MP_PANEL},
};
Expand Down Expand Up @@ -67,6 +67,7 @@ impl ChartDisplayItem {
}
},
local_path: None,
chart_type: ChartType::Downloaded,
}),
if chart.stable_request {
Some('+')
Expand Down
9 changes: 9 additions & 0 deletions phira/src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ pub fn load_local(order: &(ChartOrder, bool)) -> Vec<ChartItem> {
info: it.info.clone(),
local_path: Some(it.local_path.clone()),
illu: local_illustration(it.local_path.clone(), tex.clone(), false),
chart_type: ChartType::Imported,
})
.collect();
order.0.apply(&mut res);
Expand Down Expand Up @@ -191,6 +192,14 @@ pub struct ChartItem {
pub info: BriefChartInfo,
pub local_path: Option<String>,
pub illu: Illustration,
pub chart_type: ChartType
}

#[derive(Clone, Copy)]
pub enum ChartType {
Downloaded,
Imported,
Integrated,
}

// srange name, isn't it?
Expand Down
19 changes: 17 additions & 2 deletions phira/src/scene/chapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ prpr::tl_file!("chapter");
use crate::{
anim::Anim,
data::BriefChartInfo,
dir,
icons::Icons,
load_res_tex,
page::{ChartItem, Illustration, SFader},
page::{ChartItem, ChartType, Illustration, SFader},
resource::rtl,
};
use anyhow::Result;
Expand Down Expand Up @@ -202,8 +203,15 @@ impl Scene for ChapterScene {
},
illu: Illustration::from_done(chart.illu.clone()),
local_path: Some(local_path.clone()),
chart_type: ChartType::Integrated,
};
let info = &item.info;
let dir = format!("{}/{}", dir::charts()?, item.local_path.as_ref().unwrap().replace(':', "_"));
let path = std::path::Path::new(&dir);
if !path.exists() {
std::fs::create_dir_all(path)?;
}
let dir = prpr::dir::Dir::new(dir)?;
*ASSET_CHART_INFO.lock().unwrap() = Some(ChartInfo {
id: None,
uploader: None,
Expand All @@ -226,7 +234,14 @@ impl Scene for ChapterScene {
aspect_ratio: 16. / 9.,
background_dim: 0.6,
line_length: 6.,
offset: 0.,
offset: dir
.read("offset")
.map(|d| f32::from_be_bytes(d.get(0..4).map(|first4| {
let mut result = <[u8;4]>::default();
result.copy_from_slice(first4);
result
}).unwrap_or_default()))
.unwrap_or_default(),
tip: None,
tags: Vec::new(),

Expand Down
30 changes: 22 additions & 8 deletions phira/src/scene/song.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
data::{BriefChartInfo, LocalChart},
dir, get_data, get_data_mut,
icons::Icons,
page::{local_illustration, thumbnail_path, ChartItem, Fader, Illustration, SFader},
page::{local_illustration, thumbnail_path, ChartItem, ChartType, Fader, Illustration, SFader},
popup::Popup,
rate::RateDialog,
save_data,
Expand Down Expand Up @@ -313,6 +313,7 @@ pub struct SongScene {

update_cksum_passed: Option<bool>,
update_cksum_task: Option<Task<Result<bool>>>,
chart_type: ChartType,
}

impl SongScene {
Expand Down Expand Up @@ -483,6 +484,7 @@ impl SongScene {

update_cksum_passed: None,
update_cksum_task: None,
chart_type: chart.chart_type,
}
}

Expand Down Expand Up @@ -1279,13 +1281,25 @@ impl Scene for SongScene {
let _res = match res.downcast::<Option<f32>>() {
Ok(offset) => {
if let Some(offset) = *offset {
let dir = prpr::dir::Dir::new(format!("{}/{}", dir::charts()?, self.local_path.as_ref().unwrap()))?;
let mut info: ChartInfo = serde_yaml::from_reader(&dir.open("info.yml")?)?;
info.offset = offset;
dir.create("info.yml")?.write_all(serde_yaml::to_string(&info)?.as_bytes())?;
let path = thumbnail_path(self.local_path.as_ref().unwrap())?;
if path.exists() {
std::fs::remove_file(path)?;
let dir = format!("{}/{}", dir::charts()?, self.local_path.as_ref().unwrap().replace(':', "_"));
let path = std::path::Path::new(&dir);
if !path.exists() {
std::fs::create_dir_all(path)?;
}
let dir = prpr::dir::Dir::new(dir)?;
match self.chart_type {
ChartType::Integrated => {
dir.create("offset")?.write_all(&offset.to_be_bytes())?;
}
_ => {
let mut info: ChartInfo = serde_yaml::from_reader(&dir.open("info.yml")?)?;
info.offset = offset;
dir.create("info.yml")?.write_all(serde_yaml::to_string(&info)?.as_bytes())?;
let path = thumbnail_path(self.local_path.as_ref().unwrap())?;
if path.exists() {
std::fs::remove_file(path)?;
}
}
}
show_message(tl!("edit-saved")).ok();
}
Expand Down

0 comments on commit 00456f5

Please sign in to comment.