-
-
Save nicloay/9790e4226c8a966edefc5f62ca3c364e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.EventSystems; | |
using PaintCraft.Tools; | |
using OrthographicCamControll; | |
using PaintCraft.Canvas.Configs; | |
namespace AnotherProject.Controllers{ | |
[RequireComponent(typeof(Camera))] | |
public class ScreenCameraController : InputController { | |
public LineConfig LineConfig; | |
public Camera Camera { get; private set; } | |
[SerializeField] | |
private CameraData _cameraData; | |
void Awake() | |
{ | |
LineConfig.OnToolChange.AddListener(OnToolChange); | |
} | |
private void OnToolChange(ToolType arg0) | |
{ | |
if (arg0 == ToolType.Brush) | |
{ | |
_cameraData.Events.OnPanBegin.AddListener(OnPanBegin); | |
_cameraData.Events.OnPan.AddListener(OnPan); | |
_cameraData.Events.OnPanEnd.AddListener(OnPanEnd); | |
_cameraData.Events.OnClick.AddListener(OnClick); | |
} | |
else | |
{ | |
_cameraData.Events.OnPanBegin.RemoveListener(OnPanBegin); | |
_cameraData.Events.OnPan.RemoveListener(OnPan); | |
_cameraData.Events.OnPanEnd.RemoveListener(OnPanEnd); | |
_cameraData.Events.OnClick.RemoveListener(OnClick); | |
} | |
} | |
private void OnClick(Vector2 currentPosition) | |
{ | |
OnPanBegin(currentPosition); | |
OnPan(currentPosition, currentPosition); | |
OnPanEnd(currentPosition); | |
} | |
private void OnPanBegin(Vector2 currentPosition) | |
{ | |
BeginLine(LineConfig, 0, GetWorldPosition(currentPosition), true); | |
} | |
private void OnPan(Vector2 previousPosition, Vector2 currentPosition) | |
{ | |
ContinueLine(0, GetWorldPosition(currentPosition)); | |
} | |
private void OnPanEnd(Vector2 currentPosition) | |
{ | |
EndLine(0, GetWorldPosition(currentPosition)); | |
} | |
void Start(){ | |
if (Canvas == null){ | |
Debug.LogError("you have to provide link to the Canvas for this component", gameObject); | |
} | |
Camera = GetComponent<Camera>(); | |
if (Camera == null){ | |
Debug.Log("you have to add camera component to this object", gameObject); | |
} | |
} | |
bool IsAnyPointerOverGameObject() | |
{ | |
bool result = EventSystem.current.IsPointerOverGameObject(); | |
foreach (var touch in Input.touches) | |
{ | |
result |= EventSystem.current.IsPointerOverGameObject(touch.fingerId); | |
} | |
return result; | |
} | |
public override bool DontAllowInteraction(Vector2 worldPosition) | |
{ | |
bool result = !Camera.pixelRect.Contains(Camera.WorldToScreenPoint( worldPosition)); | |
result |= IsAnyPointerOverGameObject(); | |
return result; | |
} | |
Vector3 GetWorldPosition(Vector2 screenPosition) | |
{ | |
Vector3 vector3ScreenPosition = screenPosition; | |
vector3ScreenPosition.z = transform.position.z; | |
return Camera.ScreenToWorldPoint(vector3ScreenPosition); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment