PopupLayout.cs 16 KB

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