Compare commits
2 Commits
bc19df18cb
...
02fbd15f44
Author | SHA1 | Date |
---|---|---|
Emi Simpson | 02fbd15f44 | |
Emi Simpson | 9b2a6ffde3 |
|
@ -1569,6 +1569,7 @@ dependencies = [
|
||||||
"guillotiere",
|
"guillotiere",
|
||||||
"iced_graphics",
|
"iced_graphics",
|
||||||
"iced_native",
|
"iced_native",
|
||||||
|
"image",
|
||||||
"log",
|
"log",
|
||||||
"raw-window-handle 0.3.4",
|
"raw-window-handle 0.3.4",
|
||||||
"wgpu",
|
"wgpu",
|
||||||
|
|
|
@ -26,7 +26,7 @@ version = "0.4.0"
|
||||||
|
|
||||||
[dependencies.iced]
|
[dependencies.iced]
|
||||||
# Display windows & graphics
|
# Display windows & graphics
|
||||||
features = ["canvas"]
|
features = ["canvas", "image"]
|
||||||
version = "0.3.0"
|
version = "0.3.0"
|
||||||
|
|
||||||
[dependencies.iced_futures]
|
[dependencies.iced_futures]
|
||||||
|
|
18
src/app.rs
18
src/app.rs
|
@ -1,5 +1,5 @@
|
||||||
use crate::load_song::load_song;
|
use crate::load_song::load_song;
|
||||||
use crate::lyrics::Lyrics;
|
use crate::editor::Editor;
|
||||||
use crate::file_select::FileSelector;
|
use crate::file_select::FileSelector;
|
||||||
use rfd::AsyncFileDialog;
|
use rfd::AsyncFileDialog;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
@ -17,8 +17,9 @@ use iced_native::window;
|
||||||
use iced_native::event::Event;
|
use iced_native::event::Event;
|
||||||
|
|
||||||
pub struct DelyriumApp {
|
pub struct DelyriumApp {
|
||||||
lyrics_component: Option<Lyrics>,
|
lyrics_component: Option<Editor>,
|
||||||
file_selector: FileSelector,
|
file_selector: FileSelector,
|
||||||
|
size: (u32, u32),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
|
@ -32,6 +33,7 @@ pub enum Message {
|
||||||
Tick,
|
Tick,
|
||||||
PromptForFile,
|
PromptForFile,
|
||||||
FileOpened(PathBuf),
|
FileOpened(PathBuf),
|
||||||
|
Resized(u32, u32),
|
||||||
Null,
|
Null,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,6 +47,7 @@ impl Application for DelyriumApp {
|
||||||
DelyriumApp {
|
DelyriumApp {
|
||||||
lyrics_component: None,
|
lyrics_component: None,
|
||||||
file_selector: FileSelector::default(),
|
file_selector: FileSelector::default(),
|
||||||
|
size: (0, 0),
|
||||||
},
|
},
|
||||||
Command::none(),
|
Command::none(),
|
||||||
)
|
)
|
||||||
|
@ -86,7 +89,7 @@ impl Application for DelyriumApp {
|
||||||
Message::FileOpened(path) => {
|
Message::FileOpened(path) => {
|
||||||
println!("File opened! {}", path.display());
|
println!("File opened! {}", path.display());
|
||||||
let song = load_song(&path).unwrap().format;
|
let song = load_song(&path).unwrap().format;
|
||||||
self.lyrics_component = Some(Lyrics::new(song));
|
self.lyrics_component = Some(Editor::new(song, self.size));
|
||||||
},
|
},
|
||||||
Message::PromptForFile => {
|
Message::PromptForFile => {
|
||||||
let task = async {
|
let task = async {
|
||||||
|
@ -103,6 +106,12 @@ impl Application for DelyriumApp {
|
||||||
};
|
};
|
||||||
command = Some(task.into());
|
command = Some(task.into());
|
||||||
},
|
},
|
||||||
|
Message::Resized(w, h) => {
|
||||||
|
self.size = (w, h);
|
||||||
|
if let Some(lyrics) = self.lyrics_component.as_mut() {
|
||||||
|
lyrics.notify_resized(w, h);
|
||||||
|
}
|
||||||
|
}
|
||||||
Message::Null => { },
|
Message::Null => { },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,6 +143,9 @@ impl Application for DelyriumApp {
|
||||||
Event::Window(window::Event::FileDropped(path)) => {
|
Event::Window(window::Event::FileDropped(path)) => {
|
||||||
Some(Message::FileOpened(path))
|
Some(Message::FileOpened(path))
|
||||||
},
|
},
|
||||||
|
Event::Window(window::Event::Resized{width,height}) => {
|
||||||
|
Some(Message::Resized(width,height))
|
||||||
|
},
|
||||||
_ => { None }
|
_ => { None }
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -0,0 +1,81 @@
|
||||||
|
use crate::lyrics::Lyric;
|
||||||
|
use crate::lyrics::Lyrics;
|
||||||
|
use crate::app::Message;
|
||||||
|
use iced::Element;
|
||||||
|
use crate::styles::Theme;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use iced::Container;
|
||||||
|
use iced::Row;
|
||||||
|
use crate::palette::Palette;
|
||||||
|
use crate::load_song::extract_cover;
|
||||||
|
use crate::peripheries::Periphery;
|
||||||
|
use iced::Length;
|
||||||
|
use iced::Align;
|
||||||
|
|
||||||
|
use symphonia::core::formats::FormatReader;
|
||||||
|
|
||||||
|
use image::GenericImageView;
|
||||||
|
pub struct Editor {
|
||||||
|
lyrics: Lyrics,
|
||||||
|
theme: Theme,
|
||||||
|
song: Box<dyn FormatReader>,
|
||||||
|
left_peri: Periphery,
|
||||||
|
rite_peri: Periphery,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Editor {
|
||||||
|
pub fn new(mut song: Box<dyn FormatReader>, size: (u32, u32)) -> Self {
|
||||||
|
let cover = extract_cover(song.as_mut());
|
||||||
|
|
||||||
|
let theme = cover.as_ref()
|
||||||
|
.map(|cover| {
|
||||||
|
Theme::from_palette(
|
||||||
|
Palette::generate(&cover)
|
||||||
|
)
|
||||||
|
}).unwrap_or_else(|| Theme::default());
|
||||||
|
|
||||||
|
let cover = cover.expect("TODO");
|
||||||
|
let cover = Arc::new(cover.blur((cover.width() / 50) as f32));
|
||||||
|
|
||||||
|
let left_peri = Periphery::new(cover.clone(), true, size);
|
||||||
|
let rite_peri = Periphery::new(cover, false, size);
|
||||||
|
|
||||||
|
Self {
|
||||||
|
lyrics: Lyrics::new(),
|
||||||
|
song, theme, left_peri, rite_peri,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: work on untangling this mess
|
||||||
|
pub fn insert_text(&mut self, text: String) {
|
||||||
|
self.lyrics.insert_text(text);
|
||||||
|
}
|
||||||
|
pub fn update_line(&mut self, new_content: String, line_no: usize) {
|
||||||
|
self.lyrics.update_line(new_content, line_no);
|
||||||
|
}
|
||||||
|
pub fn advance_line(&mut self, current_line: usize) {
|
||||||
|
self.lyrics.advance_line(current_line);
|
||||||
|
}
|
||||||
|
pub fn current_line_mut(&mut self) -> (usize, &mut Lyric) {
|
||||||
|
self.lyrics.current_line_mut()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn notify_resized(&mut self, w: u32, h: u32) {
|
||||||
|
self.left_peri.notify_resized(w, h);
|
||||||
|
self.rite_peri.notify_resized(w, h);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn view(&mut self) -> Element<Message> {
|
||||||
|
|
||||||
|
Container::new(
|
||||||
|
Row::new()
|
||||||
|
.push(self.left_peri.view())
|
||||||
|
.push(self.lyrics.view(self.theme))
|
||||||
|
.push(self.rite_peri.view())
|
||||||
|
)
|
||||||
|
.align_y(Align::Center)
|
||||||
|
.style(self.theme)
|
||||||
|
.height(Length::Fill)
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,43 +1,25 @@
|
||||||
use iced::Container;
|
use iced::Length;
|
||||||
use iced::Row;
|
|
||||||
use crate::palette::Palette;
|
|
||||||
use crate::load_song::extract_cover;
|
|
||||||
use iced::Element;
|
use iced::Element;
|
||||||
use crate::styles::Theme;
|
use crate::styles::Theme;
|
||||||
use crate::app::Message;
|
use crate::app::Message;
|
||||||
|
|
||||||
use iced::Align;
|
|
||||||
use iced::Length;
|
|
||||||
use iced::widget::text_input::{self, TextInput};
|
use iced::widget::text_input::{self, TextInput};
|
||||||
use iced::widget::scrollable::{self, Scrollable};
|
use iced::widget::scrollable::{self, Scrollable};
|
||||||
|
use iced::Align;
|
||||||
use symphonia::core::formats::FormatReader;
|
|
||||||
|
|
||||||
pub struct Lyrics {
|
pub struct Lyrics {
|
||||||
lines: Vec<Lyric>,
|
lines: Vec<Lyric>,
|
||||||
scroll_state: scrollable::State,
|
scroll_state: scrollable::State,
|
||||||
theme: Theme,
|
|
||||||
song: Box<dyn FormatReader>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Lyrics {
|
impl Lyrics {
|
||||||
pub fn new(mut song: Box<dyn FormatReader>) -> Self {
|
pub fn new() -> Lyrics {
|
||||||
|
|
||||||
let mut lyric = Lyric::new();
|
let mut lyric = Lyric::new();
|
||||||
lyric.select();
|
lyric.select();
|
||||||
|
|
||||||
let cover = extract_cover(song.as_mut());
|
Self {
|
||||||
|
|
||||||
let theme = cover.map(|cover| {
|
|
||||||
Theme::from_palette(
|
|
||||||
Palette::generate(&cover)
|
|
||||||
)
|
|
||||||
}).unwrap_or_else(|| Theme::default());
|
|
||||||
|
|
||||||
Lyrics {
|
|
||||||
lines: vec![lyric],
|
lines: vec![lyric],
|
||||||
scroll_state: scrollable::State::new(),
|
scroll_state: scrollable::State::new(),
|
||||||
song, theme,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,27 +84,20 @@ impl Lyrics {
|
||||||
.expect("no line currently selected")
|
.expect("no line currently selected")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn view(&mut self) -> Element<Message> {
|
pub fn view(&mut self, theme: Theme) -> Element<Message> {
|
||||||
let is_sole_line = self.lines.len() == 1;
|
let is_sole_line = self.lines.len() == 1;
|
||||||
|
|
||||||
let lyrics = self.lines.iter_mut()
|
self.lines.iter_mut()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(i, l)| l.view(is_sole_line, i, self.theme))
|
.map(|(i, l)| l.view(is_sole_line, i, theme))
|
||||||
.fold(Scrollable::new(&mut self.scroll_state), |s, l| s.push(l))
|
.fold(Scrollable::new(&mut self.scroll_state), |s, l| s.push(l))
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
.align_items(Align::Center);
|
.align_items(Align::Center)
|
||||||
|
.into()
|
||||||
Container::new(
|
|
||||||
Row::new()
|
|
||||||
.push(lyrics)
|
|
||||||
)
|
|
||||||
.align_y(Align::Center)
|
|
||||||
.style(self.theme)
|
|
||||||
.height(Length::Fill)
|
|
||||||
.into()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Lyric {
|
pub struct Lyric {
|
||||||
state: text_input::State,
|
state: text_input::State,
|
||||||
|
|
|
@ -7,6 +7,8 @@ mod lyrics;
|
||||||
mod styles;
|
mod styles;
|
||||||
mod file_select;
|
mod file_select;
|
||||||
mod load_song;
|
mod load_song;
|
||||||
|
mod peripheries;
|
||||||
|
mod editor;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
app::DelyriumApp::run(Settings::default()).unwrap();
|
app::DelyriumApp::run(Settings::default()).unwrap();
|
||||||
|
|
|
@ -0,0 +1,92 @@
|
||||||
|
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::Units(self.h))
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue