Finalized work

This commit is contained in:
Michal Pikulski
2025-11-27 11:27:36 +01:00
parent 8bc4a88958
commit d083c2873e
6 changed files with 305 additions and 22 deletions

View File

@@ -12,8 +12,9 @@ namespace Minigames.StatueDressup.DragDrop
/// Draggable instance of a decoration that can be placed on the statue.
/// Created dynamically when dragging from menu or picking up from statue.
/// Destroyed if dropped outside statue area.
/// Supports tapping and dragging when placed on statue.
/// </summary>
public class DecorationDraggableInstance : MonoBehaviour
public class DecorationDraggableInstance : MonoBehaviour, IPointerClickHandler, IBeginDragHandler, IDragHandler, IEndDragHandler
{
[Header("References")]
[SerializeField] private Image decorationImage;
@@ -22,15 +23,19 @@ namespace Minigames.StatueDressup.DragDrop
private DecorationData _decorationData;
private RectTransform _rectTransform;
private Canvas _canvas;
private Transform _canvasParent; // Parent transform for dragging (usually canvas or draggable container)
private RectTransform _statueOutline;
private Transform _statueParent;
private StatueDecorationController _controller;
private AppleHills.Core.Settings.IStatueDressupSettings _settings;
private System.Action _onFinishedCallback;
private System.Action _onShowOutlineCallback;
private System.Action _onHideOutlineCallback;
private bool _isDragging;
private bool _isPlacedOnStatue;
private Vector3 _dragOffset;
private bool _dragStarted; // Track if drag actually started (vs just a click)
// Properties
public DecorationData Data => _decorationData;
@@ -41,10 +46,22 @@ namespace Minigames.StatueDressup.DragDrop
_rectTransform = GetComponent<RectTransform>();
_canvas = GetComponentInParent<Canvas>();
// Store initial parent for dragging context
if (transform.parent != null)
{
_canvasParent = transform.parent;
}
if (canvasGroup == null)
{
canvasGroup = gameObject.AddComponent<CanvasGroup>();
}
// Ensure the decoration image can receive raycasts
if (decorationImage != null)
{
decorationImage.raycastTarget = true;
}
}
/// <summary>
@@ -52,7 +69,7 @@ namespace Minigames.StatueDressup.DragDrop
/// </summary>
public void Initialize(DecorationData data, RectTransform statueOutline, Transform statueParent,
StatueDecorationController controller, AppleHills.Core.Settings.IStatueDressupSettings settings,
System.Action onFinishedCallback)
System.Action onFinishedCallback, System.Action onShowOutline = null, System.Action onHideOutline = null)
{
_decorationData = data;
_statueOutline = statueOutline;
@@ -60,6 +77,8 @@ namespace Minigames.StatueDressup.DragDrop
_controller = controller;
_settings = settings;
_onFinishedCallback = onFinishedCallback;
_onShowOutlineCallback = onShowOutline;
_onHideOutlineCallback = onHideOutline;
// Set sprite
if (decorationImage != null && data != null && data.DecorationSprite != null)
@@ -81,7 +100,9 @@ namespace Minigames.StatueDressup.DragDrop
/// Skips drag logic and sets up as if already placed on statue
/// </summary>
public void InitializeAsPlaced(DecorationData data, StatueDecorationController controller,
AppleHills.Core.Settings.IStatueDressupSettings settings)
AppleHills.Core.Settings.IStatueDressupSettings settings, RectTransform statueOutline = null,
Transform canvasParent = null, System.Action onShowOutline = null, System.Action onHideOutline = null,
System.Action onFinished = null)
{
_decorationData = data;
_controller = controller;
@@ -89,6 +110,23 @@ namespace Minigames.StatueDressup.DragDrop
_isPlacedOnStatue = true;
_isDragging = false;
// Store references needed for pickup
_statueOutline = statueOutline;
_statueParent = transform.parent; // Already parented to statue when this is called
if (canvasParent != null)
{
_canvasParent = canvasParent;
}
// Store outline callbacks for when picking up from statue
_onShowOutlineCallback = onShowOutline;
_onHideOutlineCallback = onHideOutline;
_onFinishedCallback = onFinished;
Logging.Debug($"[DecorationDraggableInstance] InitializeAsPlaced - Show outline callback is null: {_onShowOutlineCallback == null}");
Logging.Debug($"[DecorationDraggableInstance] InitializeAsPlaced - Hide outline callback is null: {_onHideOutlineCallback == null}");
Logging.Debug($"[DecorationDraggableInstance] InitializeAsPlaced - Finished callback is null: {_onFinishedCallback == null}");
// Set sprite
if (decorationImage != null && data != null && data.DecorationSprite != null)
{
@@ -101,7 +139,7 @@ namespace Minigames.StatueDressup.DragDrop
_rectTransform.sizeDelta = data.AuthoredSize;
}
// Make non-interactive for placed state (can be made interactive later if needed)
// Make interactive so it can be picked up
if (canvasGroup != null)
{
canvasGroup.blocksRaycasts = true;
@@ -250,8 +288,11 @@ namespace Minigames.StatueDressup.DragDrop
/// <summary>
/// Allow picking up from statue for repositioning
/// </summary>
public void StartDragFromStatue(Vector3 pointerPosition)
public void StartDragFromStatue(PointerEventData eventData)
{
Logging.Debug($"[DecorationDraggableInstance] StartDragFromStatue called for: {_decorationData?.DecorationName}");
Logging.Debug($"[DecorationDraggableInstance] Show outline callback is null: {_onShowOutlineCallback == null}");
if (_controller != null)
{
_controller.UnregisterDecoration(this);
@@ -260,17 +301,87 @@ namespace Minigames.StatueDressup.DragDrop
_isPlacedOnStatue = false;
_isDragging = true;
// Calculate offset
// Show statue outline when picking up from statue
if (_onShowOutlineCallback != null)
{
Logging.Debug("[DecorationDraggableInstance] Invoking show outline callback");
_onShowOutlineCallback.Invoke();
}
else
{
Logging.Warning("[DecorationDraggableInstance] Show outline callback is null - cannot show outline!");
}
// Reparent to canvas for dragging (so coordinates work correctly)
if (_canvasParent != null && transform.parent != _canvasParent)
{
// Store world position before reparenting
Vector3 worldPos = transform.position;
transform.SetParent(_canvasParent, false);
transform.position = worldPos; // Restore world position
}
// Calculate offset using proper camera
RectTransformUtility.ScreenPointToLocalPointInRectangle(
_canvas.transform as RectTransform,
pointerPosition,
null,
eventData.position,
eventData.pressEventCamera,
out Vector2 localPoint);
_dragOffset = _rectTransform.localPosition - (Vector3)localPoint;
Logging.Debug($"[DecorationDraggableInstance] Started drag from statue: {_decorationData?.DecorationName}");
}
#region Pointer Event Handlers
/// <summary>
/// Handle pointer click - only when placed on statue
/// </summary>
public void OnPointerClick(PointerEventData eventData)
{
// Only handle clicks when placed on statue and not currently dragging
if (!_isPlacedOnStatue || _dragStarted) return;
Logging.Debug($"[DecorationDraggableInstance] Decoration tapped: {_decorationData?.DecorationName}");
// Future: Open detail view, play sound effect, show info popup, etc.
}
/// <summary>
/// Handle drag start - only when placed on statue
/// </summary>
public void OnBeginDrag(PointerEventData eventData)
{
// Only handle drag from statue if already placed
if (!_isPlacedOnStatue) return;
_dragStarted = true;
StartDragFromStatue(eventData);
}
/// <summary>
/// Handle drag continuation
/// </summary>
public void OnDrag(PointerEventData eventData)
{
if (!_isDragging) return;
ContinueDrag(eventData);
}
/// <summary>
/// Handle drag end
/// </summary>
public void OnEndDrag(PointerEventData eventData)
{
if (!_isDragging) return;
_dragStarted = false;
EndDrag(eventData);
}
#endregion
}
}