Skip to content

Commit 74ca136

Browse files
committed
impl note authors and types
1 parent 241eb8c commit 74ca136

27 files changed

+1090
-66
lines changed

Editor/LazyNoteEditor.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace DCFApixels.Notes.Editors
5+
{
6+
[CustomEditor(typeof(LazyNote))]
7+
internal class LazyNoteEditor : Editor
8+
{
9+
private Rect rect = new Rect();
10+
private Texture2D _lineTex;
11+
private bool _IsInit = false;
12+
13+
private LazyNote Target => target as LazyNote;
14+
15+
private void Init()
16+
{
17+
if (_IsInit) return;
18+
_lineTex = CreateTexture(2, 2, Color.black);
19+
_IsInit = true;
20+
}
21+
22+
public override void OnInspectorGUI()
23+
{
24+
Init();
25+
Color defaultColor = GUI.color;
26+
Color defaultBackgroundColor = GUI.backgroundColor;
27+
28+
EditorGUI.BeginChangeCheck();
29+
SerializedProperty heightProp = serializedObject.FindProperty("_height");
30+
SerializedProperty textProp = serializedObject.FindProperty("_text");
31+
SerializedProperty colorProp = serializedObject.FindProperty("_color");
32+
SerializedProperty drawIconProp = serializedObject.FindProperty("_drawIcon");
33+
34+
Color color = colorProp.colorValue;
35+
36+
Color elemcolor = NormalizeBackgroundColor(color);
37+
rect = new Rect(0, 0, EditorGUIUtility.currentViewWidth, EditorGUIUtility.singleLineHeight * 2 + heightProp.floatValue + 5);
38+
39+
EditorGUI.DrawRect(rect, color);
40+
41+
GUI.backgroundColor = elemcolor;
42+
43+
GUIStyle areastyle = new GUIStyle(EditorStyles.wordWrappedLabel);
44+
45+
areastyle.fontSize = 14;
46+
areastyle.normal.textColor = Color.black;
47+
areastyle.hover = areastyle.normal;
48+
areastyle.focused = areastyle.normal;
49+
50+
EditorGUILayout.BeginHorizontal();
51+
GUIStyle gUIStyle = new GUIStyle(EditorStyles.label);
52+
gUIStyle.normal.textColor = new Color(0.1f, 0.1f, 0.1f, 0.2f);
53+
54+
drawIconProp.boolValue = EditorGUILayout.Toggle(drawIconProp.boolValue, GUILayout.MaxWidth(16));
55+
GUILayout.Label("", gUIStyle);
56+
57+
float originalValue = EditorGUIUtility.labelWidth;
58+
EditorGUIUtility.labelWidth = 14;
59+
GUI.color = new Color(0.2f, 0.2f, 0.2f);
60+
GUI.backgroundColor = Color.white;
61+
62+
GUIStyle gUIStylex = new GUIStyle(EditorStyles.helpBox);
63+
heightProp.floatValue = EditorGUILayout.FloatField("↕", heightProp.floatValue, gUIStylex, GUILayout.MaxWidth(58));
64+
heightProp.floatValue = Mathf.Max(20f, heightProp.floatValue);
65+
GUI.color = defaultColor;
66+
EditorGUIUtility.labelWidth = originalValue;
67+
68+
Color newColor = EditorGUILayout.ColorField(colorProp.colorValue, GUILayout.MaxWidth(40));
69+
newColor.a = 1f;
70+
colorProp.colorValue = newColor;
71+
72+
EditorGUILayout.EndHorizontal();
73+
74+
GUILayout.Box(_lineTex, GUILayout.Height(1), GUILayout.ExpandWidth(true));
75+
76+
textProp.stringValue = EditorGUILayout.TextArea(textProp.stringValue, areastyle, GUILayout.Height(heightProp.floatValue));
77+
GUI.backgroundColor = defaultBackgroundColor;
78+
79+
serializedObject.ApplyModifiedProperties();
80+
EditorGUI.EndChangeCheck();
81+
}
82+
public override void DrawPreview(Rect previewArea)
83+
{
84+
rect = previewArea;
85+
base.DrawPreview(previewArea);
86+
}
87+
private static Color NormalizeBackgroundColor(Color color)
88+
{
89+
Color.RGBToHSV(color, out float H, out float S, out float V);
90+
S -= S * 0.62f;
91+
return Color.HSVToRGB(H, S, V) * 3f;
92+
}
93+
private static Texture2D CreateTexture(int width, int height, Color32 color32)
94+
{
95+
var pixels = new Color32[width * height];
96+
for (var i = 0; i < pixels.Length; ++i)
97+
pixels[i] = color32;
98+
99+
var result = new Texture2D(width, height);
100+
result.SetPixels32(pixels);
101+
result.Apply();
102+
return result;
103+
}
104+
}
105+
}

Editor/LazyNoteEditor.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/NoteEditor.cs

Lines changed: 107 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,109 @@
1-
using System.Reflection;
2-
using UnityEditor;
1+
using UnityEditor;
32
using UnityEngine;
43

54
namespace DCFApixels.Notes.Editors
65
{
7-
using static NoteConsts;
8-
9-
internal static class NoteConsts
10-
{
11-
public const string NOTE_SEPARATOR = ">-<";
12-
}
13-
internal static class NoteUtils
6+
[CustomEditor(typeof(Note))]
7+
[CanEditMultipleObjects]
8+
internal class NoteEditor : Editor
149
{
15-
[MenuItem("GameObject/Create Note")]
16-
public static void CreateNote(MenuCommand menuCommand)
10+
private Rect fullRect = new Rect();
11+
private Texture2D _lineTex;
12+
private bool _IsInit = false;
13+
private static float _headerHeight = 27;
14+
15+
private GUIStyle _settingsButtonStyle;
16+
17+
private Note Target => target as Note;
18+
19+
private GenericMenu _authorsGenericMenu;
20+
private int _authorsGenericMenuCount;
21+
private GenericMenu _typesGenericMenu;
22+
private int _typesGenericMenuCount;
23+
24+
private NotesSettings Settings => NotesSettings.Instance;
25+
26+
27+
public GenericMenu GetAuthorsGenericMenu()
1728
{
18-
GameObject go = new GameObject("Note");
19-
go.AddComponent<Note>();
20-
GameObjectUtility.SetParentAndAlign(go, menuCommand.context as GameObject);
21-
Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
22-
Selection.activeObject = go;
29+
if (_authorsGenericMenu == null || _authorsGenericMenuCount != Settings.AuthorsCount)
30+
{
31+
_authorsGenericMenuCount = Settings.AuthorsCount;
32+
_authorsGenericMenu = new GenericMenu();
33+
foreach (var author in Settings.Authors)
34+
_authorsGenericMenu.AddItem(new GUIContent(author.name), false, OnAuthorSelected, author);
35+
}
36+
return _authorsGenericMenu;
2337
}
24-
25-
[DrawGizmo(GizmoType.Selected | GizmoType.NonSelected | GizmoType.Pickable)]
26-
public static void DrawNote(Note note, GizmoType gizmoType)
38+
private void OnAuthorSelected(object obj)
39+
{
40+
AuthorInfo author = (AuthorInfo)obj;
41+
serializedObject.FindProperty("_authorID").intValue = author._id;
42+
serializedObject.ApplyModifiedProperties();
43+
}
44+
public GenericMenu GetTypesGenericMenu()
2745
{
28-
if (note.DrawIcon)
46+
if (_typesGenericMenu == null || _typesGenericMenuCount != Settings.TypesCount)
2947
{
30-
var assembly = Assembly.GetExecutingAssembly();
31-
var packagePath = UnityEditor.PackageManager.PackageInfo.FindForAssembly(assembly).assetPath;
32-
Gizmos.DrawIcon(note.transform.position, packagePath + "/Gizmos/Runtime/Note Icon.png", false, note.Color);
48+
_typesGenericMenuCount = Settings.TypesCount;
49+
_typesGenericMenu = new GenericMenu();
50+
foreach (var type in Settings.Types)
51+
_typesGenericMenu.AddItem(new GUIContent(type.name), false, OnTypeSelected, type);
3352
}
34-
35-
string sceneNote = GetSceneNote(note.Text, note.DrawIcon);
36-
Handles.Label(note.transform.position, sceneNote, EditorStyles.whiteBoldLabel);
53+
return _typesGenericMenu;
3754
}
38-
39-
private static string GetSceneNote(string fullNote, bool isNeedSpacing)
55+
private void OnTypeSelected(object obj)
4056
{
41-
int index = fullNote.IndexOf(NOTE_SEPARATOR);
42-
if (index < 0) return string.Empty;
43-
string result = fullNote.Substring(0, index);
44-
return isNeedSpacing ? "\r\n" + result : result;
57+
NoteTypeInfo type = (NoteTypeInfo)obj;
58+
serializedObject.FindProperty("_typeID").intValue = type._id;
59+
serializedObject.ApplyModifiedProperties();
4560
}
46-
}
47-
[CustomEditor(typeof(Note))]
48-
internal class NoteEditor : Editor
49-
{
50-
private Rect rect = new Rect();
51-
private Texture2D _lineTex;
52-
private bool _IsInit = false;
53-
54-
private Note Target => target as Note;
5561

5662
private void Init()
5763
{
5864
if (_IsInit) return;
5965
_lineTex = CreateTexture(2, 2, Color.black);
66+
67+
_settingsButtonStyle = new GUIStyle(EditorStyles.miniButton);
68+
_settingsButtonStyle.padding = new RectOffset(0, 0, 0, 0);
69+
6070
_IsInit = true;
6171
}
6272

73+
74+
6375
public override void OnInspectorGUI()
6476
{
6577
Init();
66-
Color defaultColor = GUI.color;
67-
Color defaultBackgroundColor = GUI.backgroundColor;
6878

69-
EditorGUI.BeginChangeCheck();
7079
SerializedProperty heightProp = serializedObject.FindProperty("_height");
7180
SerializedProperty textProp = serializedObject.FindProperty("_text");
72-
SerializedProperty colorProp = serializedObject.FindProperty("_color");
81+
SerializedProperty authorProp = serializedObject.FindProperty("_authorID");
82+
SerializedProperty typeProp = serializedObject.FindProperty("_typeID");
7383
SerializedProperty drawIconProp = serializedObject.FindProperty("_drawIcon");
7484

75-
Color color = colorProp.colorValue;
85+
Color defaultColor = GUI.color;
86+
Color defaultBackgroundColor = GUI.backgroundColor;
87+
88+
AuthorInfo author = Settings.GetAuthorInfoOrDummy(authorProp.intValue);
89+
NoteTypeInfo noteType = Settings.GetNoteTypeInfoOrDummy(typeProp.intValue);
90+
Color headerColor = author.color;
91+
Color bodyColor = noteType.color;
92+
93+
EditorGUI.BeginChangeCheck();
94+
95+
Color headerBackColor = NormalizeBackgroundColor(headerColor);
7696

77-
Color elemcolor = NormalizeBackgroundColor(color);
78-
rect = new Rect(0, 0, EditorGUIUtility.currentViewWidth, EditorGUIUtility.singleLineHeight * 2 + heightProp.floatValue + 5);
97+
fullRect = new Rect(0, 0, EditorGUIUtility.currentViewWidth, EditorGUIUtility.singleLineHeight * 2 + heightProp.floatValue + 5);
98+
Rect headerRect = fullRect;
99+
headerRect.height = _headerHeight;
100+
Rect bodyRect = fullRect;
101+
bodyRect.yMin += _headerHeight;
79102

80-
EditorGUI.DrawRect(rect, color);
103+
EditorGUI.DrawRect(headerRect, headerColor);
104+
EditorGUI.DrawRect(bodyRect, bodyColor);
81105

82-
GUI.backgroundColor = elemcolor;
106+
GUI.backgroundColor = headerBackColor;
83107

84108
GUIStyle areastyle = new GUIStyle(EditorStyles.wordWrappedLabel);
85109

@@ -93,22 +117,44 @@ public override void OnInspectorGUI()
93117
gUIStyle.normal.textColor = new Color(0.1f, 0.1f, 0.1f, 0.2f);
94118

95119
drawIconProp.boolValue = EditorGUILayout.Toggle(drawIconProp.boolValue, GUILayout.MaxWidth(16));
120+
121+
122+
GUI.backgroundColor = Color.white;
123+
GUI.color = Color.black;
124+
GUILayout.Label("Author:", GUILayout.Width(44));
125+
GUI.color = headerColor;
126+
if (GUILayout.Button(author.IsDummy() ? "-" : author.name, EditorStyles.popup))
127+
{
128+
GetAuthorsGenericMenu().ShowAsContext();
129+
}
130+
GUI.color = Color.black;
131+
GUILayout.Label("Type:", GUILayout.Width(36));
132+
GUI.color = headerColor;
133+
if (GUILayout.Button(noteType.IsDummy() ? "-" : noteType.name, EditorStyles.popup))
134+
{
135+
GetTypesGenericMenu().ShowAsContext();
136+
}
137+
if (GUILayout.Button(EditorGUIUtility.IconContent("_Popup"), _settingsButtonStyle, GUILayout.Width(18f)))
138+
{
139+
NotesSettingsWindow.Open();
140+
}
96141
GUILayout.Label("", gUIStyle);
97142

98143
float originalValue = EditorGUIUtility.labelWidth;
99144
EditorGUIUtility.labelWidth = 14;
100145
GUI.color = new Color(0.2f, 0.2f, 0.2f);
101-
GUI.backgroundColor = Color.white;
102146

103147
GUIStyle gUIStylex = new GUIStyle(EditorStyles.helpBox);
104148
heightProp.floatValue = EditorGUILayout.FloatField("↕", heightProp.floatValue, gUIStylex, GUILayout.MaxWidth(58));
105149
heightProp.floatValue = Mathf.Max(20f, heightProp.floatValue);
106-
GUI.color = defaultColor;
107150
EditorGUIUtility.labelWidth = originalValue;
108151

109-
Color newColor = EditorGUILayout.ColorField(colorProp.colorValue, GUILayout.MaxWidth(40));
110-
newColor.a = 1f;
111-
colorProp.colorValue = newColor;
152+
153+
GUI.color = defaultColor;
154+
155+
//Color newColor = EditorGUILayout.ColorField(colorProp.colorValue, GUILayout.MaxWidth(40));
156+
//newColor.a = 1f;
157+
//colorProp.colorValue = newColor;
112158

113159
EditorGUILayout.EndHorizontal();
114160

@@ -120,9 +166,14 @@ public override void OnInspectorGUI()
120166
serializedObject.ApplyModifiedProperties();
121167
EditorGUI.EndChangeCheck();
122168
}
169+
170+
private void DrawNote(Note target, SerializedObject serializedObject)
171+
{
172+
173+
}
123174
public override void DrawPreview(Rect previewArea)
124175
{
125-
rect = previewArea;
176+
fullRect = previewArea;
126177
base.DrawPreview(previewArea);
127178
}
128179
private static Color NormalizeBackgroundColor(Color color)

Editor/NoteEditor.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)