1using Fjord.Ui;
2
3namespace Fjord.Scenes;
4
5public static class SceneHandler
6{
7 internal static Dictionary<string, Scene> Scenes = new ();
8 internal static Dictionary<string, Scene> OriginalScenes = new ();
9
10 public static List<string> LoadedScenes = new();
11
12 public static void Initialize() {
13 Debug.RegisterCommand("scene_unload", (args) => {
14 if(args.Length > 0) {
15 if(SceneHandler.Scenes.ContainsKey((string)args[0])) {
16 if(SceneHandler.IsLoaded((string)args[0])) {
17 SceneHandler.Unload((string)args[0]);
18 Debug.Log(LogLevel.Message, $"Unloaded {(string)args[0]}");
19 } else {
20 Debug.Log(LogLevel.Warning, $"{(string)args[0]} is not loaded");
21 }
22 } else {
23 Debug.Log(LogLevel.Warning, $"No scene named {(string)args[0]}");
24 }
25 } else {
26 Debug.Log(LogLevel.Error, $"No argument provided");
27 }
28 });
29
30 Debug.RegisterCommand("scene_load", (args) => {
31 if(args.Length > 0) {
32 if(SceneHandler.Scenes.ContainsKey((string)args[0])) {
33 if(!SceneHandler.IsLoaded((string)args[0])) {
34 SceneHandler.Load((string)args[0]);
35 Debug.Log(LogLevel.Message, $"Loaded {(string)args[0]}");
36 } else {
37 Debug.Log(LogLevel.Warning, $"{(string)args[0]} is already loaded");
38 }
39 } else {
40 Debug.Log(LogLevel.Warning, $"No scene named {(string)args[0]}");
41 }
42 } else {
43 Debug.Log(LogLevel.Error, $"No argument provided");
44 }
45 });
46
47 Debug.RegisterCommand("scene_remake", (args) => {
48 if(args.Length > 0) {
49 if(SceneHandler.Scenes.ContainsKey((string)args[0])) {
50 SceneHandler.Remake((string)args[0]);
51 Debug.Log(LogLevel.Message, $"Remade {(string)args[0]}");
52 } else {
53 Debug.Log(LogLevel.Warning, $"No scene named {(string)args[0]}");
54 }
55 } else {
56 Debug.Log(LogLevel.Error, $"No argument provided");
57 }
58 });
59
60 Debug.RegisterCommand("scene_allowresize", (args) => {
61 if(args.Length > 0) {
62 if(SceneHandler.Scenes.ContainsKey((string)args[0])) {
63 SceneHandler.Get((string)args[0]).AllowWindowResize = !SceneHandler.Get((string)args[0]).AllowWindowResize;
64 Debug.Log(LogLevel.Message, $"{(string)args[0]} can now{(SceneHandler.Get((string)args[0]).AllowWindowResize ? "" : "not ")}be resized");
65 } else {
66 Debug.Log(LogLevel.Warning, $"No scene named {(string)args[0]}");
67 }
68 } else {
69 Debug.Log(LogLevel.Error, $"No argument provided");
70 }
71 });
72
73 Debug.RegisterCommand("scene_alwaysback", (args) => {
74 if(args.Length > 0) {
75 if(SceneHandler.Scenes.ContainsKey((string)args[0])) {
76 SceneHandler.Get((string)args[0]).AlwaysAtBack = !SceneHandler.Get((string)args[0]).AlwaysAtBack;
77 Debug.Log(LogLevel.Message, $"{(string)args[0]} is now {(SceneHandler.Get((string)args[0]).AlwaysAtBack ? "" : "not ")}always at front");
78 } else {
79 Debug.Log(LogLevel.Warning, $"No scene named {(string)args[0]}");
80 }
81 } else {
82 Debug.Log(LogLevel.Error, $"No argument provided");
83 }
84 });
85
86 Debug.RegisterCommand("scene_alwaysfront", (args) => {
87 if(args.Length > 0) {
88 if(SceneHandler.Scenes.ContainsKey((string)args[0])) {
89 SceneHandler.Get((string)args[0]).AlwaysAtFront = !SceneHandler.Get((string)args[0]).AlwaysAtFront;
90 Debug.Log(LogLevel.Message, $"{(string)args[0]} is now {(SceneHandler.Get((string)args[0]).AlwaysAtFront ? "" : "not ")}always at front");
91 } else {
92 Debug.Log(LogLevel.Warning, $"No scene named {(string)args[0]}");
93 }
94 } else {
95 Debug.Log(LogLevel.Error, $"No argument provided");
96 }
97 });
98
99 Debug.RegisterCommand("scene_rebuildalways", (args) => {
100 if(args.Length > 0) {
101 if(SceneHandler.Scenes.ContainsKey((string)args[0])) {
102 SceneHandler.Get((string)args[0]).AlwaysRebuildTexture = !SceneHandler.Get((string)args[0]).AlwaysRebuildTexture;
103 Debug.Log(LogLevel.Message, $"{(string)args[0]} is now {(SceneHandler.Get((string)args[0]).AlwaysRebuildTexture ? "" : "not ")}always rebuilt");
104 } else {
105 Debug.Log(LogLevel.Warning, $"No scene named {(string)args[0]}");
106 }
107 } else {
108 Debug.Log(LogLevel.Error, $"No argument provided");
109 }
110 });
111
112 Debug.RegisterCommand("scene_getall", (args) => {
113 foreach(string scene in Scenes.Keys)
114 {
115 Debug.Log(scene);
116 }
117 });
118 }
119
120 public static void Register(Scene scene)
121 {
122 Scenes.Add(scene.SceneID, (Scene)scene.Clone());
123 OriginalScenes.Add(scene.SceneID, (Scene)scene.Clone());
124 }
125
126 internal static void Load(string id)
127 {
128 if (!LoadedScenes.Contains(id))
129 {
130 LoadedScenes.Add(id);
131 Scenes[id].AwakeCall();
132 } else {
133 Debug.Log($"Scene \"{id}\" doesnt exist");
134 }
135 }
136
137 internal static void Unload(string id)
138 {
139 if (LoadedScenes.Contains(id))
140 {
141 Scenes[id].SleepCall();
142 LoadedScenes.Remove(id);
143 }
144 FUI.selectedTextField = null;
145 }
146
147 internal static void Remake(string id)
148 {
149 Scenes[id] = (Scene)OriginalScenes[id].Clone();
150 Scenes[id].Entities.Clear();
151 Scenes[id].Awake();
152 }
153
154 public static void Load<T>()
155 {
156 Load(typeof(T).Name);
157 }
158
159 public static void Unload<T>()
160 {
161 Unload(typeof(T).Name);
162 }
163
164 public static void Remake<T>()
165 {
166 Remake(typeof(T).Name);
167 }
168
169 public static T Get<T>()
170 {
171 var scene = Scenes.Values.ToList().Find((val) => val.GetType() == typeof(T));
172 if(scene != null)
173 return (T)(dynamic)scene;
174 else
175 throw new Exception("Scene doesn't exist");
176 }
177
178 public static bool Get<T>(out T scene)
179 {
180 var s = Scenes.Values.ToList().Find((val) => val.GetType() == typeof(T));
181 if(s != null) {
182 scene = (T)(dynamic)s;
183 return true;
184 } else {
185 scene = default(T)!;
186 return false;
187 }
188 }
189
190 internal static Scene Get(string id)
191 {
192 return Scenes[id];
193 }
194
195 internal static bool IsLoaded(string id)
196 {
197 return LoadedScenes.Contains(id);
198 }
199
200 public static bool IsLoaded<T>()
201 {
202 return IsLoaded(typeof(T).Name);
203 }
204}