init
This commit is contained in:
@@ -0,0 +1,208 @@
|
||||
using GameFramework;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace GeometryTD.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)
|
||||
{
|
||||
foreach (var text in _confirmTexts)
|
||||
{
|
||||
text.text = confirmText;
|
||||
}
|
||||
}
|
||||
|
||||
private void RefreshCancelText(string cancelText)
|
||||
{
|
||||
foreach (var text in _cancelTexts)
|
||||
{
|
||||
text.text = cancelText;
|
||||
}
|
||||
}
|
||||
|
||||
private void RefreshOtherText(string otherText)
|
||||
{
|
||||
foreach (var text in _otherTexts)
|
||||
{
|
||||
text.text = otherText;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
using GeometryTD.Definition;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace GeometryTD.UI
|
||||
{
|
||||
public class IconArea : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Image _board;
|
||||
|
||||
[SerializeField] private Image _icon;
|
||||
|
||||
private IconAreaContext _context;
|
||||
|
||||
public virtual Sprite CurrentIconSprite => _icon != null ? _icon.sprite : null;
|
||||
|
||||
public virtual Color CurrentIconColor => _icon != null ? _icon.color : Color.white;
|
||||
|
||||
public virtual Vector2 CurrentIconSize => _icon != null ? _icon.rectTransform.rect.size : Vector2.zero;
|
||||
|
||||
public virtual Material CurrentIconMaterial => _icon != null ? _icon.material : null;
|
||||
|
||||
public virtual RectTransform IconRectTransform => _icon != null ? _icon.rectTransform : null;
|
||||
|
||||
public virtual void OnInit(IconAreaContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
Log.Error("Icon area need IconAreaContext.");
|
||||
return;
|
||||
}
|
||||
|
||||
_context = context;
|
||||
|
||||
switch (context.ComponentSlotType)
|
||||
{
|
||||
case TowerCompSlotType.Base:
|
||||
case TowerCompSlotType.Bearing:
|
||||
case TowerCompSlotType.Muzzle:
|
||||
GameEntry.SpriteCache.GetSprite(context.ComponentSlotType.ToString(), SetIcon);
|
||||
break;
|
||||
default:
|
||||
SetIcon(_context.Icon);
|
||||
break;
|
||||
}
|
||||
|
||||
SetRarity(_context.Rarity);
|
||||
SetIconColor(_context.Color);
|
||||
SetIconVisible(true);
|
||||
}
|
||||
|
||||
public virtual void SetIcon(Sprite sprite)
|
||||
{
|
||||
if (_icon != null)
|
||||
{
|
||||
_icon.sprite = sprite;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void SetRarity(RarityType rarity)
|
||||
{
|
||||
if (_board == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_board.color = rarity switch
|
||||
{
|
||||
RarityType.White => Color.white,
|
||||
RarityType.Green => Color.green,
|
||||
RarityType.Blue => Color.blue,
|
||||
RarityType.Purple => Color.magenta,
|
||||
RarityType.Red => Color.red,
|
||||
_ => Color.clear,
|
||||
};
|
||||
}
|
||||
|
||||
public virtual void SetIconColor(Color color)
|
||||
{
|
||||
if (_icon == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_icon.color = color;
|
||||
}
|
||||
|
||||
public virtual void SetIconVisible(bool visible)
|
||||
{
|
||||
if (_icon != null)
|
||||
{
|
||||
_icon.enabled = visible;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void OnReset()
|
||||
{
|
||||
SetIcon(null);
|
||||
SetRarity(RarityType.None);
|
||||
SetIconColor(Color.clear);
|
||||
SetIconVisible(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,392 @@
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.UI;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace GeometryTD.UI
|
||||
{
|
||||
public class ItemDescForm : UGuiForm
|
||||
{
|
||||
[SerializeField] private RectTransform _content;
|
||||
|
||||
[SerializeField] private TMP_Text _itemTitle;
|
||||
|
||||
[SerializeField] private TMP_Text _itemTypeText;
|
||||
|
||||
[SerializeField] private RectTransform _descScrollView;
|
||||
|
||||
[SerializeField] private TMP_Text _itemDescription;
|
||||
|
||||
[SerializeField] private TMP_Text _itemPrice;
|
||||
|
||||
[SerializeField] private Transform _tagAreaParent;
|
||||
|
||||
[SerializeField] private GameObject _tagItemPrefab;
|
||||
|
||||
[SerializeField] private bool _autoResizeHeight = true;
|
||||
|
||||
[SerializeField] private float _fixedTopHeight = 150f;
|
||||
|
||||
[SerializeField] private float _fixedBottomHeight = 150f;
|
||||
|
||||
[SerializeField] private float _fixedWidth = 300f;
|
||||
|
||||
[SerializeField] private float _maxDescriptionViewportHeight = 0f;
|
||||
|
||||
[SerializeField] private float _screenEdgePadding = 0f;
|
||||
|
||||
[SerializeField] private float _sideGap = 16f;
|
||||
|
||||
[SerializeField] private float _anchorItemWidth = 0f;
|
||||
|
||||
private readonly List<GameObject> _runtimeTagItems = new List<GameObject>();
|
||||
|
||||
private ItemDescFormContext _context;
|
||||
private Vector2 _targetPos;
|
||||
private bool _isOpened;
|
||||
|
||||
public void RefreshUI(ItemDescFormContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
Log.Warning("ItemDescForm context is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
_context = context;
|
||||
|
||||
_targetPos = ConvertScreenToAnchored(_context.ScreenPosition);
|
||||
|
||||
if (_content != null)
|
||||
{
|
||||
_content.anchoredPosition3D = _targetPos;
|
||||
}
|
||||
|
||||
if (_itemTitle != null) _itemTitle.text = _context.Title ?? string.Empty;
|
||||
if (_itemTypeText != null) _itemTypeText.text = _context.TypeText ?? string.Empty;
|
||||
if (_itemDescription != null) _itemDescription.text = _context.Description ?? string.Empty;
|
||||
if (_itemPrice != null) _itemPrice.text = $"Price: {_context.Price} Gold";
|
||||
|
||||
RefreshTags(_context.Tags);
|
||||
|
||||
ResizeToFitContent();
|
||||
ApplySideAnchoredTargetPos();
|
||||
}
|
||||
|
||||
protected override void OnOpen(object userData)
|
||||
{
|
||||
base.OnOpen(userData);
|
||||
_isOpened = true;
|
||||
|
||||
if (!(userData is ItemDescFormContext context))
|
||||
{
|
||||
Log.Error("ItemDescFormContext is invalid.");
|
||||
return;
|
||||
}
|
||||
|
||||
RefreshUI(context);
|
||||
}
|
||||
|
||||
protected override void OnClose(bool isShutdown, object userData)
|
||||
{
|
||||
_isOpened = false;
|
||||
ClearTags();
|
||||
_context = null;
|
||||
base.OnClose(isShutdown, userData);
|
||||
}
|
||||
|
||||
public void OnBlankAreaClick()
|
||||
{
|
||||
CloseSelf();
|
||||
}
|
||||
|
||||
public void OnBackgroundClick()
|
||||
{
|
||||
CloseSelf();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!_isOpened || _content == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!TryGetPointerDownScreenPosition(out Vector2 screenPoint))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (IsPointerInContent(screenPoint))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CloseSelf();
|
||||
}
|
||||
|
||||
private void ResizeToFitContent()
|
||||
{
|
||||
if (!_autoResizeHeight || _content == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_itemDescription == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_itemDescription.ForceMeshUpdate();
|
||||
float descriptionHeight = Mathf.Max(0f, _itemDescription.preferredHeight);
|
||||
SetRectHeight(_itemDescription.rectTransform, descriptionHeight);
|
||||
|
||||
RectTransform scrollContent = _itemDescription.rectTransform.parent as RectTransform;
|
||||
if (scrollContent != null)
|
||||
{
|
||||
SetRectHeight(scrollContent, descriptionHeight);
|
||||
}
|
||||
|
||||
float viewportHeight = descriptionHeight;
|
||||
if (_descScrollView != null)
|
||||
{
|
||||
viewportHeight = Mathf.Min(descriptionHeight, ResolveMaxDescriptionViewportHeight());
|
||||
SetRectHeight(_descScrollView, viewportHeight);
|
||||
|
||||
if (_descScrollView.TryGetComponent(out ScrollRect scrollRect))
|
||||
{
|
||||
scrollRect.verticalNormalizedPosition = 1f;
|
||||
}
|
||||
}
|
||||
|
||||
float targetHeight = _fixedTopHeight + viewportHeight + _fixedBottomHeight;
|
||||
|
||||
Vector2 size = new Vector2(_fixedWidth, targetHeight);
|
||||
_content.sizeDelta = size;
|
||||
}
|
||||
|
||||
private float ResolveMaxDescriptionViewportHeight()
|
||||
{
|
||||
float maxViewportHeight = _maxDescriptionViewportHeight;
|
||||
RectTransform parent = _content.parent as RectTransform;
|
||||
if (parent != null)
|
||||
{
|
||||
float availableHeight = parent.rect.height - _fixedTopHeight - _fixedBottomHeight
|
||||
- _screenEdgePadding * 2f;
|
||||
availableHeight = Mathf.Max(0f, availableHeight);
|
||||
maxViewportHeight = maxViewportHeight > 0f
|
||||
? Mathf.Min(maxViewportHeight, availableHeight)
|
||||
: availableHeight;
|
||||
}
|
||||
|
||||
return maxViewportHeight > 0f ? maxViewportHeight : float.MaxValue;
|
||||
}
|
||||
|
||||
private static void SetRectHeight(RectTransform rectTransform, float height)
|
||||
{
|
||||
if (rectTransform == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Vector2 size = rectTransform.sizeDelta;
|
||||
size.y = height;
|
||||
rectTransform.sizeDelta = size;
|
||||
}
|
||||
|
||||
private void ApplySideAnchoredTargetPos()
|
||||
{
|
||||
if (_content == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
RectTransform parent = _content.parent as RectTransform;
|
||||
if (parent == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Vector2 size = _content.sizeDelta;
|
||||
if (size.x <= 0f || size.y <= 0f)
|
||||
{
|
||||
size = _content.rect.size;
|
||||
}
|
||||
|
||||
Vector2 pivot = _content.pivot;
|
||||
float halfWidth = parent.rect.width * 0.5f;
|
||||
float halfHeight = parent.rect.height * 0.5f;
|
||||
|
||||
float minX = -halfWidth + size.x * pivot.x + _screenEdgePadding;
|
||||
float maxX = halfWidth - size.x * (1f - pivot.x) - _screenEdgePadding;
|
||||
float minY = -halfHeight + size.y * pivot.y + _screenEdgePadding;
|
||||
float maxY = halfHeight - size.y * (1f - pivot.y) - _screenEdgePadding;
|
||||
|
||||
float clampedY = minY <= maxY ? Mathf.Clamp(_targetPos.y, minY, maxY) : 0f;
|
||||
|
||||
float anchorHalfWidth = Mathf.Max(0f, _anchorItemWidth * 0.5f);
|
||||
float sideGap = Mathf.Max(0f, _sideGap);
|
||||
float horizontalOffset = anchorHalfWidth + size.x * 0.5f + sideGap;
|
||||
|
||||
float leftCandidateX = _targetPos.x - horizontalOffset;
|
||||
float rightCandidateX = _targetPos.x + horizontalOffset;
|
||||
|
||||
float finalX;
|
||||
if (IsInsideBounds(leftCandidateX, minX, maxX))
|
||||
{
|
||||
finalX = leftCandidateX;
|
||||
}
|
||||
else if (IsInsideBounds(rightCandidateX, minX, maxX))
|
||||
{
|
||||
finalX = rightCandidateX;
|
||||
}
|
||||
else
|
||||
{
|
||||
finalX = minX <= maxX ? Mathf.Clamp(leftCandidateX, minX, maxX) : 0f;
|
||||
}
|
||||
|
||||
_content.anchoredPosition = new Vector2(finalX, clampedY);
|
||||
}
|
||||
|
||||
private Vector2 ConvertScreenToAnchored(Vector2 screenPosition)
|
||||
{
|
||||
if (_content == null)
|
||||
{
|
||||
return screenPosition;
|
||||
}
|
||||
|
||||
RectTransform parent = _content.parent as RectTransform;
|
||||
if (parent == null)
|
||||
{
|
||||
return screenPosition;
|
||||
}
|
||||
|
||||
Canvas canvas = _content.GetComponentInParent<Canvas>();
|
||||
Canvas rootCanvas = canvas != null ? canvas.rootCanvas : null;
|
||||
Camera uiCamera = null;
|
||||
if (rootCanvas != null && rootCanvas.renderMode != RenderMode.ScreenSpaceOverlay)
|
||||
{
|
||||
uiCamera = rootCanvas.worldCamera != null ? rootCanvas.worldCamera : Camera.main;
|
||||
}
|
||||
|
||||
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(parent, screenPosition, uiCamera,
|
||||
out Vector2 localPoint))
|
||||
{
|
||||
return screenPosition;
|
||||
}
|
||||
|
||||
return localPoint;
|
||||
}
|
||||
|
||||
private static bool IsInsideBounds(float value, float min, float max)
|
||||
{
|
||||
return value >= min && value <= max;
|
||||
}
|
||||
|
||||
private void RefreshTags(TagItemContext[] tags)
|
||||
{
|
||||
ClearTags();
|
||||
|
||||
if (_tagAreaParent == null || _tagItemPrefab == null || tags == null || tags.Length <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_tagItemPrefab.scene.IsValid() && _tagItemPrefab.transform.IsChildOf(_tagAreaParent))
|
||||
{
|
||||
_tagItemPrefab.SetActive(false);
|
||||
}
|
||||
|
||||
for (int i = 0; i < tags.Length; i++)
|
||||
{
|
||||
TagItemContext tagContext = tags[i];
|
||||
if (tagContext == null || string.IsNullOrWhiteSpace(tagContext.TagName))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
GameObject tagGo = Instantiate(_tagItemPrefab, _tagAreaParent);
|
||||
tagGo.SetActive(true);
|
||||
|
||||
if (tagGo.TryGetComponent(out TagItem tagItem))
|
||||
{
|
||||
tagItem.OnInit(tagContext);
|
||||
}
|
||||
else
|
||||
{
|
||||
TMP_Text tagText = tagGo.GetComponentInChildren<TMP_Text>(true);
|
||||
if (tagText != null)
|
||||
{
|
||||
tagText.text = tagContext.TagName;
|
||||
}
|
||||
}
|
||||
|
||||
_runtimeTagItems.Add(tagGo);
|
||||
}
|
||||
}
|
||||
|
||||
private void ClearTags()
|
||||
{
|
||||
for (int i = _runtimeTagItems.Count - 1; i >= 0; i--)
|
||||
{
|
||||
GameObject tagGo = _runtimeTagItems[i];
|
||||
if (tagGo != null)
|
||||
{
|
||||
Destroy(tagGo);
|
||||
}
|
||||
}
|
||||
|
||||
_runtimeTagItems.Clear();
|
||||
}
|
||||
|
||||
private void CloseSelf()
|
||||
{
|
||||
GameEntry.UI.CloseUIForm(this);
|
||||
}
|
||||
|
||||
private bool IsPointerInContent(Vector2 screenPoint)
|
||||
{
|
||||
Canvas canvas = _content.GetComponentInParent<Canvas>();
|
||||
Canvas rootCanvas = canvas != null ? canvas.rootCanvas : null;
|
||||
Camera uiCamera = null;
|
||||
if (rootCanvas != null && rootCanvas.renderMode != RenderMode.ScreenSpaceOverlay)
|
||||
{
|
||||
uiCamera = rootCanvas.worldCamera != null ? rootCanvas.worldCamera : Camera.main;
|
||||
}
|
||||
|
||||
return RectTransformUtility.RectangleContainsScreenPoint(_content, screenPoint, uiCamera);
|
||||
}
|
||||
|
||||
private static bool TryGetPointerDownScreenPosition(out Vector2 screenPoint)
|
||||
{
|
||||
Touchscreen touchscreen = Touchscreen.current;
|
||||
if (touchscreen != null)
|
||||
{
|
||||
var touches = touchscreen.touches;
|
||||
for (int i = 0; i < touches.Count; i++)
|
||||
{
|
||||
var touch = touches[i];
|
||||
if (!touch.press.wasPressedThisFrame)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
screenPoint = touch.position.ReadValue();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Mouse mouse = Mouse.current;
|
||||
if (mouse != null && mouse.leftButton.wasPressedThisFrame)
|
||||
{
|
||||
screenPoint = mouse.position.ReadValue();
|
||||
return true;
|
||||
}
|
||||
|
||||
screenPoint = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
using System.Collections.Generic;
|
||||
using GeometryTD.CustomEvent;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace GeometryTD.UI
|
||||
{
|
||||
public class RewardItem : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private IconArea _iconArea;
|
||||
|
||||
[SerializeField] private TMP_Text _titleText;
|
||||
|
||||
[SerializeField] private TMP_Text _typeText;
|
||||
|
||||
[SerializeField] private TMP_Text _descriptionText;
|
||||
|
||||
[SerializeField] private RectTransform _descriptionScrollView;
|
||||
|
||||
[SerializeField] private RectTransform _descriptionContent;
|
||||
|
||||
[SerializeField] private float _maxDescriptionViewportHeight = 0f;
|
||||
|
||||
[SerializeField] private Transform _tagItemParent;
|
||||
|
||||
[SerializeField] private TagItem _tagItemTemplate;
|
||||
|
||||
private readonly List<TagItem> _runtimeTagItems = new List<TagItem>();
|
||||
|
||||
private RewardItemContext _context;
|
||||
|
||||
|
||||
public void OnInit(RewardItemContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
OnReset();
|
||||
gameObject.SetActive(false);
|
||||
return;
|
||||
}
|
||||
|
||||
_context = context;
|
||||
gameObject.SetActive(true);
|
||||
|
||||
if (_iconArea != null)
|
||||
{
|
||||
if (context.IconArea != null)
|
||||
{
|
||||
_iconArea.OnInit(context.IconArea);
|
||||
}
|
||||
else
|
||||
{
|
||||
_iconArea.OnReset();
|
||||
}
|
||||
}
|
||||
|
||||
if (_titleText != null)
|
||||
{
|
||||
_titleText.text = context.Title ?? string.Empty;
|
||||
}
|
||||
|
||||
if (_typeText != null)
|
||||
{
|
||||
_typeText.text = context.TypeText ?? string.Empty;
|
||||
}
|
||||
|
||||
if (_descriptionText != null)
|
||||
{
|
||||
_descriptionText.text = context.Description ?? string.Empty;
|
||||
}
|
||||
|
||||
ResizeDescriptionArea();
|
||||
|
||||
RefreshTags(context.Tags);
|
||||
}
|
||||
|
||||
public void OnReset()
|
||||
{
|
||||
_context = null;
|
||||
|
||||
if (_iconArea != null)
|
||||
{
|
||||
_iconArea.OnReset();
|
||||
}
|
||||
|
||||
if (_titleText != null)
|
||||
{
|
||||
_titleText.text = string.Empty;
|
||||
}
|
||||
|
||||
if (_typeText != null)
|
||||
{
|
||||
_typeText.text = string.Empty;
|
||||
}
|
||||
|
||||
if (_descriptionText != null)
|
||||
{
|
||||
_descriptionText.text = string.Empty;
|
||||
}
|
||||
|
||||
ResetDescriptionScroll();
|
||||
|
||||
ClearTags();
|
||||
}
|
||||
|
||||
public void OnClick()
|
||||
{
|
||||
if (_context == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
GameEntry.Event.Fire(this, RewardSelectItemSelectedEventArgs.Create(_context.Index));
|
||||
}
|
||||
|
||||
private void RefreshTags(TagItemContext[] tagContexts)
|
||||
{
|
||||
ClearTags();
|
||||
|
||||
if (_tagItemParent == null || tagContexts == null || tagContexts.Length <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
TagItem template = ResolveTagTemplate();
|
||||
if (template == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < tagContexts.Length; i++)
|
||||
{
|
||||
TagItemContext tagContext = tagContexts[i];
|
||||
if (tagContext == null || string.IsNullOrWhiteSpace(tagContext.TagName))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
TagItem tagItem = Instantiate(template, _tagItemParent);
|
||||
tagItem.gameObject.SetActive(true);
|
||||
tagItem.OnInit(tagContext);
|
||||
_runtimeTagItems.Add(tagItem);
|
||||
}
|
||||
}
|
||||
|
||||
private void ResizeDescriptionArea()
|
||||
{
|
||||
if (_descriptionText == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_descriptionText.ForceMeshUpdate();
|
||||
float descriptionHeight = Mathf.Max(0f, _descriptionText.preferredHeight);
|
||||
SetRectHeight(_descriptionText.rectTransform, descriptionHeight);
|
||||
|
||||
if (_descriptionContent != null)
|
||||
{
|
||||
SetRectHeight(_descriptionContent, descriptionHeight);
|
||||
}
|
||||
|
||||
if (_descriptionScrollView != null)
|
||||
{
|
||||
float viewportHeight = _maxDescriptionViewportHeight > 0f
|
||||
? Mathf.Min(descriptionHeight, _maxDescriptionViewportHeight)
|
||||
: descriptionHeight;
|
||||
SetRectHeight(_descriptionScrollView, viewportHeight);
|
||||
}
|
||||
|
||||
ResetDescriptionScroll();
|
||||
}
|
||||
|
||||
private void ResetDescriptionScroll()
|
||||
{
|
||||
if (_descriptionScrollView != null &&
|
||||
_descriptionScrollView.TryGetComponent(out ScrollRect scrollRect))
|
||||
{
|
||||
scrollRect.verticalNormalizedPosition = 1f;
|
||||
}
|
||||
}
|
||||
|
||||
private static void SetRectHeight(RectTransform rectTransform, float height)
|
||||
{
|
||||
if (rectTransform == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Vector2 size = rectTransform.sizeDelta;
|
||||
size.y = height;
|
||||
rectTransform.sizeDelta = size;
|
||||
}
|
||||
|
||||
private TagItem ResolveTagTemplate()
|
||||
{
|
||||
if (_tagItemTemplate != null)
|
||||
{
|
||||
return _tagItemTemplate;
|
||||
}
|
||||
|
||||
if (_tagItemParent == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
_tagItemTemplate = _tagItemParent.GetComponentInChildren<TagItem>(true);
|
||||
if (_tagItemTemplate != null)
|
||||
{
|
||||
_tagItemTemplate.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
return _tagItemTemplate;
|
||||
}
|
||||
|
||||
private void ClearTags()
|
||||
{
|
||||
for (int i = _runtimeTagItems.Count - 1; i >= 0; i--)
|
||||
{
|
||||
TagItem tagItem = _runtimeTagItems[i];
|
||||
if (tagItem != null)
|
||||
{
|
||||
Destroy(tagItem.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
_runtimeTagItems.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
using GeometryTD.CustomEvent;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityGameFramework.Runtime;
|
||||
|
||||
namespace GeometryTD.UI
|
||||
{
|
||||
public class RewardSelectForm : UGuiForm
|
||||
{
|
||||
[SerializeField] private Transform _selectItemParent;
|
||||
|
||||
[SerializeField] private RewardItem[] _rewardItems;
|
||||
|
||||
[SerializeField] private TMP_Text _tipText;
|
||||
|
||||
[SerializeField] private TMP_Text _refreshButtonText;
|
||||
|
||||
[SerializeField] private CommonButton _refreshButton;
|
||||
|
||||
[SerializeField] private CommonButton _giveUpButton;
|
||||
|
||||
private RewardSelectFormContext _context;
|
||||
|
||||
public void OnInit(RewardSelectFormContext context)
|
||||
{
|
||||
if (context == null)
|
||||
{
|
||||
Log.Error("RewardSelectForm need Context.");
|
||||
return;
|
||||
}
|
||||
|
||||
RefreshUI(context);
|
||||
}
|
||||
|
||||
public void RefreshUI(RewardSelectFormContext context)
|
||||
{
|
||||
_context = context;
|
||||
EnsureRewardItems();
|
||||
|
||||
if (_tipText != null)
|
||||
{
|
||||
_tipText.text = context?.TipText ?? string.Empty;
|
||||
}
|
||||
|
||||
if (_refreshButtonText != null)
|
||||
{
|
||||
_refreshButtonText.text = context?.RefreshButtonText ?? string.Empty;
|
||||
}
|
||||
|
||||
if (_refreshButton != null)
|
||||
{
|
||||
_refreshButton.Interactive = context?.CanRefresh ?? false;
|
||||
}
|
||||
|
||||
if (_giveUpButton != null)
|
||||
{
|
||||
_giveUpButton.Interactive = context?.CanGiveUp ?? false;
|
||||
}
|
||||
|
||||
if (_rewardItems == null || _rewardItems.Length <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < _rewardItems.Length; i++)
|
||||
{
|
||||
RewardItem rewardItem = _rewardItems[i];
|
||||
if (rewardItem == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
RewardItemContext itemContext = null;
|
||||
if (context?.RewardItems != null && i < context.RewardItems.Length)
|
||||
{
|
||||
itemContext = context.RewardItems[i];
|
||||
}
|
||||
|
||||
rewardItem.OnInit(itemContext);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnRefreshButtonClick()
|
||||
{
|
||||
if (_context != null && !_context.CanRefresh)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
GameEntry.Event.Fire(this, RewardSelectRefreshEventArgs.Create());
|
||||
}
|
||||
|
||||
public void OnGiveUpButtonClick()
|
||||
{
|
||||
if (_context != null && !_context.CanGiveUp)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
GameEntry.Event.Fire(this, RewardSelectGiveUpEventArgs.Create());
|
||||
}
|
||||
|
||||
protected override void OnOpen(object userData)
|
||||
{
|
||||
base.OnOpen(userData);
|
||||
|
||||
if (userData is RewardSelectFormContext context)
|
||||
{
|
||||
RefreshUI(context);
|
||||
return;
|
||||
}
|
||||
|
||||
Log.Warning("RewardSelectForm requires RewardSelectFormContext as userData.");
|
||||
}
|
||||
|
||||
protected override void OnClose(bool isShutdown, object userData)
|
||||
{
|
||||
_context = null;
|
||||
|
||||
if (_tipText != null)
|
||||
{
|
||||
_tipText.text = string.Empty;
|
||||
}
|
||||
|
||||
if (_refreshButtonText != null)
|
||||
{
|
||||
_refreshButtonText.text = string.Empty;
|
||||
}
|
||||
|
||||
if (_rewardItems != null)
|
||||
{
|
||||
for (int i = 0; i < _rewardItems.Length; i++)
|
||||
{
|
||||
RewardItem rewardItem = _rewardItems[i];
|
||||
if (rewardItem == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
rewardItem.OnReset();
|
||||
rewardItem.gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
base.OnClose(isShutdown, userData);
|
||||
}
|
||||
|
||||
private void EnsureRewardItems()
|
||||
{
|
||||
if (_rewardItems != null && _rewardItems.Length > 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_selectItemParent != null)
|
||||
{
|
||||
_rewardItems = _selectItemParent.GetComponentsInChildren<RewardItem>(true);
|
||||
}
|
||||
|
||||
if (_rewardItems == null || _rewardItems.Length <= 0)
|
||||
{
|
||||
_rewardItems = GetComponentsInChildren<RewardItem>(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
namespace GeometryTD.UI
|
||||
{
|
||||
public class TagItem : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private RectTransform _bgRect;
|
||||
|
||||
[SerializeField] private TMP_Text _tagName;
|
||||
|
||||
[SerializeField] private float _bgExtraWidth = 12f;
|
||||
|
||||
private TagItemContext _context;
|
||||
|
||||
public void OnInit(TagItemContext context)
|
||||
{
|
||||
_context = context;
|
||||
_tagName.text = _context?.TagName ?? string.Empty;
|
||||
|
||||
_tagName.ForceMeshUpdate();
|
||||
var targetWidth = _tagName.preferredWidth + _bgExtraWidth;
|
||||
_bgRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, targetWidth);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace GeometryTD.UI
|
||||
{
|
||||
public class TowerIconArea : IconArea
|
||||
{
|
||||
[SerializeField] private Image _baseIcon;
|
||||
|
||||
[SerializeField] private Image _bearingIcon;
|
||||
|
||||
[SerializeField] private Image _muzzleIcon;
|
||||
|
||||
public Sprite BaseIconSprite => _baseIcon != null ? _baseIcon.sprite : null;
|
||||
|
||||
public Sprite BearingIconSprite => _bearingIcon != null ? _bearingIcon.sprite : null;
|
||||
|
||||
public Sprite MuzzleIconSprite => _muzzleIcon != null ? _muzzleIcon.sprite : null;
|
||||
|
||||
public Color BaseIconColor => _baseIcon != null ? _baseIcon.color : Color.white;
|
||||
|
||||
public Color BearingIconColor => _bearingIcon != null ? _bearingIcon.color : Color.white;
|
||||
|
||||
public Color MuzzleIconColor => _muzzleIcon != null ? _muzzleIcon.color : Color.white;
|
||||
|
||||
public Material BaseIconMaterial => _baseIcon != null ? _baseIcon.material : null;
|
||||
|
||||
public Material BearingIconMaterial => _bearingIcon != null ? _bearingIcon.material : null;
|
||||
|
||||
public Material MuzzleIconMaterial => _muzzleIcon != null ? _muzzleIcon.material : null;
|
||||
|
||||
public override void OnInit(IconAreaContext context)
|
||||
{
|
||||
if (!(context is TowerIconAreaContext towerContext))
|
||||
{
|
||||
base.OnInit(context);
|
||||
return;
|
||||
}
|
||||
|
||||
SetRarity(towerContext.Rarity);
|
||||
SetIconVisible(false);
|
||||
|
||||
SetLayerColor(_baseIcon, towerContext.BaseColor);
|
||||
SetLayerColor(_bearingIcon, towerContext.BearingColor);
|
||||
SetLayerColor(_muzzleIcon, towerContext.MuzzleColor);
|
||||
}
|
||||
|
||||
public void OnInit(TowerIconAreaContext context)
|
||||
{
|
||||
OnInit((IconAreaContext)context);
|
||||
}
|
||||
|
||||
public override void OnReset()
|
||||
{
|
||||
base.OnReset();
|
||||
SetLayerColor(_baseIcon, Color.clear);
|
||||
SetLayerColor(_bearingIcon, Color.clear);
|
||||
SetLayerColor(_muzzleIcon, Color.clear);
|
||||
SetIconVisible(false);
|
||||
}
|
||||
|
||||
private static void SetLayerColor(Image icon, Color color)
|
||||
{
|
||||
if (icon == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
icon.color = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user