private async Task <TWidget> GetInstance <TWidget>(string widgetPath, UIMessage message, IWidgetFactory factory = null) where TWidget : Widget { if (PoolingWidgets.ContainsKey(widgetPath)) { var pool = PoolingWidgets[widgetPath]; if (pool.Count > 0) { var instance = pool.Pop(); return(instance as TWidget); } } if (factory == null) { factory = DefaultFactory; } TWidget widget; try { widget = (TWidget)await factory.CreateInstance(this, widgetPath, message); } catch (Exception e) { Her.Error(e); throw; } // mark widget factory FactoryLookup.Add(widget.ViewId, factory); return(widget); }
protected override IState BuildState() { var root = new State(); var context = Context.GlobalContext; var types = GetType().Assembly.GetTypes() .Where(x => typeof(GameProcedure <TProcedureController, TProcedureIndex>).IsAssignableFrom(x)); var procedures = new List <GameProcedure <TProcedureController, TProcedureIndex> >(); foreach (var type in types) { if (!(context.Create(type) is GameProcedure <TProcedureController, TProcedureIndex> instance)) { continue; } instance.SetContext((TProcedureController)this); procedures.Add(instance); } procedures = procedures.OrderBy(x => x.Index).ToList(); foreach (var procedure in procedures) { var id = procedure.Index; if (Indices.ContainsKey(id)) { Debug.LogErrorFormat("{0}[{1}] already added.", id, procedure.GetType().Name); continue; } Indices.Add(id, procedure); IndexLookup.Add(procedure, id); root.AddChild(id.ToString(CultureInfo.InvariantCulture), procedure); } Root = root; if (procedures.Count <= 0) { return(Root); } if (procedures.Any(p => p.Index.Equals(InitState))) { ChangeState(InitState); } else { var first = procedures[0].Index; Her.Warn($"Procedure of [{InitState}] is no available, change to {first} instead."); ChangeState(first); } return(Root); }
public object Convert(object fromObj, AdapterOptions options) { try { return(options.Convert(fromObj)); } catch (Exception e) { Her.Error(e); throw; } }
private void Start() { if (_storeValue == null || string.IsNullOrEmpty(_storeId) || string.IsNullOrEmpty(_storeKey)) { return; } var store = Her.GetStore(_storeId); store.Set(_storeKey, _storeValue); }
public void SetViewModel(object context) { if (context is TViewModel viewModel) { DataContext = viewModel; } else { Her.Warn($"{context} is not matching {typeof(TViewModel)}"); } DataReadyEvent?.Invoke(); }
public async Task <Widget> CreateInstance(IUIStack manager, string name, UIMessage message) { var loader = Her.Resolve <IViewLoader>(); var prefab = await loader.LoadView(name); if (prefab == null) { throw new Exception($"Load view: {name} failed"); } var instance = Object.Instantiate(prefab).GetComponent <Widget>(); instance.SetManagerInfo(name, manager, message); return(instance); }
protected override void Awake() { if (_instance == null) { _instance = this; RegisterServices(); InitServices(); Inject(Her.Current); } else { Her.Warn("HermitKernel can only have one instance."); Destroy(gameObject); } }
private void Start() { _viewManager = Her.Resolve <IViewManager>(); }
public virtual void CleanUpViewInfo() { Her.Resolve <IViewManager>().UnRegister(ViewId); }
public virtual void SetUpViewInfo() { ViewId = Her.Resolve <IViewManager>().Register(this); }