private Uri ResolvePrefabFromViewType(Type viewType, VariantSet requestedVariants) { var candidates = _manifest.ViewsToPrefabs .Where(x => viewType == x.Source) .ToList(); var resolved = candidates .Select(candidate => new CandidateMapping <TypeToUriMapping>(candidate, _scoreEvaluator.GetVariantScore(requestedVariants, candidate.Variants, VariantSet.Empty))) .Where(candidate => candidate.Score.HasValue) .WithMax(candidate => candidate.Score.Value) ?.Mapping; if (resolved == null) { throw new InvalidOperationException($"Failed to resolve View {viewType} to some Prefab (Variants: {requestedVariants})"); } if (Log.IsDebugEnabled) { Log.Debug($"Resolved View {viewType} to Prefab {resolved.Target} (Variants: {resolved.Variants})"); } if (candidates.Count > 1 && Log.IsDebugEnabled) { Log.Debug($"Other candidates were:{Environment.NewLine}" + $"{candidates.Except(resolved).JoinAsString(Environment.NewLine)}"); } return(resolved.Target); }
private ViewInfo ResolveFromViewModel(IViewModel viewModel, VariantSet requestedVariants) { try { return(ResolveFromModel(viewModel, requestedVariants)); } catch (InvalidOperationException) { } if (Log.IsDebugEnabled) { Log.Debug($"Resolving viewModel: {viewModel}"); } var viewModelType = viewModel.GetType(); var viewType = ResolveTargetType(viewModelType, "ViewModel", "View", _manifest.ViewModelsToViews, requestedVariants); var prefabUri = ResolvePrefabFromViewType(viewType, requestedVariants); return(new ViewInfo { ViewModel = viewModel, ViewModelType = viewModelType, ViewType = viewType, PrefabUri = prefabUri }); }
public TypeToTypeMapping(Type source, Type target, VariantSet variants) : base(source, variants) { if (target == null) { throw new ArgumentNullException(nameof(target)); } _target = target; }
public TypeToUriMapping(Type source, Uri target, VariantSet variants) : base(source, variants) { if (target == null) { throw new ArgumentNullException(nameof(target)); } _target = target.ToString(); }
public ViewToPrefabMapping(Type source, Uri target, string guid, VariantSet variants) : base(source, variants) { if (target == null) { throw new ArgumentNullException(nameof(target)); } _target = target.ToString(); _guid = guid; }
private ViewInfo ResolveFromInstance(object instance, VariantSet requestedVariants) { if (instance is IView) { return(ResolveFromView((IView)instance, requestedVariants)); } if (instance is IViewModel) { return(ResolveFromViewModel((IViewModel)instance, requestedVariants)); } return(ResolveFromModel(instance, requestedVariants)); }
private ViewInfo ResolveFromViewType(Type viewType, VariantSet requestedVariants) { if (Log.IsDebugEnabled) { Log.Debug($"Resolving viewType: {viewType}"); } var prefabUri = ResolvePrefabFromViewType(viewType, requestedVariants); return(new ViewInfo { ViewType = viewType, PrefabUri = prefabUri }); }
public Mapping(Type source, VariantSet variants) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (variants == null) { throw new ArgumentNullException(nameof(variants)); } _source = source; _variants = variants; }
private ViewInfo ResolveFromViewModelType(Type viewModelType, VariantSet requestedVariants) { if (Log.IsDebugEnabled) { Log.Debug($"Resolving viewModelType: {viewModelType}"); } var viewType = ResolveTargetType(viewModelType, "ViewModel", "View", _manifest.ViewModelsToViews, requestedVariants); var prefabUri = ResolvePrefabFromViewType(viewType, requestedVariants); return(new ViewInfo { ViewModelType = viewModelType, ViewType = viewType, PrefabUri = prefabUri }); }
public int?GetVariantScore(VariantSet requestedVariants, VariantSet candidateVariants, VariantSet candidateImplicitVariants) { var score = ZeroScore; var excessVariants = new HashSet <IVariant>(candidateVariants); foreach (var requestedVariant in requestedVariants) { // Matches exact variant? if (candidateVariants.Contains(requestedVariant)) { score += ExplicitVariantScore; excessVariants.Remove(requestedVariant); } // Matches another variant in same group? (fail!) else if (candidateVariants.Any(x => x.Group == requestedVariant.Group)) { return(null); } // Matches implicit variant? else if (candidateImplicitVariants.Contains(requestedVariant)) { score += ImplicitVariantScore; } // Matches another implicit variant in same group? else if (candidateImplicitVariants.Any(x => x.Group == requestedVariant.Group)) { score -= IncorrectImplicitVariantPenality; } // No variant specified for that group (is a fallback) else { score += FallbackVariantScore; } } score -= excessVariants.Count * ExcessVariantPenality; return(score); }
private ViewInfo ResolveFromModel(object model, VariantSet requestedVariants) { if (Log.IsDebugEnabled) { Log.Debug($"Resolving model: {model}"); } var modelType = model.GetType(); var viewModelType = ResolveTargetType(modelType, "Model", "ViewModel", _manifest.ModelsToViewModels, requestedVariants); var viewType = ResolveTargetType(viewModelType, "ViewModel", "View", _manifest.ViewModelsToViews, requestedVariants); var prefabUri = ResolvePrefabFromViewType(viewType, requestedVariants); return(new ViewInfo { Model = model, ModelType = modelType, ViewModelType = viewModelType, ViewType = viewType, PrefabUri = prefabUri }); }
private void ShowVariants(VariantSet manifestAllVariants) { var groups = manifestAllVariants .GroupBy(x => x.Group) .OrderBy(x => x.Key.Name); foreach (var group in groups) { EditorGUILayout.BeginHorizontal(); GUILayout.Label(group.Key.Name, _normalStyle); GUILayout.Label(" : ", _mappingArrowStyle); var formattedVariants = group .Select(x => $"[{x.Name}]") .JoinAsString(" "); GUILayout.Label(formattedVariants, _explicitVariantStyle); EditorGUILayout.EndHorizontal(); } }
private ViewInfo ResolveFromModel(object model, VariantSet requestedVariants) { if (Log.IsDebugEnabled) { Log.Debug($"Resolving model: {model}"); } var viewModelTypeMapping = ResolveViewModelFromModel(model.GetType(), requestedVariants); var modelType = viewModelTypeMapping.Source; var viewModelType = viewModelTypeMapping.Target; var viewType = ResolveViewFromViewModel(viewModelType, requestedVariants).Target; var prefabUri = ResolvePrefabFromViewType(viewType, requestedVariants); return(new ViewInfo { Model = model, ModelType = modelType, ViewModelType = viewModelType, ViewType = viewType, PrefabUri = prefabUri }); }
private TypeToTypeMapping ResolveViewModelFromModel(Type type, VariantSet requestedVariants) => ResolveTypeMapping(type, "Model", "ViewModel", _manifest.ModelsToViewModels, requestedVariants);
public static IPresenter With(this IPresenter This, VariantSet variants) => new VariantsPresenterDecorator(This, variants);
public static int?GetScore(this IScoreEvaluator This, Type candidateType, VariantSet candidateVariants, VariantSet candidateImplicitVariants, Type requestedType, VariantSet requestedVariants) => This.GetVariantScore(requestedVariants, candidateVariants, candidateImplicitVariants) + This.GetTypeScore(requestedType, candidateType);
private TypeToTypeMapping ResolveViewFromViewModel(Type viewModelType, VariantSet requestedVariants) => ResolveTypeMapping(viewModelType, "ViewModel", "View", _manifest.ViewModelsToViews, requestedVariants);
private Type ResolveTargetType(Type type, string sourceKind, string targetKind, List <TypeToTypeMapping> mappings, VariantSet requestedVariants) { if (type == null) { throw new ArgumentNullException(nameof(type)); } var candidates = mappings .Where(x => type.IsAssignableTo(x.Source)) .ToList(); var resolved = candidates .Select(candidate => new CandidateMapping <TypeToTypeMapping>( candidate, _scoreEvaluator.GetScore(candidate.Source, candidate.Variants, candidate.ImplicitVariants, type, requestedVariants))) .Where(candidate => candidate.Score.HasValue) .WithMax(candidate => candidate.Score.Value) ?.Mapping; if (resolved == null) { throw new InvalidOperationException($"Failed to resolve {sourceKind} {type} to some {targetKind} (Variants: {requestedVariants})"); } if (Log.IsDebugEnabled) { Log.Debug($"Resolved {sourceKind} {type} to {targetKind} {resolved.Target} (Variants: {resolved.Variants})"); } if (candidates.Count > 1 && Log.IsDebugEnabled) { Log.Debug($"Other candidates were:{Environment.NewLine}" + $"{candidates.Except(resolved).JoinAsString(Environment.NewLine)}"); } return(resolved.Target); }