MarginSizeModifier.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6. namespace TheraBytes.BetterUi
  7. {
  8. [Serializable]
  9. public class MarginSizeConfigCollection : SizeConfigCollection<MarginSizeModifier> { }
  10. /// <summary>
  11. /// This is for RectOffset objects.
  12. /// But Unity doesn't like it to play around with RectOffset objects, so we have a wrapper class for it.
  13. /// </summary>
  14. [Serializable]
  15. public class MarginSizeModifier : ScreenDependentSize<Margin>
  16. {
  17. public SizeModifierCollection ModLeft;
  18. public SizeModifierCollection ModRight;
  19. public SizeModifierCollection ModTop;
  20. public SizeModifierCollection ModBottom;
  21. public MarginSizeModifier(Margin optimizedSize, Margin minSize, Margin maxSize)
  22. : base(optimizedSize, minSize, maxSize, optimizedSize.Clone())
  23. {
  24. ModLeft = new SizeModifierCollection(new SizeModifierCollection.SizeModifier(ImpactMode.PixelWidth, 1));
  25. ModRight = new SizeModifierCollection(new SizeModifierCollection.SizeModifier(ImpactMode.PixelWidth, 1));
  26. ModTop = new SizeModifierCollection(new SizeModifierCollection.SizeModifier(ImpactMode.PixelWidth, 1));
  27. ModBottom = new SizeModifierCollection(new SizeModifierCollection.SizeModifier(ImpactMode.PixelWidth, 1));
  28. }
  29. public override void DynamicInitialization()
  30. {
  31. if (this.value == null)
  32. this.value = new Margin();
  33. }
  34. public override IEnumerable<SizeModifierCollection> GetModifiers()
  35. {
  36. yield return ModLeft;
  37. yield return ModRight;
  38. yield return ModTop;
  39. yield return ModBottom;
  40. }
  41. protected override void AdjustSize(float factor, SizeModifierCollection mod, int index)
  42. {
  43. if (this.value == null)
  44. this.value = new Margin();
  45. value[index] = Mathf.RoundToInt(GetSize(factor, OptimizedSize[index], MinSize[index], MaxSize[index]));
  46. }
  47. protected override void CalculateOptimizedSize(Margin baseValue, float factor, SizeModifierCollection mod, int index)
  48. {
  49. OptimizedSize[index] = Mathf.RoundToInt(factor * baseValue[index]);
  50. }
  51. }
  52. }