Skip to content
This repository has been archived by the owner on Dec 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from ycysdf/inventory_example
Browse files Browse the repository at this point in the history
inventory example
  • Loading branch information
ycysdf authored Mar 10, 2024
2 parents a8f7420 + 1f17683 commit 1e73040
Show file tree
Hide file tree
Showing 44 changed files with 915 additions and 482 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ xy_reactive = { path = "crates/xy_reactive", default-features = false }
rxy_web_dom = { path = "crates/rxy_web_dom", default-features = false }

async-channel = { version = "2.1" }
async-broadcast = "0.7.0"
#event-listener = "5.2"
glam = "0.24"
hashbrown = "0.14"
indexmap = "2.1"
Expand Down Expand Up @@ -81,6 +83,7 @@ bevy_tasks = { version = "0.12", features = ["multi-threaded"], optional = false
bevy_text = { version = "0.12", optional = false }
bevy_transform = { version = "0.12", optional = false }
bevy_ui = { version = "0.12", features = ["bevy_text"] }
bevy_window = { version = "0.12"}
bevy_utils = "0.12"
bevy_a11y = "0.12"
bevy_input = "0.12"
Expand Down
2 changes: 1 addition & 1 deletion README-zh-Hans.md
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ fn ui() -> impl IntoView<BevyRenderer> {
)),
div().flex_col().gap(8).children((
"--Header--",
x_iter_source(source, |n| n.to_string()),
x_iter_source(source, |n,_| n.to_string()),
"--Footer--",
)),
)),
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ fn ui() -> impl IntoView<BevyRenderer> {
)),
div().flex_col().gap(8).children((
"--Header--",
x_iter_source(source, |n| n.to_string()),
x_iter_source(source, |n,_| n.to_string()),
"--Footer--",
)),
)),
Expand Down
Binary file added assets/items/0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/items/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/items/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/items/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/items/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 21 additions & 16 deletions crates/hooked_collection/src/hooked_vec.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::VecExt;
use alloc::collections::TryReserveError;
use alloc::vec::Vec;
use core::cmp::{Ord, Ordering};
Expand All @@ -9,21 +10,23 @@ pub trait HookVec {
type Item;

#[inline]
fn on_push<'a>(&'a mut self, _items: impl Iterator<Item = &'a Self::Item>) {}
fn on_push<'a>(&'a mut self, _items: impl Iterator<Item=&'a Self::Item>) {}
#[inline]
fn on_pop<'a>(&'a mut self, _items: impl Iterator<Item = &'a Self::Item>) {}
fn on_pop<'a>(&'a mut self, _items: impl Iterator<Item=&'a Self::Item>) {}
#[inline]
fn on_insert<'a>(&'a mut self, _index: usize, _items: impl Iterator<Item = &'a Self::Item>) {}
fn on_insert<'a>(&'a mut self, _index: usize, _items: impl Iterator<Item=&'a Self::Item>) {}
#[inline]
fn on_update(&mut self, _index: usize, _item: &Self::Item) {}
#[inline]
fn on_patch(&mut self, _index: usize) {}
#[inline]
fn on_remove<'a>(&'a mut self, _index: usize, _items: impl Iterator<Item = &'a Self::Item>) {}
fn on_remove<'a>(&'a mut self, _index: usize, _items: impl Iterator<Item=&'a Self::Item>) {}
#[inline]
fn on_clear(&mut self) {}
#[inline]
fn on_move(&mut self, _from: usize, _to: usize) {}
// #[inline]
// fn on_swap(&mut self, _from: usize, _to: usize) {}
}

pub struct HookedVec<T, O> {
Expand Down Expand Up @@ -86,8 +89,8 @@ impl<T, O> HookedVec<T, O> {
}

impl<T, I, O> Index<I> for HookedVec<T, O>
where
I: SliceIndex<[T], Output = T>,
where
I: SliceIndex<[T], Output=T>,
{
type Output = T;

Expand All @@ -105,19 +108,19 @@ impl<T, O> Deref for HookedVec<T, O> {
}

impl<T, O> Extend<T> for HookedVec<T, O>
where
O: HookVec<Item = T>,
where
O: HookVec<Item=T>,
{
fn extend<II: IntoIterator<Item = T>>(&mut self, iter: II) {
fn extend<II: IntoIterator<Item=T>>(&mut self, iter: II) {
let prev_len = self.vec.len();
self.vec.extend(iter);
self.observer.on_push(self.vec.iter().skip(prev_len));
}
}

impl<T, O> HookedVec<T, O>
where
O: HookVec<Item = T>,
where
O: HookVec<Item=T>,
{
pub fn push(&mut self, item: T) {
self.observer.on_push(once(&item));
Expand Down Expand Up @@ -162,8 +165,8 @@ where
}

pub fn resize(&mut self, new_len: usize, value: T)
where
T: Clone,
where
T: Clone,
{
let prev_len = self.vec.len();
let is_push = match new_len.cmp(&prev_len) {
Expand Down Expand Up @@ -208,7 +211,9 @@ where
self.vec.truncate(len);
}

// pub fn swap(&mut self, a: usize, b: usize) {
// self.data.swap(a, b);
// }
pub fn swap(&mut self, a: usize, b: usize) {
self.observer.on_update(a, &self.vec[b]);
self.observer.on_update(b, &self.vec[a]);
self.vec.swap(a, b);
}
}
70 changes: 43 additions & 27 deletions crates/hooked_collection/src/operation_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,84 +8,93 @@ pub type VecOperationRecord<T> = SmallVec<[VecOperation<T>; 2]>;
pub type MapOperationRecord<K, T> = SmallVec<[MapOperation<K, T>; 2]>;

pub struct HookFn<F, M>(pub F, PhantomData<M>);

pub type BoxedHookFn<T> = HookFn<Box<dyn FnMut(VecOperation<&T>)>, T>;

pub fn hook_fn<F, T>(f: F) -> HookFn<F, T>
where
F: FnMut(VecOperation<&T>),
where
F: FnMut(VecOperation<&T>),
{
HookFn(f, PhantomData)
}

impl<T, F> HookVec for HookFn<F, T>
where
F: FnMut(VecOperation<&T>),
where
F: FnMut(VecOperation<&T>),
{
type Item = T;
fn on_push<'a>(&'a mut self, _items: impl Iterator<Item = &'a Self::Item>) {
fn on_push<'a>(&'a mut self, _items: impl Iterator<Item=&'a Self::Item>) {
for item in _items {
(self.0)(VecOperation::Push { item });
self.0(VecOperation::Push { item });
}
}

fn on_pop<'a>(&'a mut self, _items: impl Iterator<Item = &'a Self::Item>) {
(self.0)(VecOperation::Pop);
fn on_pop<'a>(&'a mut self, _items: impl Iterator<Item=&'a Self::Item>) {
self.0(VecOperation::Pop);
}

fn on_insert<'a>(&'a mut self, _index: usize, _items: impl Iterator<Item = &'a Self::Item>) {
fn on_insert<'a>(&'a mut self, _index: usize, _items: impl Iterator<Item=&'a Self::Item>) {
for item in _items {
(self.0)(VecOperation::Insert {
self.0(VecOperation::Insert {
index: _index,
item,
});
}
}

fn on_update(&mut self, _index: usize, item: &Self::Item) {
(self.0)(VecOperation::Update {
self.0(VecOperation::Update {
index: _index,
item,
});
}

fn on_remove<'a>(&'a mut self, _index: usize, _items: impl Iterator<Item = &'a Self::Item>) {
fn on_patch(&mut self, _index: usize) {
self.0(VecOperation::Patch { index: _index });
}

fn on_remove<'a>(&'a mut self, _index: usize, _items: impl Iterator<Item=&'a Self::Item>) {
for _ in _items {
(self.0)(VecOperation::Remove { index: _index });
self.0(VecOperation::Remove { index: _index });
}
}

fn on_clear(&mut self) {
(self.0)(VecOperation::Clear);
self.0(VecOperation::Clear);
}

fn on_move(&mut self, _from: usize, _to: usize) {
(self.0)(VecOperation::Move {
self.0(VecOperation::Move {
from: _from,
to: _to,
});
}

fn on_patch(&mut self, _index: usize) {
(self.0)(VecOperation::Patch { index: _index });
}
// fn on_swap(&mut self, _from: usize, _to: usize) {
// self.0(VecOperation::Swap {
// from: _from,
// to: _to,
// });
// }
}

impl<T> HookVec for VecOperationRecord<T>
where
T: Clone,
where
T: Clone,
{
type Item = T;

fn on_push<'a>(&'a mut self, _items: impl Iterator<Item = &'a Self::Item>) {
fn on_push<'a>(&'a mut self, _items: impl Iterator<Item=&'a Self::Item>) {
for item in _items {
self.push(VecOperation::Push { item: item.clone() });
}
}

fn on_pop<'a>(&'a mut self, _items: impl Iterator<Item = &'a Self::Item>) {
fn on_pop<'a>(&'a mut self, _items: impl Iterator<Item=&'a Self::Item>) {
self.push(VecOperation::Pop);
}

fn on_insert<'a>(&'a mut self, _index: usize, _items: impl Iterator<Item = &'a Self::Item>) {
fn on_insert<'a>(&'a mut self, _index: usize, _items: impl Iterator<Item=&'a Self::Item>) {
for item in _items {
self.push(VecOperation::Insert {
index: _index,
Expand All @@ -101,7 +110,7 @@ where
});
}

fn on_remove<'a>(&'a mut self, _index: usize, _items: impl Iterator<Item = &'a Self::Item>) {
fn on_remove<'a>(&'a mut self, _index: usize, _items: impl Iterator<Item=&'a Self::Item>) {
for _ in _items {
self.push(VecOperation::Remove { index: _index });
}
Expand All @@ -121,12 +130,19 @@ where
fn on_patch(&mut self, _index: usize) {
self.push(VecOperation::Patch { index: _index });
}

// fn on_swap(&mut self, _from: usize, _to: usize) {
// self.push(VecOperation::Swap {
// from: _from,
// to: _to,
// });
// }
}

impl<K, T> HookMap for MapOperationRecord<K, T>
where
T: Clone,
K: Clone,
where
T: Clone,
K: Clone,
{
type Key = K;
type Value = T;
Expand Down
28 changes: 18 additions & 10 deletions crates/hooked_collection/src/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ use alloc::vec;
use async_channel::Sender;

impl<T> HookVec for Sender<VecOperation<T>>
where
T: Clone,
where
T: Clone,
{
type Item = T;

fn on_push<'a>(&'a mut self, items: impl Iterator<Item = &'a Self::Item>) {
fn on_push<'a>(&'a mut self, items: impl Iterator<Item=&'a Self::Item>) {
for item in items {
let _ = self.send_blocking(VecOperation::Push { item: item.clone() });
}
}

fn on_pop<'a>(&'a mut self, _items: impl Iterator<Item = &'a Self::Item>) {
fn on_pop<'a>(&'a mut self, _items: impl Iterator<Item=&'a Self::Item>) {
let _ = self.send_blocking(VecOperation::Pop);
}
fn on_insert<'a>(&'a mut self, index: usize, items: impl Iterator<Item = &'a Self::Item>) {
fn on_insert<'a>(&'a mut self, index: usize, items: impl Iterator<Item=&'a Self::Item>) {
for item in items {
let _ = self.send_blocking(VecOperation::Insert {
index,
Expand All @@ -31,7 +31,7 @@ where
item: item.clone(),
});
}
fn on_remove<'a>(&'a mut self, index: usize, items: impl Iterator<Item = &'a Self::Item>) {
fn on_remove<'a>(&'a mut self, index: usize, items: impl Iterator<Item=&'a Self::Item>) {
for _ in items {
let _ = self.send_blocking(VecOperation::Remove { index });
}
Expand All @@ -42,12 +42,20 @@ where
fn on_move(&mut self, from: usize, to: usize) {
let _ = self.send_blocking(VecOperation::Move { from, to });
}

// fn on_swap(&mut self, from: usize, to: usize) {
// let _ = self.send_blocking(VecOperation::Swap { from, to });
// }

fn on_patch(&mut self, index: usize) {
let _ = self.send_blocking(VecOperation::Patch { index });
}
}

impl<K, V> HookMap for Sender<MapOperation<K, V>>
where
K: Clone,
V: Clone,
where
K: Clone,
V: Clone,
{
type Key = K;
type Value = V;
Expand Down Expand Up @@ -110,7 +118,7 @@ mod tests {
receiver.try_recv().unwrap(),
VecOperation::Move {
from: pre_len,
to: 0
to: 0,
}
);
vec.update(0, 5);
Expand Down
Loading

0 comments on commit 1e73040

Please sign in to comment.