Skip to content

Commit

Permalink
Merge branch 'improvement/tablet-ui' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
BeauchesneFieldDay committed Dec 6, 2024
2 parents 596d187 + c7fc9f5 commit b911168
Show file tree
Hide file tree
Showing 84 changed files with 8,184 additions and 1,307 deletions.
6 changes: 4 additions & 2 deletions Assets/Code/Scripting/ScriptSkipping.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BeauUtil;
using FieldDay;
using FieldDay.Audio;
using FieldDay.Debugging;
using FieldDay.HID.XR;
using FieldDay.Scripting;
Expand All @@ -11,14 +12,15 @@ static public class ScriptSkipping {
[InvokeOnBoot]
static private void Initialize() {
GameLoop.OnDebugUpdate.Register(() => {
if (DebugInput.IsPressed(XRHandIndex.Right, XRHandButtons.PrimaryAxisClick) || DebugInput.IsPressed(UnityEngine.KeyCode.K)) {
if (DebugInput.ConsumePress(XRHandIndex.Right, XRHandButtons.PrimaryAxisClick) || DebugInput.IsPressed(UnityEngine.KeyCode.K)) {
ScriptUtility.ForEachThread((t) => {
t.SkipSingle();
});
}
if (DebugInput.IsPressed(XRHandIndex.Left, XRHandButtons.PrimaryAxisClick) || DebugInput.IsPressed(UnityEngine.KeyCode.L)) {
if (DebugInput.ConsumePress(XRHandIndex.Left, XRHandButtons.PrimaryAxisClick) || DebugInput.IsPressed(UnityEngine.KeyCode.L)) {
UniverseUtility.LoadNextDay();
ScriptUtility.KillAllThreads();
Sfx.StopAll();
}
});
}
Expand Down
8 changes: 7 additions & 1 deletion Assets/Code/Tablet/Count/TabletCountable.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using BeauRoutine;
using BeauUtil;
using FieldDay;
Expand All @@ -10,11 +11,15 @@ namespace Pennycook.Tablet {
public sealed class TabletCountable : BatchedComponent {
public TabletCountingGroup Group;


[NonSerialized] public bool IsCounted;
}

static public class TabletCountUtility {
static public bool IsCountable(TabletCountable countable) {
if (countable.IsCounted) {
return false;
}

var group = countable.Group;
if (group.State == TabletCountingGroupState.Inactive || group.State == TabletCountingGroupState.Completed) {
return false;
Expand All @@ -33,6 +38,7 @@ static public bool TryCount(TabletHighlightable highlightable, TabletCountable c
});*/

countable.Group.CurrentlyCounted.Add(countable);
countable.IsCounted = true;

//bool identified = Ref.Replace(ref highlightable.Identified, true);
//if (identified) {
Expand Down
12 changes: 12 additions & 0 deletions Assets/Code/Tablet/Count/TabletCountingMarkerRenderState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using FieldDay.Animation;
using FieldDay.SharedState;
using UnityEngine;

namespace Pennycook.Tablet {
public sealed class TabletCountingMarkerRenderState : SharedStateComponent {
public Camera RenderCamera;
public Mesh RenderMesh;
public Material RenderMaterial;
public float RenderScale;
}
}
11 changes: 11 additions & 0 deletions Assets/Code/Tablet/Count/TabletCountingMarkerRenderState.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions Assets/Code/Tablet/Count/TabletCountingMarkerRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using BeauUtil;
using FieldDay;
using FieldDay.Animation;
using FieldDay.Rendering;
using FieldDay.SharedState;
using FieldDay.Systems;
using UnityEngine;
using UnityEngine.Rendering;

namespace Pennycook.Tablet {
[SysUpdate(GameLoopPhase.UnscaledLateUpdate, 20000)]
public sealed class TabletCountingMarkerRenderer : SharedStateSystemBehaviour<TabletCountingMarkerRenderState, TabletToolState, TabletControlState> {
public override bool HasWork() {
return base.HasWork()
&& m_StateA.RenderCamera.isActiveAndEnabled
&& m_StateB.CurrentTool == TabletTool.Count;
}

public unsafe override void ProcessWork(float deltaTime) {
RenderParams renderParms = new RenderParams(m_StateA.RenderMaterial);
renderParms.camera = m_StateA.RenderCamera;
renderParms.layer = LayerMasks.Default_Index;
renderParms.renderingLayerMask = GraphicsSettings.defaultRenderingLayerMask;

DefaultInstancedMeshParams* instanceData = stackalloc DefaultInstancedMeshParams[64];
InstancedMeshBuffer<DefaultInstancedMeshParams> instanceBuff = new InstancedMeshBuffer<DefaultInstancedMeshParams>(instanceData, 64, renderParms, m_StateA.RenderMesh);

Matrix4x4 billboardMat, scaledBillboardMat;
CameraUtility.GetBillboardingMatrix(m_StateA.RenderCamera, out billboardMat);
scaledBillboardMat = billboardMat * Matrix4x4.Scale(Vector3.one * m_StateA.RenderScale);

DefaultInstancedMeshParams instParms = default;

foreach (var c in Find.Components<TabletCountable>()) {
if (!c.IsCounted) {
continue;
}

if (c.Group.State != TabletCountingGroupState.InProgress) {
continue;
}

Vector3 worldPos = c.transform.position;
worldPos.y += 2.5f;

// TODO: scale this

Geom.SetTranslation(ref scaledBillboardMat, worldPos);
instParms.objectToWorld = scaledBillboardMat;
instanceBuff.Queue(ref instParms);
}

instanceBuff.Flush();
instanceBuff.Dispose();
}
}
}
11 changes: 11 additions & 0 deletions Assets/Code/Tablet/Count/TabletCountingMarkerRenderer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Assets/Code/Tablet/Count/TabletCountingVisuals.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using FieldDay.Animation;
using UnityEngine;

namespace Pennycook.Tablet {
public sealed class TabletCountingVisuals : MonoBehaviour {
public RectTransform RotatingRing;

private void LateUpdate() {
RotatingRing.Rotate(0, 0, 2f * Time.deltaTime);
}
}
}
11 changes: 11 additions & 0 deletions Assets/Code/Tablet/Count/TabletCountingVisuals.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 34 additions & 6 deletions Assets/Code/Tablet/Highlight/TabletHighlightState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ public class TabletHighlightState : SharedStateComponent, IRegistrationCallbacks
public RectTransform HighlightBox;
public CanvasGroup HighlightBoxGroup;

[Header("Raycast Configuration")]
public float RaycastSize = 0.4f;
public float RaycastMinDistance = 1;
[Header("Additional Elements")]
public GameObject WarpGroup;

[NonSerialized] public Transform CachedLookCameraTransform;
[NonSerialized] public Vector2 CachedHighlightCornerScale;
Expand Down Expand Up @@ -72,7 +71,7 @@ static public partial class TabletUtility {

[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
[Il2CppSetOption(Option.NullChecks, false)]
static public unsafe Rect CalculateViewportAlignedBoundingBox(Bounds bounds, Camera referenceCamera, Vector2 scale) {
static public unsafe Rect CalculateViewportAlignedClampedBoundingBox(Bounds bounds, Camera referenceCamera, Vector2 scale) {
Vector3* corners = stackalloc Vector3[8];
Vector3 min = bounds.min, max = bounds.max;
corners[0] = min;
Expand All @@ -98,8 +97,37 @@ static public unsafe Rect CalculateViewportAlignedBoundingBox(Bounds bounds, Cam
r.height *= scale.y;
return r;
}

static private Vector2 ClampTo01Space(Vector3 input) {

[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
[Il2CppSetOption(Option.NullChecks, false)]
static public unsafe Rect CalculateViewportAlignedBoundingBox(Bounds bounds, Camera referenceCamera, Vector2 scale) {
Vector3* corners = stackalloc Vector3[8];
Vector3 min = bounds.min, max = bounds.max;
corners[0] = min;
corners[1] = new Vector3(min.x, min.y, max.z);
corners[2] = new Vector3(min.x, max.y, min.z);
corners[3] = new Vector3(min.x, max.y, max.z);
corners[4] = new Vector3(max.x, min.y, min.z);
corners[5] = new Vector3(max.x, min.y, max.z);
corners[6] = new Vector3(max.x, max.y, min.z);
corners[7] = max;

//DebugDraw.AddBounds(bounds, Color.blue.WithAlpha(0.2f), 0.2f, 0.1f);

Vector2* viewCorners = stackalloc Vector2[8];
for (int i = 0; i < 8; i++) {
viewCorners[i] = referenceCamera.WorldToViewportPoint(corners[i], Camera.MonoOrStereoscopicEye.Mono);
}

Rect r = Geom.MinRect(new UnsafeSpan<Vector2>(viewCorners, 8));
r.x *= scale.x;
r.y *= scale.y;
r.width *= scale.x;
r.height *= scale.y;
return r;
}

static private Vector2 ClampTo01Space(Vector3 input) {
Vector2 output;
output.x = Mathf.Clamp01(input.x);
output.y = Mathf.Clamp01(input.y);
Expand Down
67 changes: 40 additions & 27 deletions Assets/Code/Tablet/Highlight/TabletHighlightSystem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections;
using BeauRoutine;
using BeauUtil;
using BeauUtil.Debugger;
using FieldDay;
using FieldDay.Systems;
using UnityEngine;
Expand All @@ -11,10 +12,11 @@ public class TabletHighlightSystem : SharedStateSystemBehaviour<TabletHighlightS
public override void ProcessWork(float deltaTime) {
bool isGripping = !m_StateC.GrippedHandMask.IsEmpty;
LayerMask searchMask = m_StateB.CurrentToolDef.RaycastMask;
bool allowVisualHighlights = (m_StateB.CurrentToolDef.Flags & TabletToolFlags.DoNotSetHighlight) == 0;

if (!ReferenceEquals(m_StateA.HighlightedObject, null)) {
if (!m_StateA.HighlightedObject || !m_StateA.HighlightedObject.isActiveAndEnabled || !isGripping || m_StateB.CurrentTool == TabletTool.None || searchMask == 0) {
ClearSelection(m_StateA, m_StateB, m_StateC, m_StateB.CurrentTool == TabletTool.Count);
ClearSelection(m_StateA, m_StateB, m_StateC);
return;
}
}
Expand All @@ -23,11 +25,13 @@ public override void ProcessWork(float deltaTime) {
if (Frame.Interval(3) && isGripping && m_StateB.CurrentTool != TabletTool.None && !m_StateA.RaycastJob.IsValid()) {
if (searchMask != 0) {
TabletZoomState zoomState = Find.State<TabletZoomState>();
float coneRadius = zoomState.ZoomMultiplier;
float coneDistance = 25 * zoomState.ZoomMultiplier;
float coneRadius = coneDistance * m_StateB.CurrentToolDef.RaycastUnitConeRadius * CameraHelper.UnitHeightForFOV(m_StateA.LookCamera.fieldOfView) / 2;

Log.Trace("cone radius = {0}", coneRadius);

m_StateA.CachedLookCameraTransform.GetPositionAndRotation(out Vector3 cameraPos, out Quaternion cameraRot);
m_StateA.RaycastJob = RaycastJobs.SmoothConeCast(cameraPos, Geom.Forward(cameraRot), coneRadius, coneDistance, 5, searchMask);
m_StateA.RaycastJob = RaycastJobs.SmoothConeCast(cameraPos, Geom.Forward(cameraRot), coneRadius, coneDistance, Mathf.CeilToInt(coneRadius / 2), searchMask);
RaycastJobs.Kick(ref m_StateA.RaycastJob);
}
}
Expand All @@ -43,13 +47,13 @@ public override void ProcessWork(float deltaTime) {

if (!scannable) {
if (m_StateA.HighlightedObject != null) {
ClearSelection(m_StateA, m_StateB, m_StateC, m_StateB.CurrentTool == TabletTool.Count);
ClearSelection(m_StateA, m_StateB, m_StateC);
}
} else {
Rect viewportRect = TabletUtility.CalculateViewportAlignedBoundingBox(scannable.HighlightCollider.bounds, m_StateA.LookCamera, m_StateA.CachedHighlightCornerScale);
Rect viewportRect = TabletUtility.CalculateViewportAlignedClampedBoundingBox(scannable.HighlightCollider.bounds, m_StateA.LookCamera, m_StateA.CachedHighlightCornerScale);

if (m_StateA.HighlightedObject != scannable) {
SetSelection(m_StateA, m_StateB, m_StateC, scannable, viewportRect, m_StateB.CurrentTool == TabletTool.Count);
SetSelection(m_StateA, m_StateB, m_StateC, scannable, viewportRect, allowVisualHighlights);

float vibAmp = Mathf.Clamp(1 - hit.distance / 60, 0.4f, 1) * 0.3f;
TabletUtility.PlayHaptics(vibAmp, 0.02f);
Expand All @@ -59,28 +63,32 @@ public override void ProcessWork(float deltaTime) {
}
}

if (m_StateA.HighlightedObject) {
Vector2 targetAnchor = m_StateA.TargetHighlightCorners.center;
Vector2 targetSize = m_StateA.TargetHighlightCorners.size;
if (m_StateA.IsBoxVisible && m_StateA.HighlightedObject) {
if (!allowVisualHighlights) {
m_StateA.BoxTransitionRoutine.Replace(m_StateA, ScaleBoxDown(m_StateA));
} else {
Vector2 targetAnchor = m_StateA.TargetHighlightCorners.center;
Vector2 targetSize = m_StateA.TargetHighlightCorners.size;

Vector2 anchor = m_StateA.HighlightBox.anchoredPosition;
Vector2 size = m_StateA.HighlightBox.sizeDelta;
Vector2 anchor = m_StateA.HighlightBox.anchoredPosition;
Vector2 size = m_StateA.HighlightBox.sizeDelta;

float lerpAmt = TweenUtil.Lerp(2, deltaTime);
float lerpAmt = TweenUtil.Lerp(2, deltaTime);

if (!Mathf.Approximately(anchor.x, targetAnchor.x) || !Mathf.Approximately(anchor.y, targetAnchor.y)) {
anchor = Vector2.Lerp(anchor, targetAnchor, lerpAmt);
m_StateA.HighlightBox.anchoredPosition = anchor;
}
if (!Mathf.Approximately(anchor.x, targetAnchor.x) || !Mathf.Approximately(anchor.y, targetAnchor.y)) {
anchor = Vector2.Lerp(anchor, targetAnchor, lerpAmt);
m_StateA.HighlightBox.anchoredPosition = anchor;
}

if (!Mathf.Approximately(size.x, targetSize.x) || !Mathf.Approximately(size.y, targetSize.y)) {
size = Vector2.Lerp(size, targetSize, lerpAmt);
m_StateA.HighlightBox.sizeDelta = size;
if (!Mathf.Approximately(size.x, targetSize.x) || !Mathf.Approximately(size.y, targetSize.y)) {
size = Vector2.Lerp(size, targetSize, lerpAmt);
m_StateA.HighlightBox.sizeDelta = size;
}
}
}
}

static private void SetSelection(TabletHighlightState highlight, TabletToolState toolState, TabletControlState ctrl, TabletHighlightable scannable, Rect rect, bool isCounting=false) {
static private void SetSelection(TabletHighlightState highlight, TabletToolState toolState, TabletControlState ctrl, TabletHighlightable scannable, Rect rect, bool visualHighlight = true) {
bool wasNotSelected = !highlight.HighlightedObject;

if (!wasNotSelected) {
Expand All @@ -92,18 +100,23 @@ static private void SetSelection(TabletHighlightState highlight, TabletToolState

highlight.HighlightedObject = scannable;
highlight.TargetHighlightCorners = rect;
if (!highlight.IsBoxVisible) {
highlight.HighlightBox.sizeDelta = default;
highlight.HighlightBox.anchoredPosition = rect.center;
highlight.BoxTransitionRoutine.Replace(highlight, FadeBoxIn(highlight));
} else if (wasNotSelected) {
highlight.BoxTransitionRoutine.Replace(highlight, FadeBoxIn(highlight));

if (visualHighlight) {
if (!highlight.IsBoxVisible) {
highlight.HighlightBox.sizeDelta = default;
highlight.HighlightBox.anchoredPosition = rect.center;
highlight.BoxTransitionRoutine.Replace(highlight, FadeBoxIn(highlight));
} else if (wasNotSelected) {
highlight.BoxTransitionRoutine.Replace(highlight, FadeBoxIn(highlight));
}
} else if (highlight.IsBoxVisible) {
highlight.BoxTransitionRoutine.Replace(highlight, ScaleBoxDown(highlight));
}

toolState.CurrentToolDef.OnHighlighted?.Invoke(highlight.HighlightedObject, ctrl);
}

static private void ClearSelection(TabletHighlightState highlight, TabletToolState toolState, TabletControlState ctrl, bool isCounting=false) {
static private void ClearSelection(TabletHighlightState highlight, TabletToolState toolState, TabletControlState ctrl) {
VRGame.Events.Queue(GameEvents.ObjectUnhighlighted, EvtArgs.Ref(highlight.HighlightedObject));
toolState.CurrentToolDef.OnUnhighlighted?.Invoke(highlight.HighlightedObject, ctrl);
highlight.HighlightedObject = null;
Expand Down
15 changes: 11 additions & 4 deletions Assets/Code/Tablet/Interact/TabletInteractionSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public override void ProcessWork(float deltaTime) {
case TabletToolInteractionMode.Hold:
isPressing = TabletUtility.IsButtonHeld(XRHandButtons.TriggerButton);
break;
case TabletToolInteractionMode.Always:
isPressing = true;
break;
}

if (isPressing) {
Expand Down Expand Up @@ -56,10 +59,14 @@ private TabletInteractionState.State UpdateState(double ts) {
}

case TabletInteractionState.State.Available: {
string verb = m_StateC.CurrentToolDef.GetVerb?.Invoke(m_StateB.HighlightedObject, desiredState) ?? m_StateC.CurrentToolDef.DefaultVerb;
m_StateA.InteractionLabel.SetText(verb);
if (!m_StateA.DetailsGroup.IsShowing()) {
m_StateA.InteractionGroup.Show();
if ((m_StateC.CurrentToolDef.Flags & TabletToolFlags.NoPrompt) != 0) {
m_StateA.InteractionGroup.Hide();
} else {
string verb = m_StateC.CurrentToolDef.GetVerb?.Invoke(m_StateB.HighlightedObject, desiredState) ?? m_StateC.CurrentToolDef.DefaultVerb;
m_StateA.InteractionLabel.SetText(verb);
if (!m_StateA.DetailsGroup.IsShowing()) {
m_StateA.InteractionGroup.Show();
}
}
break;
}
Expand Down
Loading

0 comments on commit b911168

Please sign in to comment.