working center
This commit is contained in:
+1
-1
@@ -13,7 +13,7 @@ fn main() {
|
|||||||
|
|
||||||
screen
|
screen
|
||||||
.draw("Hello, World".to_string())
|
.draw("Hello, World".to_string())
|
||||||
.component(Transform::new());
|
.component(Transform::center());
|
||||||
|
|
||||||
screen.run(&mut events, &mut extensions).unwrap();
|
screen.run(&mut events, &mut extensions).unwrap();
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-6
@@ -10,8 +10,6 @@ enum RenderMethod {
|
|||||||
pub struct RenderScope {
|
pub struct RenderScope {
|
||||||
transform: RawTransform,
|
transform: RawTransform,
|
||||||
render_stack: Vec<RenderMethod>,
|
render_stack: Vec<RenderMethod>,
|
||||||
width: u16,
|
|
||||||
height: u16,
|
|
||||||
parent_width: u16,
|
parent_width: u16,
|
||||||
parent_height: u16,
|
parent_height: u16,
|
||||||
}
|
}
|
||||||
@@ -21,22 +19,22 @@ impl RenderScope {
|
|||||||
RenderScope {
|
RenderScope {
|
||||||
transform: RawTransform::new(),
|
transform: RawTransform::new(),
|
||||||
render_stack: Vec::new(),
|
render_stack: Vec::new(),
|
||||||
width: 0,
|
|
||||||
height: 0,
|
|
||||||
parent_width: 0,
|
parent_width: 0,
|
||||||
parent_height: 0,
|
parent_height: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_transform(&mut self, transform: &Transform) {
|
pub fn set_transform(&mut self, transform: &Transform) {
|
||||||
|
(self.parent_width, self.parent_height) = crossterm::terminal::size().unwrap();
|
||||||
transform.use_dimensions(&mut self.transform);
|
transform.use_dimensions(&mut self.transform);
|
||||||
|
transform.use_position(self.parent_width, self.parent_height, &mut self.transform);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw_text(&mut self, text: &str) {
|
pub fn draw_text(&mut self, text: &str) {
|
||||||
self.render_stack.push(RenderMethod::Text(text.to_string()));
|
self.render_stack.push(RenderMethod::Text(text.to_string()));
|
||||||
let (w, h) = utils::str_size(text);
|
let (w, h) = utils::str_size(text);
|
||||||
self.width = std::cmp::max(self.width, w);
|
self.transform.width = self.transform.width.max(w);
|
||||||
self.height = std::cmp::max(self.height, h);
|
self.transform.height = self.transform.height.max(h);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(&self) {
|
pub fn draw(&self) {
|
||||||
|
|||||||
+16
-2
@@ -58,6 +58,11 @@ impl Transform {
|
|||||||
self.width.use_dimension(&mut raw.width);
|
self.width.use_dimension(&mut raw.width);
|
||||||
self.height.use_dimension(&mut raw.height);
|
self.height.use_dimension(&mut raw.height);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn use_position(&self, parent_width: u16, parent_height: u16, raw: &mut RawTransform) {
|
||||||
|
self.x.use_position(raw.width, parent_width, &mut raw.x);
|
||||||
|
self.y.use_position(raw.height, parent_height, &mut raw.y);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RawTransform {
|
impl RawTransform {
|
||||||
@@ -74,8 +79,17 @@ impl RawTransform {
|
|||||||
impl Dimension {
|
impl Dimension {
|
||||||
pub fn use_dimension(&self, r: &mut u16) {
|
pub fn use_dimension(&self, r: &mut u16) {
|
||||||
match self {
|
match self {
|
||||||
Dimension::Auto => {}
|
Self::Auto => {}
|
||||||
Dimension::Const(n) => *r = *n,
|
Self::Const(n) => *r = *n,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Position {
|
||||||
|
pub fn use_position(&self, size: u16, parent: u16, r: &mut u16) {
|
||||||
|
match self {
|
||||||
|
Self::Center => *r = (parent - size) / 2,
|
||||||
|
Self::Const(n) => *r = *n,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user