Merge peripheries into the editor
This commit is contained in:
parent
4e44e612ab
commit
7e64590279
|
@ -1,3 +1,6 @@
|
||||||
|
use iced::Image;
|
||||||
|
use iced::image::Handle;
|
||||||
|
use image::DynamicImage;
|
||||||
use crate::controls::ControlsEvent;
|
use crate::controls::ControlsEvent;
|
||||||
use crate::controls::Controls;
|
use crate::controls::Controls;
|
||||||
use crate::lyrics::Lyric;
|
use crate::lyrics::Lyric;
|
||||||
|
@ -5,14 +8,12 @@ use crate::lyrics::Lyrics;
|
||||||
use crate::app::Message;
|
use crate::app::Message;
|
||||||
use iced::Element;
|
use iced::Element;
|
||||||
use crate::styles::Theme;
|
use crate::styles::Theme;
|
||||||
use std::sync::Arc;
|
|
||||||
use iced::Container;
|
use iced::Container;
|
||||||
use iced::Row;
|
use iced::Row;
|
||||||
use crate::palette::Palette;
|
use crate::palette::Palette;
|
||||||
use crate::load_song::extract_cover;
|
use crate::load_song::extract_cover;
|
||||||
use crate::peripheries::Periphery;
|
|
||||||
use iced::Length;
|
use iced::Length;
|
||||||
use iced::Align;
|
use image::imageops::FilterType;
|
||||||
|
|
||||||
use symphonia::core::formats::FormatReader;
|
use symphonia::core::formats::FormatReader;
|
||||||
|
|
||||||
|
@ -21,8 +22,9 @@ pub struct Editor {
|
||||||
lyrics: Lyrics,
|
lyrics: Lyrics,
|
||||||
theme: Theme,
|
theme: Theme,
|
||||||
controls: Controls,
|
controls: Controls,
|
||||||
left_peri: Periphery,
|
bg_img: DynamicImage,
|
||||||
rite_peri: Periphery,
|
cached_resized_bg: Option<Handle>,
|
||||||
|
dimensions: (u32, u32),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Editor {
|
impl Editor {
|
||||||
|
@ -37,16 +39,17 @@ impl Editor {
|
||||||
}).unwrap_or_else(|| Theme::default());
|
}).unwrap_or_else(|| Theme::default());
|
||||||
|
|
||||||
let cover = cover.expect("TODO");
|
let cover = cover.expect("TODO");
|
||||||
let cover = Arc::new(cover.blur((cover.width() / 50) as f32));
|
let bg_img = DynamicImage::ImageBgra8(
|
||||||
|
cover.blur((cover.width() / 50) as f32).into_bgra8()
|
||||||
let left_peri = Periphery::new(cover.clone(), true, size);
|
);
|
||||||
let rite_peri = Periphery::new(cover, false, size);
|
|
||||||
|
|
||||||
let controls = Controls::new(song);
|
let controls = Controls::new(song);
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
lyrics: Lyrics::new(),
|
lyrics: Lyrics::new(),
|
||||||
controls, theme, left_peri, rite_peri,
|
dimensions: size,
|
||||||
|
cached_resized_bg: None,
|
||||||
|
controls, theme, bg_img,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,23 +74,58 @@ impl Editor {
|
||||||
self.controls.is_playing()
|
self.controls.is_playing()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn calculate_margin_width(&self) -> u32 {
|
||||||
|
let (w, _h) = self.dimensions;
|
||||||
|
let body_size = (w / 5).max(450);
|
||||||
|
|
||||||
|
w.saturating_sub(body_size) / 2
|
||||||
|
}
|
||||||
|
|
||||||
pub fn notify_resized(&mut self, w: u32, h: u32) {
|
pub fn notify_resized(&mut self, w: u32, h: u32) {
|
||||||
self.left_peri.notify_resized(w, h);
|
self.dimensions = (w, h);
|
||||||
self.rite_peri.notify_resized(w, h);
|
|
||||||
|
let (_w, h) = self.dimensions;
|
||||||
|
let margin_w = self.calculate_margin_width();
|
||||||
|
|
||||||
|
self.cached_resized_bg = if margin_w != 0 {
|
||||||
|
let resized_bg = self.bg_img.resize_to_fill(margin_w, h, FilterType::Nearest);
|
||||||
|
|
||||||
|
Some(Handle::from_pixels(
|
||||||
|
resized_bg.width(),
|
||||||
|
resized_bg.height(),
|
||||||
|
resized_bg.into_bgra8().into_raw()
|
||||||
|
))
|
||||||
|
} else { None };
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn view(&mut self) -> Element<Message> {
|
pub fn view(&mut self) -> Element<Message> {
|
||||||
|
|
||||||
Container::new(
|
let row = if let Some(margin_bg) = &self.cached_resized_bg {
|
||||||
|
let (w, _h) = self.dimensions;
|
||||||
|
|
||||||
|
let (img1, img2) = (
|
||||||
|
Image::new(margin_bg.clone())
|
||||||
|
.width(Length::Units(w as u16))
|
||||||
|
.height(Length::Fill),
|
||||||
|
Image::new(margin_bg.clone())
|
||||||
|
.width(Length::Units(w as u16))
|
||||||
|
.height(Length::Fill),
|
||||||
|
);
|
||||||
|
|
||||||
Row::new()
|
Row::new()
|
||||||
.push(self.left_peri.view())
|
.push(img1)
|
||||||
.push(self.controls.view_progress(self.theme))
|
.push(self.controls.view_progress(self.theme))
|
||||||
.push(self.lyrics.view(self.theme))
|
.push(self.lyrics.view(self.theme))
|
||||||
.push(self.rite_peri.view())
|
.push(img2)
|
||||||
)
|
} else {
|
||||||
.align_y(Align::Center)
|
Row::new()
|
||||||
.style(self.theme)
|
.push(self.controls.view_progress(self.theme))
|
||||||
.height(Length::Fill)
|
.push(self.lyrics.view(self.theme))
|
||||||
.into()
|
};
|
||||||
|
|
||||||
|
Container::new(row)
|
||||||
|
.style(self.theme)
|
||||||
|
.height(Length::Fill)
|
||||||
|
.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,6 @@ mod lyrics;
|
||||||
mod styles;
|
mod styles;
|
||||||
mod file_select;
|
mod file_select;
|
||||||
mod load_song;
|
mod load_song;
|
||||||
mod peripheries;
|
|
||||||
mod editor;
|
mod editor;
|
||||||
mod player;
|
mod player;
|
||||||
mod controls;
|
mod controls;
|
||||||
|
|
|
@ -1,92 +0,0 @@
|
||||||
use image::imageops::crop_imm;
|
|
||||||
use image::ImageBuffer;
|
|
||||||
use image::Bgra;
|
|
||||||
use iced::Length;
|
|
||||||
use iced::Element;
|
|
||||||
use crate::app::Message;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use iced::Image;
|
|
||||||
use image::imageops::FilterType;
|
|
||||||
use image::DynamicImage;
|
|
||||||
use image::GenericImageView;
|
|
||||||
use iced::widget::image::Handle;
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub struct Periphery {
|
|
||||||
left: bool,
|
|
||||||
source_image: Arc<DynamicImage>,
|
|
||||||
upscaled: (u32, ImageBuffer<Bgra<u8>, Vec<u8>>),
|
|
||||||
image: Handle,
|
|
||||||
w: u16,
|
|
||||||
h: u16,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Periphery {
|
|
||||||
|
|
||||||
pub fn new(source_image: Arc<DynamicImage>, left: bool, (w, h): (u32, u32)) -> Self {
|
|
||||||
let mut p = Periphery {
|
|
||||||
image: Handle::from_pixels(0, 0, Vec::new()),
|
|
||||||
upscaled: (0, ImageBuffer::<Bgra<u8>, Vec<u8>>::from_raw(0, 0, Vec::new()).unwrap()),
|
|
||||||
w: 0, h: 0,
|
|
||||||
left, source_image
|
|
||||||
};
|
|
||||||
p.notify_resized(w, h);
|
|
||||||
p
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn notify_resized(&mut self, w: u32, h: u32) {
|
|
||||||
let body_width = (w / 5).max(450);
|
|
||||||
let w = w.saturating_sub(body_width) / 2;
|
|
||||||
self.w = w as u16;
|
|
||||||
self.h = h as u16;
|
|
||||||
|
|
||||||
if w == 0 {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let h_scale_needed = w as f32 / self.source_image.width() as f32;
|
|
||||||
let v_scale_needed = h as f32 / self.source_image.height() as f32;
|
|
||||||
let scale = f32::max(h_scale_needed, v_scale_needed);
|
|
||||||
|
|
||||||
let nh = (scale * (self.source_image.height() as f32)).ceil() as u32;
|
|
||||||
let nw = (scale * (self.source_image.width() as f32)).ceil() as u32;
|
|
||||||
self.upscaled = (
|
|
||||||
w,
|
|
||||||
self.source_image.resize(nw, nh, FilterType::Nearest)
|
|
||||||
.into_bgra8()
|
|
||||||
);
|
|
||||||
|
|
||||||
let ratio = w as f32 / h as f32;
|
|
||||||
let source_ratio = self.source_image.width() as f32 / self.source_image.height() as f32;
|
|
||||||
|
|
||||||
let (nw, nh) = if ratio < source_ratio {
|
|
||||||
(
|
|
||||||
(self.upscaled.1.height() as f32 * ratio) as u32,
|
|
||||||
self.upscaled.1.height()
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
(
|
|
||||||
self.upscaled.1.width(),
|
|
||||||
(self.upscaled.1.width() as f32 / ratio) as u32
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
let h_shift_mul = if self.left { -1 } else { 1 };
|
|
||||||
let image = crop_imm(
|
|
||||||
&self.upscaled.1,
|
|
||||||
(((self.upscaled.1.width() / 2) as i32 + h_shift_mul * (nw as i32)).max(0) as u32).min(self.upscaled.1.width() - nw),
|
|
||||||
self.upscaled.1.height().saturating_sub(nh) / 2,
|
|
||||||
w, h,
|
|
||||||
);
|
|
||||||
|
|
||||||
self.image = Handle::from_pixels(image.width(), image.height(), image.to_image().into_raw());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn view(&mut self) -> Element<Message> {
|
|
||||||
Image::new(self.image.clone())
|
|
||||||
.width(Length::Units(self.w))
|
|
||||||
.height(Length::Fill)
|
|
||||||
.into()
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue