mirror of
https://github.com/sinai-dev/UnityExplorer.git
synced 2025-06-16 22:27:45 +08:00
A bit more progress, got a good framework for the UI going now.
This commit is contained in:
parent
88cbd0e970
commit
2da293ab21
@ -1,10 +1,10 @@
|
|||||||
using System.Collections;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using ExplorerBeta.Config;
|
using ExplorerBeta.Config;
|
||||||
using ExplorerBeta.Input;
|
using ExplorerBeta.Input;
|
||||||
using ExplorerBeta.UI;
|
using ExplorerBeta.UI;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.EventSystems;
|
|
||||||
|
|
||||||
namespace ExplorerBeta
|
namespace ExplorerBeta
|
||||||
{
|
{
|
||||||
|
@ -10,14 +10,20 @@ using ExplorerBeta.UI.Shared;
|
|||||||
|
|
||||||
namespace ExplorerBeta.UI.Main
|
namespace ExplorerBeta.UI.Main
|
||||||
{
|
{
|
||||||
// TODO REMAKE THIS
|
|
||||||
|
|
||||||
public class MainMenu
|
public class MainMenu
|
||||||
{
|
{
|
||||||
public static MainMenu Instance { get; set; }
|
public static MainMenu Instance { get; set; }
|
||||||
|
|
||||||
public PanelDragger Dragger { get; private set; }
|
public PanelDragger Dragger { get; private set; }
|
||||||
|
|
||||||
public GameObject MainPanel { get; private set; }
|
public GameObject MainPanel { get; private set; }
|
||||||
|
public GameObject PageViewport { get; private set; }
|
||||||
|
|
||||||
|
// Navbar buttons
|
||||||
|
private Button m_lastNavButtonPressed;
|
||||||
|
private readonly Color m_navButtonNormal = new Color(65f/255f, 66f/255f, 66f/255f);
|
||||||
|
private readonly Color m_navButtonHighlight = new Color(50f/255f, 195f/255f, 50f/255f);
|
||||||
|
private readonly Color m_navButtonSelected = new Color(60f/255f, 120f/255f, 60f/255f);
|
||||||
|
|
||||||
public MainMenu()
|
public MainMenu()
|
||||||
{
|
{
|
||||||
@ -29,150 +35,186 @@ namespace ExplorerBeta.UI.Main
|
|||||||
|
|
||||||
Instance = this;
|
Instance = this;
|
||||||
|
|
||||||
MainPanel = CreateBasePanel("MainMenu");
|
ConstructMenu();
|
||||||
CreateTitleBar();
|
|
||||||
CreateNavbar();
|
|
||||||
CreateViewArea();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update()
|
public void Update()
|
||||||
{
|
{
|
||||||
|
// todo
|
||||||
}
|
}
|
||||||
|
|
||||||
private void TestButtonCallback()
|
#region UI Interaction Callbacks
|
||||||
{
|
|
||||||
//if (EventSystem.current != EventSys)
|
|
||||||
// return;
|
|
||||||
|
|
||||||
var go = EventSystem.current.currentSelectedGameObject;
|
private void OnPressHide()
|
||||||
if (!go)
|
|
||||||
return;
|
|
||||||
|
|
||||||
var name = go.name;
|
|
||||||
if (go.GetComponentInChildren<Text>() is Text text)
|
|
||||||
{
|
|
||||||
name = text.text;
|
|
||||||
}
|
|
||||||
ExplorerCore.Log($"{Time.time} | Pressed {name ?? "null"}");
|
|
||||||
|
|
||||||
if (name == "X")
|
|
||||||
{
|
{
|
||||||
ExplorerCore.ShowMenu = false;
|
ExplorerCore.ShowMenu = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void OnNavButtonPressed(string pageName, Button button)
|
||||||
|
{
|
||||||
|
ExplorerCore.Log($"Pressed '{pageName}'");
|
||||||
|
|
||||||
|
var colors = button.colors;
|
||||||
|
colors.normalColor = m_navButtonSelected;
|
||||||
|
colors.selectedColor = m_navButtonSelected;
|
||||||
|
button.colors = colors;
|
||||||
|
|
||||||
|
if (m_lastNavButtonPressed && m_lastNavButtonPressed != button)
|
||||||
|
{
|
||||||
|
var oldColors = m_lastNavButtonPressed.colors;
|
||||||
|
oldColors.normalColor = m_navButtonNormal;
|
||||||
|
oldColors.selectedColor = m_navButtonNormal;
|
||||||
|
m_lastNavButtonPressed.colors = oldColors;
|
||||||
}
|
}
|
||||||
|
|
||||||
#region UI Generator
|
m_lastNavButtonPressed = button;
|
||||||
|
|
||||||
public virtual GameObject CreateBasePanel(string name)
|
|
||||||
{
|
|
||||||
var basePanel = UIFactory.CreatePanel(UIManager.CanvasRoot.gameObject, name);
|
|
||||||
var panelRect = basePanel.GetComponent<RectTransform>();
|
|
||||||
panelRect.anchorMin = new Vector2(0.327f, 0.0967f);
|
|
||||||
panelRect.anchorMax = new Vector2(0.672f, 0.904f);
|
|
||||||
panelRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 620f);
|
|
||||||
panelRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 800f);
|
|
||||||
|
|
||||||
return basePanel;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CreateTitleBar()
|
#endregion
|
||||||
|
|
||||||
|
#region UI Construction
|
||||||
|
|
||||||
|
private void ConstructMenu()
|
||||||
{
|
{
|
||||||
// Make the horizontal group for window title area
|
MainPanel = UIFactory.CreatePanel(UIManager.CanvasRoot, "MainMenu", out GameObject content);
|
||||||
var titleGroup = UIFactory.CreateHorizontalGroup(MainPanel);
|
|
||||||
var titleRect = titleGroup.GetComponent<RectTransform>();
|
|
||||||
titleRect.anchorMin = new Vector2(0.005f, 0.96f);
|
|
||||||
titleRect.anchorMax = new Vector2(0.995f, 0.994f);
|
|
||||||
titleRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 613);
|
|
||||||
titleRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 30);
|
|
||||||
|
|
||||||
var group = titleGroup.GetComponent<HorizontalLayoutGroup>();
|
var panelRect = MainPanel.GetComponent<RectTransform>();
|
||||||
group.childControlWidth = true;
|
panelRect.anchorMin = new Vector2(0.25f, 0.1f);
|
||||||
//group.childScaleWidth = true;
|
panelRect.anchorMax = new Vector2(0.75f, 0.95f);
|
||||||
group.childForceExpandHeight = true;
|
|
||||||
group.childForceExpandWidth = true;
|
|
||||||
|
|
||||||
// Create window title
|
ConstructTitleBar(content);
|
||||||
var titleLabel = UIFactory.CreateLabel(titleGroup, TextAnchor.MiddleCenter);
|
|
||||||
var labelText = titleLabel.GetComponent<Text>();
|
|
||||||
labelText.text = ExplorerCore.NAME;
|
|
||||||
labelText.fontSize = 15;
|
|
||||||
var labelRect = labelText.GetComponent<RectTransform>();
|
|
||||||
labelRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 575);
|
|
||||||
labelRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 25);
|
|
||||||
|
|
||||||
// Add drag handler (on Title label)
|
ConstructNavbar(content);
|
||||||
Dragger = new PanelDragger(titleLabel.transform.TryCast<RectTransform>(),
|
|
||||||
MainPanel.GetComponent<RectTransform>());
|
|
||||||
|
|
||||||
// Create X Button
|
ConstructMainViewport(content);
|
||||||
var exitBtnObj = UIFactory.CreateButton(titleGroup);
|
|
||||||
var exitBtn = exitBtnObj.GetComponentInChildren<Button>();
|
|
||||||
exitBtn.onClick.AddListener(new Action(TestButtonCallback));
|
|
||||||
var exitBtnText = exitBtnObj.GetComponentInChildren<Text>();
|
|
||||||
exitBtnText.text = "X";
|
|
||||||
exitBtnText.fontSize = 14;
|
|
||||||
var exitBtnRect = exitBtnObj.GetComponent<RectTransform>();
|
|
||||||
exitBtnRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 40f);
|
|
||||||
exitBtnRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 25f);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CreateNavbar()
|
private void ConstructTitleBar(GameObject content)
|
||||||
{
|
{
|
||||||
// Make the horizontal group for the nav bar
|
// Core title bar holder
|
||||||
var navArea = UIFactory.CreateHorizontalGroup(MainPanel);
|
|
||||||
var group = navArea.GetComponent<HorizontalLayoutGroup>();
|
|
||||||
group.childAlignment = TextAnchor.MiddleLeft;
|
|
||||||
group.spacing = 5;
|
|
||||||
group.childForceExpandWidth = true;
|
|
||||||
group.childForceExpandHeight = true;
|
|
||||||
group.childControlWidth = true;
|
|
||||||
group.spacing = 5;
|
|
||||||
|
|
||||||
var padding = new RectOffset();
|
var titleBar = UIFactory.CreateHorizontalGroup(content);
|
||||||
padding.left = 5;
|
|
||||||
padding.right = 5;
|
|
||||||
padding.top = 3;
|
|
||||||
padding.bottom = 3;
|
|
||||||
group.padding = padding;
|
|
||||||
|
|
||||||
var navRect = navArea.GetComponent<RectTransform>();
|
var titleGroup = titleBar.GetComponent<HorizontalLayoutGroup>();
|
||||||
navRect.anchorMin = new Vector2(0.005f, 0.93f);
|
titleGroup.childControlHeight = true;
|
||||||
navRect.anchorMax = new Vector2(0.995f, 0.97f);
|
titleGroup.childControlWidth = true;
|
||||||
var pos = navRect.localPosition;
|
titleGroup.childForceExpandHeight = true;
|
||||||
pos.y = 348;
|
titleGroup.childForceExpandWidth = true;
|
||||||
navRect.localPosition = pos;
|
titleGroup.padding.left = 15;
|
||||||
navRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 613);
|
titleGroup.padding.right = 3;
|
||||||
navRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 30);
|
titleGroup.padding.top = 3;
|
||||||
|
titleGroup.padding.bottom = 3;
|
||||||
|
|
||||||
// Add the buttons for pages (this should be done programmatically)
|
var titleLayout = titleBar.AddComponent<LayoutElement>();
|
||||||
var names = new string[] { "Scenes", "Search", "C# Console", "Options" };
|
titleLayout.minHeight = 35;
|
||||||
for (int i = 0; i < names.Length; i++)
|
titleLayout.flexibleHeight = 0;
|
||||||
|
|
||||||
|
// Explorer label
|
||||||
|
|
||||||
|
var textObj = UIFactory.CreateLabel(titleBar, TextAnchor.MiddleLeft);
|
||||||
|
|
||||||
|
var text = textObj.GetComponent<Text>();
|
||||||
|
text.text = $"<b>Explorer</b> <i>v{ExplorerCore.VERSION}</i>";
|
||||||
|
text.resizeTextForBestFit = true;
|
||||||
|
text.resizeTextMinSize = 12;
|
||||||
|
text.resizeTextMaxSize = 20;
|
||||||
|
|
||||||
|
var textLayout = textObj.AddComponent<LayoutElement>();
|
||||||
|
textLayout.flexibleWidth = 50;
|
||||||
|
|
||||||
|
// Add PanelDragger using the label object
|
||||||
|
|
||||||
|
Dragger = new PanelDragger(titleBar.GetComponent<RectTransform>(), MainPanel.GetComponent<RectTransform>());
|
||||||
|
|
||||||
|
// Hide button
|
||||||
|
|
||||||
|
var hideBtnObj = UIFactory.CreateButton(titleBar);
|
||||||
|
|
||||||
|
var hideBtn = hideBtnObj.GetComponent<Button>();
|
||||||
|
hideBtn.onClick = new Button.ButtonClickedEvent();
|
||||||
|
hideBtn.onClick.AddListener(new Action(OnPressHide));
|
||||||
|
var colorBlock = hideBtn.colors;
|
||||||
|
colorBlock.normalColor = new Color(65f/255f, 23f/255f, 23f/255f);
|
||||||
|
colorBlock.pressedColor = new Color(35f/255f, 10f/255f, 10f/255f);
|
||||||
|
colorBlock.highlightedColor = new Color(156f/255f, 0f, 0f);
|
||||||
|
hideBtn.colors = colorBlock;
|
||||||
|
|
||||||
|
var btnLayout = hideBtnObj.AddComponent<LayoutElement>();
|
||||||
|
btnLayout.minWidth = 90;
|
||||||
|
btnLayout.flexibleWidth = 2;
|
||||||
|
|
||||||
|
var hideText = hideBtnObj.GetComponentInChildren<Text>();
|
||||||
|
// Todo use actual keycode from mod config, update on OnSettingsChanged or whatever
|
||||||
|
hideText.text = "Hide (F7)";
|
||||||
|
hideText.color = Color.white;
|
||||||
|
hideText.resizeTextForBestFit = true;
|
||||||
|
hideText.resizeTextMinSize = 8;
|
||||||
|
hideText.resizeTextMaxSize = 16;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ConstructNavbar(GameObject content)
|
||||||
{
|
{
|
||||||
var btn = UIFactory.CreateButton(navArea);
|
// Todo add pages programatically
|
||||||
var text = btn.GetComponentInChildren<Text>();
|
|
||||||
text.text = names[i];
|
|
||||||
|
|
||||||
btn.GetComponent<Button>().onClick.AddListener(new Action(TestButtonCallback));
|
var navbarObj = UIFactory.CreateHorizontalGroup(content);
|
||||||
|
|
||||||
|
var navGroup = navbarObj.GetComponent<HorizontalLayoutGroup>();
|
||||||
|
navGroup.padding.left = 3;
|
||||||
|
navGroup.padding.right = 3;
|
||||||
|
navGroup.padding.top = 3;
|
||||||
|
navGroup.padding.bottom = 3;
|
||||||
|
navGroup.spacing = 5;
|
||||||
|
navGroup.childControlHeight = true;
|
||||||
|
navGroup.childControlWidth = true;
|
||||||
|
navGroup.childForceExpandHeight = true;
|
||||||
|
navGroup.childForceExpandWidth = true;
|
||||||
|
|
||||||
|
var navLayout = navbarObj.AddComponent<LayoutElement>();
|
||||||
|
navLayout.minHeight = 35;
|
||||||
|
navLayout.flexibleHeight = 0;
|
||||||
|
|
||||||
|
// todo use page enum instead
|
||||||
|
var names = new string[] { "Home", "Search", "C# Console", "Options/Misc" };
|
||||||
|
for (int i = 0; i < 4; i++)
|
||||||
|
{
|
||||||
|
var btnObj = UIFactory.CreateButton(navbarObj);
|
||||||
|
var btn = btnObj.GetComponent<Button>();
|
||||||
|
|
||||||
|
var name = names[i];
|
||||||
|
|
||||||
|
btn.onClick.AddListener(new Action(() => { OnNavButtonPressed(name, btn); }));
|
||||||
|
|
||||||
|
var text = btnObj.GetComponentInChildren<Text>();
|
||||||
|
text.text = name;
|
||||||
|
|
||||||
|
// Set button colors
|
||||||
|
var colorBlock = btn.colors;
|
||||||
if (i == 0)
|
if (i == 0)
|
||||||
{
|
{
|
||||||
var image = btn.GetComponentInChildren<Image>();
|
colorBlock.normalColor = m_navButtonSelected;
|
||||||
image.color = new Color(0.1f, 0.35f, 0.1f);
|
m_lastNavButtonPressed = btn;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
colorBlock.normalColor = m_navButtonNormal;
|
||||||
|
}
|
||||||
|
colorBlock.selectedColor = colorBlock.normalColor;
|
||||||
|
colorBlock.highlightedColor = m_navButtonHighlight;
|
||||||
|
colorBlock.pressedColor = m_navButtonSelected;
|
||||||
|
btn.colors = colorBlock;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CreateViewArea()
|
private void ConstructMainViewport(GameObject content)
|
||||||
{
|
{
|
||||||
// Make the vertical group for the viewport
|
var mainObj = UIFactory.CreateHorizontalGroup(content);
|
||||||
var viewGroup = UIFactory.CreateVerticalGroup(MainPanel);
|
var mainGroup = mainObj.GetComponent<HorizontalLayoutGroup>();
|
||||||
var viewRect = viewGroup.GetComponent<RectTransform>();
|
mainGroup.childControlHeight = true;
|
||||||
viewRect.anchorMin = new Vector2(0.005f, -0.038f);
|
mainGroup.childControlWidth = true;
|
||||||
viewRect.anchorMax = new Vector2(0.995f, 0.954f);
|
mainGroup.childForceExpandHeight = true;
|
||||||
viewRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 613);
|
mainGroup.childForceExpandWidth = true;
|
||||||
viewRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 720f);
|
|
||||||
viewRect.localPosition = new Vector3(0, -35f, 0);
|
PageViewport = mainObj;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
@ -42,6 +42,7 @@ namespace ExplorerBeta.UI
|
|||||||
{
|
{
|
||||||
var rawMousePos = InputManager.MousePosition;
|
var rawMousePos = InputManager.MousePosition;
|
||||||
|
|
||||||
|
ResizeTypes type;
|
||||||
var resizePos = Panel.InverseTransformPoint(rawMousePos);
|
var resizePos = Panel.InverseTransformPoint(rawMousePos);
|
||||||
var dragPos = DragableArea.InverseTransformPoint(rawMousePos);
|
var dragPos = DragableArea.InverseTransformPoint(rawMousePos);
|
||||||
|
|
||||||
@ -60,7 +61,8 @@ namespace ExplorerBeta.UI
|
|||||||
}
|
}
|
||||||
else if (MouseInResizeArea(resizePos))
|
else if (MouseInResizeArea(resizePos))
|
||||||
{
|
{
|
||||||
var type = GetResizeType(resizePos);
|
type = GetResizeType(resizePos);
|
||||||
|
if (type != ResizeTypes.NONE)
|
||||||
OnBeginResize(type);
|
OnBeginResize(type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -87,9 +89,8 @@ namespace ExplorerBeta.UI
|
|||||||
{
|
{
|
||||||
OnEndResize();
|
OnEndResize();
|
||||||
}
|
}
|
||||||
else if (MouseInResizeArea(resizePos))
|
else if (MouseInResizeArea(resizePos) && (type = GetResizeType(resizePos)) != ResizeTypes.NONE)
|
||||||
{
|
{
|
||||||
var type = GetResizeType(resizePos);
|
|
||||||
OnHoverResize(type);
|
OnHoverResize(type);
|
||||||
}
|
}
|
||||||
else if (WasHoveringResize)
|
else if (WasHoveringResize)
|
||||||
@ -145,7 +146,7 @@ namespace ExplorerBeta.UI
|
|||||||
private ResizeTypes m_lastResizeHoverType;
|
private ResizeTypes m_lastResizeHoverType;
|
||||||
private GameObject m_resizeCursorImage;
|
private GameObject m_resizeCursorImage;
|
||||||
|
|
||||||
private Rect m_cachedOuterResize;
|
private Rect m_resizeRect;
|
||||||
|
|
||||||
private readonly Dictionary<ResizeTypes, Rect> m_resizeMask = new Dictionary<ResizeTypes, Rect>
|
private readonly Dictionary<ResizeTypes, Rect> m_resizeMask = new Dictionary<ResizeTypes, Rect>
|
||||||
{
|
{
|
||||||
@ -179,47 +180,26 @@ namespace ExplorerBeta.UI
|
|||||||
// to give a bit of buffer and make it easier to use.
|
// to give a bit of buffer and make it easier to use.
|
||||||
|
|
||||||
// outer rect is the outer-most bounds of our resize area
|
// outer rect is the outer-most bounds of our resize area
|
||||||
var outer = new Rect();
|
var outer = new Rect(Panel.rect.x - halfThick,
|
||||||
outer.x = Panel.rect.x - halfThick;
|
Panel.rect.y - halfThick,
|
||||||
outer.y = Panel.rect.y - halfThick;
|
Panel.rect.width + dblThick,
|
||||||
outer.width = Panel.rect.width + dblThick;
|
Panel.rect.height + dblThick);
|
||||||
outer.height = Panel.rect.height + dblThick;
|
m_resizeRect = outer;
|
||||||
m_cachedOuterResize = outer;
|
|
||||||
|
|
||||||
// calculate the four cross sections to use as flags
|
// calculate the four cross sections to use as flags
|
||||||
|
|
||||||
var bottom = new Rect();
|
m_resizeMask[ResizeTypes.Bottom] = new Rect(outer.x, outer.y, outer.width, RESIZE_THICKNESS);
|
||||||
bottom.x = outer.x;
|
|
||||||
bottom.y = outer.y;
|
|
||||||
bottom.width = outer.width;
|
|
||||||
bottom.height = RESIZE_THICKNESS;
|
|
||||||
m_resizeMask[ResizeTypes.Bottom] = bottom;
|
|
||||||
|
|
||||||
var left = new Rect();
|
m_resizeMask[ResizeTypes.Left] = new Rect(outer.x, outer.y, RESIZE_THICKNESS, outer.height);
|
||||||
left.x = outer.x;
|
|
||||||
left.y = outer.y;
|
|
||||||
left.width = RESIZE_THICKNESS;
|
|
||||||
left.height = outer.height;
|
|
||||||
m_resizeMask[ResizeTypes.Left] = left;
|
|
||||||
|
|
||||||
var top = new Rect();
|
m_resizeMask[ResizeTypes.Top] = new Rect(outer.x, outer.y + Panel.rect.height, outer.width, RESIZE_THICKNESS);
|
||||||
top.x = outer.x;
|
|
||||||
top.y = outer.y + Panel.rect.height;
|
|
||||||
top.width = outer.width;
|
|
||||||
top.height = RESIZE_THICKNESS;
|
|
||||||
m_resizeMask[ResizeTypes.Top] = top;
|
|
||||||
|
|
||||||
var right = new Rect();
|
m_resizeMask[ResizeTypes.Right] = new Rect(outer.x + Panel.rect.width, outer.y, RESIZE_THICKNESS, outer.height);
|
||||||
right.x = outer.x + Panel.rect.width;
|
|
||||||
right.y = outer.y;
|
|
||||||
right.width = RESIZE_THICKNESS;
|
|
||||||
right.height = outer.height;
|
|
||||||
m_resizeMask[ResizeTypes.Right] = right;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool MouseInResizeArea(Vector2 mousePos)
|
private bool MouseInResizeArea(Vector2 mousePos)
|
||||||
{
|
{
|
||||||
return m_cachedOuterResize.Contains(mousePos) && GetResizeType(mousePos) != ResizeTypes.NONE;
|
return m_resizeRect.Contains(mousePos);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ResizeTypes GetResizeType(Vector2 mousePos)
|
private ResizeTypes GetResizeType(Vector2 mousePos)
|
||||||
@ -283,13 +263,7 @@ namespace ExplorerBeta.UI
|
|||||||
if (!m_resizeCursorImage)
|
if (!m_resizeCursorImage)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var t =
|
var t = UIManager.CanvasRoot.GetComponent<RectTransform>();
|
||||||
#if CPP
|
|
||||||
UIManager.CanvasRoot.transform.TryCast<RectTransform>();
|
|
||||||
#else
|
|
||||||
UIManager.CanvasRoot.transform as RectTransform;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
m_resizeCursorImage.transform.localPosition = t.InverseTransformPoint(InputManager.MousePosition);
|
m_resizeCursorImage.transform.localPosition = t.InverseTransformPoint(InputManager.MousePosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -357,10 +331,7 @@ namespace ExplorerBeta.UI
|
|||||||
tex.LoadImage(data, false);
|
tex.LoadImage(data, false);
|
||||||
UnityEngine.Object.DontDestroyOnLoad(tex);
|
UnityEngine.Object.DontDestroyOnLoad(tex);
|
||||||
|
|
||||||
var size = new Rect();
|
var sprite = UIManager.CreateSprite(tex);
|
||||||
size.width = 32;
|
|
||||||
size.height = 32;
|
|
||||||
var sprite = UIManager.CreateSprite(tex, size);
|
|
||||||
UnityEngine.Object.DontDestroyOnLoad(sprite);
|
UnityEngine.Object.DontDestroyOnLoad(sprite);
|
||||||
|
|
||||||
m_resizeCursorImage = new GameObject("ResizeCursorImage");
|
m_resizeCursorImage = new GameObject("ResizeCursorImage");
|
||||||
|
@ -12,7 +12,7 @@ namespace ExplorerBeta.UI
|
|||||||
private static Vector2 s_ThickElementSize = new Vector2(160f, 30f);
|
private static Vector2 s_ThickElementSize = new Vector2(160f, 30f);
|
||||||
private static Vector2 s_ThinElementSize = new Vector2(160f, 20f);
|
private static Vector2 s_ThinElementSize = new Vector2(160f, 20f);
|
||||||
//private static Vector2 s_ImageElementSize = new Vector2(100f, 100f);
|
//private static Vector2 s_ImageElementSize = new Vector2(100f, 100f);
|
||||||
private static Color s_DefaultSelectableColor = new Color(0.3f, 0.3f, 0.3f, 1f);
|
private static Color s_DefaultSelectableColor = new Color(1f, 1f, 1f, 1f);
|
||||||
private static Color s_PanelColor = new Color(0.1f, 0.1f, 0.1f, 1.0f);
|
private static Color s_PanelColor = new Color(0.1f, 0.1f, 0.1f, 1.0f);
|
||||||
private static Color s_TextColor = new Color(0.95f, 0.95f, 0.95f, 1f);
|
private static Color s_TextColor = new Color(0.95f, 0.95f, 0.95f, 1f);
|
||||||
|
|
||||||
@ -80,7 +80,7 @@ namespace ExplorerBeta.UI
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static GameObject CreatePanel(GameObject parent, string name)
|
public static GameObject CreatePanel(GameObject parent, string name, out GameObject content)
|
||||||
{
|
{
|
||||||
GameObject panelObj = CreateUIObject($"Panel_{name}", parent, s_ThickElementSize);
|
GameObject panelObj = CreateUIObject($"Panel_{name}", parent, s_ThickElementSize);
|
||||||
|
|
||||||
@ -91,16 +91,43 @@ namespace ExplorerBeta.UI
|
|||||||
rect.sizeDelta = Vector2.zero;
|
rect.sizeDelta = Vector2.zero;
|
||||||
|
|
||||||
Image image = panelObj.AddComponent<Image>();
|
Image image = panelObj.AddComponent<Image>();
|
||||||
image.sprite = UIResources.background;
|
image.type = Image.Type.Filled;
|
||||||
image.type = Image.Type.Sliced;
|
image.color = new Color(0.05f, 0.05f, 0.05f);
|
||||||
image.color = s_PanelColor;
|
|
||||||
|
var group = panelObj.AddComponent<VerticalLayoutGroup>();
|
||||||
|
group.padding.left = 3;
|
||||||
|
group.padding.right = 3;
|
||||||
|
group.padding.bottom = 3;
|
||||||
|
group.padding.top = 3;
|
||||||
|
group.childControlHeight = true;
|
||||||
|
group.childControlWidth = true;
|
||||||
|
group.childForceExpandHeight = true;
|
||||||
|
group.childForceExpandWidth = true;
|
||||||
|
|
||||||
|
content = new GameObject("Content");
|
||||||
|
content.transform.parent = panelObj.transform;
|
||||||
|
|
||||||
|
Image image2 = content.AddComponent<Image>();
|
||||||
|
image2.type = Image.Type.Filled;
|
||||||
|
image2.color = new Color(0.1f, 0.1f, 0.1f);
|
||||||
|
|
||||||
|
var group2 = content.AddComponent<VerticalLayoutGroup>();
|
||||||
|
group2.padding.left = 5;
|
||||||
|
group2.padding.right = 5;
|
||||||
|
group2.padding.bottom = 5;
|
||||||
|
group2.padding.top = 5;
|
||||||
|
group2.spacing = 5;
|
||||||
|
group2.childControlHeight = true;
|
||||||
|
group2.childControlWidth = true;
|
||||||
|
group2.childForceExpandHeight = false;
|
||||||
|
group2.childForceExpandWidth = true;
|
||||||
|
|
||||||
return panelObj;
|
return panelObj;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static GameObject CreateVerticalGroup(GameObject parent, Color color = default)
|
public static GameObject CreateVerticalGroup(GameObject parent, Color color = default)
|
||||||
{
|
{
|
||||||
var groupObj = CreateUIObject("HorizontalLayout", parent);
|
var groupObj = CreateUIObject("VerticalLayout", parent);
|
||||||
|
|
||||||
var horiGroup = groupObj.AddComponent<VerticalLayoutGroup>();
|
var horiGroup = groupObj.AddComponent<VerticalLayoutGroup>();
|
||||||
horiGroup.childAlignment = TextAnchor.UpperLeft;
|
horiGroup.childAlignment = TextAnchor.UpperLeft;
|
||||||
@ -233,14 +260,13 @@ namespace ExplorerBeta.UI
|
|||||||
GameObject handleObj = CreateUIObject("Handle", slideAreaObj);
|
GameObject handleObj = CreateUIObject("Handle", slideAreaObj);
|
||||||
|
|
||||||
Image scrollImage = scrollObj.AddComponent<Image>();
|
Image scrollImage = scrollObj.AddComponent<Image>();
|
||||||
scrollImage.sprite = UIResources.background;
|
|
||||||
scrollImage.type = Image.Type.Sliced;
|
scrollImage.type = Image.Type.Sliced;
|
||||||
scrollImage.color = s_DefaultSelectableColor;
|
scrollImage.color = new Color(0.1f, 0.1f, 0.1f);
|
||||||
|
|
||||||
Image handleImage = handleObj.AddComponent<Image>();
|
Image handleImage = handleObj.AddComponent<Image>();
|
||||||
handleImage.sprite = UIResources.standard;
|
handleImage.sprite = UIResources.standard;
|
||||||
handleImage.type = Image.Type.Sliced;
|
handleImage.type = Image.Type.Sliced;
|
||||||
handleImage.color = s_DefaultSelectableColor;
|
handleImage.color = new Color(0.4f, 0.4f, 0.4f);
|
||||||
|
|
||||||
RectTransform slideAreaRect = slideAreaObj.GetComponent<RectTransform>();
|
RectTransform slideAreaRect = slideAreaObj.GetComponent<RectTransform>();
|
||||||
slideAreaRect.sizeDelta = new Vector2(-20f, -20f);
|
slideAreaRect.sizeDelta = new Vector2(-20f, -20f);
|
||||||
@ -396,8 +422,8 @@ namespace ExplorerBeta.UI
|
|||||||
templateImage.type = Image.Type.Sliced;
|
templateImage.type = Image.Type.Sliced;
|
||||||
|
|
||||||
ScrollRect scrollRect = templateObj.AddComponent<ScrollRect>();
|
ScrollRect scrollRect = templateObj.AddComponent<ScrollRect>();
|
||||||
scrollRect.content = (RectTransform)contentObj.transform;
|
scrollRect.content = contentObj.GetComponent<RectTransform>();
|
||||||
scrollRect.viewport = (RectTransform)viewportObj.transform;
|
scrollRect.viewport = viewportObj.GetComponent<RectTransform>();
|
||||||
scrollRect.horizontal = false;
|
scrollRect.horizontal = false;
|
||||||
scrollRect.movementType = ScrollRect.MovementType.Clamped;
|
scrollRect.movementType = ScrollRect.MovementType.Clamped;
|
||||||
scrollRect.verticalScrollbar = scrollbar;
|
scrollRect.verticalScrollbar = scrollbar;
|
||||||
@ -431,14 +457,14 @@ namespace ExplorerBeta.UI
|
|||||||
{
|
{
|
||||||
text = "Option A"
|
text = "Option A"
|
||||||
});
|
});
|
||||||
//dropdown.options.Add(new Dropdown.OptionData
|
dropdown.options.Add(new Dropdown.OptionData
|
||||||
//{
|
{
|
||||||
// text = "Option B"
|
text = "Option B"
|
||||||
//});
|
});
|
||||||
//dropdown.options.Add(new Dropdown.OptionData
|
dropdown.options.Add(new Dropdown.OptionData
|
||||||
//{
|
{
|
||||||
// text = "Option C"
|
text = "Option C"
|
||||||
//});
|
});
|
||||||
|
|
||||||
dropdown.RefreshShownValue();
|
dropdown.RefreshShownValue();
|
||||||
|
|
||||||
@ -500,16 +526,33 @@ namespace ExplorerBeta.UI
|
|||||||
return dropdownObj;
|
return dropdownObj;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static GameObject CreateScrollView(GameObject parent)
|
public static GameObject CreateScrollView(GameObject parent, out GameObject content)
|
||||||
{
|
{
|
||||||
GameObject scrollObj = CreateUIObject("Scroll View", parent, new Vector2(200f, 200f));
|
GameObject scrollObj = CreateUIObject("Scroll View", parent);
|
||||||
|
|
||||||
GameObject viewportObj = CreateUIObject("Viewport", scrollObj);
|
GameObject viewportObj = CreateUIObject("Viewport", scrollObj);
|
||||||
GameObject contentObj = CreateUIObject("Content", viewportObj);
|
|
||||||
|
var viewportGroup = viewportObj.AddComponent<VerticalLayoutGroup>();
|
||||||
|
viewportGroup.childControlHeight = true;
|
||||||
|
viewportGroup.childControlWidth = true;
|
||||||
|
viewportGroup.childForceExpandHeight = true;
|
||||||
|
viewportGroup.childForceExpandWidth = true;
|
||||||
|
|
||||||
|
content = CreateUIObject("Content", viewportObj);
|
||||||
|
|
||||||
|
var contentGroup = content.AddComponent<VerticalLayoutGroup>();
|
||||||
|
contentGroup.padding.left = 5;
|
||||||
|
contentGroup.padding.right = 5;
|
||||||
|
contentGroup.padding.top = 5;
|
||||||
|
contentGroup.padding.bottom = 5;
|
||||||
|
contentGroup.childControlHeight = false;
|
||||||
|
contentGroup.childControlWidth = true;
|
||||||
|
contentGroup.childForceExpandHeight = false;
|
||||||
|
contentGroup.childForceExpandWidth = true;
|
||||||
|
|
||||||
GameObject horiScroll = CreateScrollbar(scrollObj);
|
GameObject horiScroll = CreateScrollbar(scrollObj);
|
||||||
horiScroll.name = "Scrollbar Horizontal";
|
horiScroll.name = "Scrollbar Horizontal";
|
||||||
//SetParentAndAlign(scrollbarObj, scrollObj);
|
SetParentAndAlign(horiScroll, scrollObj);
|
||||||
|
|
||||||
RectTransform horiRect = horiScroll.GetComponent<RectTransform>();
|
RectTransform horiRect = horiScroll.GetComponent<RectTransform>();
|
||||||
horiRect.anchorMin = Vector2.zero;
|
horiRect.anchorMin = Vector2.zero;
|
||||||
@ -519,7 +562,7 @@ namespace ExplorerBeta.UI
|
|||||||
|
|
||||||
GameObject vertScroll = CreateScrollbar(scrollObj);
|
GameObject vertScroll = CreateScrollbar(scrollObj);
|
||||||
vertScroll.name = "Scrollbar Vertical";
|
vertScroll.name = "Scrollbar Vertical";
|
||||||
//SetParentAndAlign(vertScroll, scrollObj);
|
SetParentAndAlign(vertScroll, scrollObj);
|
||||||
vertScroll.GetComponent<Scrollbar>().SetDirection(Scrollbar.Direction.BottomToTop, true);
|
vertScroll.GetComponent<Scrollbar>().SetDirection(Scrollbar.Direction.BottomToTop, true);
|
||||||
|
|
||||||
RectTransform vertRect = vertScroll.GetComponent<RectTransform>();
|
RectTransform vertRect = vertScroll.GetComponent<RectTransform>();
|
||||||
@ -534,10 +577,9 @@ namespace ExplorerBeta.UI
|
|||||||
viewportRect.sizeDelta = Vector2.zero;
|
viewportRect.sizeDelta = Vector2.zero;
|
||||||
viewportRect.pivot = Vector2.up;
|
viewportRect.pivot = Vector2.up;
|
||||||
|
|
||||||
RectTransform contentRect = contentObj.GetComponent<RectTransform>();
|
RectTransform contentRect = content.GetComponent<RectTransform>();
|
||||||
contentRect.anchorMin = Vector2.up;
|
contentRect.anchorMin = Vector2.up;
|
||||||
contentRect.anchorMax = Vector2.one;
|
contentRect.anchorMax = Vector2.one;
|
||||||
contentRect.sizeDelta = new Vector2(0f, 300f);
|
|
||||||
contentRect.pivot = Vector2.up;
|
contentRect.pivot = Vector2.up;
|
||||||
|
|
||||||
ScrollRect scrollRect = scrollObj.AddComponent<ScrollRect>();
|
ScrollRect scrollRect = scrollObj.AddComponent<ScrollRect>();
|
||||||
@ -549,6 +591,7 @@ namespace ExplorerBeta.UI
|
|||||||
scrollRect.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport;
|
scrollRect.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport;
|
||||||
scrollRect.horizontalScrollbarSpacing = -3f;
|
scrollRect.horizontalScrollbarSpacing = -3f;
|
||||||
scrollRect.verticalScrollbarSpacing = -3f;
|
scrollRect.verticalScrollbarSpacing = -3f;
|
||||||
|
scrollRect.scrollSensitivity = 25;
|
||||||
|
|
||||||
Image scrollImage = scrollObj.AddComponent<Image>();
|
Image scrollImage = scrollObj.AddComponent<Image>();
|
||||||
scrollImage.sprite = UIResources.background;
|
scrollImage.sprite = UIResources.background;
|
||||||
|
@ -20,7 +20,7 @@ namespace ExplorerBeta.UI
|
|||||||
public static void Init()
|
public static void Init()
|
||||||
{
|
{
|
||||||
var res = UIFactory.UIResources = new UIFactory.Resources();
|
var res = UIFactory.UIResources = new UIFactory.Resources();
|
||||||
var bg = CreateSprite(MakeSolidTexture(new Color(0.1f, 0.1f, 0.1f), 1, 1), new Rect(0, 0, 1, 1));
|
var bg = CreateSprite(MakeSolidTexture(new Color(0.16f, 0.16f, 0.16f), 1, 1));
|
||||||
res.background = bg;
|
res.background = bg;
|
||||||
|
|
||||||
// Create core UI Canvas and Event System handler
|
// Create core UI Canvas and Event System handler
|
||||||
@ -95,11 +95,15 @@ namespace ExplorerBeta.UI
|
|||||||
// todo
|
// todo
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Sprite CreateSprite(Texture2D tex, Rect size)
|
public static Sprite CreateSprite(Texture2D tex, Rect size = default)
|
||||||
{
|
{
|
||||||
#if CPP
|
#if CPP
|
||||||
var pivot = Vector2.zero;
|
var pivot = Vector2.zero;
|
||||||
var border = Vector4.zero;
|
var border = Vector4.zero;
|
||||||
|
|
||||||
|
if (size == default)
|
||||||
|
size = new Rect(0, 0, tex.width, tex.height);
|
||||||
|
|
||||||
return Sprite.CreateSprite_Injected(tex, ref size, ref pivot, 100f, 0u, SpriteMeshType.Tight, ref border, false);
|
return Sprite.CreateSprite_Injected(tex, ref size, ref pivot, 100f, 0u, SpriteMeshType.Tight, ref border, false);
|
||||||
#else
|
#else
|
||||||
return Sprite.Create(tex, size, Vector2.zero);
|
return Sprite.Create(tex, size, Vector2.zero);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user