Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit a6a1a4d

Browse files
committed
cleanup
1 parent 078c2e2 commit a6a1a4d

File tree

1 file changed

+50
-57
lines changed

1 file changed

+50
-57
lines changed

src/UI/Widgets/UnityObjects/Texture2DWidget.cs

Lines changed: 50 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,28 @@ namespace UnityExplorer.UI.Widgets
2121
public class Texture2DWidget : UnityObjectWidget
2222
{
2323
private Texture2D TextureRef;
24+
private float realWidth;
25+
private float realHeight;
2426

2527
private bool textureViewerWanted;
28+
private ButtonRef toggleButton;
2629

27-
private InputFieldRef textureSavePathInput;
28-
private Image textureImage;
29-
private LayoutElement textureImageLayout;
30-
31-
private ButtonRef textureButton;
32-
private GameObject textureViewer;
33-
34-
private float realWidth;
35-
private float realHeight;
30+
private GameObject textureViewerRoot;
31+
private InputFieldRef savePathInput;
32+
private Image image;
33+
private LayoutElement imageLayout;
3634

3735
public override void OnBorrowed(object target, Type targetType, ReflectionInspector inspector)
3836
{
3937
base.OnBorrowed(target, targetType, inspector);
4038

41-
TextureRef = (Texture2D)target.TryCast(typeof(Texture2D));
42-
textureButton.Component.gameObject.SetActive(true);
39+
TextureRef = target.TryCast<Texture2D>();
4340

4441
realWidth = TextureRef.width;
4542
realHeight = TextureRef.height;
4643

47-
if (this.textureViewer)
48-
this.textureViewer.transform.SetParent(inspector.UIRoot.transform);
44+
if (this.textureViewerRoot)
45+
this.textureViewerRoot.transform.SetParent(inspector.UIRoot.transform);
4946

5047
InspectorPanel.Instance.Dragger.OnFinishResize += OnInspectorFinishResize;
5148
}
@@ -56,13 +53,14 @@ public override void OnReturnToPool()
5653

5754
TextureRef = null;
5855

59-
if (textureImage.sprite)
60-
GameObject.Destroy(textureImage.sprite);
56+
if (image.sprite)
57+
GameObject.Destroy(image.sprite);
6158

6259
if (textureViewerWanted)
6360
ToggleTextureViewer();
6461

65-
this.textureViewer.transform.SetParent(Pool<Texture2DWidget>.Instance.InactiveHolder.transform);
62+
if (this.textureViewerRoot)
63+
this.textureViewerRoot.transform.SetParent(Pool<Texture2DWidget>.Instance.InactiveHolder.transform);
6664

6765
base.OnReturnToPool();
6866
}
@@ -73,22 +71,22 @@ private void ToggleTextureViewer()
7371
{
7472
// disable
7573
textureViewerWanted = false;
76-
textureViewer.SetActive(false);
77-
textureButton.ButtonText.text = "View Texture";
74+
textureViewerRoot.SetActive(false);
75+
toggleButton.ButtonText.text = "View Texture";
7876

7977
ParentInspector.mainContentHolder.SetActive(true);
8078
}
8179
else
8280
{
8381
// enable
84-
if (!textureImage.sprite)
82+
if (!image.sprite)
8583
SetupTextureViewer();
8684

8785
SetImageSize();
8886

8987
textureViewerWanted = true;
90-
textureViewer.SetActive(true);
91-
textureButton.ButtonText.text = "Hide Texture";
88+
textureViewerRoot.SetActive(true);
89+
toggleButton.ButtonText.text = "Hide Texture";
9290

9391
ParentInspector.mainContentHolder.gameObject.SetActive(false);
9492
}
@@ -102,11 +100,10 @@ private void SetupTextureViewer()
102100
string name = TextureRef.name;
103101
if (string.IsNullOrEmpty(name))
104102
name = "untitled";
105-
106-
textureSavePathInput.Text = Path.Combine(ConfigManager.Default_Output_Path.Value, $"{name}.png");
103+
savePathInput.Text = Path.Combine(ConfigManager.Default_Output_Path.Value, $"{name}.png");
107104

108105
Sprite sprite = TextureHelper.CreateSprite(TextureRef);
109-
textureImage.sprite = sprite;
106+
image.sprite = sprite;
110107
}
111108

112109
private void OnInspectorFinishResize(RectTransform _)
@@ -116,6 +113,9 @@ private void OnInspectorFinishResize(RectTransform _)
116113

117114
private void SetImageSize()
118115
{
116+
if (!imageLayout)
117+
return;
118+
119119
RuntimeHelper.StartCoroutine(SetImageSizeCoro());
120120
}
121121

@@ -132,8 +132,8 @@ IEnumerator SetImageSizeCoro()
132132
// If our image is smaller than the viewport, just use 100% scaling
133133
if (realWidth < rectWidth && realHeight < rectHeight)
134134
{
135-
textureImageLayout.minWidth = realWidth;
136-
textureImageLayout.minHeight = realHeight;
135+
imageLayout.minWidth = realWidth;
136+
imageLayout.minHeight = realHeight;
137137
}
138138
else // we will need to scale down the image to fit
139139
{
@@ -144,13 +144,13 @@ IEnumerator SetImageSizeCoro()
144144
// if width needs to be scaled more than height
145145
if (viewWidthRatio < viewHeightRatio)
146146
{
147-
textureImageLayout.minWidth = realWidth * viewWidthRatio;
148-
textureImageLayout.minHeight = realHeight * viewWidthRatio;
147+
imageLayout.minWidth = realWidth * viewWidthRatio;
148+
imageLayout.minHeight = realHeight * viewWidthRatio;
149149
}
150150
else // if height needs to be scaled more than width
151151
{
152-
textureImageLayout.minWidth = realWidth * viewHeightRatio;
153-
textureImageLayout.minHeight = realHeight * viewHeightRatio;
152+
imageLayout.minWidth = realWidth * viewHeightRatio;
153+
imageLayout.minHeight = realHeight * viewHeightRatio;
154154
}
155155
}
156156
}
@@ -159,22 +159,19 @@ private void OnSaveTextureClicked()
159159
{
160160
if (!TextureRef)
161161
{
162-
ExplorerCore.LogWarning("Ref Texture is null, maybe it was destroyed?");
162+
ExplorerCore.LogWarning("Texture is null, maybe it was destroyed?");
163163
return;
164164
}
165165

166-
if (string.IsNullOrEmpty(textureSavePathInput.Text))
166+
if (string.IsNullOrEmpty(savePathInput.Text))
167167
{
168168
ExplorerCore.LogWarning("Save path cannot be empty!");
169169
return;
170170
}
171171

172-
string path = textureSavePathInput.Text;
172+
string path = savePathInput.Text;
173173
if (!path.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase))
174-
{
175-
ExplorerCore.LogWarning("Desired save path must end with '.png'!");
176-
return;
177-
}
174+
path += ".png";
178175

179176
path = IOUtility.EnsureValidFilePath(path);
180177

@@ -201,49 +198,45 @@ public override GameObject CreateContent(GameObject uiRoot)
201198

202199
// Button
203200

204-
textureButton = UIFactory.CreateButton(unityObjectRow, "TextureButton", "View Texture", new Color(0.2f, 0.3f, 0.2f));
205-
textureButton.Transform.SetSiblingIndex(0);
206-
UIFactory.SetLayoutElement(textureButton.Component.gameObject, minHeight: 25, minWidth: 150);
207-
textureButton.OnClick += ToggleTextureViewer;
201+
toggleButton = UIFactory.CreateButton(UIRoot, "TextureButton", "View Texture", new Color(0.2f, 0.3f, 0.2f));
202+
toggleButton.Transform.SetSiblingIndex(0);
203+
UIFactory.SetLayoutElement(toggleButton.Component.gameObject, minHeight: 25, minWidth: 150);
204+
toggleButton.OnClick += ToggleTextureViewer;
208205

209206
// Texture viewer
210207

211-
textureViewer = UIFactory.CreateVerticalGroup(uiRoot, "TextureViewer", false, false, true, true, 2, new Vector4(5, 5, 5, 5),
208+
textureViewerRoot = UIFactory.CreateVerticalGroup(uiRoot, "TextureViewer", false, false, true, true, 2, new Vector4(5, 5, 5, 5),
212209
new Color(0.1f, 0.1f, 0.1f), childAlignment: TextAnchor.UpperLeft);
213-
UIFactory.SetLayoutElement(textureViewer, flexibleWidth: 9999, flexibleHeight: 9999);
210+
UIFactory.SetLayoutElement(textureViewerRoot, flexibleWidth: 9999, flexibleHeight: 9999);
214211

215212
// Save helper
216213

217-
GameObject saveRowObj = UIFactory.CreateHorizontalGroup(textureViewer, "SaveRow", false, false, true, true, 2, new Vector4(2, 2, 2, 2),
214+
GameObject saveRowObj = UIFactory.CreateHorizontalGroup(textureViewerRoot, "SaveRow", false, false, true, true, 2, new Vector4(2, 2, 2, 2),
218215
new Color(0.1f, 0.1f, 0.1f));
219216

220217
ButtonRef saveBtn = UIFactory.CreateButton(saveRowObj, "SaveButton", "Save .PNG", new Color(0.2f, 0.25f, 0.2f));
221218
UIFactory.SetLayoutElement(saveBtn.Component.gameObject, minHeight: 25, minWidth: 100, flexibleWidth: 0);
222219
saveBtn.OnClick += OnSaveTextureClicked;
223220

224-
textureSavePathInput = UIFactory.CreateInputField(saveRowObj, "SaveInput", "...");
225-
UIFactory.SetLayoutElement(textureSavePathInput.UIRoot, minHeight: 25, minWidth: 100, flexibleWidth: 9999);
221+
savePathInput = UIFactory.CreateInputField(saveRowObj, "SaveInput", "...");
222+
UIFactory.SetLayoutElement(savePathInput.UIRoot, minHeight: 25, minWidth: 100, flexibleWidth: 9999);
226223

227224
// Actual texture viewer
228225

229-
//GameObject imageViewport = UIFactory.CreateVerticalGroup(textureViewer, "ImageViewport", false, false, true, true);
230-
//imageRect = imageViewport.GetComponent<RectTransform>();
231-
//UIFactory.SetLayoutElement(imageViewport, flexibleWidth: 9999, flexibleHeight: 9999);
226+
GameObject imageViewport = UIFactory.CreateVerticalGroup(textureViewerRoot, "ImageViewport", false, false, true, true,
227+
bgColor: new(1,1,1,0), childAlignment: TextAnchor.MiddleCenter);
228+
UIFactory.SetLayoutElement(imageViewport, flexibleWidth: 9999, flexibleHeight: 9999);
232229

233-
GameObject imageHolder = UIFactory.CreateUIObject("ImageHolder", textureViewer);
234-
textureImageLayout = UIFactory.SetLayoutElement(imageHolder, 1, 1, 0, 0);
235-
imageHolder.AddComponent<Image>().color = Color.clear;
236-
var outline = imageHolder.AddComponent<Outline>();
237-
outline.effectColor = Color.black;
238-
outline.effectDistance = new(2, 2);
230+
GameObject imageHolder = UIFactory.CreateUIObject("ImageHolder", imageViewport);
231+
imageLayout = UIFactory.SetLayoutElement(imageHolder, 1, 1, 0, 0);
239232

240233
var actualImageObj = UIFactory.CreateUIObject("ActualImage", imageHolder);
241234
var actualRect = actualImageObj.GetComponent<RectTransform>();
242235
actualRect.anchorMin = new(0, 0);
243236
actualRect.anchorMax = new(1, 1);
244-
textureImage = actualImageObj.AddComponent<Image>();
237+
image = actualImageObj.AddComponent<Image>();
245238

246-
textureViewer.SetActive(false);
239+
textureViewerRoot.SetActive(false);
247240

248241
return ret;
249242
}

0 commit comments

Comments
 (0)