Files
2026-05-26 16:15:54 +08:00

137 lines
4.0 KiB
C#

using CW.Common;
using PaintCore;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
namespace game
{
[RequireComponent(typeof(CwHitPointers))]
public class BrushPointerMouse : CwPointer
{
public Vector2 Offset { set { offset = value; } get { return offset; } }
[SerializeField] private Vector2 offset = Vector2.zero;
/// <summary>If you enable this, then a paint preview will be shown under the mouse as long as the <b>RequiredKey</b> is not pressed.</summary>
public bool Preview { set { preview = value; } get { return preview; } }
[SerializeField] private bool preview;
/// <summary>This component will paint while any of the specified mouse buttons or keyboard keys are held.</summary>
public List<KeyCode> Keys { get { if (keys == null) { keys = new List<KeyCode>(); } return keys; } }
[SerializeField] private List<KeyCode> keys;
private readonly int PREVIEW_FINGER_INDEX = -1;
private readonly int PAINT_FINGER_INDEX = 1;
[System.NonSerialized]
private bool oldHeld;
public bool TryAddKey(KeyCode key)
{
if (Keys.Contains(key) == false)
{
keys.Add(key);
return true;
}
return false;
}
public bool GetKeyHeld()
{
if (keys != null)
{
foreach (var key in keys)
{
if (CwInput.GetKeyIsHeld(key) == true)
{
return true;
}
}
}
return false;
}
#if UNITY_EDITOR
protected virtual void Reset()
{
if (keys == null)
{
keys = new List<KeyCode>();
}
else
{
keys.Clear();
}
keys.Add(KeyCode.Mouse0);
}
#endif
protected virtual void Update()
{
var newHeld = false;
var enablePreview = false;
var enablePaint = false;
bool isFollowing = false;
Vector2 screenPosition = Vector2.zero;
if (CwInput.GetMouseExists() == true)
{
CwInputManager.Finger finger;
newHeld = GetKeyHeld();
enablePaint = newHeld == true || oldHeld == true;
enablePreview = preview == true && enablePaint == false;
Vector2 position = CwInput.GetMousePosition();
position += offset * CwInputManager.ScaleFactor;
if (enablePreview == true)
{
GetFinger(PREVIEW_FINGER_INDEX, position, 1.0f, true, out finger);
cachedHitPointers.HandleFingerUpdate(finger, false, false);
screenPosition = position;
isFollowing = true;
}
if (enablePaint == true)
{
if (EventSystem.current == null) return;
if (EventSystem.current.IsPointerOverGameObject()) return;
var down = GetFinger(PAINT_FINGER_INDEX, position, 1.0f, true, out finger);
cachedHitPointers.HandleFingerUpdate(finger, down, newHeld == false);
screenPosition = position;
isFollowing = true;
}
}
if (isFollowing) FishingYachtAct.Publish<EventWashingGestureFollowPositionData>(new EventWashingGestureFollowPositionData(screenPosition, true));
else FishingYachtAct.Publish<EventWashingGestureFollowPositionData>(new EventWashingGestureFollowPositionData(screenPosition, false));
if (enablePreview == false)
{
TryNullFinger(PREVIEW_FINGER_INDEX);
}
if (enablePaint == false)
{
TryNullFinger(PAINT_FINGER_INDEX);
}
oldHeld = newHeld;
}
}
}