-
-
Notifications
You must be signed in to change notification settings - Fork 259
Register custom icons in class macro #1407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,13 @@ where | |
| Self::class_id() | ||
| } | ||
|
|
||
| /// Class icon resource path. Will use this icon if available. | ||
| /// | ||
| /// You can also set an icon via `#[class(icon = "path/to/icon.svg")]`. | ||
| fn icon() -> &'static str { | ||
| "" | ||
| } | ||
|
Comment on lines
+47
to
+52
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not 100% sure if this should be in the builder.class::<MyClass>()
.icon("path/to/icon.svg")
.internal()
.build()But if we decide
|
||
|
|
||
| /// Initialization level, during which this class should be initialized with Godot. | ||
| /// | ||
| /// The default is a good choice in most cases; override only if you have very specific initialization requirements. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,6 +60,12 @@ fn global_dyn_traits_by_typeid() -> GlobalGuard<'static, HashMap<any::TypeId, Ve | |
| /// Besides the name, this type holds information relevant for the deregistration of the class. | ||
| pub struct LoadedClass { | ||
| name: ClassId, | ||
|
|
||
| // Class icon needs to be retained for registered for class lifetime, this is not accessed directly. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Grammar? Not clear what you mean. |
||
| #[cfg(since_api = "4.4")] | ||
| #[allow(unused)] | ||
| icon: crate::builtin::GString, | ||
|
|
||
| is_editor_plugin: bool, | ||
| } | ||
|
|
||
|
|
@@ -97,6 +103,8 @@ struct ClassRegistrationInfo { | |
| /// Godot low-level class creation parameters. | ||
| godot_params: GodotCreationInfo, | ||
|
|
||
| icon_path: crate::builtin::GString, | ||
|
|
||
| #[allow(dead_code)] // Currently unused; may be useful for diagnostics in the future. | ||
| init_level: InitLevel, | ||
| is_editor_plugin: bool, | ||
|
|
@@ -149,6 +157,7 @@ pub(crate) fn register_class< | |
| out!("Manually register class {}", std::any::type_name::<T>()); | ||
|
|
||
| let godot_params = GodotCreationInfo { | ||
| icon_path: ptr::null(), | ||
| to_string_func: Some(callbacks::to_string::<T>), | ||
| notification_func: Some(callbacks::on_notification::<T>), | ||
| reference_func: Some(callbacks::reference::<T>), | ||
|
|
@@ -167,6 +176,7 @@ pub(crate) fn register_class< | |
|
|
||
| register_class_raw(ClassRegistrationInfo { | ||
| class_name: T::class_id(), | ||
| icon_path: crate::builtin::GString::from(T::icon()), | ||
| parent_class_name: Some(T::Base::class_id()), | ||
| register_methods_constants_fn: None, | ||
| register_properties_fn: None, | ||
|
|
@@ -258,8 +268,10 @@ fn register_classes_and_dyn_traits( | |
|
|
||
| let loaded_class = LoadedClass { | ||
| name: class_name, | ||
| icon: info.icon_path.clone(), | ||
| is_editor_plugin: info.is_editor_plugin, | ||
| }; | ||
|
|
||
| let metadata = ClassMetadata {}; | ||
|
|
||
| // Transpose Class->Trait relations to Trait->Class relations. | ||
|
|
@@ -426,6 +438,7 @@ fn fill_class_info(item: PluginItem, c: &mut ClassRegistrationInfo) { | |
| is_instantiable, | ||
| reference_fn, | ||
| unreference_fn, | ||
| icon, | ||
| }) => { | ||
| c.parent_class_name = Some(base_class_name); | ||
| c.default_virtual_fn = default_get_virtual_fn; | ||
|
|
@@ -466,6 +479,24 @@ fn fill_class_info(item: PluginItem, c: &mut ClassRegistrationInfo) { | |
| c.godot_params.is_runtime = | ||
| sys::conv::bool_to_sys(crate::private::is_class_runtime(is_tool)); | ||
| } | ||
|
|
||
| #[cfg(before_api = "4.4")] | ||
| let _ = icon; // mark used | ||
| #[cfg(since_api = "4.4")] | ||
| { | ||
| let chosen = if !icon.is_empty() { | ||
| icon | ||
| } else { | ||
| // Default icon from `ExtensionLibrary::default_class_icon()`. | ||
| *crate::init::DEFAULT_ICON.lock() | ||
| }; | ||
|
Comment on lines
+487
to
+492
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When you have |
||
|
|
||
| // It's possible that there's no icon. | ||
| if !chosen.is_empty() { | ||
| c.icon_path = crate::builtin::GString::from(chosen); | ||
| c.godot_params.icon_path = c.icon_path.string_sys(); | ||
|
Comment on lines
+496
to
+497
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It's admittedly hard to see if you are not familiar with this; maybe we should rethink this API (just requiring |
||
| } | ||
| } | ||
| } | ||
|
|
||
| PluginItem::InherentImpl(InherentImpl { | ||
|
|
@@ -664,6 +695,7 @@ fn lock_or_panic<T>(global: &'static Global<T>, ctx: &str) -> GlobalGuard<'stati | |
| fn default_registration_info(class_name: ClassId) -> ClassRegistrationInfo { | ||
| ClassRegistrationInfo { | ||
| class_name, | ||
| icon_path: crate::builtin::GString::new(), | ||
| parent_class_name: None, | ||
| register_methods_constants_fn: None, | ||
| register_properties_fn: None, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * Copyright (c) godot-rust; Bromeon and contributors. | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
| */ | ||
|
|
||
| use godot::prelude::*; | ||
|
|
||
| use crate::framework::itest; | ||
|
|
||
| const ICON: &str = "res://icons/icon.svg"; | ||
| #[derive(GodotClass)] | ||
| #[class(init, base=RefCounted, icon = ICON)] | ||
| struct ClassWithIconRefCounted { | ||
| base: Base<RefCounted>, | ||
| } | ||
|
|
||
| #[derive(GodotClass)] | ||
| #[class(init, base=Node, tool, icon = ICON)] | ||
| struct ClassWithIconNode { | ||
| base: Base<Node>, | ||
| } | ||
|
Comment on lines
+13
to
+23
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any notable difference in the icon logic for If not, it should be enough to just test with |
||
|
|
||
| #[itest] | ||
| fn class_icon_registers() { | ||
| let instance1 = ClassWithIconRefCounted::new_gd(); | ||
| let instance2 = ClassWithIconNode::new_alloc(); | ||
|
|
||
| assert!(instance1.is_instance_valid()); | ||
| assert!(instance2.is_instance_valid()); | ||
|
|
||
| instance2.free(); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be simpler to exclude the whole fallback logic from this PR and not try to achieve too many things at once. Icon fallbacks can easily be retrofitted later.