fix
- TowerSelectItem 显示问题 - CombatSelectForm 不同分辨率的适配
This commit is contained in:
@@ -20,7 +20,7 @@ namespace GeometryTD.Entity
|
||||
{
|
||||
userData = null;
|
||||
if (tilemap == null || !TryGetPointerWorldPosition(tilemap, mapTransform, out Vector3 worldPosition,
|
||||
out Vector2 contentPosition))
|
||||
out Vector2 screenPosition))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -28,25 +28,25 @@ namespace GeometryTD.Entity
|
||||
Vector3Int clickedCell = tilemap.WorldToCell(worldPosition);
|
||||
CombatSelectClickObjectType clickObjectType = CombatSelectClickObjectType.None;
|
||||
int towerEntityId = 0;
|
||||
Vector2 resolvedContentPosition = contentPosition;
|
||||
Vector2 resolvedScreenPosition = screenPosition;
|
||||
|
||||
if (towerEntityIdByFoundationCell != null &&
|
||||
towerEntityIdByFoundationCell.TryGetValue(clickedCell, out int occupiedTowerEntityId))
|
||||
{
|
||||
clickObjectType = CombatSelectClickObjectType.Tower;
|
||||
towerEntityId = occupiedTowerEntityId;
|
||||
resolvedContentPosition = BuildContentPositionFromCell(tilemap, clickedCell, contentPosition);
|
||||
resolvedScreenPosition = BuildScreenPositionFromCell(tilemap, clickedCell, screenPosition);
|
||||
}
|
||||
else if (isFoundationCell != null && isFoundationCell(clickedCell))
|
||||
{
|
||||
clickObjectType = CombatSelectClickObjectType.Foundation;
|
||||
resolvedContentPosition = BuildContentPositionFromCell(tilemap, clickedCell, contentPosition);
|
||||
resolvedScreenPosition = BuildScreenPositionFromCell(tilemap, clickedCell, screenPosition);
|
||||
}
|
||||
|
||||
userData = new CombatSelectFormUserData
|
||||
{
|
||||
ClickObjectType = clickObjectType,
|
||||
ContentPosition = resolvedContentPosition,
|
||||
ScreenPosition = resolvedScreenPosition,
|
||||
WorldPosition = worldPosition,
|
||||
CellPosition = clickedCell,
|
||||
TowerEntityId = towerEntityId,
|
||||
@@ -59,10 +59,10 @@ namespace GeometryTD.Entity
|
||||
|
||||
private static bool TryGetPointerWorldPosition(Tilemap tilemap, Transform mapTransform,
|
||||
out Vector3 worldPosition,
|
||||
out Vector2 contentPosition)
|
||||
out Vector2 screenPosition)
|
||||
{
|
||||
worldPosition = Vector3.zero;
|
||||
contentPosition = Vector2.zero;
|
||||
screenPosition = Vector2.zero;
|
||||
|
||||
Camera mainCamera = GameEntry.Scene != null ? GameEntry.Scene.MainCamera : Camera.main;
|
||||
if (mainCamera == null)
|
||||
@@ -84,38 +84,37 @@ namespace GeometryTD.Entity
|
||||
}
|
||||
|
||||
worldPosition = ray.GetPoint(enterDistance);
|
||||
contentPosition = BuildContentPosition(Input.mousePosition);
|
||||
contentPosition = new Vector2((int)contentPosition.x, (int)contentPosition.y);
|
||||
contentPosition = new Vector2(contentPosition.x + 0.5f, contentPosition.y + 0.5f);
|
||||
screenPosition = BuildScreenPosition(Input.mousePosition);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static Vector2 BuildContentPosition(Vector3 pointerScreenPosition)
|
||||
private static Vector2 BuildScreenPosition(Vector3 pointerScreenPosition)
|
||||
{
|
||||
return new Vector2(
|
||||
pointerScreenPosition.x - Screen.width * 0.5f,
|
||||
pointerScreenPosition.y - Screen.height * 0.5f);
|
||||
return new Vector2(pointerScreenPosition.x, pointerScreenPosition.y);
|
||||
}
|
||||
|
||||
private static Vector2 BuildContentPositionFromCell(Tilemap tilemap, Vector3Int cellPosition,
|
||||
Vector2 fallbackContentPosition)
|
||||
private static Vector2 BuildScreenPositionFromCell(Tilemap tilemap, Vector3Int cellPosition,
|
||||
Vector2 fallbackScreenPosition)
|
||||
{
|
||||
if (tilemap == null)
|
||||
{
|
||||
return fallbackContentPosition;
|
||||
return fallbackScreenPosition;
|
||||
}
|
||||
|
||||
Camera mainCamera = GameEntry.Scene != null ? GameEntry.Scene.MainCamera : Camera.main;
|
||||
if (mainCamera == null)
|
||||
{
|
||||
return fallbackContentPosition;
|
||||
return fallbackScreenPosition;
|
||||
}
|
||||
|
||||
Vector3 cellCenterWorld = tilemap.GetCellCenterWorld(cellPosition);
|
||||
Vector3 cellScreenPosition = mainCamera.WorldToScreenPoint(cellCenterWorld);
|
||||
Vector2 contentPosition = BuildContentPosition(cellScreenPosition);
|
||||
contentPosition = new Vector2((int)contentPosition.x, (int)contentPosition.y);
|
||||
return new Vector2(contentPosition.x + 0.5f, contentPosition.y + 0.5f);
|
||||
if (cellScreenPosition.z < 0f)
|
||||
{
|
||||
return fallbackScreenPosition;
|
||||
}
|
||||
|
||||
return BuildScreenPosition(cellScreenPosition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace GeometryTD.UI
|
||||
public class CombatSelectFormContext : UIContext
|
||||
{
|
||||
public bool IsVisible;
|
||||
public Vector2 ContentPosition;
|
||||
public Vector2 ScreenPosition;
|
||||
public bool ShowBuildArea;
|
||||
public bool ShowFuncArea;
|
||||
public TowerSelectItemContext[] BuildItems;
|
||||
|
||||
@@ -99,13 +99,13 @@ namespace GeometryTD.UI
|
||||
{
|
||||
case CombatSelectClickObjectType.Foundation:
|
||||
_useCase.SetUpgradeVisible(true);
|
||||
_useCase.ShowForFoundation(userData.ContentPosition);
|
||||
_useCase.ShowForFoundation(userData.ScreenPosition);
|
||||
break;
|
||||
case CombatSelectClickObjectType.Tower:
|
||||
_useCase.SetUpgradePrice(userData.UpgradeCost);
|
||||
_useCase.SetUpgradeVisible(!userData.IsTowerAtMaxLevel);
|
||||
_useCase.SetDestroyGain(userData.DestroyGain);
|
||||
_useCase.ShowForTower(userData.ContentPosition);
|
||||
_useCase.ShowForTower(userData.ScreenPosition);
|
||||
break;
|
||||
default:
|
||||
_useCase.Hide();
|
||||
@@ -127,7 +127,7 @@ namespace GeometryTD.UI
|
||||
_useCase = combatSelectFormUseCase;
|
||||
}
|
||||
|
||||
public int? ShowForFoundation(Vector2 contentPosition)
|
||||
public int? ShowForFoundation(Vector2 screenPosition)
|
||||
{
|
||||
if (_useCase == null)
|
||||
{
|
||||
@@ -136,11 +136,11 @@ namespace GeometryTD.UI
|
||||
}
|
||||
|
||||
_useCase.SetUpgradeVisible(true);
|
||||
_useCase.ShowForFoundation(contentPosition);
|
||||
_useCase.ShowForFoundation(screenPosition);
|
||||
return OpenUI(_useCase.TryRefresh());
|
||||
}
|
||||
|
||||
public int? ShowForTower(Vector2 contentPosition, int upgradeCost, int destroyGain)
|
||||
public int? ShowForTower(Vector2 screenPosition, int upgradeCost, int destroyGain)
|
||||
{
|
||||
if (_useCase == null)
|
||||
{
|
||||
@@ -151,7 +151,7 @@ namespace GeometryTD.UI
|
||||
_useCase.SetUpgradePrice(upgradeCost);
|
||||
_useCase.SetUpgradeVisible(true);
|
||||
_useCase.SetDestroyGain(destroyGain);
|
||||
_useCase.ShowForTower(contentPosition);
|
||||
_useCase.ShowForTower(screenPosition);
|
||||
return OpenUI(_useCase.TryRefresh());
|
||||
}
|
||||
|
||||
@@ -190,7 +190,7 @@ namespace GeometryTD.UI
|
||||
return new CombatSelectFormContext
|
||||
{
|
||||
IsVisible = rawData.IsVisible,
|
||||
ContentPosition = rawData.ContentPosition,
|
||||
ScreenPosition = rawData.ScreenPosition,
|
||||
ShowBuildArea = rawData.IsVisible && rawData.DisplayMode == CombatSelectDisplayMode.Build,
|
||||
ShowFuncArea = rawData.IsVisible && rawData.DisplayMode == CombatSelectDisplayMode.Func,
|
||||
BuildItems = BuildItemContexts(rawData.BuildItems),
|
||||
@@ -290,4 +290,4 @@ namespace GeometryTD.UI
|
||||
RefreshFromUseCase();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace GeometryTD.UI
|
||||
public class CombatSelectFormRawData
|
||||
{
|
||||
public bool IsVisible;
|
||||
public Vector2 ContentPosition;
|
||||
public Vector2 ScreenPosition;
|
||||
public CombatSelectDisplayMode DisplayMode;
|
||||
public TowerSelectItemRawData[] BuildItems;
|
||||
public TowerSelectItemRawData UpgradeItem;
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace GeometryTD.UI
|
||||
public class CombatSelectFormUserData
|
||||
{
|
||||
public CombatSelectClickObjectType ClickObjectType;
|
||||
public Vector2 ContentPosition;
|
||||
public Vector2 ScreenPosition;
|
||||
public Vector3 WorldPosition;
|
||||
public Vector3Int CellPosition;
|
||||
public int TowerEntityId;
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace GeometryTD.UI
|
||||
private Func<int> _coinProvider;
|
||||
private bool _isVisible;
|
||||
private CombatSelectDisplayMode _displayMode = CombatSelectDisplayMode.Hidden;
|
||||
private Vector2 _contentPosition;
|
||||
private Vector2 _screenPosition;
|
||||
|
||||
public CombatSelectFormUseCase()
|
||||
{
|
||||
@@ -174,16 +174,16 @@ namespace GeometryTD.UI
|
||||
_destroyOption.IsGain = true;
|
||||
}
|
||||
|
||||
public void ShowForFoundation(Vector2 contentPosition)
|
||||
public void ShowForFoundation(Vector2 screenPosition)
|
||||
{
|
||||
_contentPosition = contentPosition;
|
||||
_screenPosition = screenPosition;
|
||||
_displayMode = CombatSelectDisplayMode.Build;
|
||||
_isVisible = true;
|
||||
}
|
||||
|
||||
public void ShowForTower(Vector2 contentPosition)
|
||||
public void ShowForTower(Vector2 screenPosition)
|
||||
{
|
||||
_contentPosition = contentPosition;
|
||||
_screenPosition = screenPosition;
|
||||
_displayMode = CombatSelectDisplayMode.Func;
|
||||
_isVisible = true;
|
||||
}
|
||||
@@ -239,7 +239,7 @@ namespace GeometryTD.UI
|
||||
return new CombatSelectFormRawData
|
||||
{
|
||||
IsVisible = _isVisible,
|
||||
ContentPosition = _contentPosition,
|
||||
ScreenPosition = _screenPosition,
|
||||
DisplayMode = _displayMode,
|
||||
BuildItems = buildItems,
|
||||
UpgradeItem = BuildOptionRawData(_upgradeOption, currentCoin),
|
||||
|
||||
@@ -33,7 +33,7 @@ namespace GeometryTD.UI
|
||||
return;
|
||||
}
|
||||
|
||||
_content.anchoredPosition = context.ContentPosition;
|
||||
_content.anchoredPosition = ResolveContentAnchoredPosition(context.ScreenPosition);
|
||||
|
||||
if (_buildArea != null)
|
||||
{
|
||||
@@ -107,5 +107,35 @@ namespace GeometryTD.UI
|
||||
Transform contentTransform = transform.Find("Content");
|
||||
_content = contentTransform as RectTransform;
|
||||
}
|
||||
|
||||
private Vector2 ResolveContentAnchoredPosition(Vector2 screenPosition)
|
||||
{
|
||||
if (_content == null)
|
||||
{
|
||||
return Vector2.zero;
|
||||
}
|
||||
|
||||
RectTransform parentRect = _content.parent as RectTransform;
|
||||
if (parentRect == 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(parentRect, screenPosition, uiCamera,
|
||||
out Vector2 localPoint))
|
||||
{
|
||||
return Vector2.zero;
|
||||
}
|
||||
|
||||
return localPoint;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user