Skip to content

Commit

Permalink
improve TabView
Browse files Browse the repository at this point in the history
  • Loading branch information
in0finite committed Aug 7, 2024
1 parent 9519c16 commit 5be0b5f
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 85 deletions.
3 changes: 2 additions & 1 deletion UGameCore.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "UGameCore",
"rootNamespace": "UGameCore",
"references": [
"GUID:e2bff2fa8d032b63b9d3231c79d6019c"
"GUID:e2bff2fa8d032b63b9d3231c79d6019c",
"GUID:6055be8ebefd69e48b49212b09b47b2f"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
9 changes: 6 additions & 3 deletions UGameCore/Console/ConsoleLogEntry.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ RectTransform:
m_Children:
- {fileID: 9081293583190540610}
m_Father: {fileID: 0}
m_RootOrder: -1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
Expand Down Expand Up @@ -78,6 +77,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 2b7c05e26686e47b5874ea0d528990ab, type: 3}
m_Name:
m_EditorClassIdentifier:
doubleClickDuration: 0.5
--- !u!114 &867307416837962207
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -120,7 +120,11 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 6effe6d24bcc44e10b6b960beaec0706, type: 3}
m_Name:
m_EditorClassIdentifier:
layoutObject: {fileID: 5028219830301639461}
layoutObject: {fileID: 9081293583190540610}
extraWidth: 0
extraHeight: 0
overridePriority: 0
priority: 1
--- !u!114 &8102206722136416101
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -172,7 +176,6 @@ RectTransform:
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 4105868403999074561}
m_RootOrder: -1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
Expand Down
21 changes: 14 additions & 7 deletions UGameCore/Editor/UI/TabViewInspector.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using UnityEngine;
using UnityEditor;
using UGameCore.Utilities.UI;
using System.Linq;

namespace UGameCore.Editor {

[CustomEditor(typeof(TabView))]
namespace UGameCore.Editor
{

[CustomEditor(typeof(TabView))]
[CanEditMultipleObjects]
public class TabViewInspector : UnityEditor.Editor
{
Expand All @@ -25,10 +27,15 @@ public override void OnInspectorGUI()

foreach(TabView tabView in this.targets) {
var list = tabView.GetTabsList ();
list.Clear ();
list.AddRange( tabView.TabsInChildren );
// tabView.ApplyTabsFromList ();
UGameCore.Utilities.Utilities.MarkObjectAsDirty( tabView );
var newList = tabView.TabsInChildren;

if (list.SequenceEqual(newList))
continue;

list.Clear();
list.AddRange(newList);

UGameCore.Utilities.Utilities.MarkObjectAsDirty(tabView);
}

}
Expand Down
38 changes: 27 additions & 11 deletions UGameCore/Utilities/UI/Scripts/Tab.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
using UnityEngine;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace UGameCore.Utilities.UI {


[ExecuteInEditMode]
[DisallowMultipleComponent]
public class Tab : MonoBehaviour {

public TabView tabView;
public RectTransform button ;
public RectTransform panel ;
public RectTransform RootObject => this.panel;

[Tooltip("Optional text component placed before content")]
public TextMeshProUGUI TextBeforeContent;
[Tooltip("Place where all content is inserted")]
public RectTransform ContentParent;
public UnityEvent OnTabActivated;

private bool m_savedOriginalButtonColor = false;
private bool m_savedOriginalButtonColor = false;
private Color m_originalButtonColor = Color.white;
public Color originalButtonColor {
get {
Expand All @@ -24,8 +35,9 @@ public Color originalButtonColor {
public Text buttonTextComponent { get { if(this.button) return this.button.GetComponentInChildren<Text>(); else return null; } }

public Button buttonComponent { get { if(this.button) return this.button.GetComponent<Button>(); else return null; } }
public Button TabButton => this.buttonComponent;

public string tabButtonText {
public string tabButtonText {
get {
if (this.buttonTextComponent)
return this.buttonTextComponent.text;
Expand All @@ -38,20 +50,24 @@ public string tabButtonText {

void Awake()
{

if (this.button)
if (null == this.tabView)
this.tabView = this.transform.parent != null ? this.transform.parent.GetComponent<TabView>() : null;

if (null == this.button)
this.button = this.TryGetComponent(out Button b) ? b.GetRectTransform() : null;

if (this.button != null)
this.SaveOriginalButtonColorIfNeeded ();

}

void Start()
{
// add button click listener which will change active tab
this.buttonComponent.onClick.AddListener( () => { this.tabView.SwitchTab(this); } );

// remember original color of button - maybe it wasn't done in Awake() because button was null
this.SaveOriginalButtonColorIfNeeded ();
if (this.buttonComponent != null)
this.buttonComponent.onClick.AddListener(() => this.tabView.SwitchTab(this));

if (this.button != null)
this.SaveOriginalButtonColorIfNeeded ();
}

void SaveOriginalButtonColorIfNeeded() {
Expand Down
108 changes: 45 additions & 63 deletions UGameCore/Utilities/UI/Scripts/TabView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,24 @@
using System;
using System.Linq;

namespace UGameCore.Utilities.UI {

public class TabView : MonoBehaviour {
namespace UGameCore.Utilities.UI
{
[DisallowMultipleComponent]
public class TabView : MonoBehaviour {


// private RectTransform m_rectTransform = null;
// public RectTransform rectTransform { get { return m_rectTransform; } private set { m_rectTransform = value; } }
public RectTransform rectTransform { get { return this.GetComponent<RectTransform>(); } }
RectTransform rectTransform { get { return this.GetRectTransform(); } }

[SerializeField] private List<Tab> m_tabs = new List<Tab>();

public List<Tab> TabsInChildren { get {
var tabs = new List<Tab> (this.transform.childCount / 2);
foreach (Transform child in this.transform) {
var tab = child.GetComponent<Tab> ();
if (tab != null)
tabs.Add (tab);
}
return tabs;
}
}

/// <summary>
/// List of tabs. You can modify it as you wish, but you need to manually update TabView.
/// </summary>
public List<Tab> GetTabsList() { return m_tabs; }
public Tab[] TabsInChildren => this.gameObject.GetFirstLevelChildrenSingleComponent<Tab>().ToArray();

public int NumTabs { get { return m_tabs.Count; } }
/// <summary>
/// List of tabs. You can modify it as you wish, but you need to manually update TabView.
/// </summary>
public List<Tab> GetTabsList() => m_tabs;

private Tab m_activeTab = null;
public Tab ActiveTab { get { return this.m_activeTab; } }
private Tab m_activeTab = null;
public Tab ActiveTab => m_activeTab;


// public Func<string, RectTransform> createTabPanelFunction ;
Expand All @@ -43,10 +30,6 @@ public List<Tab> TabsInChildren { get {
// public Action<RectTransform> setTabButtonPositionFunction ;
// public Action<RectTransform> setTabPanelPositionFunction ;

public Action<Tab> activateTabFunction ;
public Action<Tab> deactivateTabFunction ;


public event Action<Tab> onTabAdded = delegate {};
public event Action onSwitchedTab = delegate {};

Expand Down Expand Up @@ -83,9 +66,6 @@ private TabView() {
// createTabButtonFunction = CreateTabButton;
// setTabButtonPositionFunction = SetPositionOfTabButton;
// setTabPanelPositionFunction = SetPositionOfTabPanel;
activateTabFunction = ActivateTab;
deactivateTabFunction = DeactivateTab;

}

/// <summary>
Expand Down Expand Up @@ -307,44 +287,46 @@ public void DeleteTabAndHisPanel( Tab tab ) {

public void SwitchTab (Tab newActiveTab) {

if (newActiveTab == m_activeTab)
return;

foreach( var tab in m_tabs.WhereAlive () ) {

if (tab == newActiveTab) {
// this is the new active tab
// activate new tab
activateTabFunction (tab);
} else {
// this is not the active tab
// deactivate it
deactivateTabFunction (tab);
}
// deactivate other tabs
foreach (Tab tab in m_tabs)
{
if (tab != null && tab != newActiveTab)
{
if (tab.RootObject != null)
{
tab.RootObject.gameObject.SetActive(false);
MySetDirty(tab.RootObject.gameObject);
}

if (tab.buttonImageComponent != null)
tab.buttonImageComponent.color = tab.originalButtonColor;
}
}

m_activeTab = newActiveTab;
if (newActiveTab == m_activeTab)
return;

Utilities.InvokeEventExceptionSafe (this.onSwitchedTab);

}
// activate new tab
if (newActiveTab != null)
{
if (newActiveTab.RootObject != null)
{
newActiveTab.RootObject.gameObject.SetActive(true);
MySetDirty(newActiveTab.RootObject.gameObject);
}

public static void ActivateTab (Tab tab) {
tab.panel.gameObject.SetActive (true);
tab.buttonImageComponent.color = tab.tabView.activeTabColor;
if (newActiveTab.buttonImageComponent != null)
newActiveTab.buttonImageComponent.color = this.activeTabColor;
}

MySetDirty (tab.panel.gameObject);
MySetDirty (tab.buttonImageComponent);
}

public static void DeactivateTab (Tab tab) {
tab.panel.gameObject.SetActive (false);
tab.buttonImageComponent.color = tab.originalButtonColor;
m_activeTab = newActiveTab;

MySetDirty (tab.panel.gameObject);
MySetDirty (tab.buttonImageComponent);
}
// notify

this.onSwitchedTab.InvokeEventExceptionSafe();
if (newActiveTab != null)
newActiveTab.OnTabActivated?.Invoke();
}

public void ApplyTabsFromList () {

Expand Down

0 comments on commit 5be0b5f

Please sign in to comment.