Created
October 14, 2018 19:54
-
-
Save Frooxius/1ba9851574709d1124d7fd1e92363ef4 to your computer and use it in GitHub Desktop.
TouchToggle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace FrooxEngine | |
{ | |
[Category("Transform/Interaction")] | |
public class TouchToggle : Component, ITouchable | |
{ | |
public readonly Sync<bool> State; | |
public readonly Sync<TouchEventType> EventType; | |
public readonly Sync<EventState> ToggleEvent; | |
public readonly Sync<EventState> OnEvent; | |
public readonly Sync<EventState> OffEvent; | |
public readonly Sync<bool> AcceptPhysicalTouch; | |
public readonly Sync<bool> AcceptRemoteTouch; | |
public readonly Sync<VibratePreset> Vibrate; | |
protected override void OnAwake() | |
{ | |
ToggleEvent.Value = EventState.Begin; | |
EventType.Value = TouchEventType.Touch; | |
AcceptPhysicalTouch.Value = true; | |
AcceptRemoteTouch.Value = true; | |
Vibrate.Value = VibratePreset.Short; | |
} | |
public void OnTouch(TouchEventInfo eventInfo) | |
{ | |
switch(eventInfo.type) | |
{ | |
case TouchType.Physical: | |
if (!AcceptPhysicalTouch) | |
return; | |
break; | |
case TouchType.Remote: | |
if (!AcceptRemoteTouch) | |
return; | |
break; | |
default: | |
Debug.Warning("Unknown touch type: " + eventInfo.type); | |
return; | |
} | |
bool prevState = State.Value; | |
if (ToggleEvent.Value != EventState.None && eventInfo.GetEventState(EventType) == ToggleEvent.Value) | |
State.Value = !State.Value; | |
if (OnEvent.Value != EventState.None && eventInfo.GetEventState(EventType) == OnEvent.Value) | |
State.Value = true; | |
if (OffEvent.Value != EventState.None && eventInfo.GetEventState(EventType) == OffEvent.Value) | |
State.Value = false; | |
if(State.Value != prevState) | |
eventInfo.source?.Slot.TryVibrate(Vibrate); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment