~raph/interp-toy

70cf15dfa1ce0745aff623fc71b1bd48c808f15b — Raph Levien 2 years ago 5aab980
Start getting masters into UI

Still WIP, the masters don't really do anything. At this point, more UI
work is needed to get things plumbed.
4 files changed, 89 insertions(+), 22 deletions(-)

M Cargo.lock
M Cargo.toml
M src/app_state.rs
M src/main.rs
M Cargo.lock => Cargo.lock +8 -6
@@ 183,12 183,13 @@ dependencies = [
[[package]]
name = "druid"
version = "0.3.0"
source = "git+https://github.com/xi-editor/druid?rev=8e56a17e10d323e6aaf4c8da03655db6f1a62060#8e56a17e10d323e6aaf4c8da03655db6f1a62060"
source = "git+https://github.com/xi-editor/druid?rev=2962b3579b7e1dbad79ccd87bf0c8333c4b1d397#2962b3579b7e1dbad79ccd87bf0c8333c4b1d397"
dependencies = [
 "druid-shell 0.3.0 (git+https://github.com/xi-editor/druid?rev=8e56a17e10d323e6aaf4c8da03655db6f1a62060)",
 "druid-shell 0.3.0 (git+https://github.com/xi-editor/druid?rev=2962b3579b7e1dbad79ccd87bf0c8333c4b1d397)",
 "fluent-bundle 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
 "fluent-locale 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "fluent-syntax 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
 "unic-langid 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
 "unicode-segmentation 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
]


@@ 196,7 197,7 @@ dependencies = [
[[package]]
name = "druid-shell"
version = "0.3.0"
source = "git+https://github.com/xi-editor/druid?rev=8e56a17e10d323e6aaf4c8da03655db6f1a62060#8e56a17e10d323e6aaf4c8da03655db6f1a62060"
source = "git+https://github.com/xi-editor/druid?rev=2962b3579b7e1dbad79ccd87bf0c8333c4b1d397#2962b3579b7e1dbad79ccd87bf0c8333c4b1d397"
dependencies = [
 "cairo-rs 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
 "cocoa 0.18.4 (registry+https://github.com/rust-lang/crates.io-index)",


@@ 204,6 205,7 @@ dependencies = [
 "direct2d 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "directwrite 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
 "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
 "objc 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
 "piet-common 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
 "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",


@@ 313,7 315,7 @@ dependencies = [
name = "interp-toy"
version = "0.1.0"
dependencies = [
 "druid 0.3.0 (git+https://github.com/xi-editor/druid?rev=8e56a17e10d323e6aaf4c8da03655db6f1a62060)",
 "druid 0.3.0 (git+https://github.com/xi-editor/druid?rev=2962b3579b7e1dbad79ccd87bf0c8333c4b1d397)",
 "nalgebra 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "rbf-interp 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
]


@@ 1046,8 1048,8 @@ dependencies = [
"checksum direct2d 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7fa6ff10857eb253d1ae16987ebfd27372f4129b0c7a3fa41466fbdf7e453e75"
"checksum direct3d11 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "315aa929e68ba066cb6fb86f1b22af24f517e02fd9b5734c4d07e42cb9f4aefa"
"checksum directwrite 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8cdcd739e9351c411b8caf5cab32a27c818cfe06260595da121382ecdd22083d"
"checksum druid 0.3.0 (git+https://github.com/xi-editor/druid?rev=8e56a17e10d323e6aaf4c8da03655db6f1a62060)" = "<none>"
"checksum druid-shell 0.3.0 (git+https://github.com/xi-editor/druid?rev=8e56a17e10d323e6aaf4c8da03655db6f1a62060)" = "<none>"
"checksum druid 0.3.0 (git+https://github.com/xi-editor/druid?rev=2962b3579b7e1dbad79ccd87bf0c8333c4b1d397)" = "<none>"
"checksum druid-shell 0.3.0 (git+https://github.com/xi-editor/druid?rev=2962b3579b7e1dbad79ccd87bf0c8333c4b1d397)" = "<none>"
"checksum dxgi 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "1639bbfd6765e92a40267d217a7acbac5b49320b68013f39a8e4376aa8c1e091"
"checksum either 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5527cfe0d098f36e3f8839852688e63c8fff1c90b2b405aef730615f9a7bcf7b"
"checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2"

M Cargo.toml => Cargo.toml +1 -1
@@ 7,6 7,6 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
druid = {git = "https://github.com/xi-editor/druid", rev = "8e56a17e10d323e6aaf4c8da03655db6f1a62060" }
druid = {git = "https://github.com/xi-editor/druid", rev = "2962b3579b7e1dbad79ccd87bf0c8333c4b1d397" }
rbf-interp = "0.1.2"
nalgebra = "0.18"

M src/app_state.rs => src/app_state.rs +43 -1
@@ 1,3 1,4 @@
use std::ops::Deref;
use std::sync::Arc;

use nalgebra::DVector;


@@ 17,11 18,16 @@ pub struct AppState {
    pub weight: f64,

    pub pts: Arc<Vec<InterpPt>>,

    pub masters: Arc<Vec<Master>>,
}

impl Data for AppState {
    fn same(&self, other: &AppState) -> bool {
        self.width.same(&other.width)
            && self.weight.same(&other.weight)
            && self.pts.same(&other.pts)
            && self.masters.same(&other.masters)
    }
}



@@ 31,21 37,34 @@ pub struct InterpPt {
}

#[derive(Clone)]
pub struct Master {
    pub weight: f64,
}

#[derive(Clone)]
pub struct InterpSample {
    pub pt: Point,
    pub width: f64,
    pub weight: f64,
}

impl Data for Master {
    fn same(&self, other: &Self) -> bool {
        self.weight.same(&other.weight)
    }
}

// All this should be produced by a derive macro.
pub mod lenses {
    // Discussion: if the inner type were listed first, then
    // the capitalization wouldn't have to be twizzled.
    pub mod calc_state {
        use super::super::AppState;
        use super::super::{AppState, Master};
        use druid::Lens;
        use std::sync::Arc;
        pub struct Width;
        pub struct Weight;
        pub struct Masters;

        impl Lens<AppState, f64> for Width {
            fn get<'a>(&self, data: &'a AppState) -> &'a f64 {


@@ 66,6 85,29 @@ pub mod lenses {
                f(&mut data.weight)
            }
        }

        impl Lens<AppState, Arc<Vec<Master>>> for Masters {
            fn get<'a>(&self, data: &'a AppState) -> &'a Arc<Vec<Master>> {
                &data.masters
            }

            fn with_mut<V, F: FnOnce(&mut Arc<Vec<Master>>) -> V>(
                &self,
                data: &mut AppState,
                f: F,
            ) -> V {
                f(&mut data.masters)
            }
        }
    }
}

impl AppState {
    pub fn add_new_master(&mut self) {
        let mut masters = self.masters.deref().to_owned();
        masters.push(Master { weight: self.weight });
        self.masters = masters.into();
        println!("adding new master");
    }
}


M src/main.rs => src/main.rs +37 -14
@@ 1,44 1,67 @@
use druid::shell::{runloop, WindowBuilder};
use druid::widget::{ActionWrapper, Button, Column, DynLabel, Padding, Row, Slider};
use druid::{LensWrap, UiMain, UiState};
use druid::widget::{ActionWrapper, Button, Column, DynLabel, Padding, Row, Scroll, Slider};
use druid::{LensWrap, UiMain, UiState, Widget};

mod app_state;
mod interp_pane;
mod list;

use app_state::{lenses, AppState, InterpPt};
use app_state::{lenses, AppState, InterpPt, Master};
use interp_pane::InterpPane;
use list::List;

fn main() {
    druid::shell::init();
fn build_master_ui() -> impl Widget<Master> {
    // Discussion: we might want spacing as a separate param for the list widget.
    Padding::uniform(
        3.0,
        DynLabel::new(|data: &Master, _env| format!("weight {:.2}", data.weight)),
    )
}

    let mut run_loop = runloop::RunLoop::new();
    let mut builder = WindowBuilder::new();
fn build_ui() -> impl Widget<AppState> {
    let pane = InterpPane::default();
    let mut col = Column::new();
    let label = DynLabel::new(|data: &AppState, _env| format!("value: {}", data.width));
    let button = Button::new("increment");
    col.add_child(Padding::uniform(5.0, label), 1.0);
    col.add_child(Padding::uniform(5.0, button), 1.0);
    let label = DynLabel::new(|data: &AppState, _env| format!("weight: {:.2}", data.weight));
    let new_master_button = Button::new("New Master");
    col.add_child(Padding::uniform(5.0, label), 0.0);
    let new_master_button = ActionWrapper::new(new_master_button, |data: &mut AppState, _env| {
        data.add_new_master()
    });
    col.add_child(Padding::uniform(5.0, new_master_button), 0.0);
    col.add_child(
        Padding::uniform(
            5.0,
            LensWrap::new(Slider::default(), lenses::calc_state::Width),
            LensWrap::new(Slider::default(), lenses::calc_state::Weight),
        ),
        1.0,
    );
    col.add_child(
        Padding::uniform(
            5.0,
            LensWrap::new(Slider::default(), lenses::calc_state::Weight),
            LensWrap::new(Slider::default(), lenses::calc_state::Width),
        ),
        1.0,
    );
    col.add_child(
        Scroll::new(LensWrap::new(
            List::new(|| Box::new(build_master_ui())),
            lenses::calc_state::Masters,
        )),
        5.0,
    );
    let col = ActionWrapper::new(col, |data: &mut AppState, _env| data.width += 0.1);
    let mut row = Row::new();
    row.add_child(pane, 2.0);
    row.add_child(col, 1.0);
    let root = row;
    row
}

fn main() {
    druid::shell::init();

    let mut run_loop = runloop::RunLoop::new();
    let mut builder = WindowBuilder::new();
    let root = build_ui();

    let app_state = AppState::default();
    let state = UiState::new(root, app_state);