public void RegListener(IListen listener, Signal signal, bool onceTime = true) { Debug.Log("Signal Bus: New Reg - " + listener + " : " + signal.Id + " : " + signal.SelfName); ListenerData newData = new ListenerData(listener, signal, onceTime); listeners.Add(newData); }
/// <summary> /// Start a new listener and that listens to the identified keycode, and executes it's onInput event when the right input is detected. /// Each listener requires a unique ID to be able to stop the listener later on. /// </summary> /// <param name="id"></param> /// <param name="keyCode"></param> /// <param name="onInput"></param> /// <param name="stopType"></param> public static void StartListening(string id, KeyCode keyCode, Action onInput, InputListenerKeyType keyType = InputListenerKeyType.GetKey, InputListenerStopType stopType = InputListenerStopType.Manual) { ListenerData foundListenerDataWithIdenticalID = GetListenerData(id); if (foundListenerDataWithIdenticalID != null) { Debug.LogError("A listener with id " + id + " already exists!"); return; } ListenerData foundListenerData = GetListenerData(keyCode, keyType, stopType); if (foundListenerData == null) { Dictionary <string, Action> onInputEventsById = new Dictionary <string, Action>(); onInputEventsById.Add(id, onInput); ListenerData listenerData = new ListenerData() { KeyCode = keyCode, KeyType = keyType, StopType = stopType, OnInputEventsById = onInputEventsById, }; listenerDataContainer.Add(listenerData); Coroutine listenerCoroutine = CoroutineHelper.Start(ListenToInput(keyCode, keyType, stopType)); listenerData.ListenerCoroutine = listenerCoroutine; } else { foundListenerData.OnInputEventsById.Add(id, onInput); } }
static ListenerData DebugGetListenerData(object relay, Delegate d) { ListenerData result = null; Dictionary <object, Dictionary <MethodInfo, ListenerData> > byRelay = null; object target = d.Target; if (target == null) { target = _anonymousDummyObject; } if (!_listenerData.TryGetValue(target, out byRelay)) { byRelay = new Dictionary <object, Dictionary <MethodInfo, ListenerData> >(); _listenerData.Add(target, byRelay); } Dictionary <MethodInfo, ListenerData> byMethod = null; if (!byRelay.TryGetValue(relay, out byMethod)) { byMethod = new Dictionary <MethodInfo, ListenerData>(); byRelay.Add(relay, byMethod); } if (!byMethod.TryGetValue(d.Method, out result)) { result = new ListenerData(d.Target, relay, d.Method); byMethod.Add(d.Method, result); } return(result); }
/// <summary> /// 属性变动事件注册 /// </summary> /// <param name="name"></param> /// <param name="callback"></param> virtual public void AddListener(string name, Action <object> callback, int order = -1, int triggerNum = -1, bool isTriggerCacheData = false) { if (!dataMap.ContainsKey(name)) { BDebug.LogError("暂时无数据,提前监听:" + name); } //创建监听数据 var listenerData = new ListenerData(order, triggerNum, callback); // List <ListenerData> callbackList = null; if (!callbackMap.TryGetValue(name, out callbackList)) { callbackList = new List <ListenerData>(); callbackMap[name] = callbackList; } //触发排序插入 bool isadd = false; for (int i = 0; i < callbackList.Count; i++) { var cw = callbackList[i]; if (listenerData.Order < cw.Order) { callbackList.Insert(i, listenerData); isadd = true; break; } } if (!isadd) { callbackList.Add(listenerData); } if (isTriggerCacheData) { List <object> list = null; this.valueCacheMap.TryGetValue(name, out list); if (list != null) { foreach (var value in list) { callback(value); } //置空 this.valueCacheMap[name].Clear(); } } }
private static ListenerData GetListenerData(KeyCode keyCode, InputListenerKeyType keyType, InputListenerStopType stopType) { ListenerData foundListenerData = listenerDataContainer.Find(listenerData => { bool match = keyCode == listenerData.KeyCode && keyType == listenerData.KeyType && stopType == listenerData.StopType; return(match); }); return(foundListenerData); }
public void RegisterListenerDataWithListener(ListenerData listenerData) { ContractUtility.Requires <ArgumentNullException>(listenerData.ListenerCreationDataAndEventSources.IsNotNull(), "listenerData.ListenerCreationDataAndEventSources instance cannot be null"); ContractUtility.Requires <ArgumentNullException>(listenerData.ListenerCreationDataAndEventSources.ListenerCreationData.IsNotNull(), "The " + "listenerData.ListenerCreationDataAndEventSources.ListenerCreationData instance cannot be null"); if (Bootstrapper.CheckForPermissibleBootstrapedListenerType(listenerData)) { IListener listener = Container.Instance.Resolve <IListener>(listenerData.ListenerType.ToString()); listener.RegisterListener(listenerData.ListenerCreationDataAndEventSources); } }
private void DeleteListener(ListenerData data) { for (int i = 0; i < listeners.Count; i++) { if (listeners[i] == data) { Debug.Log("Signal Bus: Listener Data Delete - " + data.Listener + " : " + data.Signal); listeners.Remove(data); } } }
void MarkDirtyStaticEmoji() { if (alltexts != null && alltexts.Count > 0) { if (listeners == null) { listeners = new Dictionary <InlineText, ListenerData>(); } for (int i = 0; i < alltexts.Count; ++i) { InlineText text = alltexts[i]; List <IFillData> emojidata = text.PopEmojiData(); if (text != null && emojidata != null && emojidata.Count > 0 && allatlas != null && allatlas.Count > 0) { bool isdirty = false; ListenerData data = null; if (listeners.TryGetValue(text, out data)) { isdirty = data.Dirty(text); if (isdirty) { data.Set(text); } } else { data = new ListenerData(); data.Set(text); listeners.Add(text, data); } int staticcnt = 0; for (int j = 0; j < emojidata.Count; ++j) { IFillData taginfo = emojidata[j]; SpriteAsset asset = null; SpriteInfoGroup groupinfo = manager.FindSpriteGroup(taginfo.Tag, out asset); if (groupinfo != null && groupinfo.spritegroups.Count == 1) { staticcnt++; } } if (staticcnt > 0 && isdirty) { this.TryRendering(text); } } } } }
/// <summary> /// Check if a listener with ID exists. /// </summary> /// <param name="keyCode"></param> /// <param name="stopType"></param> /// <param name="id"></param> /// <returns></returns> public static bool ContainsListenerWithId(string id) { ListenerData foundListenerData = GetListenerData(id); if (foundListenerData == null) { return(false); } if (!foundListenerData.OnInputEventsById.ContainsKey(id)) { return(false); } return(true); }
private static IEnumerator ListenToInput(KeyCode keyCode, InputListenerKeyType keyType, InputListenerStopType stopType) { Func <KeyCode, bool> keyCheck = null; switch (keyType) { case InputListenerKeyType.GetKeyDown: keyCheck = x => Input.GetKeyDown(x); break; case InputListenerKeyType.GetKey: keyCheck = x => Input.GetKey(x); break; case InputListenerKeyType.GetKeyUp: keyCheck = x => Input.GetKeyUp(x); break; } while (true) { while (!keyCheck(keyCode)) { yield return(null); } ListenerData listenerData = GetListenerData(keyCode, keyType, stopType); List <Action> onInputs = listenerData.OnInputEventsById.Values.ToList(); foreach (Action onInput in onInputs) { if (onInput != null) { onInput(); } } if (stopType == InputListenerStopType.Automatic) { break; } yield return(null); } }
private void CheckListener() { List <int> frstList = new List <int>(); List <int> scndList = new List <int>(); for (int i = 0; i < frstList.Count; i++) { int x = scndList.BinarySearch(frstList[i]); if (x >= 0) { } } if (listeners.Count > 0 && currentsSignal.Count > 0) { for (int i = 0; i < listeners.Count; i++) { ListenerData data = listeners[i]; for (int a = 0; a < currentsSignal.Count; a++) { Signal signal = currentsSignal[a]; if (data.Signal.SelfName == signal.SelfName || data.Signal.Id == signal.Id) { Debug.Log("Signal Bus: Sig Found - " + data.Listener + " : " + signal.Id + " : " + signal.SelfName); data.Listener.ItIsMySignal(signal); if (data.Signal.OnceTime == true) { DeleteListener(data); DeleteSignal(signal); } } } } } }
private void ProcessListenersToBeAttached() { //Process listeners to be attached if (m_listenerToBeAttached.Count > 0) { for (int i = 0; i < m_listenerToBeAttached.Count; ++i) { ListenerData data = m_listenerToBeAttached[i]; if (data.Listener != null) { if (data.Handler == null) { AddListener(data.Listener, data.EventName); } else { AddListener(data.Listener, data.Handler, data.EventName); } } } m_listenerToBeAttached.Clear(); } }
/// <summary> /// Stop listening to the input specified by the ID. /// </summary> /// <param name="id"></param> /// <param name="keyCode"></param> /// <param name="stopType"></param> /// <param name="debug"></param> public static void StopListening(string id, bool debug = false) { ListenerData foundListenerData = GetListenerData(id); if (foundListenerData == null) { if (debug) { Debug.LogError("Can't stop listener with id " + id); } return; } foundListenerData.OnInputEventsById[id] = null; foundListenerData.OnInputEventsById.Remove(id); if (foundListenerData.OnInputEventsById.Count > 0) { return; } CoroutineHelper.Stop(foundListenerData.ListenerCoroutine); listenerDataContainer.Remove(foundListenerData); }
public void AddListener(IInputListener listener, int priority = 0) { foreach(ListenerData l in m_listeners) { // don't add duplicate listeners if(l.Listener == listener) { return; } } ListenerData ld = new ListenerData(); ld.Listener = listener; ld.Priority = priority; m_listeners.Add(ld); m_listeners.Sort( delegate(ListenerData p1, ListenerData p2) { return p1.Priority.CompareTo(p2.Priority); } ); }
private static ListenerData GetListenerData(string id) { ListenerData foundListenerData = listenerDataContainer.Find(listenerData => listenerData.OnInputEventsById.ContainsKey(id)); return(foundListenerData); }
private void TriggerListenerExt(bool isSilently, string type, params object[] args) { string argsStr = ""; if (args != null) { for (int index = 0; index < args.Length; index++) { if (index != 0) { argsStr += ", "; } argsStr += args[index] == null ? "null" : args[index].ToString(); } } if (!isSilently) { Debugger.Log("Trigger " + type + ": " + argsStr); } RemoveInvalidListener(type); Type[] argTypes = new Type[args.Length]; for (int index = 0; index < args.Length; index++) { if (args[index] == null) { argTypes[index] = null; } else { argTypes[index] = args[index].GetType(); } } if (mListenerDict.ContainsKey(type)) { List <Delegate> list = new List <Delegate>(); foreach (ExpandDelegate epDelegate in mListenerDict[type].Values) { if (epDelegate == null) { continue; } if (epDelegate.Listener != null) { Type[] types = epDelegate.Listener.GetType().GetGenericArguments(); if (IsTypesAssignableFrom(types, argTypes)) { list.Add(epDelegate.Listener); } } if (epDelegate.OncePerFrameListener != null) { Type listenerDataDefinitionType = null; switch (argTypes.Length) { case 0: listenerDataDefinitionType = typeof(ListenerData); break; case 1: listenerDataDefinitionType = typeof(ListenerData <>); break; case 2: listenerDataDefinitionType = typeof(ListenerData <,>); break; case 3: listenerDataDefinitionType = typeof(ListenerData <, ,>); break; case 4: listenerDataDefinitionType = typeof(ListenerData <, , ,>); break; } if (listenerDataDefinitionType != null) { Type listenerDataType = listenerDataDefinitionType.MakeGenericType(argTypes); Type elementType = GetFirstArgumentElementType(epDelegate.OncePerFrameListener); if (elementType.IsAssignableFrom(listenerDataType)) { ConstructorInfo constructor = listenerDataType.GetConstructor(argTypes); if (constructor != null) { ListenerData listenerData = constructor.Invoke(args) as ListenerData; epDelegate.OncePerFrameArgList.Add(listenerData); } } } } } foreach (Delegate listener in list) { #region For lua&WebGL switch (args.Length) { case 0: if (listener is Action) { (listener as Action)(); } continue; case 1: if (listener is Action <object> ) { (listener as Action <object>)(args[0]); } continue; case 2: if (listener is Action <object, object> ) { (listener as Action <object, object>)(args[0], args[1]); } continue; case 3: if (listener is Action <object, object, object> ) { (listener as Action <object, object, object>)(args[0], args[1], args[2]); } continue; case 4: if (listener is Action <object, object, object, object> ) { (listener as Action <object, object, object, object>)(args[0], args[1], args[2], args[3]); } continue; } #endregion listener.DynamicInvoke(args); } } }