126 lines
3.6 KiB
C#
126 lines
3.6 KiB
C#
using System.Collections.Generic;
|
|
using Godot;
|
|
using RPPackage;
|
|
|
|
public partial class Global : Node
|
|
{
|
|
public RPClientEDWS RPClient = new();
|
|
public string sessionId;
|
|
public Node CurrentScene { get; set; }
|
|
public Theme GlobalTheme = null;
|
|
|
|
readonly GodotConfigManager GlobalConfig = new("user://config.cfg");
|
|
public Dictionary<string, Variant> GlobalConfigDict = new() {
|
|
{"font_size", 20},
|
|
{"server_url", "wss://game.zzyxyz.com/"},
|
|
{"user_name", "undefined"}
|
|
};
|
|
|
|
public Dictionary<string, Variant> GlobalData = new() {
|
|
{"player_color", "red"},
|
|
};
|
|
|
|
// Called when the node enters the scene tree for the first time.
|
|
public override void _Ready()
|
|
{
|
|
if (OS.GetName() == "Android") {
|
|
bool ret = OS.RequestPermissions();
|
|
GD.Print($"RequestPermissions ret is {ret}");
|
|
}
|
|
|
|
// OS.RequestPermissions();
|
|
RPClient.OnRPCError += (string errCode, string type, string cmd, string errMsg) => {
|
|
GD.PrintErr($"errCode {errCode}, type/cmd {type}/{cmd}, errMsg {errMsg}");
|
|
};
|
|
RPClient.OnClose += (string eventName, object[] args) => {
|
|
SetProcess(false);
|
|
};
|
|
|
|
Viewport root = GetTree().Root;
|
|
CurrentScene = root.GetChild(root.GetChildCount() - 1);
|
|
|
|
GlobalConfig.LoadConfig("Global", GlobalConfigDict);
|
|
SetProcess(false);
|
|
}
|
|
|
|
public void ConfigFlush()
|
|
{
|
|
int font_size = (int)GlobalConfigDict["font_size"];
|
|
GlobalTheme.DefaultFontSize = font_size;
|
|
// GlobalTheme?.SetFontSize("font_size", "Label", font_size);
|
|
// GlobalTheme?.SetFontSize("font_size", "Button", font_size);
|
|
// GlobalTheme?.SetFontSize("font_size", "TextEdit", font_size);
|
|
// GlobalTheme?.SetFontSize("font_size", "LineEdit", font_size);
|
|
// CurrentScene.GetWindow().AddThemeFontSizeOverride("Control", (int)GlobalConfigDict["font_size"]);
|
|
}
|
|
|
|
private void OnGotoScene()
|
|
{
|
|
ConfigFlush();
|
|
}
|
|
|
|
public void SaveConfig()
|
|
{
|
|
GlobalConfig.SaveConfig("Global", GlobalConfigDict);
|
|
}
|
|
|
|
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
public override void _Process(double delta)
|
|
{
|
|
RPClient.PollEx(delta);
|
|
}
|
|
|
|
public override void _Notification(int what)
|
|
{
|
|
if (what == NotificationWMCloseRequest) {
|
|
// SaveConfig();
|
|
RPClient.Close();
|
|
GetTree().Quit(); // default behavior
|
|
}
|
|
}
|
|
|
|
public delegate void ChangeSceneCallback(Node newSence);
|
|
private static ChangeSceneCallback changeSceneCallback = null;
|
|
public void GotoScene(string path, ChangeSceneCallback callback = null)
|
|
{
|
|
// This function will usually be called from a signal callback,
|
|
// or some other function from the current scene.
|
|
// Deleting the current scene at this point is
|
|
// a bad idea, because it may still be executing code.
|
|
// This will result in a crash or unexpected behavior.
|
|
|
|
// The solution is to defer the load to a later time, when
|
|
// we can be sure that no code from the current scene is running:
|
|
if (callback != null) {
|
|
changeSceneCallback = callback;
|
|
}
|
|
Callable callbackWrapper = new(null, nameof(changeSceneCallback));
|
|
|
|
CallDeferred(MethodName.DeferredGotoScene, path, callbackWrapper);
|
|
changeSceneCallback = null;
|
|
}
|
|
|
|
public void DeferredGotoScene(string path, Callable onLoaded)
|
|
{
|
|
// It is now safe to remove the current scene.
|
|
CurrentScene.Free();
|
|
|
|
// Load a new scene.
|
|
var nextScene = GD.Load<PackedScene>(path);
|
|
|
|
// Instance the new scene.
|
|
CurrentScene = nextScene.Instantiate();
|
|
if (changeSceneCallback != null) {
|
|
onLoaded.Call(CurrentScene);
|
|
}
|
|
|
|
// Add it to the active scene, as child of root.
|
|
GetTree().Root.AddChild(CurrentScene);
|
|
|
|
// Optionally, to make it compatible with the SceneTree.change_scene_to_file() API.
|
|
GetTree().CurrentScene = CurrentScene;
|
|
|
|
OnGotoScene();
|
|
}
|
|
}
|