Skip to content

Commit

Permalink
DrawingContext::push_grid
Browse files Browse the repository at this point in the history
  • Loading branch information
mrDIMAS committed Jul 6, 2024
1 parent dee1459 commit 45ab5b1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
48 changes: 7 additions & 41 deletions editor/src/absm/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::fyrox::{
core::{
algebra::{Matrix3, Point2, Vector2},
color::Color,
math::{round_to_step, Rect},
math::Rect,
pool::Handle,
reflect::prelude::*,
type_traits::prelude::*,
Expand Down Expand Up @@ -309,56 +309,22 @@ uuid_provider!(AbsmCanvas = "100b1c33-d017-4fe6-95e7-e1daf310ef27");

impl Control for AbsmCanvas {
fn draw(&self, ctx: &mut DrawingContext) {
let size = 9999.0;
let grid_size = 9999.0;

let local_bounds = self
let grid_bounds = self
.widget
.bounding_rect()
.inflate(size, size)
.translate(Vector2::new(size * 0.5, size * 0.5));
DrawingContext::push_rect_filled(ctx, &local_bounds, None);
.inflate(grid_size, grid_size)
.translate(Vector2::new(grid_size * 0.5, grid_size * 0.5));
ctx.push_rect_filled(&grid_bounds, None);
ctx.commit(
self.clip_bounds(),
self.widget.background(),
CommandTexture::None,
None,
);
let step_size = 50.0;

let mut local_left_bottom = local_bounds.left_top_corner();
local_left_bottom.x = round_to_step(local_left_bottom.x, step_size);
local_left_bottom.y = round_to_step(local_left_bottom.y, step_size);

let mut local_right_top = local_bounds.right_bottom_corner();
local_right_top.x = round_to_step(local_right_top.x, step_size);
local_right_top.y = round_to_step(local_right_top.y, step_size);

let w = (local_right_top.x - local_left_bottom.x).abs();
let h = (local_right_top.y - local_left_bottom.y).abs();

let nw = ((w / step_size).ceil()) as usize;
let nh = ((h / step_size).ceil()) as usize;

for ny in 0..=nh {
let k = ny as f32 / (nh) as f32;
let y = local_left_bottom.y + k * h;
ctx.push_line(
Vector2::new(local_left_bottom.x - step_size, y),
Vector2::new(local_right_top.x + step_size, y),
1.0 / self.zoom,
);
}

for nx in 0..=nw {
let k = nx as f32 / (nw) as f32;
let x = local_left_bottom.x + k * w;
ctx.push_line(
Vector2::new(x, local_left_bottom.y + step_size),
Vector2::new(x, local_right_top.y - step_size),
1.0 / self.zoom,
);
}

ctx.push_grid(self.zoom, Vector2::repeat(50.0), grid_bounds);
ctx.commit(
self.clip_bounds(),
Brush::Solid(Color::opaque(60, 60, 60)),
Expand Down
37 changes: 37 additions & 0 deletions fyrox-ui/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
Thickness,
};
use bytemuck::{Pod, Zeroable};
use fyrox_core::math::round_to_step;
use fyrox_resource::untyped::UntypedResource;
use std::ops::Range;

Expand Down Expand Up @@ -665,6 +666,42 @@ pub trait Draw {
prev = next;
}
}

fn push_grid(&mut self, zoom: f32, cell_size: Vector2<f32>, grid_bounds: Rect<f32>) {
let mut local_left_bottom = grid_bounds.left_top_corner();
local_left_bottom.x = round_to_step(local_left_bottom.x, cell_size.x);
local_left_bottom.y = round_to_step(local_left_bottom.y, cell_size.y);

let mut local_right_top = grid_bounds.right_bottom_corner();
local_right_top.x = round_to_step(local_right_top.x, cell_size.x);
local_right_top.y = round_to_step(local_right_top.y, cell_size.y);

let w = (local_right_top.x - local_left_bottom.x).abs();
let h = (local_right_top.y - local_left_bottom.y).abs();

let nw = ((w / cell_size.x).ceil()) as usize;
let nh = ((h / cell_size.y).ceil()) as usize;

for ny in 0..=nh {
let k = ny as f32 / (nh) as f32;
let y = local_left_bottom.y + k * h;
self.push_line(
Vector2::new(local_left_bottom.x - cell_size.x, y),
Vector2::new(local_right_top.x + cell_size.x, y),
1.0 / zoom,
);
}

for nx in 0..=nw {
let k = nx as f32 / (nw) as f32;
let x = local_left_bottom.x + k * w;
self.push_line(
Vector2::new(x, local_left_bottom.y + cell_size.y),
Vector2::new(x, local_right_top.y - cell_size.y),
1.0 / zoom,
);
}
}
}

#[derive(Clone, Debug)]
Expand Down

0 comments on commit 45ab5b1

Please sign in to comment.