- 规范 UI 设计成为:
- UIForm 负责展示
- Controller 负责控制
- UseCase 负责业务逻辑
- RawData 负责提供原始数据
- Context 负责为 UIForm 提供数据
- 添加和补充一系列 UI 功能
This commit is contained in:
@@ -0,0 +1,232 @@
|
||||
//------------------------------------------------------------
|
||||
// Game Framework
|
||||
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
|
||||
// Homepage: https://gameframework.cn/
|
||||
// Feedback: mailto:ellan@gameframework.cn
|
||||
//------------------------------------------------------------
|
||||
|
||||
using GameFramework;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
using UnityEngine.UI;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class DialogForm : UGuiForm
|
||||
{
|
||||
[SerializeField] private TMP_Text _titleText = null;
|
||||
[SerializeField] private TMP_Text _messageText = null;
|
||||
[SerializeField] private GameObject[] _modeObjects = null;
|
||||
[SerializeField] private TMP_Text[] _confirmTexts = null;
|
||||
[SerializeField] private TMP_Text[] _cancelTexts = null;
|
||||
[SerializeField] private TMP_Text[] _otherTexts = null;
|
||||
|
||||
private int _dialogMode = 1;
|
||||
private bool _pauseGame = false;
|
||||
private object _userData = null;
|
||||
private GameFrameworkAction<object> _onClickConfirmGFAction = null;
|
||||
private GameFrameworkAction<object> _onClickCancelGFAction = null;
|
||||
private GameFrameworkAction<object> _onClickOtherGFAction = null;
|
||||
private DialogFormContext _context = null;
|
||||
|
||||
public int DialogMode => _dialogMode;
|
||||
|
||||
public bool PauseGame => _pauseGame;
|
||||
|
||||
public object UserData => _userData;
|
||||
|
||||
public void OnConfirmButtonClick()
|
||||
{
|
||||
Close();
|
||||
|
||||
if (_onClickConfirmGFAction != null)
|
||||
{
|
||||
_onClickConfirmGFAction(_userData);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnCancelButtonClick()
|
||||
{
|
||||
Close();
|
||||
|
||||
if (_onClickCancelGFAction != null)
|
||||
{
|
||||
_onClickCancelGFAction(_userData);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnOtherButtonClick()
|
||||
{
|
||||
Close();
|
||||
|
||||
if (_onClickOtherGFAction != null)
|
||||
{
|
||||
_onClickOtherGFAction(_userData);
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_2017_3_OR_NEWER
|
||||
protected override void OnOpen(object userData)
|
||||
#else
|
||||
protected internal override void OnOpen(object userData)
|
||||
#endif
|
||||
{
|
||||
base.OnOpen(userData);
|
||||
|
||||
if (userData is DialogFormContext context)
|
||||
{
|
||||
RefreshUI(context);
|
||||
return;
|
||||
}
|
||||
|
||||
if (userData is DialogFormRawData dialogParams)
|
||||
{
|
||||
RefreshUI(BuildContext(dialogParams));
|
||||
return;
|
||||
}
|
||||
|
||||
Log.Warning("DialogForm requires DialogFormContext or DialogParams as userData.");
|
||||
}
|
||||
|
||||
#if UNITY_2017_3_OR_NEWER
|
||||
protected override void OnClose(bool isShutdown, object userData)
|
||||
#else
|
||||
protected internal override void OnClose(bool isShutdown, object userData)
|
||||
#endif
|
||||
{
|
||||
if (_pauseGame)
|
||||
{
|
||||
GameEntry.Base.ResumeGame();
|
||||
}
|
||||
|
||||
_dialogMode = 1;
|
||||
_titleText.text = string.Empty;
|
||||
_messageText.text = string.Empty;
|
||||
_pauseGame = false;
|
||||
_userData = null;
|
||||
_context = null;
|
||||
|
||||
RefreshConfirmText(string.Empty);
|
||||
_onClickConfirmGFAction = null;
|
||||
|
||||
RefreshCancelText(string.Empty);
|
||||
_onClickCancelGFAction = null;
|
||||
|
||||
RefreshOtherText(string.Empty);
|
||||
_onClickOtherGFAction = null;
|
||||
|
||||
base.OnClose(isShutdown, userData);
|
||||
}
|
||||
|
||||
public void RefreshUI(DialogFormContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
Log.Warning("DialogForm context is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
_context = context;
|
||||
|
||||
_dialogMode = context.Mode;
|
||||
RefreshDialogMode();
|
||||
|
||||
_titleText.text = context.Title;
|
||||
_messageText.text = context.Message;
|
||||
|
||||
_pauseGame = context.PauseGame;
|
||||
RefreshPauseGame();
|
||||
|
||||
_userData = context.UserData;
|
||||
|
||||
RefreshConfirmText(context.ConfirmText);
|
||||
_onClickConfirmGFAction = context.OnClickConfirm;
|
||||
|
||||
RefreshCancelText(context.CancelText);
|
||||
_onClickCancelGFAction = context.OnClickCancel;
|
||||
|
||||
RefreshOtherText(context.OtherText);
|
||||
_onClickOtherGFAction = context.OnClickOther;
|
||||
}
|
||||
|
||||
private static DialogFormContext BuildContext(DialogFormRawData rawData)
|
||||
{
|
||||
if (rawData == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new DialogFormContext
|
||||
{
|
||||
Mode = rawData.Mode,
|
||||
Title = rawData.Title,
|
||||
Message = rawData.Message,
|
||||
PauseGame = rawData.PauseGame,
|
||||
ConfirmText = rawData.ConfirmText,
|
||||
OnClickConfirm = rawData.OnClickConfirm,
|
||||
CancelText = rawData.CancelText,
|
||||
OnClickCancel = rawData.OnClickCancel,
|
||||
OtherText = rawData.OtherText,
|
||||
OnClickOther = rawData.OnClickOther,
|
||||
UserData = rawData.UserData
|
||||
};
|
||||
}
|
||||
|
||||
private void RefreshDialogMode()
|
||||
{
|
||||
for (int i = 1; i <= _modeObjects.Length; i++)
|
||||
{
|
||||
_modeObjects[i - 1].SetActive(i == _dialogMode);
|
||||
}
|
||||
}
|
||||
|
||||
private void RefreshPauseGame()
|
||||
{
|
||||
if (_pauseGame)
|
||||
{
|
||||
GameEntry.Base.PauseGame();
|
||||
}
|
||||
}
|
||||
|
||||
private void RefreshConfirmText(string confirmText)
|
||||
{
|
||||
if (string.IsNullOrEmpty(confirmText))
|
||||
{
|
||||
confirmText = GameEntry.Localization.GetString("Dialog.ConfirmButton");
|
||||
}
|
||||
|
||||
for (int i = 0; i < _confirmTexts.Length; i++)
|
||||
{
|
||||
_confirmTexts[i].text = confirmText;
|
||||
}
|
||||
}
|
||||
|
||||
private void RefreshCancelText(string cancelText)
|
||||
{
|
||||
if (string.IsNullOrEmpty(cancelText))
|
||||
{
|
||||
cancelText = GameEntry.Localization.GetString("Dialog.CancelButton");
|
||||
}
|
||||
|
||||
for (int i = 0; i < _cancelTexts.Length; i++)
|
||||
{
|
||||
_cancelTexts[i].text = cancelText;
|
||||
}
|
||||
}
|
||||
|
||||
private void RefreshOtherText(string otherText)
|
||||
{
|
||||
if (string.IsNullOrEmpty(otherText))
|
||||
{
|
||||
otherText = GameEntry.Localization.GetString("Dialog.OtherButton");
|
||||
}
|
||||
|
||||
for (int i = 0; i < _otherTexts.Length; i++)
|
||||
{
|
||||
_otherTexts[i].text = otherText;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c04f3be6bab0c7748bc236f9023e47f9
|
||||
timeCreated: 1528026160
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,43 @@
|
||||
using Definition.Enum;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class IconArea : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Image _board;
|
||||
|
||||
[SerializeField] private Image _icon;
|
||||
|
||||
public void OnInit(Sprite sprite, ItemRarity rarity)
|
||||
{
|
||||
SetIcon(sprite);
|
||||
SetRarity(rarity);
|
||||
}
|
||||
|
||||
public void SetIcon(Sprite sprite)
|
||||
{
|
||||
_icon.sprite = sprite;
|
||||
}
|
||||
|
||||
public void SetRarity(ItemRarity rarity)
|
||||
{
|
||||
_board.color = rarity switch
|
||||
{
|
||||
ItemRarity.White => Color.white,
|
||||
ItemRarity.Green => Color.green,
|
||||
ItemRarity.Blue => Color.blue,
|
||||
ItemRarity.Purple => Color.magenta,
|
||||
ItemRarity.Red => Color.red,
|
||||
_ => Color.clear,
|
||||
};
|
||||
}
|
||||
|
||||
public void OnReset()
|
||||
{
|
||||
SetIcon(null);
|
||||
SetRarity(ItemRarity.None);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 578fa91d15f54e74a95ccce338d0a772
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user