Skip to content

Commit

Permalink
Slider fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
andybak committed Feb 25, 2025
1 parent 8bfd27e commit ee46008
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 124 deletions.
29 changes: 27 additions & 2 deletions Assets/Prefabs/ModelFilterPanel.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -2811,11 +2811,36 @@ PrefabInstance:
serializedVersion: 3
m_TransformParent: {fileID: 7949933859663638067}
m_Modifications:
- target: {fileID: 1066218673489108619, guid: da0b519cb21425a48b46ae7057256fb1,
type: 3}
propertyPath: m_Step
value: 1
objectReference: {fileID: 0}
- target: {fileID: 1066218673489108619, guid: da0b519cb21425a48b46ae7057256fb1,
type: 3}
propertyPath: hoverName
value: Max number of faces
objectReference: {fileID: 0}
- target: {fileID: 1066218673489108619, guid: da0b519cb21425a48b46ae7057256fb1,
type: 3}
propertyPath: m_Maximum
value: 10000
objectReference: {fileID: 0}
- target: {fileID: 1066218673489108619, guid: da0b519cb21425a48b46ae7057256fb1,
type: 3}
propertyPath: m_Minimum
value: 100
objectReference: {fileID: 0}
- target: {fileID: 1102641234472584921, guid: da0b519cb21425a48b46ae7057256fb1,
type: 3}
propertyPath: m_Name
value: Complexity Slider
objectReference: {fileID: 0}
- target: {fileID: 3281331942210031505, guid: da0b519cb21425a48b46ae7057256fb1,
type: 3}
propertyPath: m_LocalScale.z
value: 0.03
objectReference: {fileID: 0}
- target: {fileID: 5501061831439391173, guid: da0b519cb21425a48b46ae7057256fb1,
type: 3}
propertyPath: m_text
Expand All @@ -2829,7 +2854,7 @@ PrefabInstance:
- target: {fileID: 8386300346263520932, guid: da0b519cb21425a48b46ae7057256fb1,
type: 3}
propertyPath: m_LocalPosition.x
value: 0.004600067
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8386300346263520932, guid: da0b519cb21425a48b46ae7057256fb1,
type: 3}
Expand All @@ -2839,7 +2864,7 @@ PrefabInstance:
- target: {fileID: 8386300346263520932, guid: da0b519cb21425a48b46ae7057256fb1,
type: 3}
propertyPath: m_LocalPosition.z
value: -0.12890083
value: -0.123
objectReference: {fileID: 0}
- target: {fileID: 8386300346263520932, guid: da0b519cb21425a48b46ae7057256fb1,
type: 3}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/model/controller/FilterPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void HandleOk()
{
var category = m_CategoryContainer.Value;
var orderBy = m_OrderByContainer.Value;
var triangleCount = (int) m_TriangleCountSlider.m_Value;
var triangleCount = (int) m_TriangleCountSlider.Value;
PeltzerMain.Instance.GetPolyMenuMain().SetApiOrderBy(orderBy);
PeltzerMain.Instance.GetPolyMenuMain().SetApiCategoryFilter(category);
PeltzerMain.Instance.GetPolyMenuMain().SetApiTriangleCountMax(triangleCount);
Expand Down
262 changes: 141 additions & 121 deletions Assets/Scripts/model/controller/Slider.cs
Original file line number Diff line number Diff line change
@@ -1,122 +1,142 @@
// Copyright 2024 The Open Blocks Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using com.google.apps.peltzer.client.model.main;
using com.google.apps.peltzer.client.model.util;
using UnityEngine;
using UnityEngine.Events;

namespace com.google.apps.peltzer.client.model.controller
{
public class Slider : PolyMenuButton
{
private static readonly int SHADER_SLIDE_VALUE_PROP = Shader.PropertyToID("_SlideValue");
public UnityEvent<float> m_Action;
public MeshRenderer m_SliderRenderer;
public float m_Minimum = 0;
public float m_Maximum = 1;
public float m_Step = 0.1f;
public int m_UpdateInterval = 1;
public bool m_UpdateOnlyOnRelease;

[NonSerialized] public float m_Value;
[NonSerialized] public float m_NormalizedValue;

private bool m_IsDragging;
private Material m_SliderMaterial;
private float m_LastUpdateFrame;

public override void Start()
{
base.Start();
PeltzerMain.Instance.peltzerController.PeltzerControllerActionHandler += ControllerEventHandler;
m_SliderMaterial = m_SliderRenderer.material;
}

public override void Update()
{
if (m_IsDragging && !m_UpdateOnlyOnRelease)
{
if (Time.frameCount - m_LastUpdateFrame > m_UpdateInterval)
{
m_LastUpdateFrame = Time.frameCount;
m_Action.Invoke(m_Value);
}
}
}

private static float GetNormalizedLocalPosition(RaycastHit hit)
{
// Convert world hit point to local space of the hit object
Transform hitTransform = hit.transform;
Vector3 localHitPoint = hitTransform.InverseTransformPoint(hit.point);

Collider collider = hit.collider;
Bounds localBounds = collider.bounds;
Vector3 localMin = hitTransform.InverseTransformPoint(localBounds.min);
Vector3 localMax = hitTransform.InverseTransformPoint(localBounds.max);

// Calculate the normalized positions (0 to 1)
var normalized = Mathf.InverseLerp(localMin.x, localMax.x, localHitPoint.x);
// Fudge factor because my maths is off somewhere
float fudge = 0.1f;
normalized = Mathf.InverseLerp(0 + fudge, 1 - fudge, normalized);
return normalized;
}

public void SetHitPoint(RaycastHit hit)
{
if (m_IsDragging)
{
float normalizedLocalPosition = GetNormalizedLocalPosition(hit);
m_Value = Mathf.Lerp(m_Minimum, m_Maximum, normalizedLocalPosition);
m_Value = Mathf.Round(m_Value / m_Step) * m_Step;
m_NormalizedValue = Mathf.InverseLerp(m_Minimum, m_Maximum, m_Value);
m_SliderMaterial.SetFloat(SHADER_SLIDE_VALUE_PROP, m_NormalizedValue);
}
}

private void ControllerEventHandler(object sender, ControllerEventArgs args)
{
if (!ActionIsAllowed()) return;
if (args.ControllerType != ControllerType.PELTZER) return;
if (args.ButtonId != ButtonId.Trigger) return;
switch (args.Action)
{
case ButtonAction.DOWN:
m_IsDragging = true;
break;
case ButtonAction.UP:
m_IsDragging = false;
if (m_UpdateOnlyOnRelease)
{
m_Action.Invoke(m_Value);
}
break;
case ButtonAction.NONE:
return;
}
}

/// <summary>
/// Returns whether or not action is currently allowed.
/// </summary>
/// <returns>Whether or not action is allowed.</returns>
internal bool ActionIsAllowed()
{
return PeltzerMain.Instance.restrictionManager.menuActionsAllowed;
}
}
// Copyright 2024 The Open Blocks Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using com.google.apps.peltzer.client.model.main;
using UnityEngine;
using UnityEngine.Events;

namespace com.google.apps.peltzer.client.model.controller
{
public class Slider : PolyMenuButton
{
private static readonly int SHADER_SLIDE_VALUE_PROP = Shader.PropertyToID("_SlideValue");
public UnityEvent<float> m_Action;
public MeshRenderer m_SliderRenderer;
public float m_Minimum = 0;
public float m_Maximum = 1;
public float m_Step = 0.1f;
public int m_UpdateInterval = 1;
public bool m_UpdateOnlyOnRelease;

public float Value
{
get
{
float val = Mathf.Lerp(m_Minimum, m_Maximum, m_NormalizedValue);
val = Mathf.Round(val / m_Step) * m_Step;
hoverName = val.ToString();
return val;
}
}

[NonSerialized] private float m_NormalizedValue;

private bool m_IsDragging;
private Material m_SliderMaterial;
private float m_LastUpdateFrame;

public override void Start()
{
base.Start();
PeltzerMain.Instance.peltzerController.PeltzerControllerActionHandler += ControllerEventHandler;
m_SliderMaterial = m_SliderRenderer.material;
}

public override void Update()
{
if (m_IsDragging && !m_UpdateOnlyOnRelease)
{
if (Time.frameCount - m_LastUpdateFrame > m_UpdateInterval)
{
m_LastUpdateFrame = Time.frameCount;
m_Action.Invoke(Value);
}
}
}

private static float GetNormalizedLocalPosition(RaycastHit hit)
{
// Convert world hit point to local space of the hit object
Transform hitTransform = hit.transform;
Vector3 localHitPoint = hitTransform.InverseTransformPoint(hit.point);

Collider collider = hit.collider;
Bounds localBounds = collider.bounds;
Vector3 localMin = hitTransform.InverseTransformPoint(localBounds.min);
Vector3 localMax = hitTransform.InverseTransformPoint(localBounds.max);

float xPos = localHitPoint.x;

// Correct the direction of x
if (localMin.x < localMax.x)
{
xPos = -xPos;
}

// Calculate the normalized positions (0 to 1)
var normalized = Mathf.InverseLerp(localMin.x, localMax.x, xPos);



// Fudge factor because my maths is off somewhere
float fudge = 0.1f;
normalized = Mathf.InverseLerp(0 + fudge, 1 - fudge, normalized);
return normalized;
}

public void SetHitPoint(RaycastHit hit)
{
if (m_IsDragging)
{
float normalizedLocalPosition = GetNormalizedLocalPosition(hit);
float val = Mathf.Lerp(m_Minimum, m_Maximum, normalizedLocalPosition);
val = Mathf.Round(val / m_Step) * m_Step;
m_NormalizedValue = Mathf.InverseLerp(m_Minimum, m_Maximum, val);
m_SliderMaterial.SetFloat(SHADER_SLIDE_VALUE_PROP, m_NormalizedValue);
}
}

private void ControllerEventHandler(object sender, ControllerEventArgs args)
{
if (!ActionIsAllowed()) return;
if (args.ControllerType != ControllerType.PELTZER) return;
if (args.ButtonId != ButtonId.Trigger) return;
switch (args.Action)
{
case ButtonAction.DOWN:
m_IsDragging = true;
break;
case ButtonAction.UP:
m_IsDragging = false;
if (m_UpdateOnlyOnRelease)
{
m_Action.Invoke(Value);
}
break;
case ButtonAction.NONE:
return;
}
}

/// <summary>
/// Returns whether or not action is currently allowed.
/// </summary>
/// <returns>Whether or not action is allowed.</returns>
internal bool ActionIsAllowed()
{
return PeltzerMain.Instance.restrictionManager.menuActionsAllowed;
}
}
}

0 comments on commit ee46008

Please sign in to comment.