Skip to content

Commit 59778c6

Browse files
committed
More work on onscreen controls.
1 parent 54ccf57 commit 59778c6

18 files changed

+1150
-531
lines changed
8.66 KB
Loading

Assets/Samples/RebindingUI/OnScreen/Button.png.meta

Lines changed: 143 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Samples/RebindingUI/OnScreen/CustomOnScreenControl.cs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,6 @@ public Vector2 position
5151
}
5252
}
5353

54-
/// <summary>
55-
/// Gets or sets the size of the geometric clipping area.
56-
/// </summary>
57-
public Vector2 size
58-
{
59-
get => m_NormalizedBounds.size;
60-
set
61-
{
62-
if (m_NormalizedBounds.size.Equals(value))
63-
return;
64-
65-
m_NormalizedBounds.size = value;
66-
OnConfigurationChanged();
67-
}
68-
}
69-
7054
/// <summary>
7155
/// Gets or sets the bounds of the geometric clipping area.
7256
/// </summary>
@@ -163,14 +147,18 @@ protected override void OnEnable()
163147
{
164148
base.OnEnable();
165149

166-
// TODO This should really be reacting to changes of control path and properties
167-
const float threshold = 10.0f;
168-
if (control is ButtonControl)
169-
m_Detector = new Detector<ActiveDetector>(1, new ActiveDetector());
170-
else if (control is StickControl)
171-
m_Detector = new Detector<DragDetector>(1, new DragDetector(0.0f));
172-
else
173-
throw new Exception($"Unsupported control type: {control.GetType()}");
150+
// We cannot setup detector if we have no input control
151+
var c = control;
152+
if (c != null)
153+
{
154+
// TODO This should really be reacting to changes of control path and properties
155+
if (c is ButtonControl)
156+
m_Detector = new Detector<ActiveDetector>(1, new ActiveDetector());
157+
else if (c is StickControl)
158+
m_Detector = new Detector<DragDetector>(1, new DragDetector(0.0f));
159+
else
160+
throw new Exception($"Unsupported control type: {control.GetType()}");
161+
}
174162

175163
ChangeDevice();
176164
InputSystem.onDeviceChange += OnDeviceChange;

Assets/Samples/RebindingUI/OnScreen/Detector.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public virtual void Reset(Rect bounds, AreaShape shape, GestureEventHandler hand
2525
m_Handler = handler;
2626
m_Bounds = bounds;
2727
m_Shape = shape;
28-
m_GestureFlags = GestureEvent.Flags.None;
2928
m_Count = 0;
3029
}
3130

@@ -76,7 +75,6 @@ public void FireEvent(in GestureEvent gestureEvent)
7675
//set => SetValue(key, value);
7776
protected Rect m_Bounds;
7877
protected AreaShape m_Shape;
79-
private GestureEvent.Flags m_GestureFlags;
8078
private GestureEventHandler m_Handler;
8179
protected readonly TouchState[] m_Touches;
8280
protected int m_Count;

Assets/Samples/RebindingUI/OnScreen/Editor.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
// This should be in an editor only assembly.
2+
3+
#if UNITY_EDITOR
4+
5+
using System;
6+
using UnityEditor;
7+
using UnityEngine.InputSystem.OnScreen;
8+
using UnityEngine.UI;
9+
10+
namespace UnityEngine.InputSystem.Samples.RebindUI
11+
{
12+
//[CustomEditor(typeof(OnScreenControlUI))]
13+
public class OnScreenControlEditor : UnityEditor.Editor
14+
{
15+
#region Unity Editor Menu Extensions
16+
17+
private const int Priority = 10;
18+
private const int RigPriority = 21; // Note: Diff > 10 inserts separator
19+
private const string PrimaryStickControlPath = "<Gamepad>/leftStick";
20+
private const string SecondaryStickControlPath = "<Gamepad>/rightStick";
21+
private const string PrimaryButtonControlPath = "<Gamepad>/buttonSouth";
22+
private const string SecondaryButtonControlPath = "<Gamepad>/buttonEast";
23+
private const string Menu = "GameObject/Input System/";
24+
25+
private enum UIIntegration
26+
{
27+
None = 0,
28+
UGUI = 1,
29+
UIElements = 2,
30+
}
31+
32+
[MenuItem(Menu + "On-Screen Button", false, Priority)]
33+
private static void CreateOnScreenButton(MenuCommand menuCommand)
34+
{
35+
FinalizeGameObject(CreateButton(PrimaryButtonControlPath, UIIntegration.None), menuCommand.context);
36+
}
37+
38+
[MenuItem(Menu + "On-Screen Button (UI)", false, Priority)]
39+
private static void CreateOnScreenButtonUI(MenuCommand menuCommand)
40+
{
41+
FinalizeGameObject(CreateButton(PrimaryButtonControlPath, UIIntegration.UGUI), menuCommand.context);
42+
}
43+
44+
[MenuItem(Menu + "On-Screen Stick", false, Priority)]
45+
private static void CreateOnScreenStick(MenuCommand menuCommand)
46+
{
47+
var go = CreateStick(PrimaryStickControlPath, UIIntegration.None);
48+
FinalizeGameObject(go, menuCommand.context);
49+
}
50+
51+
[MenuItem(Menu + "On-Screen Gamepad (1 Stick, 2 Buttons)", isValidateFunction: false, RigPriority)]
52+
private static void CreateOnScreenGamepad1Stick2Buttons(MenuCommand menuCommand)
53+
{
54+
var go = CreateGamepad1Stick2Buttons(UIIntegration.None);
55+
FinalizeGameObject(go: go, menuCommand.context);
56+
}
57+
58+
[MenuItem(Menu + "On-Screen Gamepad (2 Sticks, 2 Buttons)", isValidateFunction: false, RigPriority)]
59+
private static void CreateOnScreenGamepad2Sticks2Buttons(MenuCommand menuCommand)
60+
{
61+
var go = CreateGamepad2Stick2Buttons(UIIntegration.None);
62+
FinalizeGameObject(go, menuCommand.context);
63+
}
64+
65+
private static void FinalizeGameObject(GameObject go, Object parent)
66+
{
67+
// Ensure it gets parented correctly if a context object was selected
68+
GameObjectUtility.SetParentAndAlign(go, parent as GameObject);
69+
70+
// Register the creation in the undo system
71+
Undo.RegisterCreatedObjectUndo(go, $"Create {go.name}");
72+
73+
// Select the newly created object
74+
Selection.activeObject = go;
75+
}
76+
77+
private static GameObject CreateStick(string controlPath, UIIntegration uiIntegration, string name = "OnScreenStick")
78+
{
79+
// There is currently no difference between stick and button apart from path.
80+
return CreateButton(controlPath, uiIntegration, name);
81+
}
82+
83+
private static GameObject CreateButton(string controlPath, UIIntegration uiIntegration, string name = "OnScreenButton")
84+
{
85+
var go = new GameObject(name: name);
86+
87+
var control = go.AddComponent<CustomOnScreenControl>();
88+
control.curve = Curve.Linear;
89+
control.stickRadiusMillimeters = 7.2f;
90+
control.bounds = new Rect(0.0f, 0.5f, 0.5f, 0.0f);
91+
control.controlPath = controlPath;
92+
93+
switch (uiIntegration)
94+
{
95+
case UIIntegration.UGUI:
96+
{
97+
// When used with UGUI we want the on-screen control to be a canvas object
98+
go.AddComponent<RectTransform>();
99+
100+
var uiButtonGo = new GameObject("Button");
101+
var rectTransform = uiButtonGo.AddComponent<RectTransform>();
102+
var uiButton = uiButtonGo.AddComponent<RawImage>();
103+
uiButton.color = new Color(0.5f, 0.5f, 0.5f, 0.5f);
104+
uiButton.raycastTarget = false;
105+
uiButton.transform.parent = go.transform;
106+
107+
var controlUI = go.AddComponent<OnScreenControlUI>();
108+
controlUI.control = control;
109+
controlUI.bounds = rectTransform;
110+
}
111+
break;
112+
case UIIntegration.UIElements:
113+
throw new NotImplementedException("UIElements support not yet implemented");
114+
break;
115+
case UIIntegration.None:
116+
default:
117+
break;
118+
}
119+
120+
return go;
121+
}
122+
123+
private static GameObject CreateGamepad1Stick2Buttons(UIIntegration uiIntegration)
124+
{
125+
var gamepad = new GameObject(MakeName("Gamepad 1-Stick 2-Buttons", uiIntegration));
126+
127+
var stick = CreateStick(PrimaryStickControlPath, uiIntegration);
128+
stick.transform.SetParent(gamepad.transform);
129+
130+
var primaryButton = CreateButton(PrimaryButtonControlPath, uiIntegration, "PrimaryButton");
131+
primaryButton.transform.SetParent(gamepad.transform);
132+
133+
var secondaryButton = CreateButton(SecondaryButtonControlPath, uiIntegration, "SecondaryButton");
134+
secondaryButton.transform.SetParent(gamepad.transform);
135+
136+
return gamepad;
137+
}
138+
139+
private static GameObject CreateGamepad2Stick2Buttons(UIIntegration uiIntegration)
140+
{
141+
var gamepad = new GameObject(MakeName("Gamepad 2-Sticks 2-Buttons", uiIntegration));
142+
143+
var primaryStick = CreateStick(PrimaryStickControlPath, uiIntegration, "Primary Stick");
144+
primaryStick.transform.SetParent(gamepad.transform);
145+
146+
var secondaryStick = CreateStick(SecondaryStickControlPath, uiIntegration, "Secondary Stick");
147+
secondaryStick.transform.SetParent(gamepad.transform);
148+
149+
var primaryButton = CreateButton(PrimaryButtonControlPath, uiIntegration, "PrimaryButton");
150+
primaryButton.transform.SetParent(gamepad.transform);
151+
152+
var secondaryButton = CreateButton(SecondaryButtonControlPath, uiIntegration, "SecondaryButton");
153+
secondaryButton.transform.SetParent(gamepad.transform);
154+
155+
return gamepad;
156+
}
157+
158+
private static string MakeName(string name, UIIntegration uiIntegration)
159+
{
160+
switch (uiIntegration)
161+
{
162+
case UIIntegration.UGUI: return name + " (UI)";
163+
case UIIntegration.None: return name + " (UI Elements)";
164+
default: return name;
165+
}
166+
}
167+
168+
#endregion
169+
}
170+
}
171+
172+
#endif // UNITY_EDITOR

Assets/Samples/RebindingUI/OnScreen/Editor/OnScreenControlEditor.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)