1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
use std::collections::HashMap;
use std::collections::hash_map::Entry as HashMapEntry;
use ecs::Component;
use crate::asset::Id as AssetId;
/// Renderer object ID.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Id
{
Asset(AssetId),
Other(u64),
}
/// Renderer object store.
#[derive(Debug, Default, Component)]
pub struct Store
{
objects: HashMap<Id, Object>,
}
impl Store
{
pub fn get_obj(&self, id: &Id) -> Option<&Object>
{
self.objects.get(id)
}
pub fn get_texture_obj(&self, id: &Id) -> Option<&Object>
{
let obj = self.get_obj(id)?;
if !matches!(obj.kind(), Kind::Texture) {
return None;
}
Some(obj)
}
pub fn contains_with_id(&self, id: &Id) -> bool
{
self.objects.contains_key(id)
}
pub fn insert(&mut self, id: Id, object: Object)
{
self.objects.insert(id, object);
}
pub fn entry(&mut self, id: Id) -> StoreEntry<'_>
{
StoreEntry { inner: self.objects.entry(id) }
}
}
#[derive(Debug)]
pub struct StoreEntry<'store>
{
inner: HashMapEntry<'store, Id, Object>,
}
impl<'store> StoreEntry<'store>
{
pub fn or_insert(self, default_obj: Object) -> &'store mut Object
{
self.inner.or_insert(default_obj)
}
pub fn or_insert_with(
self,
default_func: impl FnOnce() -> Object,
) -> &'store mut Object
{
self.inner.or_insert_with(default_func)
}
}
/// Renderer object.
#[derive(Debug, Clone)]
pub struct Object
{
raw: u32,
kind: Kind,
}
impl Object
{
pub fn from_raw(raw: u32, kind: Kind) -> Self
{
Self { raw, kind }
}
pub fn as_raw(&self) -> u32
{
self.raw
}
pub fn kind(&self) -> Kind
{
self.kind
}
}
/// Renderer object kind.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum Kind
{
Texture,
Mesh,
}
|