forked from michaelchessall/SS14-Persistence
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuideReagentEmbed.xaml.cs
More file actions
432 lines (382 loc) · 16.6 KB
/
GuideReagentEmbed.xaml.cs
File metadata and controls
432 lines (382 loc) · 16.6 KB
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
using Content.Client.Chemistry.EntitySystems;
using Content.Client.Guidebook.Richtext;
using Content.Client.Message;
using Content.Client.UserInterface.ControlExtensions;
using Content.Shared.CCVar;
using Content.Shared.Chemistry.Reaction;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.Contraband;
using Content.Shared.Localizations;
using Content.Shared.Metabolism;
using JetBrains.Annotations;
using Robust.Client.AutoGenerated;
using Robust.Client.Graphics;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Configuration;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
namespace Content.Client.Guidebook.Controls;
/// <summary>
/// Control for embedding a reagent into a guidebook.
/// </summary>
[UsedImplicitly, GenerateTypedNameReferences]
public sealed partial class GuideReagentEmbed : BoxContainer, IDocumentTag, ISearchableControl, IPrototypeRepresentationControl
{
[Dependency] private readonly IEntitySystemManager _systemManager = default!;
[Dependency] private readonly ILogManager _logManager = default!;
[Dependency] private readonly IPrototypeManager _prototype = default!;
[Dependency] private readonly IConfigurationManager _config = default!;
private readonly ChemistryGuideDataSystem _chemistryGuideData;
private readonly ContrabandSystem _contraband;
private readonly ISawmill _sawmill;
public IPrototype? RepresentedPrototype { get; private set; }
public GuideReagentEmbed()
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
_sawmill = _logManager.GetSawmill("guidebook.reagent");
_chemistryGuideData = _systemManager.GetEntitySystem<ChemistryGuideDataSystem>();
_contraband = _systemManager.GetEntitySystem<ContrabandSystem>();
MouseFilter = MouseFilterMode.Stop;
}
public GuideReagentEmbed(string reagent) : this()
{
GenerateControl(_prototype.Index<ReagentPrototype>(reagent));
}
public GuideReagentEmbed(ReagentPrototype reagent) : this()
{
GenerateControl(reagent);
}
public GuideReagentEmbed(ReactionPrototype reagent) : this()
{
GenerateControl(reagent);
}
public bool CheckMatchesSearch(string query)
{
return this.ChildrenContainText(query);
}
public void SetHiddenState(bool state, string query)
{
Visible = CheckMatchesSearch(query) ? state : !state;
}
public bool TryParseTag(Dictionary<string, string> args, [NotNullWhen(true)] out Control? control)
{
control = null;
if (!args.TryGetValue("Reagent", out var id))
{
_sawmill.Error("Reagent embed tag is missing reagent prototype argument");
return false;
}
if (!_prototype.TryIndex<ReagentPrototype>(id, out var reagent))
{
_sawmill.Error($"Specified reagent prototype \"{id}\" is not a valid reagent prototype");
return false;
}
GenerateControl(reagent);
control = this;
return true;
}
private void GenerateControl(ReagentPrototype reagent)
{
RepresentedPrototype = reagent;
NameBackground.PanelOverride = new StyleBoxFlat
{
BackgroundColor = reagent.SubstanceColor
};
var r = reagent.SubstanceColor.R;
var g = reagent.SubstanceColor.G;
var b = reagent.SubstanceColor.B;
var textColor = 0.2126f * r + 0.7152f * g + 0.0722f * b > 0.5
? Color.Black
: Color.White;
ReagentName.SetMarkup(Loc.GetString("guidebook-reagent-name",
("color", textColor), ("name", reagent.LocalizedName)));
#region Recipe
var reactions = _prototype.EnumeratePrototypes<ReactionPrototype>()
.Where(p => !p.Source && p.Products.ContainsKey(reagent.ID))
.OrderBy(p => p.Priority)
.ThenBy(p => p.Products.Count)
.ToList();
if (reactions.Any())
{
foreach (var reactionPrototype in reactions)
{
RecipesDescriptionContainer.AddChild(new GuideReagentReaction(reactionPrototype, _prototype, _systemManager));
}
}
else
{
RecipesContainer.Visible = false;
}
#endregion
#region Effects
if (_chemistryGuideData.ReagentGuideRegistry.TryGetValue(reagent.ID, out var guideEntryRegistry) &&
guideEntryRegistry.GuideEntries != null &&
guideEntryRegistry.GuideEntries.Values.Any(pair => pair.EffectDescriptions.Any() || pair.Metabolites?.Any() == true))
{
EffectsDescriptionContainer.Children.Clear();
foreach (var (stage, effect) in guideEntryRegistry.GuideEntries)
{
var hasMetabolites = effect.Metabolites?.Any() == true;
if (!effect.EffectDescriptions.Any() && !hasMetabolites)
continue;
var groupLabel = new RichTextLabel();
groupLabel.SetMarkup(Loc.GetString("guidebook-reagent-effects-metabolism-stage-rate",
("stage", _prototype.Index<MetabolismStagePrototype>(stage).LocalizedName), ("rate", effect.MetabolismRate)));
var descriptionLabel = new RichTextLabel
{
Margin = new Thickness(25, 0, 10, 0)
};
var descMsg = new FormattedMessage();
var descriptionsCount = effect.EffectDescriptions.Length;
var i = 0;
foreach (var effectString in effect.EffectDescriptions)
{
descMsg.AddMarkupOrThrow(effectString);
i++;
if (i < descriptionsCount || hasMetabolites)
descMsg.PushNewline();
}
if (hasMetabolites)
{
var metabolites = new List<string>();
foreach (var (metabolite, ratio) in effect.Metabolites!)
{
metabolites.Add(Loc.GetString("guidebook-reagent-effects-metabolite-item", ("rate", (double)ratio), ("reagent", _prototype.Index(metabolite).LocalizedName)));
}
metabolites.Sort();
descMsg.AddMarkupOrThrow(Loc.GetString("guidebook-reagent-effects-metabolites", ("items", ContentLocalizationManager.FormatList(metabolites))));
}
descriptionLabel.SetMessage(descMsg);
EffectsDescriptionContainer.AddChild(groupLabel);
EffectsDescriptionContainer.AddChild(descriptionLabel);
}
}
else
{
EffectsContainer.Visible = false;
}
#endregion
#region PlantMetabolisms
if (_chemistryGuideData.ReagentGuideRegistry.TryGetValue(reagent.ID, out var guideEntryRegistryPlant) &&
guideEntryRegistryPlant.PlantMetabolisms != null &&
guideEntryRegistryPlant.PlantMetabolisms.Count > 0)
{
PlantMetabolismsDescriptionContainer.Children.Clear();
var metabolismLabel = new RichTextLabel();
metabolismLabel.SetMarkup(Loc.GetString("guidebook-reagent-plant-metabolisms-rate"));
var descriptionLabel = new RichTextLabel
{
Margin = new Thickness(25, 0, 10, 0)
};
var descMsg = new FormattedMessage();
var descriptionsCount = guideEntryRegistryPlant.PlantMetabolisms.Count;
var i = 0;
foreach (var effectString in guideEntryRegistryPlant.PlantMetabolisms)
{
descMsg.AddMarkupOrThrow(effectString);
i++;
if (i < descriptionsCount)
descMsg.PushNewline();
}
descriptionLabel.SetMessage(descMsg);
PlantMetabolismsDescriptionContainer.AddChild(metabolismLabel);
PlantMetabolismsDescriptionContainer.AddChild(descriptionLabel);
}
else
{
PlantMetabolismsContainer.Visible = false;
}
#endregion
GenerateSources(reagent);
FormattedMessage description = new();
description.AddText(reagent.LocalizedDescription);
description.PushNewline();
description.AddMarkupOrThrow(Loc.GetString("guidebook-reagent-physical-description",
("description", reagent.LocalizedPhysicalDescription)));
if (_config.GetCVar(CCVars.ContrabandExamine))
{
// Department-restricted text
if (reagent.AllowedJobs.Count > 0 || reagent.AllowedDepartments.Count > 0)
{
description.PushNewline();
description.AddMarkupPermissive(
_contraband.GenerateDepartmentExamineMessage(reagent.AllowedDepartments, reagent.AllowedJobs, ContrabandItemType.Reagent));
}
// Other contraband text
else if (reagent.ContrabandSeverity != null &&
_prototype.Resolve(reagent.ContrabandSeverity.Value, out var severity))
{
description.PushNewline();
description.AddMarkupPermissive(Loc.GetString(severity.ExamineText, ("type", ContrabandItemType.Reagent)));
}
}
ReagentDescription.SetMessage(description);
}
private void GenerateControl(ReactionPrototype reagent)
{
RepresentedPrototype = reagent;
NameBackground.PanelOverride = new StyleBoxFlat
{
BackgroundColor = Color.Black
};
if (reagent.Effects.Length < 1)
return;
IEnumerable<Shared.EntityEffects.Effects.EntitySpawning.SpawnEntity> spawnEvents = reagent.Effects.OfType<Shared.EntityEffects.Effects.EntitySpawning.SpawnEntity>();
if (spawnEvents.Count() == 0)
return;
var spawnId = spawnEvents.First().Entity;
ReagentName.SetMarkup(Loc.GetString("guidebook-reagent-name",
("color", Color.White), ("name", _prototype.Index<EntityPrototype>(spawnId).Name)));
_sawmill.Info($"{reagent.ID}");
#region Recipe
var reactions = _prototype.EnumeratePrototypes<ReactionPrototype>()
.Where(p => !p.Source && p.Effects.Length >= 1)
.OrderBy(p => p.Priority)
.ThenBy(p => p.Products.Count)
.ToList();
if (reactions.Any())
{
foreach (var reactionPrototype in reactions)
{
IEnumerable<Shared.EntityEffects.Effects.EntitySpawning.SpawnEntity> stuff = reactionPrototype.Effects.OfType<Shared.EntityEffects.Effects.EntitySpawning.SpawnEntity>();
if (stuff.Count() >= 1 && stuff.First().Entity == spawnId)
{
RecipesDescriptionContainer.AddChild(new GuideReagentReaction(reactionPrototype, _prototype, _systemManager, spawnId));
}
}
}
else
{
RecipesContainer.Visible = false;
}
#endregion
#region Effects
if (_chemistryGuideData.ReagentGuideRegistry.TryGetValue(reagent.ID, out var guideEntryRegistry) &&
guideEntryRegistry.GuideEntries != null &&
guideEntryRegistry.GuideEntries.Values.Any(pair => pair.EffectDescriptions.Any() || pair.Metabolites?.Any() == true))
{
EffectsDescriptionContainer.Children.Clear();
foreach (var (stage, effect) in guideEntryRegistry.GuideEntries)
{
var hasMetabolites = effect.Metabolites?.Any() == true;
if (!effect.EffectDescriptions.Any() && !hasMetabolites)
continue;
var groupLabel = new RichTextLabel();
groupLabel.SetMarkup(Loc.GetString("guidebook-reagent-effects-metabolism-stage-rate",
("stage", _prototype.Index<MetabolismStagePrototype>(stage).LocalizedName), ("rate", effect.MetabolismRate)));
var descriptionLabel = new RichTextLabel
{
Margin = new Thickness(25, 0, 10, 0)
};
var descMsg = new FormattedMessage();
var descriptionsCount = effect.EffectDescriptions.Length;
var i = 0;
foreach (var effectString in effect.EffectDescriptions)
{
descMsg.AddMarkupOrThrow(effectString);
i++;
if (i < descriptionsCount || hasMetabolites)
descMsg.PushNewline();
}
if (hasMetabolites)
{
var metabolites = new List<string>();
foreach (var (metabolite, ratio) in effect.Metabolites!)
{
metabolites.Add(Loc.GetString("guidebook-reagent-effects-metabolite-item", ("rate", (double)ratio), ("reagent", _prototype.Index(metabolite).LocalizedName)));
}
metabolites.Sort();
descMsg.AddMarkupOrThrow(Loc.GetString("guidebook-reagent-effects-metabolites", ("items", ContentLocalizationManager.FormatList(metabolites))));
}
descriptionLabel.SetMessage(descMsg);
EffectsDescriptionContainer.AddChild(groupLabel);
EffectsDescriptionContainer.AddChild(descriptionLabel);
}
}
else
{
EffectsContainer.Visible = false;
}
#endregion
#region PlantMetabolisms
if (_chemistryGuideData.ReagentGuideRegistry.TryGetValue(reagent.ID, out var guideEntryRegistryPlant) &&
guideEntryRegistryPlant.PlantMetabolisms != null &&
guideEntryRegistryPlant.PlantMetabolisms.Count > 0)
{
PlantMetabolismsDescriptionContainer.Children.Clear();
var metabolismLabel = new RichTextLabel();
metabolismLabel.SetMarkup(Loc.GetString("guidebook-reagent-plant-metabolisms-rate"));
var descriptionLabel = new RichTextLabel
{
Margin = new Thickness(25, 0, 10, 0)
};
var descMsg = new FormattedMessage();
var descriptionsCount = guideEntryRegistryPlant.PlantMetabolisms.Count;
var i = 0;
foreach (var effectString in guideEntryRegistryPlant.PlantMetabolisms)
{
descMsg.AddMarkupOrThrow(effectString);
i++;
if (i < descriptionsCount)
descMsg.PushNewline();
}
descriptionLabel.SetMessage(descMsg);
PlantMetabolismsDescriptionContainer.AddChild(metabolismLabel);
PlantMetabolismsDescriptionContainer.AddChild(descriptionLabel);
}
else
{
PlantMetabolismsContainer.Visible = false;
}
#endregion
SourcesContainer.Visible = false;
FormattedMessage description = new();
description.AddText(_prototype.Index<EntityPrototype>(spawnId).Description);
ReagentDescription.SetMessage(description);
}
private void GenerateSources(ReagentPrototype reagent)
{
var sources = _chemistryGuideData.GetReagentSources(reagent.ID);
if (sources.Count == 0)
{
SourcesContainer.Visible = false;
return;
}
SourcesContainer.Visible = true;
var orderedSources = sources
.OrderBy(o => o.OutputCount)
.ThenBy(o => o.IdentifierString);
foreach (var source in orderedSources)
{
if (source is ReagentEntitySourceData entitySourceData)
{
SourcesDescriptionContainer.AddChild(new GuideReagentReaction(
entitySourceData.SourceEntProto,
entitySourceData.Solution,
entitySourceData.MixingType,
_prototype,
_systemManager));
}
else if (source is ReagentReactionSourceData reactionSourceData)
{
SourcesDescriptionContainer.AddChild(new GuideReagentReaction(
reactionSourceData.ReactionPrototype,
_prototype,
_systemManager));
}
else if (source is ReagentGasSourceData gasSourceData)
{
SourcesDescriptionContainer.AddChild(new GuideReagentReaction(
gasSourceData.GasPrototype,
gasSourceData.MixingType,
_prototype,
_systemManager));
}
}
}
}