Settings are now loaded from and saved to the config.json file. Reworked the UI structure a bit (the script that manage the settings have to be active before the UI is opened to actually take effect at game startup). Fixed several UI bugs (slider deselection, UI labels blocking the sliders, etc).
This commit is contained in:
@@ -3,82 +3,35 @@ using UnityEngine.UI;
|
||||
using UnityEngine.EventSystems;
|
||||
using TMPro;
|
||||
|
||||
public class MoveSliderDragHandler : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
|
||||
public class MoveSliderDragHandler : AbstractSlider, IDragHandler, IBeginDragHandler, IEndDragHandler
|
||||
{
|
||||
public RectTransform sliderBackground; // Assign in Inspector
|
||||
public float minValue = 2f;
|
||||
public float maxValue = 5f;
|
||||
public float slideareaOffsetMultiplier = 0.9f;
|
||||
public TMP_Text warningText;
|
||||
public float warningThreshholdValue = 0.65f;
|
||||
public float CurrentValue { get; private set; }
|
||||
|
||||
public ContinuoslocomotionConfigurator configurator;
|
||||
|
||||
private RectTransform handleRect;
|
||||
private Vector2 backgroundStart;
|
||||
private float backgroundWidth;
|
||||
|
||||
void Awake()
|
||||
override public void OnEndDrag(PointerEventData eventData)
|
||||
{
|
||||
handleRect = GetComponent<RectTransform>();
|
||||
|
||||
if (sliderBackground != null)
|
||||
{
|
||||
backgroundStart = sliderBackground.position;
|
||||
backgroundWidth = sliderBackground.rect.width;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void OnBeginDrag(PointerEventData eventData)
|
||||
{
|
||||
UpdateSlider(eventData);
|
||||
}
|
||||
|
||||
public void OnDrag(PointerEventData eventData)
|
||||
{
|
||||
UpdateSlider(eventData);
|
||||
}
|
||||
|
||||
public void OnEndDrag(PointerEventData eventData)
|
||||
{
|
||||
UpdateSlider(eventData);
|
||||
base.OnEndDrag(eventData);
|
||||
configurator.UpdateSpeed(CurrentValue);
|
||||
|
||||
EventSystem.current.SetSelectedGameObject(null);
|
||||
}
|
||||
private void UpdateSlider(PointerEventData eventData)
|
||||
|
||||
override protected void UpdateSlider(PointerEventData eventData, bool draggingEnded = false)
|
||||
{
|
||||
Debug.Log("UpDating Slider");
|
||||
Vector2 localPoint;
|
||||
RectTransformUtility.ScreenPointToLocalPointInRectangle(sliderBackground, eventData.position, eventData.pressEventCamera, out localPoint);
|
||||
base.UpdateSlider(eventData, draggingEnded);
|
||||
DisplayWarningTextIfNecessary();
|
||||
}
|
||||
|
||||
float halfWidth = sliderBackground.rect.width * 0.5f;
|
||||
override protected void UpdateHandlePosition()
|
||||
{
|
||||
base.UpdateHandlePosition();
|
||||
DisplayWarningTextIfNecessary();
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Only allow dragging within 90% of the slider width, centered
|
||||
float limit = halfWidth * slideareaOffsetMultiplier;
|
||||
float clampedX = Mathf.Clamp(localPoint.x, -limit, limit);
|
||||
|
||||
Vector3 newPosition = new Vector3(clampedX, handleRect.localPosition.y, handleRect.localPosition.z);
|
||||
handleRect.localPosition = newPosition;
|
||||
|
||||
// Normalize within the limited 90% range
|
||||
float normalized = (clampedX + limit) / (limit * 2f);
|
||||
//Debug.Log(normalized);
|
||||
CurrentValue = Mathf.Lerp(minValue, maxValue, normalized);
|
||||
|
||||
// Displaying the warning.
|
||||
|
||||
if (CurrentValue > (warningThreshholdValue*(maxValue - minValue) + minValue)) warningText.gameObject.SetActive(true);
|
||||
|
||||
protected void DisplayWarningTextIfNecessary()
|
||||
{
|
||||
if (CurrentValue > (warningThreshholdValue * (maxValue - minValue) + minValue)) warningText.gameObject.SetActive(true);
|
||||
else warningText.gameObject.SetActive(false);
|
||||
|
||||
//Debug.Log(warningThreshholdValue);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user