82 lines
2.5 KiB
C#
82 lines
2.5 KiB
C#
using System;
|
|
using Cysharp.Threading.Tasks;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace DanieleMarotta.RiversongCodeShowcase
|
|
{
|
|
public class BuildingPlacementTooltipController : UIControllerSystem<BuildingPlacementTooltipUIView>, IUpdatable
|
|
{
|
|
[InjectService]
|
|
private IEditingService _editingService;
|
|
|
|
[InjectService]
|
|
private IPointerService _pointerService;
|
|
|
|
[InjectService]
|
|
private TextFormatHelper _textFormatHelper;
|
|
|
|
public BuildingPlacementTooltipController(IServiceLocator serviceLocator) : base(serviceLocator)
|
|
{
|
|
}
|
|
|
|
protected override BuildingPlacementTooltipUIView View => UIRoot.GetView<BuildingPlacementTooltipUIView>();
|
|
|
|
public override async UniTask InitializeAsync()
|
|
{
|
|
await base.InitializeAsync();
|
|
|
|
View.RootElement.SetPickingModeRecursive(PickingMode.Ignore);
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
if (_pointerService.IsPointerOverUI)
|
|
{
|
|
View.Clear();
|
|
return;
|
|
}
|
|
|
|
var editingState = _editingService.EditingState;
|
|
|
|
if (editingState.ActiveTool != editingState.BuildTool)
|
|
{
|
|
View.Clear();
|
|
return;
|
|
}
|
|
|
|
var validation = editingState.BuildTool.GetLastValidationResult();
|
|
if (validation == EditToolValidationResult.Success)
|
|
{
|
|
View.Clear();
|
|
return;
|
|
}
|
|
|
|
var buildingName = _textFormatHelper.FormatImportantText(editingState.BuildTool.Building.BuildingName);
|
|
var errorFormat = GetFailedValidationFormat(validation);
|
|
var text = string.Format(errorFormat, buildingName);
|
|
|
|
View.SetText(text);
|
|
View.RootElement.SetAbsoluteScreenPosition(Mouse.current.position.ReadValue());
|
|
}
|
|
|
|
private string GetFailedValidationFormat(EditToolValidationResult validation)
|
|
{
|
|
switch (validation)
|
|
{
|
|
case EditToolValidationResult.BlockedTile:
|
|
return "There is an obstacle here";
|
|
|
|
case EditToolValidationResult.CanOnlyBePlacedNearWater:
|
|
return "The {0} must be placed near water";
|
|
|
|
case EditToolValidationResult.CanOnlyBePlacedOnFertileGround:
|
|
return "The {0} must be placed on fertile ground";
|
|
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
}
|
|
}
|
|
}
|