77using UnityEngine . UI ;
88#endif
99
10+ using System ;
1011using UnityEngine . Events ;
1112using System . Collections . Generic ;
1213
1314namespace S1API . UI
1415{
1516 /// <summary>
16- /// Utility class for creating and managing UI elements in Unity projects .
17+ /// Static utility class for dynamically generating and managing UI elements within Unity applications .
1718 /// </summary>
1819 /// <remarks>
19- /// Provides static methods to dynamically generate UI components such as panels, buttons, text blocks, and layouts.
20- /// Includes utilities for configuring and organizing UI elements in a hierarchy.
20+ /// Contains methods for creating reusable and customizable UI components, such as panels, buttons, text elements, layouts,
21+ /// and more. Designed to facilitate rapid development and organization of UI hierarchies, with options
22+ /// for styling and behavior configuration.
2123 /// </remarks>
2224 public static class UIFactory
2325 {
@@ -28,8 +30,9 @@ public static class UIFactory
2830 /// <param name="anchorMin">The minimum anchor point of the RectTransform. Defaults to (0.5, 0.5) if not specified.</param>
2931 /// <param name="anchorMax">The maximum anchor point of the RectTransform. Defaults to (0.5, 0.5) if not specified.</param>
3032 /// <param name="fullAnchor">Whether to stretch the panel across the entire parent RectTransform. Overrides anchorMin and anchorMax if true.</param>
31- /// <returns>The created GameObject representing the panel.</returns>
32- public static GameObject Panel ( string name , Transform parent , Color bgColor , Vector2 ? anchorMin = null , Vector2 ? anchorMax = null , bool fullAnchor = false )
33+ /// <returns>The GameObject representing the created UI panel.</returns>
34+ public static GameObject Panel ( string name , Transform parent , Color bgColor , Vector2 ? anchorMin = null ,
35+ Vector2 ? anchorMax = null , bool fullAnchor = false )
3336 {
3437 GameObject go = new GameObject ( name ) ;
3538 go . transform . SetParent ( parent , false ) ;
@@ -61,7 +64,8 @@ public static GameObject Panel(string name, Transform parent, Color bgColor, Vec
6164 /// <param name="anchor">The alignment of the text within its RectTransform. Defaults to `TextAnchor.UpperLeft`.</param>
6265 /// <param name="style">The font style of the text. Defaults to `FontStyle.Normal`.</param>
6366 /// <returns>The created Text component with the specified properties applied.</returns>
64- public static Text Text ( string name , string content , Transform parent , int fontSize = 14 , TextAnchor anchor = TextAnchor . UpperLeft , FontStyle style = FontStyle . Normal )
67+ public static Text Text ( string name , string content , Transform parent , int fontSize = 14 ,
68+ TextAnchor anchor = TextAnchor . UpperLeft , FontStyle style = FontStyle . Normal )
6569 {
6670 GameObject go = new GameObject ( name ) ;
6771 go . transform . SetParent ( parent , false ) ;
@@ -79,15 +83,11 @@ public static Text Text(string name, string content, Transform parent, int fontS
7983 return txt ;
8084 }
8185
82- /// Creates a scrollable vertical list UI component with its child hierarchy configured for Unity UI.
83- /// The created hierarchy includes:
84- /// - A parent GameObject containing a ScrollRect component.
85- /// - A child "Viewport" GameObject for clipping and masking.
86- /// - A "Content" GameObject inside the viewport with a vertical layout and content size fitter.
86+ /// Creates a scrollable vertical list UI component with a configured child hierarchy, allowing vertical scrolling of dynamically added items.
8787 /// <param name="name">The name of the scrollable list GameObject.</param>
8888 /// <param name="parent">The parent transform where the scrollable list will be added.</param>
8989 /// <param name="scrollRect">Outputs the ScrollRect component associated with the created scrollable list.</param>
90- /// <returns>Returns the RectTransform of the "Content" GameObject, which items can be added to.</returns>
90+ /// <returns>Returns the RectTransform of the "Content" GameObject, allowing items to be added to the scrollable list .</returns>
9191 public static RectTransform ScrollableVerticalList ( string name , Transform parent , out ScrollRect scrollRect )
9292 {
9393 var scrollGO = new GameObject ( name ) ;
@@ -143,18 +143,21 @@ public static void FitContentHeight(RectTransform content)
143143 fitter . verticalFit = ContentSizeFitter . FitMode . PreferredSize ;
144144 }
145145
146- /// Creates a button with a label inside a parent UI element.
146+ /// Creates a button with a label and specified dimensions inside a parent UI element.
147147 /// <param name="name">The name of the button GameObject.</param>
148148 /// <param name="label">The text to display on the button.</param>
149149 /// <param name="parent">The Transform to which the button will be attached.</param>
150150 /// <param name="bgColor">The background color of the button.</param>
151+ /// <param name="Width">The width of the button.</param>
152+ /// <param name="Height">The height of the button.</param>
151153 /// <returns>A tuple containing the button's GameObject, Button component, and Text component.</returns>
152- public static ( GameObject , Button , Text ) ButtonWithLabel ( string name , string label , Transform parent , Color bgColor )
154+ public static ( GameObject , Button , Text ) ButtonWithLabel ( string name , string label , Transform parent ,
155+ Color bgColor , float Width , float Height )
153156 {
154157 GameObject go = new GameObject ( name ) ;
155158 go . transform . SetParent ( parent , false ) ;
156159 var rt = go . AddComponent < RectTransform > ( ) ;
157- rt . sizeDelta = new Vector2 ( 160f , 40f ) ;
160+ rt . sizeDelta = new Vector2 ( Height , Width ) ;
158161
159162 var img = go . AddComponent < Image > ( ) ;
160163 img . color = bgColor ;
@@ -186,8 +189,8 @@ public static (GameObject, Button, Text) ButtonWithLabel(string name, string lab
186189 /// <summary>
187190 /// Sets an icon as a child of the specified parent transform with the given sprite.
188191 /// </summary>
189- /// <param name="sprite">The sprite to use for the icon.</param>
190- /// <param name="parent">The transform to set as the parent of the icon.</param>
192+ /// <param name="sprite">The sprite to be used as the icon.</param>
193+ /// <param name="parent">The transform that will act as the parent of the icon.</param>
191194 public static void SetIcon ( Sprite sprite , Transform parent )
192195 {
193196 var icon = new GameObject ( "Icon" ) ;
@@ -208,24 +211,20 @@ public static void SetIcon(Sprite sprite, Transform parent)
208211 /// <param name="parent">The parent transform where the text block will be added.</param>
209212 /// <param name="title">The title text of the text block, displayed in bold.</param>
210213 /// <param name="subtitle">The subtitle text of the text block, displayed below the title.</param>
211- /// <param name="isCompleted">
212- /// A boolean indicating whether the text block represents a completed state.
213- /// If true, an additional label indicating "Already Delivered" will be added.
214- /// </param>
214+ /// <param name="isCompleted">A boolean indicating whether the text block represents a completed state. If true, an additional label indicating "Already Delivered" will be added.</param>
215215 public static void CreateTextBlock ( Transform parent , string title , string subtitle , bool isCompleted )
216216 {
217217 Text ( parent . name + "Title" , title , parent , 16 , TextAnchor . MiddleLeft , FontStyle . Bold ) ;
218218 Text ( parent . name + "Subtitle" , subtitle , parent , 14 , TextAnchor . UpperLeft ) ;
219219 if ( isCompleted )
220- Text ( "CompletedLabel" , "<color=#888888><i>Already Delivered</i></color>" , parent , 12 , TextAnchor . UpperLeft ) ;
220+ Text ( "CompletedLabel" , "<color=#888888><i>Already Delivered</i></color>" , parent , 12 ,
221+ TextAnchor . UpperLeft ) ;
221222 }
222223
223- /// <summary>
224- /// Adds a clickable button component to the specified game object and sets its interactions and event handling.
225- /// </summary>
224+ /// Adds a button component to the specified game object, sets its target graphic, and configures its interaction settings.
226225 /// <param name="go">The game object to which the button component is added.</param>
227226 /// <param name="clickHandler">The UnityAction to invoke when the button is clicked.</param>
228- /// <param name="enabled">A boolean value indicating whether the button should be interactable.</param>
227+ /// <param name="enabled">Determines whether the button is interactable.</param>
229228 public static void CreateRowButton ( GameObject go , UnityAction clickHandler , bool enabled )
230229 {
231230 var btn = go . AddComponent < Button > ( ) ;
@@ -244,7 +243,7 @@ public static void ClearChildren(Transform parent)
244243 GameObject . Destroy ( child . gameObject ) ;
245244 }
246245
247- /// Configures a GameObject to use a VerticalLayoutGroup with the specified spacing and padding.
246+ /// Configures a GameObject to use a VerticalLayoutGroup with specified spacing and padding.
248247 /// <param name="go">The GameObject to which a VerticalLayoutGroup will be added or configured.</param>
249248 /// <param name="spacing">The spacing between child objects within the VerticalLayoutGroup. Default is 10.</param>
250249 /// <param name="padding">The padding around the edges of the VerticalLayoutGroup. If null, a default RectOffset of (10, 10, 10, 10) will be used.</param>
@@ -255,15 +254,14 @@ public static void VerticalLayoutOnGO(GameObject go, int spacing = 10, RectOffse
255254 layout . padding = padding ?? new RectOffset ( 10 , 10 , 10 , 10 ) ;
256255 }
257256
258- /// <summary>
259257 /// Creates a quest row GameObject with a specific layout, including an icon panel and text panel.
260- /// </summary>
261258 /// <param name="name">The name for the row GameObject.</param>
262259 /// <param name="parent">The parent Transform to attach the row GameObject to.</param>
263260 /// <param name="iconPanel">An output parameter that receives the generated icon panel GameObject.</param>
264261 /// <param name="textPanel">An output parameter that receives the generated text panel GameObject.</param>
265262 /// <returns>The newly created quest row GameObject.</returns>
266- public static GameObject CreateQuestRow ( string name , Transform parent , out GameObject iconPanel , out GameObject textPanel )
263+ public static GameObject CreateQuestRow ( string name , Transform parent , out GameObject iconPanel ,
264+ out GameObject textPanel )
267265 {
268266 // Create the main row object
269267 var row = new GameObject ( "Row_" + name ) ;
@@ -314,12 +312,56 @@ public static GameObject CreateQuestRow(string name, Transform parent, out GameO
314312 return row ;
315313 }
316314
315+ /// Creates a top bar UI element with a title and an optional button.
316+ /// <param name="name">The name of the GameObject representing the top bar.</param>
317+ /// <param name="parent">The transform to which the top bar will be parented.</param>
318+ /// <param name="title">The text content for the title displayed in the top bar.</param>
319+ /// <param name="buttonWidth">The width of the button, if displayed.</param>
320+ /// <param name="buttonHeight">The height of the button, if displayed.</param>
321+ /// <param name="onRightButtonClick">An optional action to be invoked when the button is clicked. If null, the button will not be created.</param>
322+ /// <param name="rightButtonText">The text to display on the optional button. Defaults to "Action" if not specified.</param>
323+ /// <returns>The created GameObject representing the top bar.</returns>
324+ public static GameObject TopBar ( string name , Transform parent , string title , float buttonWidth , float buttonHeight ,
325+ Action onRightButtonClick = null ,
326+ string rightButtonText = "Action" )
327+ {
328+ var topBar = Panel ( name , parent , new Color ( 0.15f , 0.15f , 0.15f ) ,
329+ new Vector2 ( 0f , 0.85f ) , new Vector2 ( 1f , 1f ) ) ;
330+
331+ var layout = topBar . AddComponent < HorizontalLayoutGroup > ( ) ;
332+ layout . padding = new RectOffset ( 75 , 75 , 30 , 30 ) ;
333+ layout . spacing = 20 ;
334+ layout . childAlignment = TextAnchor . MiddleCenter ;
335+ layout . childForceExpandWidth = false ;
336+ layout . childForceExpandHeight = true ;
337+
338+ // Title
339+ var titleText = Text ( "TopBarTitle" , title , topBar . transform , 26 , TextAnchor . MiddleLeft , FontStyle . Bold ) ;
340+ var titleLayout = titleText . gameObject . AddComponent < LayoutElement > ( ) ;
341+ titleLayout . minWidth = 300 ;
342+ titleLayout . flexibleWidth = 1 ;
343+
344+ // Button (if any)
345+ if ( onRightButtonClick != null )
346+ {
347+ var ( btnGO , btn , label ) = ButtonWithLabel ( "TopBarButton" , rightButtonText , topBar . transform , new Color ( 0.25f , 0.5f , 1f ) , buttonWidth , buttonHeight ) ;
348+ ButtonUtils . AddListener ( btn , onRightButtonClick ) ;
349+
350+ var btnLayout = btnGO . AddComponent < LayoutElement > ( ) ;
351+ btnLayout . minWidth = buttonWidth ;
352+ btnLayout . preferredHeight = buttonHeight ;
353+ btnLayout . flexibleWidth = 0 ;
354+ }
317355
318- /// Binds an action to the accept button and updates its label text.
319- /// <param name="btn">The button to bind the action to.</param>
320- /// <param name="label">The text label of the button to update.</param>
321- /// <param name="text">The new text to display on the label.</param>
322- /// <param name="callback">The action to invoke when the button is clicked.</param>
356+ return topBar ;
357+ }
358+
359+
360+ /// Binds an action to a button and updates its label text.
361+ /// <param name="btn">The button to which the action will be bound.</param>
362+ /// <param name="label">The text label associated with the button.</param>
363+ /// <param name="text">The text to set as the label of the button.</param>
364+ /// <param name="callback">The action that will be executed when the button is clicked.</param>
323365 public static void BindAcceptButton ( Button btn , Text label , string text , UnityAction callback )
324366 {
325367 label . text = text ;
@@ -332,28 +374,27 @@ public static void BindAcceptButton(Button btn, Text label, string text, UnityAc
332374/// <summary>
333375/// Represents a handler that encapsulates a callback action to be invoked when a click event occurs.
334376/// </summary>
377+ /// <remarks>
378+ /// This class provides a mechanism to handle and execute logic when a click event is triggered.
379+ /// It associates an action defined by a UnityAction delegate with the click event.
380+ /// </remarks>
335381public class ClickHandler
336382{
337383 /// <summary>
338- /// A private field that stores the UnityAction delegate to be invoked when a click event occurs .
384+ /// A private field that stores the UnityAction delegate to be invoked during a specific click event.
339385 /// </summary>
340386 private readonly UnityAction _callback ;
341387
342- /// <summary>
343- /// Handles click events by invoking a specified callback action.
344- /// </summary>
388+ /// Represents a handler that encapsulates a callback action to be invoked when a click event occurs.
345389 public ClickHandler ( UnityAction callback )
346390 {
347391 _callback = callback ;
348392 }
349393
350- /// <summary>
351- /// Invokes the callback action associated with the click event.
352- /// </summary>
394+ /// Invokes the callback action associated with a click event.
353395 /// <remarks>
354- /// This method triggers the Unity action passed during the initialization
355- /// of the ClickHandler object. It serves as the mechanism to handle click
356- /// events and execute the associated logic.
396+ /// Executes the UnityAction delegate provided during the creation of the ClickHandler instance.
397+ /// This method is used to process and handle click events associated with the handler.
357398 /// </remarks>
358399 public void OnClick ( )
359400 {
0 commit comments