// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System; using System.Collections.Generic; using UnityEngine; namespace Microsoft.MixedReality.Toolkit.UI { /// /// Theme Engine to control initialized GameObject's scale based on state changes /// public class InteractableScaleTheme : InteractableThemeBase { protected Vector3 originalScale; protected Transform hostTransform; public InteractableScaleTheme() { Types = new Type[] { typeof(Transform) }; Name = "Scale Theme"; } /// public override ThemeDefinition GetDefaultThemeDefinition() { return new ThemeDefinition() { ThemeType = GetType(), StateProperties = new List() { new ThemeStateProperty() { Name = "Scale", Type = ThemePropertyTypes.Vector3, Values = new List(), Default = new ThemePropertyValue() { Vector3 = Vector3.one} }, }, CustomProperties = new List() { new ThemeProperty() { Name = "Relative Scale", Tooltip = "Should the scale be relative to initial Gameobject scale, or absolute", Type = ThemePropertyTypes.Bool, Value = new ThemePropertyValue() { Bool = false } }, }, }; } /// public override void Init(GameObject host, ThemeDefinition settings) { hostTransform = host.transform; originalScale = hostTransform.localScale; base.Init(host, settings); } /// public override ThemePropertyValue GetProperty(ThemeStateProperty property) { ThemePropertyValue start = new ThemePropertyValue(); start.Vector3 = hostTransform != null ? hostTransform.localScale : Vector3.zero; return start; } /// public override void SetValue(ThemeStateProperty property, int index, float percentage) { Vector3 lerpTarget = property.Values[index].Vector3; bool relative = Properties[0].Value.Bool; if (relative) { lerpTarget = Vector3.Scale(originalScale, lerpTarget); } SetScale(Vector3.Lerp(property.StartValue.Vector3, lerpTarget, percentage)); } /// protected override void SetValue(ThemeStateProperty property, ThemePropertyValue value) { SetScale(value.Vector3); } private void SetScale(Vector3 newScale) { if (hostTransform != null) { hostTransform.localScale = newScale; } } } }