Skip to content

Commit

Permalink
Added dialog history
Browse files Browse the repository at this point in the history
  • Loading branch information
ShutovKS committed Sep 11, 2023
1 parent c4892d7 commit 1759108
Show file tree
Hide file tree
Showing 9 changed files with 1,318 additions and 75 deletions.
1,255 changes: 1,187 additions & 68 deletions src/Visual novel/Assets/Data/Addressables/Screens/DialogueScreen.prefab

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#region
#region

using System;
using Data.Dynamic;
using Infrastructure.Services;
using Infrastructure.Services.CoroutineRunner;
using Infrastructure.Services.LocalisationDataLoad;
using Infrastructure.Services.SaveLoadData;
using Infrastructure.Services.UIFactory;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;

#endregion
Expand Down Expand Up @@ -37,11 +39,15 @@ private void Awake()

private void InitializedManagers()
{
_historyManager = new HistoryManager(_uiFactoryInfo.DialogueUI.History);
var onNewDialog = new UnityAction<string, string, string>(_historyManager.AddedDialogInHistory);

_dialogueManager = new DialogueManager(
_localisationDataLoad.GetPhraseId,
_uiFactoryInfo.DialogueUI,
_uiFactoryInfo.BackgroundUI,
_coroutineRunner);
_coroutineRunner,
onNewDialog);

_saveLoadManager = new SaveLoadManager(
_saveLoadData,
Expand All @@ -52,8 +58,6 @@ private void InitializedManagers()
_uiFactoryInfo.DialogueUI,
_dialogueManager.SetDialog);

_historyManager = new HistoryManager();

_settingsManager = new SettingsManager();

_buttonManager = new ButtonManager(_uiFactoryInfo.DialogueUI.Buttons);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ namespace Infrastructure.ScenesManagers.Core
public class DialogueManager
{
public DialogueManager(Func<string, IPhrase> onGetPart, DialogueUI dialogueUI,
BackgroundUI backgroundUI, ICoroutineRunner coroutineRunner)
BackgroundUI backgroundUI, ICoroutineRunner coroutineRunner,
UnityAction<string, string, string> onNewDialog)
{
_onGetPart = onGetPart;
_dialogueUI = dialogueUI;
_backgroundUI = backgroundUI;
_coroutineRunner = coroutineRunner;
_onNewDialog = onNewDialog;
}

private const float SECONDS_DELAY_DEFAULT = 0.05f;
Expand All @@ -31,6 +33,7 @@ public DialogueManager(Func<string, IPhrase> onGetPart, DialogueUI dialogueUI,
public IPhrase CurrentDialogue { get; private set; }
private readonly BackgroundUI _backgroundUI;
private readonly ICoroutineRunner _coroutineRunner;
private readonly UnityAction<string, string, string> _onNewDialog;
private readonly DialogueUI _dialogueUI;

private readonly Func<string, IPhrase> _onGetPart;
Expand Down Expand Up @@ -126,6 +129,7 @@ private void AutoDialogSwitchIfComplete()

private void SetPhraseTyping(Phrase phrase)
{
_onNewDialog.Invoke(phrase.ID, phrase.Name, phrase.Text);
_dialogueUI.Answers.SetActiveAnswerOptions(false);
_backgroundUI.SetBackgroundImage(GetTexture2D("Backgrounds/" + phrase.BackgroundPath));
_dialogueUI.DialogueText.SetAuthorName(phrase.Name);
Expand Down Expand Up @@ -170,7 +174,12 @@ private void SetResponseChoice(Responses response)
{
var index = i;
tuples[i] = (response.ResponseList[i].AnswerText,
() => SetDialog(response.ResponseList[index].IDNextDialog));
() =>
{
//TODO: add name to response
_onNewDialog.Invoke(response.ID, null, response.ResponseList[index].AnswerText);
SetDialog(response.ResponseList[index].IDNextDialog);
});
}

_dialogueUI.Answers.SetAnswerOptions(tuples);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
namespace Infrastructure.ScenesManagers.Core
using UI.Dialogue;

namespace Infrastructure.ScenesManagers.Core
{
public class HistoryManager
{
public HistoryManager(HistoryUI historyUI)
{
_historyUI = historyUI;
}

private readonly HistoryUI _historyUI;

public void OpenDialogHistory()
{
_historyUI.SetActivePanel(true);
_historyUI.RegisterBackButtonCallback(() => _historyUI.SetActivePanel(false));
}

public void AddedDialogInHistory(string id,string name, string text)
{
_historyUI.CreateHistoryPhrase(id, name, text);
}
}
}
1 change: 1 addition & 0 deletions src/Visual novel/Assets/Scripts/UI/Dialogue/DialogueUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class DialogueUI : MonoBehaviour, ILocalizableUI
[field: SerializeField] public PersonAvatarUI Person { get; private set; }
[field: SerializeField] public DialogueTextUI DialogueText { get; private set; }
[field: SerializeField] public ButtonsUI Buttons { get; private set; }
[field: SerializeField] public HistoryUI History { get; private set; }

public void Localize(UILocalisation localisation)
{
Expand Down
11 changes: 11 additions & 0 deletions src/Visual novel/Assets/Scripts/UI/Dialogue/HistoryPhraseUI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using TMPro;
using UnityEngine;

namespace UI.Dialogue
{
public class HistoryPhraseUI : MonoBehaviour
{
[field: SerializeField] public TextMeshProUGUI NameText;
[field: SerializeField] public TextMeshProUGUI TextText;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions src/Visual novel/Assets/Scripts/UI/Dialogue/HistoryUI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using Units.Tools;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace UI.Dialogue
{
public class HistoryUI : MonoBehaviour
{
[SerializeField] private GameObject _historyPhrasePrefab;
[SerializeField] private Transform _contentTransform;
[SerializeField] private GameObject _historyGameObject;
[SerializeField] private Button _backButton;

private readonly Dictionary<string, GameObject> _historyPhrases = new();

public void SetActivePanel(bool value) => _historyGameObject.SetActive(value);

public void RegisterBackButtonCallback(UnityAction action) => _backButton.RegisterNewCallback(action);

public void CreateHistoryPhrase(string id, string name, string text)
{
var historyPhraseInstantiate = Instantiate(_historyPhrasePrefab, _contentTransform);
historyPhraseInstantiate.SetActive(true);
_historyPhrases.Add(id, historyPhraseInstantiate);

if (historyPhraseInstantiate.TryGetComponent(out HistoryPhraseUI historyPhraseUI))
{
historyPhraseUI.NameText.text = name;
historyPhraseUI.TextText.text = text;
}
else throw new Exception("No HistoryPhraseUI in instance historyPhrasePrefab");

var contentPanelRT = _contentTransform.GetComponent<RectTransform>();
var panel = historyPhraseInstantiate.GetComponent<RectTransform>();

var scrollSizeDelta = contentPanelRT.sizeDelta;
scrollSizeDelta.y += contentPanelRT.childCount == 1
? panel.sizeDelta.y
: panel.sizeDelta.y * 1.5f;

contentPanelRT.sizeDelta = scrollSizeDelta;

var panelAnchoredPosition = panel.anchoredPosition;
panelAnchoredPosition.y = - scrollSizeDelta.y + panel.sizeDelta.y * 0.5f;
panel.anchoredPosition = panelAnchoredPosition;
}

public void DestroyHistoryPhrase(string id)
{
if (_historyPhrases.TryGetValue(id, out var go))
{
Destroy(go);
_historyPhrases.Remove(id);
}
else throw new Exception($"No id {id} in dictionary history phrases");
}
}
}
11 changes: 11 additions & 0 deletions src/Visual novel/Assets/Scripts/UI/Dialogue/HistoryUI.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1759108

Please sign in to comment.