Skip to content

Instantly share code, notes, and snippets.

@Frooxius
Created October 14, 2018 19:54
Show Gist options
  • Save Frooxius/1ba9851574709d1124d7fd1e92363ef4 to your computer and use it in GitHub Desktop.
Save Frooxius/1ba9851574709d1124d7fd1e92363ef4 to your computer and use it in GitHub Desktop.
TouchToggle
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