mirror of
https://github.com/Ratstail91/Keep-It-Alive.git
synced 2025-11-29 10:34:27 +11:00
Working on it
This commit is contained in:
53
Assets/CarbonInput/Scripts/TouchInput/BaseTouchInput.cs
Normal file
53
Assets/CarbonInput/Scripts/TouchInput/BaseTouchInput.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace CarbonInput {
|
||||
/// <summary>
|
||||
/// Base class for all touch controls.
|
||||
/// </summary>
|
||||
public class BaseTouchInput : MonoBehaviour {
|
||||
/// <summary>
|
||||
/// The index of the player this control belongs to. If set to Any, it will use the first free player.
|
||||
/// </summary>
|
||||
[Tooltip("The index of the player this control belongs to. If set to Any, it will use the first free player.")]
|
||||
public PlayerIndex Index;
|
||||
/// <summary>
|
||||
/// Mapping of this control.
|
||||
/// </summary>
|
||||
protected TouchMapping Mapping;
|
||||
|
||||
/// <summary>
|
||||
/// Initialize this input by injecting a <see cref="TouchMapping"/> into <see cref="GamePad.PlayerMappings"/>.
|
||||
/// </summary>
|
||||
protected void InitMapping() {
|
||||
if(Index == PlayerIndex.Any) {
|
||||
ControllerInstance[] mappings = GamePad.GetPlayerMappings();
|
||||
for(int i = 1; i < CarbonController.PlayerIndices; i++) {
|
||||
if(mappings[i].Controller.Replacable || mappings[i].Controller is TouchMapping) {
|
||||
UseMapping(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
// all mappings already in use
|
||||
} else {
|
||||
UseMapping((int)Index);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Changes index <paramref name="idx"/> of the <see cref="GamePad.PlayerMappings"/> to a <see cref="TouchMapping"/>.
|
||||
/// </summary>
|
||||
/// <param name="idx"></param>
|
||||
private void UseMapping(int idx) {
|
||||
ControllerInstance[] mappings = GamePad.GetPlayerMappings();
|
||||
// if there is already a TouchMapping, use it.
|
||||
if(mappings[idx] != null && mappings[idx].Controller is TouchMapping)
|
||||
Mapping = (TouchMapping)mappings[idx].Controller;
|
||||
else {//otherwise overwrite the old value
|
||||
Mapping = ScriptableObject.CreateInstance<TouchMapping>();
|
||||
mappings[idx] = new ControllerInstance(Mapping, 0);
|
||||
}
|
||||
// if we set PlayerIndex.One, we must also set PlayerIndex.Any, because AnyBehaviour.UseMappingOne needs this
|
||||
if(idx == 1) mappings[0] = mappings[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
18
Assets/CarbonInput/Scripts/TouchInput/BaseTouchInput.cs.meta
Normal file
18
Assets/CarbonInput/Scripts/TouchInput/BaseTouchInput.cs.meta
Normal file
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 996c12ad06e1905499f873d083ef1fb0
|
||||
labels:
|
||||
- Touchinput
|
||||
- Touch
|
||||
- Input
|
||||
- Gamepad
|
||||
- Joystick
|
||||
timeCreated: 1455829286
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
44
Assets/CarbonInput/Scripts/TouchInput/DisableTouchInput.cs
Normal file
44
Assets/CarbonInput/Scripts/TouchInput/DisableTouchInput.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace CarbonInput {
|
||||
/// <summary>
|
||||
/// Attach this to the canvas all touch controls are in.
|
||||
/// On startup this script will check if there are any real gamepads and if so, it will disable the touch controls.
|
||||
/// </summary>
|
||||
public class DisableTouchInput : MonoBehaviour {
|
||||
[Tooltip("If true, touch controls will be disabled on Console Platforms, even if there are no gamepads connected.")]
|
||||
public bool HideOnConsole = true;
|
||||
[Tooltip("If true, touch controls will be disabled in Web Player, even if there are no gamepads connected.")]
|
||||
public bool HideOnWeb = true;
|
||||
[Tooltip("If true, touch controls will be disabled in the Editor, even if there are no gamepads connected.")]
|
||||
public bool HideOnEditMode = false;
|
||||
[Tooltip("If true, touch controls will be disabled on Windows, Linux and Mac, even if there are no gamepads connected.")]
|
||||
public bool HideOnPC = true;
|
||||
void Start() {
|
||||
#if UNITY_EDITOR
|
||||
if(HideOnEditMode) { Hide(); return; }
|
||||
#endif
|
||||
#if UNITY_WEB
|
||||
if(HideOnWeb) { Hide(); return; }
|
||||
#endif
|
||||
#if UNITY_STANDALONE
|
||||
if(HideOnPC) { Hide(); return; }
|
||||
#endif
|
||||
if(HideOnConsole && Application.isConsolePlatform
|
||||
|| GamePad.GamePadCount > 0) { // There are gamepads so we don't need touchcontrols
|
||||
Hide();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deactivates all children with a <see cref="BaseTouchInput"/> component.
|
||||
/// </summary>
|
||||
private void Hide() {
|
||||
// Iterate over all children
|
||||
foreach(RectTransform rect in GetComponentsInChildren<RectTransform>()) {
|
||||
if(rect.GetComponent<BaseTouchInput>() != null) // Deactivate all TouchControls
|
||||
rect.gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f8fff0c72c46bce4e827c2d00d47911f
|
||||
labels:
|
||||
- Touchinput
|
||||
- Touch
|
||||
- Input
|
||||
- Gamepad
|
||||
- Joystick
|
||||
timeCreated: 1456010930
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
57
Assets/CarbonInput/Scripts/TouchInput/TouchButton.cs
Normal file
57
Assets/CarbonInput/Scripts/TouchInput/TouchButton.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace CarbonInput {
|
||||
/// <summary>
|
||||
/// Touch control simulating a single gamepad button.
|
||||
/// </summary>
|
||||
public class TouchButton : BaseTouchInput, IPointerDownHandler, IPointerUpHandler, IDragHandler {
|
||||
/// <summary>
|
||||
/// The <see cref="CButton"/> this control emulates.
|
||||
/// </summary>
|
||||
public CButton Button;
|
||||
/// <summary>
|
||||
/// Opacity of this control if it is pressed.
|
||||
/// </summary>
|
||||
[Tooltip("Opacity of this control if it is pressed.")]
|
||||
[Range(0, 1)]
|
||||
public float OpacityPressed = 0.5f;
|
||||
/// <summary>
|
||||
/// Opacity of this control if it is not pressed.
|
||||
/// </summary>
|
||||
[Tooltip("Opacity of this control if it is not pressed.")]
|
||||
[Range(0, 1)]
|
||||
public float OpacityReleased = 1f;
|
||||
|
||||
void Start() {
|
||||
InitMapping();
|
||||
UpdateState(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the state of this control. This methods sets the opacity and the state in the <see cref="TouchMapping"/>.
|
||||
/// </summary>
|
||||
/// <param name="pressed"></param>
|
||||
public void UpdateState(bool pressed) {
|
||||
var image = GetComponent<Image>();
|
||||
var color = image.color;
|
||||
color.a = pressed ? OpacityPressed : OpacityReleased;
|
||||
image.color = color;
|
||||
if(Mapping != null) Mapping[Button] = pressed;
|
||||
}
|
||||
|
||||
public void OnPointerDown(PointerEventData eventData) {
|
||||
UpdateState(true);
|
||||
}
|
||||
|
||||
public void OnPointerUp(PointerEventData eventData) {
|
||||
UpdateState(false);
|
||||
}
|
||||
|
||||
public void OnDrag(PointerEventData eventData) {
|
||||
RectTransform rect = GetComponent<RectTransform>();
|
||||
UpdateState(RectTransformUtility.RectangleContainsScreenPoint(rect, eventData.position));
|
||||
}
|
||||
}
|
||||
}
|
||||
18
Assets/CarbonInput/Scripts/TouchInput/TouchButton.cs.meta
Normal file
18
Assets/CarbonInput/Scripts/TouchInput/TouchButton.cs.meta
Normal file
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d698f325b18cad948af4abeae474734d
|
||||
labels:
|
||||
- Touchinput
|
||||
- Touch
|
||||
- Input
|
||||
- Gamepad
|
||||
- Joystick
|
||||
timeCreated: 1455826413
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
45
Assets/CarbonInput/Scripts/TouchInput/TouchMapping.cs
Normal file
45
Assets/CarbonInput/Scripts/TouchInput/TouchMapping.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
namespace CarbonInput {
|
||||
/// <summary>
|
||||
/// Internal wrapper class, used to access touch input.
|
||||
/// </summary>
|
||||
public class TouchMapping : CarbonController {
|
||||
/// <summary>
|
||||
/// Currently pressed buttons.
|
||||
/// </summary>
|
||||
private readonly bool[] buttonMap = new bool[ButtonCount];
|
||||
/// <summary>
|
||||
/// Current values of all axes.
|
||||
/// </summary>
|
||||
private readonly float[] axisMap = new float[AxisCount];
|
||||
|
||||
private void OnEnable() {
|
||||
name = "TouchInput";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets if the specific button is pressed or not.
|
||||
/// </summary>
|
||||
/// <param name="button"></param>
|
||||
/// <returns></returns>
|
||||
public bool this[CButton button] {
|
||||
get { return buttonMap[(int)button]; }
|
||||
set { buttonMap[(int)button] = value; }
|
||||
}
|
||||
/// <summary>
|
||||
/// Gets or sets the value of the given axis.
|
||||
/// </summary>
|
||||
/// <param name="axis"></param>
|
||||
/// <returns></returns>
|
||||
public float this[CAxis axis] {
|
||||
get { return axisMap[(int)axis]; }
|
||||
set { axisMap[(int)axis] = value; }
|
||||
}
|
||||
|
||||
public override bool GetButton(CButton btn, int id) {
|
||||
return this[btn];
|
||||
}
|
||||
public override float GetAxis(CAxis axis, int id) {
|
||||
return this[axis];
|
||||
}
|
||||
}
|
||||
}
|
||||
18
Assets/CarbonInput/Scripts/TouchInput/TouchMapping.cs.meta
Normal file
18
Assets/CarbonInput/Scripts/TouchInput/TouchMapping.cs.meta
Normal file
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4fae99fbdd9f6aa47b8f0aedfa36d8b7
|
||||
labels:
|
||||
- Touchinput
|
||||
- Touch
|
||||
- Input
|
||||
- Gamepad
|
||||
- Joystick
|
||||
timeCreated: 1455827496
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
175
Assets/CarbonInput/Scripts/TouchInput/TouchStick.cs
Normal file
175
Assets/CarbonInput/Scripts/TouchInput/TouchStick.cs
Normal file
@@ -0,0 +1,175 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
namespace CarbonInput {
|
||||
/// <summary>
|
||||
/// Touch control simulating a thumbstick.
|
||||
/// </summary>
|
||||
public class TouchStick : BaseTouchInput, IPointerDownHandler, IPointerUpHandler, IDragHandler {
|
||||
private const float NearZero = 0.0001f;
|
||||
|
||||
/// <summary>
|
||||
/// Horizontal axis of this control.
|
||||
/// </summary>
|
||||
[Tooltip("Horizontal axis")]
|
||||
public CAxis X = CAxis.LX;
|
||||
/// <summary>
|
||||
/// Vertical axis of this control.
|
||||
/// </summary>
|
||||
[Tooltip("Vertical axis")]
|
||||
public CAxis Y = CAxis.LY;
|
||||
|
||||
/// <summary>
|
||||
/// Touches inside this area will be handled by the stick.
|
||||
/// </summary>
|
||||
[Tooltip("Touches inside this area will be handled by the stick.")]
|
||||
public RectTransform TouchArea;
|
||||
/// <summary>
|
||||
/// Base of the joystick.
|
||||
/// </summary>
|
||||
[Tooltip("Base of the joystick.")]
|
||||
public RectTransform Base;
|
||||
/// <summary>
|
||||
/// Knob of the joystick.
|
||||
/// </summary>
|
||||
[Tooltip("Knob of the joystick.")]
|
||||
public RectTransform Stick;
|
||||
|
||||
/// <summary>
|
||||
/// Maximum distance between center of base and center of stick.
|
||||
/// </summary>
|
||||
[Tooltip("Maximum distance between center of base and center of stick.")]
|
||||
[Range(20, 120)]
|
||||
public float Range = 60;
|
||||
/// <summary>
|
||||
/// Should the joystick disappear on release?
|
||||
/// </summary>
|
||||
[Tooltip("Should the joystick disappear on release?")]
|
||||
public bool HideOnRelease;
|
||||
/// <summary>
|
||||
/// If HideOnRelease is set to true, this value will determine after which time the joystick will start to fade out.
|
||||
/// </summary>
|
||||
[Tooltip("If HideOnRelease is set to true, this value will determine after which time the joystick will start to fade out.")]
|
||||
public float FadeoutDelay;
|
||||
/// <summary>
|
||||
/// If HideOnRelease is set to true, this value will determine how long the fadeout will last.
|
||||
/// </summary>
|
||||
[Tooltip("If HideOnRelease is set to true, this value will determine how long the fadeout will last.")]
|
||||
public float FadeoutTime = 1f;
|
||||
/// <summary>
|
||||
/// If the user moves to far away from the stick, should the stick follow?
|
||||
/// </summary>
|
||||
[Tooltip("If the user moves to far away from the stick, should the stick follow?")]
|
||||
public bool Movable;
|
||||
|
||||
private CanvasRenderer[] childRenderer;
|
||||
|
||||
void Start() {
|
||||
InitMapping();
|
||||
childRenderer = GetComponentsInChildren<CanvasRenderer>();
|
||||
if(HideOnRelease) Hide(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shows this control.
|
||||
/// </summary>
|
||||
public void Show() {
|
||||
StopAllCoroutines();
|
||||
SetOpacity(1f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hides this control.
|
||||
/// </summary>
|
||||
/// <param name="fadeout">If true, the control will slowly fade out.</param>
|
||||
public void Hide(bool fadeout) {
|
||||
StopAllCoroutines();
|
||||
if(fadeout) StartCoroutine(FadeSequence());
|
||||
else SetOpacity(0f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the opacity of this control and all children.
|
||||
/// </summary>
|
||||
/// <param name="opacity"></param>
|
||||
private void SetOpacity(float opacity) {
|
||||
foreach(CanvasRenderer renderer in childRenderer) renderer.SetAlpha(opacity);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Coroutine used to slowly fadeout.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private IEnumerator FadeSequence() {
|
||||
if(FadeoutDelay > 0) yield return new WaitForSeconds(FadeoutDelay);
|
||||
float opacity = 1f;
|
||||
float speed = 1f / FadeoutTime;
|
||||
while(opacity >= 0.0f) {
|
||||
opacity -= Time.deltaTime * speed;
|
||||
if(opacity < 0) opacity = 0;
|
||||
SetOpacity(opacity);
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the value of this stick in the <see cref="TouchMapping"/> and also sets the knob position.
|
||||
/// If <see cref="Movable"/> is true, it will also follow the user.
|
||||
/// </summary>
|
||||
/// <param name="pos">Touch position in world space</param>
|
||||
private void UpdateStick(Vector2 pos) {
|
||||
// get direction in local space
|
||||
Vector2 direction = (pos - (Vector2)Base.position);
|
||||
direction.x /= Base.lossyScale.x;
|
||||
direction.y /= Base.lossyScale.y;
|
||||
float length = direction.magnitude;
|
||||
if(length < NearZero) {
|
||||
UpdateAxis(Vector2.zero);
|
||||
return;
|
||||
}
|
||||
if(length > Range) {
|
||||
if(Movable) {
|
||||
Vector2 delta = direction.normalized * (length - Range);
|
||||
Vector2 newPos = (Vector2)Base.localPosition + delta;
|
||||
newPos.x = Mathf.Clamp(newPos.x, TouchArea.rect.xMin, TouchArea.rect.xMax);
|
||||
newPos.y = Mathf.Clamp(newPos.y, TouchArea.rect.yMin, TouchArea.rect.yMax);
|
||||
Base.localPosition = newPos;
|
||||
}
|
||||
length = Range;
|
||||
}
|
||||
UpdateAxis(direction.normalized * (length / Range));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the <see cref="AxisMapping"/>.
|
||||
/// </summary>
|
||||
/// <param name="axis"></param>
|
||||
private void UpdateAxis(Vector2 axis) {
|
||||
if(Mapping == null) return;
|
||||
Stick.localPosition = axis * Range;
|
||||
Mapping[X] = axis.x;
|
||||
Mapping[Y] = -axis.y; // invert to match "normal" controller axis
|
||||
}
|
||||
|
||||
public void OnPointerDown(PointerEventData data) {
|
||||
Show();
|
||||
if(RectTransformUtility.RectangleContainsScreenPoint(Stick, data.position) ||
|
||||
RectTransformUtility.RectangleContainsScreenPoint(Base, data.position)) {
|
||||
UpdateStick(data.position);
|
||||
} else if(Movable) {
|
||||
Base.position = data.position;
|
||||
UpdateAxis(Vector2.zero);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPointerUp(PointerEventData data) {
|
||||
UpdateAxis(Vector2.zero);
|
||||
if(HideOnRelease) Hide(true);
|
||||
}
|
||||
|
||||
public void OnDrag(PointerEventData data) {
|
||||
UpdateStick(data.position);
|
||||
}
|
||||
}
|
||||
}
|
||||
18
Assets/CarbonInput/Scripts/TouchInput/TouchStick.cs.meta
Normal file
18
Assets/CarbonInput/Scripts/TouchInput/TouchStick.cs.meta
Normal file
@@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d08a4cd93a9455549863e3be1bc004f2
|
||||
labels:
|
||||
- Touchinput
|
||||
- Touch
|
||||
- Input
|
||||
- Gamepad
|
||||
- Joystick
|
||||
timeCreated: 1455826432
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user