예제 #1
0
        internal VisualTreeDetail ToVisualTreeDetail(VisualTreeConversionContext context)
        {
            var description = ValidateNotNull(Description, nameof(Description));

            int?repeat = context.GetRepeat(Repeat);

            if (repeat == null)
            {
                string relativePath = ValidateNotNull(Path, nameof(Path));
                ValidateNull(Format, nameof(Format), nameof(Repeat));
                ValidateNull(FormatPaths, nameof(FormatPaths), nameof(Repeat));

                return(new VisualTreeDetail(description, context.GetContext(relativePath)));
            }
            else
            {
                var format      = ValidateNotNull(Format, nameof(Format));
                var formatPaths = ValidateNotNull(FormatPaths, nameof(FormatPaths));
                var index       = ValidateNotNull(Index, nameof(Index));
                ValidateNull(Path, nameof(Path), nameof(Repeat));

                List <FormattableDescription> detailDescriptions = new List <FormattableDescription>();
                for (int i = 1; i <= repeat; i++)
                {
                    var formatElement = context.WithIndex(index, i).BuildDescription(format, formatPaths);
                    detailDescriptions.Add(formatElement);
                }
                return(new VisualTreeDetail(description, detailDescriptions));
            }
        }
예제 #2
0
        internal IEnumerable <VisualTreeNode> ConvertVisualNodes(VisualTreeNode?parent, VisualTreeConversionContext parentContext)
        {
            int?repeat       = parentContext.GetRepeat(Repeat);
            var relativePath = ValidateNotNull(Path, nameof(Path));

            if (repeat == null)
            {
                var context = parentContext.WithContextFromPath(relativePath);
                yield return(ToVisualTreeNode(context));
            }
            else
            {
                var index = ValidateNotNull(Index, nameof(Index));
                for (int i = 1; i <= repeat; i++)
                {
                    var context = parentContext.WithIndex(index, i).WithContextFromPath(relativePath);
                    yield return(ToVisualTreeNode(context));
                }
            }

            VisualTreeNode ToVisualTreeNode(VisualTreeConversionContext context)
            {
                FormattableDescription description        = BuildDescription(context);
                FormattableDescription?kitOnlyDescription = BuildKitOnlyDescription(context);

                Func <VisualTreeNode?, IReadOnlyList <VisualTreeNode> > childrenProvider = newNode => Children.SelectMany(child => child.ConvertVisualNodes(newNode, context)).ToList().AsReadOnly();
                var details         = Details.Select(detail => detail.ToVisualTreeDetail(context)).ToList().AsReadOnly();
                var midiNoteField   = MidiNotePath is null ? null : context.GetMidiNoteField(MidiNotePath);
                var kitIndex        = KitIndex == null ? (int?)null : context.GetIndex(KitIndex);
                var instrumentIndex = InstrumentIndex == null ? (int?)null : context.GetIndex(InstrumentIndex);

                return(new VisualTreeNode(parent, context.ContainerContext, childrenProvider, details, description, kitOnlyDescription, midiNoteField, kitIndex, instrumentIndex));
            }

            FormattableDescription BuildDescription(VisualTreeConversionContext context)
            {
                if (Description != null)
                {
                    ValidateNull(Format, nameof(Format), nameof(Description));
                    ValidateNull(FormatPaths, nameof(FormatPaths), nameof(Description));
                    return(new FormattableDescription(Description, null));
                }
                else
                {
                    var format      = ValidateNotNull(Format, nameof(Format));
                    var formatPaths = ValidateNotNull(FormatPaths, nameof(FormatPaths));
                    return(context.BuildDescription(format, formatPaths));
                }
            }

            FormattableDescription?BuildKitOnlyDescription(VisualTreeConversionContext context)
            {
                if (KitOnlyFormat is null)
                {
                    return(null);
                }
                else
                {
                    var formatPaths = ValidateNotNull(FormatPaths, nameof(FormatPaths));
                    return(context.BuildDescription(KitOnlyFormat, formatPaths));
                }
            }
        }