Skip to content

Commit 1e9ea80

Browse files
authored
Merge pull request #732 from godot-rust/qol/streamlined-modules-2
Streamline modules - part II: `godot::extra`, notifications
2 parents 4f9f47f + 685e0a2 commit 1e9ea80

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+171
-139
lines changed

godot-codegen/src/conv/name_conversions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ pub fn to_pascal_case(ty_name: &str) -> String {
7777
.replace("Sdfgiy", "SdfgiY")
7878
}
7979

80+
#[allow(dead_code)] // Keep around in case we need it later.
8081
pub fn shout_to_pascal(shout_case: &str) -> String {
8182
// TODO use heck?
8283

godot-codegen/src/conv/type_conversions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ fn to_rust_type_uncached(full_ty: &GodotTy, ctx: &mut Context) -> RustTy {
172172
let bitfield_ty = conv::make_enum_name(bitfield);
173173

174174
RustTy::EngineBitfield {
175-
tokens: quote! { crate::engine::global::#bitfield_ty },
175+
tokens: quote! { crate::global::#bitfield_ty },
176176
surrounding_class: None,
177177
}
178178
};
@@ -193,7 +193,7 @@ fn to_rust_type_uncached(full_ty: &GodotTy, ctx: &mut Context) -> RustTy {
193193
let enum_ty = conv::make_enum_name(qualified_enum);
194194

195195
RustTy::EngineEnum {
196-
tokens: quote! { crate::engine::global::#enum_ty },
196+
tokens: quote! { crate::global::#enum_ty },
197197
surrounding_class: None,
198198
}
199199
};

godot-codegen/src/generator/notifications.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
use crate::context::Context;
99
use crate::models::domain::TyName;
1010
use crate::models::json::JsonClassConstant;
11-
use crate::{conv, util};
11+
use crate::util;
1212
use proc_macro2::{Ident, TokenStream};
13-
use quote::{format_ident, quote};
13+
use quote::quote;
1414

1515
pub fn make_notify_methods(class_name: &TyName, ctx: &mut Context) -> TokenStream {
1616
// Note: there are two more methods, but only from Node downwards, not from Object:
@@ -85,10 +85,10 @@ pub fn make_notification_enum(
8585
c = class_name.rust_ty
8686
);
8787

88-
let mut notification_enumerators_pascal = Vec::new();
88+
let mut notification_enumerators_shout = Vec::new();
8989
let mut notification_enumerators_ord = Vec::new();
9090
for (constant_ident, constant_value) in all_constants {
91-
notification_enumerators_pascal.push(constant_ident);
91+
notification_enumerators_shout.push(constant_ident);
9292
notification_enumerators_ord.push(constant_value);
9393
}
9494

@@ -97,16 +97,22 @@ pub fn make_notification_enum(
9797
///
9898
/// Makes it easier to keep an overview all possible notification variants for a given class, including
9999
/// notifications defined in base classes.
100+
///
101+
/// Contains the [`Unknown`][Self::Unknown] variant for forward compatibility.
100102
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
101103
#[repr(i32)]
104+
#[allow(non_camel_case_types)]
102105
#cfg_attributes
103106
pub enum #enum_name {
104107
#(
105-
#notification_enumerators_pascal = #notification_enumerators_ord,
108+
#notification_enumerators_shout = #notification_enumerators_ord,
106109
)*
107110

108111
/// Since Godot represents notifications as integers, it's always possible that a notification outside the known types
109112
/// is received. For example, the user can manually issue notifications through `Object::notify()`.
113+
///
114+
/// This is also necessary if you develop an extension on a Godot version and want to be forward-compatible with newer
115+
/// versions. If Godot adds new notifications, they will be unknown to your extension, but you can still handle them.
110116
Unknown(i32),
111117
}
112118

@@ -115,7 +121,7 @@ pub fn make_notification_enum(
115121
fn from(enumerator: i32) -> Self {
116122
match enumerator {
117123
#(
118-
#notification_enumerators_ord => Self::#notification_enumerators_pascal,
124+
#notification_enumerators_ord => Self::#notification_enumerators_shout,
119125
)*
120126
other_int => Self::Unknown(other_int),
121127
}
@@ -126,7 +132,7 @@ pub fn make_notification_enum(
126132
fn from(notification: #enum_name) -> i32 {
127133
match notification {
128134
#(
129-
#enum_name::#notification_enumerators_pascal => #notification_enumerators_ord,
135+
#enum_name::#notification_enumerators_shout => #notification_enumerators_ord,
130136
)*
131137
#enum_name::Unknown(int) => int,
132138
}
@@ -139,27 +145,20 @@ pub fn make_notification_enum(
139145

140146
/// Tries to interpret the constant as a notification one, and transforms it to a Rust identifier on success.
141147
pub fn try_to_notification(constant: &JsonClassConstant) -> Option<Ident> {
142-
constant
143-
.name
144-
.strip_prefix("NOTIFICATION_")
145-
.map(|s| util::ident(&conv::shout_to_pascal(s)))
148+
constant.name.strip_prefix("NOTIFICATION_").map(util::ident) // used to be conv::shout_to_pascal(s)
146149
}
147150

148151
// ----------------------------------------------------------------------------------------------------------------------------------------------
149152
// Implementation
150153

151-
/// Workaround for Godot bug https://github.com/godotengine/godot/issues/75839
154+
/// Workaround for Godot bug https://github.com/godotengine/godot/issues/75839, fixed in 4.2.
152155
///
153156
/// Godot has a collision for two notification constants (DRAW, NODE_CACHE_REQUESTED) in the same inheritance branch (as of 4.0.2).
154157
/// This cannot be represented in a Rust enum, so we merge the two constants into a single enumerator.
155158
fn workaround_constant_collision(all_constants: &mut Vec<(Ident, i32)>) {
156-
for first in ["Draw", "VisibilityChanged"] {
157-
if let Some(index_of_draw) = all_constants
158-
.iter()
159-
.position(|(constant_name, _)| constant_name == first)
160-
{
161-
all_constants[index_of_draw].0 = format_ident!("{first}OrNodeRecacheRequested");
162-
all_constants.retain(|(constant_name, _)| constant_name != "NodeRecacheRequested");
163-
}
164-
}
159+
// This constant has never been used by the engine.
160+
#[cfg(before_api = "4.2")]
161+
all_constants.retain(|(constant_name, _)| constant_name != "NODE_RECACHE_REQUESTED");
162+
163+
let _ = &all_constants; // unused warning
165164
}

godot-core/src/builtin/array.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ impl<T: ArrayElement> Var for Array<T> {
870870
}
871871

872872
PropertyHintInfo {
873-
hint: crate::engine::global::PropertyHint::ARRAY_TYPE,
873+
hint: crate::global::PropertyHint::ARRAY_TYPE,
874874
hint_string: T::godot_type_name().into(),
875875
}
876876
}
@@ -879,7 +879,7 @@ impl<T: ArrayElement> Var for Array<T> {
879879
impl<T: ArrayElement + TypeStringHint> Export for Array<T> {
880880
fn default_export_info() -> PropertyHintInfo {
881881
PropertyHintInfo {
882-
hint: crate::engine::global::PropertyHint::TYPE_STRING,
882+
hint: crate::global::PropertyHint::TYPE_STRING,
883883
hint_string: T::type_string().into(),
884884
}
885885
}

godot-core/src/builtin/basis.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ use sys::{ffi_methods, GodotFfi};
1010

1111
use crate::builtin::math::{ApproxEq, FloatExt, GlamConv, GlamType};
1212
use crate::builtin::real_consts::FRAC_PI_2;
13-
use crate::builtin::{real, Quaternion, RMat3, RQuat, RVec2, RVec3, Vector3};
14-
15-
use crate::engine::global::EulerOrder;
13+
use crate::builtin::{real, EulerOrder, Quaternion, RMat3, RQuat, RVec2, RVec3, Vector3};
1614
use std::cmp::Ordering;
1715
use std::fmt::Display;
1816
use std::ops::{Mul, MulAssign};

godot-core/src/builtin/meta/registration/method.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use sys::interface_fn;
1010

1111
use crate::builtin::meta::{ClassName, PropertyInfo, VarcallSignatureTuple};
1212
use crate::builtin::{StringName, Variant};
13-
use crate::engine::global::MethodFlags;
13+
use crate::global::MethodFlags;
1414

1515
/// Info relating to an argument or return type in a method.
1616
pub struct MethodParamOrReturnInfo {

godot-core/src/builtin/packed_array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ macro_rules! impl_packed_array {
514514
// In 4.3 Godot can (and does) use type hint strings for packed arrays, see https://github.com/godotengine/godot/pull/82952.
515515
if sys::GdextBuild::since_api("4.3") {
516516
$crate::property::PropertyHintInfo {
517-
hint: $crate::engine::global::PropertyHint::TYPE_STRING,
517+
hint: $crate::global::PropertyHint::TYPE_STRING,
518518
hint_string: <$Element as $crate::property::TypeStringHint>::type_string().into(),
519519
}
520520
} else {

godot-core/src/builtin/quaternion.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ use godot_ffi as sys;
99
use sys::{ffi_methods, GodotFfi};
1010

1111
use crate::builtin::math::{ApproxEq, FloatExt, GlamConv, GlamType};
12-
use crate::builtin::{inner, real, Basis, RQuat, RealConv, Vector3};
13-
14-
use crate::engine::global::EulerOrder;
12+
use crate::builtin::{inner, real, Basis, EulerOrder, RQuat, RealConv, Vector3};
1513
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
1614

1715
use super::meta::impl_godot_as_self;

godot-core/src/builtin/rect2.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ use godot_ffi as sys;
99
use sys::{ffi_methods, GodotFfi};
1010

1111
use crate::builtin::math::ApproxEq;
12-
use crate::builtin::{real, Rect2i, Vector2};
13-
use crate::engine::global::Side;
12+
use crate::builtin::{real, Rect2i, Side, Vector2};
1413

1514
use super::meta::impl_godot_as_self;
1615

godot-core/src/builtin/rect2i.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77

88
use std::cmp;
99

10-
use crate::engine::global::Side;
10+
use crate::builtin::meta::impl_godot_as_self;
11+
use crate::builtin::{Rect2, Side, Vector2i};
1112
use godot_ffi as sys;
1213
use sys::{ffi_methods, GodotFfi};
1314

14-
use super::{meta::impl_godot_as_self, Rect2, Vector2i};
15-
1615
/// 2D axis-aligned integer bounding box.
1716
///
1817
/// `Rect2i` consists of a position, a size, and several utility functions. It is typically used for

0 commit comments

Comments
 (0)