Skip to content

Commit

Permalink
Separate painting logic into its own function
Browse files Browse the repository at this point in the history
this will be useful for integrating the program into... say...
muni-overly >:)
  • Loading branch information
muni-corn committed Aug 1, 2023
1 parent 0b1cca0 commit 73dd7c3
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod audio;

use cpal::Stream;
use eframe::{
egui::{self, CentralPanel, Context, Vec2},
egui::{self, CentralPanel, Context, Ui, Vec2},
Frame,
};
use egui_extras::RetainedImage;
Expand Down Expand Up @@ -56,25 +56,33 @@ const SIZE: Vec2 = Vec2::new(300.0, 300.0);
const HALF_SPEAK_THRESHOLD_DBFS: f32 = -30.0;
const FULL_SPEAK_THRESHOLD_DBFS: f32 = -20.0;

impl MuniTuberApp {
fn paint(&mut self, ctx: &Context, ui: &mut Ui) {
let breath_value = self.start.elapsed().as_secs_f32().sin() / 75.0;
let breath_scale_x = 1.0 - breath_value;
let breath_scale_y = 1.0 + breath_value;

// determine head_base to use
let head_base = {
let volume = *self.audio_state.volume.lock().unwrap();
if volume > FULL_SPEAK_THRESHOLD_DBFS {
&self.full_speak
} else if volume > HALF_SPEAK_THRESHOLD_DBFS {
&self.half_speak
} else {
&self.quiet
}
};

head_base.show_size(ui, SIZE * Vec2::new(breath_scale_x, breath_scale_y));
}
}

impl eframe::App for MuniTuberApp {
fn update(&mut self, ctx: &Context, _frame: &mut Frame) {
CentralPanel::default().show(ctx, |ui| {
ui.with_layout(egui::Layout::bottom_up(egui::Align::Max), |ui| {
let breath_value = self.start.elapsed().as_secs_f32().sin() / 75.0;
let breath_scale_x = 1.0 - breath_value;
let breath_scale_y = 1.0 + breath_value;

let img = {
let volume = *self.audio_state.volume.lock().unwrap();
if volume > FULL_SPEAK_THRESHOLD_DBFS {
&self.full_speak
} else if volume > HALF_SPEAK_THRESHOLD_DBFS {
&self.half_speak
} else {
&self.quiet
}
};
img.show_size(ui, SIZE * Vec2::new(breath_scale_x, breath_scale_y))
self.paint(ctx, ui);
});
});
ctx.request_repaint();
Expand Down

0 comments on commit 73dd7c3

Please sign in to comment.