|
|
|
@ -10,6 +10,7 @@ pub struct Pane {
|
|
|
|
|
|
|
|
|
|
position: (i32, i32),
|
|
|
|
|
|
|
|
|
|
#[serde(skip)]
|
|
|
|
|
pub(crate) signals: Vec<(usize, usize)>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -56,6 +57,34 @@ impl Pane {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn init(&self, name: &str, world: &World) {
|
|
|
|
|
for index in 0..self.tiles.len() {
|
|
|
|
|
let mut tile = self
|
|
|
|
|
.tiles
|
|
|
|
|
.borrow_mut(index)
|
|
|
|
|
.unwrap_or_else(|| unreachable!());
|
|
|
|
|
let ctx = InitContext::new(
|
|
|
|
|
world,
|
|
|
|
|
(name.to_string(), index % self.width, index / self.width),
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
tile.init(ctx);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn init_signals(&mut self) {
|
|
|
|
|
let mut signals = Vec::new();
|
|
|
|
|
|
|
|
|
|
for (x, y, tile) in self.tiles_iter() {
|
|
|
|
|
if tile.signal().is_some() {
|
|
|
|
|
signals.push((x, y));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.signals = signals;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the width of the current pane
|
|
|
|
|
///
|
|
|
|
|
/// ## Example
|
|
|
|
@ -480,9 +509,13 @@ impl Pane {
|
|
|
|
|
commit.apply(self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn tiles(&self) -> &VecCell<FullTile> {
|
|
|
|
|
&self.tiles
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns an iterator over the tiles and their coordinates
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn tiles(&self) -> impl Iterator<Item = (usize, usize, VecRef<'_, FullTile>)> + '_ {
|
|
|
|
|
pub fn tiles_iter(&self) -> impl Iterator<Item = (usize, usize, VecRef<'_, FullTile>)> + '_ {
|
|
|
|
|
self.tiles
|
|
|
|
|
.iter()
|
|
|
|
|
.enumerate()
|
|
|
|
@ -492,7 +525,7 @@ impl Pane {
|
|
|
|
|
/// Draws the Pane at `(dx + self.position.0, dy + self.position.1)` on a [`TextSurface`].
|
|
|
|
|
/// Empty tiles will leave the `TextSurface` untouched, but tiles are free to modify the characters around them.
|
|
|
|
|
pub fn draw(&self, dx: i32, dy: i32, surface: &mut TextSurface) {
|
|
|
|
|
for (x, y, tile) in self.tiles() {
|
|
|
|
|
for (x, y, tile) in self.tiles_iter() {
|
|
|
|
|
let x = x as i32 + dx + self.position.0 as i32;
|
|
|
|
|
let y = y as i32 + dy + self.position.1 as i32;
|
|
|
|
|
|
|
|
|
@ -614,4 +647,41 @@ mod test {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_init() {
|
|
|
|
|
use crate::tile::Wire;
|
|
|
|
|
use std::collections::HashSet;
|
|
|
|
|
use Orientation::*;
|
|
|
|
|
|
|
|
|
|
let mut pane = test_tile_setup!(
|
|
|
|
|
2,
|
|
|
|
|
2,
|
|
|
|
|
[
|
|
|
|
|
Wire::new(Horizontal),
|
|
|
|
|
Wire::new(Vertical),
|
|
|
|
|
Wire::new(Any),
|
|
|
|
|
()
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assert_eq!(pane.signals, vec![]);
|
|
|
|
|
|
|
|
|
|
pane.get_mut((0, 0))
|
|
|
|
|
.unwrap()
|
|
|
|
|
.set_signal(Some(Signal::empty((0, 0), Direction::Right)));
|
|
|
|
|
pane.get_mut((1, 0))
|
|
|
|
|
.unwrap()
|
|
|
|
|
.set_signal(Some(Signal::empty((1, 0), Direction::Right)));
|
|
|
|
|
|
|
|
|
|
pane.init_signals();
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
pane.signals
|
|
|
|
|
.iter()
|
|
|
|
|
.copied()
|
|
|
|
|
.collect::<HashSet<(usize, usize)>>(),
|
|
|
|
|
HashSet::from_iter(vec![(0, 0), (1, 0)].into_iter())
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|