Skip to content

Commit

Permalink
Editing rust scripts in the editor is not supported (#53)
Browse files Browse the repository at this point in the history
Closes #52
  • Loading branch information
TitanNano authored Aug 21, 2024
1 parent 91c5129 commit be87ea4
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 18 deletions.
69 changes: 69 additions & 0 deletions rust-script/src/editor_ui_hacks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

use godot::engine::{EditorInterface, Engine};
use godot::log::godot_warn;
use godot::meta::ToGodot;
use godot::prelude::GodotConvert;

#[derive(Clone, Copy)]
pub enum EditorToaserSeverity {
Warning,
}

impl From<EditorToaserSeverity> for u8 {
fn from(value: EditorToaserSeverity) -> Self {
use EditorToaserSeverity::*;

match value {
Warning => 1,
}
}
}

impl GodotConvert for EditorToaserSeverity {
type Via = u8;
}

impl ToGodot for EditorToaserSeverity {
fn to_godot(&self) -> Self::Via {
(*self).into()
}
}

pub fn show_editor_toast(message: &str, severity: EditorToaserSeverity) {
if !Engine::singleton().is_editor_hint() {
return;
}

let Some(base_control) = EditorInterface::singleton().get_base_control() else {
godot_warn!("[godot-rust-script] unable to access editor UI!");
return;
};

let editor_toaser = base_control
.find_children_ex("*".into())
.type_("EditorToaster".into())
.recursive(true)
.owned(false)
.done()
.get(0);

let Some(mut editor_toaser) = editor_toaser else {
godot_warn!("[godot-rust-script] unable to access editor toast notifications!");
return;
};

if !editor_toaser.has_method("_popup_str".into()) {
godot_warn!("[godot-rust-script] Internal toast notifications API no longer exists!");
return;
}

editor_toaser.call(
"_popup_str".into(),
&[message.to_variant(), severity.to_variant(), "".to_variant()],
);
}
1 change: 1 addition & 0 deletions rust-script/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

mod apply;

mod editor_ui_hacks;
mod interface;
mod runtime;
mod static_script_registry;
Expand Down
11 changes: 2 additions & 9 deletions rust-script/src/runtime/resource_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

use godot::classes::{
file_access, ClassDb, FileAccess, IResourceFormatLoader, IScriptLanguageExtension, Script,
};
use godot::classes::{ClassDb, IResourceFormatLoader, IScriptLanguageExtension, Script};
use godot::global::godot_print;
use godot::obj::Base;
use godot::prelude::{
Expand Down Expand Up @@ -81,13 +79,8 @@ impl IResourceFormatLoader for RustScriptResourceLoader {
godot_print!("loading script with path: {}, {}", path, original_path);

let class_name = RustScriptLanguage::path_to_class_name(&path);

let handle = FileAccess::open(path, file_access::ModeFlags::READ).unwrap();
let rust_script = RustScript::new(class_name);

let mut script: Gd<Script> = rust_script.upcast();

script.set_source_code(handle.get_as_text());
let script: Gd<Script> = rust_script.upcast();

script.to_variant()
}
Expand Down
4 changes: 4 additions & 0 deletions rust-script/src/runtime/resource_saver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ impl IResourceFormatSaver for RustScriptResourceSaver {
script.set_path(path.clone());
}

if !script.has_source_code() {
return global::Error::OK;
}

let handle = FileAccess::open(path, file_access::ModeFlags::WRITE);

let mut handle = match handle {
Expand Down
15 changes: 7 additions & 8 deletions rust-script/src/runtime/rust_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ pub(crate) struct RustScript {
#[var(get = get_class_name, set = set_class_name, usage_flags = [STORAGE])]
class_name: GString,

#[var(usage_flags = [STORAGE])]
source_code: GString,

#[var( get = owner_ids, set = set_owner_ids, usage_flags = [STORAGE])]
#[allow(dead_code)]
owner_ids: Array<i64>,
Expand Down Expand Up @@ -150,7 +147,6 @@ impl IScriptExtension for RustScript {
fn init(base: Base<Self::Base>) -> Self {
Self {
class_name: GString::new(),
source_code: GString::new(),
base,
owners: Default::default(),
owner_ids: Default::default(),
Expand All @@ -162,12 +158,11 @@ impl IScriptExtension for RustScript {
}

fn get_source_code(&self) -> GString {
self.source_code.clone()
}
fn set_source_code(&mut self, code: GString) {
self.source_code = code;
GString::default()
}

fn set_source_code(&mut self, _code: GString) {}

fn get_language(&self) -> Option<Gd<ScriptLanguage>> {
RustScriptLanguage::singleton().map(Gd::upcast)
}
Expand Down Expand Up @@ -427,4 +422,8 @@ impl IScriptExtension for RustScript {
self.reload(false);
}
}

fn has_source_code(&self) -> bool {
false
}
}
18 changes: 17 additions & 1 deletion rust-script/src/runtime/rust_script_language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
use std::ffi::OsStr;

use godot::classes::{Engine, FileAccess, IScriptLanguageExtension, ProjectSettings, Script};
use godot::global;
use godot::obj::Base;
use godot::prelude::{
godot_api, Array, Dictionary, GString, Gd, GodotClass, Object, PackedStringArray, VariantArray,
};
use itertools::Itertools;

use crate::apply::Apply;
use crate::editor_ui_hacks::{show_editor_toast, EditorToaserSeverity};
use crate::static_script_registry::RustScriptMetaData;

use super::{rust_script::RustScript, SCRIPT_REGISTRY};
Expand Down Expand Up @@ -189,7 +191,21 @@ impl IScriptLanguageExtension for RustScriptLanguage {
}

fn overrides_external_editor(&mut self) -> bool {
false
true
}

fn open_in_external_editor(
&mut self,
_script: Gd<Script>,
_line: i32,
_col: i32,
) -> global::Error {
show_editor_toast(
"Editing rust scripts from inside Godot is currently not supported.",
EditorToaserSeverity::Warning,
);

global::Error::OK
}

fn get_string_delimiters(&self) -> PackedStringArray {
Expand Down

0 comments on commit be87ea4

Please sign in to comment.