This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extension: Refactor code into separate files (#64)
Co-authored-by: Max Oberaigner <[email protected]>
- Loading branch information
Showing
3 changed files
with
94 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// This file handles the uncanny mechanics of sending a notification signal | ||
// (as implemented by godot) from this Godot Engine Singleton to | ||
// | ||
// the top-level "GlobalSignals" autoloaded Scene. | ||
// From there the signals can be further dispached in GDScript | ||
// by connecting to this signal. | ||
|
||
use godot::engine::Engine; | ||
use godot::prelude::*; | ||
|
||
pub enum GlobalSignals { | ||
ScriptUpdated, | ||
} | ||
|
||
pub fn global_notify(signal: GlobalSignals) { | ||
let mut global_signals = get_autoload("/root/GlobalSignals"); | ||
|
||
let signal_name = match signal { | ||
GlobalSignals::ScriptUpdated => "script_updated", | ||
} | ||
.into(); | ||
|
||
global_signals.emit_signal(signal_name, &[]); | ||
} | ||
|
||
fn get_autoload(name: &str) -> Gd<Node> { | ||
let name: NodePath = StringName::from(name).into(); | ||
|
||
Engine::singleton() | ||
.get_main_loop() | ||
.expect("could not get main loop") | ||
.cast::<SceneTree>() | ||
.get_root() | ||
.expect("could not get root of scene") | ||
.get_node(name) | ||
.expect("could not find element in scene") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// "Server-Side" state of the Godot Project. | ||
// | ||
// Keeps a hold of the currently selected project, | ||
// script, etc. | ||
// | ||
// TODO: Rethink wheter this is really the best way to do this, | ||
// or whether we should just send the full selector down with | ||
// every call from the GDScript side. | ||
|
||
use std::sync::{Mutex, OnceLock}; | ||
|
||
use compiler::Ast; | ||
|
||
pub fn ast() -> &'static Mutex<Ast> { | ||
static AST: OnceLock<Mutex<Ast>> = OnceLock::new(); | ||
AST.get_or_init(|| Mutex::new(Ast::default())) | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct EditorState { | ||
current_sprite: Option<i64>, | ||
} | ||
|
||
impl EditorState { | ||
pub fn set_current_sprite(&mut self, id: i64) { | ||
// TODO: Validate whether sprite_id is valid! | ||
self.current_sprite = Some(id); | ||
} | ||
} | ||
|
||
pub fn editor_state() -> &'static Mutex<EditorState> { | ||
static EDITOR_STATE: OnceLock<Mutex<EditorState>> = OnceLock::new(); | ||
EDITOR_STATE.get_or_init(|| Mutex::new(EditorState::default())) | ||
} |