Created
July 8, 2020 08:29
-
-
Save MrMMatricks/7e0d26f55b4d2421cc0e4d4291866f2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StylableColorDrawer : OdinValueDrawer<StylableColor> | |
{ | |
private int colorFieldWidth = 30; | |
protected override void DrawPropertyLayout(GUIContent label) | |
{ | |
Rect rect = EditorGUILayout.GetControlRect(); | |
StylableColor value = this.ValueEntry.SmartValue; | |
var styles = value.styles; | |
if(styles == null) | |
{ | |
if (StyleManager.Instance == null) | |
{ | |
SirenixEditorGUI.WarningMessageBox("Please Add Style Manager to scene"); | |
return; | |
} | |
if (StyleManager.Instance.Styles == null) | |
{ | |
SirenixEditorGUI.WarningMessageBox("Please Add styles to the Style Manager"); | |
return; | |
} | |
styles = StyleManager.Instance.Styles; | |
} | |
if (string.IsNullOrEmpty(value.styleId)) | |
{ | |
value.styleId = styles.AllConfigs.FirstOrDefault().Id; | |
} | |
if (label != null) | |
{ | |
rect = EditorGUI.PrefixLabel(rect, label); | |
} | |
var dropDownRect = rect.AlignLeft(rect.width - colorFieldWidth - 10); | |
var returnList = GenericSelector<StyleKeyList.StyleKey>.DrawSelectorDropdown(dropDownRect, styles.GetStyle(value.styleId).StyleConfig.Name, this.ShowSelector); | |
if (returnList != null) | |
{ | |
value.styleId = returnList.FirstOrDefault().Id; | |
} | |
SirenixEditorGUI.DrawSolidRect(rect.AlignRight(colorFieldWidth), value.Color, false); | |
SirenixEditorGUI.DrawBorders(rect.AlignRight(colorFieldWidth), 1); | |
this.ValueEntry.SmartValue = value; | |
UnityEditor.EditorApplication.QueuePlayerLoopUpdate(); | |
ValueEntry.ApplyChanges(); | |
} | |
private OdinSelector<StyleKeyList.StyleKey> ShowSelector(Rect rect) | |
{ | |
StylableColor value = this.ValueEntry.SmartValue; | |
var styles = value.styles; | |
if (styles == null) | |
{ | |
styles = StyleManager.Instance.Styles; | |
} | |
GenericSelector<StyleKeyList.StyleKey> selector = new GenericSelector<StyleKeyList.StyleKey>("", false, x => x.Name, styles.AllConfigs); | |
selector.SetSelection(styles.GetStyleConfig(value.styleId)); | |
selector.SelectionTree.Config.DrawSearchToolbar = false; | |
selector.EnableSingleClickToSelect(); | |
selector.SelectionTree.DefaultMenuStyle.Height = 22; | |
var window = selector.ShowInPopup(rect); | |
return selector; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment