public void AddPassage(Passage passage)
 {
     if (_passageContentMacroStack.Count != 0)
     {
         Logger.LogWarning("A macro isn´t closed before passage " + passage.Name + " begins");
     }
     _passageContentMacroStack.Clear();
     _lastPassageContent = null;
     _currentPassage = passage;
     _currentFormat = new PassageContentFormat();
     _tree.AddPassage(passage);
 }
        public PassageContentFormat Copy()
        {
            PassageContentFormat format = new PassageContentFormat();
            format.Bold = _bold;
            format.Monospace = _monospace;
            format.Comment = _comment;
            format.Italic = _italic;
            format.Strikeout = _strikeout;
            format.Subscript = _subscript;
            format.Superscript = _superscript;
            format.Underline = _underline;

            return format;
        }
        public void TestTreePassageFormatTextComplex()
        {
            Tree tree = TreeBuilder.createTree(_passageTagComplex);
            Assert.AreEqual("Start", tree.StartPassage.Name);
            Assert.AreEqual(4, tree.Passages.Count);

            Assert.AreEqual(3, tree.StartPassage.PassageContentList.Count);
            Assert.IsFalse(tree.StartPassage.PassageContentList[0].ContentFormat.Italic);
            Assert.IsTrue(tree.StartPassage.PassageContentList[1].ContentFormat.Italic);
            Assert.IsFalse(tree.StartPassage.PassageContentList[2].ContentFormat.Italic);

            Assert.NotNull(tree.Passages["StoryTitle"]);
            Passage passage1 = tree.Passages["StoryTitle"];

            Assert.AreEqual(5, passage1.PassageContentList.Count);
            PassageContentFormat format = new PassageContentFormat();

            format.Italic = true;
            Assert.AreEqual("\r\nYour story wil::l di[s]]play ", passage1.PassageContentList[0].PassageText.Text);
            Assert.IsTrue(format.euquals(passage1.PassageContentList[0].ContentFormat));

            format.Bold = true;
            Assert.AreEqual("this passage", passage1.PassageContentList[1].PassageText.Text);
            Assert.IsTrue(format.euquals(passage1.PassageContentList[1].ContentFormat));

            format.Bold = false;
            Assert.AreEqual(" first Edit it by double clicking it\r\n", passage1.PassageContentList[2].PassageText.Text);
            Assert.IsTrue(format.euquals(passage1.PassageContentList[2].ContentFormat));

            Assert.AreEqual("HIERTEXT", passage1.PassageContentList[3].PassageLink.DisplayText);
            Assert.IsTrue(format.euquals(passage1.PassageContentList[3].ContentFormat));

            format.Italic = false;
            Assert.AreEqual("\r\nUntitled Story\r\n", passage1.PassageContentList[4].PassageText.Text);
            Assert.IsTrue(format.euquals(passage1.PassageContentList[4].ContentFormat));

            Assert.NotNull(tree.Passages["myPassage"]);
            Passage passage2 = tree.Passages["myPassage"];

            Assert.AreEqual(4, passage2.PassageContentList.Count);
            format = new PassageContentFormat();

            Assert.AreEqual("you are ", passage2.PassageContentList[0].PassageText.Text);
            Assert.IsTrue(format.euquals(passage2.PassageContentList[0].ContentFormat));

            format.Italic = true;
            Assert.AreEqual("done\r\n", passage2.PassageContentList[1].PassageText.Text);
            Assert.IsTrue(format.euquals(passage2.PassageContentList[1].ContentFormat));

            Assert.AreEqual("start", passage2.PassageContentList[2].PassageText.Text);
            Assert.IsTrue(format.euquals(passage2.PassageContentList[2].ContentFormat));

            Assert.AreEqual("\r\n", passage2.PassageContentList[3].PassageText.Text);
            Assert.IsTrue(format.euquals(passage2.PassageContentList[3].ContentFormat));

            Assert.NotNull(tree.Passages["StoryAuthor"]);
            Passage passage3 = tree.Passages["StoryAuthor"];

            Assert.AreEqual(1, passage3.PassageContentList.Count);
            format = new PassageContentFormat();

            Assert.AreEqual("Anonymous x\r\n", passage3.PassageContentList[0].PassageText.Text);
            Assert.IsTrue(format.euquals(passage3.PassageContentList[0].ContentFormat));
        }
 public bool euquals(PassageContentFormat format)
 {
     return _bold == format.Bold &&
         _italic == format.Italic &&
         _monospace == format.Monospace &&
         _comment == format.Comment &&
         _strikeout == format.Strikeout &&
         _subscript == format.Subscript &&
         _superscript == format.Superscript &&
         _underline == format.Underline;
 }
        public void AddPassageContent(PassageContent passageContent)
        {
            // set format
            passageContent.ContentFormat = _currentFormat;
            _currentFormat = _currentFormat.Copy();

            PassageContent lastContent = _lastPassageContent;

            // merge text content together
            if (lastContent != null &&
                lastContent.Type == PassageContent.ContentType.TextContent &&
                passageContent.Type == PassageContent.ContentType.TextContent &&
                CurrentFormat.euquals(lastContent.ContentFormat))
            {
                lastContent.PassageText.MergePassageText(passageContent.PassageText);
                passageContent = lastContent;
            }
            else if (MacroStackCount == 0)
            {
                _currentPassage.AddPassageContent(passageContent);
            }
            else if (lastContent.Type == PassageContent.ContentType.MacroContent &&
                    lastContent.PassageMacro.MacroType == PassageMacro.PassageMarcroType.BranchMacro)
            {
                PassageMacro macro = _passageContentMacroStack.Last().PassageMacro;
                PassageMacroBranch branch = macro.PassageMacroBranch;

                branch.AddBranch(passageContent.PassageBranch);

                _passageContentMacroStack.Add(passageContent);
            }
            // stack: macro branch if - input: else ifelse
            else if (_passageContentMacroStack[_passageContentMacroStack.Count - 2].Type == PassageContent.ContentType.MacroContent &&
                    _passageContentMacroStack[_passageContentMacroStack.Count - 2].PassageMacro.MacroType == PassageMacro.PassageMarcroType.BranchMacro &&
                    _passageContentMacroStack.Last().Type == PassageContent.ContentType.BranchContent &&
                    _passageContentMacroStack.Last().PassageBranch.BranchType != PassageMacroBranchNode.MacroBranchType.Else &&
                    passageContent.Type == PassageContent.ContentType.BranchContent &&
                    passageContent.PassageBranch.BranchType != PassageMacroBranchNode.MacroBranchType.If)
            {
                FinishBranchStatement();
                PassageMacroBranch branch = _passageContentMacroStack.Last().PassageMacro.PassageMacroBranch;

                branch.AddBranch(passageContent.PassageBranch);

                _passageContentMacroStack.Add(passageContent);

            }
            else if (_passageContentMacroStack.Last().Type == PassageContent.ContentType.BranchContent)
            {
                PassageMacroBranchNode branchNode = _passageContentMacroStack.Last().PassageBranch;
                branchNode.AddPassageContent(passageContent);
            }
            else
            {
                throw new Exception("unknown case");
            }

            // put on stack if macro branch
            if (passageContent.Type == PassageContent.ContentType.MacroContent &&
                passageContent.PassageMacro.MacroType == PassageMacro.PassageMarcroType.BranchMacro)
            {
                _passageContentMacroStack.Add(passageContent);
            }

            _lastPassageContent = passageContent;
        }
        private void FinishBranchStatement()
        {
            _passageContentMacroStack.RemoveAt(_passageContentMacroStack.Count - 1);

            if (_passageContentMacroStack.Count == 0)
            {
                _lastPassageContent = _currentPassage.PassageContentList.Last();
                _currentFormat = _lastPassageContent.ContentFormat.Copy();
            }
            else
            {
                _lastPassageContent = _passageContentMacroStack.Last();
                _currentFormat = _lastPassageContent.ContentFormat.Copy();
            }
        }