Skip to content

Commit

Permalink
Merge #883
Browse files Browse the repository at this point in the history
883: Property support for std collection types r=Bromeon a=ironpeak

Implemented the `Export` trait for `HashMap`, `HashSet` (new) and `Vec` so that they can be exported.

The PR also includes an example project using the types as properties.

Co-authored-by: Hrafn Orri Hrafnkelsson <HrafnOrri@BitCrow.net>
Co-authored-by: Jan Haller <bromeon@gmail.com>
  • Loading branch information
3 people authored Jul 17, 2022
2 parents 032e12b + 45119ef commit 3d7ddc5
Show file tree
Hide file tree
Showing 22 changed files with 351 additions and 24 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ members = [
"examples/resource",
"examples/native-plugin",
"examples/rpc",
"examples/array-export",
"examples/builder-export",
"examples/property-export",
"impl/proc-macros"
]

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ The [/examples](https://github.com/godot-rust/godot-rust/tree/master/examples) d
- [**hello-world**](https://github.com/godot-rust/godot-rust/tree/master/examples/hello-world) - Your first project, writes to the console.
- [**spinning-cube**](https://github.com/godot-rust/godot-rust/tree/master/examples/spinning-cube) - Spin our own node in place, exposing editor properties.
- [**scene-create**](https://github.com/godot-rust/godot-rust/tree/master/examples/scene-create) - Load, instance and place scenes using Rust code.
- [**array-export**](https://github.com/godot-rust/godot-rust/tree/master/examples/array-export) - Export more complex properties (here arrays) from Rust.
- [**builder-export**](https://github.com/godot-rust/godot-rust/tree/master/examples/builder-export) - Export using the builder API.
- [**property-export**](https://github.com/godot-rust/godot-rust/tree/master/examples/property-export) - Export complex properties such as collections.
- [**dodge-the-creeps**](https://github.com/godot-rust/godot-rust/tree/master/examples/dodge-the-creeps) - A Rust port of the [little Godot game](https://docs.godotengine.org/en/stable/getting_started/step_by_step/your_first_game.html).
- [**signals**](https://github.com/godot-rust/godot-rust/tree/master/examples/signals) - Connect and emit signals.
- [**resource**](https://github.com/godot-rust/godot-rust/tree/master/examples/resource) - Create and use custom resources.
Expand Down
17 changes: 0 additions & 17 deletions examples/array-export/array_export_library.gdnlib

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "array-export"
name = "builder-export"
version = "0.1.0"
authors = ["The godot-rust developers"]
edition = "2021"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[gd_resource type="NativeScript" load_steps=2 format=2]

[ext_resource path="res://array_export_library.gdnlib" type="GDNativeLibrary" id=1]
[ext_resource path="res://builder_export_library.gdnlib" type="GDNativeLibrary" id=1]

[resource]
resource_name = "ExportsArrays"
Expand Down
File renamed without changes.
17 changes: 17 additions & 0 deletions examples/builder-export/builder_export_library.gdnlib
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[entry]

X11.64="res://../../target/debug/libbuilder_export.so"
OSX.64="res://../../target/debug/libbuilder_export.dylib"
Windows.64="res://../../target/debug/builder_export.dll"

[dependencies]

X11.64=[ ]
OSX.64=[ ]

[general]

singleton=false
load_once=true
symbol_prefix="godot_"
reloadable=true
File renamed without changes.
File renamed without changes
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ _global_script_class_icons={

[application]

config/name="array_export"
config/name="builder_export"
run/main_scene="res://ExportsArrays.tscn"
config/icon="res://icon.png"

Expand Down
File renamed without changes.
14 changes: 14 additions & 0 deletions examples/property-export/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "property-export"
version = "0.1.0"
authors = ["The godot-rust developers"]
edition = "2021"
rust-version = "1.56"
license = "MIT"
publish = false

[lib]
crate-type = ["cdylib"]

[dependencies]
gdnative = { path = "../../gdnative" }
22 changes: 22 additions & 0 deletions examples/property-export/GDScriptPrinter.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
extends Node

func _ready():
var rust = get_node("../PropertyExport")

print("\n-----------------------------------------------------------------")
print("Print from GDScript (note the lexicographically ordered map/set):")
print(" Vec (name):");
for name in rust.name_vec:
print(" * %s" % name)

print("\n HashMap (string -> color):")
for string in rust.color_map:
var color = rust.color_map[string]
print(" * %s -> #%s" % [string, color.to_html(false)]);

print("\n HashSet (ID):")
for id in rust.id_set:
print(" * %s" % id)

# The program has printed the contents and fulfilled its purpose, quit
get_tree().quit()
24 changes: 24 additions & 0 deletions examples/property-export/Main.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[gd_scene load_steps=4 format=2]

[ext_resource path="res://property_export_library.gdnlib" type="GDNativeLibrary" id=1]
[ext_resource path="res://GDScriptPrinter.gd" type="Script" id=2]

[sub_resource type="NativeScript" id=1]
resource_name = "PropertyExport"
class_name = "PropertyExport"
library = ExtResource( 1 )

[node name="Node" type="Node"]

[node name="PropertyExport" type="Node" parent="."]
script = SubResource( 1 )
name_vec = [ "Godot", "Godette", "Go ." ]
color_map = {
"blue": Color( 0.184314, 0.160784, 0.8, 1 ),
"green": Color( 0.0941176, 0.447059, 0.192157, 1 ),
"teal": Color( 0.0941176, 0.423529, 0.564706, 1 )
}
id_set = [ 21, 77, 8, 90 ]

[node name="GDScriptPrinter" type="Node" parent="."]
script = ExtResource( 2 )
14 changes: 14 additions & 0 deletions examples/property-export/default_env.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[gd_resource type="Environment" load_steps=2 format=2]

[sub_resource type="ProceduralSky" id=1]
sky_top_color = Color( 0.0470588, 0.454902, 0.976471, 1 )
sky_horizon_color = Color( 0.556863, 0.823529, 0.909804, 1 )
sky_curve = 0.25
ground_bottom_color = Color( 0.101961, 0.145098, 0.188235, 1 )
ground_horizon_color = Color( 0.482353, 0.788235, 0.952941, 1 )
ground_curve = 0.01
sun_energy = 16.0

[resource]
background_mode = 2
background_sky = SubResource( 1 )
22 changes: 22 additions & 0 deletions examples/property-export/project.godot
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
; Engine configuration file.
; It's best edited using the editor UI and not directly,
; since the parameters that go here are not all obvious.
;
; Format:
; [section] ; section goes between []
; param=value ; assign values to parameters

config_version=4

_global_script_classes=[ ]
_global_script_class_icons={
}

[application]

config/name="Godot Rust - Property Export"
run/main_scene="res://Main.tscn"

[rendering]

environment/default_environment="res://default_env.tres"
17 changes: 17 additions & 0 deletions examples/property-export/property_export_library.gdnlib
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[entry]

X11.64="res://../../target/debug/libproperty_export.so"
OSX.64="res://../../target/debug/libproperty_export.dylib"
Windows.64="res://../../target/debug/property_export.dll"

[dependencies]

X11.64=[ ]
OSX.64=[ ]

[general]

singleton=false
load_once=true
symbol_prefix="godot_"
reloadable=true
49 changes: 49 additions & 0 deletions examples/property-export/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use gdnative::prelude::*;

use std::collections::{HashMap, HashSet};

#[derive(NativeClass, Default)]
#[inherit(Node)]
pub struct PropertyExport {
#[property]
name_vec: Vec<String>,

#[property]
color_map: HashMap<GodotString, Color>,

#[property]
id_set: HashSet<i64>,
}

#[methods]
impl PropertyExport {
fn new(_base: &Node) -> Self {
Self::default()
}

#[export]
fn _ready(&self, _base: &Node) {
godot_print!("------------------------------------------------------------------");
godot_print!("Print from Rust (note the unordered map/set):");
godot_print!(" Vec (name):");
for name in &self.name_vec {
godot_print!(" * {}", name);
}

godot_print!("\n HashMap (string -> color):");
for (string, color) in &self.color_map {
godot_print!(" * {} -> #{}", string, color.to_html(false));
}

godot_print!("\n HashSet (ID):");
for id in &self.id_set {
godot_print!(" * {}", id);
}
}
}

fn init(handle: InitHandle) {
handle.add_class::<PropertyExport>();
}

godot_init!(init);
Loading

0 comments on commit 3d7ddc5

Please sign in to comment.