Skip to content

Commit

Permalink
Merge branch 'godot-rust:master' into feature/reimplement-rect2i
Browse files Browse the repository at this point in the history
  • Loading branch information
yannick-was-taken authored Apr 9, 2023
2 parents c90a08b + 096eccd commit ca0b28c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
11 changes: 8 additions & 3 deletions godot-codegen/src/api_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub struct BuiltinClass {
pub indexing_return_type: Option<String>,
pub is_keyed: bool,
pub members: Option<Vec<Member>>,
pub constants: Option<Vec<Constant>>,
// pub constants: Option<Vec<BuiltinConstant>>,
pub enums: Option<Vec<BuiltinClassEnum>>, // no bitfield
pub operators: Vec<Operator>,
pub methods: Option<Vec<BuiltinClassMethod>>,
Expand All @@ -56,7 +56,7 @@ pub struct Class {
pub is_instantiable: bool,
pub inherits: Option<String>,
// pub api_type: String,
// pub constants: Option<Vec<Constant>>,
pub constants: Option<Vec<ClassConstant>>,
pub enums: Option<Vec<Enum>>,
pub methods: Option<Vec<ClassMethod>>,
// pub properties: Option<Vec<Property>>,
Expand Down Expand Up @@ -100,13 +100,18 @@ pub struct EnumConstant {
pub value: i32,
}

pub type ClassConstant = EnumConstant;

/*
// Constants of builtin types have a string value like "Vector2(1, 1)", hence also a type field
#[derive(DeJson)]
pub struct Constant {
pub struct BuiltinConstant {
pub name: String,
#[nserde(rename = "type")]
pub type_: String,
pub value: String,
}
*/

#[derive(DeJson)]
pub struct Operator {
Expand Down
23 changes: 20 additions & 3 deletions godot-codegen/src/class_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ fn make_class(class: &Class, class_name: &TyName, ctx: &mut Context) -> Generate
let constructor = make_constructor(class, ctx);
let methods = make_methods(&class.methods, class_name, ctx);
let enums = make_enums(&class.enums, class_name, ctx);
let constants = make_constants(&class.constants, class_name, ctx);
let inherits_macro = format_ident!("inherits_transitive_{}", class_name.rust_ty);
let all_bases = ctx.inheritance_tree().collect_all_bases(class_name);
let virtual_trait = make_virtual_methods_trait(class, &all_bases, &virtual_trait_str, ctx);
Expand Down Expand Up @@ -205,6 +206,7 @@ fn make_class(class: &Class, class_name: &TyName, ctx: &mut Context) -> Generate
impl #class_name {
#constructor
#methods
#constants
}
impl crate::obj::GodotClass for #class_name {
type Base = #base;
Expand Down Expand Up @@ -420,9 +422,8 @@ fn make_builtin_methods(
}

fn make_enums(enums: &Option<Vec<Enum>>, _class_name: &TyName, _ctx: &Context) -> TokenStream {
let enums = match enums {
Some(e) => e,
None => return TokenStream::new(),
let Some(enums) = enums else {
return TokenStream::new();
};

let definitions = enums.iter().map(util::make_enum_definition);
Expand All @@ -432,6 +433,22 @@ fn make_enums(enums: &Option<Vec<Enum>>, _class_name: &TyName, _ctx: &Context) -
}
}

fn make_constants(
constants: &Option<Vec<ClassConstant>>,
_class_name: &TyName,
_ctx: &Context,
) -> TokenStream {
let Some(constants) = constants else {
return TokenStream::new();
};

let definitions = constants.iter().map(util::make_constant_definition);

quote! {
#( #definitions )*
}
}

/// Depending on the built-in class, adds custom constructors and methods.
fn make_special_builtin_methods(class_name: &TyName, _ctx: &Context) -> TokenStream {
if class_name.godot_ty == "Array" {
Expand Down
11 changes: 10 additions & 1 deletion godot-codegen/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

use crate::api_parser::Enum;
use crate::api_parser::{ClassConstant, Enum};
use crate::special_cases::is_builtin_scalar;
use crate::{Context, ModName, RustTy, TyName};
use proc_macro2::{Ident, Literal, TokenStream};
Expand Down Expand Up @@ -106,6 +106,15 @@ pub fn make_enum_definition(enum_: &Enum) -> TokenStream {
}
}

pub fn make_constant_definition(constant: &ClassConstant) -> TokenStream {
let ClassConstant { name, value } = constant;
let name = ident(name);

quote! {
pub const #name: i32 = #value;
}
}

fn make_enum_name(enum_name: &str) -> Ident {
// TODO clean up enum name

Expand Down
8 changes: 7 additions & 1 deletion itest/rust/src/codegen_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use crate::itest;
use godot::builtin::inner::{InnerColor, InnerString};
use godot::engine::{FileAccess, HttpRequest, HttpRequestVirtual};
use godot::engine::{FileAccess, HttpRequest, HttpRequestVirtual, Image};
use godot::prelude::*;

#[itest]
Expand Down Expand Up @@ -50,6 +50,12 @@ fn codegen_static_class_method() {
// see also object_test for reference count verification
}

#[itest]
fn codegen_constants() {
assert_eq!(Image::MAX_WIDTH, 16777216);
// assert_eq!(Material::RENDER_PRIORITY_MIN, -128);
}

// ----------------------------------------------------------------------------------------------------------------------------------------------

#[derive(GodotClass)]
Expand Down

0 comments on commit ca0b28c

Please sign in to comment.