riversong code showcase
This commit is contained in:
66
Source/Riversong/Game/UI/Helpers/DraggableManipulator.cs
Normal file
66
Source/Riversong/Game/UI/Helpers/DraggableManipulator.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public class DraggableManipulator : PointerManipulator
|
||||
{
|
||||
private const string DraggedUssClassName = "dragged";
|
||||
|
||||
private Vector2 _startMousePosition;
|
||||
|
||||
private Vector2 _startElementPosition;
|
||||
|
||||
private VisualElement _draggedElement;
|
||||
|
||||
public DraggableManipulator(VisualElement draggedElement)
|
||||
{
|
||||
_draggedElement = draggedElement;
|
||||
}
|
||||
|
||||
protected override void RegisterCallbacksOnTarget()
|
||||
{
|
||||
target.RegisterCallback<PointerDownEvent>(OnPointerDown);
|
||||
target.RegisterCallback<PointerMoveEvent>(OnPointerMove);
|
||||
target.RegisterCallback<PointerUpEvent>(OnPointerUp);
|
||||
}
|
||||
|
||||
protected override void UnregisterCallbacksFromTarget()
|
||||
{
|
||||
target.UnregisterCallback<PointerDownEvent>(OnPointerDown);
|
||||
target.UnregisterCallback<PointerMoveEvent>(OnPointerMove);
|
||||
target.UnregisterCallback<PointerUpEvent>(OnPointerUp);
|
||||
}
|
||||
|
||||
private void OnPointerDown(PointerDownEvent evt)
|
||||
{
|
||||
if (evt.button != 0) return;
|
||||
|
||||
target.CapturePointer(evt.pointerId);
|
||||
evt.StopPropagation();
|
||||
|
||||
_startMousePosition = evt.position;
|
||||
_startElementPosition = new Vector2(_draggedElement.resolvedStyle.left, _draggedElement.resolvedStyle.top);
|
||||
|
||||
_draggedElement.AddToClassList(DraggedUssClassName);
|
||||
}
|
||||
|
||||
private void OnPointerMove(PointerMoveEvent evt)
|
||||
{
|
||||
if (!target.HasPointerCapture(evt.pointerId)) return;
|
||||
|
||||
var delta = (Vector2)evt.position - _startMousePosition;
|
||||
_draggedElement.style.left = _startElementPosition.x + delta.x;
|
||||
_draggedElement.style.top = _startElementPosition.y + delta.y;
|
||||
}
|
||||
|
||||
private void OnPointerUp(PointerUpEvent evt)
|
||||
{
|
||||
if (!target.HasPointerCapture(evt.pointerId)) return;
|
||||
|
||||
target.ReleasePointer(evt.pointerId);
|
||||
|
||||
_draggedElement.RemoveFromClassList(DraggedUssClassName);
|
||||
}
|
||||
}
|
||||
}
|
||||
66
Source/Riversong/Game/UI/Helpers/TextFormatHelper.cs
Normal file
66
Source/Riversong/Game/UI/Helpers/TextFormatHelper.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public class TextFormatHelper
|
||||
{
|
||||
public string Pluralize(int value, string singular, string plural)
|
||||
{
|
||||
return value == 1 ? singular : plural;
|
||||
}
|
||||
|
||||
public string FormatImportantText(string text)
|
||||
{
|
||||
return $"<b>{text}</b>";
|
||||
}
|
||||
|
||||
public void FormatImportantText(StringBuilder sb, string text)
|
||||
{
|
||||
sb.Append("<b>");
|
||||
sb.Append(text);
|
||||
sb.Append("</b>");
|
||||
}
|
||||
|
||||
public void FormatUnlockConditions(UnlockDefinition unlock, StringBuilder sb)
|
||||
{
|
||||
if (unlock.Type != UnlockType.UnlockBuilding || unlock.Conditions.Count <= 0) return;
|
||||
|
||||
sb.Append("To unlock the ");
|
||||
FormatImportantText(sb, unlock.Building.BuildingName);
|
||||
sb.AppendLine(", your village needs a little more:");
|
||||
|
||||
foreach (var condition in unlock.Conditions)
|
||||
{
|
||||
sb.AppendLine();
|
||||
FormatUnlockCondition(sb, condition);
|
||||
}
|
||||
}
|
||||
|
||||
private void FormatUnlockCondition(StringBuilder sb, UnlockCondition condition)
|
||||
{
|
||||
sb.Append("\u2022 ");
|
||||
|
||||
switch (condition.Type)
|
||||
{
|
||||
case UnlockConditionType.BuildingPlaced:
|
||||
sb.Append("Add a ");
|
||||
FormatImportantText(sb, condition.Building.BuildingName);
|
||||
sb.Append(" to your village");
|
||||
return;
|
||||
|
||||
case UnlockConditionType.HouseCountAtTierOrAbove:
|
||||
sb.Append("Grow ");
|
||||
sb.Append(condition.HouseCount);
|
||||
sb.Append(' ');
|
||||
sb.Append(Pluralize(condition.HouseCount, "house", "houses"));
|
||||
sb.Append(" to tier ");
|
||||
sb.Append(condition.MinHouseTierIndex + 1);
|
||||
return;
|
||||
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
40
Source/Riversong/Game/UI/Helpers/VisualElementExtensions.cs
Normal file
40
Source/Riversong/Game/UI/Helpers/VisualElementExtensions.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public static class VisualElementExtensions
|
||||
{
|
||||
public static VisualElement GetFirstAncestorOrSelf(this VisualElement element, Func<VisualElement, bool> predicate)
|
||||
{
|
||||
while (element != null)
|
||||
{
|
||||
if (predicate(element)) return element;
|
||||
element = element.parent;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void SetAbsoluteScreenPosition(this VisualElement element, Vector2 screenPoint)
|
||||
{
|
||||
element.style.position = Position.Absolute;
|
||||
element.style.left = screenPoint.x;
|
||||
element.style.top = Screen.height - screenPoint.y;
|
||||
}
|
||||
|
||||
public static void SetScale(this VisualElement element, float value)
|
||||
{
|
||||
element.style.scale = new StyleScale(new Scale(Vector2.one * value));
|
||||
}
|
||||
|
||||
public static void SetPickingModeRecursive(this VisualElement element, PickingMode pickingMode)
|
||||
{
|
||||
element.pickingMode = pickingMode;
|
||||
|
||||
var hierarchy = element.hierarchy;
|
||||
for (var i = 0; i < hierarchy.childCount; i++) hierarchy.ElementAt(i).SetPickingModeRecursive(pickingMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user