mirror of
https://github.com/GrahamKracker/UnityExplorer.git
synced 2025-07-16 00:07:52 +08:00
Make InputFieldRef helper, InteractiveString and IOUtility
This commit is contained in:
135
src/UI/IValues/InteractiveString.cs
Normal file
135
src/UI/IValues/InteractiveString.cs
Normal file
@ -0,0 +1,135 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityExplorer.Core.Config;
|
||||
using UnityExplorer.UI.CacheObject;
|
||||
using UnityExplorer.UI.Widgets;
|
||||
|
||||
namespace UnityExplorer.UI.IValues
|
||||
{
|
||||
public class InteractiveString : InteractiveValue
|
||||
{
|
||||
private string RealValue;
|
||||
public string EditedValue = "";
|
||||
|
||||
public InputFieldRef inputField;
|
||||
public ButtonRef ApplyButton;
|
||||
|
||||
public GameObject SaveFileRow;
|
||||
public InputFieldRef SaveFilePath;
|
||||
|
||||
public override void OnBorrowed(CacheObjectBase owner)
|
||||
{
|
||||
base.OnBorrowed(owner);
|
||||
|
||||
inputField.InputField.readOnly = !owner.CanWrite;
|
||||
ApplyButton.Button.gameObject.SetActive(owner.CanWrite);
|
||||
|
||||
SaveFilePath.Text = Path.Combine(ConfigManager.Default_Output_Path.Value, "untitled.txt");
|
||||
}
|
||||
|
||||
private bool IsStringTooLong(string s)
|
||||
{
|
||||
if (s == null)
|
||||
return false;
|
||||
|
||||
return s.Length >= UIManager.MAX_INPUTFIELD_CHARS;
|
||||
}
|
||||
|
||||
public override void SetValue(object value)
|
||||
{
|
||||
RealValue = value as string;
|
||||
SaveFileRow.SetActive(IsStringTooLong(RealValue));
|
||||
|
||||
if (value == null)
|
||||
{
|
||||
inputField.Text = "";
|
||||
EditedValue = "";
|
||||
}
|
||||
else
|
||||
{
|
||||
EditedValue = (string)value;
|
||||
inputField.Text = EditedValue;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnApplyClicked()
|
||||
{
|
||||
CurrentOwner.SetValueFromIValue(EditedValue);
|
||||
}
|
||||
|
||||
private void OnInputChanged(string input)
|
||||
{
|
||||
EditedValue = input;
|
||||
|
||||
if (IsStringTooLong(EditedValue))
|
||||
{
|
||||
ExplorerCore.LogWarning("InputField length has reached maximum character count!");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnSaveFileClicked()
|
||||
{
|
||||
if (RealValue == null)
|
||||
return;
|
||||
|
||||
if (string.IsNullOrEmpty(SaveFilePath.Text))
|
||||
{
|
||||
ExplorerCore.LogWarning("Cannot save an empty file path!");
|
||||
return;
|
||||
}
|
||||
|
||||
var path = IOUtility.EnsureValid(SaveFilePath.Text);
|
||||
|
||||
if (File.Exists(path))
|
||||
File.Delete(path);
|
||||
|
||||
File.WriteAllText(path, RealValue);
|
||||
}
|
||||
|
||||
public override GameObject CreateContent(GameObject parent)
|
||||
{
|
||||
UIRoot = UIFactory.CreateVerticalGroup(parent, "InteractiveString", false, false, true, true, 3, new Vector4(4, 4, 4, 4),
|
||||
new Color(0.06f, 0.06f, 0.06f));
|
||||
|
||||
// Save to file helper
|
||||
|
||||
SaveFileRow = UIFactory.CreateUIObject("SaveFileRow", UIRoot);
|
||||
UIFactory.SetLayoutElement(SaveFileRow, flexibleWidth: 9999);
|
||||
UIFactory.SetLayoutGroup<VerticalLayoutGroup>(SaveFileRow, false, true, true, true, 3);
|
||||
|
||||
UIFactory.CreateLabel(SaveFileRow, "Info", "<color=red>String is too long! Save to file if you want to see the full string.</color>",
|
||||
TextAnchor.MiddleLeft);
|
||||
|
||||
var horizRow = UIFactory.CreateUIObject("Horiz", SaveFileRow);
|
||||
UIFactory.SetLayoutGroup<HorizontalLayoutGroup>(horizRow, false, false, true, true, 4);
|
||||
|
||||
var saveButton = UIFactory.CreateButton(horizRow, "SaveButton", "Save file");
|
||||
UIFactory.SetLayoutElement(saveButton.Button.gameObject, minHeight: 25, minWidth: 100, flexibleWidth: 0);
|
||||
saveButton.OnClick += OnSaveFileClicked;
|
||||
|
||||
SaveFilePath = UIFactory.CreateInputField(horizRow, "SaveInput", "...");
|
||||
UIFactory.SetLayoutElement(SaveFilePath.UIRoot, minHeight: 25, flexibleWidth: 9999);
|
||||
|
||||
// Main Input / apply
|
||||
|
||||
ApplyButton = UIFactory.CreateButton(UIRoot, "ApplyButton", "Apply", new Color(0.2f, 0.27f, 0.2f));
|
||||
UIFactory.SetLayoutElement(ApplyButton.Button.gameObject, minHeight: 25, minWidth: 100, flexibleWidth: 0);
|
||||
ApplyButton.OnClick += OnApplyClicked;
|
||||
|
||||
inputField = UIFactory.CreateInputField(UIRoot, "InputField", "empty");
|
||||
inputField.UIRoot.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||
UIFactory.SetLayoutElement(inputField.UIRoot, minHeight: 25, flexibleHeight: 500, flexibleWidth: 9999);
|
||||
inputField.InputField.lineType = InputField.LineType.MultiLineNewline;
|
||||
inputField.OnValueChanged += OnInputChanged;
|
||||
|
||||
return UIRoot;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -9,10 +9,29 @@ using UnityExplorer.UI.ObjectPool;
|
||||
|
||||
namespace UnityExplorer.UI.IValues
|
||||
{
|
||||
public class InteractiveValue : IPooledObject
|
||||
public abstract class InteractiveValue : IPooledObject
|
||||
{
|
||||
public GameObject UIRoot { get; set; }
|
||||
public static Type GetIValueTypeForState(ValueState state)
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
case ValueState.String:
|
||||
return typeof(InteractiveString);
|
||||
//case ValueState.Enum:
|
||||
// return typeof(InteractiveEnum);
|
||||
case ValueState.Collection:
|
||||
return typeof(InteractiveList);
|
||||
case ValueState.Dictionary:
|
||||
return typeof(InteractiveDictionary);
|
||||
//case ValueState.ValueStruct:
|
||||
// return typeof(InteractiveValueStruct);
|
||||
//case ValueState.Color:
|
||||
// return typeof(InteractiveColor);
|
||||
default: return typeof(InteractiveValue);
|
||||
}
|
||||
}
|
||||
|
||||
public GameObject UIRoot { get; set; }
|
||||
public float DefaultHeight => -1f;
|
||||
|
||||
public virtual bool CanWrite => this.CurrentOwner.CanWrite;
|
||||
@ -20,30 +39,6 @@ namespace UnityExplorer.UI.IValues
|
||||
public CacheObjectBase CurrentOwner => m_owner;
|
||||
private CacheObjectBase m_owner;
|
||||
|
||||
//public object EditedValue { get; private set; }
|
||||
|
||||
public virtual void SetLayout() { }
|
||||
|
||||
public static Type GetIValueTypeForState(ValueState state)
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
//case ValueState.String:
|
||||
// return null;
|
||||
//case ValueState.Enum:
|
||||
// return null;
|
||||
case ValueState.Collection:
|
||||
return typeof(InteractiveList);
|
||||
case ValueState.Dictionary:
|
||||
return typeof(InteractiveDictionary);
|
||||
//case ValueState.ValueStruct:
|
||||
// return null;
|
||||
//case ValueState.Color:
|
||||
// return null;
|
||||
default: return typeof(InteractiveValue);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void OnBorrowed(CacheObjectBase owner)
|
||||
{
|
||||
if (this.m_owner != null)
|
||||
@ -63,26 +58,26 @@ namespace UnityExplorer.UI.IValues
|
||||
this.m_owner = null;
|
||||
}
|
||||
|
||||
public virtual void SetValue(object value) { }
|
||||
public abstract void SetValue(object value);
|
||||
|
||||
//public virtual void SetValue(object value)
|
||||
public virtual void SetLayout() { }
|
||||
|
||||
public abstract GameObject CreateContent(GameObject parent);
|
||||
|
||||
//
|
||||
//public virtual GameObject CreateContent(GameObject parent)
|
||||
//{
|
||||
// this.EditedValue = value;
|
||||
// UIRoot = UIFactory.CreateUIObject(this.GetType().Name, parent);
|
||||
// UIRoot.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||
// UIFactory.SetLayoutGroup<VerticalLayoutGroup>(UIRoot, true, true, true, true, 3, childAlignment: TextAnchor.MiddleLeft);
|
||||
//
|
||||
// UIFactory.CreateLabel(UIRoot, "Label", "this is an ivalue", TextAnchor.MiddleLeft);
|
||||
// UIFactory.CreateInputField(UIRoot, "InputFIeld", "...", out var input);
|
||||
// UIFactory.SetLayoutElement(input.gameObject, minHeight: 25, flexibleHeight: 500);
|
||||
// input.lineType = InputField.LineType.MultiLineNewline;
|
||||
// input.gameObject.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||
//
|
||||
// return UIRoot;
|
||||
//}
|
||||
|
||||
public virtual GameObject CreateContent(GameObject parent)
|
||||
{
|
||||
UIRoot = UIFactory.CreateUIObject(this.GetType().Name, parent);
|
||||
UIRoot.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||
UIFactory.SetLayoutGroup<VerticalLayoutGroup>(UIRoot, true, true, true, true, 3, childAlignment: TextAnchor.MiddleLeft);
|
||||
|
||||
UIFactory.CreateLabel(UIRoot, "Label", "this is an ivalue", TextAnchor.MiddleLeft);
|
||||
UIFactory.CreateInputField(UIRoot, "InputFIeld", "...", out var input);
|
||||
UIFactory.SetLayoutElement(input.gameObject, minHeight: 25, flexibleHeight: 500);
|
||||
input.lineType = InputField.LineType.MultiLineNewline;
|
||||
input.gameObject.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||
|
||||
return UIRoot;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user