Ejemplo n.º 1
0
        /// <summary>
        /// Checks if all text elements inside the given container are superscript.
        /// </summary>
        /// <returns> <c>true</c> if all text is superscript (level 1); <c>false</c> otherwise. </returns>
        private bool AllTextIsSuperscript(IInlineContainer container, int superscriptLevel = 0)
        {
            foreach (var inline in container.Inlines)
            {
                if (inline is SuperscriptTextInline textInline)
                {
                    // Remove any nested superscripts.
                    if (AllTextIsSuperscript(textInline, superscriptLevel + 1) == false)
                    {
                        return(false);
                    }
                }
                else if (inline is IInlineContainer)
                {
                    // Remove any superscripts.
                    if (AllTextIsSuperscript((IInlineContainer)inline, superscriptLevel) == false)
                    {
                        return(false);
                    }
                }
                else if (inline is IInlineLeaf && !ParseHelpers.IsMarkdownBlankOrWhiteSpace(((IInlineLeaf)inline).Text))
                {
                    if (superscriptLevel != 1)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Removes all superscript elements from the given container.
        /// </summary>
        private void RemoveSuperscriptRuns(IInlineContainer container, bool insertCaret)
        {
            for (int i = 0; i < container.Inlines.Count; i++)
            {
                var inline = container.Inlines[i];
                if (inline is SuperscriptTextInline textInline)
                {
                    // Remove any nested superscripts.
                    RemoveSuperscriptRuns(textInline, insertCaret);

                    // Remove the superscript element, insert all the children.
                    container.Inlines.RemoveAt(i);
                    if (insertCaret)
                    {
                        container.Inlines.Insert(i++, new TextRunInline {
                            Text = "^"
                        });
                    }

                    foreach (var superscriptInline in textInline.Inlines)
                    {
                        container.Inlines.Insert(i++, superscriptInline);
                    }

                    i--;
                }
                else if (inline is IInlineContainer)
                {
                    // Remove any superscripts.
                    RemoveSuperscriptRuns((IInlineContainer)inline, insertCaret);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Checks if all text elements inside the given container are superscript.
        /// </summary>
        /// <returns> <c>true</c> if all text is superscript (level 1); <c>false</c> otherwise. </returns>
        private bool AllTextIsSuperscript(IInlineContainer container, int superscriptLevel = 0)
        {
            foreach (var inline in container.Inlines)
            {
                var textInline = inline as SuperscriptTextInline;
                if (textInline != null)
                {
                    // Remove any nested superscripts.
                    if (AllTextIsSuperscript(textInline, superscriptLevel + 1) == false)
                    {
                        return(false);
                    }
                }
                else if (inline is IInlineContainer)
                {
                    // Remove any superscripts.
                    if (AllTextIsSuperscript((IInlineContainer)inline, superscriptLevel) == false)
                    {
                        return(false);
                    }
                }
                else if (inline is IInlineLeaf && !Common.IsBlankOrWhiteSpace(((IInlineLeaf)inline).Text))
                {
                    if (superscriptLevel != 1)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Ejemplo n.º 4
0
        internal static PlainText AppendWithLineInfo(this IInlineContainer container, string text, int lineNumber1, int linePosition1, int lineNumber2, int linePosition2)
        {
            Debug.Assert(container != null);
            Debug.Assert(text != null);
            Debug.Assert(lineNumber1 >= 0);
            Debug.Assert(linePosition1 >= 0);
            Debug.Assert(lineNumber2 >= 0);
            Debug.Assert(linePosition2 >= 0);
            Debug.Assert(lineNumber1 < lineNumber2 || lineNumber1 == lineNumber2 && linePosition1 <= linePosition2);
            var pt = container.Inlines.LastNode as PlainText;

            if (pt == null)
            {
                container.Inlines.Add(pt = new PlainText(text));
                pt.SetLineInfo(lineNumber1, linePosition1, lineNumber2, linePosition2);
            }
            else
            {
                if (text.Length == 0)
                {
                    return(pt);                     // ExtendLineInfo won't accept (0)
                }
                pt.Content += text;
                pt.ExtendLineInfo(lineNumber2, linePosition2);
            }
            ((Node)container).ExtendLineInfo(lineNumber2, linePosition2);
            return(pt);
        }
        /// <summary>
        /// Removes all superscript elements from the given container.
        /// </summary>
        private void RemoveSuperscriptRuns(IInlineContainer container, bool insertCaret)
        {
            for (int i = 0; i < container.Inlines.Count; i++)
            {
                var inline = container.Inlines[i];
                if (inline is SuperscriptTextInline textInline)
                {
                    RemoveSuperscriptRuns(textInline, insertCaret);

                    container.Inlines.RemoveAt(i);
                    if (insertCaret)
                    {
                        container.Inlines.Insert(i++, new TextRunInline {
                            Text = "^"
                        });
                    }

                    foreach (var superscriptInline in textInline.Inlines)
                    {
                        container.Inlines.Insert(i++, superscriptInline);
                    }

                    i--;
                }
                else if (inline is IInlineContainer)
                {
                    RemoveSuperscriptRuns((IInlineContainer)inline, insertCaret);
                }
            }
        }
Ejemplo n.º 6
0
 public void Clear(IInlineContainer parent)
 {
     for (var i = parent.Children.Count - 1; i >= 0; i--)
     {
         var child = parent.Children[i];
         parent.RemoveContent(child);
     }
 }
Ejemplo n.º 7
0
    public static SimpleInlineElement AppendContent(this IInlineContainer container, string text,
                                                    IStyleDefinition?localStyle = null)
    {
        var element = new SimpleInlineElement()
        {
            Text = text
        };

        localStyle?.MergeInto(element.LocalStyles);
        container.AppendContent(element);
        return(element);
    }
Ejemplo n.º 8
0
    public static SimpleInlineElement AppendContent(this IInlineContainer container, string text,
                                                    params string[] styles)
    {
        var element = AppendContent(container, text);

        foreach (var style in styles)
        {
            element.AddStyle(style);
        }

        return(element);
    }
Ejemplo n.º 9
0
    public void Append(IInlineContainer parent, InlineElement inlineElement)
    {
        if (inlineElement.Parent != null)
        {
            inlineElement.Paragraph?.Invalidate(Paragraph.InvalidationFlags.TextFlow);
            inlineElement.Parent?.RemoveContent(inlineElement);
        }

        _children ??= new List <InlineElement>();
        _children.Add(inlineElement);

        inlineElement.Parent = parent;
        inlineElement.Paragraph?.Invalidate(Paragraph.InvalidationFlags.TextFlow);
    }
Ejemplo n.º 10
0
        /// <summary>
        /// Append a <see cref="PlainText"/> node to the beginning of the paragraph.
        /// </summary>
        /// <param name="text">The text to be inserted.</param>
        /// <returns>Either the new <see cref="PlainText"/> node inserted, or the existing <see cref="PlainText"/> at the beginning of the paragraph.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="text"/> is <c>null</c>.</exception>
        public static PlainText Prepend(this IInlineContainer container, string text)
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }
            var pt = container.Inlines.FirstNode as PlainText;

            if (pt == null)
            {
                container.Inlines.AddFirst(pt = new PlainText());
            }
            pt.Content = text + pt.Content;
            return(pt);
        }
Ejemplo n.º 11
0
    public void Remove(IInlineContainer parent, InlineElement inlineElement)
    {
        if (inlineElement.Parent != parent)
        {
            throw new ArgumentException("Element is not in this parent");
        }

        inlineElement.Paragraph?.Invalidate(Paragraph.InvalidationFlags.TextFlow);

        if (_children != null && _children.Remove(inlineElement))
        {
            if (_children.Count == 0)
            {
                _children = null;
            }
        }

        inlineElement.Parent = null;
    }
Ejemplo n.º 12
0
        /// <summary>
        /// RUN
        /// </summary>
        /// <returns><c>true</c> if one or more nodes has been parsed.</returns>
        private bool ParseRun(RunParsingMode mode, IInlineContainer container, bool setLineNumber)
        {
            ParseStart();
            var parsedAny = false;

            while (!NeedsTerminate())
            {
                // Read more
                InlineNode inline = null;
                if ((inline = ParseExpandable()) != null)
                {
                    goto NEXT;
                }
                switch (mode)
                {
                case RunParsingMode.Run:     // RUN
                    if ((inline = ParseInline()) != null)
                    {
                        goto NEXT;
                    }
                    break;

                case RunParsingMode.ExpandableText:     // EXPANDABLE_TEXT
                    if ((inline = ParsePartialPlainText()) != null)
                    {
                        goto NEXT;
                    }
                    break;

                case RunParsingMode.ExpandableUrl:     // EXPANDABLE_URL
                    if ((inline = ParseUrlText()) != null)
                    {
                        goto NEXT;
                    }
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(mode), mode, null);
                }
                break;
NEXT:
                parsedAny = true;
                // Remember that ParsePartialText stops whenever there's a susceptable termination of PLAIN_TEXT
                // So we need to marge the consequent PlainText objects.
                if (inline is PlainText newtext)
                {
                    if (container.Inlines.LastNode is PlainText lastText)
                    {
                        lastText.Content += newtext.Content;
                        lastText.ExtendLineInfo(lineNumber, linePosition);
                        continue;
                    }
                }
                container.Inlines.Add(inline);
            }
            // Note that the content of RUN should not be empty.
            if (parsedAny)
            {
                ParseSuccessful((Node)container, setLineNumber);
                return(true);
            }
            else
            {
                Fallback();
                return(false);
            }
        }
Ejemplo n.º 13
0
    private void AppendHitPointDescription(GameObject critter, IInlineContainer content)
    {
        var subdualDamage = critter.GetStat(Stat.subdual_damage);
        var currentHp     = critter.GetStat(Stat.hp_current);
        var maxHp         = critter.GetStat(Stat.hp_max);

        StyleDefinition hpStyleDefinition = null;

        if (critter.IsPC())
        {
            if (currentHp < maxHp)
            {
                hpStyleDefinition = new StyleDefinition()
                {
                    Color = new PackedLinearColorA(0xFFFF0000)
                };
            }
        }
        else if (GameSystems.Critter.IsDeadNullDestroyed(critter) || currentHp <= 0)
        {
            hpStyleDefinition = new StyleDefinition()
            {
                Color = new PackedLinearColorA(0xFF7F7F7F)
            };
        }
        else
        {
            var injuryLevel = UiSystems.Tooltip.GetInjuryLevel(critter);
            hpStyleDefinition = new StyleDefinition()
            {
                Color = UiSystems.Tooltip.GetInjuryLevelColor(injuryLevel)
            };
        }

        if (critter.IsPC())
        {
            content.AppendContent(_translations[103]);
            content.AppendContent(": ");
            content.AppendContent(currentHp.ToString(), hpStyleDefinition);
            content.AppendContent($"/{maxHp}");

            if (subdualDamage > 0)
            {
                content.AppendContent($"({subdualDamage})", CommonStyles.HpSubdualDamage);
            }
        }
        else if (GameSystems.Critter.IsDeadNullDestroyed(critter) || currentHp <= 0)
        {
            content.AppendContent(_translations[108]);
            content.AppendContent(": ");
            content.AppendContent((maxHp - currentHp).ToString(), hpStyleDefinition);
        }
        else
        {
            content.AppendContent(GetInjuryLevelDescription(critter), hpStyleDefinition);

            if (subdualDamage <= 0)
            {
                // Show the amount of damage dealt so far
                if (currentHp < maxHp)
                {
                    content.AppendContent("\n");
                    content.AppendContent(_translations[108]); // "Damage"
                    content.AppendContent(": ");
                    content.AppendContent((maxHp - currentHp).ToString(), hpStyleDefinition);
                }
            }
            else
            {
                if (subdualDamage >= maxHp)
                {
                    content.AppendContent("\n");
                    content.AppendContent(_translations[108]); // "Damage"
                    content.AppendContent(": ");
                    content.AppendContent($"({subdualDamage})", CommonStyles.HpSubdualDamage);
                }
                else
                {
                    content.AppendContent("\n");
                    content.AppendContent(_translations[108]); // "Damage"
                    content.AppendContent(": @1");
                    content.AppendContent((maxHp - currentHp).ToString(), hpStyleDefinition);
                    content.AppendContent("@0 ");
                    content.AppendContent($"({subdualDamage})", CommonStyles.HpSubdualDamage);
                }
            }
        }
    }
Ejemplo n.º 14
0
        /// <summary>
        /// Removes all superscript elements from the given container.
        /// </summary>
        /// <param name="container"></param>
        /// <param name="insertCaret"></param>
        private void RemoveSuperscriptRuns(IInlineContainer container, bool insertCaret)
        {
            for (int i = 0; i < container.Inlines.Count; i ++)
            {
                var inline = container.Inlines[i];
                if (inline is SuperscriptTextInline)
                {
                    // Remove any nested superscripts.
                    RemoveSuperscriptRuns((IInlineContainer)inline, insertCaret);

                    // Remove the superscript element, insert all the children.
                    container.Inlines.RemoveAt(i);
                    if (insertCaret)
                        container.Inlines.Insert(i++, new TextRunInline { Text = "^" });
                    foreach (var superscriptInline in ((SuperscriptTextInline)inline).Inlines)
                        container.Inlines.Insert(i++, superscriptInline);
                    i--;
                }
                else if (inline is IInlineContainer)
                {
                    // Remove any superscripts.
                    RemoveSuperscriptRuns((IInlineContainer)inline, insertCaret);
                }
            }
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Checks if all text elements inside the given container are superscript.
 /// </summary>
 /// <param name="container"></param>
 /// <returns> <c>true</c> if all text is superscript (level 1); <c>false</c> otherwise. </returns>
 private bool AllTextIsSuperscript(IInlineContainer container, int superscriptLevel = 0)
 {
     foreach (var inline in container.Inlines)
     {
         if (inline is SuperscriptTextInline)
         {
             // Remove any nested superscripts.
             if (AllTextIsSuperscript((IInlineContainer)inline, superscriptLevel + 1) == false)
                 return false;
         }
         else if (inline is IInlineContainer)
         {
             // Remove any superscripts.
             if (AllTextIsSuperscript((IInlineContainer)inline, superscriptLevel) == false)
                 return false;
         }
         else if (inline is IInlineLeaf && !Common.IsBlankOrWhiteSpace(((IInlineLeaf)inline).Text))
         {
             if (superscriptLevel != 1)
                 return false;
         }
     }
     return true;
 }