Files
XCharts/Editor/ChildComponents/ThemeDrawer.cs

136 lines
5.3 KiB
C#
Raw Normal View History

2021-01-11 08:54:28 +08:00
using System.IO;
using UnityEditor;
using UnityEngine;
#if dUI_TextMeshPro
using TMPro;
#endif
2022-02-19 22:37:57 +08:00
using XCharts.Runtime;
2021-01-11 08:54:28 +08:00
2021-12-24 13:33:09 +08:00
namespace XCharts.Editor
2021-01-11 08:54:28 +08:00
{
2021-11-23 13:20:07 +08:00
[CustomPropertyDrawer(typeof(ThemeStyle), true)]
public class ThemeStyleDrawer : BasePropertyDrawer
2021-01-11 08:54:28 +08:00
{
public override string ClassName { get { return "Theme"; } }
public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)
{
base.OnGUI(pos, prop, label);
var defaultWidth = pos.width;
var defaultX = pos.x;
2021-11-23 13:20:07 +08:00
var chart = prop.serializedObject.targetObject as BaseChart;
2022-03-09 07:26:15 +08:00
if (MakeComponentFoldout(prop, "m_Show", false, new HeaderMenuInfo("Reset|Reset to theme default color", () =>
2022-05-22 22:17:38 +08:00
{
chart.theme.sharedTheme.ResetTheme();
chart.RefreshAllComponent();
}), new HeaderMenuInfo("Export|Export theme to asset for a new theme", () =>
{
ExportThemeWindow.target = chart;
EditorWindow.GetWindow(typeof(ExportThemeWindow));
}), new HeaderMenuInfo("Sync color to custom|Sync shared theme color to custom color", () =>
{
chart.theme.SyncSharedThemeColorToCustom();
})))
2021-01-11 08:54:28 +08:00
{
++EditorGUI.indentLevel;
2021-11-23 13:20:07 +08:00
var chartNameList = XCThemeMgr.GetAllThemeNames();
2021-01-11 08:54:28 +08:00
var lastIndex = chartNameList.IndexOf(chart.theme.themeName);
var y = pos.y + EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
2021-01-11 08:54:28 +08:00
var selectedIndex = EditorGUI.Popup(new Rect(pos.x, y, pos.width, EditorGUIUtility.singleLineHeight),
2021-11-23 13:20:07 +08:00
"Shared Theme", lastIndex, chartNameList.ToArray());
AddSingleLineHeight();
2021-01-11 08:54:28 +08:00
if (lastIndex != selectedIndex)
{
2021-11-23 13:20:07 +08:00
XCThemeMgr.SwitchTheme(chart, chartNameList[selectedIndex]);
2021-01-11 08:54:28 +08:00
}
2021-11-23 13:20:07 +08:00
PropertyField(prop, "m_SharedTheme");
2022-03-30 08:24:47 +08:00
PropertyField(prop, "m_TransparentBackground");
2021-11-23 13:20:07 +08:00
PropertyField(prop, "m_EnableCustomTheme");
2022-05-22 22:17:38 +08:00
using(new EditorGUI.DisabledScope(!prop.FindPropertyRelative("m_EnableCustomTheme").boolValue))
{
PropertyField(prop, "m_CustomBackgroundColor");
PropertyField(prop, "m_CustomColorPalette");
}
--EditorGUI.indentLevel;
2021-01-11 08:54:28 +08:00
}
}
private void AddPropertyField(Rect pos, SerializedProperty prop, ref float y)
{
float height = EditorGUI.GetPropertyHeight(prop, new GUIContent(prop.displayName), true);
EditorGUI.PropertyField(new Rect(pos.x, y, pos.width, height), prop, true);
y += height + EditorGUIUtility.standardVerticalSpacing;
m_Heights[m_KeyName] += height + EditorGUIUtility.standardVerticalSpacing;
}
}
2021-12-24 13:33:09 +08:00
public class ExportThemeWindow : UnityEditor.EditorWindow
2021-01-11 08:54:28 +08:00
{
public static BaseChart target;
private static ExportThemeWindow window;
private string m_ChartName;
static void Init()
{
2022-05-22 22:17:38 +08:00
window = (ExportThemeWindow) EditorWindow.GetWindow(typeof(ExportThemeWindow), false, "Export Theme", true);
2021-01-11 08:54:28 +08:00
window.minSize = new Vector2(600, 50);
window.maxSize = new Vector2(600, 50);
window.Show();
}
void OnInspectorUpdate()
{
Repaint();
}
private void OnGUI()
{
if (target == null)
{
Close();
return;
}
GUILayout.Space(10);
GUILayout.Label("Input a new name for theme:");
m_ChartName = GUILayout.TextField(m_ChartName);
GUILayout.Space(10);
GUILayout.Label("Export path:");
if (string.IsNullOrEmpty(m_ChartName))
{
GUILayout.Label("Need input a new name.");
}
else
{
2021-11-23 13:20:07 +08:00
GUILayout.Label(XCThemeMgr.GetThemeAssetPath(m_ChartName));
2021-01-11 08:54:28 +08:00
}
GUILayout.Space(20);
if (GUILayout.Button("Export"))
{
if (string.IsNullOrEmpty(m_ChartName))
{
ShowNotification(new GUIContent("ERROR:Need input a new name!"));
}
2021-11-23 13:20:07 +08:00
else if (XCThemeMgr.ContainsTheme(m_ChartName))
2021-01-11 08:54:28 +08:00
{
ShowNotification(new GUIContent("ERROR:The name you entered is already in use!"));
}
2021-11-23 13:20:07 +08:00
else if (IsAssetsExist(XCThemeMgr.GetThemeAssetPath(m_ChartName)))
2021-01-11 08:54:28 +08:00
{
2022-05-22 22:17:38 +08:00
ShowNotification(new GUIContent("ERROR:The asset is exist! \npath=" +
XCThemeMgr.GetThemeAssetPath(m_ChartName)));
2021-01-11 08:54:28 +08:00
}
else
{
2021-11-23 13:20:07 +08:00
XCThemeMgr.ExportTheme(target.theme.sharedTheme, m_ChartName);
2022-05-22 22:17:38 +08:00
ShowNotification(new GUIContent("SUCCESS:The theme is exported. \npath=" +
XCThemeMgr.GetThemeAssetPath(m_ChartName)));
2021-01-11 08:54:28 +08:00
}
}
}
private bool IsAssetsExist(string path)
{
return File.Exists(Application.dataPath + "/../" + path);
}
}
}