Skip to content

Commit 7970c56

Browse files
authored
Merge pull request #638 from x2f/master
Added godot_print_rich! in log.rs
2 parents d2de819 + e55850d commit 7970c56

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

godot-core/src/log.rs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,34 @@ macro_rules! godot_print {
8484
($fmt:literal $(, $args:expr)* $(,)?) => {
8585
$crate::log::print(&[
8686
$crate::builtin::Variant::from(
87-
$crate::builtin::GString::from(
88-
format!($fmt $(, $args)*)
89-
)
87+
format!($fmt $(, $args)*)
9088
)
9189
])
9290
};
9391
}
9492

95-
pub use crate::{godot_error, godot_print, godot_script_error, godot_warn};
93+
/// Prints to the Godot console. Supports BBCode, color and URL tags.
94+
///
95+
/// Slower than [`godot_print!`].
96+
///
97+
/// _Godot equivalent: [`@GlobalScope.print_rich()`](https://docs.godotengine.org/en/stable/classes/[email protected]#class-globalscope-method-print-rich)_.
98+
#[macro_export]
99+
macro_rules! godot_print_rich {
100+
($fmt:literal $(, $args:expr)* $(,)?) => {
101+
$crate::log::print_rich(&[
102+
$crate::builtin::Variant::from(
103+
format!($fmt $(, $args)*)
104+
)
105+
])
106+
};
107+
}
108+
109+
pub use crate::{godot_error, godot_print, godot_print_rich, godot_script_error, godot_warn};
96110

97111
use crate::builtin::{StringName, Variant};
98112
use crate::sys::{self, GodotFfi};
99113

114+
#[doc(hidden)]
100115
/// Prints to the Godot console, used by the [`godot_print!`] macro.
101116
pub fn print(varargs: &[Variant]) {
102117
unsafe {
@@ -119,3 +134,27 @@ pub fn print(varargs: &[Variant]) {
119134
// TODO use generated method, but figure out how print() with zero args can be called
120135
// crate::engine::utilities::print(head, rest);
121136
}
137+
138+
#[doc(hidden)]
139+
/// Prints to the Godot console, used by the [`godot_print_rich!`] macro.
140+
pub fn print_rich(varargs: &[Variant]) {
141+
unsafe {
142+
let method_name = StringName::from("print_rich");
143+
let call_fn = sys::interface_fn!(variant_get_ptr_utility_function)(
144+
method_name.string_sys(),
145+
2648703342i64,
146+
);
147+
let call_fn = call_fn.unwrap_unchecked();
148+
149+
let mut args = Vec::new();
150+
args.extend(varargs.iter().map(Variant::sys_const));
151+
152+
let args_ptr = args.as_ptr();
153+
let _variant = Variant::from_sys_init_default(|return_ptr| {
154+
call_fn(return_ptr, args_ptr, args.len() as i32);
155+
});
156+
}
157+
158+
// TODO use generated method, but figure out how print_rich() with zero args can be called
159+
// crate::engine::utilities::print_rich(head, rest);
160+
}

0 commit comments

Comments
 (0)