Files
chameleon/Assets/Scripts/Components/UI/UiPointerSurface.cs
T
adminandClaude Fable 5 6c03e5d3f6 Refactor Assets/Scripts: trim restating comments, minor structure cleanups
Sweep over 48 files (Editor/ excluded): remove comments that restate the
code, condense class summaries, fix three stale doc statements
(HsvColorPicker alpha bar, PaintHud layoutPrefab, MasterVolume
FallbackDefault). Inline two single-use wrappers (IsPointerOverUi,
BuildModes), drop an unused using, replace a magic literal with the
existing ParallaxBase constant, and convert ~10 UnityEngine.Object null
checks to the implicit-bool convention. No serialized fields, signatures,
RPC/NetworkVariable names, defaults, or #if blocks touched.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-08 08:29:18 +02:00

39 lines
1.5 KiB
C#

using System;
using UnityEngine;
using UnityEngine.EventSystems;
/// <summary>
/// Turns a pointer press/drag on a UI graphic into a normalized (x, y) position in [0,1] within its
/// RectTransform (origin bottom-left). Used by the colour picker's SV field and hue bar. The graphic
/// on the same object must be a raycast target.
/// </summary>
[RequireComponent(typeof(RectTransform))]
public class UiPointerSurface : MonoBehaviour, IPointerDownHandler, IDragHandler
{
/// <summary>Fired on press and while dragging with the normalized (x, y) in [0,1].</summary>
public event Action<Vector2> Picked;
private RectTransform _rect;
private void Awake() => _rect = (RectTransform)transform;
public void OnPointerDown(PointerEventData eventData) => Report(eventData);
public void OnDrag(PointerEventData eventData) => Report(eventData);
private void Report(PointerEventData eventData)
{
// pressEventCamera is null for a Screen Space - Overlay canvas, which is the
// correct camera argument there.
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(
_rect, eventData.position, eventData.pressEventCamera, out Vector2 local))
return;
Rect r = _rect.rect;
if (r.width <= 0f || r.height <= 0f) return;
float x = Mathf.Clamp01((local.x - r.xMin) / r.width);
float y = Mathf.Clamp01((local.y - r.yMin) / r.height);
Picked?.Invoke(new Vector2(x, y));
}
}