// ***********************************************************************
// Assembly : Game
// Author : Kimch
// Created : 2021-09-17
// Description :
// Last Modified By :
// Last Modified On :
// ***********************************************************************
//
//
// ***********************************************************************
namespace G.UI
{
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
///
///
///
partial class BookDetailWindow
{
class LineWidget : KUIWidget
{
[KUIFlag]
TextMeshProUGUI _tmpContent;
[KUIFlag]
Image _imgCorrect;
private string _content;
private int _lineIndex;
private Tween _tween;
public override void Refresh()
{
if (this.data is LineInfo lineInfo)
{
_content = lineInfo.content;
_lineIndex = lineInfo.index;
_tmpContent.text = _content;
SetColor();
}
}
public void ShowContentWithAnim(LineInfo lineInfo)
{
_content = lineInfo.content;
_lineIndex = lineInfo.index;
_imgCorrect.color = Color.clear;
//_tmpContent.text = _content;
//_tmpContent.alpha = 1f;
//_tmpContent.CrossFadeAlpha(1f, 1f, true);
_tmpContent.text = "";
_tween = _tmpContent.DOText(_content, 2f);
}
public bool SetColor()
{
if (this.transform.GetSiblingIndex() == _lineIndex)
{
_imgCorrect.color = Color.clear;
_tmpContent.color = Color.black;
return true;
}
else
{
_imgCorrect.color = Color.white;
_tmpContent.color=(new Color32(211, 57, 57, 255));
return false;
}
}
public void OnDropEndMy()
{
SetColor();
PostNotification(GlobalDefine.EVENT_BOOK_ARRANGE);
}
private void Awake()
{
SetViewData();
GetComponent().onDragEnd.AddListener(OnDropEndMy);
}
private void OnDisable()
{
if (_tween != null)
_tween.Kill(false);
}
}
class LineInfo
{
public string content;
public int index;
}
#region Field
#pragma warning disable CS0649 // 从未对字段赋值,字段将一直保持其默认值 null
[KUIFlag]
Button _btnBack;
[KUIFlag]
KUIList __listPoem;
[KUIFlag]
TextMeshProUGUI _tmpDescription;
[KUIFlag]
TextMeshProUGUI _tmpPointer;
[KUIFlag]
TextMeshProUGUI _tmpName;
[KUIFlag]
TextMeshProUGUI _tmpAuthor;
[KUIFlag]
GameObject _goActived;
#pragma warning restore CS0649 // 从未对字段赋值,字段将一直保持其默认值 null
private List _lineInfos = new List(8);
private List _lineWidgets = new List();
#endregion
#region Method
///
///
///
public void InitView()
{
SetViewData();
_btnBack.onClick.AddListener(this.OnBackBtnClick);
__listPoem.AddTemplate(true);
//__listPoem.GetComponent().onDrop.AddListener(OnLineDrop);
}
///
///
///
public void RefreshView()
{
StopAllCoroutine();
StartCoroutine(RefreshViewCO());
//if (this.data is ItemBook book)
//{
// _lineInfos.Clear();
// for (int i = 0, c = book.contents.Length; i < c; i++)
// {
// _lineInfos.Add(new LineInfo
// {
// content = book.contents[i],
// index = c - i - 1,
// });
// }
// _tmpName.text = book.name;
// _tmpAuthor.text = book.author;
// _tmpDescription.text = book.description;
// _tmpDescription.enableVertexGradient = book.isActive;
// _goActived.SetActive(book.isActive);
// if (book.isActive)
// {
// __listPoem.GetComponent().blocksRaycasts = false;
// _tmpPointer.alpha = 0f;
// StartCoroutine(ShowPoemWithAnim(_lineInfos));
// //_lineWidgets.Clear();
// //__listPoem.Clear();
// //for (int i = _lineInfos.Count - 1; i >= 0; i--)
// //{
// // var lineWidget = __listPoem.GetItem();
// // lineWidget.SetData(_lineInfos[i]);
// // //lineWidget.transform.SetAsLastSibling();
// //}
// }
// else
// {
// __listPoem.GetComponent().blocksRaycasts = true;
// _tmpPointer.alpha = 1f;
// ListRandom(_lineInfos);
// __listPoem.Clear();
// _lineWidgets.Clear();
// __listPoem.transform.
// for (int i = 0; i < _lineInfos.Count; i++)
// {
// var lineWidget = __listPoem.GetItem();
// lineWidget.SetData(_lineInfos[i]);
// lineWidget.transform.SetAsFirstSibling();
// _lineWidgets.Add(lineWidget);
// }
// }
//}
}
IEnumerator RefreshViewCO()
{
if (this.data is ItemBook book)
{
_lineInfos.Clear();
for (int i = 0, c = book.contents.Length; i < c; i++)
{
_lineInfos.Add(new LineInfo
{
content = book.contents[i],
index = c - i - 1,
});
}
_tmpName.text = book.name;
_tmpAuthor.text = book.author;
_tmpDescription.text = book.description;
_tmpDescription.enableVertexGradient = book.isActive;
_goActived.SetActive(book.isActive);
if (book.isActive)
{
__listPoem.GetComponent().blocksRaycasts = false;
_tmpPointer.alpha = 0f;
__listPoem.Clear();
yield return null;
for (int i = 0; i < _lineInfos.Count; i++)
{
var lineWidget = __listPoem.GetItem();
lineWidget.transform.SetAsFirstSibling();
lineWidget.ShowContentWithAnim(_lineInfos[i]);
yield return new WaitForSeconds(1f);
}
}
else
{
__listPoem.GetComponent().blocksRaycasts = true;
_tmpPointer.alpha = 1f;
ListRandom(_lineInfos);
_lineWidgets.Clear();
__listPoem.Clear();
var lineParent = __listPoem.transform;
lineParent.DetachChildren();
yield return null;
for (int i = 0; i < _lineInfos.Count; i++)
{
var lineWidget = __listPoem.GetItem();
lineWidget.SetData(_lineInfos[i]);
lineWidget.transform.SetParent(lineParent);
lineWidget.transform.SetAsFirstSibling();
_lineWidgets.Add(lineWidget);
}
foreach (var lw in _lineWidgets)
{
lw.SetColor();
}
}
}
}
static void ListRandom(List sources)
{
LineInfo temp = sources[0];
int index = Random.Range(1, sources.Count - 1);
sources[0] = sources[index];
sources[index] = temp;
for (int i = 1; i < sources.Count; i++)
{
index = Random.Range(1, sources.Count - 1);
if (index != i)
{
temp = sources[i];
sources[i] = sources[index];
sources[index] = temp;
}
}
}
public void ClearView()
{
_lineWidgets.Clear();
__listPoem.Clear();
}
IEnumerator ShowPoemWithAnim(List lines)
{
__listPoem.Clear();
yield return null;
for (int i = 0; i < _lineInfos.Count; i++)
{
var lineWidget = __listPoem.GetItem();
lineWidget.transform.SetAsFirstSibling();
lineWidget.ShowContentWithAnim(_lineInfos[i]);
yield return new WaitForSeconds(1f);
}
}
void OnLineDrop()
{
bool arrange = true;
foreach (var lw in _lineWidgets)
{
if (!lw.SetColor())
arrange = false;
}
if (arrange)
{
if (BookProxy.Instance.ActiveBook(this.data as ItemBook))
{
ToastBox.ShowEffect("UI_fx_Arrangement01");
RefreshView();
}
}
}
void OnBackBtnClick()
{
CloseWindow(this);
SoundProxy.PlayFxAsync(GlobalDefine.BUTTON_CLICK_SOUND);
}
#endregion
}
}