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