Implemented PageHandler proof of concept, fixed something with scrollviews

This commit is contained in:
sinaioutlander 2020-10-24 00:41:55 +11:00
parent 76c578a9ea
commit 25747503cc
5 changed files with 202 additions and 19 deletions

View File

@ -240,6 +240,7 @@
<Compile Include="UI\Main\Pages\SearchPage.cs" /> <Compile Include="UI\Main\Pages\SearchPage.cs" />
<Compile Include="UI\Main\PanelDragger.cs" /> <Compile Include="UI\Main\PanelDragger.cs" />
<Compile Include="UI\InteractiveValue\InteractiveValue.cs" /> <Compile Include="UI\InteractiveValue\InteractiveValue.cs" />
<Compile Include="UI\Shared\PageHandler.cs" />
<Compile Include="UI\Shared\Syntax.cs" /> <Compile Include="UI\Shared\Syntax.cs" />
<Compile Include="UI\UIManager.cs" /> <Compile Include="UI\UIManager.cs" />
<Compile Include="Unstrip\ImageConversion\ImageConversionUnstrip.cs" /> <Compile Include="Unstrip\ImageConversion\ImageConversionUnstrip.cs" />

View File

@ -65,8 +65,12 @@ namespace ExplorerBeta.UI.Main
if (m_activePage == page || page == null) if (m_activePage == page || page == null)
return; return;
m_activePage?.Content?.SetActive(false);
m_activePage = page; m_activePage = page;
m_activePage.Content?.SetActive(true);
var button = page.RefNavbarButton; var button = page.RefNavbarButton;
var colors = button.colors; var colors = button.colors;
@ -87,16 +91,7 @@ namespace ExplorerBeta.UI.Main
#region UI Interaction Callbacks #region UI Interaction Callbacks
private void OnPressHide() // ... none needed yet
{
ExplorerCore.ShowMenu = false;
}
private void OnNavButtonPressed(BaseMenuPage page)
{
ExplorerCore.Log($"Pressed '{page.Name}'");
SetPage(page);
}
#endregion #endregion
@ -159,7 +154,7 @@ namespace ExplorerBeta.UI.Main
var hideBtnObj = UIFactory.CreateButton(titleBar); var hideBtnObj = UIFactory.CreateButton(titleBar);
var hideBtn = hideBtnObj.GetComponent<Button>(); var hideBtn = hideBtnObj.GetComponent<Button>();
hideBtn.onClick.AddListener(new Action(OnPressHide)); hideBtn.onClick.AddListener(new Action(() => { ExplorerCore.ShowMenu = false; }));
var colorBlock = hideBtn.colors; var colorBlock = hideBtn.colors;
colorBlock.normalColor = new Color(65f/255f, 23f/255f, 23f/255f); colorBlock.normalColor = new Color(65f/255f, 23f/255f, 23f/255f);
colorBlock.pressedColor = new Color(35f/255f, 10f/255f, 10f/255f); colorBlock.pressedColor = new Color(35f/255f, 10f/255f, 10f/255f);
@ -207,7 +202,7 @@ namespace ExplorerBeta.UI.Main
page.RefNavbarButton = btn; page.RefNavbarButton = btn;
btn.onClick.AddListener(new Action(() => { OnNavButtonPressed(page); })); btn.onClick.AddListener(new Action(() => { SetPage(page); }));
var text = btnObj.GetComponentInChildren<Text>(); var text = btnObj.GetComponentInChildren<Text>();
text.text = page.Name; text.text = page.Name;

View File

@ -2,8 +2,10 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using ExplorerBeta;
using ExplorerBeta.UI; using ExplorerBeta.UI;
using ExplorerBeta.UI.Main; using ExplorerBeta.UI.Main;
using ExplorerBeta.UI.Shared;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
@ -13,7 +15,7 @@ namespace Explorer.UI.Main.Pages
{ {
public override string Name => "Home"; public override string Name => "Home";
private GameObject m_mainViewport; private PageHandler m_sceneListPages;
public override void Init() public override void Init()
{ {
@ -31,8 +33,8 @@ namespace Explorer.UI.Main.Pages
{ {
var parent = MainMenu.Instance.PageViewport; var parent = MainMenu.Instance.PageViewport;
m_mainViewport = UIFactory.CreateHorizontalGroup(parent); Content = UIFactory.CreateHorizontalGroup(parent);
var mainGroup = m_mainViewport.GetComponent<HorizontalLayoutGroup>(); var mainGroup = Content.GetComponent<HorizontalLayoutGroup>();
mainGroup.padding.left = 3; mainGroup.padding.left = 3;
mainGroup.padding.right = 3; mainGroup.padding.right = 3;
mainGroup.padding.top = 3; mainGroup.padding.top = 3;
@ -43,7 +45,7 @@ namespace Explorer.UI.Main.Pages
mainGroup.childControlHeight = true; mainGroup.childControlHeight = true;
mainGroup.childControlWidth = true; mainGroup.childControlWidth = true;
var leftPaneObj = UIFactory.CreateVerticalGroup(m_mainViewport, new Color(72f / 255f, 72f / 255f, 72f / 255f)); var leftPaneObj = UIFactory.CreateVerticalGroup(Content, new Color(72f / 255f, 72f / 255f, 72f / 255f));
var leftLayout = leftPaneObj.AddComponent<LayoutElement>(); var leftLayout = leftPaneObj.AddComponent<LayoutElement>();
leftLayout.minWidth = 350; leftLayout.minWidth = 350;
leftLayout.flexibleWidth = 0; leftLayout.flexibleWidth = 0;
@ -59,7 +61,7 @@ namespace Explorer.UI.Main.Pages
leftGroup.childForceExpandWidth = false; leftGroup.childForceExpandWidth = false;
leftGroup.childForceExpandHeight = true; leftGroup.childForceExpandHeight = true;
var rightPaneObj = UIFactory.CreateVerticalGroup(m_mainViewport, new Color(72f / 255f, 72f / 255f, 72f / 255f)); var rightPaneObj = UIFactory.CreateVerticalGroup(Content, new Color(72f / 255f, 72f / 255f, 72f / 255f));
var rightLayout = rightPaneObj.AddComponent<LayoutElement>(); var rightLayout = rightPaneObj.AddComponent<LayoutElement>();
rightLayout.flexibleWidth = 999999; rightLayout.flexibleWidth = 999999;
@ -68,6 +70,28 @@ namespace Explorer.UI.Main.Pages
rightGroup.childForceExpandWidth = true; rightGroup.childForceExpandWidth = true;
rightGroup.childControlHeight = true; rightGroup.childControlHeight = true;
rightGroup.childControlWidth = true; rightGroup.childControlWidth = true;
ConstructScenePane(leftPaneObj);
}
private void ConstructScenePane(GameObject leftPane)
{
m_sceneListPages = new PageHandler(100);
m_sceneListPages.ConstructUI(leftPane);
m_sceneListPages.OnPageChanged += RefreshSceneObjectList;
var scrollTest = UIFactory.CreateScrollView(leftPane, out GameObject content);
for (int i = 0; i < 50; i++)
{
var obj = UIFactory.CreateLabel(content, TextAnchor.MiddleCenter);
var text = obj.GetComponent<Text>();
text.text = "Hello world " + i;
}
}
private void RefreshSceneObjectList()
{
ExplorerCore.Log("Would update scene list here");
} }
#endregion #endregion

View File

@ -0,0 +1,155 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using ExplorerBeta.Config;
namespace ExplorerBeta.UI.Shared
{
public enum Turn
{
Left,
Right
}
public class PageHandler
{
public PageHandler(int listCount)
{
ListCount = listCount;
m_itemsPerPage = ModConfig.Instance?.Default_Page_Limit ?? 20;
}
// callback for when the page is turned
public event Action OnPageChanged;
// set and maintained by owner of list
public int ListCount { get; set; }
// For now this is just set when the PageHandler is created, based on config.
// At some point I might make it possible to change this after creation again.
private readonly int m_itemsPerPage;
private int m_currentPage;
// the last page index (not using "index" to avoid confusion with next property)
public int LastPage => (int)Math.Ceiling(ListCount / (decimal)m_itemsPerPage) - 1;
// The index of the first element of the current page
public int IndexOffset
{
get
{
int offset = m_currentPage * m_itemsPerPage;
if (offset >= ListCount)
{
offset = 0;
m_currentPage = 0;
}
return offset;
}
}
public void TurnPage(Turn direction)
{
if (direction == Turn.Left)
{
if (m_currentPage > 0)
{
m_currentPage--;
OnPageChanged?.Invoke();
RefreshUI();
}
}
else
{
if (m_currentPage < LastPage)
{
m_currentPage++;
OnPageChanged?.Invoke();
RefreshUI();
}
}
}
#region UI
// TODO
private GameObject m_pageUIHolder;
private Text m_currentPageLabel;
public void Show() => m_pageUIHolder?.SetActive(true);
public void Hide() => m_pageUIHolder?.SetActive(false);
public void ConstructUI(GameObject parent)
{
m_pageUIHolder = UIFactory.CreateHorizontalGroup(parent);
var image = m_pageUIHolder.GetComponent<Image>();
image.color = new Color(0f, 0f, 0f, 0f);
var parentGroup = m_pageUIHolder.GetComponent<HorizontalLayoutGroup>();
parentGroup.childForceExpandHeight = true;
parentGroup.childForceExpandWidth = false;
parentGroup.childControlWidth = true;
parentGroup.childControlHeight = true;
var parentLayout = m_pageUIHolder.AddComponent<LayoutElement>();
parentLayout.minHeight = 45;
parentLayout.preferredHeight = 45;
parentLayout.flexibleHeight = 3;
parentLayout.minWidth = 300;
parentLayout.preferredWidth = 300;
parentLayout.flexibleWidth = 0;
var leftBtnObj = UIFactory.CreateButton(m_pageUIHolder);
var leftBtn = leftBtnObj.GetComponent<Button>();
leftBtn.onClick.AddListener(new Action(() => { TurnPage(Turn.Left); }));
var leftBtnText = leftBtnObj.GetComponentInChildren<Text>();
leftBtnText.text = "<";
var leftBtnLayout = leftBtnObj.AddComponent<LayoutElement>();
leftBtnLayout.flexibleHeight = 0;
leftBtnLayout.flexibleWidth = 0;
leftBtnLayout.minWidth = 30;
leftBtnLayout.preferredWidth = 30;
leftBtnLayout.minHeight = 30;
leftBtnLayout.preferredHeight = 30;
var labelObj = UIFactory.CreateLabel(m_pageUIHolder, TextAnchor.MiddleCenter);
m_currentPageLabel = labelObj.GetComponent<Text>();
m_currentPageLabel.text = "Page 1 / TODO";
var textLayout = labelObj.AddComponent<LayoutElement>();
textLayout.flexibleWidth = 1.5f;
textLayout.preferredWidth = 200;
var rightBtnObj = UIFactory.CreateButton(m_pageUIHolder);
var rightBtn = rightBtnObj.GetComponent<Button>();
rightBtn.onClick.AddListener(new Action(() => { TurnPage(Turn.Right); }));
var rightBtnText = rightBtnObj.GetComponentInChildren<Text>();
rightBtnText.text = ">";
var rightBtnLayout = rightBtnObj.AddComponent<LayoutElement>();
rightBtnLayout.flexibleHeight = 0;
rightBtnLayout.flexibleWidth = 0;
rightBtnLayout.preferredWidth = 30;
rightBtnLayout.minWidth = 30;
rightBtnLayout.minHeight = 30;
rightBtnLayout.preferredHeight = 30;
RefreshUI();
}
public void RefreshUI()
{
m_currentPageLabel.text = $"Page {m_currentPage + 1} / {LastPage + 1}";
}
#endregion
}
}

View File

@ -55,9 +55,11 @@ namespace ExplorerBeta.UI
private static void SetDefaultColorTransitionValues(Selectable slider) private static void SetDefaultColorTransitionValues(Selectable slider)
{ {
ColorBlock colors = slider.colors; ColorBlock colors = slider.colors;
colors.highlightedColor = new Color(0.4f, 0.4f, 0.4f); colors.normalColor = new Color(0.3f, 0.3f, 0.3f);
colors.pressedColor = new Color(0.05f, 0.05f, 0.05f); colors.highlightedColor = new Color(0.45f, 0.45f, 0.45f);
colors.pressedColor = new Color(0.1f, 0.1f, 0.1f);
colors.disabledColor = new Color(0.7f, 0.7f, 0.7f); colors.disabledColor = new Color(0.7f, 0.7f, 0.7f);
slider.colors = colors;
} }
private static void SetParentAndAlign(GameObject child, GameObject parent) private static void SetParentAndAlign(GameObject child, GameObject parent)
@ -530,6 +532,12 @@ namespace ExplorerBeta.UI
{ {
GameObject scrollObj = CreateUIObject("Scroll View", parent); GameObject scrollObj = CreateUIObject("Scroll View", parent);
var mainLayout = scrollObj.AddComponent<LayoutElement>();
mainLayout.flexibleWidth = 999;
mainLayout.flexibleHeight = 999;
mainLayout.preferredHeight = 200;
mainLayout.preferredWidth = 200;
GameObject viewportObj = CreateUIObject("Viewport", scrollObj); GameObject viewportObj = CreateUIObject("Viewport", scrollObj);
var viewportGroup = viewportObj.AddComponent<VerticalLayoutGroup>(); var viewportGroup = viewportObj.AddComponent<VerticalLayoutGroup>();