Example #1
0
        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);
        }
Example #2
0
        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();
        }
Example #5
0
        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;
        }
Example #6
0
        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));
        }
Example #7
0
        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
            });
        }
Example #8
0
        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;
        }
Example #9
0
        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
            });
        }
Example #10
0
        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);
        }
Example #11
0
        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
            });
        }
Example #12
0
        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();
            }
        }
Example #13
0
        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
            });
        }
Example #14
0
 private TypeToTypeMapping ResolveViewModelFromModel(Type type, VariantSet requestedVariants) =>
 ResolveTypeMapping(type, "Model", "ViewModel", _manifest.ModelsToViewModels, requestedVariants);
Example #15
0
 public static IPresenter With(this IPresenter This, VariantSet variants) =>
 new VariantsPresenterDecorator(This, variants);
Example #16
0
 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);
Example #17
0
 private TypeToTypeMapping ResolveViewFromViewModel(Type viewModelType, VariantSet requestedVariants) =>
 ResolveTypeMapping(viewModelType, "ViewModel", "View", _manifest.ViewModelsToViews, requestedVariants);
Example #18
0
        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);
        }