Skip to content

Instantly share code, notes, and snippets.

@Frooxius
Created February 18, 2019 22:40
Show Gist options
  • Save Frooxius/fd194483035f6d4d614f837440205d44 to your computer and use it in GitHub Desktop.
Save Frooxius/fd194483035f6d4d614f837440205d44 to your computer and use it in GitHub Desktop.
Touch Value Option
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FrooxEngine
{
[GenericTypes(GenericTypes.Group.NeosPrimitives)]
[Category("Transform/Interaction")]
public class TouchValueOption<T> : Component, ITouchable
{
public readonly RelayRef<IField<T>> Target;
public readonly Sync<T> Value;
public readonly FieldDrive<bool> ActiveIndicator;
public readonly FieldDrive<bool> HoverIndicator;
public readonly Sync<VibratePreset> HoverVibrate;
public readonly Sync<VibratePreset> Vibrate;
public readonly Sync<bool> SetOnTouchBegin;
public readonly Sync<bool> SetOnTouchStay;
public readonly Sync<bool> SetOnTouchEnd;
public readonly Sync<bool> AcceptOutOfSightTouch;
public readonly Sync<bool> AcceptPhysicalTouch;
public readonly Sync<bool> AcceptRemoteTouch;
protected override void OnAwake()
{
Vibrate.Value = VibratePreset.Medium;
HoverVibrate.Value = VibratePreset.Short;
SetOnTouchBegin.Value = true;
AcceptPhysicalTouch.Value = true;
AcceptRemoteTouch.Value = true;
}
public bool CanTouchOutOfSight => AcceptOutOfSightTouch;
public bool CanTouchInteract(Component touchSource) => true;
public void OnTouch(TouchEventInfo eventInfo)
{
if (Target.Target == null)
return;
if (Coder<T>.Equals(Target.Target.Value, Value.Value))
return;
if (eventInfo.type == TouchType.Physical && !AcceptPhysicalTouch)
return;
if (eventInfo.type == TouchType.Remote && !AcceptRemoteTouch)
return;
if (eventInfo.hover == EventState.Begin)
eventInfo.source?.Slot.TryVibrate(HoverVibrate);
if (HoverIndicator.Target != null)
{
if (eventInfo.hover == EventState.Begin || eventInfo.hover == EventState.Stay)
HoverIndicator.Target.Value = true;
else if (eventInfo.hover == EventState.End)
HoverIndicator.Target.Value = false;
}
bool set = false;
if(SetOnTouchBegin && eventInfo.touch == EventState.Begin)
{
Target.Target.Value = Value;
set = true;
}
if (SetOnTouchStay && eventInfo.touch == EventState.Stay)
{
Target.Target.Value = Value;
set = true;
}
if (SetOnTouchEnd && eventInfo.touch == EventState.End)
{
Target.Target.Value = Value;
set = true;
}
if (set)
eventInfo.source?.Slot.TryVibrate(Vibrate);
}
protected override void OnChanges()
{
if (ActiveIndicator.IsLinkValid)
ActiveIndicator.Target.Value = Target.Target != null &&
Coder<T>.Equals(Target.Target.Value, Value.Value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment