Skip to content

Commit cc8f01b

Browse files
authored
Merge pull request #81 from Santarh/refactorInspector
Refactor UvRotationUnit property drawing.
2 parents e61263e + 4480269 commit cc8f01b

File tree

5 files changed

+55
-57
lines changed

5 files changed

+55
-57
lines changed

MToon/Editor/EditorEnums.cs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace MToon
2+
{
3+
public enum EditorRotationUnit
4+
{
5+
Rounds = 0,
6+
Degrees = 1,
7+
Radians = 2,
8+
}
9+
}

MToon/Editor/EditorEnums.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MToon/Editor/EditorUtils.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace MToon
77
{
8-
public class EditorUtils
8+
public static class EditorUtils
99
{
1010
private static string BasePath { get { return Path.Combine(Application.dataPath, "VRM/MToon"); } }
1111

MToon/Editor/MToonInspector.cs

+42-49
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ namespace MToon
88
{
99
public class MToonInspector : ShaderGUI
1010
{
11-
private static bool isAdvancedLightingPanelFoldout = false;
12-
private static MToon.RotationUnit ofUvRotationUnit = MToon.RotationUnit.Rounds;
11+
private const float RoundsToDegree = 360f;
12+
private const float RoundsToRadian = (float) Math.PI * 2f;
1313

14+
private static bool isAdvancedLightingPanelFoldout = false;
15+
private static EditorRotationUnit editorRotationUnit = EditorRotationUnit.Rounds;
16+
1417
private MaterialProperty _version;
1518
private MaterialProperty _blendMode;
1619
private MaterialProperty _bumpMap;
@@ -318,26 +321,27 @@ private void Draw(MaterialEditor materialEditor, Material[] materials)
318321
materialEditor.ShaderProperty(_uvAnimScrollX, "Scroll X (per second)");
319322
materialEditor.ShaderProperty(_uvAnimScrollY, "Scroll Y (per second)");
320323

321-
switch (EditorGUILayout.EnumPopup("Rotation Unit", ofUvRotationUnit))
322324
{
323-
case MToon.RotationUnit.Rounds:
324-
ofUvRotationUnit = MToon.RotationUnit.Rounds;
325-
break;
326-
case MToon.RotationUnit.Degrees:
327-
ofUvRotationUnit = MToon.RotationUnit.Degrees;
328-
break;
329-
case MToon.RotationUnit.Radians:
330-
ofUvRotationUnit = MToon.RotationUnit.Radians;
331-
break;
332-
default:
333-
ofUvRotationUnit = MToon.RotationUnit.Rounds;
334-
break;
335-
};
336-
var uvRotation = GetUvRotationValue(ofUvRotationUnit, _uvAnimRotation.floatValue);
337-
338-
uvRotation = EditorGUILayout.DelayedFloatField("Rotation value (per second)", uvRotation);
339-
_uvAnimRotation.floatValue = GetUvRoundValue(ofUvRotationUnit, uvRotation);
340-
325+
var control = EditorGUILayout.GetControlRect(hasLabel: true);
326+
const int popupMargin = 5;
327+
const int popupWidth = 80;
328+
329+
var floatControl = new Rect(control);
330+
floatControl.width -= popupMargin + popupWidth;
331+
var popupControl = new Rect(control);
332+
popupControl.x = floatControl.x + floatControl.width + popupMargin;
333+
popupControl.width = popupWidth;
334+
335+
EditorGUI.BeginChangeCheck();
336+
var inspectorRotationValue = GetInspectorRotationValue(editorRotationUnit, _uvAnimRotation.floatValue);
337+
inspectorRotationValue = EditorGUI.FloatField(floatControl, "Rotation value (per second)", inspectorRotationValue);
338+
if (EditorGUI.EndChangeCheck())
339+
{
340+
materialEditor.RegisterPropertyChangeUndo("UvAnimRotationValueChanged");
341+
_uvAnimRotation.floatValue = GetRawRotationValue(editorRotationUnit, inspectorRotationValue);
342+
}
343+
editorRotationUnit = (EditorRotationUnit) EditorGUI.EnumPopup(popupControl, editorRotationUnit);
344+
}
341345
}
342346
}
343347
EditorGUILayout.EndVertical();
@@ -422,44 +426,33 @@ private static void TextureWithHdrColor(MaterialEditor materialEditor, string la
422426

423427
}
424428

425-
private float GetUvRoundValue(MToon.RotationUnit unit , float val)
429+
private static float GetRawRotationValue(EditorRotationUnit unit, float inspectorValue)
426430
{
427431
switch (unit)
428432
{
429-
case MToon.RotationUnit.Rounds:
430-
{
431-
return val;
432-
}
433-
case MToon.RotationUnit.Degrees:
434-
{
435-
return val / 360.0f;
436-
}
437-
case MToon.RotationUnit.Radians:
438-
{
439-
return val / (2.0f * 3.14159265359f);
440-
}
433+
case EditorRotationUnit.Rounds:
434+
return inspectorValue;
435+
case EditorRotationUnit.Degrees:
436+
return inspectorValue / RoundsToDegree;
437+
case EditorRotationUnit.Radians:
438+
return inspectorValue / RoundsToRadian;
441439
default:
442-
return val;
440+
throw new ArgumentOutOfRangeException();
443441
}
444442
}
445-
private float GetUvRotationValue(MToon.RotationUnit unit,float val)
443+
444+
private static float GetInspectorRotationValue(EditorRotationUnit unit, float rawValue)
446445
{
447446
switch (unit)
448447
{
449-
case MToon.RotationUnit.Rounds:
450-
{
451-
return val;
452-
}
453-
case MToon.RotationUnit.Degrees:
454-
{
455-
return val * 360.0f;
456-
}
457-
case MToon.RotationUnit.Radians:
458-
{
459-
return val * (2.0f * 3.14159265359f);
460-
}
448+
case EditorRotationUnit.Rounds:
449+
return rawValue;
450+
case EditorRotationUnit.Degrees:
451+
return rawValue * RoundsToDegree;
452+
case EditorRotationUnit.Radians:
453+
return rawValue * RoundsToRadian;
461454
default:
462-
return val;
455+
throw new ArgumentOutOfRangeException();
463456
}
464457
}
465458
}

MToon/Scripts/Enums.cs

-7
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,6 @@ public enum CullMode
3535
Back = 2,
3636
}
3737

38-
public enum RotationUnit
39-
{
40-
Rounds = 0,
41-
Degrees = 1,
42-
Radians = 2
43-
}
44-
4538
public struct RenderQueueRequirement
4639
{
4740
public int DefaultValue;

0 commit comments

Comments
 (0)