bug fix and better rendering
This commit is contained in:
+2
-1
@@ -4,7 +4,8 @@ pub struct Rect;
|
||||
|
||||
impl Element for Rect {
|
||||
fn render(&mut self, scope: &mut crate::render::RenderScope) {
|
||||
scope.draw_rect(300, 300, 0xffffff);
|
||||
let (w, h) = scope.get_size();
|
||||
scope.draw_rect(0, 0, w, h, 0xffffff);
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn std::any::Any {
|
||||
|
||||
+13
-2
@@ -61,8 +61,19 @@ impl Screen {
|
||||
let (w, h) = self.window.get_size();
|
||||
let mut scope = RenderScope::new(w, h);
|
||||
|
||||
for e in &self.widgets {
|
||||
e.0.lock().unwrap().render(&mut scope);
|
||||
for elem in &self.widgets {
|
||||
scope.clear();
|
||||
if let Some(t) = elem.get() {
|
||||
scope.set_transform(&t);
|
||||
}
|
||||
|
||||
self.render_extension(elem.clone()).unwrap();
|
||||
elem.0.lock().unwrap().render(&mut scope);
|
||||
|
||||
if let Some(t) = elem.get() {
|
||||
scope.set_transform(&t);
|
||||
}
|
||||
|
||||
scope.draw();
|
||||
}
|
||||
|
||||
|
||||
+4
-1
@@ -2,6 +2,7 @@ use minifb::{Window, WindowOptions};
|
||||
use osgui::{
|
||||
elements::Rect,
|
||||
extensions::velocity::{Velocity, VelocityExtension},
|
||||
style::Transform,
|
||||
Screen,
|
||||
};
|
||||
|
||||
@@ -20,7 +21,9 @@ fn main() {
|
||||
let mut app = Screen::new(window);
|
||||
app.extension(VelocityExtension);
|
||||
|
||||
app.draw(Rect).component(Velocity(100, 0));
|
||||
app.draw(Rect)
|
||||
.component(Transform::new().dimensions(40, 40))
|
||||
.component(Velocity(500, 0));
|
||||
|
||||
app.run().unwrap();
|
||||
}
|
||||
|
||||
+10
-13
@@ -2,7 +2,7 @@ use crate::style::{RawTransform, Transform};
|
||||
|
||||
enum RenderMethod {
|
||||
// Text(String),
|
||||
Rectangle(usize, usize, u32),
|
||||
Rectangle(usize, usize, usize, usize, u32),
|
||||
}
|
||||
|
||||
pub struct RenderScope {
|
||||
@@ -29,16 +29,9 @@ impl RenderScope {
|
||||
transform.use_position(self.parent_width, self.parent_height, &mut self.transform);
|
||||
}
|
||||
|
||||
// pub fn draw_text(&mut self, text: &str) {
|
||||
// self.render_stack.push(RenderMethod::Text(text.to_string()));
|
||||
// let (w, h) = utils::str_size(text);
|
||||
// self.transform.width = self.transform.width.max(w);
|
||||
// self.transform.height = self.transform.height.max(h);
|
||||
// }
|
||||
|
||||
pub fn draw_rect(&mut self, width: usize, height: usize, color: u32) {
|
||||
pub fn draw_rect(&mut self, x: usize, y: usize, width: usize, height: usize, color: u32) {
|
||||
self.render_stack
|
||||
.push(RenderMethod::Rectangle(width, height, color));
|
||||
.push(RenderMethod::Rectangle(x, y, width, height, color));
|
||||
self.transform.width = self.transform.width.max(width);
|
||||
self.transform.height = self.transform.height.max(height);
|
||||
}
|
||||
@@ -46,9 +39,12 @@ impl RenderScope {
|
||||
pub fn draw(&mut self) {
|
||||
for m in &self.render_stack {
|
||||
match m {
|
||||
&RenderMethod::Rectangle(width, height, color) => {
|
||||
for y in 0..height {
|
||||
for x in 0..width {
|
||||
&RenderMethod::Rectangle(pos_x, pos_y, width, height, color) => {
|
||||
for x in self.transform.x + pos_x..self.transform.x + pos_x + width {
|
||||
for y in self.transform.y + pos_y..self.transform.y + pos_y + height {
|
||||
if self.buffer.len() > y * self.parent_height + x
|
||||
&& self.parent_width > x
|
||||
{
|
||||
self.buffer[y * self.parent_height + x] = color;
|
||||
}
|
||||
}
|
||||
@@ -56,6 +52,7 @@ impl RenderScope {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
self.render_stack.clear();
|
||||
|
||||
Reference in New Issue
Block a user