220 lines
5.9 KiB
C#
220 lines
5.9 KiB
C#
using UnityEngine;
|
|
|
|
public class RealWaterMouseInput : MonoBehaviour
|
|
{
|
|
[Tooltip("Force multiplier applied to all touch and mouse inputs.")]
|
|
[Range(0.01f, 1f)]
|
|
public float InputForce = 1f;
|
|
|
|
[Tooltip("Enables multi touch input to RealWater planes for supported devices (max 10 simultaneous inputs).")]
|
|
public bool MultiTouchInput;
|
|
|
|
[Tooltip("Maximum number of simultaneous touch inputs to the plane. Large numbers of concurrent touches may decrease performance.")]
|
|
[Range(1f, 10f)]
|
|
public int MaxTouches = 3;
|
|
|
|
[Tooltip("Extrapolates between input points, allows for smoother more consistent input")]
|
|
public bool ExtrapolateInput = true;
|
|
|
|
private bool checkForInput = true;
|
|
|
|
private float inputChangeCheckFrequency = 2000f;
|
|
|
|
private RaycastHit hit;
|
|
|
|
private RWInput[] touches = new RWInput[10];
|
|
|
|
private RWInput mouse = new RWInput();
|
|
|
|
private BresenhamLineDrawer lineDrawer = new BresenhamLineDrawer();
|
|
|
|
private void Start()
|
|
{
|
|
InvokeRepeating("InputChangeDetection", 0.003f, 1f / inputChangeCheckFrequency);
|
|
for (int i = 0; i < 10; i++)
|
|
{
|
|
touches[i] = new RWInput();
|
|
}
|
|
}
|
|
|
|
public bool GetCheckInput()
|
|
{
|
|
return checkForInput;
|
|
}
|
|
|
|
public void SetCheckCollisions(bool val)
|
|
{
|
|
checkForInput = val;
|
|
}
|
|
|
|
private void InputChangeDetection()
|
|
{
|
|
if (Input.GetMouseButtonUp(0))
|
|
{
|
|
mouse.SetFirstInput(val: true);
|
|
}
|
|
if (!MultiTouchInput || !ExtrapolateInput)
|
|
{
|
|
return;
|
|
}
|
|
Touch[] array = Input.touches;
|
|
for (int i = 0; i < array.Length; i++)
|
|
{
|
|
Touch touch = array[i];
|
|
if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Began)
|
|
{
|
|
for (int j = 0; j < 10; j++)
|
|
{
|
|
touches[j].SetFirstInput(val: true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void CheckInput()
|
|
{
|
|
if (!MultiTouchInput)
|
|
{
|
|
if (!Input.GetMouseButton(0) || !checkForInput)
|
|
{
|
|
return;
|
|
}
|
|
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit) && hit.collider.gameObject.name == "RealWater Plane")
|
|
{
|
|
float num = hit.collider.gameObject.GetComponent<RealWaterInterface>().GetRows();
|
|
float num2 = hit.collider.gameObject.GetComponent<RealWaterInterface>().GetCols();
|
|
float speed = hit.collider.gameObject.GetComponent<RealWaterInterface>().GetSpeed();
|
|
int num3 = (int)(hit.textureCoord.x * num2);
|
|
int num4 = (int)(hit.textureCoord.y * num);
|
|
int bR = hit.collider.gameObject.GetComponent<RealWaterInterface>().GetBR();
|
|
bool paused = hit.collider.gameObject.GetComponent<RealWaterInterface>().GetPaused();
|
|
if (speed != 0f && num3 > bR + 1 && (float)num3 < num2 - (float)bR + 1f && (float)num4 < num - (float)(bR + 2) && num4 > bR + 2 && !paused)
|
|
{
|
|
if (ExtrapolateInput)
|
|
{
|
|
if (!mouse.GetFirstInput())
|
|
{
|
|
if (mouse.GetFirstPoint())
|
|
{
|
|
mouse.SetX1(num4);
|
|
mouse.SetY1(num3);
|
|
mouse.SetPlane1(hit.collider.GetInstanceID());
|
|
}
|
|
else
|
|
{
|
|
mouse.SetX2(num4);
|
|
mouse.SetY2(num3);
|
|
mouse.SetPlane2(hit.collider.GetInstanceID());
|
|
}
|
|
if (mouse.GetPlane1() == mouse.GetPlane2())
|
|
{
|
|
lineDrawer.DrawLine(mouse.GetX1(), mouse.GetX2(), mouse.GetY1(), mouse.GetY2(), hit, InputForce);
|
|
}
|
|
mouse.SetFirstPoint(!mouse.GetFirstPoint());
|
|
}
|
|
else
|
|
{
|
|
mouse.SetX1(num4);
|
|
mouse.SetY1(num3);
|
|
mouse.SetX2(num4);
|
|
mouse.SetY2(num3);
|
|
mouse.SetFirstInput(val: false);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
hit.collider.gameObject.GetComponent<RealWaterInterface>().DisturbBufferAt(num4, num3, (int)(18000000f * InputForce), ambient: false);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
mouse.SetFirstInput(val: true);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
mouse.SetFirstInput(val: true);
|
|
}
|
|
return;
|
|
}
|
|
int num5 = 0;
|
|
if (Input.touchCount <= 0 || !checkForInput)
|
|
{
|
|
return;
|
|
}
|
|
Touch[] array = Input.touches;
|
|
foreach (Touch touch in array)
|
|
{
|
|
if (num5 >= MaxTouches)
|
|
{
|
|
break;
|
|
}
|
|
if (Physics.Raycast(Camera.main.ScreenPointToRay(touch.position), out hit) && hit.collider.gameObject.name == "RealWater Plane")
|
|
{
|
|
float num6 = hit.collider.gameObject.GetComponent<RealWaterInterface>().GetRows();
|
|
float num7 = hit.collider.gameObject.GetComponent<RealWaterInterface>().GetCols();
|
|
int num8 = (int)(hit.textureCoord.x * num7);
|
|
int num9 = (int)(hit.textureCoord.y * num6);
|
|
int bR2 = hit.collider.gameObject.GetComponent<RealWaterInterface>().GetBR();
|
|
if (num8 > bR2 + 1 && (float)num8 < num7 - (float)bR2 + 1f && (float)num9 < num6 - (float)(bR2 + 2) && num9 > bR2 + 1)
|
|
{
|
|
if (ExtrapolateInput)
|
|
{
|
|
if (!touches[num5].GetFirstInput())
|
|
{
|
|
if (touches[num5].GetFirstPoint())
|
|
{
|
|
touches[num5].SetX1(num9);
|
|
touches[num5].SetY1(num8);
|
|
touches[num5].SetPlane1(hit.collider.GetInstanceID());
|
|
}
|
|
else
|
|
{
|
|
touches[num5].SetX2(num9);
|
|
touches[num5].SetY2(num8);
|
|
touches[num5].SetPlane2(hit.collider.GetInstanceID());
|
|
}
|
|
if (touches[num5].GetPlane1() == touches[num5].GetPlane2())
|
|
{
|
|
lineDrawer.DrawLine(touches[num5].GetX1(), touches[num5].GetX2(), touches[num5].GetY1(), touches[num5].GetY2(), hit, InputForce);
|
|
}
|
|
touches[num5].SetFirstPoint(!touches[num5].GetFirstPoint());
|
|
}
|
|
else
|
|
{
|
|
touches[num5].SetX1(num9);
|
|
touches[num5].SetY1(num8);
|
|
touches[num5].SetX2(num9);
|
|
touches[num5].SetY2(num8);
|
|
touches[num5].SetFirstInput(val: false);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
hit.collider.gameObject.GetComponent<RealWaterInterface>().DisturbBufferAt(num9, num8, (int)(18000000f * InputForce), ambient: false);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
touches[num5].SetFirstInput(val: true);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
touches[num5].SetFirstInput(val: true);
|
|
}
|
|
num5++;
|
|
}
|
|
num5 = 0;
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
mouse.SetFirstInput(val: true);
|
|
for (int i = 0; i < 10; i++)
|
|
{
|
|
touches[i].SetFirstInput(val: true);
|
|
}
|
|
}
|
|
}
|