Skip to content

Commit

Permalink
remove image crate
Browse files Browse the repository at this point in the history
  • Loading branch information
eye-wave committed Jun 19, 2024
1 parent 46d4578 commit 967a7af
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ crate-type = ["cdylib","lib"]

[dependencies]
atomic_float = "0.1"
image = { version = "0.24.8", default-features = false, features = ["png"] }
lazy_static = "1.4.0"
# Remove the `assert_process_allocs` feature to allow allocations on the audio
# thread in debug builds.
nih_plug = { git = "https://github.com/robbert-vdh/nih-plug.git", features = ["assert_process_allocs", "standalone"] }
Expand All @@ -27,6 +27,12 @@ nih_plug_vizia = { git = "https://github.com/robbert-vdh/nih-plug.git" }
rand = "0.8.5"
rand_pcg = "0.3.1"

[profile.dev]
incremental = true
debug = 1
opt-level = 1
codegen-units = 16

[profile.release]
lto = "thin"
strip = "symbols"
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ ifeq ($(UNAME), Darwin)
endif

preview:
RUSTC_WRAPPER=sccache
cargo watch -x run

dev:
RUSTC_WRAPPER=sccache
cargo watch -x "xtask bundle $(PROJECT_NAME)" -s "make post-build"

build:
cargo xtask bundle $(PROJECT_NAME) --release
$(MAKE) post-build

post-build:
@mkdir -p $(VST3_PATH)
Expand Down
22 changes: 1 addition & 21 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::sync::Arc;

use crate::OneXOscParams;

mod icons;
mod osc_buttons;

#[derive(Lens, Clone)]
Expand All @@ -20,21 +21,6 @@ pub(crate) fn default_state() -> Arc<ViziaState> {
ViziaState::new(|| (300, 100))
}

macro_rules! load_image {
($cx: expr,$path1:expr, $path2:expr) => {
$cx.load_image(
$path1,
image::load_from_memory_with_format(include_bytes!($path2), image::ImageFormat::Png)
.unwrap(),
ImageRetentionPolicy::DropWhenUnusedForOneFrame,
);
};

($cx: expr, $path1:expr, @ $path2:expr) => {
load_image!($cx, $path1, concat!("./editor/assets/", $path2));
};
}

pub(crate) fn create(
params: Arc<OneXOscParams>,
editor_state: Arc<ViziaState>,
Expand All @@ -43,12 +29,6 @@ pub(crate) fn create(
assets::register_noto_sans_light(cx);
assets::register_noto_sans_thin(cx);

load_image!(cx,"sine-wave.png",@ "MdiSineWave.png");
load_image!(cx,"triangle-wave.png",@ "MdiTriangleWave.png");
load_image!(cx,"square-wave.png",@ "MdiSquareWave.png");
load_image!(cx,"saw-wave.png",@ "MdiSawtoothWave.png");
load_image!(cx,"noise.png",@ "MingcuteRandomFill.png");

cx.add_stylesheet(include_style!("src/editor/theme.css"))
.ok();

Expand Down
Binary file removed src/editor/assets/MdiSawtoothWave.png
Binary file not shown.
Binary file removed src/editor/assets/MdiSineWave.png
Binary file not shown.
Binary file removed src/editor/assets/MdiSquareWave.png
Binary file not shown.
Binary file removed src/editor/assets/MdiTriangleWave.png
Binary file not shown.
Binary file removed src/editor/assets/MingcuteRandomFill.png
Binary file not shown.
97 changes: 97 additions & 0 deletions src/editor/icons.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use lazy_static::lazy_static;
use nih_plug_vizia::vizia::prelude::*;
use nih_plug_vizia::vizia::vg;
use rand::thread_rng;
use rand::Rng;
use std::f32::consts::PI;

#[derive(Debug, Clone)]
pub enum Point {
Line(f32, f32),
}

pub struct Icon {
points: Vec<Point>,
}

impl View for Icon {
fn draw(&self, cx: &mut DrawContext, canvas: &mut Canvas) {
let bg_color: vg::Color = cx.background_color().into();
let stroke_color: vg::Color = cx.font_color().into();

let margin = 16.0;
let bounds = cx.bounds();

let x = bounds.x + margin / 2.0;
let y = bounds.y + margin / 2.0;
let w = bounds.w - margin;
let h = bounds.h - margin;

let mut path = vg::Path::new();
path.rect(x, y, w, h);
path.close();

let paint = vg::Paint::color(bg_color);
canvas.fill_path(&path, &paint);

let mut path = vg::Path::new();
path.move_to(x, y + h / 2.0);
for point in self.points.iter() {
match *point {
Point::Line(px, py) => path.line_to(x + px * w, y + py * h),
}
}

if let Some(Point::Line(px, py)) = self.points.last() {
path.move_to(x + px * w, y + py * h);
}

path.close();

let paint = vg::Paint::color(stroke_color);
canvas.stroke_path(&path, &paint);
}
}

impl Icon {
pub fn new(cx: &mut Context, points: Vec<Point>) -> Handle<Self> {
Self { points }.build(cx, |_| {})
}
}

lazy_static! {
pub static ref SINE_ICON_POINTS: Vec<Point> = (0..50)
.map(|n| {
Point::Line(
(n as f32) / 50.0,
1.0 - (((n as f32) / 25.0 * PI).sin() / 2.0 + 0.5),
)
})
.collect();
pub static ref TRIANGLE_ICON_POINTS: Vec<Point> = vec![
Point::Line(0.25, 0.0),
Point::Line(0.75, 1.0),
Point::Line(1.0, 0.5),
];
pub static ref SQUARE_ICON_POINTS: Vec<Point> = vec![
Point::Line(0.0, 0.0),
Point::Line(0.5, 0.0),
Point::Line(0.5, 1.0),
Point::Line(1.0, 1.0),
Point::Line(1.0, 0.5),
];
pub static ref SAW_ICON_POINTS: Vec<Point> = vec![
Point::Line(0.5, 0.0),
Point::Line(0.5, 1.0),
Point::Line(1.0, 0.5),
];
pub static ref NOISE_ICON_POINTS: Vec<Point> = (0..25)
.map(|n| Point::Line((n as f32) / 25.0, {
if n % 2 == 0 {
thread_rng().gen_range(0.0..0.5)
} else {
thread_rng().gen_range(0.5..1.0)
}
}))
.collect();
}
37 changes: 24 additions & 13 deletions src/editor/osc_buttons.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use nih_plug::params::{EnumParam, Param};
use nih_plug_vizia::{vizia::prelude::*, widgets::param_base::ParamWidgetBase};

use super::icons::{
Icon, Point, NOISE_ICON_POINTS, SAW_ICON_POINTS, SINE_ICON_POINTS, SQUARE_ICON_POINTS,
TRIANGLE_ICON_POINTS,
};
use crate::model::osc::OscillatorType;

enum RadioEvent {
Expand Down Expand Up @@ -47,20 +51,27 @@ impl BtnGroup {
.build(cx, |cx| {
HStack::new(cx, |cx| {
Binding::new(cx, Self::current_state, |cx, state| {
[
(OscillatorType::Sine, "sine-wave.png"),
(OscillatorType::Triangle, "triangle-wave.png"),
(OscillatorType::Square, "square-wave.png"),
(OscillatorType::Sawtooth, "saw-wave.png"),
(OscillatorType::Noise, "noise.png"),
]
.iter()
.for_each(|(variant, src)| {
Image::new(cx, *src)
.on_mouse_up(|cx, _| cx.emit(RadioEvent::Set(*variant)))
.checked(state.map(|v| v.same(variant)))
([
(OscillatorType::Sine, SINE_ICON_POINTS.to_vec()),
(OscillatorType::Triangle, TRIANGLE_ICON_POINTS.to_vec()),
(OscillatorType::Square, SQUARE_ICON_POINTS.to_vec()),
(OscillatorType::Sawtooth, SAW_ICON_POINTS.to_vec()),
(OscillatorType::Noise, NOISE_ICON_POINTS.to_vec()),
] as [(OscillatorType, Vec<Point>); 5])
.iter()
.for_each(|(variant, points)| {
let variant = *variant;
let points = points.clone();

HStack::new(cx, move |cx| {
Icon::new(cx, points.clone());
})
.width(Pixels(48.0))
.height(Pixels(48.0))
.on_mouse_up(move |cx, _| cx.emit(RadioEvent::Set(variant)))
.checked(state.map(move |v| v.same(&variant)))
.class("osc-btn");
});
});
});
});
})
Expand Down
11 changes: 6 additions & 5 deletions src/editor/theme.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@
background-color: #333;
}

image.osc-btn {
hstack.osc-btn {
border-radius: 100%;
background-color: #444;
background-size: cover;
width: 48px;
height: 48px;
width: 56px;
height: 56px;
border: 4px solid #404040;

top: 20px;
right: 10px;
}

image.osc-btn:nth-child(1) {
hstack.osc-btn:nth-child(1) {
left: 10px;
}

image.osc-btn:checked {
hstack.osc-btn:checked {
background-color: #888;
border: 4px solid #aa8;
}

0 comments on commit 967a7af

Please sign in to comment.