Skip to content

Commit be87ea4

Browse files
authored
Editing rust scripts in the editor is not supported (#53)
Closes #52
1 parent 91c5129 commit be87ea4

File tree

6 files changed

+100
-18
lines changed

6 files changed

+100
-18
lines changed

rust-script/src/editor_ui_hacks.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
5+
*/
6+
7+
use godot::engine::{EditorInterface, Engine};
8+
use godot::log::godot_warn;
9+
use godot::meta::ToGodot;
10+
use godot::prelude::GodotConvert;
11+
12+
#[derive(Clone, Copy)]
13+
pub enum EditorToaserSeverity {
14+
Warning,
15+
}
16+
17+
impl From<EditorToaserSeverity> for u8 {
18+
fn from(value: EditorToaserSeverity) -> Self {
19+
use EditorToaserSeverity::*;
20+
21+
match value {
22+
Warning => 1,
23+
}
24+
}
25+
}
26+
27+
impl GodotConvert for EditorToaserSeverity {
28+
type Via = u8;
29+
}
30+
31+
impl ToGodot for EditorToaserSeverity {
32+
fn to_godot(&self) -> Self::Via {
33+
(*self).into()
34+
}
35+
}
36+
37+
pub fn show_editor_toast(message: &str, severity: EditorToaserSeverity) {
38+
if !Engine::singleton().is_editor_hint() {
39+
return;
40+
}
41+
42+
let Some(base_control) = EditorInterface::singleton().get_base_control() else {
43+
godot_warn!("[godot-rust-script] unable to access editor UI!");
44+
return;
45+
};
46+
47+
let editor_toaser = base_control
48+
.find_children_ex("*".into())
49+
.type_("EditorToaster".into())
50+
.recursive(true)
51+
.owned(false)
52+
.done()
53+
.get(0);
54+
55+
let Some(mut editor_toaser) = editor_toaser else {
56+
godot_warn!("[godot-rust-script] unable to access editor toast notifications!");
57+
return;
58+
};
59+
60+
if !editor_toaser.has_method("_popup_str".into()) {
61+
godot_warn!("[godot-rust-script] Internal toast notifications API no longer exists!");
62+
return;
63+
}
64+
65+
editor_toaser.call(
66+
"_popup_str".into(),
67+
&[message.to_variant(), severity.to_variant(), "".to_variant()],
68+
);
69+
}

rust-script/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
mod apply;
88

9+
mod editor_ui_hacks;
910
mod interface;
1011
mod runtime;
1112
mod static_script_registry;

rust-script/src/runtime/resource_loader.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
55
*/
66

7-
use godot::classes::{
8-
file_access, ClassDb, FileAccess, IResourceFormatLoader, IScriptLanguageExtension, Script,
9-
};
7+
use godot::classes::{ClassDb, IResourceFormatLoader, IScriptLanguageExtension, Script};
108
use godot::global::godot_print;
119
use godot::obj::Base;
1210
use godot::prelude::{
@@ -81,13 +79,8 @@ impl IResourceFormatLoader for RustScriptResourceLoader {
8179
godot_print!("loading script with path: {}, {}", path, original_path);
8280

8381
let class_name = RustScriptLanguage::path_to_class_name(&path);
84-
85-
let handle = FileAccess::open(path, file_access::ModeFlags::READ).unwrap();
8682
let rust_script = RustScript::new(class_name);
87-
88-
let mut script: Gd<Script> = rust_script.upcast();
89-
90-
script.set_source_code(handle.get_as_text());
83+
let script: Gd<Script> = rust_script.upcast();
9184

9285
script.to_variant()
9386
}

rust-script/src/runtime/resource_saver.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ impl IResourceFormatSaver for RustScriptResourceSaver {
3030
script.set_path(path.clone());
3131
}
3232

33+
if !script.has_source_code() {
34+
return global::Error::OK;
35+
}
36+
3337
let handle = FileAccess::open(path, file_access::ModeFlags::WRITE);
3438

3539
let mut handle = match handle {

rust-script/src/runtime/rust_script.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ pub(crate) struct RustScript {
3838
#[var(get = get_class_name, set = set_class_name, usage_flags = [STORAGE])]
3939
class_name: GString,
4040

41-
#[var(usage_flags = [STORAGE])]
42-
source_code: GString,
43-
4441
#[var( get = owner_ids, set = set_owner_ids, usage_flags = [STORAGE])]
4542
#[allow(dead_code)]
4643
owner_ids: Array<i64>,
@@ -150,7 +147,6 @@ impl IScriptExtension for RustScript {
150147
fn init(base: Base<Self::Base>) -> Self {
151148
Self {
152149
class_name: GString::new(),
153-
source_code: GString::new(),
154150
base,
155151
owners: Default::default(),
156152
owner_ids: Default::default(),
@@ -162,12 +158,11 @@ impl IScriptExtension for RustScript {
162158
}
163159

164160
fn get_source_code(&self) -> GString {
165-
self.source_code.clone()
166-
}
167-
fn set_source_code(&mut self, code: GString) {
168-
self.source_code = code;
161+
GString::default()
169162
}
170163

164+
fn set_source_code(&mut self, _code: GString) {}
165+
171166
fn get_language(&self) -> Option<Gd<ScriptLanguage>> {
172167
RustScriptLanguage::singleton().map(Gd::upcast)
173168
}
@@ -427,4 +422,8 @@ impl IScriptExtension for RustScript {
427422
self.reload(false);
428423
}
429424
}
425+
426+
fn has_source_code(&self) -> bool {
427+
false
428+
}
430429
}

rust-script/src/runtime/rust_script_language.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
use std::ffi::OsStr;
88

99
use godot::classes::{Engine, FileAccess, IScriptLanguageExtension, ProjectSettings, Script};
10+
use godot::global;
1011
use godot::obj::Base;
1112
use godot::prelude::{
1213
godot_api, Array, Dictionary, GString, Gd, GodotClass, Object, PackedStringArray, VariantArray,
1314
};
1415
use itertools::Itertools;
1516

1617
use crate::apply::Apply;
18+
use crate::editor_ui_hacks::{show_editor_toast, EditorToaserSeverity};
1719
use crate::static_script_registry::RustScriptMetaData;
1820

1921
use super::{rust_script::RustScript, SCRIPT_REGISTRY};
@@ -189,7 +191,21 @@ impl IScriptLanguageExtension for RustScriptLanguage {
189191
}
190192

191193
fn overrides_external_editor(&mut self) -> bool {
192-
false
194+
true
195+
}
196+
197+
fn open_in_external_editor(
198+
&mut self,
199+
_script: Gd<Script>,
200+
_line: i32,
201+
_col: i32,
202+
) -> global::Error {
203+
show_editor_toast(
204+
"Editing rust scripts from inside Godot is currently not supported.",
205+
EditorToaserSeverity::Warning,
206+
);
207+
208+
global::Error::OK
193209
}
194210

195211
fn get_string_delimiters(&self) -> PackedStringArray {

0 commit comments

Comments
 (0)