Skip to content

Commit

Permalink
Adding settings and UI for customizing the dead zon on OSX.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen committed Feb 27, 2016
1 parent 5cd00ea commit 11ae957
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ public static bool LockRotationAll {
public float RotSensMin = RotSensMinDefault;
public float RotSensMax = RotSensMaxDefault;

#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
public const float RotDeadDefault = 30, RotDeadMinDefault = 0, RotDeadMaxDefault = 100f;
public float RotDead = RotDeadDefault;
public float RotDeadMin = RotDeadMinDefault;
public float RotDeadMax = RotDeadMaxDefault;

public const float TransDeadDefault = 30, TransDeadMinDefault = 0, TransDeadMaxDefault = 100f;
public float TransDead = TransDeadDefault;
public float TransDeadMin = TransDeadMinDefault;
public float TransDeadMax = TransDeadMaxDefault;
#endif

// Setting storage keys
private const string TransSensKey = "Translation sensitivity";
private const string TransSensMinKey = "Translation sensitivity minimum";
Expand All @@ -114,6 +126,16 @@ public static bool LockRotationAll {
private const string LockRotationXKey = "Rotation lock X";
private const string LockRotationYKey = "Rotation lock Y";
private const string LockRotationZKey = "Rotation lock Z";

#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
private const string TransDeadKey = "Translation dead zone size";
private const string TransDeadMinKey = "Translation dead zone size minimum";
private const string TransDeadMaxKey = "Translation dead zone size maximum";

private const string RotDeadKey = "Rotation dead zone size";
private const string RotDeadMinKey = "Rotation dead zone size minimum";
private const string RotDeadMaxKey = "Rotation dead zone size maximum";
#endif
#region - Singleton -
public static SpaceNavigator Instance {
get {
Expand Down Expand Up @@ -213,6 +235,41 @@ public virtual void OnGUI() {

GUILayout.EndHorizontal();
#endregion - Sensitivity + gearbox -

#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
#region - Dead Zone -
GUILayout.BeginVertical();
GUILayout.Label("Dead Zone");
GUILayout.Space(4);


#region - Translation + rotation -
GUILayout.BeginVertical();
#region - Translation -
GUILayout.BeginHorizontal();
GUILayout.Label("Translation", GUILayout.Width(67));
TransDead = EditorGUILayout.FloatField(TransDead, GUILayout.Width(30));
TransDeadMin = EditorGUILayout.FloatField(TransDeadMin, GUILayout.Width(30));
TransDead = GUILayout.HorizontalSlider(TransDead, TransDeadMin, TransDeadMax);
TransDeadMax = EditorGUILayout.FloatField(TransDeadMax, GUILayout.Width(30));
GUILayout.EndHorizontal();
#endregion - Translation -

#region - Rotation -
GUILayout.BeginHorizontal();
GUILayout.Label("Rotation", GUILayout.Width(67));
RotDead = EditorGUILayout.FloatField(RotDead, GUILayout.Width(30));
RotDeadMin = EditorGUILayout.FloatField(RotDeadMin, GUILayout.Width(30));
RotDead = GUILayout.HorizontalSlider(RotDead, RotDeadMin, RotDeadMax);
RotDeadMax = EditorGUILayout.FloatField(RotDeadMax, GUILayout.Width(30));
GUILayout.EndHorizontal();
#endregion - Rotation -
GUILayout.EndVertical();
#endregion - Translation + rotation -

GUILayout.EndVertical();
#endregion - Deadzone -

#endif
}

Expand All @@ -235,6 +292,16 @@ public void ReadSettings() {
RotSensMin = PlayerPrefs.GetFloat(RotSensMinKey, RotSensMinDefault);
RotSensMax = PlayerPrefs.GetFloat(RotSensMaxKey, RotSensMaxDefault);

#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
RotDead = PlayerPrefs.GetFloat(RotDeadKey, RotDeadDefault);
RotDeadMin = PlayerPrefs.GetFloat(RotDeadMinKey, RotDeadMinDefault);
RotDeadMax = PlayerPrefs.GetFloat(RotDeadMaxKey, RotDeadMaxDefault);

TransDead = PlayerPrefs.GetFloat(TransDeadKey, TransDeadDefault);
TransDeadMin = PlayerPrefs.GetFloat(TransDeadMinKey, TransDeadMinDefault);
TransDeadMax = PlayerPrefs.GetFloat(TransDeadMaxKey, TransDeadMaxDefault);
#endif

_lockRotationAll = PlayerPrefs.GetInt(LockRotationAllKey, 0) == 1;
_lockRotationX = PlayerPrefs.GetInt(LockRotationXKey, 0) == 1;
_lockRotationY = PlayerPrefs.GetInt(LockRotationYKey, 0) == 1;
Expand All @@ -258,6 +325,16 @@ public void WriteSettings() {
PlayerPrefs.SetFloat(RotSensMinKey, RotSensMin);
PlayerPrefs.SetFloat(RotSensMaxKey, RotSensMax);

#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
PlayerPrefs.SetFloat(RotDeadKey, RotDead);
PlayerPrefs.SetFloat(RotDeadMinKey, RotDeadMin);
PlayerPrefs.SetFloat(RotDeadMaxKey, RotDeadMax);

PlayerPrefs.SetFloat(TransDeadKey, TransDead);
PlayerPrefs.SetFloat(TransDeadMinKey, TransDeadMin);
PlayerPrefs.SetFloat(TransDeadMaxKey, TransDeadMax);
#endif

PlayerPrefs.SetInt(LockRotationAllKey, _lockRotationAll ? 1 : 0);
PlayerPrefs.SetInt(LockRotationXKey, _lockRotationX ? 1 : 0);
PlayerPrefs.SetInt(LockRotationYKey, _lockRotationY ? 1 : 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public override Vector3 GetTranslation() {
SampleTranslation(ref x, ref y, ref z);
float sensitivity = Application.isPlaying ? PlayTransSens : TransSens[CurrentGear];

return (
_clientID == 0 ?
return (
_clientID == 0 ?
Vector3.zero :
new Vector3(
LockTranslationX || LockTranslationAll ? 0 : SubtractDeadzone(x, TranslationDeadzone),
LockTranslationY || LockTranslationAll ? 0 : SubtractDeadzone(-z, TranslationDeadzone),
LockTranslationZ || LockTranslationAll ? 0 : SubtractDeadzone(-y, TranslationDeadzone)) * sensitivity * TransSensScale);
new Vector3 (
LockTranslationX || LockTranslationAll ? 0 : SubtractDeadzone (x, TransDead),
LockTranslationY || LockTranslationAll ? 0 : SubtractDeadzone (-z, TransDead),
LockTranslationZ || LockTranslationAll ? 0 : SubtractDeadzone (-y, TransDead)) * sensitivity * TransSensScale);
}
public override Quaternion GetRotation() {
int rx = 0, ry = 0, rz = 0;
Expand All @@ -49,16 +49,16 @@ public override Quaternion GetRotation() {
Quaternion.identity :
Quaternion.Euler(
new Vector3(
LockRotationX || LockRotationAll ? 0 : SubtractDeadzone(-rx, RotationDeadzone),
LockRotationY || LockRotationAll ? 0 : SubtractDeadzone(rz, RotationDeadzone),
LockRotationZ || LockRotationAll ? 0 : SubtractDeadzone(ry, RotationDeadzone)) * sensitivity * RotSensScale));
LockRotationX || LockRotationAll ? 0 : SubtractDeadzone(-rx, RotDead),
LockRotationY || LockRotationAll ? 0 : SubtractDeadzone(rz, RotDead),
LockRotationZ || LockRotationAll ? 0 : SubtractDeadzone(ry, RotDead)) * sensitivity * RotSensScale));
}

private float SubtractDeadzone(int value, int deadzone)
private float SubtractDeadzone(int value, float deadzone)
{
int signMultiplier = value < 0 ? -1 : 1;
int absoluteValue = Mathf.Abs(value);
return Mathf.Max(absoluteValue - Mathf.Abs(deadzone), 0) * signMultiplier;
return value < 0
? Math.Min(0, value + Math.Abs(deadzone))
: Math.Max(0, value - Math.Abs(deadzone));
}

#region - Singleton -
Expand Down

0 comments on commit 11ae957

Please sign in to comment.