Skip to content

Commit

Permalink
feat(AnimationsManager): Composite Animation.
Browse files Browse the repository at this point in the history
The animations manager composite animation now exists
  • Loading branch information
indiegabo committed Nov 6, 2023
1 parent 13cdf95 commit ebaa09f
Show file tree
Hide file tree
Showing 28 changed files with 1,880 additions and 1 deletion.
43 changes: 43 additions & 0 deletions Assets/Composite_Test.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 8bed12526f9ecb043ba63c51bdace746, type: 3}
m_Name: Composite_Test
m_EditorClassIdentifier:
_animationName: Composite_Test
_fps: 8
_antecipationCycle:
_animation: {fileID: 11400000}
_frames:
- _sprite: {fileID: 591964735, guid: 6a622a616b2d0204b8c9a9d1a19503c2, type: 3}
_id:
_identifiable: 1
_coreCycle:
_animation: {fileID: 11400000}
_frames:
- _sprite: {fileID: 786707139, guid: 6a622a616b2d0204b8c9a9d1a19503c2, type: 3}
_id:
- _sprite: {fileID: -694870178, guid: 6a622a616b2d0204b8c9a9d1a19503c2, type: 3}
_id:
- _sprite: {fileID: 1868516302, guid: 6a622a616b2d0204b8c9a9d1a19503c2, type: 3}
_id:
_identifiable: 1
_recoveryCycle:
_animation: {fileID: 11400000}
_frames:
- _sprite: {fileID: 1883112409, guid: 6a622a616b2d0204b8c9a9d1a19503c2, type: 3}
_id:
- _sprite: {fileID: -538822814, guid: 6a622a616b2d0204b8c9a9d1a19503c2, type: 3}
_id:
- _sprite: {fileID: -1684204194, guid: 6a622a616b2d0204b8c9a9d1a19503c2, type: 3}
_id:
_identifiable: 1
_loopableCore: 1
8 changes: 8 additions & 0 deletions Assets/Composite_Test.asset.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ MonoBehaviour:
_defaultAnimation: {fileID: 0}
_spriteAnimations:
- {fileID: 11400000, guid: 0c605438a4369e14e93856d7b7899618, type: 2}
- {fileID: 11400000, guid: 02fc119d4d99d7f49bf3a855dab25ddd, type: 2}
_animationChanged:
m_PersistentCalls:
m_Calls: []
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

namespace SpriteAnimations.Editor
{
public class SpriteAnimationViewComposite : SpriteAnimationView
{
#region Fields

private Toggle _loopableCoreField;
private CycleElement _cycleElement;
private SpriteAnimationComposite _compositeSpriteAnimation;
private AnimationPreviewElement _animationPreview;
private Cycle _currentCycle;
private List<Button> _tabButtons;

#endregion

#region Getters

public override AnimationType AnimationType => AnimationType.Composite;

#endregion

#region Constructors

public SpriteAnimationViewComposite()
{
VisualTreeAsset tree = Resources.Load<VisualTreeAsset>("UI Documents/Animations Views/AnimationViewComposite");
TemplateContainer template = tree.CloneTree();
template.style.flexGrow = 1;

_loopableCoreField = template.Q<Toggle>("loopable-core-field");
_loopableCoreField.RegisterValueChangedCallback(OnLoopableValueChanges);

VisualElement animationPreviewContainer = template.Q<VisualElement>("animation-preview-container");
animationPreviewContainer.Clear();
_animationPreview = new AnimationPreviewElement();
animationPreviewContainer.Add(_animationPreview);

VisualElement cycleContainer = template.Q<VisualElement>("cycle-view");

_tabButtons = template.Query<Button>(className: "tab-button").ToList();

_tabButtons.ForEach(tabButton =>
{
tabButton.clicked += () => OnCycleButtonClicked(tabButton);
});

_cycleElement = new CycleElement();
cycleContainer.Add(_cycleElement);

_contentContainer.Add(template);
}

#endregion

#region Initializing

public override void Initialize(SpriteAnimation animation)
{
base.Initialize(animation);
_compositeSpriteAnimation = animation as SpriteAnimationComposite;
_loopableCoreField.value = _compositeSpriteAnimation.IsLoopableCore;
InitializeCycle(_compositeSpriteAnimation.AntecipationCycle);

foreach (Button button in _tabButtons)
{
if (button.viewDataKey != "antecipation") return;
button.AddToClassList("active");
break;
}

_animationPreview.Initialize(this, this, _cycleElement, _viewZoomSlider);
}

public override void Dismiss()
{
base.Dismiss();
_compositeSpriteAnimation = null;
_cycleElement?.Dismiss();
}

#endregion

#region Cycles

private void InitializeCycle(Cycle cycle)
{
_cycleElement?.Dismiss();
_currentCycle = cycle;
_cycleElement.Initialize(cycle, this); // Must be initialized before the preview
}

private void EvaluateCycle(string cycleName)
{
switch (cycleName)
{
case "antecipation":
InitializeCycle(_compositeSpriteAnimation.AntecipationCycle);
break;
case "core":
InitializeCycle(_compositeSpriteAnimation.CoreCycle);
break;
case "recovery":
InitializeCycle(_compositeSpriteAnimation.RecoveryCycle);
break;
default:
InitializeCycle(_compositeSpriteAnimation.AntecipationCycle);
break;
}
}

private void OnCycleButtonClicked(Button button)
{
_tabButtons.ForEach(tabButton => tabButton.RemoveFromClassList("active"));
button.AddToClassList("active");
EvaluateCycle(button.viewDataKey);
}

#endregion

#region Loopable

private void OnLoopableValueChanges(ChangeEvent<bool> changeEvent)
{
_compositeSpriteAnimation.IsLoopableCore = changeEvent.newValue;
}

#endregion
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ protected SpriteAnimationView Fabricate(AnimationType animationType)
AnimationType.SingleCycle => new SpriteAnimationViewSingleCycle(),
AnimationType.Windrose => new SpriteAnimationViewWindrose(),
AnimationType.Combo => new SpriteAnimationViewCombo(),
AnimationType.Composite => new SpriteAnimationViewComposite(),
_ => throw new ArgumentOutOfRangeException(nameof(animationType), animationType, null),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ private void OnCreateClicked()
AnimationType.SingleCycle => CreateSingleCycleAnimation(path, name),
AnimationType.Windrose => CreateWindroseAnimation(path, name),
AnimationType.Combo => CreateComboAnimation(path, name),
AnimationType.Composite => CreateCompositeAnimation(path, name),
_ => throw new ArgumentOutOfRangeException(nameof(_typeField.value), null, null)
};

Expand Down Expand Up @@ -126,6 +127,14 @@ private SpriteAnimationCombo CreateComboAnimation(string path, string name)
return comboAsset;
}

private SpriteAnimationComposite CreateCompositeAnimation(string path, string name)
{
SpriteAnimationComposite compositeAsset = ScriptableObject.CreateInstance<SpriteAnimationComposite>();
compositeAsset.GenerateCycles();
AssetDatabase.CreateAsset(compositeAsset, $"{path}/{name}.asset");
return compositeAsset;
}

#endregion

#region Events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
background-color: rgb(77, 7, 24);
}

.interactable.active {
background-color: rgb(77, 7, 24);
}

.sa-label {
font-size: 14px;
color: rgb(255, 255, 255);
Expand Down Expand Up @@ -223,3 +227,26 @@

#waiting-time-field > Label {
}

.tabs-bar {
padding-top: 5px;
padding-right: 5px;
padding-bottom: 5px;
padding-left: 5px;
height: 50px;
flex-direction: row;
align-items: center;
}

.tabs-bar > Button {
margin-top: 0;
margin-right: 0;
margin-bottom: 0;
margin-left: 3px;
padding-top: 0;
padding-right: 0;
padding-bottom: 0;
padding-left: 0;
width: 120px;
height: 33px;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="True">
<Style src="project://database/Assets/SpriteAnimations/Resources/Styles/AnimationManagerStyles.uss?fileID=7433441132597879392&amp;guid=a292484cf1ed97c4e9a9a18c4bde1c46&amp;type=3#AnimationManagerStyles" />
<ui:VisualElement name="container" style="flex-grow: 1; flex-direction: row; width: 100%; height: 100%; align-items: flex-start;">
<ui:VisualElement name="left" style="height: 100%; max-width: 240px; align-items: center; background-color: rgb(31, 4, 9); padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px;">
<ui:VisualElement name="animation-preview-container" style="flex-grow: 0; flex-direction: row; align-items: center; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px;">
<ui:VisualElement style="flex-grow: 0; flex-shrink: 0; height: 200px; width: 200px; -unity-background-image-tint-color: rgb(173, 1, 1); background-color: rgb(38, 79, 120);" />
</ui:VisualElement>
</ui:VisualElement>
<ui:VisualElement name="content" style="flex-grow: 1; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; height: 100%;">
<ui:VisualElement name="loopable-core-field-container" style="flex-grow: 0; flex-direction: row-reverse; width: 100%; align-items: center; margin-bottom: 0; margin-top: 0; margin-right: 0; margin-left: 0; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px;">
<ui:Toggle label="Loopable core?" name="loopable-core-field" value="true" tooltip="If the Sprite Animator should restart the animation automatically upon its cycle end." style="align-items: center; margin-top: 0; margin-right: 0; margin-bottom: 0; margin-left: 0; -unity-text-align: middle-left;" />
</ui:VisualElement>
<ui:VisualElement name="tabs-bar" class="tabs-bar">
<ui:VisualElement name="expander" style="flex-grow: 1;" />
<ui:Button text="Antecipation" parse-escape-sequences="true" display-tooltip-when-elided="true" name="tab-button-antecipation" view-data-key="antecipation" class="interactable tab-button active" />
<ui:Button text="Core" parse-escape-sequences="true" display-tooltip-when-elided="true" name="tab-button-core" view-data-key="core" class="interactable tab-button" />
<ui:Button text="Recovery" parse-escape-sequences="true" display-tooltip-when-elided="true" name="tab-button-recovery" view-data-key="recovery" class="interactable tab-button" />
</ui:VisualElement>
<ui:VisualElement name="cycle-view" style="flex-grow: 1;" />
</ui:VisualElement>
</ui:VisualElement>
</ui:UXML>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Assets/SpriteAnimations/Runtime/Scripts/AnimationType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public enum AnimationType
{
SingleCycle,
Windrose,
Combo
Combo,
Composite
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

using System;
using System.Collections.Generic;
using UnityEngine;

namespace SpriteAnimations
{
public class SpriteAnimationComposite : SpriteAnimation
{
#region Inspector

[SerializeField]
private Cycle _antecipationCycle;

[SerializeField]
private Cycle _coreCycle;

[SerializeField]
private Cycle _recoveryCycle;

[SerializeField]
private bool _loopableCore;

#endregion

#region Getters

public Cycle AntecipationCycle { get => _antecipationCycle; set => _antecipationCycle = value; }
public Cycle CoreCycle { get => _coreCycle; set => _coreCycle = value; }
public Cycle RecoveryCycle { get => _recoveryCycle; set => _recoveryCycle = value; }
public bool IsLoopableCore { get => _loopableCore; set => _loopableCore = value; }

public override Type PerformerType => typeof(CompositePerformer);
public override AnimationType AnimationType => AnimationType.Composite;

#endregion

#region Cycles

/// <summary>
/// This must be executed upon the asset creation
/// </summary>
public void GenerateCycles()
{
_antecipationCycle = new Cycle(this);
_coreCycle = new Cycle(this);
_recoveryCycle = new Cycle(this);
}

#endregion

#region Calculations

public override int CalculateFramesCount()
{
return _antecipationCycle.Size + _coreCycle.Size + _recoveryCycle.Size;
}

#endregion
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ebaa09f

Please sign in to comment.