Engine
This commit is contained in:
+5
-14
@@ -12,23 +12,14 @@ impl Event for KeyPress {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let cx = Context::new(app);
|
let console = Console::new();
|
||||||
cx.refresh();
|
|
||||||
|
|
||||||
std::thread::spawn({
|
console.thread(move |ctx| loop {
|
||||||
let cx = cx.clone();
|
crossterm::event::read().unwrap();
|
||||||
move || loop {
|
ctx.emit_event_threaded(&KeyPress);
|
||||||
crossterm::event::read().unwrap();
|
|
||||||
cx.emit_event_threaded(&KeyPress);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let (width, height) = crossterm::terminal::size().unwrap();
|
console.run(app);
|
||||||
|
|
||||||
loop {
|
|
||||||
cx.get_view()(&mut DrawContext::new(width, height));
|
|
||||||
std::thread::sleep(std::time::Duration::from_millis(16));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn app(cx: &Arc<Context>) -> View {
|
fn app(cx: &Arc<Context>) -> View {
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
|
use crate::{prelude::Context, DrawContext, View};
|
||||||
|
|
||||||
|
pub trait Engine {
|
||||||
|
fn render(&self, ctx: &mut DrawContext, view: View);
|
||||||
|
fn draw_text(&self, ctx: &mut DrawContext, text: &str);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Console {
|
||||||
|
threads: Mutex<Vec<Arc<dyn Fn(Arc<Context>) + Send + Sync>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Console {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
threads: Mutex::new(Vec::new()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn thread<F: Fn(Arc<Context>) + Send + Sync + 'static>(&self, run: F) {
|
||||||
|
self.threads.lock().unwrap().push(Arc::new(run));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(&self, component: F) {
|
||||||
|
let cx = Context::new(component);
|
||||||
|
cx.refresh();
|
||||||
|
|
||||||
|
for thread in self.threads.lock().unwrap().iter() {
|
||||||
|
let thread = thread.clone();
|
||||||
|
|
||||||
|
std::thread::spawn({
|
||||||
|
let cx = cx.clone();
|
||||||
|
move || thread(cx)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let (width, height) = crossterm::terminal::size().unwrap();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
cx.get_view()(&mut DrawContext::new(width, height));
|
||||||
|
std::thread::sleep(std::time::Duration::from_millis(16));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Engine for Console {
|
||||||
|
fn render(&self, ctx: &mut DrawContext, view: View) {
|
||||||
|
view(&mut DrawContext::new(
|
||||||
|
ctx.available.width,
|
||||||
|
ctx.available.height,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw_text(&self, _ctx: &mut DrawContext, text: &str) {
|
||||||
|
println!("{}", text);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub mod component;
|
pub mod component;
|
||||||
|
pub mod engine;
|
||||||
pub mod state;
|
pub mod state;
|
||||||
|
|
||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
pub use crate::component::*;
|
pub use crate::component::*;
|
||||||
|
pub use crate::engine::*;
|
||||||
pub use crate::state::*;
|
pub use crate::state::*;
|
||||||
pub use crate::{DrawContext, View};
|
pub use crate::{DrawContext, View};
|
||||||
}
|
}
|
||||||
@@ -23,6 +25,9 @@ pub struct DrawContext {
|
|||||||
used: Size,
|
used: Size,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct RenderContext {}
|
||||||
|
|
||||||
impl DrawContext {
|
impl DrawContext {
|
||||||
pub fn new(width: u16, height: u16) -> Self {
|
pub fn new(width: u16, height: u16) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
|||||||
Reference in New Issue
Block a user