You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
295 lines
13 KiB
295 lines
13 KiB
8 years ago
|
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
|
||
|
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
|
||
9 years ago
|
|
||
10 years ago
|
using UnityEditor;
|
||
|
using UnityEngine;
|
||
|
|
||
|
namespace Fungus
|
||
|
{
|
||
8 years ago
|
[CustomEditor (typeof(Portrait))]
|
||
|
public class PortraitEditor : CommandEditor
|
||
|
{
|
||
|
protected SerializedProperty stageProp;
|
||
|
protected SerializedProperty displayProp;
|
||
|
protected SerializedProperty characterProp;
|
||
|
protected SerializedProperty replacedCharacterProp;
|
||
|
protected SerializedProperty portraitProp;
|
||
|
protected SerializedProperty offsetProp;
|
||
|
protected SerializedProperty fromPositionProp;
|
||
|
protected SerializedProperty toPositionProp;
|
||
|
protected SerializedProperty facingProp;
|
||
|
protected SerializedProperty useDefaultSettingsProp;
|
||
|
protected SerializedProperty fadeDurationProp;
|
||
|
protected SerializedProperty moveDurationProp;
|
||
|
protected SerializedProperty shiftOffsetProp;
|
||
|
protected SerializedProperty waitUntilFinishedProp;
|
||
|
protected SerializedProperty moveProp;
|
||
|
protected SerializedProperty shiftIntoPlaceProp;
|
||
|
|
||
|
protected virtual void OnEnable()
|
||
|
{
|
||
|
if (NullTargetCheck()) // Check for an orphaned editor instance
|
||
|
return;
|
||
9 years ago
|
|
||
8 years ago
|
stageProp = serializedObject.FindProperty("stage");
|
||
|
displayProp = serializedObject.FindProperty("display");
|
||
|
characterProp = serializedObject.FindProperty("character");
|
||
|
replacedCharacterProp = serializedObject.FindProperty("replacedCharacter");
|
||
|
portraitProp = serializedObject.FindProperty("portrait");
|
||
|
offsetProp = serializedObject.FindProperty("offset");
|
||
|
fromPositionProp = serializedObject.FindProperty("fromPosition");
|
||
|
toPositionProp = serializedObject.FindProperty("toPosition");
|
||
|
facingProp = serializedObject.FindProperty("facing");
|
||
|
useDefaultSettingsProp = serializedObject.FindProperty("useDefaultSettings");
|
||
|
fadeDurationProp = serializedObject.FindProperty("fadeDuration");
|
||
|
moveDurationProp = serializedObject.FindProperty("moveDuration");
|
||
|
shiftOffsetProp = serializedObject.FindProperty("shiftOffset");
|
||
|
waitUntilFinishedProp = serializedObject.FindProperty("waitUntilFinished");
|
||
|
moveProp = serializedObject.FindProperty("move");
|
||
|
shiftIntoPlaceProp = serializedObject.FindProperty("shiftIntoPlace");
|
||
|
}
|
||
|
|
||
|
public override void DrawCommandGUI()
|
||
|
{
|
||
|
serializedObject.Update();
|
||
|
|
||
|
Portrait t = target as Portrait;
|
||
10 years ago
|
|
||
8 years ago
|
if (Stage.activeStages.Count > 1)
|
||
|
{
|
||
|
CommandEditor.ObjectField<Stage>(stageProp,
|
||
|
new GUIContent("Portrait Stage", "Stage to display the character portraits on"),
|
||
|
new GUIContent("<Default>"),
|
||
|
Stage.activeStages);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
8 years ago
|
t._Stage = null;
|
||
8 years ago
|
}
|
||
|
// Format Enum names
|
||
8 years ago
|
string[] displayLabels = StringFormatter.FormatEnumNames(t.Display,"<None>");
|
||
8 years ago
|
displayProp.enumValueIndex = EditorGUILayout.Popup("Display", (int)displayProp.enumValueIndex, displayLabels);
|
||
10 years ago
|
|
||
8 years ago
|
string characterLabel = "Character";
|
||
8 years ago
|
if (t.Display == DisplayType.Replace)
|
||
8 years ago
|
{
|
||
|
CommandEditor.ObjectField<Character>(replacedCharacterProp,
|
||
|
new GUIContent("Replace", "Character to replace"),
|
||
|
new GUIContent("<None>"),
|
||
|
Character.activeCharacters);
|
||
|
characterLabel = "With";
|
||
|
}
|
||
|
|
||
|
CommandEditor.ObjectField<Character>(characterProp,
|
||
|
new GUIContent(characterLabel, "Character to display"),
|
||
|
new GUIContent("<None>"),
|
||
|
Character.activeCharacters);
|
||
10 years ago
|
|
||
8 years ago
|
bool showOptionalFields = true;
|
||
8 years ago
|
Stage s = t._Stage;
|
||
8 years ago
|
// Only show optional portrait fields once required fields have been filled...
|
||
8 years ago
|
if (t._Character != null) // Character is selected
|
||
8 years ago
|
{
|
||
8 years ago
|
if (t._Character.Portraits == null || // Character has a portraits field
|
||
|
t._Character.Portraits.Count <= 0 ) // Character has at least one portrait
|
||
8 years ago
|
{
|
||
|
EditorGUILayout.HelpBox("This character has no portraits. Please add portraits to the character's prefab before using this command.", MessageType.Error);
|
||
|
showOptionalFields = false;
|
||
|
}
|
||
8 years ago
|
if (t._Stage == null) // If default portrait stage selected
|
||
8 years ago
|
{
|
||
8 years ago
|
if (t._Stage == null) // If no default specified, try to get any portrait stage in the scene
|
||
8 years ago
|
{
|
||
|
s = GameObject.FindObjectOfType<Stage>();
|
||
|
}
|
||
|
}
|
||
|
if (s == null)
|
||
|
{
|
||
|
EditorGUILayout.HelpBox("No portrait stage has been set.", MessageType.Error);
|
||
|
showOptionalFields = false;
|
||
|
}
|
||
|
}
|
||
8 years ago
|
if (t.Display != DisplayType.None && t._Character != null && showOptionalFields)
|
||
8 years ago
|
{
|
||
8 years ago
|
if (t.Display != DisplayType.Hide && t.Display != DisplayType.MoveToFront)
|
||
8 years ago
|
{
|
||
|
// PORTRAIT
|
||
|
CommandEditor.ObjectField<Sprite>(portraitProp,
|
||
|
new GUIContent("Portrait", "Portrait representing character"),
|
||
|
new GUIContent("<Previous>"),
|
||
8 years ago
|
t._Character.Portraits);
|
||
|
if (t._Character.PortraitsFace != FacingDirection.None)
|
||
8 years ago
|
{
|
||
|
// FACING
|
||
|
// Display the values of the facing enum as <-- and --> arrows to avoid confusion with position field
|
||
|
string[] facingArrows = new string[]
|
||
|
{
|
||
|
"<Previous>",
|
||
|
"<--",
|
||
|
"-->",
|
||
|
};
|
||
|
facingProp.enumValueIndex = EditorGUILayout.Popup("Facing", (int)facingProp.enumValueIndex, facingArrows);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
8 years ago
|
t.Facing = FacingDirection.None;
|
||
8 years ago
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
8 years ago
|
t._Portrait = null;
|
||
|
t.Facing = FacingDirection.None;
|
||
8 years ago
|
}
|
||
|
string toPositionPrefix = "";
|
||
8 years ago
|
if (t.Move)
|
||
8 years ago
|
{
|
||
|
// MOVE
|
||
|
EditorGUILayout.PropertyField(moveProp);
|
||
|
}
|
||
8 years ago
|
if (t.Move)
|
||
8 years ago
|
{
|
||
8 years ago
|
if (t.Display != DisplayType.Hide)
|
||
8 years ago
|
{
|
||
|
// START FROM OFFSET
|
||
|
EditorGUILayout.PropertyField(shiftIntoPlaceProp);
|
||
|
}
|
||
|
}
|
||
8 years ago
|
if (t.Move)
|
||
8 years ago
|
{
|
||
8 years ago
|
if (t.Display != DisplayType.Hide)
|
||
8 years ago
|
{
|
||
8 years ago
|
if (t.ShiftIntoPlace)
|
||
8 years ago
|
{
|
||
8 years ago
|
t.FromPosition = null;
|
||
8 years ago
|
// OFFSET
|
||
|
// Format Enum names
|
||
8 years ago
|
string[] offsetLabels = StringFormatter.FormatEnumNames(t.Offset,"<Previous>");
|
||
8 years ago
|
offsetProp.enumValueIndex = EditorGUILayout.Popup("From Offset", (int)offsetProp.enumValueIndex, offsetLabels);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
8 years ago
|
t.Offset = PositionOffset.None;
|
||
8 years ago
|
// FROM POSITION
|
||
|
CommandEditor.ObjectField<RectTransform>(fromPositionProp,
|
||
|
new GUIContent("From Position", "Move the portrait to this position"),
|
||
|
new GUIContent("<Previous>"),
|
||
8 years ago
|
s.Positions);
|
||
8 years ago
|
}
|
||
|
}
|
||
|
toPositionPrefix = "To ";
|
||
|
}
|
||
|
else
|
||
|
{
|
||
8 years ago
|
t.ShiftIntoPlace = false;
|
||
|
t.FromPosition = null;
|
||
8 years ago
|
toPositionPrefix = "At ";
|
||
|
}
|
||
8 years ago
|
if (t.Display == DisplayType.Show || (t.Display == DisplayType.Hide && t.Move) )
|
||
8 years ago
|
{
|
||
|
// TO POSITION
|
||
|
CommandEditor.ObjectField<RectTransform>(toPositionProp,
|
||
|
new GUIContent(toPositionPrefix+"Position", "Move the portrait to this position"),
|
||
|
new GUIContent("<Previous>"),
|
||
8 years ago
|
s.Positions);
|
||
8 years ago
|
}
|
||
|
else
|
||
|
{
|
||
8 years ago
|
t.ToPosition = null;
|
||
8 years ago
|
}
|
||
8 years ago
|
if (!t.Move && t.Display != DisplayType.MoveToFront)
|
||
8 years ago
|
{
|
||
|
// MOVE
|
||
|
EditorGUILayout.PropertyField(moveProp);
|
||
|
}
|
||
8 years ago
|
if (t.Display != DisplayType.MoveToFront)
|
||
8 years ago
|
{
|
||
|
|
||
|
EditorGUILayout.Separator();
|
||
10 years ago
|
|
||
8 years ago
|
// USE DEFAULT SETTINGS
|
||
|
EditorGUILayout.PropertyField(useDefaultSettingsProp);
|
||
8 years ago
|
if (!t.UseDefaultSettings) {
|
||
8 years ago
|
// FADE DURATION
|
||
|
EditorGUILayout.PropertyField(fadeDurationProp);
|
||
8 years ago
|
if (t.Move)
|
||
8 years ago
|
{
|
||
|
// MOVE SPEED
|
||
|
EditorGUILayout.PropertyField(moveDurationProp);
|
||
|
}
|
||
8 years ago
|
if (t.ShiftIntoPlace)
|
||
8 years ago
|
{
|
||
|
// SHIFT OFFSET
|
||
|
EditorGUILayout.PropertyField(shiftOffsetProp);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
8 years ago
|
t.Move = false;
|
||
|
t.UseDefaultSettings = true;
|
||
8 years ago
|
EditorGUILayout.Separator();
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
EditorGUILayout.PropertyField(waitUntilFinishedProp);
|
||
10 years ago
|
|
||
|
|
||
8 years ago
|
if (t._Portrait != null && t.Display != DisplayType.Hide)
|
||
8 years ago
|
{
|
||
8 years ago
|
Texture2D characterTexture = t._Portrait.texture;
|
||
10 years ago
|
|
||
8 years ago
|
float aspect = (float)characterTexture.width / (float)characterTexture.height;
|
||
|
Rect previewRect = GUILayoutUtility.GetAspectRect(aspect, GUILayout.Width(100), GUILayout.ExpandWidth(true));
|
||
10 years ago
|
|
||
8 years ago
|
if (characterTexture != null)
|
||
|
{
|
||
|
GUI.DrawTexture(previewRect,characterTexture,ScaleMode.ScaleToFit,true,aspect);
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
if (t.Display != DisplayType.Hide)
|
||
8 years ago
|
{
|
||
|
string portraitName = "<Previous>";
|
||
8 years ago
|
if (t._Portrait != null)
|
||
8 years ago
|
{
|
||
8 years ago
|
portraitName = t._Portrait.name;
|
||
8 years ago
|
}
|
||
|
string portraitSummary = " " + portraitName;
|
||
|
int toolbarInt = 1;
|
||
|
string[] toolbarStrings = {"<--", portraitSummary, "-->"};
|
||
|
toolbarInt = GUILayout.Toolbar (toolbarInt, toolbarStrings, GUILayout.MinHeight(20));
|
||
|
int portraitIndex = -1;
|
||
10 years ago
|
|
||
8 years ago
|
if (toolbarInt != 1)
|
||
|
{
|
||
8 years ago
|
for(int i=0; i<t._Character.Portraits.Count; i++){
|
||
|
if(portraitName == t._Character.Portraits[i].name)
|
||
8 years ago
|
{
|
||
|
portraitIndex = i;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
if (toolbarInt == 0)
|
||
|
{
|
||
|
if(portraitIndex > 0)
|
||
|
{
|
||
8 years ago
|
t._Portrait = t._Character.Portraits[--portraitIndex];
|
||
8 years ago
|
}
|
||
|
else
|
||
|
{
|
||
8 years ago
|
t._Portrait = null;
|
||
8 years ago
|
}
|
||
|
}
|
||
|
if (toolbarInt == 2)
|
||
|
{
|
||
8 years ago
|
if(portraitIndex < t._Character.Portraits.Count-1)
|
||
8 years ago
|
{
|
||
8 years ago
|
t._Portrait = t._Character.Portraits[++portraitIndex];
|
||
8 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
serializedObject.ApplyModifiedProperties();
|
||
|
}
|
||
|
}
|
||
10 years ago
|
}
|