-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditor.cs
199 lines (156 loc) · 5.9 KB
/
Editor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System;
using System.Collections.Generic;
using Sandbox;
using Sandbox.Game.GameSystems;
using Sandbox.Game.Gui;
using Sandbox.Game.World;
using Sandbox.Graphics.GUI;
using Sandbox.ModAPI;
using VRage.Game;
using VRage.Input;
using VRageMath;
using VRageRender;
namespace Digi.ParticleEditor
{
// FIXME: world reload causes particle to not refresh on changes nor on F key
public class Editor
{
bool _showEditor = false;
public bool ShowEditor
{
get => _showEditor;
private set
{
_showEditor = value;
EditorVisibleChanged?.Invoke(value);
}
}
public bool CanShowEditor => MySession.Static.IsServer && MySession.Static.OnlineMode == MyOnlineModeEnum.OFFLINE;
public event Action<bool> EditorVisibleChanged;
public readonly EditorUI EditorUI;
int OriginalHUDState;
public Backup Backup => (Backup)Components[typeof(Backup)];
public LastSeenParticles LastSeenParticles => (LastSeenParticles)Components[typeof(LastSeenParticles)];
public readonly Dictionary<Type, EditorComponentBase> Components = new Dictionary<Type, EditorComponentBase>();
public Editor()
{
EditorUI = new EditorUI(this);
EditorUI.ActuallyClosed += EditorUI_Closed;
LoadComponents();
MyRenderProxy.SetGravityProvider(RenderGravityOverride);
// TODO: reset vanilla particles on world unload
}
public void Dispose()
{
foreach(EditorComponentBase comp in Components.Values)
{
comp.Dispose();
}
MyRenderProxy.SetGravityProvider(MyGravityProviderSystem.CalculateTotalGravityInPoint);
EditorUI?.Dispose();
if(ShowEditor)
{
CloseEditor();
}
}
void LoadComponents()
{
foreach(Type type in GetType().Assembly.GetTypes())
{
if(type.IsAbstract || !type.IsClass)
continue;
if(typeof(EditorComponentBase).IsAssignableFrom(type))
{
//MyLog.Default.WriteLine($"ParticleEditor: Added component '{type.Name}'");
if(Components.ContainsKey(type))
{
Log.Error($"Component '{type.Name}' is already added to components list!");
continue;
}
EditorComponentBase comp = (EditorComponentBase)Activator.CreateInstance(type, new[] { this });
Components.Add(type, comp);
//FieldInfo field = GetType().GetField(type.Name, BindingFlags.SetField | BindingFlags.Instance | BindingFlags.Public);
//if(field != null)
//{
// field.SetValue(this, comp);
//}
}
}
}
Vector3 RenderGravityOverride(Vector3D point)
{
return EditorUI?.StatusUI?.RenderGravityOverride(point) ?? MyGravityProviderSystem.CalculateTotalGravityInPoint(point);
}
public void Update()
{
if(MySector.MainCamera == null || MyInput.Static == null || MySession.Static == null)
{
if(ShowEditor)
CloseEditor();
return;
}
bool inMenu = MySandboxGame.Static.IsCursorVisible || (MyAPIGateway.Gui?.ChatEntryVisible ?? false);
if(!CanShowEditor)
{
if(!inMenu && MyInput.Static.IsNewKeyPressed(MyKeys.F) && MyInput.Static.IsAnyShiftKeyPressed())
MyAPIGateway.Utilities.ShowNotification("Particle Editor only allowed in offline worlds", 3000, MyFontEnum.Red);
if(ShowEditor)
CloseEditor();
return;
}
if(!inMenu && MyInput.Static.IsNewKeyPressed(MyKeys.F) && MyInput.Static.IsAnyShiftKeyPressed())
{
if(!ShowEditor)
OpenEditor();
//else
// CloseEditor();
}
if(ShowEditor)
{
// prevent goodbot and other stuff from showing up...
MyHud.Questlog.Visible = false;
}
foreach(EditorComponentBase comp in Components.Values)
{
if(ShowEditor || comp.AlwaysUpdate)
{
comp.Update();
}
}
}
bool shownInvulWarn = false;
void OpenEditor()
{
if(ShowEditor)
return;
OriginalHUDState = MyHud.HudState;
if(!shownInvulWarn && MySession.Static.SurvivalMode && (MySession.Static.AdminSettings & AdminSettingsEnum.Invulnerable) == 0)
{
shownInvulWarn = true;
Notifications.Show("You are not invulnerable, enable that in admin menu (Alt+F10)", 5, Color.Yellow);
// no easy way to set it properly like admin menu does, with sync and all that.
//Notifications.Show("Made you invulnerable to avoid unpleasantries. Turn off from admin menu (Alt+F10)");
//MySession.Static.AdminSettings |= AdminSettingsEnum.Invulnerable;
}
EditorUI.Open();
MyGuiSandbox.AddScreen(EditorUI);
ShowEditor = true;
}
void EditorUI_Closed()
{
MyHud.SetHudState(OriginalHUDState);
ShowEditor = false;
}
void CloseEditor()
{
if(!ShowEditor)
return;
ShowEditor = false;
if(EditorUI != null)
{
EditorUI.Close();
MyGuiSandbox.RemoveScreen(EditorUI);
}
}
}
}