-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditorUI_Helpers.cs
390 lines (333 loc) · 15.2 KB
/
EditorUI_Helpers.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Digi.ParticleEditor.GameData;
using Digi.ParticleEditor.UIControls;
using Sandbox;
using Sandbox.Game.Gui;
using Sandbox.Graphics;
using Sandbox.Graphics.GUI;
using VRage.FileSystem;
using VRage.Game;
using VRage.ObjectBuilders;
using VRage.ObjectBuilders.Private;
using VRage.Render.Particles;
using VRage.Utils;
using VRageMath;
using VRageRender.Animations;
namespace Digi.ParticleEditor
{
public partial class EditorUI
{
public static string[] Vector2AxisNames = { "X", "Y" };
public static string[] Vector3AxisNames = { "X", "Y", "Z" };
public static string[] Vector3AxisTooltips =
{
"X axis: positive values go towards right, negative towards left.",
"Y axis: positive values go upwards, negative downwards.",
"Z axis: positive values go towards back, negative values go forward.",
};
public static string[] Vector4AxisNames = { "X", "Y", "Z", "W" };
//public static string[] Vector4AxisTooltips =
//{
// Vector3AxisTooltips[0],
// Vector3AxisTooltips[1],
// Vector3AxisTooltips[2],
// "W axis: unknown",
//};
public static string[] Vector4ColorNames = { "R", "G", "B", "A" };
public static double RoundTo5(double value)
{
return Math.Round(value / 5.0) * 5;
}
public static Vector4 ColorForPreview(Vector4 color)
{
// "normalize" color to 0 to 1
float max = Math.Max(Math.Max(color.X, color.Y), Math.Max(color.Z, color.W));
if(max > 1)
{
color.X /= max;
color.Y /= max;
color.Z /= max;
}
color.W = Math.Max(color.W, 0.05f);
return color;
}
public static string CleanupXML(string xml)
{
// remove some pointless text complexity
xml = xml.Replace("\r", "");
xml = xml.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>\n", "");
xml = xml.Replace("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "");
return xml;
}
public static bool SerializeToXML(string filePath, MyObjectBuilder_Base ob)
{
try
{
//string xml = MyAPIGateway.Utilities.SerializeToXML(particleOB);
//xml = xml.Replace(" encoding=\"utf-16\"", "");
//File.WriteAllText(filePath, xml);
return MyObjectBuilderSerializerKeen.SerializeXML(filePath, false, ob);
}
catch(Exception e)
{
Log.Error(e);
return false;
}
}
public static bool CanReadInputs()
{
MyGuiScreenBase focusScreen = MyScreenManager.GetScreenWithFocus();
if(focusScreen == null)
return false;
// normal gameplay
if(focusScreen is MyGuiScreenGamePlay)
return true;
// if currently controlling the game, allow all inputs regardless of where mouse is
if(focusScreen is UIGamePassthroughScreenBase passThrough && passThrough.GamePassThrough)
return true;
// if not controlling game and using a screen that allows hotkeys, check if they're allowed depending on where mouse is
if(focusScreen is IScreenAllowHotkeys allowHotkeys && allowHotkeys.IsAllowed(MyGuiManager.MouseCursorPosition))
return true;
// in a screen that doesn't specify it allows hotkeys, so don't.
return false;
}
public static Color TransitionAlpha(Vector4 color, float transition)
{
Vector4 vector = color;
vector.W *= transition;
return new Color(vector);
}
public static MyGuiScreenMessageBox PopupInfo(string title, string text, MyMessageBoxStyleEnum type = MyMessageBoxStyleEnum.Info)
{
MyGuiScreenMessageBox box = MyGuiSandbox.CreateMessageBox(type, MyMessageBoxButtonsType.OK, new StringBuilder(text), new StringBuilder(title));
MyGuiSandbox.AddScreen(box);
return box;
}
public static MyGuiScreenMessageBox PopupConfirmation(string text, Action confirmed, Action declined = null, bool focusNoButton = false)
{
MyGuiScreenMessageBox box = MyGuiSandbox.CreateMessageBox(MyMessageBoxStyleEnum.Info, MyMessageBoxButtonsType.YES_NO, new StringBuilder(text), new StringBuilder("Confirmation"),
yesButtonText: MyStringId.GetOrCompute("Yes"), noButtonText: MyStringId.GetOrCompute("No"),
callback: (result) =>
{
if(result == MyGuiScreenMessageBox.ResultEnum.YES)
confirmed?.Invoke();
else
declined?.Invoke();
}, focusedResult: focusNoButton ? MyGuiScreenMessageBox.ResultEnum.NO : MyGuiScreenMessageBox.ResultEnum.YES);
MyGuiSandbox.AddScreen(box);
return box;
}
public static MyGuiScreenMessageBox PopupInfoAction(string title, string text, string yesButton = "Yes", string noButton = "No", Action<MyGuiScreenMessageBox.ResultEnum> callback = null, MyMessageBoxStyleEnum type = MyMessageBoxStyleEnum.Info)
{
MyGuiScreenMessageBox box = MyGuiSandbox.CreateMessageBox(type, MyMessageBoxButtonsType.YES_NO, new StringBuilder(text), new StringBuilder(title),
yesButtonText: MyStringId.GetOrCompute(yesButton), noButtonText: MyStringId.GetOrCompute(noButton),
callback: callback);
MyGuiSandbox.AddScreen(box);
return box;
}
public static string GetDescriptionAttrib(Type type, string memberName)
{
MemberInfo[] members = type.GetMember(memberName);
if(members == null || members.Length <= 0)
return null;
object[] attribs = members[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if(attribs == null || attribs.Length <= 0)
return null;
DescriptionAttribute attrib = attribs[0] as DescriptionAttribute;
if(attrib == null)
return null;
return attrib.Description;
}
static IEnumerable<string> FileDialogPlaces()
{
// shows up in reverse order for some reason
yield return MyFileSystem.ContentPath;
yield return Path.Combine(MyFileSystem.UserDataPath, "ParticleEditor");
yield return MyFileSystem.ModsPath;
}
static Form GetMainForm()
{
if(Application.OpenForms.Count > 0)
return Application.OpenForms[0];
else
return new Form { TopMost = true };
}
public static void FileDialog<TDialog>(string title, string directory, string filter, Action<string> callback, string preFilledFileName = null) where TDialog : FileDialog, new()
{
Thread thread = new Thread(new ThreadStart(() =>
{
try
{
using(TDialog dialog = new TDialog())
{
dialog.Title = title;
dialog.Filter = filter;
dialog.RestoreDirectory = true;
dialog.AddExtension = true;
dialog.AutoUpgradeEnabled = true;
if(directory != null && Directory.Exists(directory))
dialog.InitialDirectory = directory;
foreach(string folder in FileDialogPlaces())
dialog.CustomPlaces.Add(folder);
if(!string.IsNullOrEmpty(preFilledFileName))
dialog.FileName = preFilledFileName;
if(dialog.ShowDialog(GetMainForm()) == DialogResult.OK)
{
MySandboxGame.Static.Invoke(() => callback(dialog.FileName), "ParticleEditorPlugin-FileDialog");
}
}
}
catch(Exception e)
{
Log.Error(e);
}
}));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
public static void OpenMultipleFilesDialog(string title, string directory, string filter, Action<string[]> callback)
{
Thread thread = new Thread(new ThreadStart(() =>
{
try
{
using(OpenFileDialog dialog = new OpenFileDialog())
{
dialog.Title = title;
dialog.Filter = filter;
dialog.RestoreDirectory = true;
dialog.AddExtension = true;
dialog.AutoUpgradeEnabled = true;
dialog.Multiselect = true;
if(directory != null && Directory.Exists(directory))
dialog.InitialDirectory = directory;
foreach(string folder in FileDialogPlaces())
dialog.CustomPlaces.Add(folder);
if(dialog.ShowDialog(GetMainForm()) == DialogResult.OK)
{
MySandboxGame.Static.Invoke(() => callback(dialog.FileNames), "ParticleEditorPlugin-MultiOpenFileDialog");
}
}
}
catch(Exception e)
{
Log.Error(e);
}
}));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
public static void FinalizeScrollable(MyGuiControlScrollablePanel panel, MyGuiControlParent content, VerticalControlsHost host, Func<bool> grayOutCondition = null)
{
bool grayedControls = grayOutCondition?.Invoke() ?? false;
if(grayedControls)
panel.SetToolTip("This is turned off, double click it in the list to enable it.");
else
panel.SetToolTip("");
content.Size = new Vector2(content.Size.X, host.CurrentPosition.Y);
// offset controls up by half content's Y size
foreach(MyGuiControlBase control in content.Controls)
{
control.PositionY -= host.CurrentPosition.Y / 2f;
if(grayedControls)
{
control.Enabled = false;
}
}
panel.RefreshInternals();
}
public static object GetGeneralPropertyOriginalOrDefault(PropId propId)
{
if(Instance.SelectedParticle == null)
return null;
if(propId.Type == PropType.General)
{
MyObjectBuilder_ParticleEffect originalData = EditorUI.Instance.SelectedParticle?.OriginalData;
MyObjectBuilder_ParticleEffect defaultData = MyParticleEffectDataSerializer.SerializeToObjectBuilder(EditorUI.DefaultData);
return GetFromOB(originalData) ?? GetFromOB(defaultData);
}
else
{
Log.Error($"Unknown PropType='{propId.Type}' for property '{propId.Name}'");
}
return null;
object GetFromOB(MyObjectBuilder_ParticleEffect ob)
{
if(ob != null)
{
FieldInfo field = ob.GetType().GetField(propId.Name, BindingFlags.GetField | BindingFlags.Instance | BindingFlags.Public);
if(field != null)
return field.GetValue(ob);
else
Log.Error($"Cannot find field with name '{propId.Name}' on type '{ob.GetType().Name}'");
}
return null;
}
}
public static GenerationProperty GetDefaultPropData(object propHost, IMyConstProperty prop)
{
if(propHost == null) throw new ArgumentNullException("propHost");
if(prop == null) throw new ArgumentNullException("prop");
if(propHost is MyParticleGPUGenerationData emitter)
{
foreach(IMyConstProperty p in DefaultEmitter.GetProperties())
{
if(p.Name == prop.Name)
return p.SerializeToObjectBuilder();
}
}
else if(propHost is MyParticleLightData light)
{
foreach(IMyConstProperty p in DefaultLight.GetProperties())
{
if(p.Name == prop.Name)
return p.SerializeToObjectBuilder();
}
}
else
{
throw new Exception($"Unknown propHost type: {propHost.GetType().Name}");
}
throw new Exception($"Couldn't find property '{prop.Name}' for '{propHost.GetType().Name}'");
}
public static bool GetOriginalPropData(object propHost, string propName, out GenerationProperty propOB)
{
if(propHost == null) throw new ArgumentNullException("propHost");
if(propName == null) throw new ArgumentNullException("propName");
MyObjectBuilder_ParticleEffect originalData = EditorUI.Instance.SelectedParticle?.OriginalData;
propOB = default;
if(originalData == null)
return false;
if(propHost is MyParticleGPUGenerationData emitter)
{
ParticleGeneration emitterOB = originalData.ParticleGenerations.Find(e => e.Name == emitter.Name);
if(emitterOB != null)
{
propOB = emitterOB.Properties.Find(p => p.Name == propName);
return true;
}
}
else if(propHost is MyParticleLightData light)
{
ParticleLight lightOB = originalData.ParticleLights.Find(e => e.Name == light.Name);
if(lightOB != null)
{
propOB = lightOB.Properties.Find(p => p.Name == propName);
return true;
}
}
else
{
throw new Exception($"Unknown propHost type: {propHost.GetType().Name}");
}
return false;
}
}
}