Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/tonygiang/MyBox
Browse files Browse the repository at this point in the history
  • Loading branch information
Deadcows committed Sep 9, 2021
2 parents 118fb89 + 089d4d0 commit 3e57feb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
33 changes: 21 additions & 12 deletions Attributes/CharactersRangeAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
}
else
{
var charactersRange = (CharactersRangeAttribute) attribute;
var charactersRange = (CharactersRangeAttribute)attribute;
var mode = charactersRange.Mode;
var ignoreCase = charactersRange.IgnoreCase;
var filter = charactersRange.Characters;
Expand All @@ -75,37 +75,46 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
if (ignoreCase) c = char.ToUpper(c);
return filter.Contains(c) ^ allow;
});

bool ifMatch = mode == CharacterRangeMode.WarningIfAny;
bool ifNotMatch = mode == CharacterRangeMode.WarningIfNotMatch;
bool anyFiltered = filteredCharacters.Any();
bool warn = (ifMatch && anyFiltered || ifNotMatch && anyFiltered);
var originalPosition = position;

DrawWarning();
position.width -= 20;

property.stringValue = EditorGUI.TextField(position, label, property.stringValue);
DrawTooltip();

if (!warning)
{
property.stringValue = filteredCharacters.Aggregate(
property.stringValue,
(p, c) => p.Replace(c.ToString(), ""));
}

if (warn)
{
GUILayoutUtility.GetRect(GUIContent.none, EditorStyles.objectField);
position = originalPosition;
position.y += EditorGUIUtility.singleLineHeight;
DrawWarning();
position.x += EditorGUIUtility.labelWidth;
var warningContent = new GUIContent("Containing disallowed characters!");
EditorGUI.LabelField(position, warningContent, EditorStyles.miniBoldLabel);
}

property.serializedObject.ApplyModifiedProperties();


void DrawWarning()
{
if (!warning) return;

bool ifMatch = mode == CharacterRangeMode.WarningIfAny;
bool ifNotMatch = mode == CharacterRangeMode.WarningIfNotMatch;

bool anyFiltered = filteredCharacters.Any();
bool warn = (ifMatch && anyFiltered || ifNotMatch && anyFiltered);
if (property.stringValue.Length == 0) warn = false;

MyGUI.DrawColouredRect(position, warn ? MyGUI.Colors.Yellow : Color.clear);
}

void DrawTooltip()
{
string tooltip = "Characters range ";
Expand Down
27 changes: 19 additions & 8 deletions Attributes/RegexStringAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public RegexStringAttribute(string regex, RegexStringMode mode = RegexStringMode
AttributeMode = mode;
}
}

public enum RegexStringMode
{
/// <summary>
Expand Down Expand Up @@ -59,8 +59,13 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
}
else
{
var regex = (RegexStringAttribute) attribute;
var regex = (RegexStringAttribute)attribute;
var mode = regex.AttributeMode;
bool ifMatch = mode == RegexStringMode.WarningIfMatch;
bool ifNotMatch = mode == RegexStringMode.WarningIfNotMatch;
bool anyMatching = regex.Regex.IsMatch(property.stringValue);
bool warn = (ifMatch && anyMatching) || (ifNotMatch && !anyMatching);
var originalPosition = position;

DrawWarning();
position.width -= 20;
Expand All @@ -73,6 +78,17 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
if (mode == RegexStringMode.Match) OnKeepMatching();
}

if (warn)
{
GUILayoutUtility.GetRect(GUIContent.none, EditorStyles.objectField);
position = originalPosition;
position.y += EditorGUIUtility.singleLineHeight;
DrawWarning();
position.x += EditorGUIUtility.labelWidth;
var warningContent = new GUIContent("Regex rule violated!");
EditorGUI.LabelField(position, warningContent, EditorStyles.miniBoldLabel);
}

property.serializedObject.ApplyModifiedProperties();


Expand All @@ -81,15 +97,10 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten

void DrawWarning()
{
bool ifMatch = mode == RegexStringMode.WarningIfMatch;
bool ifNotMatch = mode == RegexStringMode.WarningIfNotMatch;
if (!ifMatch && !ifNotMatch) return;

bool anyMatching = regex.Regex.IsMatch(property.stringValue);
bool warn = (ifMatch && anyMatching) || (ifNotMatch && !anyMatching);
MyGUI.DrawColouredRect(position, warn ? MyGUI.Colors.Yellow : Color.clear);
}

void DrawTooltip()
{
string tooltip = "Regex field: ";
Expand Down

0 comments on commit 3e57feb

Please sign in to comment.