Skip to content

Commit

Permalink
Merge pull request #931 from ousttrue/feature/add_validation_context
Browse files Browse the repository at this point in the history
add ValidationContext
  • Loading branch information
PoChang007 authored May 7, 2021
2 parents 42b7a9b + 4d4cb0c commit 77087f0
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public static IEnumerable<Validation> Validate(GameObject ExportRoot)
var jaw = animator.GetBoneTransform(HumanBodyBones.Jaw);
if (jaw != null)
{
yield return Validation.Warning(ValidationMessages.JAW_BONE_IS_INCLUDED.Msg());
yield return Validation.Warning(ValidationMessages.JAW_BONE_IS_INCLUDED.Msg(), ValidationContext.Create(jaw));
}
}
}
Expand Down
88 changes: 70 additions & 18 deletions Assets/UniGLTF/Editor/UniGLTF/Validation/ValidationExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,89 @@
using System;
using UnityEditor;
using UnityEngine;

namespace UniGLTF
{
public static class ValidationExtensions
{
static GUIContent s_info;
public static GUIContent Info
{
get
{
if (s_info == null)
{
s_info = EditorGUIUtility.IconContent("console.infoicon");
}
return s_info;
}
}

static GUIContent s_warn;
public static GUIContent Warn
{
get
{
if (s_warn == null)
{
s_warn = EditorGUIUtility.IconContent("console.warnicon");
}
return s_warn;
}
}

static GUIContent s_error;
public static GUIContent Error
{
get
{
if (s_error == null)
{
s_error = EditorGUIUtility.IconContent("console.erroricon");
}
return s_error;
}
}

public static void DrawGUI(this Validation self)
{
if (string.IsNullOrEmpty(self.Message))
{
return;
}

switch (self.ErrorLevel)
using (new GUILayout.VerticalScope(GUI.skin.box))
{
case ErrorLevels.Info:
EditorGUILayout.HelpBox(self.Message, MessageType.Info);
break;
case ErrorLevels.Warning:
EditorGUILayout.HelpBox(self.Message, MessageType.Warning);
break;
case ErrorLevels.Critical:
case ErrorLevels.Error:
EditorGUILayout.HelpBox(self.Message, MessageType.Error);
break;

default:
throw new NotImplementedException();
}
using (new GUILayout.HorizontalScope())
{
switch (self.ErrorLevel)
{
case ErrorLevels.Info:
EditorGUILayout.LabelField(Info, GUILayout.Width(30), GUILayout.Height(30));
break;

if (self.Extended != null)
{
self.Extended();
case ErrorLevels.Warning:
EditorGUILayout.LabelField(Warn, GUILayout.Width(30), GUILayout.Height(30));
break;

case ErrorLevels.Error:
EditorGUILayout.LabelField(Error, GUILayout.Width(30), GUILayout.Height(30));
break;
}

using (new GUILayout.VerticalScope())
{
EditorGUILayout.LabelField(self.Message);
if (self.Context.Context != null)
{
EditorGUILayout.ObjectField(self.Context.Context, self.Context.Type, true);
}
if (self.Context.Extended != null)
{
self.Context.Extended();
}
}
}
}
}
}
Expand Down
54 changes: 41 additions & 13 deletions Assets/UniGLTF/Runtime/UniGLTF/Validation/Validation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,37 @@ public enum ErrorLevels
Critical,
}

public struct ValidationContext
{
/// <summary>
/// Messageの発生個所にジャンプするための情報
/// </summary>
public Type Type;
public UnityEngine.Component Context;

/// <summary>
/// DrawGUIから呼び出す。追加のGUIボタンなどを実装する
/// </summary>
public Action Extended;

public static ValidationContext Create<T>(T c) where T : UnityEngine.Component
{
return new ValidationContext
{
Type = typeof(T),
Context = c,
};
}

public static ValidationContext Create(Action extended)
{
return new ValidationContext
{
Extended = extended,
};
}
}

public struct Validation
{
public readonly ErrorLevels ErrorLevel;
Expand All @@ -52,34 +83,31 @@ public bool CanExport

public readonly String Message;

/// <summary>
/// DrawGUIから呼び出す。追加のGUIボタンなどを実装する
/// </summary>
public Action Extended;
public ValidationContext Context;

Validation(ErrorLevels canExport, string message, Action extended = null)
Validation(ErrorLevels canExport, string message, ValidationContext context = default)
{
ErrorLevel = canExport;
Message = message;
Extended = extended;
Context = context;
}

public static Validation Critical(string msg)
public static Validation Critical(string msg, ValidationContext context = default)
{
return new Validation(ErrorLevels.Critical, msg);
return new Validation(ErrorLevels.Critical, msg, context);
}

public static Validation Error(string msg, Action action = null)
public static Validation Error(string msg, ValidationContext context = default)
{
return new Validation(ErrorLevels.Error, msg, action);
return new Validation(ErrorLevels.Error, msg, context);
}

public static Validation Warning(string msg)
public static Validation Warning(string msg, ValidationContext context = default)
{
return new Validation(ErrorLevels.Warning, msg);
return new Validation(ErrorLevels.Warning, msg, context);
}

public static Validation Info(string msg)
public static Validation Info(string msg, ValidationContext context = default)
{
return new Validation(ErrorLevels.Info, msg);
}
Expand Down
6 changes: 3 additions & 3 deletions Assets/VRM/Editor/FirstPerson/VRMFirstPersonValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ public static bool IsValid(this VRMFirstPerson.RendererFirstPersonFlags r, strin
{
if (r.Renderer == null)
{
validation = Validation.Error($"{name}.Renderer is null", extended);
validation = Validation.Error($"{name}.Renderer is null", ValidationContext.Create(extended));
return false;
}

if (!Hierarchy.Contains(r.Renderer.transform))
{
validation = Validation.Error($"{name}.Renderer is out of hierarchy", extended);
validation = Validation.Error($"{name}.Renderer is out of hierarchy", ValidationContext.Create(extended));
return false;
}

if (!r.Renderer.EnableForExport())
{
validation = Validation.Error($"{name}.Renderer is not active", extended);
validation = Validation.Error($"{name}.Renderer is not active", ValidationContext.Create(extended));
return false;
}

Expand Down

0 comments on commit 77087f0

Please sign in to comment.