public static void Initialize <I, O>(this ITemporaryUI <I, O> ui, I args, EventHandler <EventArgs <O> > callback, IProcess parent) { if (ui is Component) { if (ui is IHasCanvasBranch) { MainCanvas.main.Add(((Component)ui).transform, ((IHasCanvasBranch)ui).Branch); } else { MainCanvas.main.Add(((Component)ui).transform); } } if (ui is IDebugMethods) { var methods = ((IDebugMethods)ui).GetMethods().ToArray(); DebugMenuListener.AddContextMethods(methods); ui.Complete += (s, e) => DebugMenuListener.RemoveContextMethods(methods); } if (callback != null) { EventHandler <EventArgs <O> > handler = null; ProcessExitCallback exitCallback = (s, e) => { ui.Complete -= handler; Debug.Log("Detaching UI callback: " + ui + "; " + parent); }; handler = (s, e) => { if (parent != null) { parent.OnExit -= exitCallback; } callback(s, e); //Debug.Log("Detaching process callback: " + i + "; " + parent); }; ui.Complete += handler; if (parent != null) { parent.OnExit += exitCallback; } } ui.Initialize(args); }
static void Run_Internal <I, O>(this IProcess <I> process, I input, ProcessExitCallback <O> callback, IProcess parent) { if (process is ISettableParent) { ((ISettableParent)process).SetParent(parent); } ProcessExitCallback castCallback = null; if (parent != null) { ProcessExitCallback forceChildExit = (s, e) => { process.OnExit -= castCallback; process.ForceExit(); }; ProcessExitCallback detachChild = (s, e) => parent.OnExit -= forceChildExit; process.OnExit += detachChild; process.OnExit += (s, e) => RemoveAllEvents(process); parent.OnExit += forceChildExit; } if (callback != null) { castCallback = (s, e) => callback(s, (O)e); process.OnExit += castCallback; } if (process is IDebugMethods) { var methods = ((IDebugMethods)process).GetMethods().ToArray(); DebugMenuListener.AddContextMethods(methods); process.OnExit += (s, e) => DebugMenuListener.RemoveContextMethods(methods); } process.Initialize(input); }