This commit is contained in:
Sinai 2022-03-21 01:04:44 +11:00
parent 078c2e2b51
commit a6a1a4d046

View File

@ -21,31 +21,28 @@ namespace UnityExplorer.UI.Widgets
public class Texture2DWidget : UnityObjectWidget
{
private Texture2D TextureRef;
private bool textureViewerWanted;
private InputFieldRef textureSavePathInput;
private Image textureImage;
private LayoutElement textureImageLayout;
private ButtonRef textureButton;
private GameObject textureViewer;
private float realWidth;
private float realHeight;
private bool textureViewerWanted;
private ButtonRef toggleButton;
private GameObject textureViewerRoot;
private InputFieldRef savePathInput;
private Image image;
private LayoutElement imageLayout;
public override void OnBorrowed(object target, Type targetType, ReflectionInspector inspector)
{
base.OnBorrowed(target, targetType, inspector);
TextureRef = (Texture2D)target.TryCast(typeof(Texture2D));
textureButton.Component.gameObject.SetActive(true);
TextureRef = target.TryCast<Texture2D>();
realWidth = TextureRef.width;
realHeight = TextureRef.height;
if (this.textureViewer)
this.textureViewer.transform.SetParent(inspector.UIRoot.transform);
if (this.textureViewerRoot)
this.textureViewerRoot.transform.SetParent(inspector.UIRoot.transform);
InspectorPanel.Instance.Dragger.OnFinishResize += OnInspectorFinishResize;
}
@ -56,13 +53,14 @@ namespace UnityExplorer.UI.Widgets
TextureRef = null;
if (textureImage.sprite)
GameObject.Destroy(textureImage.sprite);
if (image.sprite)
GameObject.Destroy(image.sprite);
if (textureViewerWanted)
ToggleTextureViewer();
this.textureViewer.transform.SetParent(Pool<Texture2DWidget>.Instance.InactiveHolder.transform);
if (this.textureViewerRoot)
this.textureViewerRoot.transform.SetParent(Pool<Texture2DWidget>.Instance.InactiveHolder.transform);
base.OnReturnToPool();
}
@ -73,22 +71,22 @@ namespace UnityExplorer.UI.Widgets
{
// disable
textureViewerWanted = false;
textureViewer.SetActive(false);
textureButton.ButtonText.text = "View Texture";
textureViewerRoot.SetActive(false);
toggleButton.ButtonText.text = "View Texture";
ParentInspector.mainContentHolder.SetActive(true);
}
else
{
// enable
if (!textureImage.sprite)
if (!image.sprite)
SetupTextureViewer();
SetImageSize();
textureViewerWanted = true;
textureViewer.SetActive(true);
textureButton.ButtonText.text = "Hide Texture";
textureViewerRoot.SetActive(true);
toggleButton.ButtonText.text = "Hide Texture";
ParentInspector.mainContentHolder.gameObject.SetActive(false);
}
@ -102,11 +100,10 @@ namespace UnityExplorer.UI.Widgets
string name = TextureRef.name;
if (string.IsNullOrEmpty(name))
name = "untitled";
textureSavePathInput.Text = Path.Combine(ConfigManager.Default_Output_Path.Value, $"{name}.png");
savePathInput.Text = Path.Combine(ConfigManager.Default_Output_Path.Value, $"{name}.png");
Sprite sprite = TextureHelper.CreateSprite(TextureRef);
textureImage.sprite = sprite;
image.sprite = sprite;
}
private void OnInspectorFinishResize(RectTransform _)
@ -116,6 +113,9 @@ namespace UnityExplorer.UI.Widgets
private void SetImageSize()
{
if (!imageLayout)
return;
RuntimeHelper.StartCoroutine(SetImageSizeCoro());
}
@ -132,8 +132,8 @@ namespace UnityExplorer.UI.Widgets
// If our image is smaller than the viewport, just use 100% scaling
if (realWidth < rectWidth && realHeight < rectHeight)
{
textureImageLayout.minWidth = realWidth;
textureImageLayout.minHeight = realHeight;
imageLayout.minWidth = realWidth;
imageLayout.minHeight = realHeight;
}
else // we will need to scale down the image to fit
{
@ -144,13 +144,13 @@ namespace UnityExplorer.UI.Widgets
// if width needs to be scaled more than height
if (viewWidthRatio < viewHeightRatio)
{
textureImageLayout.minWidth = realWidth * viewWidthRatio;
textureImageLayout.minHeight = realHeight * viewWidthRatio;
imageLayout.minWidth = realWidth * viewWidthRatio;
imageLayout.minHeight = realHeight * viewWidthRatio;
}
else // if height needs to be scaled more than width
{
textureImageLayout.minWidth = realWidth * viewHeightRatio;
textureImageLayout.minHeight = realHeight * viewHeightRatio;
imageLayout.minWidth = realWidth * viewHeightRatio;
imageLayout.minHeight = realHeight * viewHeightRatio;
}
}
}
@ -159,22 +159,19 @@ namespace UnityExplorer.UI.Widgets
{
if (!TextureRef)
{
ExplorerCore.LogWarning("Ref Texture is null, maybe it was destroyed?");
ExplorerCore.LogWarning("Texture is null, maybe it was destroyed?");
return;
}
if (string.IsNullOrEmpty(textureSavePathInput.Text))
if (string.IsNullOrEmpty(savePathInput.Text))
{
ExplorerCore.LogWarning("Save path cannot be empty!");
return;
}
string path = textureSavePathInput.Text;
string path = savePathInput.Text;
if (!path.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase))
{
ExplorerCore.LogWarning("Desired save path must end with '.png'!");
return;
}
path += ".png";
path = IOUtility.EnsureValidFilePath(path);
@ -201,49 +198,45 @@ namespace UnityExplorer.UI.Widgets
// Button
textureButton = UIFactory.CreateButton(unityObjectRow, "TextureButton", "View Texture", new Color(0.2f, 0.3f, 0.2f));
textureButton.Transform.SetSiblingIndex(0);
UIFactory.SetLayoutElement(textureButton.Component.gameObject, minHeight: 25, minWidth: 150);
textureButton.OnClick += ToggleTextureViewer;
toggleButton = UIFactory.CreateButton(UIRoot, "TextureButton", "View Texture", new Color(0.2f, 0.3f, 0.2f));
toggleButton.Transform.SetSiblingIndex(0);
UIFactory.SetLayoutElement(toggleButton.Component.gameObject, minHeight: 25, minWidth: 150);
toggleButton.OnClick += ToggleTextureViewer;
// Texture viewer
textureViewer = UIFactory.CreateVerticalGroup(uiRoot, "TextureViewer", false, false, true, true, 2, new Vector4(5, 5, 5, 5),
textureViewerRoot = UIFactory.CreateVerticalGroup(uiRoot, "TextureViewer", false, false, true, true, 2, new Vector4(5, 5, 5, 5),
new Color(0.1f, 0.1f, 0.1f), childAlignment: TextAnchor.UpperLeft);
UIFactory.SetLayoutElement(textureViewer, flexibleWidth: 9999, flexibleHeight: 9999);
UIFactory.SetLayoutElement(textureViewerRoot, flexibleWidth: 9999, flexibleHeight: 9999);
// Save helper
GameObject saveRowObj = UIFactory.CreateHorizontalGroup(textureViewer, "SaveRow", false, false, true, true, 2, new Vector4(2, 2, 2, 2),
GameObject saveRowObj = UIFactory.CreateHorizontalGroup(textureViewerRoot, "SaveRow", false, false, true, true, 2, new Vector4(2, 2, 2, 2),
new Color(0.1f, 0.1f, 0.1f));
ButtonRef saveBtn = UIFactory.CreateButton(saveRowObj, "SaveButton", "Save .PNG", new Color(0.2f, 0.25f, 0.2f));
UIFactory.SetLayoutElement(saveBtn.Component.gameObject, minHeight: 25, minWidth: 100, flexibleWidth: 0);
saveBtn.OnClick += OnSaveTextureClicked;
textureSavePathInput = UIFactory.CreateInputField(saveRowObj, "SaveInput", "...");
UIFactory.SetLayoutElement(textureSavePathInput.UIRoot, minHeight: 25, minWidth: 100, flexibleWidth: 9999);
savePathInput = UIFactory.CreateInputField(saveRowObj, "SaveInput", "...");
UIFactory.SetLayoutElement(savePathInput.UIRoot, minHeight: 25, minWidth: 100, flexibleWidth: 9999);
// Actual texture viewer
//GameObject imageViewport = UIFactory.CreateVerticalGroup(textureViewer, "ImageViewport", false, false, true, true);
//imageRect = imageViewport.GetComponent<RectTransform>();
//UIFactory.SetLayoutElement(imageViewport, flexibleWidth: 9999, flexibleHeight: 9999);
GameObject imageViewport = UIFactory.CreateVerticalGroup(textureViewerRoot, "ImageViewport", false, false, true, true,
bgColor: new(1,1,1,0), childAlignment: TextAnchor.MiddleCenter);
UIFactory.SetLayoutElement(imageViewport, flexibleWidth: 9999, flexibleHeight: 9999);
GameObject imageHolder = UIFactory.CreateUIObject("ImageHolder", textureViewer);
textureImageLayout = UIFactory.SetLayoutElement(imageHolder, 1, 1, 0, 0);
imageHolder.AddComponent<Image>().color = Color.clear;
var outline = imageHolder.AddComponent<Outline>();
outline.effectColor = Color.black;
outline.effectDistance = new(2, 2);
GameObject imageHolder = UIFactory.CreateUIObject("ImageHolder", imageViewport);
imageLayout = UIFactory.SetLayoutElement(imageHolder, 1, 1, 0, 0);
var actualImageObj = UIFactory.CreateUIObject("ActualImage", imageHolder);
var actualRect = actualImageObj.GetComponent<RectTransform>();
actualRect.anchorMin = new(0, 0);
actualRect.anchorMax = new(1, 1);
textureImage = actualImageObj.AddComponent<Image>();
image = actualImageObj.AddComponent<Image>();
textureViewer.SetActive(false);
textureViewerRoot.SetActive(false);
return ret;
}