Exemplo n.º 1
0
 /// <summary>Initializes a new instance of the MappedInputController class</summary>
 public MappedInputController(IControllerTarget target, IConfig config) : base(target, config)
 {
     foreach (var mapping in this.InputMappings)
     {
         InputManager.AddMappedInputHandler(mapping.Key, this.Id, mapping.Value);
     }
 }
Exemplo n.º 2
0
        /// <summary>Creates an entity controller instance</summary>
        /// <param name="controllerId">Controller identifier</param>
        /// <param name="settings">Controller configuration</param>
        /// <param name="target">Controller target</param>
        /// <returns>The created IGame instance</returns>
        public virtual IController Create(string controllerId, IConfig settings, IControllerTarget target)
        {
#if DEBUG
            if (!this.constructors.ContainsKey(controllerId))
            {
                throw new ArgumentOutOfRangeException(
                          "controllerConfig", "No controller found with id '{0}'.".FormatInvariant(controllerId));
            }
#endif

            var ctor = this.constructors[controllerId];

            //// Log.Trace("Creating controller '{0}' ({1}) with target '{2}'\nSettings: {3}", controllerId, ctor.DeclaringType.FullName, target.Id, settings);

            var defaultParameters = new Dictionary <Type, object>
            {
                { typeof(IResourceLibrary), this.resources },
                { typeof(TControllerTarget), target },
                { typeof(IConfig), settings },
            };
            var parameters = ctor.GetParameters().Select(p => p.ParameterType)
                             .Select(t =>
                                     defaultParameters.ContainsKey(t)  ? defaultParameters[t] :
                                     t.IsArray ? GlobalContainer.ResolveAll(t) :
                                     GlobalContainer.Resolve(t))
                             .ToArray();

            var controller = (IController)ctor.Invoke(parameters);
            this.controllerManager.AddController(controller);
            return(controller);
        }
Exemplo n.º 3
0
        /// <summary>Creates controllers for the object</summary>
        /// <param name="controllerFactories">Controller factories</param>
        /// <param name="controllers">Primary controller configurations</param>
        /// <param name="customControllers">Additional controller configurations/setting overrides</param>
        protected static TController[] CreateControllers(
            IControllerFactory[] controllerFactories,
            ControllerConfig[] controllers,
            ControllerConfig[] customControllers,
            IControllerTarget target)
        {
            if (controllers == null)
            {
                throw new ArgumentNullException("controllers");
            }
            if (customControllers == null)
            {
                throw new ArgumentNullException("customControllers");
            }

            // Find the additional controllers that are actually
            // overrides for built-in controllers' settings
            var overrides = customControllers
                            .Where(cc =>
                                   controllers.Any(bicc => cc.ControllerId == bicc.ControllerId))
                            .ToDictionary(
                cc => cc.ControllerId,
                cc => new DictionaryConfig(cc.Settings));

            // Create a dictionary of controller configs keyed by their ids
            // Start with the built-in controllers, then the non-overrides.
            // Next, merge in settings overrides where available.
            var controllersToCreate = controllers
                                      .ToDictionary(
                cc => cc.ControllerId,
                cc => new DictionaryConfig(cc.Settings))
                                      .Concat(customControllers
                                              .Where(cc => !overrides.ContainsKey(cc.ControllerId))
                                              .ToDictionary(
                                                  cc => cc.ControllerId,
                                                  cc => new DictionaryConfig(cc.Settings)))
                                      .ToDictionary(
                kvp => kvp.Key,
                kvp => overrides.ContainsKey(kvp.Key) ?
                kvp.Value.Merge(overrides[kvp.Key]) : kvp.Value);

            var controllerFactory = controllerFactories.FirstOrDefault(f => target.GetType().GetInterfaces().Any(t => t == f.TargetType));

            if (controllerFactory == null)
            {
                var message = "No factories found for controllers with targets of type {0} (factories: {1})".FormatInvariant(target.GetType().FullName, string.Join(", ", controllerFactories.Select(f => f.TargetType.FullName).ToArray()));
                throw new ArgumentException(message, "controllerFactories");
            }

            return(controllersToCreate
                   .Select(kvp =>
                           controllerFactory.Create(kvp.Key, kvp.Value, target))
                   .Cast <TController>()
                   .ToArray());
        }
Exemplo n.º 4
0
        /// <summary>Initializes a new instance of the Controller class</summary>
        /// <param name="target">Target to be controlled</param>
        /// <param name="config">Controller configuration</param>
        public Controller(IControllerTarget target, IConfig config)
        {
            var controllerId =
                this.GetType().GetCustomAttributes(typeof(ControllerAttribute), false)
                .Cast <ControllerAttribute>()
                .Select(attr => attr.Id)
                .FirstOrDefault();

            this.Id     = new RuntimeId(controllerId);
            this.Target = target;
            this.Config = config;
        }