Skip to content

Commit

Permalink
feat: Add Runnable and ImageAspectRatioFitter
Browse files Browse the repository at this point in the history
Note - Runnable is to replace Runner due to a lot of errors being encountered
in other projects.
  • Loading branch information
Vatsal Ambastha committed Apr 7, 2020
1 parent 1ddd0ff commit dd671e7
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ public static class UnexInitializer {
public static void Initialize() {
Dispatcher.Init();
Runner.Init();
Runnable.Init();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace UnityEngine.UI {
[ExecuteAlways]
[RequireComponent(typeof(Image))]
[RequireComponent(typeof(AspectRatioFitter))]
public class ImageAspectRatioFitter : MonoBehaviour {
AspectRatioFitter fitter;
Image image;
Texture2D tex;

void Awake(){
Init();
}

void OnValidate() {
Init();
}

void Init(){
fitter = GetComponent<AspectRatioFitter>();
image = GetComponent<Image>();
}

void Update() {
if (image.sprite == null) return;

var t = image.sprite.texture;
if (t == tex)
return;
tex = t;
fitter.aspectRatio = (float) t.width / t.height;
}
}
}

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

169 changes: 169 additions & 0 deletions Assets/Adrenak.Unex/Scripts/Runtime/Utils/Runnable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/**
* Copyright 2015 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

// Uncomment to enable debugging of the Runnable class.
//#define ENABLE_RUNNABLE_DEBUGGING

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

namespace Adrenak.Unex {
/// <summary>
/// Helper class for running co-routines without having to inherit from MonoBehavior.
/// </summary>
public class Runnable : MonoBehaviour {

static Runnable instance;
#region Public Properties
/// <summary>
/// Returns the Runnable instance.
/// </summary>
public static Runnable Instance {
get {
if (instance == null)
instance = GameObject.FindObjectOfType<Runnable>();
if (instance == null)
Init();
return instance;
}
}

public static void Init(){
var go = new GameObject("Runnable");
go.hideFlags = HideFlags.HideAndDontSave;
instance = go.AddComponent<Runnable>();
}
#endregion

#region Public Interface
/// <summary>
/// Start a co-routine function.
/// </summary>
/// <param name="routine">The IEnumerator returns by the co-routine function the user is invoking.</param>
/// <returns>Returns a ID that can be passed into Stop() to halt the co-routine.</returns>
public static int Run(IEnumerator routine) {
Routine r = new Routine(routine);
return r.ID;
}

/// <summary>
/// Stops a active co-routine.
/// </summary>
/// <param name="ID">THe ID of the co-routine to stop.</param>
public static void Stop(int ID) {
Routine r = null;
if (Instance._routines.TryGetValue(ID, out r))
r.Stop = true;
}

/// <summary>
/// Check if a routine is still running.
/// </summary>
/// <param name="id">The ID returned by Run().</param>
/// <returns>Returns true if the routine is still active.</returns>
static public bool IsRunning(int id) {
return Instance._routines.ContainsKey(id);
}

#if UNITY_EDITOR
private static bool _editorRunnable = false;

/// <summary>
/// This function enables the Runnable in edit mode.
/// </summary>
public static void EnableRunnableInEditor() {
if (!_editorRunnable) {
_editorRunnable = true;
UnityEditor.EditorApplication.update += UpdateRunnable;
}
}
static void UpdateRunnable() {
if (!Application.isPlaying)
Instance.UpdateRoutines();
}

#endif
#endregion

#region Private Types
/// <summary>
/// This class handles a running co-routine.
/// </summary>
private class Routine : IEnumerator {
#region Public Properties
public int ID { get; private set; }
public bool Stop { get; set; }
#endregion

#region Private Data
private bool _moveNext = false;
private IEnumerator _enumerator = null;
#endregion

public Routine(IEnumerator a_enumerator) {
_enumerator = a_enumerator;
Runnable.Instance.StartCoroutine(this);
Stop = false;
ID = Runnable.Instance._nextRoutineId++;

Runnable.Instance._routines[ID] = this;
#if ENABLE_RUNNABLE_DEBUGGING
Log.Debug("Runnable.Routine()", "Coroutine {0} started.", ID );
#endif
}

#region IEnumerator Interface
public object Current { get { return _enumerator.Current; } }
public bool MoveNext() {
_moveNext = _enumerator.MoveNext();
if (_moveNext && Stop)
_moveNext = false;

if (!_moveNext) {
Runnable.Instance._routines.Remove(ID); // remove from the mapping
}

return _moveNext;
}
public void Reset() { _enumerator.Reset(); }
#endregion
}
#endregion

#region Private Data
private Dictionary<int, Routine> _routines = new Dictionary<int, Routine>();
private int _nextRoutineId = 1;
#endregion

/// <summary>
/// THis can be called by the user to force all co-routines to get a time slice, this is usually
/// invoked from an EditorApplication.Update callback so we can use runnable in Editor mode.
/// </summary>
public void UpdateRoutines() {
if (_routines.Count > 0) {
// we are not in play mode, so we must manually update our co-routines ourselves
List<Routine> routines = new List<Routine>();
foreach (var kp in _routines)
routines.Add(kp.Value);

foreach (var r in routines)
r.MoveNext();
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/Adrenak.Unex/Scripts/Runtime/Utils/Runnable.cs.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/Adrenak.Unex/Scripts/Runtime/Utils/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using UnityEngine;

namespace Adrenak.Unex {
[Obsolete("Avoid using Runner and instead use Runnable.")]
public class Runner : MonoBehaviour {
/// <summary>
/// Represents the different states in which the instance can be in
Expand Down

0 comments on commit dd671e7

Please sign in to comment.