Skip to content

Commit

Permalink
Fixed some bugs that could occur in some situations
Browse files Browse the repository at this point in the history
  • Loading branch information
instance-id committed Jul 31, 2021
1 parent c8ac6e4 commit 78812df
Show file tree
Hide file tree
Showing 14 changed files with 362 additions and 108 deletions.
2 changes: 1 addition & 1 deletion Editor/EATKEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ void ButtonCascade()
buttonContainerDictionary.ForEach(x =>
{
// -- Then query for each button within the row ------
// -- Set default button cascase delay and counters --
// -- Set default button cascade delay and counters --
var buttons = x.Value.Query<Button>().ToList();
var buttonCascade = 100;
var buttonCount = buttons.Count;
Expand Down
4 changes: 1 addition & 3 deletions Editor/id.instance.elementanimationtoolkit.Editor.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
"name": "id.instance.elementanimationtoolkit.Editor",
"rootNamespace": "",
"references": [
"id.instance.elementanimationtoolkit",
"id.instance.extensions",
"id.instance.extensions.Editor"
"id.instance.elementanimationtoolkit"
],
"includePlatforms": [
"Editor"
Expand Down
98 changes: 84 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,99 @@

# ElementAnimationToolkit

### A collection of Unity UIElements animation extension methods, new animated elements, and examples.
### A collection of Unity UIToolkit/UIElements chainable animation extension methods, premade animated elements, and examples. <!-- omit in toc -->
![](Documentation/media/intro_animation.gif)

<br>

- [ElementAnimationToolkit](#elementanimationtoolkit)
- [Recent Features or Changes](#recent-features-or-changes)
- [Documentation](#documentation)
- [Installation](#installation)
- [Install via OpenUPM](#install-via-openupm)
- [Unity Package Manager](#unity-package-manager)
- [Animations and Features](#animations-and-features)
- [Usage / Examples:](#usage--examples)
- [Background Color Fade-In](#background-color-fade-in)
- [Hover Border Pulse](#hover-border-pulse)
- [Fade-in sequence](#fade-in-sequence)

<br>

### Recent Features or Changes
| New Animation types: | |
| -------------------- | --------------------- |
| Sequences | |
| | ShowForDuration |
| Base Annimations | |
| | AnimateOpacityDelayed |

## [Documentation]("https://docs.instance.id/elementanimationtoolkit/")

### Note: UIElements Animations are listed as Experimental by Unity - API subject to change. Also, since Element Animation Toolkit is currently underactive development, it is possible for API changes in this library as well
---

### Note: UIElements Animations are listed as Experimental by Unity - API subject to change. Also, since Element Animation Toolkit is currently underactive development, it is possible for API changes in this library as well <!-- omit in toc -->

<br>

#### The intent of this package is to help asset developers more easily create their own animation sequences via code.
#### The intent of this package is to help asset developers more easily create their own animation sequences via code.<!-- omit in toc -->
Though, I have been adding some new "ready to go" UIElement types, such as the "AnimatedFoldout", which you just use as you would a a typical foldout comes animated with no additional coding, minus some exposed properties to adjust the animation to your liking. I do intend to add more of these as I go along.

<br>

This package began simply as a small collection of helpers methods I created across my last few projects to make animating editor elements much easier and finally put into a single place. I continue to add to it as I go along but decided to put together several examples and wanted to share them.

### [Install via OpenUPM](https://openupm.com/packages/id.instance.elementanimationtoolkit)
<br>

### Installation

---

#### [Install via OpenUPM](https://openupm.com/packages/id.instance.elementanimationtoolkit)
```openupm add id.instance.elementanimationtoolkit```

### [Unity Package Manager](https://github.com/instance-id/ElementAnimationToolkit/tree/upm)
#### [Unity Package Manager](https://github.com/instance-id/ElementAnimationToolkit/tree/upm)
```github.com/instance-id/elementanimationtoolkit.git#upm```

### Usage:
<br>

### Animations and Features

---
| Animation types: | Method |
| ------------------ | ---------------------- |
| Sequences: | |
| | AnimCharacterSequence |
| | AnimFadeInSequence |
| | ShowForDuration |
| Continuous: | |
| | AnimBorderPulse |
| Hover: | |
| | HoverBorderPulse |
| Base Annimations: | |
| | AnimateBackgroundColor |
| | AnimateBorderColor |
| | AnimateColor |
| | AnimateImageTintColor |
| | AnimateOpacity |
| | AnimateOpacityDelayed |
| | AnimateWidth |
| | AnimateHeight |
| Extensions/Helpers | |
| | AutoToggleAfter |
| | ApplyPrivacyMask |
| | SetDisplay |
| | GetDisplay |
| | SetOpacity |
| | GetDisplay |
| | SetText |
| | OpenURL |

### Usage / Examples:

Examples: Tools > instance.id > Element Animation Toolkit

Note: The code is pretty ~~heavily~~ excessively documented and currently most method summaries for extension methods have examples in them. Be sure to check the comments for additional details!
Note: The code is pretty ~~heavily~~ excessively documented and currently most method summaries for extension methods have examples in them. Be sure to check the comments for additional details!

![](https://i.imgur.com/hY3DGDA.png)

Expand All @@ -48,8 +118,8 @@ Note: The code is pretty ~~heavily~~ excessively documented and currently most m

There are several fairly basic base animation helper methods which can easily be used on their own, but are also the basis of the more complex animation sequences:

### Background Color Fade-In
![](Documentation/media/background_fade_example.gif)
#### Background Color Fade-In
![](Documentation/media/background_fade_example.gif)
(The initial fade from gray to blue)

<details>
Expand All @@ -71,7 +141,7 @@ visualElement.AnimateBackgroundColor(originalColor, fadeColor, durationOfFade);

---

### Hover Border Pulse
#### Hover Border Pulse
![](Documentation/media/hoverborderpulse_example.gif)
<details>
<summary>Example hover animation: HoverBorderPulse()</summary>
Expand All @@ -87,7 +157,7 @@ visualElement.HoverBorderPulse(pulseStartColor: GetColor.FromHex("#7F3B3A"), pul

---

### Fade-in sequence
#### Fade-in sequence
![](Documentation/media/fade_example.gif)

<details>
Expand Down Expand Up @@ -118,7 +188,7 @@ Additionally there are many helper methods relating to many different Types from
<summary>Example color helper method: GetColor.FromHex()</summary>

Usage:
```cs
```cs
Color color = GetColor.FromHex("#CCCCCC");
```

Expand All @@ -139,7 +209,7 @@ public static Color FromHex(this string color)
<summary>Example creating an external url link : OpenURL() </summary>

Usage:
```cs
```cs
VisualElement visualElement = new VisualElement();
visualElement.OpenURL("https://github.com/instance-id/ElementAnimationToolkit");
```
Expand Down Expand Up @@ -184,7 +254,7 @@ Lastly is the USS button, which takes you to the USS stylesheet and the location

The animations without a row of buttons have a right-click context menu implemented which has similar options to jump directly into
the code at the proper location for the animation. (The methods I have created for jumping straight to the proper lines of code
are of course included and you are welcome to take advantage of them for your own needs.)
are of course included and you are welcome to take advantage of them for your own needs.)
![](Documentation/media/jumptomenu.png)


Expand Down
4 changes: 2 additions & 2 deletions Runtime/Animations/AnimationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static void ExecuteIn(this Action action, VisualElement element, long del
/// <param name="includeChildren">Register child elements in addition to the target element</param>
/// <typeparam name="TEventType">The event callback type in which to register</typeparam>
/// <returns>The target element</returns>
public static VisualElement RegisterCallback<TEventType>(this VisualElement element, EventCallback<TEventType> callback, bool includeChildren)
public static VisualElement RegisterCallback<TEventType>(this VisualElement element, EventCallback<TEventType> callback, bool includeChildren, TrickleDown useTrickleDown = TrickleDown.NoTrickleDown)
where TEventType : EventBase<TEventType>, new()
{
element.RegisterCallback(callback);
Expand All @@ -60,7 +60,7 @@ public static VisualElement RegisterCallback<TEventType>(this VisualElement elem
/// <param name="includeChildren">Register child elements in addition to the target element</param>
/// <typeparam name="TEventType">The event callback type in which to register</typeparam>
/// <returns>The target element</returns>
public static void UnregisterCallback<TEventType>(this VisualElement element, EventCallback<TEventType> callback, bool includeChildren)
public static void UnregisterCallback<TEventType>(this VisualElement element, EventCallback<TEventType> callback, bool includeChildren, TrickleDown useTrickleDown = TrickleDown.NoTrickleDown)
where TEventType : EventBase<TEventType>, new()
{
element.UnregisterCallback(callback);
Expand Down
43 changes: 37 additions & 6 deletions Runtime/Animations/AnimationSequences.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// -- Project : https://github.com/instance-id/ElementAnimationToolkit --
// -- instance.id 2020 | http://github.com/instance-id | http://instance.id --
// ----------------------------------------------------------------------------
Expand All @@ -7,13 +7,10 @@

#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.Linq;
using instance.id.EATK.Extensions;
// using instance.id.Extensions;
// using instance.id.Extensions;
using UnityEngine;
using UnityEngine.UIElements;
using System.Collections.Generic;
using instance.id.EATK.Extensions;
using UnityEngine.UIElements.Experimental;

namespace instance.id.EATK
Expand Down Expand Up @@ -263,6 +260,40 @@ private static ValueAnimation<StyleValues> DoFadeOut(VisualElement elementToFade
}

#endregion

// ----------------------------------------------- ShowForDuration
// --| ShowForDuration -------------------------------------------

#region ShowForDuration

/// <summary>
/// Show a hidden VisualElement by fading it's opacity for a specified duration and then hide it again when duration completes
/// </summary>
/// <param name="element">The element in which to display</param>
/// <param name="fadeDurationMs">The amount of time in milliseconds it should take to fade from one opacity extent to the other</param>
/// <param name="displayDurationMs">The amount of time in milliseconds to display the element before fading opacity back to 0</param>
/// <param name="callback">A callback to be executed upon completion of the animation sequence</param>
/// <param name="easing">The easing transition type to use for the animated values</param>
/// <returns>A list containing the ValueAnimation objects for each phase of the animation sequence</returns>
/// <remarks>Upon completion of the animation, the elements DisplayStyle will be set back to it's original value</remarks>
public static List<ValueAnimation<StyleValues>> ShowForDuration(this VisualElement element, int fadeDurationMs, int displayDurationMs, Action callback = null, Func<float, float> easing = null)
{
bool isDisplayed; // @formatter:off
List<ValueAnimation<StyleValues>> anims = new List<ValueAnimation<StyleValues>>();
if ((isDisplayed = element.GetDisplay()) == false) element.SetDisplay(true);

anims.Add(element.SetOpacity(0.Zero()).AnimateOpacity(0.Zero(), 1, fadeDurationMs, () =>
{
anims.Add(element.AnimateOpacityDelayed(1, 0.Zero(), 500, displayDurationMs, () =>
{
if (!isDisplayed) element.SetDisplay(false);
}));
}));

return anims;
} // @formatter:on

#endregion
}
}
#endif
35 changes: 21 additions & 14 deletions Runtime/Animations/ContinuousAnimations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// -- Project : https://github.com/instance-id/ElementAnimationToolkit --
// -- instance.id 2020 | http://github.com/instance-id | http://instance.id --
// ----------------------------------------------------------------------------

#if UNITY_EDITOR
using System;
using instance.id.EATK.Extensions;
Expand Down Expand Up @@ -36,20 +37,25 @@ public static class ContinuousAnimations
/// <param name="borderSelection">The parameters of the Vector4(1-4) represent which borders should have their colors changed: 1(x) = left, 2(y) = top, 3(z) = right, 4(w) = bottom.
/// If only the top and bottom borders are desired to pulse, you would pass new Vector4(0, 1, 0, 1)</param>
public static IVisualElementScheduledItem AnimBorderPulse(this VisualElement element, Color color1, Color color2, Color original = default,
int color1DurationMs = 1000,
int color2DurationMs = 1000,
bool addBorder = false,
Vector2 borderStartEndWidth = default,
Action callback = null,
Vector4 borderSelection = default)
int color1DurationMs = 1000,
int color2DurationMs = 1000,
bool addBorder = false,
Vector2 borderStartEndWidth = default,
Action callback = null,
Vector4 borderSelection = default,
IVisualElementScheduledItem repeatedAnim = default
)
{
if (borderStartEndWidth == default)
borderStartEndWidth = new Vector2(1, 0);

bool doBorderPulse;
var pulseIn = new ValueAnimation<StyleValues>();
var pulseOut = new ValueAnimation<StyleValues>();
IVisualElementScheduledItem repeatedAnim = null;
pulseIn.autoRecycle = true;
pulseOut.autoRecycle = true;
pulseIn.KeepAlive();
pulseOut.KeepAlive();

doBorderPulse = true;
if (addBorder) element.SetBorderWidth(borderStartEndWidth.x);
Expand Down Expand Up @@ -131,12 +137,13 @@ void DoCleanup()
{
if (pulseOut.isRunning) pulseOut.Stop();
if (pulseIn.isRunning) pulseIn.Stop();

element.SetBorderColor();
callback?.Invoke();
if (addBorder) element.SetBorderWidth(borderStartEndWidth.y);
if (borderSelection != default) ReplaceBorderValues();
callback?.Invoke();
} // @formatter:off

// -- Pulse color will fade original => desired color --
// -- via the AnimateTo local function. Once completed --
// -- the AnimateFrom function is called animating back --
Expand All @@ -146,31 +153,31 @@ void PulseIn(IVisualElementScheduledItem repeated)
{
if (!repeated.isActive) { DoCleanup(); return; }
if (pulseOut.isRunning) pulseOut.Stop();
callback?.Invoke();
pulseIn = element.AnimateBorderColor(
color1,
color2,
color1DurationMs,
() => PulseOut(repeated));
() => PulseOut(repeated)).KeepAlive();
}

void PulseOut(IVisualElementScheduledItem repeated)
{
if (!repeated.isActive) { DoCleanup(); return; }
if (pulseIn.isRunning) pulseIn.Stop();
callback?.Invoke();
pulseOut = element.AnimateBorderColor(
color2,
color1,
color2DurationMs);
color2DurationMs, () => { if(!repeated.isActive) DoCleanup();}).KeepAlive();
} // @formatter:on

var recurring = color1DurationMs + color2DurationMs + 20;

repeatedAnim = element.schedule
.Execute(() => PulseIn(repeatedAnim))
.Execute(() => { PulseIn(repeatedAnim); })
.StartingIn(0)
.Every(recurring)
.Until(() => !doBorderPulse);

return repeatedAnim;
}
}
Expand Down
Loading

0 comments on commit 78812df

Please sign in to comment.