pub struct GridEngine { /* private fields */ }Expand description
The main engine for managing a 2D grid system.
GridEngine provides functionality for:
- Adding items to specific grid positions
- Moving items while handling collisions
- Removing items from the grid
- Tracking changes through an event system
- Expand the grid dynamically on the y axis
When items collide during placement or movement, the engine automatically repositions affected items to prevent overlapping, the default is to move the collided items down, increasing their y axis.
Implementations§
Source§impl GridEngine
impl GridEngine
Sourcepub fn new(rows: usize, cols: usize) -> GridEngine
pub fn new(rows: usize, cols: usize) -> GridEngine
Creates a new GridEngine with specified dimensions.
§Arguments
rows- Initial number of rows in the gridcols- Initial number of columns in the grid
§Example
use grid_engine::grid_engine::GridEngine;
let grid = GridEngine::new(10, 10); // Creates a 10x10 gridExamples found in repository?
53fn main() {
54 println!("Grid App");
55
56 let mut grid = GridEngine::new(10, 12);
57
58 grid.events_mut().add_changes_listener(Box::new(|event| {
59 println!("Event triggered: {:#?}", event);
60 }));
61
62 grid.add_item("a".to_string(), 2, 2, 2, 4).unwrap();
63 print_grid(&grid);
64 grid.add_item("b".to_string(), 4, 2, 2, 4).unwrap();
65 print_grid(&grid);
66 grid.add_item("c".to_string(), 0, 2, 2, 2).unwrap();
67 print_grid(&grid);
68 grid.remove_item("b").unwrap();
69 print_grid(&grid);
70 grid.move_item("a", 1, 0).unwrap();
71 print_grid(&grid);
72}Sourcepub fn get_nodes(&self) -> Vec<Node>
pub fn get_nodes(&self) -> Vec<Node>
Get the nodes sorted by id
§Example
use grid_engine::grid_engine::GridEngine;
let mut grid = GridEngine::new(10, 10);
grid.add_item("b".to_string(), 0, 0, 2, 2).unwrap();
grid.add_item("a".to_string(), 0, 2, 2, 2).unwrap();
let nodes = grid.get_nodes();
assert_eq!(nodes.len(), 2);
assert_eq!(nodes[0].id(), "a");
assert_eq!(nodes[1].id(), "b");Sourcepub fn get_inner_grid(&self) -> &InnerGrid
pub fn get_inner_grid(&self) -> &InnerGrid
Gets a reference to the underlying grid structure.
This provides access to the raw grid data for inspection purposes. Note that modifications should be made through GridEngine’s public methods rather than directly manipulating the inner grid.
§Returns
A reference to the InnerGrid instance
§Example
use grid_engine::grid_engine::GridEngine;
use std::error::Error;
let grid = GridEngine::new(10, 10);
let inner_grid = grid.get_inner_grid();
assert_eq!(inner_grid.rows(), 10);
assert_eq!(inner_grid.cols(), 10);Examples found in repository?
22fn print_grid(grid: &GridEngine) {
23 let mut grid_str_formatted = String::new();
24 grid_str_formatted.push_str(" ");
25 for i in 0..grid.get_inner_grid().cols() {
26 grid_str_formatted.push_str(&format!(" {} ", i));
27 }
28 grid_str_formatted.push('\n');
29
30 grid.get_inner_grid()
31 .iter_rows()
32 .enumerate()
33 .for_each(|(row_number, row)| {
34 row.enumerate().for_each(|(index, cell)| {
35 if index == 0 {
36 grid_str_formatted.push_str(&format!("{:0>2}", row_number));
37 }
38 match cell {
39 Some(item) => {
40 grid_str_formatted.push_str(&format!("[{}]", item));
41 }
42 None => {
43 grid_str_formatted.push_str(&format!("[{}]", " "));
44 }
45 };
46 });
47 grid_str_formatted.push('\n');
48 });
49
50 println!("{}", grid_str_formatted);
51}Sourcepub fn add_item(
&mut self,
id: String,
x: usize,
y: usize,
w: usize,
h: usize,
) -> Result<&Node, GridEngineError>
pub fn add_item( &mut self, id: String, x: usize, y: usize, w: usize, h: usize, ) -> Result<&Node, GridEngineError>
Adds an item to the grid at the specified position.
If the new item would collide with existing items, those items are automatically repositioned to avoid overlap.
§Arguments
id- Unique identifier for the itemx- X coordinate (column) for item placementy- Y coordinate (row) for item placementw- Width of the item in grid cellsh- Height of the item in grid cells
§Returns
Ok(&Node)- Reference to the newly added nodeErr(GridEngineError)- If item already exists or placement fails
§Example
use grid_engine::grid_engine::GridEngine;
use std::error::Error;
let mut grid = GridEngine::new(10, 10);
grid.add_item("box1".to_string(), 0, 0, 2, 2)?; // 2x2 item at top-left
// Check if the item was added correctly
let item = grid.get_nodes();
assert_eq!(item.len(), 1);
assert_eq!(item[0].id(), "box1");
Examples found in repository?
53fn main() {
54 println!("Grid App");
55
56 let mut grid = GridEngine::new(10, 12);
57
58 grid.events_mut().add_changes_listener(Box::new(|event| {
59 println!("Event triggered: {:#?}", event);
60 }));
61
62 grid.add_item("a".to_string(), 2, 2, 2, 4).unwrap();
63 print_grid(&grid);
64 grid.add_item("b".to_string(), 4, 2, 2, 4).unwrap();
65 print_grid(&grid);
66 grid.add_item("c".to_string(), 0, 2, 2, 2).unwrap();
67 print_grid(&grid);
68 grid.remove_item("b").unwrap();
69 print_grid(&grid);
70 grid.move_item("a", 1, 0).unwrap();
71 print_grid(&grid);
72}Sourcepub fn remove_item(&mut self, id: &str) -> Result<Node, GridEngineError>
pub fn remove_item(&mut self, id: &str) -> Result<Node, GridEngineError>
Removes an item from the grid by its ID.
§Arguments
id- ID of the item to remove
§Returns
Ok(Node)- The removed nodeErr(GridEngineError)- If item doesn’t exist
§Example
use grid_engine::grid_engine::GridEngine;
use std::error::Error;
let mut grid = GridEngine::new(10, 10);
grid.add_item("box1".to_string(), 0, 0, 2, 2)?;
grid.remove_item("box1")?; // Removes the item
Examples found in repository?
53fn main() {
54 println!("Grid App");
55
56 let mut grid = GridEngine::new(10, 12);
57
58 grid.events_mut().add_changes_listener(Box::new(|event| {
59 println!("Event triggered: {:#?}", event);
60 }));
61
62 grid.add_item("a".to_string(), 2, 2, 2, 4).unwrap();
63 print_grid(&grid);
64 grid.add_item("b".to_string(), 4, 2, 2, 4).unwrap();
65 print_grid(&grid);
66 grid.add_item("c".to_string(), 0, 2, 2, 2).unwrap();
67 print_grid(&grid);
68 grid.remove_item("b").unwrap();
69 print_grid(&grid);
70 grid.move_item("a", 1, 0).unwrap();
71 print_grid(&grid);
72}Sourcepub fn move_item(
&mut self,
id: &str,
new_x: usize,
new_y: usize,
) -> Result<(), GridEngineError>
pub fn move_item( &mut self, id: &str, new_x: usize, new_y: usize, ) -> Result<(), GridEngineError>
Moves an existing item to a new position in the grid.
If the move would cause collisions, affected items are automatically repositioned to prevent overlap.
§Arguments
id- ID of the item to movenew_x- New X coordinatenew_y- New Y coordinate
§Returns
Ok(())- If move successfulErr(GridEngineError)- If item doesn’t exist or move invalid
§Example
use grid_engine::grid_engine::GridEngine;
let mut grid = GridEngine::new(10, 10);
grid.add_item("box1".to_string(), 0, 0, 2, 2)?;
grid.move_item("box1", 2, 2)?; // Moves box to position 2,2
// Check if the item was moved correctly
let item = grid.get_nodes();
assert_eq!(item.len(), 1);
assert_eq!(item[0].x(), &2);
assert_eq!(item[0].y(), &2);
Examples found in repository?
53fn main() {
54 println!("Grid App");
55
56 let mut grid = GridEngine::new(10, 12);
57
58 grid.events_mut().add_changes_listener(Box::new(|event| {
59 println!("Event triggered: {:#?}", event);
60 }));
61
62 grid.add_item("a".to_string(), 2, 2, 2, 4).unwrap();
63 print_grid(&grid);
64 grid.add_item("b".to_string(), 4, 2, 2, 4).unwrap();
65 print_grid(&grid);
66 grid.add_item("c".to_string(), 0, 2, 2, 2).unwrap();
67 print_grid(&grid);
68 grid.remove_item("b").unwrap();
69 print_grid(&grid);
70 grid.move_item("a", 1, 0).unwrap();
71 print_grid(&grid);
72}Sourcepub fn events_mut(&mut self) -> &mut GridEvents
pub fn events_mut(&mut self) -> &mut GridEvents
Returns a mutable reference to the grid events system.
Examples found in repository?
53fn main() {
54 println!("Grid App");
55
56 let mut grid = GridEngine::new(10, 12);
57
58 grid.events_mut().add_changes_listener(Box::new(|event| {
59 println!("Event triggered: {:#?}", event);
60 }));
61
62 grid.add_item("a".to_string(), 2, 2, 2, 4).unwrap();
63 print_grid(&grid);
64 grid.add_item("b".to_string(), 4, 2, 2, 4).unwrap();
65 print_grid(&grid);
66 grid.add_item("c".to_string(), 0, 2, 2, 2).unwrap();
67 print_grid(&grid);
68 grid.remove_item("b").unwrap();
69 print_grid(&grid);
70 grid.move_item("a", 1, 0).unwrap();
71 print_grid(&grid);
72}