You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
1.9 KiB

  1. #![allow(dead_code)]
  2. use hibitset::BitSetLike;
  3. use shrev::EventChannel;
  4. use specs::{
  5. storage::{TryDefault, UnprotectedStorage},
  6. world::Index,
  7. Component, DenseVecStorage,
  8. };
  9. pub struct FlaggedStorage<C, T = DenseVecStorage<C>>
  10. where
  11. C: Send + Sync + 'static,
  12. {
  13. channel: EventChannel<ComponentEvent<C>>,
  14. storage: T,
  15. }
  16. pub enum ComponentEvent<C>
  17. where
  18. C: Send + Sync + 'static,
  19. {
  20. Inserted(Index),
  21. Modified(Index, C),
  22. Removed(Index, C),
  23. }
  24. impl<C, T> FlaggedStorage<C, T>
  25. where
  26. C: Send + Sync + 'static,
  27. {
  28. pub fn channel(&self) -> &EventChannel<ComponentEvent<C>> {
  29. &self.channel
  30. }
  31. pub fn channel_mut(&mut self) -> &mut EventChannel<ComponentEvent<C>> {
  32. &mut self.channel
  33. }
  34. }
  35. impl<C, T> Default for FlaggedStorage<C, T>
  36. where
  37. C: Send + Sync + 'static,
  38. T: TryDefault,
  39. {
  40. fn default() -> Self {
  41. FlaggedStorage {
  42. channel: EventChannel::new(),
  43. storage: T::unwrap_default(),
  44. }
  45. }
  46. }
  47. impl<C: Component + Clone, T: UnprotectedStorage<C>> UnprotectedStorage<C> for FlaggedStorage<C, T>
  48. where
  49. C: Send + Sync + 'static,
  50. {
  51. unsafe fn clean<B>(&mut self, has: B)
  52. where
  53. B: BitSetLike,
  54. {
  55. self.storage.clean(has);
  56. }
  57. unsafe fn get(&self, id: Index) -> &C {
  58. self.storage.get(id)
  59. }
  60. unsafe fn get_mut(&mut self, id: Index) -> &mut C {
  61. let ret = self.storage.get_mut(id);
  62. self.channel
  63. .single_write(ComponentEvent::Modified(id, ret.clone()));
  64. ret
  65. }
  66. unsafe fn insert(&mut self, id: Index, comp: C) {
  67. self.storage.insert(id, comp);
  68. self.channel.single_write(ComponentEvent::Inserted(id));
  69. }
  70. unsafe fn remove(&mut self, id: Index) -> C {
  71. let c = self.storage.remove(id);
  72. self.channel
  73. .single_write(ComponentEvent::Removed(id, c.clone()));
  74. c
  75. }
  76. }