using System;
using UnityEditor;
using UnityEngine;
namespace HIKKY.VketCloudSDK.UI.EditorWindow.GUIHelper
{
///
/// ラベルを管理するクラスです。
///
public class Label
{
///タイトルスタイルの定義
private static GUIStyle titleStyle = null;
///ヘッダ1スタイルの定義
private static GUIStyle header1Style = null;
///ヘッダ2スタイルの定義
private static GUIStyle header2Style = null;
///通常スタイルの定義
private static GUIStyle normalStyle = null;
///テキストエリアスタイルの定義
private static GUIStyle _textAreaLabelStyle = null;
///テキストエリアスタイルの定義
private static GUIStyle _labelTextboxStyle = null;
///LabelTextboxのスクロール位置
private static Vector2 _scrollChangeLogPosition = Vector2.zero;
///最後に入力されたテキストを保存
private static string _lastText = "";
///
/// タイトルスタイルのラベルを作成します。
///
public static void Title(string text)
{
if (titleStyle == null)
{
titleStyle = new GUIStyle(EditorStyles.largeLabel);
titleStyle.fontSize = 20;
titleStyle.fontStyle = FontStyle.Bold;
}
EditorGUILayout.LabelField(text, titleStyle, GUILayout.Height(30));
}
public static void Title(string text, params GUILayoutOption[] options)
{
if (titleStyle == null)
{
titleStyle = new GUIStyle(EditorStyles.largeLabel);
titleStyle.fontSize = 20;
titleStyle.fontStyle = FontStyle.Bold;
}
EditorGUILayout.LabelField(text, titleStyle, options);
}
///
/// Header1スタイルのラベルを作成します。
///
public static void Header1(string text)
{
if (header1Style == null)
{
header1Style = new GUIStyle(EditorStyles.largeLabel);
header1Style.fontSize = 16;
header1Style.fontStyle = FontStyle.Bold;
}
EditorGUILayout.LabelField(text, header1Style, GUILayout.Height(26));
}
///
/// Header2スタイルのラベルを作成します。
///
public static void Header2(string text)
{
if (header2Style == null)
{
header2Style = new GUIStyle(EditorStyles.largeLabel);
header2Style.fontSize = 12;
header2Style.fontStyle = FontStyle.Bold;
}
EditorGUILayout.LabelField(text, header2Style);
}
///
/// 通常スタイルのラベルを作成します。
///
public static void Normal(string text)
{
if (normalStyle == null)
{
normalStyle = new GUIStyle(EditorStyles.label);
normalStyle.fontSize = 12;
normalStyle.fontStyle = FontStyle.Normal;
}
EditorGUILayout.LabelField(text, normalStyle);
}
public static void Bold(string text)
{
if (normalStyle == null)
{
normalStyle = new GUIStyle(EditorStyles.label);
normalStyle.fontSize = 12;
normalStyle.fontStyle = FontStyle.Normal;
normalStyle.fontStyle = FontStyle.Bold;
}
EditorGUILayout.LabelField(text, normalStyle);
}
///
/// 通常スタイルのラベルを作成します。
///
public static void WrappedText(string text, params GUILayoutOption[] options)
{
if (normalStyle == null)
{
normalStyle = new GUIStyle(EditorStyles.label);
normalStyle.fontSize = 12;
normalStyle.fontStyle = FontStyle.Normal;
normalStyle.wordWrap = true;
}
EditorGUILayout.LabelField(text, normalStyle, options);
}
///
/// テキストエリアのラベルを作成します。
///
public static void TextArea(string text, params GUILayoutOption[] options)
{
if (_textAreaLabelStyle == null)
{
_textAreaLabelStyle = new GUIStyle(EditorStyles.label)
{
wordWrap = true,
normal = new GUIStyleState
{
background = EditorStyles.textArea.normal.background,
textColor = EditorStyles.label.normal.textColor
},
hover = new GUIStyleState
{
background = EditorStyles.textArea.normal.background,
textColor = EditorStyles.label.normal.textColor
},
focused = new GUIStyleState
{
background = EditorStyles.textArea.normal.background,
textColor = EditorStyles.label.normal.textColor
},
active = new GUIStyleState
{
background = EditorStyles.textArea.normal.background,
textColor = EditorStyles.label.normal.textColor
}
};
}
GUI.SetNextControlName("MyTextArea");
string result = EditorGUILayout.TextArea(_lastText, _textAreaLabelStyle, options);
if (result != _lastText)
{
_lastText = text;
}
else
{
_lastText = text;
}
}
///
/// ログ情報ラベル
///
public static void LabelTextbox(string text, params GUILayoutOption[] options)
{
if (_labelTextboxStyle == null)
{
_labelTextboxStyle = new GUIStyle()
{
richText = true,
wordWrap = true,
normal = new GUIStyleState
{
textColor = EditorGUIUtility.isProSkin ? Color.white : Color.black,
},
};
}
var changeLogStyle = TextStyleMarkdown(text);
using (var scrollChangeLogView = new EditorGUILayout.ScrollViewScope(_scrollChangeLogPosition, "ProgressBarBack", options))
{
_scrollChangeLogPosition = scrollChangeLogView.scrollPosition;
GUILayout.Label(changeLogStyle, _labelTextboxStyle, GUILayout.ExpandHeight(true), GUILayout.ExpandWidth(true));
}
}
///
/// UnityLabelで使えるリッチテキストに変換している
///
///
///
///
private static string TextStyleMarkdown(string text)
{
if (string.IsNullOrEmpty(text)) return "Loading...";
var texts = text.Split('\n');
string res = "";
for (int i = 0; i < texts.Length; i++)
{
if (!string.IsNullOrEmpty(texts[i]))
{
if (EditorGUIUtility.isProSkin) // Dark Mode
{
switch (texts[i].Substring(0, 1))
{
case "#":
if (texts[i].Substring(0, 2) != "##")
texts[i] = $"{texts[i].Substring(1, texts[i].Length - 1)}";
else
texts[i] = $"-{texts[i].Substring(2, texts[i].Length - 2)}";
break;
case "-":
texts[i] = $" ·{texts[i].Substring(1, texts[i].Length - 1)}";
break;
default:
break;
}
}
else
{
switch (texts[i].Substring(0, 1))
{
case "#":
if (texts[i].Substring(0, 2) != "##")
texts[i] = $"{texts[i].Substring(1, texts[i].Length - 1)}";
else
texts[i] = $"-{texts[i].Substring(2, texts[i].Length - 2)}";
break;
case "-":
texts[i] = $" ·{texts[i].Substring(1, texts[i].Length - 1)}";
break;
default:
break;
}
}
}
res += $"{texts[i]}\n";
}
return res;
}
}
}