View render
This commit is contained in:
+19
-11
@@ -1,9 +1,9 @@
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use crate::{prelude::Context, DrawContext, View};
|
||||
use crate::{prelude::Context, render::Area, DrawContext, View};
|
||||
|
||||
pub trait Engine {
|
||||
fn render_view(&self, ctx: &DrawContext, view: &View);
|
||||
fn render_view(&self, area: &Area, view: &View) -> DrawContext;
|
||||
fn draw_context(&self, ctx: &DrawContext);
|
||||
}
|
||||
|
||||
@@ -37,10 +37,15 @@ impl Console {
|
||||
|
||||
loop {
|
||||
let (width, height) = crossterm::terminal::size().unwrap();
|
||||
let mut ctx = DrawContext::new(width, height);
|
||||
|
||||
self.render_view(&mut ctx, &cx.get_view());
|
||||
self.draw_context(&ctx);
|
||||
self.draw_context(&self.render_view(
|
||||
&Area {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width,
|
||||
height,
|
||||
},
|
||||
&cx.get_view(),
|
||||
));
|
||||
|
||||
std::thread::sleep(std::time::Duration::from_millis(16));
|
||||
}
|
||||
@@ -48,17 +53,20 @@ impl Console {
|
||||
}
|
||||
|
||||
impl Engine for Console {
|
||||
fn render_view(&self, ctx: &DrawContext, view: &View) {
|
||||
let mut context = DrawContext::new(ctx.allocated.width, ctx.allocated.height);
|
||||
fn render_view(&self, area: &Area, view: &View) -> DrawContext {
|
||||
let mut context = DrawContext::new(area.clone());
|
||||
view(&mut context);
|
||||
context
|
||||
}
|
||||
|
||||
fn draw_context(&self, ctx: &DrawContext) {
|
||||
for inst in &ctx.drawing {
|
||||
match inst {
|
||||
crate::render::DrawInstruction::Text(point, text) => println!("{text}"),
|
||||
crate::render::DrawInstruction::Child(point, child) => self.draw_context(child),
|
||||
crate::render::DrawInstruction::View(point, view) => self.render_view(ctx, view),
|
||||
crate::render::DrawInstruction::Text(_point, text) => println!("{text}"),
|
||||
crate::render::DrawInstruction::Child(_point, child) => self.draw_context(child),
|
||||
crate::render::DrawInstruction::View(area, view) => {
|
||||
self.draw_context(&self.render_view(area, view))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+14
-9
@@ -3,7 +3,7 @@ use crate::View;
|
||||
#[derive(Clone)]
|
||||
pub enum DrawInstruction {
|
||||
Text(Point, String),
|
||||
View(Point, View),
|
||||
View(Area, View),
|
||||
Child(Point, DrawContext),
|
||||
}
|
||||
|
||||
@@ -19,23 +19,29 @@ pub struct Point {
|
||||
pub y: u16,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Area {
|
||||
pub x: u16,
|
||||
pub y: u16,
|
||||
pub width: u16,
|
||||
pub height: u16,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DrawContext {
|
||||
pub available: Size,
|
||||
pub area: Area,
|
||||
pub allocated: Size,
|
||||
pub parent: Point,
|
||||
pub drawing: Vec<DrawInstruction>,
|
||||
}
|
||||
|
||||
impl DrawContext {
|
||||
pub fn new(width: u16, height: u16) -> Self {
|
||||
pub fn new(area: Area) -> Self {
|
||||
Self {
|
||||
available: Size { width, height },
|
||||
area,
|
||||
allocated: Size {
|
||||
width: 0,
|
||||
height: 0,
|
||||
},
|
||||
parent: Point { x: 0, y: 0 },
|
||||
drawing: Vec::new(),
|
||||
}
|
||||
}
|
||||
@@ -54,8 +60,7 @@ impl DrawContext {
|
||||
.push(DrawInstruction::Text(point, text.to_string()));
|
||||
}
|
||||
|
||||
pub fn draw_view(&mut self, point: Point, view: View) {
|
||||
let ctx = DrawContext::new(self.allocated.width, self.allocated.height);
|
||||
self.drawing.push(DrawInstruction::Child(point, ctx));
|
||||
pub fn draw_view(&mut self, area: Area, view: View) {
|
||||
self.drawing.push(DrawInstruction::View(area, view));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user