timeline clickable
This commit is contained in:
54
src/music.rs
54
src/music.rs
@@ -1,10 +1,15 @@
|
||||
use crate::message::Message;
|
||||
use crate::utils::string_to_polygon;
|
||||
use crate::{polygon_draw::*, utils::string_to_color};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use kira::{AudioManager, sound::static_sound::StaticSoundData};
|
||||
|
||||
use iced::mouse;
|
||||
use iced::event::Status;
|
||||
use iced::mouse::Cursor;
|
||||
use iced::widget::canvas::Event;
|
||||
use iced::{mouse, padding};
|
||||
|
||||
use iced::widget::canvas;
|
||||
use iced::widget::canvas::Stroke;
|
||||
use iced::widget::canvas::Style;
|
||||
@@ -149,13 +154,13 @@ impl Music {
|
||||
self.find_poly_frame(delta).polygons.remove(i);
|
||||
}
|
||||
}
|
||||
impl<Message> canvas::Program<Message> for Music {
|
||||
impl canvas::Program<Message> for Music {
|
||||
// No internal state
|
||||
type State = ();
|
||||
type State = bool;
|
||||
|
||||
fn draw(
|
||||
&self,
|
||||
_state: &(),
|
||||
_state: &Self::State,
|
||||
renderer: &Renderer,
|
||||
_theme: &Theme,
|
||||
bounds: Rectangle,
|
||||
@@ -166,7 +171,7 @@ impl<Message> canvas::Program<Message> for Music {
|
||||
let padding = 8.;
|
||||
let w = bounds.width - (padding * 2.);
|
||||
for (delta, _) in &self.poly_frame {
|
||||
let x = delta / self.length * w + padding;
|
||||
let x = delta / self.length * w + 8.;
|
||||
frame.fill_rectangle(
|
||||
iced::Point { x: x, y: 0.0 },
|
||||
frame.size(),
|
||||
@@ -187,7 +192,7 @@ impl<Message> canvas::Program<Message> for Music {
|
||||
},
|
||||
);
|
||||
|
||||
let x = self.current_delta / self.length * w + padding;
|
||||
let x = self.current_delta / self.length * w + 8.;
|
||||
frame.stroke_rectangle(
|
||||
iced::Point::new(x, 0.),
|
||||
iced::Size {
|
||||
@@ -204,4 +209,41 @@ impl<Message> canvas::Program<Message> for Music {
|
||||
// Then, we produce the geometry
|
||||
vec![frame.into_geometry()]
|
||||
}
|
||||
fn update(
|
||||
&self,
|
||||
state: &mut Self::State,
|
||||
event: Event,
|
||||
bounds: Rectangle,
|
||||
cursor: Cursor,
|
||||
) -> (Status, Option<Message>) {
|
||||
//eprintln!("event = {:?}", event);
|
||||
if let Event::Mouse(mouse_event) = event {
|
||||
match mouse_event {
|
||||
mouse::Event::ButtonPressed(mouse::Button::Left) => {
|
||||
*state = true;
|
||||
if let Some(position) = cursor.position_in(bounds) {
|
||||
let pos_x = (position.x - 8.0) / (bounds.width - 16.);
|
||||
let delta = (pos_x * self.length).clamp(0., self.length);
|
||||
return (
|
||||
Status::Captured,
|
||||
Some(crate::message::Message::ClickedOnTimeLine(delta)),
|
||||
);
|
||||
}
|
||||
}
|
||||
mouse::Event::ButtonReleased(mouse::Button::Left) => *state = false,
|
||||
mouse::Event::CursorMoved { position: _ } => {
|
||||
if let Some(position) = cursor.position_in(bounds)
|
||||
&& *state
|
||||
{
|
||||
let pos_x = (position.x - 8.0) / (bounds.width - 16.);
|
||||
let delta = (pos_x * self.length).clamp(0., self.length);
|
||||
return (Status::Captured, Some(Message::ClickedOnTimeLine(delta)));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
(Status::Ignored, None)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user