Signed-off-by: Slendi <slendi@socopon.com>
This commit is contained in:
64
src/main.rs
64
src/main.rs
@@ -1,4 +1,5 @@
|
||||
use std::{
|
||||
io::Write,
|
||||
sync::mpsc::{self, Receiver, Sender},
|
||||
thread::{self},
|
||||
time::Duration,
|
||||
@@ -6,7 +7,7 @@ use std::{
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use midly::Smf;
|
||||
use notan::{draw::*, prelude::*};
|
||||
use raylib::prelude::*;
|
||||
use serialport::SerialPortType;
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
@@ -164,29 +165,23 @@ pub enum SerialStatus {
|
||||
SerialDisconnected,
|
||||
}
|
||||
|
||||
#[derive(AppState)]
|
||||
struct SingerApp {
|
||||
song_state: Option<AppSongState>,
|
||||
tx: Sender<SerialCommand>,
|
||||
rx: Receiver<SerialStatus>,
|
||||
connected: Option<String>,
|
||||
font: Font,
|
||||
}
|
||||
|
||||
impl SingerApp {
|
||||
pub fn new(gfx: &mut Graphics) -> Self {
|
||||
pub fn new() -> Self {
|
||||
let (tx_cmd, rx_cmd) = mpsc::channel();
|
||||
let (tx_status, rx_status) = mpsc::channel();
|
||||
Self::spawn_serial_worker(rx_cmd, tx_status);
|
||||
let font = gfx
|
||||
.create_font(include_bytes!("assets/Ubuntu-B.ttf"))
|
||||
.expect("failed to load embedded font");
|
||||
Self {
|
||||
song_state: None,
|
||||
tx: tx_cmd,
|
||||
rx: rx_status,
|
||||
connected: None,
|
||||
font,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -250,53 +245,40 @@ impl SingerApp {
|
||||
}
|
||||
}
|
||||
|
||||
fn draw(&mut self, gfx: &mut Graphics) {
|
||||
let mut draw = gfx.create_draw();
|
||||
draw.clear(Color::WHITE);
|
||||
|
||||
draw.line((40.0, 40.0), (100.0, 200.0))
|
||||
.width(15.0)
|
||||
.color(Color::BLUE);
|
||||
draw.rect((220.0, 100.0), (120.0, 60.0)).color(Color::GREEN);
|
||||
fn draw(&mut self, draw: &mut RaylibDrawHandle<'_>) {
|
||||
draw.clear_background(Color::WHITE);
|
||||
draw.draw_line_ex(
|
||||
Vector2::new(40.0, 40.0),
|
||||
Vector2::new(100.0, 200.0),
|
||||
15.0,
|
||||
Color::BLUE,
|
||||
);
|
||||
draw.draw_rectangle(220, 100, 120, 60, Color::GREEN);
|
||||
|
||||
let status_color = if self.connected.is_some() {
|
||||
Color::GREEN
|
||||
} else {
|
||||
Color::GRAY
|
||||
};
|
||||
draw.rect((14.0, 14.0), (20.0, 20.0)).color(status_color);
|
||||
draw.draw_rectangle(14, 14, 20, 20, status_color);
|
||||
|
||||
let status_text = match &self.connected {
|
||||
Some(name) => format!("Serial connected: {name}"),
|
||||
None => "Serial disconnected".to_string(),
|
||||
};
|
||||
draw.text(&self.font, &status_text)
|
||||
.position(44.0, 30.0)
|
||||
.size(22.0)
|
||||
.color(Color::BLACK);
|
||||
|
||||
gfx.render(&draw);
|
||||
draw.draw_text(&status_text, 44, 14, 22, Color::BLACK);
|
||||
}
|
||||
}
|
||||
|
||||
fn setup(gfx: &mut Graphics) -> SingerApp {
|
||||
SingerApp::new(gfx)
|
||||
}
|
||||
fn main() {
|
||||
let (mut rl, thread) = raylib::init().size(1280, 720).title("singer").build();
|
||||
rl.toggle_fullscreen();
|
||||
|
||||
fn update(_app: &mut notan::app::App, state: &mut SingerApp) {
|
||||
state.update();
|
||||
}
|
||||
let mut app = SingerApp::new();
|
||||
|
||||
fn draw(gfx: &mut Graphics, state: &mut SingerApp) {
|
||||
state.draw(gfx);
|
||||
}
|
||||
|
||||
#[notan_main]
|
||||
fn main() -> Result<(), String> {
|
||||
notan::init_with(setup)
|
||||
.add_config(WindowConfig::new().set_title("singer").set_fullscreen(true))
|
||||
.add_config(DrawConfig)
|
||||
.update(update)
|
||||
.draw(draw)
|
||||
.build()
|
||||
while !rl.window_should_close() {
|
||||
app.update();
|
||||
let mut draw = rl.begin_drawing(&thread);
|
||||
app.draw(&mut draw);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user