-
-
Notifications
You must be signed in to change notification settings - Fork 223
Initial test of Godot duplicator with Area2D #1141
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 1 commit
8ab81f1
033ba1d
49a0f47
7421b02
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 |
---|---|---|
|
@@ -5,8 +5,8 @@ | |
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
use godot::builtin::{dict, Color, Dictionary, GString, Variant, VariantType}; | ||
use godot::classes::{INode, IRefCounted, Node, Object, RefCounted, Resource, Texture}; | ||
use godot::builtin::{dict, Color, Dictionary, GString, StringName, Variant, VariantType}; | ||
use godot::classes::{Area2D, INode, IRefCounted, Node, Object, RefCounted, Resource, Texture}; | ||
use godot::global::{PropertyHint, PropertyUsageFlags}; | ||
use godot::meta::{GodotConvert, PropertyHintInfo, ToGodot}; | ||
use godot::obj::{Base, EngineBitfield, EngineEnum, Gd, NewAlloc, NewGd, OnEditor}; | ||
|
@@ -537,3 +537,85 @@ fn test_var_with_renamed_funcs() { | |
|
||
obj.free(); | ||
} | ||
|
||
#[derive(GodotClass)] | ||
#[class(base=Node)] | ||
struct SomeDuplicator { | ||
#[var] | ||
int_val: i32, | ||
|
||
#[var] | ||
optional_node: Option<Gd<Node>>, | ||
|
||
#[export] | ||
export_area: OnEditor<Gd<Area2D>>, | ||
} | ||
|
||
#[godot_api] | ||
impl INode for SomeDuplicator { | ||
fn init(_base: Base<Node>) -> Self { | ||
SomeDuplicator { | ||
int_val: 0, | ||
optional_node: None, | ||
export_area: OnEditor::default(), | ||
} | ||
} | ||
} | ||
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. As this uses default values for every field, it can be generated. This can be done on the struct declaration above, by adding #[derive(GodotClass)]
#[class(init, base=Node)] 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. Addressed. |
||
|
||
#[itest] | ||
fn test_some_duplicator() { | ||
let mut obj = SomeDuplicator::new_alloc(); | ||
assert_eq!(obj.bind().int_val, 0); | ||
assert_eq!(obj.bind().optional_node, None); | ||
|
||
obj.bind_mut().int_val = 5; | ||
assert_eq!(obj.bind().int_val, 5); | ||
DominikMendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let some_node = Some(Node::new_alloc()); | ||
let some_id = some_node.as_ref().unwrap().instance_id(); | ||
obj.bind_mut().optional_node = some_node.clone(); | ||
let obj_id = obj.bind().optional_node.as_ref().unwrap().instance_id(); | ||
assert_eq!(obj_id, some_id); | ||
DominikMendel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
obj.bind_mut() | ||
.optional_node | ||
.as_mut() | ||
.unwrap() | ||
.set_name("renamed"); | ||
assert_eq!( | ||
obj.bind().optional_node.as_ref().unwrap().get_name(), | ||
StringName::from("renamed") | ||
); | ||
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. Again, just tests what was set. The idea of adding this test is to check if 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. Understood. Addressed. |
||
|
||
let mut some_area = Area2D::new_alloc(); | ||
some_area.set_collision_layer(1); | ||
some_area.set_collision_mask(1); | ||
obj.bind_mut().export_area.init(some_area); | ||
assert_eq!(obj.bind().export_area.get_collision_layer(), 1); | ||
assert_eq!(obj.bind().export_area.get_collision_mask(), 1); | ||
|
||
let duplicated_obj = obj.duplicate(); | ||
let duplicated_obj: Gd<SomeDuplicator> = duplicated_obj.unwrap().cast(); | ||
|
||
assert_eq!(duplicated_obj.bind().int_val, 5); | ||
assert_eq!( | ||
duplicated_obj | ||
.bind() | ||
.optional_node | ||
.as_ref() | ||
.unwrap() | ||
.instance_id(), | ||
obj_id | ||
); | ||
assert_eq!( | ||
duplicated_obj | ||
.bind() | ||
.optional_node | ||
.as_ref() | ||
.unwrap() | ||
.get_name(), | ||
StringName::from("renamed") | ||
); | ||
assert_eq!(duplicated_obj.bind().export_area.get_collision_layer(), 1); | ||
assert_eq!(duplicated_obj.bind().export_area.get_collision_mask(), 1); | ||
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. You can extract this into a variable, and then use everywhere: let duplicated = duplicated_obj.bind(); 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. Done. |
||
} |
Uh oh!
There was an error while loading. Please reload this page.