Skip to content

Commit 4b94e65

Browse files
committed
fix(q): updating the data module for
#4145
1 parent 55b1c5e commit 4b94e65

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

packages/rust/q/src/data/cache.rs

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
use godot::prelude::*;
2+
use papaya::HashMap;
3+
use godot::classes::{ Texture2D, CanvasLayer, Control, AudioStream };
4+
use std::sync::Arc;
5+
use std::marker::PhantomData;
6+
use crate::data::shader_data::ShaderCache;
7+
8+
pub struct ResourceCache<T: GodotClass> {
9+
map: HashMap<String, Gd<T>>,
10+
_marker: PhantomData<T>,
11+
}
12+
13+
impl<T: GodotClass> ResourceCache<T> {
14+
pub fn new() -> Self {
15+
Self {
16+
map: HashMap::new(),
17+
_marker: PhantomData,
18+
}
19+
}
20+
21+
pub fn insert(&self, key: &str, object: Gd<T>) {
22+
self.map.pin().insert(key.to_string(), object);
23+
}
24+
25+
pub fn get(&self, key: &str) -> Option<Gd<T>> {
26+
self.map.pin().get(&key.to_string()).cloned()
27+
}
28+
29+
pub fn get_arc(&self, key: &str) -> Option<Arc<Gd<T>>> {
30+
self.map
31+
.pin()
32+
.get(&key.to_string())
33+
.map(|gd| Arc::new(gd.clone()))
34+
}
35+
36+
pub fn contains(&self, key: &str) -> bool {
37+
self.map.pin().contains_key(&key.to_string())
38+
}
39+
40+
pub fn insert_upcast<U>(&self, key: &str, object: Gd<U>) where U: Inherits<T> + GodotClass {
41+
self.insert(key, object.upcast::<T>());
42+
}
43+
44+
pub fn get_as<U>(&self, key: &str) -> Option<Gd<U>> where U: Inherits<T> + GodotClass {
45+
self.get(key)?.try_cast::<U>().ok()
46+
}
47+
48+
pub fn remove(&self, key: &str) -> Option<Gd<T>> {
49+
self.map.pin().remove(&key.to_string()).cloned()
50+
}
51+
}
52+
53+
#[derive(GodotClass)]
54+
#[class(base = Node)]
55+
pub struct CacheManager {
56+
base: Base<Node>,
57+
texture_cache: ResourceCache<Texture2D>,
58+
canvas_layer_cache: ResourceCache<CanvasLayer>,
59+
ui_cache: ResourceCache<Control>,
60+
audio_cache: ResourceCache<AudioStream>,
61+
shader_cache: Gd<ShaderCache>,
62+
}
63+
64+
#[godot_api]
65+
impl INode for CacheManager {
66+
fn init(base: Base<Self::Base>) -> Self {
67+
let shader_cache = Gd::from_init_fn(|base| ShaderCache::init(base));
68+
69+
Self {
70+
base,
71+
texture_cache: ResourceCache::new(),
72+
canvas_layer_cache: ResourceCache::new(),
73+
ui_cache: ResourceCache::new(),
74+
audio_cache: ResourceCache::new(),
75+
shader_cache,
76+
}
77+
}
78+
79+
fn ready(&mut self) {
80+
let shader_cache = self.shader_cache.clone();
81+
82+
{
83+
let mut base = self.base_mut();
84+
base.add_child(&shader_cache.upcast::<Node>());
85+
}
86+
}
87+
}
88+
89+
#[godot_api]
90+
impl CacheManager {
91+
fn internal_canvas_layer_cache(&self) -> &ResourceCache<CanvasLayer> {
92+
&self.canvas_layer_cache
93+
}
94+
95+
fn internal_ui_cache(&self) -> &ResourceCache<Control> {
96+
&self.ui_cache
97+
}
98+
99+
fn internal_texture_cache(&self) -> &ResourceCache<Texture2D> {
100+
&self.texture_cache
101+
}
102+
103+
pub fn internal_audio_cache(&self) -> &ResourceCache<AudioStream> {
104+
&self.audio_cache
105+
}
106+
107+
fn internal_shader_cache(&self) -> &Gd<ShaderCache> {
108+
&self.shader_cache
109+
}
110+
111+
fn internal_shader_cache_as_node(&self) -> Gd<Node> {
112+
self.shader_cache.clone().upcast::<Node>()
113+
}
114+
115+
#[func]
116+
fn get_from_canvas_layer_cache(&self, key: GString) -> Option<Gd<CanvasLayer>> {
117+
self.canvas_layer_cache.get(key.to_string().as_str())
118+
}
119+
120+
#[func]
121+
fn get_from_ui_cache(&self, key: GString) -> Option<Gd<Control>> {
122+
self.ui_cache.get(key.to_string().as_str())
123+
}
124+
125+
#[func]
126+
fn get_from_texture_cache(&self, key: GString) -> Option<Gd<Texture2D>> {
127+
self.texture_cache.get(key.to_string().as_str())
128+
}
129+
130+
#[func]
131+
fn get_from_audio_cache(&self, key: GString) -> Option<Gd<AudioStream>> {
132+
self.audio_cache.get(key.to_string().as_str())
133+
}
134+
135+
#[func]
136+
fn obtain_shader_cache(&self) -> Gd<ShaderCache> {
137+
self.shader_cache.clone()
138+
}
139+
}

packages/rust/q/src/data/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub mod uxui_data;
2+
pub mod gui_data;
3+
pub mod user_data;
4+
pub mod abstract_data_map;
5+
pub mod shader_data;
6+
pub mod cache;

0 commit comments

Comments
 (0)