| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- using UnityEngine.UI;
- namespace DevilLink2.UI
- {
- public enum PopupSize { Large, Compact }
- public static class PopupSizeSpecification
- {
- public const float LargeWidth=.94f,LargeMaxHeight=.88f,CompactWidth=.84f,CompactMaxHeight=.82f;
- }
- public enum PopupKind { Daily, Wheel, Shop, Profile, Settings, Help, Pause, Result }
- // Layout only. Moves original bound objects, never clones controls or modifies player state.
- public sealed class PopupLayout : MonoBehaviour
- {
- private sealed class Block { public RectTransform Rect; public float Height,Width; public bool Flexible; }
- private readonly List<Block> body=new List<Block>(),footer=new List<Block>();
- private RectTransform card,viewport,content,foot;private ScrollRect scroll;private PopupHeader header;
- private Text feedback;private bool detachedFeedback;private Vector2 feedbackSize;
- private PopupKind kind;private bool ready;private int signature;private Rect lastSafe;
- public PopupSize Size => kind==PopupKind.Daily||kind==PopupKind.Wheel||kind==PopupKind.Shop||kind==PopupKind.Help?PopupSize.Large:PopupSize.Compact;
- public Vector2 CurrentSize => card?card.rect.size:Vector2.zero;
- public bool IsBodyScrollable => scroll && scroll.vertical;
- public static void Install(RectTransform panel,Text title,Button close,PopupKind kind)
- {
- if(panel.GetComponent<PopupLayout>())return;
- var prefab=Resources.Load<GameObject>("PopupHeader_Common");
- if(!prefab || !prefab.GetComponent<PopupHeader>()){Debug.LogError("PopupHeader_Common prefab is missing; popup left unchanged.");return;}
- var layout=panel.gameObject.AddComponent<PopupLayout>();layout.card=panel;layout.kind=kind;
- foreach(var old in panel.GetComponentsInChildren<Image>(true).Where(i=>i.name=="TitleIcon"))old.gameObject.SetActive(false);
- layout.header=Instantiate(prefab,panel,false).GetComponent<PopupHeader>();layout.header.name="PopupHeader_Common";layout.header.Bind(title,close);
- if(kind!=PopupKind.Daily)layout.BuildBody();
- layout.ready=true;layout.Apply();
- }
- private RectTransform Named(string name) { return card.GetComponentsInChildren<RectTransform>(true).FirstOrDefault(r=>r.name==name); }
- private void Add(string name,bool buttons=false)
- {
- var r=Named(name);if(!r)return;
- 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")};
- r.SetParent(buttons?foot:content,false);r.localScale=Vector3.one;(buttons?footer:body).Add(item);
- }
- private void Group(string name,string[] names,Vector2 origin,Vector2 size)
- {
- var group=UIBuilder.Rect(name,content,0,0,size.x,size.y);
- 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);}
- body.Add(new Block{Rect=group,Width=size.x,Height=size.y});
- }
- private void BuildBody()
- {
- viewport=UIBuilder.Rect("PopupBodyViewport",card,16,124,card.rect.width-32,300);
- var hit=viewport.gameObject.AddComponent<Image>();hit.color=Color.clear;hit.raycastTarget=true;
- viewport.gameObject.AddComponent<RectMask2D>();
- content=UIBuilder.Rect("PopupBodyContent",viewport,0,0,viewport.rect.width,300);
- scroll=viewport.gameObject.AddComponent<ScrollRect>();scroll.viewport=viewport;scroll.content=content;scroll.horizontal=false;scroll.movementType=ScrollRect.MovementType.Clamped;scroll.scrollSensitivity=20;
- foot=UIBuilder.Rect("PopupFooter",card,16,0,card.rect.width-32,0);
- switch(kind){
- case PopupKind.Wheel:
- Add("Status");Add("WheelDesignCluster");Add("WheelInventory");Add("Feedback");Add("Note");break;
- case PopupKind.Shop:
- Group("ShopTabs",new[]{"ItemsTab","RecordsTab"},new Vector2(18,104),new Vector2(370,46));
- var pages=UIBuilder.Rect("ShopPages",content,0,0,370,440);
- pages.gameObject.AddComponent<RectMask2D>();
- var pageHit=pages.gameObject.AddComponent<Image>();pageHit.color=Color.clear;
- var products=Named("Items");var records=Named("CoinRecords");
- products.SetParent(pages,false);records.SetParent(pages,false);
- UIBuilder.Place(products,0,0,370,440);UIBuilder.Place(records,0,0,370,440);
- var productScroll=pages.gameObject.AddComponent<ScrollRect>();productScroll.viewport=pages;productScroll.content=products;productScroll.horizontal=false;productScroll.movementType=ScrollRect.MovementType.Clamped;
- // Reuse the removed balance row (32 + 12 gap) as content space; keep shell geometry.
- body.Add(new Block{Rect=pages,Width=370,Height=484,Flexible=true});
- Add("Feedback",true);footer.Last().Height=14;Add("Note",true);break;
- case PopupKind.Profile:
- if(Named("ProfileOverview"))Add("ProfileOverview");else foreach(var name in new[]{"Avatar","Nickname","Identity","Economy","ModeProgress"})Add(name);
- Add("EditNickname",true);Add("ChangeAvatar",true);break;
- case PopupKind.Settings:
- foreach(var name in new[]{"Audio","Music","Sfx","Data","Shop","Reset","Info","Help","Terms","Privacy","Version"})Add(name);break;
- case PopupKind.Help:
- foreach(var name in new[]{"Subtitle","HowTo","FAQ"})Add(name);Add("Footer",true);break;
- case PopupKind.Pause:
- Add("Note");Add("Resume",true);Add("Restart",true);Add("Lobby",true);
- foreach(var button in footer){button.Width=260;button.Height=36;}break;
- case PopupKind.Result:
- Add("FinalScore");Add("Rewards");Add("Next",true);Add("Retry",true);Add("Lobby",true);
- foreach(var button in footer){button.Width=280;button.Height=36;}break;
- }
- if(kind==PopupKind.Profile || kind==PopupKind.Settings){
- var r=card.parent.Find("Feedback") as RectTransform;
- 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});}
- }
- }
- private bool Editing()
- {
- foreach(var name in new[]{"NicknameEditor","AvatarPicker","ResetConfirmation","LegalPanel"}){var child=card.parent.Find(name);if(child && child.gameObject.activeSelf)return true;}
- return false;
- }
- private Rect SafeRect()
- {
- var parent=(RectTransform)card.parent;var bounds=parent.rect;
- var canvas=GetComponentInParent<Canvas>();Vector2 min,max;
- var camera=canvas && canvas.renderMode!=RenderMode.ScreenSpaceOverlay?canvas.worldCamera:null;
- 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)){
- 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);
- if(right>left && top>bottom)return Rect.MinMaxRect(left,bottom,right,top);
- }
- return bounds;
- }
- private void LateUpdate()
- {
- if(!ready)return;var safe=SafeRect();int hash=17;
- 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());}
- hash=unchecked(hash*31+(Editing()?1:0));
- if(hash!=signature || safe!=lastSafe){signature=hash;Apply();}
- }
- private float Arrange(List<Block> blocks,float width)
- {
- float y=0;int count=0;
- foreach(var b in blocks){
- if(!b.Rect.gameObject.activeSelf)continue;
- if(feedback && b.Rect==feedback.rectTransform && detachedFeedback){if(kind==PopupKind.Profile){if(count++>0)y+=12;y+=b.Height;}continue;}
- var text=b.Rect.GetComponent<Text>();
- if(text && !text.enabled)continue;
- if(text && string.IsNullOrEmpty(text.text) && !((kind==PopupKind.Shop || kind==PopupKind.Profile) && b.Rect.name=="Feedback"))continue;
- float w=b.Flexible?Mathf.Min(b.Width,width):b.Width;
- // Settings audio uses the full 16px-inset body; menus sit another 10px inward.
- if(kind==PopupKind.Settings && b.Rect.GetComponent<Button>())
- w=b.Rect.name=="Music" || b.Rect.name=="Sfx"?width:Mathf.Max(1,width-20);
- if(w>width+.5f && !b.Flexible)Debug.LogWarning("Popup body needs more horizontal room: "+b.Rect.name);
- float h=b.Height;
- UIBuilder.Place(b.Rect,(width-w)*.5f,0,w,h);
- if(text && !((kind==PopupKind.Shop || kind==PopupKind.Profile) && (b.Rect.name=="Feedback" || b.Rect.name=="Note")))h=Mathf.Max(h,text.preferredHeight+4);
- if(count++>0)y+=kind==PopupKind.Settings || kind==PopupKind.Pause || kind==PopupKind.Result?10:12;
- UIBuilder.Place(b.Rect,(width-w)*.5f,y,w,h);y+=h;
- var button=b.Rect.GetComponent<Button>();
- if(button){
- var settings=kind==PopupKind.Settings?GetComponentInParent<SettingsControlsVisuals>():null;
- if(kind==PopupKind.Pause || kind==PopupKind.Result)ResizeFlowButton(b.Rect,w,h);
- else if(settings)settings.Resize(button,w);else ResizeButton(b.Rect,w);
- }
- }
- return y;
- }
- private static void ResizeFlowButton(RectTransform button,float width,float height)
- {
- // These surfaces inherited stretch anchors from GameplayContentLayout.Button.
- // An explicit local rectangle avoids adding a second parent-width to sizeDelta.
- foreach(var key in new[]{"UI11BButtonFace","UI11BButton","Face"}){
- var face=button.Find(key) as RectTransform;
- if(face)UIBuilder.Place(face,1,1,width-2,height-2);
- }
- var label=button.Find("Label") as RectTransform;
- if(label){UIBuilder.Place(label,8,0,width-16,height);label.GetComponent<Text>().alignment=TextAnchor.MiddleCenter;}
- }
- private static void ResizeButton(RectTransform button,float width)
- {
- var label=button.Find("Label") as RectTransform;if(label)UIBuilder.Place(label,4,0,width-8,label.rect.height);
- 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);}
- var track=button.Find("SwitchTrack") as RectTransform;
- if(track){
- UIBuilder.Place(track,width-70,12,62,22);
- var slider=button.GetComponentInChildren<Slider>(true);
- if(slider){var r=(RectTransform)slider.transform;float w=Mathf.Min(156,width-176);UIBuilder.Place(r,96,30,w,30);
- var rail=r.Find("Track") as RectTransform;if(rail)rail.sizeDelta=new Vector2(w,rail.sizeDelta.y);}
- if(label)UIBuilder.Place(label,12,0,width-90,label.rect.height);
- }
- }
- private void Apply()
- {
- if(!ready)return;
- lastSafe=SafeRect();
- if(kind==PopupKind.Daily){header.Resize(card.rect.width);return;}
- if(feedback){
- detachedFeedback=Editing();
- feedback.transform.SetParent(detachedFeedback?card.parent:foot,false);
- if(detachedFeedback){UIBuilder.Place(feedback.rectTransform,38,824,feedbackSize.x,feedbackSize.y);feedback.transform.SetAsLastSibling();}
- }
- bool large=Size==PopupSize.Large;
- float width=lastSafe.width*(large?PopupSizeSpecification.LargeWidth:PopupSizeSpecification.CompactWidth),maxHeight=lastSafe.height*(large?PopupSizeSpecification.LargeMaxHeight:PopupSizeSpecification.CompactMaxHeight);
- // Recompute from the original Compact baseline, never add to the previous frame's size.
- if(kind==PopupKind.Settings)width=Mathf.Min(width+34,Mathf.Max(1,lastSafe.width-32));
- float bodyWidth=width-32;
- if(kind==PopupKind.Shop)body.First(b=>b.Rect.name=="ShopPages").Height=484;
- if(kind==PopupKind.Help)body.First(b=>b.Rect.name=="FAQ").Width=Mathf.Min(338,bodyWidth);
- float bodyHeight=Arrange(body,bodyWidth),footerHeight=Arrange(footer,bodyWidth);
- float topInset=kind==PopupKind.Pause?110:124;
- float bottomInset=kind==PopupKind.Profile?40:44;
- float fixedHeight=topInset+bottomInset+(footerHeight>0?footerHeight+12:0);
- float height=Mathf.Min(maxHeight,fixedHeight+bodyHeight);
- // Previous Settings: body458 + gaps120 + header124 + bottom44 = 746.
- // Keep that shell/top edge; only its body scrolls when necessary.
- if(kind==PopupKind.Settings)height=Mathf.Min(maxHeight,746);
- // Original Help: 28+46+470+34+36 gaps+124 header+44 bottom = 782.
- if(kind==PopupKind.Help)height=Mathf.Min(maxHeight,782);
- // Shop trims only its lower edge; both tabs retain the same shell and top edge.
- float shopPositionHeight=height;
- if(kind==PopupKind.Shop)height=Mathf.Max(fixedHeight+24,height-44);
- // Never compress fixed-size title/footer; on unusually tiny safe areas keep them reachable via scrolling.
- float viewHeight=Mathf.Max(24,height-fixedHeight);
- if(fixedHeight+24>height){height=Mathf.Min(lastSafe.height,fixedHeight+24);viewHeight=24;}
- if(kind==PopupKind.Shop){
- // Only the shared content slot adapts. Active tab and ledger count never size the shell.
- var slot=body.First(b=>b.Rect.name=="ShopPages");
- slot.Height=Mathf.Max(64,viewHeight-58);
- bodyHeight=Arrange(body,bodyWidth);
- }
- if(kind==PopupKind.Help){
- // Keep FAQ scrolling inside a fixed viewport, with Footer outside its mask.
- var faq=body.First(b=>b.Rect.name=="FAQ");
- faq.Height=Mathf.Max(24,viewHeight-98);bodyHeight=Arrange(body,bodyWidth);
- var help=GetComponentInParent<HelpFaqVisuals>();if(help)help.RefreshViewport();
- }
- var parent=(RectTransform)card.parent;
- // Profile keeps its previously approved top edge; trim only unused space below.
- float positionHeight=kind==PopupKind.Profile?Mathf.Min(maxHeight,736):kind==PopupKind.Shop?shopPositionHeight:height;
- UIBuilder.Place(card,lastSafe.center.x-width*.5f-parent.rect.xMin,parent.rect.yMax-lastSafe.center.y-positionHeight*.5f,width,height);
- header.Resize(width);
- 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);}
- UIBuilder.Place(viewport,16,topInset,bodyWidth,viewHeight);content.sizeDelta=new Vector2(bodyWidth,Mathf.Max(viewHeight,bodyHeight));
- scroll.vertical=bodyHeight>viewHeight+.5f;
- if(!scroll.vertical){content.anchoredPosition=Vector2.zero;scroll.StopMovement();}
- UIBuilder.Place(foot,16,height-bottomInset-footerHeight,bodyWidth,footerHeight);
- }
- }
- }
|