Skip to content

Commit

Permalink
Add typeless AddPageLinkButton method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Haruma-K committed Nov 20, 2022
1 parent 4608bc1 commit 136f0c5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
60 changes: 37 additions & 23 deletions Assets/UnityDebugSheet/Runtime/Core/Scripts/DebugPageBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ public abstract class DebugPageBase : Page, IRecyclerViewCellProvider, IRecycler
private readonly Dictionary<string, ObjectPool<GameObject>> _prefabPools =
new Dictionary<string, ObjectPool<GameObject>>();

private bool _addedOrRemovedInThisFrame;

private int _currentItemId;
private string _overrideTitle;
private GameObject _poolRoot;
private PrefabContainer _prefabContainer;
private RecyclerView _recyclerView;
private bool _addedOrRemovedInThisFrame;

protected abstract string Title { get; }

Expand Down Expand Up @@ -438,10 +439,17 @@ public int AddPickerOption(PickerOptionCellModel model, int priority = 0)
return AddItem(AssetKeys.PickerOption, model, priority);
}

public int AddPageLinkButton(string text, string subText = null, Color? textColor = null,
Color? subTextColor = null, Sprite icon = null, Color? iconColor = null, string titleOverride = null,
Action<DebugPageBase> onLoad = null, int priority = 0)
{
return AddPageLinkButton<DebugPage>(text, subText, textColor, subTextColor, icon, iconColor,
titleOverride, onLoad, priority);
}

public int AddPageLinkButton(Type pageType, string text, string subText = null, Color? textColor = null,
Color? subTextColor = null, Sprite icon = null, Color? iconColor = null,
Action<DebugPageBase> onLoad = null,
int priority = 0)
Color? subTextColor = null, Sprite icon = null, Color? iconColor = null, string titleOverride = null,
Action<DebugPageBase> onLoad = null, int priority = 0)
{
var textModel = new CellTextsModel();
textModel.Text = text;
Expand All @@ -451,12 +459,12 @@ public int AddPageLinkButton(Type pageType, string text, string subText = null,
var iconModel = new CellIconModel();
iconModel.Sprite = icon;
if (iconColor != null) iconModel.Color = iconColor.Value;
return AddPageLinkButton(pageType, textModel, iconModel, onLoad, priority);
return AddPageLinkButton(pageType, textModel, iconModel, titleOverride, onLoad, priority);
}

public int AddPageLinkButton<TPage>(string text, string subText = null, Color? textColor = null,
Color? subTextColor = null, Sprite icon = null, Color? iconColor = null, Action<TPage> onLoad = null,
int priority = 0) where TPage : DebugPageBase
Color? subTextColor = null, Sprite icon = null, Color? iconColor = null, string titleOverride = null,
Action<TPage> onLoad = null, int priority = 0) where TPage : DebugPageBase
{
var textModel = new CellTextsModel();
textModel.Text = text;
Expand All @@ -466,26 +474,30 @@ public int AddPageLinkButton<TPage>(string text, string subText = null, Color? t
var iconModel = new CellIconModel();
iconModel.Sprite = icon;
if (iconColor != null) iconModel.Color = iconColor.Value;
return AddPageLinkButton(textModel, iconModel, onLoad, priority);
return AddPageLinkButton(textModel, iconModel, titleOverride, onLoad, priority);
}

public int AddPageLinkButton(CellTextsModel textModel, CellIconModel iconModel = null,
string titleOverride = null, Action<DebugPageBase> onLoad = null, int priority = 0)
{
return AddPageLinkButton<DebugPage>(textModel, iconModel, titleOverride, onLoad, priority);
}

public int AddPageLinkButton(Type pageType, CellTextsModel textModel, CellIconModel iconModel = null,
Action<DebugPageBase> onLoad = null, int priority = 0)
string titleOverride = null, Action<DebugPageBase> onLoad = null, int priority = 0)
{
return AddPageLinkButton(pageType, null, textModel, iconModel, onLoad, priority);
return AddPageLinkButton(pageType, null, textModel, iconModel, titleOverride, onLoad, priority);
}

public int AddPageLinkButton<TPage>(CellTextsModel textModel, CellIconModel iconModel = null,
Action<TPage> onLoad = null, int priority = 0) where TPage : DebugPageBase
string titleOverride = null, Action<TPage> onLoad = null, int priority = 0) where TPage : DebugPageBase
{
return AddPageLinkButton(null, textModel, iconModel, onLoad, priority);
return AddPageLinkButton(null, textModel, iconModel, titleOverride, onLoad, priority);
}

public int AddPageLinkButton(Type pageType, DebugPageBase prefab, string text, string subText = null,
Color? textColor = null,
Color? subTextColor = null, Sprite icon = null, Color? iconColor = null,
Action<DebugPageBase> onLoad = null,
int priority = 0)
Color? textColor = null, Color? subTextColor = null, Sprite icon = null, Color? iconColor = null,
string titleOverride = null, Action<DebugPageBase> onLoad = null, int priority = 0)
{
var textModel = new CellTextsModel();
textModel.Text = text;
Expand All @@ -495,12 +507,12 @@ public int AddPageLinkButton(Type pageType, DebugPageBase prefab, string text, s
var iconModel = new CellIconModel();
iconModel.Sprite = icon;
if (iconColor != null) iconModel.Color = iconColor.Value;
return AddPageLinkButton(pageType, prefab, textModel, iconModel, onLoad, priority);
return AddPageLinkButton(pageType, prefab, textModel, iconModel, titleOverride, onLoad, priority);
}

public int AddPageLinkButton<TPage>(TPage prefab, string text, string subText = null, Color? textColor = null,
Color? subTextColor = null, Sprite icon = null, Color? iconColor = null, Action<TPage> onLoad = null,
int priority = 0) where TPage : DebugPageBase
Color? subTextColor = null, Sprite icon = null, Color? iconColor = null, string titleOverride = null,
Action<TPage> onLoad = null, int priority = 0) where TPage : DebugPageBase
{
var textModel = new CellTextsModel();
textModel.Text = text;
Expand All @@ -510,12 +522,12 @@ public int AddPageLinkButton<TPage>(TPage prefab, string text, string subText =
var iconModel = new CellIconModel();
iconModel.Sprite = icon;
if (iconColor != null) iconModel.Color = iconColor.Value;
return AddPageLinkButton(prefab, textModel, iconModel, onLoad, priority);
return AddPageLinkButton(prefab, textModel, iconModel, titleOverride, onLoad, priority);
}

public int AddPageLinkButton(Type pageType, DebugPageBase prefab, CellTextsModel textModel,
CellIconModel iconModel = null,
Action<DebugPageBase> onLoad = null, int priority = 0)
CellIconModel iconModel = null, string titleOverride = null, Action<DebugPageBase> onLoad = null,
int priority = 0)
{
var useSubText = textModel != null && !string.IsNullOrEmpty(textModel.SubText);
var useIcon = iconModel != null && iconModel.Sprite != null;
Expand All @@ -541,13 +553,14 @@ public int AddPageLinkButton(Type pageType, DebugPageBase prefab, CellTextsModel

buttonModel.PageType = pageType;
buttonModel.Prefab = prefab;
buttonModel.PageTitleOverride = titleOverride;
buttonModel.OnLoad += onLoad;
buttonModel.ShowArrow = true;
return AddPageLinkButton(buttonModel, priority);
}

public int AddPageLinkButton<TPage>(TPage prefab, CellTextsModel textModel, CellIconModel iconModel = null,
Action<TPage> onLoad = null, int priority = 0) where TPage : DebugPageBase
string titleOverride = null, Action<TPage> onLoad = null, int priority = 0) where TPage : DebugPageBase
{
var useSubText = textModel != null && !string.IsNullOrEmpty(textModel.SubText);
var useIcon = iconModel != null && iconModel.Sprite != null;
Expand All @@ -573,6 +586,7 @@ public int AddPageLinkButton<TPage>(TPage prefab, CellTextsModel textModel, Cell

buttonModel.PageType = typeof(TPage);
buttonModel.Prefab = prefab;
buttonModel.PageTitleOverride = titleOverride;
buttonModel.OnLoad += x => onLoad?.Invoke((TPage)x);
buttonModel.ShowArrow = true;
return AddPageLinkButton(buttonModel, priority);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public sealed class PageLinkButtonCell : Cell<PageLinkButtonCellModel>

protected override void SetModel(PageLinkButtonCellModel model)
{
if (model.PageType == null)
throw new Exception($"{nameof(PageLinkButtonCellModel)}.PageType cannot be null.");
var pageType = model.PageType ?? typeof(DebugPage);

_contentsCanvasGroup.alpha = model.Interactable ? 1.0f : 0.3f;

Expand All @@ -39,9 +38,11 @@ protected override void SetModel(PageLinkButtonCellModel model)
button.onClick.AddListener(() =>
{
if (model.Prefab == null)
DebugSheet.Of(transform).PushPage(model.PageType, true, onLoad: model.InvokeOnLoad);
DebugSheet.Of(transform).PushPage(pageType, true, onLoad: model.InvokeOnLoad,
titleOverride: model.PageTitleOverride);
else
DebugSheet.Of(transform).PushPage(model.PageType, model.Prefab, true, onLoad: model.InvokeOnLoad);
DebugSheet.Of(transform).PushPage(pageType, model.Prefab, true, onLoad: model.InvokeOnLoad,
titleOverride: model.PageTitleOverride);
});

// Height
Expand All @@ -60,6 +61,7 @@ public PageLinkButtonCellModel(bool useSubTextOrIcon)
UseSubTextOrIcon = useSubTextOrIcon;
}

public string PageTitleOverride { get; set; }

public CellIconModel Icon { get; } = new CellIconModel();

Expand Down

0 comments on commit 136f0c5

Please sign in to comment.