Skip to content

Commit

Permalink
Modify guide snap distance via guide panel slider (#151)
Browse files Browse the repository at this point in the history
* Implemented changing the guide snap distance using a slider in the guide panel.

* Fixed slider knob not initialized to the correct value.
  • Loading branch information
insominx authored Aug 11, 2021
1 parent bd0af4c commit ee14a3a
Show file tree
Hide file tree
Showing 11 changed files with 985 additions and 13 deletions.
643 changes: 632 additions & 11 deletions Assets/Prefabs/Panels/GuideToolsPanel.prefab

Large diffs are not rendered by default.

Binary file added Assets/Resources/Icons/pointersnap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
92 changes: 92 additions & 0 deletions Assets/Resources/Icons/pointersnap.png.meta

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

Binary file added Assets/Resources/Icons/pointersnap_space.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
92 changes: 92 additions & 0 deletions Assets/Resources/Icons/pointersnap_space.png.meta

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

54 changes: 54 additions & 0 deletions Assets/Scripts/Commands/ModifyStencilAttractDistanceCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2021 The Open Brush 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.

namespace TiltBrush
{
public class ModifyStencilAttractDistanceCommand : BaseCommand
{
private readonly float m_StartDistance;
private float m_EndDistance;
private bool m_Final;

public ModifyStencilAttractDistanceCommand(float endDistance, bool final = false,
BaseCommand parent = null) : base(parent)
{
m_StartDistance = WidgetManager.m_Instance.StencilAttractDist;
m_EndDistance = endDistance;
m_Final = final;
}

public override bool NeedsSave => true;

protected override void OnUndo()
{
WidgetManager.m_Instance.StencilAttractDist = m_StartDistance;
}

protected override void OnRedo()
{
WidgetManager.m_Instance.StencilAttractDist = m_EndDistance;
}

public override bool Merge(BaseCommand other)
{
if (base.Merge(other)) { return true; }
if (m_Final) { return false; }
ModifyStencilAttractDistanceCommand distanceCommand = other as ModifyStencilAttractDistanceCommand;
if (distanceCommand == null) { return false; }
m_EndDistance = distanceCommand.m_EndDistance;
m_Final = distanceCommand.m_Final;
return true;
}
}
} // namespace TiltBrush

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

83 changes: 83 additions & 0 deletions Assets/Scripts/GUI/StencilAttractDistanceSlider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2021 The Open Brush 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 UnityEngine;
using System;

namespace TiltBrush
{
public class StencilAttractDistanceSlider : BaseSlider
{
[SerializeField] private float m_MinAttractDistance = 0.25f;
[SerializeField] private float m_MaxAttractDistance = 2f;

void OnEnable()
{
App.Switchboard.StencilAttractDistChanged += OnStencilAttractDistChanged;
OnStencilAttractDistChanged();
}

protected override void OnDestroy()
{
base.OnDestroy();
App.Switchboard.StencilAttractDistChanged -= OnStencilAttractDistChanged;
}

public override void UpdateValue(float value)
{
base.UpdateValue(value);
SetSliderPositionToReflectValue();

SetDescriptionText(m_DescriptionText, $"{value * 100:0}%");
}

// If some other logic (not the slider) changes the value, we
// will be notified here so that we can update the slider visuals
private void OnStencilAttractDistChanged()
{
if (WidgetManager.m_Instance != null)
{
float value = WidgetManager.m_Instance.StencilAttractDist;
float range = m_MaxAttractDistance - m_MinAttractDistance;
float newSliderValue = (value - m_MinAttractDistance) / range;
UpdateValue(newSliderValue);
}
}

public override void ButtonReleased()
{
base.ButtonReleased();
EndModifyCommand();
}

public override void ResetState()
{
if (m_HadButtonPress)
{
EndModifyCommand();
}
base.ResetState();
}

void EndModifyCommand()
{
float percent = GetCurrentValue();
float displacement = m_MaxAttractDistance - m_MinAttractDistance;
float newDistance = m_MinAttractDistance + percent * displacement;

SketchMemoryScript.m_Instance.PerformAndRecordCommand(
new ModifyStencilAttractDistanceCommand(newDistance, true));
}
}
} // namespace TiltBrush
11 changes: 11 additions & 0 deletions Assets/Scripts/GUI/StencilAttractDistanceSlider.cs.meta

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

6 changes: 6 additions & 0 deletions Assets/Scripts/Switchboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Switchboard
public event Action MirrorVisibilityChanged;
public event Action PanelDismissed;
public event Action StencilModeChanged;
public event Action StencilAttractDistChanged;
public event Action AudioReactiveStateChanged;
public event Action MemoryExceededChanged;
public event Action MemoryWarningAcceptedChanged;
Expand Down Expand Up @@ -64,6 +65,11 @@ public void TriggerStencilModeChanged()
StencilModeChanged?.Invoke();
}

public void TriggerStencilAttractDistChanged()
{
StencilAttractDistChanged?.Invoke();
}

public void TriggerAudioReactiveStateChanged()
{
AudioReactiveStateChanged?.Invoke();
Expand Down
14 changes: 12 additions & 2 deletions Assets/Scripts/WidgetManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,16 @@ public LayerMask StencilLayerMask
get { return LayerMask.GetMask(m_StencilLayerName); }
}

public float StencilAttractDist
{
get => m_StencilAttractDist;
set
{
m_StencilAttractDist = value;
App.Switchboard.TriggerStencilAttractDistChanged();
}
}

public List<GrabWidgetData> WidgetsNearBrush
{
get { return m_WidgetsNearBrush; }
Expand Down Expand Up @@ -857,7 +867,7 @@ public void MagnetizeToStencils(ref Vector3 pos, ref Quaternion rot)
Collider collider = stencil.m_WidgetScript.GrabCollider;
float centerDist = (collider.bounds.center - samplePos).sqrMagnitude;
if (centerDist >
(m_StencilAttractDist * m_StencilAttractDist + collider.bounds.extents.sqrMagnitude))
(StencilAttractDist * StencilAttractDist + collider.bounds.extents.sqrMagnitude))
{
continue;
}
Expand All @@ -867,7 +877,7 @@ public void MagnetizeToStencils(ref Vector3 pos, ref Quaternion rot)

// Find out how far we are from this point and save it as a score.
float distToSurfactPoint = (m_StencilContactInfos[sIndex].pos - samplePos).magnitude;
float score = 1.0f - (distToSurfactPoint / m_StencilAttractDist);
float score = 1.0f - (distToSurfactPoint / StencilAttractDist);
if (score > fBestScore)
{
iPrimaryIndex = sIndex;
Expand Down

0 comments on commit ee14a3a

Please sign in to comment.