/// <summary> /// Renders a dialog step /// </summary> /// <param name="data">Dialog Step Data</param> /// <param name="flexFieldObject">Flex field object to which the dialog belongs</param> /// <returns>Dialog Step Render Result</returns> public async Task <ExportDialogStepRenderResult> RenderDialogStep(ExportDialogData data, FlexFieldObject flexFieldObject) { KortistoNpc npc = flexFieldObject as KortistoNpc; if (npc == null) { return(null); } TaleChoiceNode choiceNode = data.Choice; if (choiceNode == null) { return(null); } ExportTemplate template = await _defaultTemplateProvider.GetDefaultTemplateByType(_project.Id, TemplateType.TaleChoice); ExportDialogStepRenderResult renderResult = new ExportDialogStepRenderResult(); renderResult.StepCode = ReplaceNodeId(template.Code, data); renderResult.StepCode = ExportUtil.BuildRangePlaceholderRegex(Placeholder_ChoicesStart, Placeholder_ChoicesEnd).Replace(renderResult.StepCode, m => { return(ExportUtil.TrimEmptyLines(ExportUtil.IndentListTemplate(BuildChoices(m.Groups[1].Value, data, choiceNode, npc), m.Groups[2].Value))); }); return(renderResult); }
/// <summary> /// Builds the list of choices /// </summary> /// <param name="choiceTemplate">Choice Template</param> /// <param name="data">Export dialog data</param> /// <param name="choiceNode">Choice node</param> /// <param name="npc">Npc to which the dialog belongs</param> /// <returns>Choices as string</returns> private string BuildChoices(string choiceTemplate, ExportDialogData data, TaleChoiceNode choiceNode, KortistoNpc npc) { if (choiceNode.Choices == null) { return(string.Empty); } string choicesResult = string.Empty; for (int curChoice = 0; curChoice < choiceNode.Choices.Count; ++curChoice) { choicesResult += BuildSingleChoice(choiceTemplate, data, choiceNode.Choices[curChoice], choiceNode, npc, curChoice); } return(choicesResult); }
/// <summary> /// Renders a dialog step /// </summary> /// <param name="data">Dialog Step Data</param> /// <param name="flexFieldObject">Flex field object to which the dialog belongs</param> /// <returns>Dialog Step Render Result</returns> public async Task <ExportDialogStepRenderResult> RenderDialogStep(ExportDialogData data, FlexFieldObject flexFieldObject) { KortistoNpc npc = flexFieldObject as KortistoNpc; if (npc == null) { return(null); } TaleChoiceNode choiceNode = data.Choice; if (choiceNode == null) { return(null); } ExportTemplate template = await _defaultTemplateProvider.GetDefaultTemplateByType(_project.Id, TemplateType.TaleChoice); if (!_renderers.ContainsKey(template.RenderingEngine)) { throw new KeyNotFoundException(string.Format("Unknown rendering engine {0} for ChoiceNode", template.RenderingEngine.ToString())); } string oldContext = _errorCollection.CurrentErrorContext; _errorCollection.CurrentErrorContext = _localizer["ErrorContextChoice"]; try { return(await _renderers[template.RenderingEngine].RenderDialogStep(template, data, npc, choiceNode)); } catch (Exception ex) { _errorCollection.AddException(ex); return(new ExportDialogStepRenderResult { StepCode = "<<ERROR_RENDERING_CHOICE>>" }); } finally { _errorCollection.CurrentErrorContext = oldContext; } }
/// <summary> /// Builds a parent text preview for the a dialog step /// </summary> /// <param name="child">Child node</param> /// <param name="parent">Parent</param> /// <param name="npc">Npc to which the dialog belongs</param> /// <param name="errorCollection">Error Collection</param> /// <returns>Parent text preview for the dialog step</returns> public Task <string> BuildParentTextPreview(ExportDialogData child, ExportDialogData parent, KortistoNpc npc, ExportPlaceholderErrorCollection errorCollection) { TaleChoiceNode choiceNode = parent.Choice; if (choiceNode == null) { return(Task.FromResult <string>(null)); } List <string> previewForChoices = new List <string>(); foreach (TaleChoice curChoice in choiceNode.Choices) { ExportDialogDataChild childObj = parent.Children.FirstOrDefault(c => c.NodeChildId == curChoice.Id); if (childObj == null || childObj.Child != child) { continue; } previewForChoices.Add(ExportUtil.BuildTextPreview(curChoice.Text)); } return(Task.FromResult(string.Join(", ", previewForChoices))); }
/// <summary> /// Renders a choice step /// </summary> /// <param name="template">Export template</param> /// <param name="data">Dialog Step Data</param> /// <param name="npc">Npc object to which the dialog belongs</param> /// <param name="choiceNode">Choice node to render</param> /// <returns>Dialog Step Render Result</returns> public async Task <ExportDialogStepRenderResult> RenderDialogStep(ExportTemplate template, ExportDialogData data, KortistoNpc npc, TaleChoiceNode choiceNode) { ExportDialogStepRenderResult renderResult = new ExportDialogStepRenderResult(); Template parsedTemplate = ScribanParsingUtil.ParseTemplate(template.Code, _errorCollection); if (parsedTemplate == null) { return(renderResult); } _languageKeyGenerator.SetErrorCollection(_errorCollection); ScribanChoice choiceData = new ScribanChoice(); SetRenderObjectBaseDataFromFlexFieldObject(choiceData, data, npc); choiceData.Choices = await BuildChoiceOptions(data, choiceNode, npc, choiceData); TemplateContext context = BuildTemplateContext(choiceData); renderResult.StepCode = await parsedTemplate.RenderAsync(context); return(renderResult); }
/// <summary> /// Builds the export choices /// </summary> /// <param name="data">Dialog Step Data</param> /// <param name="choiceNode">Choice node</param> /// <param name="npc">Npc object to which the dialog belongs</param> /// <param name="parentChoiceData">Choice data to which the choices belong</param> /// <returns>List of choice options</returns> private async Task <List <ScribanChoiceOption> > BuildChoiceOptions(ExportDialogData data, TaleChoiceNode choiceNode, KortistoNpc npc, ScribanChoice parentChoiceData) { if (choiceNode.Choices == null) { return(new List <ScribanChoiceOption>()); } List <ScribanChoiceOption> choiceOptions = new List <ScribanChoiceOption>(); foreach (TaleChoice curChoice in choiceNode.Choices) { ScribanChoiceOption curChoiceOption = await MapSingleChoice(data, curChoice, npc, parentChoiceData); choiceOptions.Add(curChoiceOption); } return(choiceOptions); }
/// <summary> /// Sorts the children by choices for better readability /// </summary> /// <param name="children">Children to sort</param> /// <param name="choice">Choice node</param> /// <returns>Sorted Children</returns> private List <ExportDialogDataChild> SortChildrenByChoices(List <ExportDialogDataChild> children, TaleChoiceNode choice) { List <ExportDialogDataChild> sortedChildren = new List <ExportDialogDataChild>(); foreach (TaleChoice curChoide in choice.Choices) { ExportDialogDataChild curChild = children.FirstOrDefault(c => c.NodeChildId == curChoide.Id); if (curChild != null) { sortedChildren.Add(curChild); children.Remove(curChild); } } sortedChildren.AddRange(children); return(sortedChildren); }
/// <summary> /// Builds a choice /// </summary> /// <param name="choiceTemplate">Choice Template</param> /// <param name="data">Export dialog data</param> /// <param name="choiceObj">Choice element</param> /// <param name="choiceNode">Choice node</param> /// <param name="npc">Npc to which the dialog belongs</param> /// <param name="choiceIndex">Index of the choice</param> /// <returns>Choices as string</returns> private string BuildSingleChoice(string choiceTemplate, ExportDialogData data, TaleChoice choiceObj, TaleChoiceNode choiceNode, KortistoNpc npc, int choiceIndex) { ExportDialogDataChild choice = data.Children.FirstOrDefault(c => c.NodeChildId == choiceObj.Id); string choiceContent = ReplaceBaseStepPlaceholders(choiceTemplate, data, choice != null ? choice.Child : null); choiceContent = ExportUtil.BuildPlaceholderRegex(Placeholder_Choice_Id).Replace(choiceContent, choiceObj.Id.ToString()); choiceContent = ExportUtil.BuildPlaceholderRegex(Placeholder_Choice_Index).Replace(choiceContent, choiceIndex.ToString()); choiceContent = ExportUtil.RenderPlaceholderIfTrue(choiceContent, Placeholder_HasConditionStart, Placeholder_HasConditionEnd, choiceObj.Condition != null && !string.IsNullOrEmpty(choiceObj.Condition.ConditionElements)); choiceContent = ExportUtil.BuildPlaceholderRegex(Placeholder_Condition).Replace(choiceContent, m => { return(BuildCondition(choiceObj.Condition, npc)); }); choiceContent = ExportUtil.RenderPlaceholderIfTrue(choiceContent, Placeholder_Choice_IsRepeatable_Start, Placeholder_Choice_IsRepeatable_End, choiceObj.IsRepeatable); choiceContent = ExportUtil.RenderPlaceholderIfTrue(choiceContent, Placeholder_Choice_IsNotRepeatable_Start, Placeholder_Choice_IsNotRepeatable_End, !choiceObj.IsRepeatable); choiceContent = ExportUtil.BuildPlaceholderRegex(Placeholder_Choice_Text).Replace(choiceContent, ExportUtil.EscapeCharacters(choiceObj.Text, _exportSettings.EscapeCharacter, _exportSettings.CharactersNeedingEscaping, _exportSettings.NewlineCharacter)); choiceContent = ExportUtil.BuildPlaceholderRegex(Placeholder_Choice_Text_Preview).Replace(choiceContent, ExportUtil.BuildTextPreview(choiceObj.Text)); choiceContent = ExportUtil.BuildPlaceholderRegex(Placeholder_Choice_Text_LangKey).Replace(choiceContent, m => { return(_languageKeyGenerator.GetDialogTextLineKey(npc.Id, npc.Name, "choice", choiceNode.Id + "_" + choiceObj.Id, choiceObj.Text).Result); }); return(choiceContent); }
/// <summary> /// Renders a choice step /// </summary> /// <param name="template">Export template</param> /// <param name="data">Dialog Step Data</param> /// <param name="npc">Npc object to which the dialog belongs</param> /// <param name="choiceNode">Choice node to render</param> /// <returns>Dialog Step Render Result</returns> public async Task <ExportDialogStepRenderResult> RenderDialogStep(ExportTemplate template, ExportDialogData data, KortistoNpc npc, TaleChoiceNode choiceNode) { ExportDialogStepRenderResult renderResult = new ExportDialogStepRenderResult(); renderResult.StepCode = ReplaceNodeId(template.Code, data); renderResult.StepCode = await ExportUtil.BuildRangePlaceholderRegex(Placeholder_ChoicesStart, Placeholder_ChoicesEnd).ReplaceAsync(renderResult.StepCode, async m => { string choices = await BuildChoices(m.Groups[1].Value, data, choiceNode, npc); return(ExportUtil.TrimEmptyLines(ExportUtil.IndentListTemplate(choices, m.Groups[2].Value))); }); return(renderResult); }