summaryrefslogtreecommitdiff
path: root/Thronefall_v1.0/Rewired/Rewired.Demos/PlayerMouseSpriteExample.cs
blob: 194fb5dbc7e463c1f0183a9cd137af89739d3e57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using UnityEngine;

namespace Rewired.Demos;

[AddComponentMenu("")]
public class PlayerMouseSpriteExample : MonoBehaviour
{
	[Tooltip("The Player that will control the mouse")]
	public int playerId;

	[Tooltip("The Rewired Action used for the mouse horizontal axis.")]
	public string horizontalAction = "MouseX";

	[Tooltip("The Rewired Action used for the mouse vertical axis.")]
	public string verticalAction = "MouseY";

	[Tooltip("The Rewired Action used for the mouse wheel axis.")]
	public string wheelAction = "MouseWheel";

	[Tooltip("The Rewired Action used for the mouse left button.")]
	public string leftButtonAction = "MouseLeftButton";

	[Tooltip("The Rewired Action used for the mouse right button.")]
	public string rightButtonAction = "MouseRightButton";

	[Tooltip("The Rewired Action used for the mouse middle button.")]
	public string middleButtonAction = "MouseMiddleButton";

	[Tooltip("The distance from the camera that the pointer will be drawn.")]
	public float distanceFromCamera = 1f;

	[Tooltip("The scale of the sprite pointer.")]
	public float spriteScale = 0.05f;

	[Tooltip("The pointer prefab.")]
	public GameObject pointerPrefab;

	[Tooltip("The click effect prefab.")]
	public GameObject clickEffectPrefab;

	[Tooltip("Should the hardware pointer be hidden?")]
	public bool hideHardwarePointer = true;

	[NonSerialized]
	private GameObject pointer;

	[NonSerialized]
	private PlayerMouse mouse;

	private void Awake()
	{
		pointer = UnityEngine.Object.Instantiate(pointerPrefab);
		pointer.transform.localScale = new Vector3(spriteScale, spriteScale, spriteScale);
		if (hideHardwarePointer)
		{
			Cursor.visible = false;
		}
		mouse = PlayerMouse.Factory.Create();
		mouse.playerId = playerId;
		mouse.xAxis.actionName = horizontalAction;
		mouse.yAxis.actionName = verticalAction;
		mouse.wheel.yAxis.actionName = wheelAction;
		mouse.leftButton.actionName = leftButtonAction;
		mouse.rightButton.actionName = rightButtonAction;
		mouse.middleButton.actionName = middleButtonAction;
		mouse.pointerSpeed = 1f;
		mouse.wheel.yAxis.repeatRate = 5f;
		mouse.screenPosition = new Vector2((float)Screen.width * 0.5f, (float)Screen.height * 0.5f);
		mouse.ScreenPositionChangedEvent += OnScreenPositionChanged;
		OnScreenPositionChanged(mouse.screenPosition);
	}

	private void Update()
	{
		if (ReInput.isReady)
		{
			pointer.transform.Rotate(Vector3.forward, mouse.wheel.yAxis.value * 20f);
			if (mouse.leftButton.justPressed)
			{
				CreateClickEffect(new Color(0f, 1f, 0f, 1f));
			}
			if (mouse.rightButton.justPressed)
			{
				CreateClickEffect(new Color(1f, 0f, 0f, 1f));
			}
			if (mouse.middleButton.justPressed)
			{
				CreateClickEffect(new Color(1f, 1f, 0f, 1f));
			}
		}
	}

	private void OnDestroy()
	{
		if (ReInput.isReady)
		{
			mouse.ScreenPositionChangedEvent -= OnScreenPositionChanged;
		}
	}

	private void CreateClickEffect(Color color)
	{
		GameObject obj = UnityEngine.Object.Instantiate(clickEffectPrefab);
		obj.transform.localScale = new Vector3(spriteScale, spriteScale, spriteScale);
		obj.transform.position = Camera.main.ScreenToWorldPoint(new Vector3(mouse.screenPosition.x, mouse.screenPosition.y, distanceFromCamera));
		obj.GetComponentInChildren<SpriteRenderer>().color = color;
		UnityEngine.Object.Destroy(obj, 0.5f);
	}

	private void OnScreenPositionChanged(Vector2 position)
	{
		Vector3 position2 = Camera.main.ScreenToWorldPoint(new Vector3(position.x, position.y, distanceFromCamera));
		pointer.transform.position = position2;
	}
}