protected string GetGotoText(DialogueNode node)
 {
     if (node != null)
     {
         if (node is DialogueNodeSentence)
         {
             if (checkBoxOptionConstants.Checked)
             {
                 return(EditorHelper.FormatTextEntry((node as DialogueNodeSentence).Sentence, EditorCore.LanguageWorkstring));
             }
             else
             {
                 return((node as DialogueNodeSentence).Sentence);
             }
         }
         else if (node is DialogueNodeChoice)
         {
             return((node as DialogueNodeChoice).Choice);
         }
         else if (node is DialogueNodeGoto)
         {
             return(GetGotoText((node as DialogueNodeGoto).Goto));
         }
     }
     return("");
 }
        private void OnListBoxReplies_DrawItem(object sender, DrawItemEventArgs e)
        {
            var listBox = sender as ListBox;

            if (e.Index < 0 || e.Index >= listBox.Items.Count)
            {
                return;
            }

            var g    = e.Graphics;
            var item = listBox.Items[e.Index] as DialogueNodeReply;

            var color = Color.Black;

            if (!groupBoxChoice.Enabled)
            {
                color = Color.LightGray;
            }
            else if (item.Conditions.Count > 0 && radioButtonReplyConditionsFalse.Checked)
            {
                color = Color.Gray;
            }

            StringBuilder stringBuilder = new StringBuilder();

            if (checkBoxShowReplyConditions.Checked)
            {
                foreach (NodeCondition condition in item.Conditions)
                {
                    stringBuilder.AppendFormat("[{0}] ", condition.GetDisplayText());
                }
            }

            if (checkBoxOptionConstants.Checked)
            {
                stringBuilder.Append(EditorHelper.FormatTextEntry(item.Reply, EditorCore.LanguageWorkstring));
            }
            else
            {
                stringBuilder.Append(item.Reply);
            }

            e.DrawBackground();

            if (e.State.HasFlag(DrawItemState.Selected))
            {
                g.FillRectangle(new SolidBrush(Color.LightBlue), e.Bounds);
            }
            //else
            //    g.FillRectangle(new SolidBrush(Color.White), e.Bounds);

            g.DrawString(stringBuilder.ToString(), fontReplies, new SolidBrush(color), new PointF(e.Bounds.X, e.Bounds.Y));

            e.DrawFocusRectangle();
        }
        static public void ExportFaceFx_Internal(string directory, Project project, List <Dialogue> dialogues, List <Language> languages, bool workstringOnly)
        {
            foreach (Language language in languages)
            {
                string languagePath = Path.Combine(directory, language.LocalizationCode);
                foreach (Dialogue dialogue in dialogues)
                {
                    var orderedListNodes = new List <DialogueNode>();
                    dialogue.GetOrderedNodes(ref orderedListNodes);
                    foreach (DialogueNode dialogueNode in orderedListNodes)
                    {
                        if (dialogueNode is DialogueNodeSentence || dialogueNode is DialogueNodeReply)
                        {
                            string nodeVoicingID = EditorHelper.GetPrettyNodeVoicingID(dialogue, dialogueNode);
                            string path          = Path.Combine(languagePath, nodeVoicingID + ".txt");

                            string lineToWrite = "";
                            if (dialogueNode is DialogueNodeSentence)
                            {
                                lineToWrite = (dialogueNode as DialogueNodeSentence).Sentence;
                            }
                            else
                            {
                                lineToWrite = (dialogueNode as DialogueNodeReply).Reply;
                            }

                            if (!workstringOnly)
                            {
                                var entry = dialogue.Translations.GetNodeEntry(dialogueNode, language);
                                lineToWrite = (entry != null) ? entry.Text : "";
                            }

                            //Export empy files
                            //if (lineToWrite.Count() == 0)
                            //    continue;

                            lineToWrite = EditorHelper.FormatTextEntry(lineToWrite, language);

                            if (!Directory.Exists(languagePath))
                            {
                                Directory.CreateDirectory(languagePath);
                            }

                            using (System.IO.StreamWriter file = new System.IO.StreamWriter(path, false, Encoding.UTF8))
                            {
                                file.WriteLine(lineToWrite);
                            }
                        }
                    }
                }
            }
        }
        static public void ExportDialoguesToWwiseTextFile(string directory, Project project, List <Dialogue> dialogues, Language language, DateTime dateFrom)
        {
            string path = Path.Combine(directory, "Wwise_" + project.GetName() + "_" + language.VoicingCode + ".txt");

            using (System.IO.StreamWriter file = new System.IO.StreamWriter(path, false, Encoding.Unicode))
            {
                file.WriteLine("Filename\tContainer\tSVComment\tComment");

                foreach (Dialogue dialogue in dialogues)
                {
                    var orderedListNodes = new List <DialogueNode>();
                    dialogue.GetOrderedNodes(ref orderedListNodes);
                    foreach (DialogueNode dialogueNode in orderedListNodes)
                    {
                        if (dialogueNode is DialogueNodeSentence)
                        {
                            DialogueNodeSentence dialogueNodeSentence = dialogueNode as DialogueNodeSentence;
                            //if (dialogueNodeSentence.LastEditDate < dateFrom)   //No need for that, reexport everything
                            //    continue;

                            string nodeID     = EditorHelper.GetPrettyNodeVoicingID(dialogue, dialogueNodeSentence);
                            string container  = dialogue.SceneType;
                            string speaker    = project.GetActorName(dialogueNodeSentence.SpeakerID);
                            string voicedText = EditorHelper.FormatTextEntry(dialogueNodeSentence.Sentence, language);  //language = workstring if workstringOnly

                            if (speaker == "")
                            {
                                speaker = "Undefined";
                            }

                            //Filename, Container, SVComment, Comment
                            file.WriteLine(nodeID + "\t" + container + "\t" + speaker + "\t" + voicedText);
                        }
                    }
                }
            }
        }
        static public void ExportLocasToCSVFile(string directory, Project project, List <Dialogue> dialogues, Language language, bool workstringOnly, bool workstringFallback, DateTime dateFrom, ExporterStats.ProjectStats projectStats)
        {
            string path = Path.Combine(directory, "Loca_" + project.GetName() + "_" + language.VoicingCode + ".csv");

            using (System.IO.StreamWriter file = new System.IO.StreamWriter(path, false, Encoding.UTF8))
            {
                //status, scene, voicing id, index, speaker, context, voicing comments, intensity, sentence, comments
                {
                    ExporterCsv.CsvLineWriter header = new ExporterCsv.CsvLineWriter();
                    header.AddField("Status");
                    header.AddField("Dialogue");
                    header.AddField("Voicing ID");
                    header.AddField("Index");
                    header.AddField("Speaker");
                    header.AddField("Context");
                    header.AddField("Voicing Comments");
                    header.AddField("Voicing Intensity");
                    header.AddField("Sentence");
                    header.AddField("Comments");
                    header.WriteLine(file);
                }

                foreach (Dialogue dialogue in dialogues)
                {
                    int index = 1;

                    var orderedListNodes = new List <DialogueNode>();
                    dialogue.GetOrderedNodes(ref orderedListNodes);
                    foreach (DialogueNode dialogueNode in orderedListNodes)
                    {
                        if (dialogueNode is DialogueNodeSentence)
                        {
                            DialogueNodeSentence dialogueNodeSentence = dialogueNode as DialogueNodeSentence;

                            bool done = false;
                            if (dialogueNodeSentence.LastEditDate < dateFrom)
                            {
                                done = true;
                            }

                            string nodeID    = EditorHelper.GetPrettyNodeVoicingID(dialogue, dialogueNodeSentence);
                            string speaker   = project.GetActorName(dialogueNodeSentence.SpeakerID);
                            string scene     = dialogue.GetName();
                            string context   = dialogueNodeSentence.Context;
                            string comment   = dialogueNodeSentence.Comment;
                            string intensity = dialogueNodeSentence.VoiceIntensity;
                            string status    = (done) ? "OK" : "TODO";

                            string voicedText = dialogueNodeSentence.Sentence;
                            if (!workstringOnly)
                            {
                                TranslationEntry entry = dialogue.Translations.GetNodeEntry(dialogueNode, language);
                                if (entry == null)
                                {
                                    if (!workstringFallback)
                                    {
                                        voicedText = "<Missing Translation>";
                                    }
                                }
                                else
                                {
                                    voicedText = entry.Text;
                                }
                            }

                            // use null as langage to force constant replacement in csv file
                            voicedText = EditorHelper.FormatTextEntry(voicedText, null);  //language = workstring if workstringOnly

                            //voicedText = voicedText.Replace("’", "'");
                            //voicedText = voicedText.Replace("…", "...");

                            if (speaker == "")
                            {
                                speaker = "<Undefined>";
                            }

                            //status, scene, node id, index, speaker, context, voicing comments, intensity, sentence, comments
                            ExporterCsv.CsvLineWriter line = new ExporterCsv.CsvLineWriter();
                            line.AddField(status);
                            line.AddField(scene);
                            line.AddField(nodeID);
                            line.AddField(index);
                            line.AddField(speaker);
                            line.AddField(context);
                            line.AddField(comment);
                            line.AddField(intensity);
                            line.AddField(voicedText);
                            line.AddEmptyField();
                            line.WriteLine(file);

                            ++index;

                            if (!done)
                            {
                                projectStats.AddSentence(dialogue, dialogueNodeSentence.SpeakerID, voicedText);
                            }
                        }
                    }
                }
            }
        }
        protected void PlayNode(DialogueNode nextNode, bool forward = true)
        {
            if (forward && currentNode != null && !(currentNode is DialogueNodeReply) && !(currentNode is DialogueNodeGoto))
            {
                previousNodes.Add(currentNode);
            }

            if (nextNode == null)
            {
                Finish();
                return;
            }

            currentNode = nextNode;

            currentDocument.SelectNode(currentNode);

            ResetGroups();

            if (currentNode.Conditions.Count > 0 && GetCurrentOptionConditions() == EOptionConditions.AlwaysFalse)
            {
                PlayNode(currentNode.Next);
                return;
            }

            if (currentNode is DialogueNodeSentence)
            {
                var sentence = currentNode as DialogueNodeSentence;

                ResetSentence();

                Actor speaker = ResourcesHandler.Project.GetActorFromID(sentence.SpeakerID);
                if (speaker != null)
                {
                    labelSpeaker.Text = speaker.Name;

                    string strPathPortraitSpeaker = Path.Combine(EditorHelper.GetProjectDirectory(), speaker.Portrait);
                    if (File.Exists(strPathPortraitSpeaker))
                    {
                        pictureBoxSpeaker.Image = Image.FromFile(strPathPortraitSpeaker);
                    }
                }

                Actor listener = ResourcesHandler.Project.GetActorFromID(sentence.ListenerID);
                if (listener != null)
                {
                    labelListener.Text = listener.Name;

                    string strPathPortraitListener = Path.Combine(EditorHelper.GetProjectDirectory(), listener.Portrait);
                    if (File.Exists(strPathPortraitListener))
                    {
                        pictureBoxListener.Image = Image.FromFile(strPathPortraitListener);
                    }
                }

                if (checkBoxOptionConstants.Checked)
                {
                    labelSentence.Text = EditorHelper.FormatTextEntry(sentence.Sentence, EditorCore.LanguageWorkstring);
                }
                else
                {
                    labelSentence.Text = sentence.Sentence;
                }
            }
            else if (currentNode is DialogueNodeChoice)
            {
                var choice = currentNode as DialogueNodeChoice;

                groupBoxChoice.Visible    = true;
                labelChoice.Text          = String.Format("Choice : {0}", choice.Choice);
                listBoxReplies.DataSource = new BindingSource(choice.Replies, null);

                checkBoxShowReplyConditions.Checked    = false;
                radioButtonReplyConditionsTrue.Checked = true;

                foreach (DialogueNodeReply reply in choice.Replies)
                {
                    if (reply.Conditions.Count > 0)
                    {
                        labelRepliesConditions.Enabled          = true;
                        checkBoxShowReplyConditions.Enabled     = true;
                        radioButtonReplyConditionsTrue.Enabled  = true;
                        radioButtonReplyConditionsFalse.Enabled = true;
                        break;
                    }
                }

                if (choice.Conditions.Count > 0 && GetCurrentOptionConditions() == EOptionConditions.AskEveryTime)
                {
                    ShowConditions(choice);
                }
            }
            else if (currentNode is DialogueNodeGoto)
            {
                var nodeGoto = currentNode as DialogueNodeGoto;

                if (nodeGoto.Conditions.Count > 0 && GetCurrentOptionConditions() == EOptionConditions.AskEveryTime)
                {
                    groupBoxGoto.Visible = true;
                    labelGoto.Text       = String.Format("Goto > \"{0}\"", GetGotoText(nodeGoto.Goto));

                    ShowConditions(nodeGoto);
                }
                else
                {
                    PlayNode(nodeGoto.Goto);
                }
            }
            else if (currentNode is DialogueNodeBranch)
            {
                var nodeBranch = currentNode as DialogueNodeBranch;

                if (nodeBranch.Conditions.Count > 0 && GetCurrentOptionConditions() == EOptionConditions.AskEveryTime)
                {
                    groupBoxGoto.Visible = true;
                    labelGoto.Text       = String.Format("Branch > \"{0}\"", GetGotoText(nodeBranch.Branch));

                    ShowConditions(nodeBranch);
                }
                else
                {
                    PlayNode(nodeBranch.Branch);
                }
            }
        }
        static public void ExportToPO(string directory, Project project, List <Dialogue> dialogues, Language language, bool workstringOnly, bool workstringFallback)
        {
            string path = Path.Combine(directory, "Dialogues.po");

            var  locas     = new Dictionary <string, POEntry>();
            bool isDefault = workstringOnly || (project.GetDefaultLanguage() == language);

            //Fill existing locas

            /*if (File.Exists(path))
             * {
             *  using (System.IO.StreamReader file = new System.IO.StreamReader(path, Encoding.UTF8))
             *  {
             *      while (!file.EndOfStream)
             *      {
             *          string lineId = file.ReadLine();
             *          if (lineId.StartsWith("msgid"))
             *          {
             *              lineId = lineId.Replace("msgid", "");
             *              lineId = lineId.Trim();
             *              lineId = lineId.Substring(1);
             *              lineId = lineId.Substring(0, lineId.Length - 1);
             *
             *              string lineStr = file.ReadLine();
             *              if (lineStr.StartsWith("msgstr"))
             *              {
             *                  lineStr = lineStr.Replace("msgstr", "");
             *                  lineStr = lineStr.Trim();
             *                  lineStr = lineStr.Substring(1);
             *                  lineStr = lineStr.Substring(0, lineStr.Length - 1);
             *
             *                  if (!locas.ContainsKey(lineId))
             *                      locas.Add(lineId, new POEntry() { text = lineStr });
             *              }
             *          }
             *      }
             *  }
             * }*/

            //Parse texts to localize and fill nodes references
            foreach (Dialogue dialogue in dialogues)
            {
                var orderedListNodes = new List <DialogueNode>();
                dialogue.GetOrderedNodes(ref orderedListNodes);
                foreach (DialogueNode dialogueNode in orderedListNodes)
                {
                    if (dialogueNode is DialogueNodeSentence || dialogueNode is DialogueNodeReply)
                    {
                        string workstring;
                        if (dialogueNode is DialogueNodeSentence)
                        {
                            workstring = (dialogueNode as DialogueNodeSentence).Sentence;
                        }
                        else
                        {
                            workstring = (dialogueNode as DialogueNodeReply).Reply;
                        }

                        POEntry entry;
                        if (!locas.TryGetValue(workstring, out entry))
                        {
                            string localizedText = workstring;
                            if (!workstringOnly)
                            {
                                TranslationEntry translation = dialogue.Translations.GetNodeEntry(dialogueNode, language);
                                if (translation == null)
                                {
                                    if (!workstringFallback)
                                    {
                                        localizedText = "";
                                    }
                                }
                                else
                                {
                                    localizedText = translation.Text;
                                }
                            }

                            localizedText = EditorHelper.FormatTextEntry(localizedText, language);  //language = workstring if workstringOnly

                            entry = new POEntry()
                            {
                                text = localizedText
                            };
                            locas.Add(workstring, entry);
                        }

                        entry.nodes.Add(EditorHelper.GetPrettyNodeID(dialogue, dialogueNode));
                    }
                }
            }

            //Re-write file
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(path, false, Encoding.UTF8))
            {
                string date = Utility.GetDateAsString(Utility.GetCurrentTime());

                //Header
                file.WriteLine("# Dialogues " + language.Name + " translation.");
                file.WriteLine("# ");
                file.WriteLine("msgid \"\"");
                file.WriteLine("msgstr \"\"");
                file.WriteLine("\"Project-Id-Version: Dialogues\\n\"");
                file.WriteLine("\"POT-Creation-Date: " + date + "\\n\"");
                file.WriteLine("\"PO-Revision-Date: " + date + "\\n\"");
                file.WriteLine("\"Language-Team: \\n\"");
                file.WriteLine("\"Language: " + language.LocalizationCode + "\\n\"");
                file.WriteLine("\"MIME-Version: 1.0\\n\"");
                file.WriteLine("\"Content-Type: text/plain; charset=UTF-8\\n\"");
                file.WriteLine("\"Content-Transfer-Encoding: 8bit\\n\"");
                file.WriteLine("\"Plural-Forms: nplurals=2; plural=(n != 1);\\n\"");
            }

            //Write entries
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(path, true, Encoding.UTF8))
            {
                foreach (var kvp in locas)
                {
                    if (kvp.Value.nodes.Count > 0)
                    {
                        file.WriteLine("");
                        foreach (string node in kvp.Value.nodes)
                        {
                            file.WriteLine(String.Format("#: {0}", node));
                        }
                        file.WriteLine(String.Format("msgid \"{0}\"", kvp.Key));
                        file.WriteLine(String.Format("msgstr \"{0}\"", kvp.Value.text));
                    }
                }
            }
        }