From 75de75100ce04d528112c7581472c0cca2c9cfc1 Mon Sep 17 00:00:00 2001 From: Dmitry Stepanov Date: Tue, 23 Jul 2024 10:18:04 +0300 Subject: [PATCH] rect fill for tile map --- fyrox-impl/src/scene/tilemap/brush.rs | 15 +++++++++++++++ fyrox-impl/src/scene/tilemap/mod.rs | 23 +++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/fyrox-impl/src/scene/tilemap/brush.rs b/fyrox-impl/src/scene/tilemap/brush.rs index d8ec49429..51ab41653 100644 --- a/fyrox-impl/src/scene/tilemap/brush.rs +++ b/fyrox-impl/src/scene/tilemap/brush.rs @@ -18,6 +18,7 @@ use crate::{ }, scene::{debug::SceneDrawingContext, tilemap::tileset::TileDefinitionHandle}, }; +use fyrox_core::math::Rect; use std::{ any::Any, error::Error, @@ -118,6 +119,20 @@ pub struct TileMapBrush { } impl TileMapBrush { + /// Returns bounding rectangle of the tile map brush in grid coordinates. + #[inline] + pub fn bounding_rect(&self) -> Rect { + let mut min = Vector2::repeat(i32::MAX); + let mut max = Vector2::repeat(i32::MIN); + + for tile in self.tiles.iter() { + min = tile.local_position.inf(&min); + max = tile.local_position.sup(&max); + } + + Rect::from_points(min, max) + } + /// Draw brush outline to the scene drawing context. pub fn draw_outline( &self, diff --git a/fyrox-impl/src/scene/tilemap/mod.rs b/fyrox-impl/src/scene/tilemap/mod.rs index 566c28731..3911c76c3 100644 --- a/fyrox-impl/src/scene/tilemap/mod.rs +++ b/fyrox-impl/src/scene/tilemap/mod.rs @@ -314,6 +314,29 @@ impl TileMap { } } } + + /// Fills the given rectangle using the specified brush. + #[inline] + pub fn rect_fill(&mut self, rect: Rect, brush: &TileMapBrush) { + let brush_rect = brush.bounding_rect(); + for y in + (rect.position.y..(rect.position.y + rect.size.y)).step_by(brush_rect.size.y as usize) + { + for x in (rect.position.x..(rect.position.x + rect.size.x)) + .step_by(brush_rect.size.x as usize) + { + for brush_tile in brush.tiles.iter() { + let position = Vector2::new(x, y) + brush_tile.local_position; + if rect.contains(position) { + self.insert_tile(Tile { + position, + definition_handle: brush_tile.definition_handle, + }); + } + } + } + } + } } impl Default for TileMap {