Tanoda
InteractionToggle.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using UnityEngine;
12using UnityEngine.Serialization;
13
14namespace Leap.Unity.Interaction {
15
20
21 [Tooltip("The height that this button rests at; this value is a lerp in between the min and max height.")]
22 [Range(0f, 1f)]
24 public float toggledRestingHeight = 0.25f;
25
26 [SerializeField, FormerlySerializedAs("_toggled")]
27 private bool _isToggled = false;
28
29 [SerializeField]
30 private bool _startToggled = false;
31
33 public bool isToggled {
34 get {
35 return _isToggled;
36 }
37 set {
38 if (_isToggled != value) {
39 _isToggled = value;
40 if (_isToggled) {
41 OnToggle();
42 } else {
43 OnUntoggle();
44 }
45 restingHeight = isToggled ? toggledRestingHeight : _originalRestingHeight;
46 rigidbody.WakeUp();
47 _pressedThisFrame = value;
48 _unpressedThisFrame = !value;
49 }
50 }
51 }
52
54 [FormerlySerializedAs("toggleEvent")]
55 [SerializeField]
56 private UnityEvent _toggleEvent = new UnityEvent();
57
61 public Action OnToggle = () => { };
62
64 [FormerlySerializedAs("unToggleEvent")]
65 [SerializeField]
66 public UnityEvent _untoggleEvent = new UnityEvent();
67
71 public Action OnUntoggle = () => { };
72
73 private float _originalRestingHeight;
75 get {
76 return _originalRestingHeight;
77 }
78 }
79
84 get {
85 return initialLocalPosition + Vector3.back * Mathf.Lerp(minMaxHeight.x, minMaxHeight.y, toggledRestingHeight);
86 }
87 }
88
92 public override Vector3 RelaxedLocalPosition {
93 get {
94 if (!Application.isPlaying) {
95 return initialLocalPosition + Vector3.back * Mathf.Lerp(minMaxHeight.x, minMaxHeight.y, restingHeight);
96 }
97 else {
98 return initialLocalPosition + Vector3.back * Mathf.Lerp(minMaxHeight.x, minMaxHeight.y, untoggledRestingHeight);
99 }
100 }
101 }
102
103 protected override void Awake() {
104 base.Awake();
105
106 _originalRestingHeight = restingHeight;
107 }
108
109 protected override void Start() {
110 base.Start();
111
112 if (_startToggled) {
113 isToggled = true;
114 }
115
116 OnToggle += _toggleEvent.Invoke;
117 OnUntoggle += _untoggleEvent.Invoke;
118 }
119
120 protected override void OnEnable() {
121 OnPress += OnPressed;
122 base.OnEnable();
123 }
124
125 protected override void OnDisable() {
126 base.OnDisable();
127 OnPress -= OnPressed;
128 }
129
130 private void OnPressed() {
132 }
133
138 public void Toggle() {
139 isToggled = true;
140 }
141
145 public void Untoggle() {
146 isToggled = false;
147 }
148 }
149}
Rigidbody rigidbody
The Rigidbody associated with this interaction object.
A physics-enabled button. Activated by physically pressing the button, with events for press and unpr...
Vector2 minMaxHeight
The minimum and maximum heights the button can exist at.
Vector3 initialLocalPosition
The initial position of this element in local space, stored on Start().
float restingHeight
The height that this button rests at; this value is a lerp in between the min and max height.
A physics-enabled toggle. Toggling is triggered by physically pushing the toggle to its compressed po...
Vector3 RelaxedToggledLocalPosition
Returns the local position of this toggle when it is able to relax into its untoggled position.
Action OnUntoggle
Called when the toggle is unticked.
Action OnToggle
Called when the toggle is ticked (not when unticked; for that, use OnUntoggle.)
float toggledRestingHeight
The height that this toggle rests at when it is toggled.
override Vector3 RelaxedLocalPosition
Returns the local position of this toggle when it is able to relax into its untoggled position.
bool isToggled
summary> Triggered when this toggle is toggled.
void Untoggle()
Sets this InteractionToggle to the "untoggled" state.
void Toggle()
Sets this InteractionToggle to the "toggled" state. Calling this function won't oscillate the state o...