Skip to content

Commit

Permalink
Add clips to timeline
Browse files Browse the repository at this point in the history
  • Loading branch information
geom3trik committed Aug 20, 2021
1 parent 9eb16b7 commit 5ba4c7e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/ui/components/control_bars/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Widget for TransportControlBar {
.on_press(|_, state, button| {
button.emit(state, TransportEvent::Stop);
})
.bind(AppData::is_playing, |_| ())
.bind(AppData::is_playing, |data| ())
.build(state, controls, |builder| builder);

entity
Expand Down
62 changes: 58 additions & 4 deletions src/ui/components/timeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use crate::backend::timeline::TimelineTrackSaveState;

use crate::ui::app_data::AppData;

const ZOOM_LEVELS: [f32; 10] = [0.1, 0.2, 0.3, 0.5, 1.0, 2.0, 3.0, 4.0, 8.0, 16.0];

#[derive(Debug, Clone, PartialEq)]
pub enum ScrollEvent {
ScrollH(Scroll),
Expand Down Expand Up @@ -41,11 +43,20 @@ impl Model for ScrollState {
}

/// A general purpose timeline widget
pub struct Timeline {}
pub struct Timeline {
// TEMP - Move to model
zoom_level: usize,

tracks: Entity,

}

impl Timeline {
pub fn new() -> Self {
Self {}
Self {
zoom_level: 4,
tracks: Entity::null(),
}
}
}

Expand All @@ -54,6 +65,26 @@ impl Widget for Timeline {
type Data = Vec<TimelineTrackSaveState>;

fn on_build(&mut self, state: &mut State, entity: Entity) -> Self::Ret {

let bar = Element::new().build(state, entity, |builder|
builder
.set_height(Pixels(20.0))
.set_text("Bar")
.set_layout_type(LayoutType::Row)
);

Button::with_label("OUT")
.on_press(|data, state, button|{
button.emit(state, ZoomEvent::ZoomOut);
})
.build(state, bar, |builder| builder.set_width(Pixels(100.0)));

Button::with_label("IN")
.on_press(|data, state, button| {
button.emit(state, ZoomEvent::ZoomIn);
})
.build(state, bar, |builder| builder.set_width(Pixels(100.0)));

let scroll_data = ScrollState::default().build(state, entity);

scroll_data
Expand Down Expand Up @@ -144,12 +175,12 @@ impl Widget for Timeline {
.set_row_between(state, Pixels(2.0))
.set_height(state, Auto);

ListView::new(|item: &TimelineTrackSaveState| Track::new(item.name().clone()))
self.tracks = ListView::new(|item: &TimelineTrackSaveState| Track::new(item.name().clone()))
.bind(AppData::project_interface.then(ProjectStateInterface::save_state).then(ProjectSaveState::timeline_tracks), |tracks| tracks.clone())
.build(state, tracks, |builder|
builder
.set_height(Auto)
.set_width(Auto)
.set_width(Pixels(1000.0))
.set_row_between( Pixels(2.0))
);

Expand Down Expand Up @@ -189,4 +220,27 @@ impl Widget for Timeline {
fn on_update(&mut self, state: &mut State, entity: Entity, data: &Self::Data) {

}

fn on_event(&mut self, state: &mut State, entity: Entity, event: &mut Event) {
if let Some(zoom_event) = event.message.downcast() {
match zoom_event {
ZoomEvent::ZoomIn => {
self.zoom_level = self.zoom_level.saturating_add(1).clamp(0, 9);
self.tracks.set_width(state, Pixels(1000.0 * ZOOM_LEVELS[self.zoom_level]));
}

ZoomEvent::ZoomOut => {
self.zoom_level = self.zoom_level.saturating_sub(1).clamp(0, 9);
self.tracks.set_width(state, Pixels(1000.0 * ZOOM_LEVELS[self.zoom_level]));
}
}
}
}
}

// Temp
#[derive(Debug, Clone, PartialEq)]
pub enum ZoomEvent {
ZoomIn,
ZoomOut,
}
28 changes: 25 additions & 3 deletions src/ui/components/timeline/track.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,53 @@

use std::collections::HashMap;

use tuix::*;

//use crate::backend::timeline::TimelineTrackSaveState;
use crate::backend::timeline::TimelineTrackSaveState;

// Track (TODO)
pub struct Track {
name: String,

clips: HashMap<String, Entity>,
}

impl Track {
pub fn new(name: String) -> Self {
Self {
name: name.clone(),
clips: HashMap::new(),
}
}
}

impl Widget for Track {
type Ret = Entity;
type Data = ();
type Data = TimelineTrackSaveState;
fn on_build(&mut self, state: &mut State, entity: Entity) -> Self::Ret {
entity
.set_background_color(state, Color::rgb(150, 100, 190))
.set_height(state, Pixels(80.0))
.set_width(state, Pixels(1000.0))
//.set_width(state, Pixels(1000.0))
.set_text(state, &self.name)
}

fn on_update(&mut self, state: &mut State, entity: Entity, data: &Self::Data) {
for clip in data.audio_clips().iter() {
println!("Clip Time: {:?}", clip.timeline_start());
println!("Clip Duration: {:?}", clip.duration());

if !self.clips.contains_key(clip.name()) {
self.clips.insert(clip.name().clone(), Element::new().build(state, entity, |builder|
builder
.set_background_color(Color::rgb(100, 80, 150))
.set_width(Pixels(50.0))
));
}


}
}
}


Expand Down

0 comments on commit 5ba4c7e

Please sign in to comment.