UIFactory.cs 5.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using TMPro;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.Events;
  6. namespace BuffaloSiege
  7. {
  8. public sealed class UIFactory
  9. {
  10. public readonly ArtCatalog Art;public Action ClickSound;
  11. public UIFactory(ArtCatalog a){Art=a;}
  12. public static RectTransform Root(string name,Transform parent)
  13. {var r=new GameObject(name,typeof(RectTransform)).GetComponent<RectTransform>();r.SetParent(parent,false);return r;}
  14. public static void Fill(RectTransform r,float left=0,float top=0,float right=0,float bottom=0)
  15. {r.anchorMin=Vector2.zero;r.anchorMax=Vector2.one;r.pivot=new Vector2(.5f,.5f);r.offsetMin=new Vector2(left,bottom);r.offsetMax=new Vector2(-right,-top);}
  16. public static void Place(RectTransform r,float x,float y,float w,float h)
  17. {r.anchorMin=r.anchorMax=new Vector2(0,1);r.pivot=new Vector2(0,1);r.anchoredPosition=new Vector2(x,-y);r.sizeDelta=new Vector2(w,h);}
  18. GameObject Make(string name,Transform parent,GameObject prefab)
  19. {var o=prefab?UnityEngine.Object.Instantiate(prefab,parent,false):new GameObject(name,typeof(RectTransform));o.name=name;if(!prefab)o.transform.SetParent(parent,false);o.SetActive(true);return o;}
  20. public Image Panel(Transform parent,string name,float x,float y,float w,float h,string sprite="ui.panel.frame")
  21. {var o=Make(name,parent,Art.panelPrefab);var im=o.GetComponent<Image>()??o.AddComponent<Image>();im.sprite=Art.Get(sprite);im.type=UnityEngine.UI.Image.Type.Sliced;im.pixelsPerUnitMultiplier=sprite=="ui.button.wood"?2.5f:1;im.raycastTarget=false;im.color=Color.white;Place((RectTransform)o.transform,x,y,w,h);return im;}
  22. public Image Image(Transform parent,string id,float x,float y,float w,float h,bool preserve=true)
  23. {var r=Root(id,parent);Place(r,x,y,w,h);var im=r.gameObject.AddComponent<Image>();im.sprite=Art.Get(id);im.preserveAspect=preserve;im.raycastTarget=false;return im;}
  24. public Image Solid(Transform parent,string name,Color color,float x,float y,float w,float h)
  25. {var r=Root(name,parent);Place(r,x,y,w,h);var im=r.gameObject.AddComponent<Image>();im.color=color;im.raycastTarget=false;return im;}
  26. public TMP_Text Text(Transform parent,string value,float x,float y,float w,float h,float size=28,TextAlignmentOptions align=TextAlignmentOptions.MidlineLeft,Color? color=null,bool small=false)
  27. {
  28. var o=Make("Text_"+(value.Length>24?value.Substring(0,24):value),parent,Art.textPrefab);Place((RectTransform)o.transform,x,y,w,h);var t=o.GetComponent<TextMeshProUGUI>()??o.AddComponent<TextMeshProUGUI>();
  29. t.font=small?Art.body:Art.serif;t.fontSize=size;t.fontSizeMax=size;t.fontSizeMin=size*.82f;t.enableAutoSizing=true;t.text=value;t.alignment=align;t.color=color??Art.cream;t.raycastTarget=false;t.margin=new Vector4(2,0,2,0);t.textWrappingMode=TextWrappingModes.Normal;t.overflowMode=TextOverflowModes.Ellipsis;return t;
  30. }
  31. public Button Button(Transform parent,string label,float x,float y,float w,float h,Action action,bool gold=false,float size=28)
  32. {
  33. var o=Make("Button_"+label,parent,Art.buttonPrefab);Place((RectTransform)o.transform,x,y,w,h);var im=o.GetComponent<Image>()??o.AddComponent<Image>();im.sprite=Art.Get(gold?"ui.button.gold":"ui.button.wood");im.type=UnityEngine.UI.Image.Type.Sliced;im.pixelsPerUnitMultiplier=gold?1:2.5f;im.raycastTarget=true;
  34. var b=o.GetComponent<Button>()??o.AddComponent<Button>();b.targetGraphic=im;b.transition=Selectable.Transition.ColorTint;b.colors=new ColorBlock{normalColor=Color.white,highlightedColor=new Color(1,1,.9f),pressedColor=new Color(.77f,.65f,.48f),selectedColor=Color.white,disabledColor=new Color(.48f,.43f,.43f,.8f),colorMultiplier=1,fadeDuration=.09f};b.navigation=new Navigation{mode=Navigation.Mode.None};
  35. if(!o.GetComponent<ButtonFeedback>())o.AddComponent<ButtonFeedback>();b.onClick.RemoveAllListeners();b.onClick.AddListener(()=>{ClickSound?.Invoke();action?.Invoke();});
  36. if(!string.IsNullOrEmpty(label))Text(o.transform,label,12,3,w-24,h-6,size,TextAlignmentOptions.Midline,gold?Art.ink:Art.cream);return b;
  37. }
  38. public Button IconButton(Transform parent,string icon,float x,float y,float size,Action action)
  39. {var b=Button(parent,"",x,y,size,size,action);b.name="Button_"+icon;Image(b.transform,"ui."+icon,12,12,size-24,size-24);return b;}
  40. public RectTransform Scroll(Transform parent,string name,float contentHeight,float top,float bottom,float left=0,float right=0)
  41. {
  42. var host=Root(name,parent);Fill(host,left,top,right,bottom);var image=host.gameObject.AddComponent<Image>();image.color=new Color(0,0,0,0.001f);var viewport=Root("Viewport",host);Fill(viewport);viewport.gameObject.AddComponent<RectMask2D>();
  43. var content=Root("Content",viewport);content.anchorMin=new Vector2(0,1);content.anchorMax=new Vector2(1,1);content.pivot=new Vector2(.5f,1);content.sizeDelta=new Vector2(0,contentHeight);content.anchoredPosition=Vector2.zero;
  44. var scroll=host.gameObject.AddComponent<ScrollRect>();scroll.viewport=viewport;scroll.content=content;scroll.horizontal=false;scroll.vertical=true;scroll.movementType=ScrollRect.MovementType.Clamped;scroll.scrollSensitivity=35;scroll.decelerationRate=.08f;return content;
  45. }
  46. public static void Clear(Transform parent)
  47. {for(int i=parent.childCount-1;i>=0;i--){var o=parent.GetChild(i).gameObject;o.SetActive(false);if(Application.isPlaying)UnityEngine.Object.Destroy(o);else UnityEngine.Object.DestroyImmediate(o);}}
  48. }
  49. }