Skip to content

Commit f0befb4

Browse files
committed
editor support for dyn types
1 parent f59d870 commit f0befb4

File tree

8 files changed

+474
-11
lines changed

8 files changed

+474
-11
lines changed

editor/src/plugins/inspector/editors/dyntype.rs

Lines changed: 424 additions & 0 deletions
Large diffs are not rendered by default.

editor/src/plugins/inspector/editors/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1919
// SOFTWARE.
2020

21+
use crate::plugins::inspector::editors::dyntype::DynTypePropertyEditorDefinition;
2122
use crate::plugins::inspector::editors::shader::field::ShaderSourceCodeEditorDefinition;
2223
use crate::{
2324
fyrox::{
@@ -185,6 +186,7 @@ use crate::{
185186
use fyrox::resource::texture::TextureKind;
186187

187188
pub mod animation;
189+
pub mod dyntype;
188190
pub mod font;
189191
pub mod handle;
190192
pub mod resource;
@@ -632,6 +634,8 @@ pub fn make_property_editors_container(
632634

633635
container.insert(EnumPropertyEditorDefinition::<TextureKind>::new());
634636

637+
container.insert(DynTypePropertyEditorDefinition {});
638+
635639
reg_node_handle_editors!(
636640
container,
637641
sender,

editor/src/plugins/inspector/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ use crate::{
5656
utils::window_content,
5757
Editor, Message, WidgetMessage, WrapMode,
5858
};
59+
use fyrox::core::dyntype::DynTypeConstructorContainer;
5960
use fyrox::gui::style::resource::StyleResource;
6061
use fyrox::{
6162
core::type_traits::prelude::*,
@@ -81,6 +82,7 @@ pub struct AnimationDefinition {
8182
pub struct EditorEnvironment {
8283
pub resource_manager: ResourceManager,
8384
pub serialization_context: Arc<SerializationContext>,
85+
pub dyn_type_constructors: Arc<DynTypeConstructorContainer>,
8486
/// List of animations definitions (name + handle). It is filled only if current selection
8587
/// is `AnimationBlendingStateMachine`. The list is filled using ABSM's animation player.
8688
pub available_animations: Vec<AnimationDefinition>,
@@ -325,6 +327,7 @@ impl InspectorPlugin {
325327
ui: &mut UserInterface,
326328
resource_manager: ResourceManager,
327329
serialization_context: Arc<SerializationContext>,
330+
dyn_type_constructors: Arc<DynTypeConstructorContainer>,
328331
available_animations: &[AnimationDefinition],
329332
sender: &MessageSender,
330333
icon_request_sender: Sender<IconRequest>,
@@ -338,6 +341,7 @@ impl InspectorPlugin {
338341
sender: sender.clone(),
339342
icon_request_sender,
340343
style,
344+
dyn_type_constructors,
341345
});
342346

343347
let context = InspectorContext::from_object(InspectorContextArgs {
@@ -406,6 +410,7 @@ impl EditorPlugin for InspectorPlugin {
406410
ui,
407411
editor.engine.resource_manager.clone(),
408412
editor.engine.serialization_context.clone(),
413+
editor.engine.dyn_type_constructors.clone(),
409414
&available_animations,
410415
&editor.message_sender,
411416
editor.asset_browser.preview_sender.clone(),

editor/src/scene/settings/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ impl SceneSettingsWindow {
151151
let environment = Arc::new(EditorEnvironment {
152152
resource_manager: engine.resource_manager.clone(),
153153
serialization_context: engine.serialization_context.clone(),
154+
dyn_type_constructors: engine.dyn_type_constructors.clone(),
154155
available_animations: Default::default(),
155156
sender,
156157
icon_request_sender,

fyrox-core/src/dyntype.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
use crate::{reflect::prelude::*, type_traits::prelude::*, visitor::prelude::*, SafeLock};
2222
use fxhash::FxHashMap;
23-
use parking_lot::Mutex;
23+
use parking_lot::{Mutex, MutexGuard};
2424
use std::{
2525
any::{type_name, Any, TypeId},
2626
fmt::{Debug, Display, Formatter},
@@ -87,7 +87,7 @@ where
8787

8888
#[derive(Debug, TypeUuidProvider)]
8989
#[type_uuid(id = "87d9ef74-09a9-4228-a2d1-df270b50fddb")]
90-
pub struct DynTypeWrapper(Box<dyn DynType>);
90+
pub struct DynTypeWrapper(pub Box<dyn DynType>);
9191

9292
impl Clone for DynTypeWrapper {
9393
fn clone(&self) -> Self {
@@ -197,7 +197,7 @@ impl Reflect for DynTypeWrapper {
197197
}
198198

199199
#[derive(Default, Reflect, Clone, Debug)]
200-
pub struct DynTypeContainer(Option<DynTypeWrapper>);
200+
pub struct DynTypeContainer(pub Option<DynTypeWrapper>);
201201

202202
impl DynTypeContainer {
203203
pub fn value_ref(&self) -> Option<&dyn DynType> {
@@ -245,24 +245,36 @@ impl Visit for DynTypeContainer {
245245
}
246246
}
247247

248-
pub type DynTypeConstructor = Box<dyn Fn() -> Box<dyn DynType>>;
248+
pub type DynTypeConstructor = Box<dyn Fn() -> Box<dyn DynType> + Send + 'static>;
249249

250+
pub struct DynTypeConstructorDefinition {
251+
pub name: String,
252+
pub constructor: DynTypeConstructor,
253+
}
254+
255+
#[derive(Default)]
250256
pub struct DynTypeConstructorContainer {
251-
map: Mutex<FxHashMap<Uuid, DynTypeConstructor>>,
257+
map: Mutex<FxHashMap<Uuid, DynTypeConstructorDefinition>>,
252258
}
253259

254260
impl DynTypeConstructorContainer {
255261
pub fn try_create(&self, type_uuid: &Uuid) -> Option<Box<dyn DynType>> {
256-
self.map.safe_lock().get(type_uuid).map(|c| (*c)())
262+
self.map
263+
.safe_lock()
264+
.get(type_uuid)
265+
.map(|c| (*c.constructor)())
257266
}
258267

259-
pub fn add<T>(&self) -> &Self
268+
pub fn add<T>(&self, name: &str) -> &Self
260269
where
261270
T: TypeUuidProvider + Default + DynType,
262271
{
263272
let old = self.map.safe_lock().insert(
264273
<T as TypeUuidProvider>::type_uuid(),
265-
Box::new(|| Box::new(T::default())),
274+
DynTypeConstructorDefinition {
275+
name: name.to_string(),
276+
constructor: Box::new(|| Box::new(T::default())),
277+
},
266278
);
267279

268280
assert!(old.is_none());
@@ -273,7 +285,7 @@ impl DynTypeConstructorContainer {
273285
pub fn add_custom(
274286
&self,
275287
type_uuid: Uuid,
276-
constructor: DynTypeConstructor,
288+
constructor: DynTypeConstructorDefinition,
277289
) -> Result<(), String> {
278290
let mut map = self.map.safe_lock();
279291
let old = map.insert(type_uuid, constructor);
@@ -282,4 +294,8 @@ impl DynTypeConstructorContainer {
282294

283295
Ok(())
284296
}
297+
298+
pub fn inner(&self) -> MutexGuard<FxHashMap<Uuid, DynTypeConstructorDefinition>> {
299+
self.map.safe_lock()
300+
}
285301
}

fyrox-impl/src/engine/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ use crate::{
112112
};
113113
use fxhash::{FxHashMap, FxHashSet};
114114
use fyrox_animation::AnimationTracksData;
115+
use fyrox_core::dyntype::DynTypeConstructorContainer;
115116
use fyrox_graphics::server::SharedGraphicsServer;
116117
use fyrox_graphics_gl::server::GlGraphicsServer;
117118
use fyrox_sound::{
@@ -516,6 +517,10 @@ pub struct Engine {
516517
/// A container with widget constructors.
517518
pub widget_constructors: Arc<WidgetConstructorContainer>,
518519

520+
/// A container with constructors for dynamic types. See [`DynTypeConstructorContainer`] for more
521+
/// info.
522+
pub dyn_type_constructors: Arc<DynTypeConstructorContainer>,
523+
519524
/// Script processor is used to run script methods in a strict order.
520525
pub script_processor: ScriptProcessor,
521526
}
@@ -1546,6 +1551,7 @@ impl Engine {
15461551
plugins: Default::default(),
15471552
serialization_context,
15481553
widget_constructors,
1554+
dyn_type_constructors: Default::default(),
15491555
script_processor: Default::default(),
15501556
plugins_enabled: false,
15511557
elapsed_time: 0.0,
@@ -2620,6 +2626,7 @@ impl Engine {
26202626
fn register_plugin_internal(
26212627
serialization_context: &Arc<SerializationContext>,
26222628
widget_constructors: &Arc<WidgetConstructorContainer>,
2629+
dyn_type_constructors: &Arc<DynTypeConstructorContainer>,
26232630
resource_manager: &ResourceManager,
26242631
plugin: &dyn Plugin,
26252632
) {
@@ -2628,6 +2635,7 @@ impl Engine {
26282635
plugin.register(PluginRegistrationContext {
26292636
serialization_context,
26302637
widget_constructors,
2638+
dyn_type_constructors,
26312639
resource_manager,
26322640
}),
26332641
);
@@ -2637,6 +2645,7 @@ impl Engine {
26372645
Self::register_plugin_internal(
26382646
&self.serialization_context,
26392647
&self.widget_constructors,
2648+
&self.dyn_type_constructors,
26402649
&self.resource_manager,
26412650
plugin,
26422651
)
@@ -2862,6 +2871,7 @@ impl Engine {
28622871
Self::register_plugin_internal(
28632872
&self.serialization_context,
28642873
&self.widget_constructors,
2874+
&self.dyn_type_constructors,
28652875
&self.resource_manager,
28662876
plugin,
28672877
);

fyrox-impl/src/plugin/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use crate::{
4444
plugin::error::GameResult,
4545
scene::{Scene, SceneContainer},
4646
};
47+
use fyrox_core::dyntype::DynTypeConstructorContainer;
4748
use std::{
4849
ops::{Deref, DerefMut},
4950
path::Path,
@@ -120,6 +121,9 @@ pub struct PluginRegistrationContext<'a> {
120121
/// A reference to serialization context of the engine. See [`WidgetConstructorContainer`] for more
121122
/// info.
122123
pub widget_constructors: &'a Arc<WidgetConstructorContainer>,
124+
/// A container with constructors for dynamic types. See [`DynTypeConstructorContainer`] for more
125+
/// info.
126+
pub dyn_type_constructors: &'a Arc<DynTypeConstructorContainer>,
123127
/// A reference to the resource manager instance of the engine. Could be used to register resource loaders.
124128
pub resource_manager: &'a ResourceManager,
125129
}

fyrox-ui/src/inspector/editors/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::{
2929
button::Button,
3030
canvas::Canvas,
3131
core::{
32-
algebra::{UnitQuaternion, Vector2, Vector3, Vector4},
32+
algebra::{Matrix2, Matrix3, Matrix4, SMatrix, UnitQuaternion, Vector2, Vector3, Vector4},
3333
color::Color,
3434
color_gradient::ColorGradient,
3535
math::{curve::Curve, Rect, SmoothAngle},
@@ -112,7 +112,6 @@ use crate::{
112112
};
113113
use fxhash::FxHashMap;
114114
use fyrox_animation::machine::Parameter;
115-
use fyrox_core::algebra::{Matrix2, Matrix3, Matrix4, SMatrix};
116115
use fyrox_texture::TextureResource;
117116
use std::{
118117
any::{Any, TypeId},

0 commit comments

Comments
 (0)