using System.Collections.Generic; using System.Linq; using Godot; using GodotExt; using JetBrains.Annotations; using OpenScadGraphEditor.Actions; using OpenScadGraphEditor.Library; using OpenScadGraphEditor.Nodes; using OpenScadGraphEditor.Refactorings; namespace OpenScadGraphEditor.Widgets.VariableCustomizer { [UsedImplicitly] public class VariableCustomizer : MarginContainer { private Control _sectionTemplate; private Control _variableTemplate; private Control _sectionsContainer; private readonly Dictionary _sections = new Dictionary(); private readonly Dictionary _variables = new Dictionary(); private readonly Dictionary _variablesByName = new Dictionary(); private IEditorContext _context; public override void _Ready() { // get the templates and remove them from the tree so they don't render _sectionTemplate = this.WithName("SectionTemplate"); _variableTemplate = this.WithName("VariableTemplate"); _sectionTemplate.RemoveFromParent(); _variableTemplate.RemoveFromParent(); _sectionsContainer = this.WithName("Sections"); } public void Setup(IEditorContext context, List variables) { _context = context; // fill the variables by name dictionary _variablesByName.Clear(); foreach (var variable in variables) { _variablesByName.Add(variable.Name, variable); } // first get all the variables from the project and group them by section var variableGroups = variables // only variables in the customizer .Where(it => it.ShowInCustomizer) .GroupBy(v => v.CustomizerDescription.Tab) .ToList(); // add a section for variables that don't appear in the customizer var otherVariables = variables .Where(it => !it.ShowInCustomizer) .ToList(); variableGroups.AddRange(otherVariables.GroupBy(it => "[Not in customizer]")); foreach (var variableGroup in variableGroups) { var sectionName = variableGroup.Key; if (sectionName == "") { sectionName = "Parameters"; } var variablesInSection = variableGroup.ToList(); // get the section control var section = CreateOrGetSection(sectionName); var sectionContents = section.WithName("SectionContents"); // build all variables in the section foreach (var variable in variablesInSection) { var variableControl = CreateOrGetVariable(variable); // move it below the section variableControl.MoveToNewParent(sectionContents); } } // kill all unused variables var unusedVariables = _variables.Keys.Except(variables .Select(it => it.Name)).ToList(); foreach (var unusedVariable in unusedVariables) { _variables[unusedVariable].RemoveAndFree(); _variables.Remove(unusedVariable); } // kill all unused sections var unusedSections = _sections.Keys .Except(variableGroups.Select(it => it.Key == "" ? "Parameters" : it.Key)) .ToList(); foreach (var unusedSection in unusedSections) { _sections[unusedSection].RemoveAndFree(); _sections.Remove(unusedSection); } } private Control CreateOrGetSection(string name) { if (_sections.TryGetValue(name, out var result)) { return result; } // create a clone of the section template result = _sectionTemplate.Duplicate() as Control; // set the name result.WithName