PopupLayout.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace DevilLink2.UI
  6. {
  7. public enum PopupSize { Large, Compact }
  8. public static class PopupSizeSpecification
  9. {
  10. public const float LargeWidth=.94f,LargeMaxHeight=.88f,CompactWidth=.84f,CompactMaxHeight=.82f;
  11. }
  12. public enum PopupKind { Daily, Wheel, Shop, Profile, Settings, Help, Pause, Result }
  13. // Layout only. Moves original bound objects, never clones controls or modifies player state.
  14. public sealed class PopupLayout : MonoBehaviour
  15. {
  16. private sealed class Block { public RectTransform Rect; public float Height,Width; public bool Flexible; }
  17. private readonly List<Block> body=new List<Block>(),footer=new List<Block>();
  18. private RectTransform card,viewport,content,foot;private ScrollRect scroll;private PopupHeader header;
  19. private Text feedback;private bool detachedFeedback;private Vector2 feedbackSize;
  20. private PopupKind kind;private bool ready;private int signature;private Rect lastSafe;
  21. public PopupSize Size => kind==PopupKind.Daily||kind==PopupKind.Wheel||kind==PopupKind.Shop||kind==PopupKind.Help?PopupSize.Large:PopupSize.Compact;
  22. public Vector2 CurrentSize => card?card.rect.size:Vector2.zero;
  23. public bool IsBodyScrollable => scroll && scroll.vertical;
  24. public static void Install(RectTransform panel,Text title,Button close,PopupKind kind)
  25. {
  26. if(panel.GetComponent<PopupLayout>())return;
  27. var prefab=Resources.Load<GameObject>("PopupHeader_Common");
  28. if(!prefab || !prefab.GetComponent<PopupHeader>()){Debug.LogError("PopupHeader_Common prefab is missing; popup left unchanged.");return;}
  29. var layout=panel.gameObject.AddComponent<PopupLayout>();layout.card=panel;layout.kind=kind;
  30. foreach(var old in panel.GetComponentsInChildren<Image>(true).Where(i=>i.name=="TitleIcon"))old.gameObject.SetActive(false);
  31. layout.header=Instantiate(prefab,panel,false).GetComponent<PopupHeader>();layout.header.name="PopupHeader_Common";layout.header.Bind(title,close);
  32. if(kind!=PopupKind.Daily)layout.BuildBody();
  33. layout.ready=true;layout.Apply();
  34. }
  35. private RectTransform Named(string name) { return card.GetComponentsInChildren<RectTransform>(true).FirstOrDefault(r=>r.name==name); }
  36. private void Add(string name,bool buttons=false)
  37. {
  38. var r=Named(name);if(!r)return;
  39. var item=new Block{Rect=r,Height=r.rect.height,Width=r.rect.width,Flexible=r.GetComponent<Text>() || r.GetComponent<Button>() || (kind==PopupKind.Profile && r.name=="ProfileOverview")};
  40. r.SetParent(buttons?foot:content,false);r.localScale=Vector3.one;(buttons?footer:body).Add(item);
  41. }
  42. private void Group(string name,string[] names,Vector2 origin,Vector2 size)
  43. {
  44. var group=UIBuilder.Rect(name,content,0,0,size.x,size.y);
  45. foreach(var key in names){var r=Named(key);if(!r)continue;var pos=r.anchoredPosition;r.SetParent(group,false);r.localScale=Vector3.one;r.anchoredPosition=pos-new Vector2(origin.x,-origin.y);}
  46. body.Add(new Block{Rect=group,Width=size.x,Height=size.y});
  47. }
  48. private void BuildBody()
  49. {
  50. viewport=UIBuilder.Rect("PopupBodyViewport",card,16,124,card.rect.width-32,300);
  51. var hit=viewport.gameObject.AddComponent<Image>();hit.color=Color.clear;hit.raycastTarget=true;
  52. viewport.gameObject.AddComponent<RectMask2D>();
  53. content=UIBuilder.Rect("PopupBodyContent",viewport,0,0,viewport.rect.width,300);
  54. scroll=viewport.gameObject.AddComponent<ScrollRect>();scroll.viewport=viewport;scroll.content=content;scroll.horizontal=false;scroll.movementType=ScrollRect.MovementType.Clamped;scroll.scrollSensitivity=20;
  55. foot=UIBuilder.Rect("PopupFooter",card,16,0,card.rect.width-32,0);
  56. switch(kind){
  57. case PopupKind.Wheel:
  58. Add("Status");Add("WheelDesignCluster");Add("WheelInventory");Add("Feedback");Add("Note");break;
  59. case PopupKind.Shop:
  60. Group("ShopTabs",new[]{"ItemsTab","RecordsTab"},new Vector2(18,104),new Vector2(370,46));
  61. var pages=UIBuilder.Rect("ShopPages",content,0,0,370,440);
  62. pages.gameObject.AddComponent<RectMask2D>();
  63. var pageHit=pages.gameObject.AddComponent<Image>();pageHit.color=Color.clear;
  64. var products=Named("Items");var records=Named("CoinRecords");
  65. products.SetParent(pages,false);records.SetParent(pages,false);
  66. UIBuilder.Place(products,0,0,370,440);UIBuilder.Place(records,0,0,370,440);
  67. var productScroll=pages.gameObject.AddComponent<ScrollRect>();productScroll.viewport=pages;productScroll.content=products;productScroll.horizontal=false;productScroll.movementType=ScrollRect.MovementType.Clamped;
  68. // Reuse the removed balance row (32 + 12 gap) as content space; keep shell geometry.
  69. body.Add(new Block{Rect=pages,Width=370,Height=484,Flexible=true});
  70. Add("Feedback",true);footer.Last().Height=14;Add("Note",true);break;
  71. case PopupKind.Profile:
  72. if(Named("ProfileOverview"))Add("ProfileOverview");else foreach(var name in new[]{"Avatar","Nickname","Identity","Economy","ModeProgress"})Add(name);
  73. Add("EditNickname",true);Add("ChangeAvatar",true);break;
  74. case PopupKind.Settings:
  75. foreach(var name in new[]{"Audio","Music","Sfx","Data","Shop","Reset","Info","Help","Terms","Privacy","Version"})Add(name);break;
  76. case PopupKind.Help:
  77. foreach(var name in new[]{"Subtitle","HowTo","FAQ"})Add(name);Add("Footer",true);break;
  78. case PopupKind.Pause:
  79. Add("Note");Add("Resume",true);Add("Restart",true);Add("Lobby",true);
  80. foreach(var button in footer){button.Width=260;button.Height=36;}break;
  81. case PopupKind.Result:
  82. Add("FinalScore");Add("Rewards");Add("Next",true);Add("Retry",true);Add("Lobby",true);
  83. foreach(var button in footer){button.Width=280;button.Height=36;}break;
  84. }
  85. if(kind==PopupKind.Profile || kind==PopupKind.Settings){
  86. var r=card.parent.Find("Feedback") as RectTransform;
  87. if(r){feedback=r.GetComponent<Text>();feedbackSize=r.sizeDelta;r.SetParent(foot,false);footer.Add(new Block{Rect=r,Width=r.rect.width,Height=kind==PopupKind.Profile?14:32,Flexible=true});}
  88. }
  89. }
  90. private bool Editing()
  91. {
  92. foreach(var name in new[]{"NicknameEditor","AvatarPicker","ResetConfirmation","LegalPanel"}){var child=card.parent.Find(name);if(child && child.gameObject.activeSelf)return true;}
  93. return false;
  94. }
  95. private Rect SafeRect()
  96. {
  97. var parent=(RectTransform)card.parent;var bounds=parent.rect;
  98. var canvas=GetComponentInParent<Canvas>();Vector2 min,max;
  99. var camera=canvas && canvas.renderMode!=RenderMode.ScreenSpaceOverlay?canvas.worldCamera:null;
  100. if(Screen.width>0 && Screen.height>0 && RectTransformUtility.ScreenPointToLocalPointInRectangle(parent,Screen.safeArea.min,camera,out min) && RectTransformUtility.ScreenPointToLocalPointInRectangle(parent,Screen.safeArea.max,camera,out max)){
  101. float left=Mathf.Max(bounds.xMin,min.x),right=Mathf.Min(bounds.xMax,max.x),bottom=Mathf.Max(bounds.yMin,min.y),top=Mathf.Min(bounds.yMax,max.y);
  102. if(right>left && top>bottom)return Rect.MinMaxRect(left,bottom,right,top);
  103. }
  104. return bounds;
  105. }
  106. private void LateUpdate()
  107. {
  108. if(!ready)return;var safe=SafeRect();int hash=17;
  109. foreach(var b in body.Concat(footer)){var text=b.Rect.GetComponent<Text>();hash=unchecked(hash*31+(b.Rect.gameObject.activeSelf?1:0));if(text)hash=unchecked(hash*31+(text.text??"").GetHashCode());}
  110. hash=unchecked(hash*31+(Editing()?1:0));
  111. if(hash!=signature || safe!=lastSafe){signature=hash;Apply();}
  112. }
  113. private float Arrange(List<Block> blocks,float width)
  114. {
  115. float y=0;int count=0;
  116. foreach(var b in blocks){
  117. if(!b.Rect.gameObject.activeSelf)continue;
  118. if(feedback && b.Rect==feedback.rectTransform && detachedFeedback){if(kind==PopupKind.Profile){if(count++>0)y+=12;y+=b.Height;}continue;}
  119. var text=b.Rect.GetComponent<Text>();
  120. if(text && !text.enabled)continue;
  121. if(text && string.IsNullOrEmpty(text.text) && !((kind==PopupKind.Shop || kind==PopupKind.Profile) && b.Rect.name=="Feedback"))continue;
  122. float w=b.Flexible?Mathf.Min(b.Width,width):b.Width;
  123. // Settings audio uses the full 16px-inset body; menus sit another 10px inward.
  124. if(kind==PopupKind.Settings && b.Rect.GetComponent<Button>())
  125. w=b.Rect.name=="Music" || b.Rect.name=="Sfx"?width:Mathf.Max(1,width-20);
  126. if(w>width+.5f && !b.Flexible)Debug.LogWarning("Popup body needs more horizontal room: "+b.Rect.name);
  127. float h=b.Height;
  128. UIBuilder.Place(b.Rect,(width-w)*.5f,0,w,h);
  129. if(text && !((kind==PopupKind.Shop || kind==PopupKind.Profile) && (b.Rect.name=="Feedback" || b.Rect.name=="Note")))h=Mathf.Max(h,text.preferredHeight+4);
  130. if(count++>0)y+=kind==PopupKind.Settings || kind==PopupKind.Pause || kind==PopupKind.Result?10:12;
  131. UIBuilder.Place(b.Rect,(width-w)*.5f,y,w,h);y+=h;
  132. var button=b.Rect.GetComponent<Button>();
  133. if(button){
  134. var settings=kind==PopupKind.Settings?GetComponentInParent<SettingsControlsVisuals>():null;
  135. if(kind==PopupKind.Pause || kind==PopupKind.Result)ResizeFlowButton(b.Rect,w,h);
  136. else if(settings)settings.Resize(button,w);else ResizeButton(b.Rect,w);
  137. }
  138. }
  139. return y;
  140. }
  141. private static void ResizeFlowButton(RectTransform button,float width,float height)
  142. {
  143. // These surfaces inherited stretch anchors from GameplayContentLayout.Button.
  144. // An explicit local rectangle avoids adding a second parent-width to sizeDelta.
  145. foreach(var key in new[]{"UI11BButtonFace","UI11BButton","Face"}){
  146. var face=button.Find(key) as RectTransform;
  147. if(face)UIBuilder.Place(face,1,1,width-2,height-2);
  148. }
  149. var label=button.Find("Label") as RectTransform;
  150. if(label){UIBuilder.Place(label,8,0,width-16,height);label.GetComponent<Text>().alignment=TextAnchor.MiddleCenter;}
  151. }
  152. private static void ResizeButton(RectTransform button,float width)
  153. {
  154. var label=button.Find("Label") as RectTransform;if(label)UIBuilder.Place(label,4,0,width-8,label.rect.height);
  155. foreach(var key in new[]{"UI11BButtonFace","Face"}){var r=button.Find(key) as RectTransform;if(r)r.sizeDelta=new Vector2(width-2,r.sizeDelta.y);}
  156. var track=button.Find("SwitchTrack") as RectTransform;
  157. if(track){
  158. UIBuilder.Place(track,width-70,12,62,22);
  159. var slider=button.GetComponentInChildren<Slider>(true);
  160. if(slider){var r=(RectTransform)slider.transform;float w=Mathf.Min(156,width-176);UIBuilder.Place(r,96,30,w,30);
  161. var rail=r.Find("Track") as RectTransform;if(rail)rail.sizeDelta=new Vector2(w,rail.sizeDelta.y);}
  162. if(label)UIBuilder.Place(label,12,0,width-90,label.rect.height);
  163. }
  164. }
  165. private void Apply()
  166. {
  167. if(!ready)return;
  168. lastSafe=SafeRect();
  169. if(kind==PopupKind.Daily){header.Resize(card.rect.width);return;}
  170. if(feedback){
  171. detachedFeedback=Editing();
  172. feedback.transform.SetParent(detachedFeedback?card.parent:foot,false);
  173. if(detachedFeedback){UIBuilder.Place(feedback.rectTransform,38,824,feedbackSize.x,feedbackSize.y);feedback.transform.SetAsLastSibling();}
  174. }
  175. bool large=Size==PopupSize.Large;
  176. float width=lastSafe.width*(large?PopupSizeSpecification.LargeWidth:PopupSizeSpecification.CompactWidth),maxHeight=lastSafe.height*(large?PopupSizeSpecification.LargeMaxHeight:PopupSizeSpecification.CompactMaxHeight);
  177. // Recompute from the original Compact baseline, never add to the previous frame's size.
  178. if(kind==PopupKind.Settings)width=Mathf.Min(width+34,Mathf.Max(1,lastSafe.width-32));
  179. float bodyWidth=width-32;
  180. if(kind==PopupKind.Shop)body.First(b=>b.Rect.name=="ShopPages").Height=484;
  181. if(kind==PopupKind.Help)body.First(b=>b.Rect.name=="FAQ").Width=Mathf.Min(338,bodyWidth);
  182. float bodyHeight=Arrange(body,bodyWidth),footerHeight=Arrange(footer,bodyWidth);
  183. float topInset=kind==PopupKind.Pause?110:124;
  184. float bottomInset=kind==PopupKind.Profile?40:44;
  185. float fixedHeight=topInset+bottomInset+(footerHeight>0?footerHeight+12:0);
  186. float height=Mathf.Min(maxHeight,fixedHeight+bodyHeight);
  187. // Previous Settings: body458 + gaps120 + header124 + bottom44 = 746.
  188. // Keep that shell/top edge; only its body scrolls when necessary.
  189. if(kind==PopupKind.Settings)height=Mathf.Min(maxHeight,746);
  190. // Original Help: 28+46+470+34+36 gaps+124 header+44 bottom = 782.
  191. if(kind==PopupKind.Help)height=Mathf.Min(maxHeight,782);
  192. // Shop trims only its lower edge; both tabs retain the same shell and top edge.
  193. float shopPositionHeight=height;
  194. if(kind==PopupKind.Shop)height=Mathf.Max(fixedHeight+24,height-44);
  195. // Never compress fixed-size title/footer; on unusually tiny safe areas keep them reachable via scrolling.
  196. float viewHeight=Mathf.Max(24,height-fixedHeight);
  197. if(fixedHeight+24>height){height=Mathf.Min(lastSafe.height,fixedHeight+24);viewHeight=24;}
  198. if(kind==PopupKind.Shop){
  199. // Only the shared content slot adapts. Active tab and ledger count never size the shell.
  200. var slot=body.First(b=>b.Rect.name=="ShopPages");
  201. slot.Height=Mathf.Max(64,viewHeight-58);
  202. bodyHeight=Arrange(body,bodyWidth);
  203. }
  204. if(kind==PopupKind.Help){
  205. // Keep FAQ scrolling inside a fixed viewport, with Footer outside its mask.
  206. var faq=body.First(b=>b.Rect.name=="FAQ");
  207. faq.Height=Mathf.Max(24,viewHeight-98);bodyHeight=Arrange(body,bodyWidth);
  208. var help=GetComponentInParent<HelpFaqVisuals>();if(help)help.RefreshViewport();
  209. }
  210. var parent=(RectTransform)card.parent;
  211. // Profile keeps its previously approved top edge; trim only unused space below.
  212. float positionHeight=kind==PopupKind.Profile?Mathf.Min(maxHeight,736):kind==PopupKind.Shop?shopPositionHeight:height;
  213. UIBuilder.Place(card,lastSafe.center.x-width*.5f-parent.rect.xMin,parent.rect.yMax-lastSafe.center.y-positionHeight*.5f,width,height);
  214. header.Resize(width);
  215. foreach(var name in new[]{"UnifiedGothicPanel","UI11BArt"}){var r=card.Find(name) as RectTransform;if(r && r.gameObject.activeSelf)UIBuilder.Place(r,0,0,width,height);}
  216. UIBuilder.Place(viewport,16,topInset,bodyWidth,viewHeight);content.sizeDelta=new Vector2(bodyWidth,Mathf.Max(viewHeight,bodyHeight));
  217. scroll.vertical=bodyHeight>viewHeight+.5f;
  218. if(!scroll.vertical){content.anchoredPosition=Vector2.zero;scroll.StopMovement();}
  219. UIBuilder.Place(foot,16,height-bottomInset-footerHeight,bodyWidth,footerHeight);
  220. }
  221. }
  222. }