/// <summary>
        /// Gets a context value local to a drawer type for a given key, and creates a new instance of <see cref="T" />
        /// as a default value if the context doesn't already exist.
        /// </summary>
        /// <returns>Returns true if a new context was created.</returns>
        public bool Get <TValue>(OdinDrawer drawerInstance, string key, out PropertyContext <TValue> context)
        {
            bool isNew;

            this.TryGetDrawerContext(drawerInstance.GetType(), key, out context, out isNew);

            return(isNew);
        }
        /// <summary>
        /// Gets a context value local to a drawer type for a given key, and creates a new instance of <see cref="T" /> as a default value if the context doesn't already exist.
        /// </summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="drawerInstance">An instance of the drawer type linked to the context value to get.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns>
        /// The found context.
        /// </returns>
        /// <exception cref="ArgumentNullException">drawerInstance</exception>
        /// <exception cref="System.ArgumentNullException">drawerInstance is null</exception>
        public PropertyContext <TValue> Get <TValue>(OdinDrawer drawerInstance, TValue defaultValue)
        {
            if (drawerInstance == null)
            {
                throw new ArgumentNullException("drawerInstance");
            }

            return(this.Get(drawerInstance.GetType(), "_", defaultValue));
        }
        /// <summary>
        /// Gets a context value local to a drawer type for a given key, and creates a new instance of <see cref="T" /> as a default value if the context doesn't already exist.
        /// </summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="drawerInstance">An instance of the drawer type linked to the context value to get.</param>
        /// <returns>
        /// The found context.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">drawerInstance is null</exception>
        public PropertyContext <TValue> Get <TValue>(OdinDrawer drawerInstance) where TValue : new()
        {
            if (drawerInstance == null)
            {
                throw new ArgumentNullException("drawerInstance");
            }

            return(this.Get <TValue>(drawerInstance.GetType(), "_"));
        }
        /// <summary>
        /// <para>Gets a context value local to a drawer type for a given key, using a given delegate to generate a default value if the context doesn't already exist.</para>
        /// </summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="drawerInstance">An instance of the drawer type linked to the context value to get.</param>
        /// <param name="key">The key of the context value to get.</param>
        /// <param name="getDefaultValue">A delegate for generating a default value.</param>
        /// <returns>
        /// The found context.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">drawerInstance is null</exception>
        public PropertyContext <TValue> Get <TValue>(OdinDrawer drawerInstance, string key, Func <TValue> getDefaultValue)
        {
            if (drawerInstance == null)
            {
                throw new ArgumentNullException("drawerInstance");
            }

            return(this.Get(drawerInstance.GetType(), key, getDefaultValue));
        }
Пример #5
0
        /// <summary>
        /// <para>Gets a temporary context value local to a drawer type for a given key, and creates a new instance of <see cref="T" /> as a default value if the context doesn't already exist.</para>
        /// <para>Temporary context values are reset at the start of every GUI frame; arrays are set to default values, collections are cleared, and context types that implement <see cref="ITemporaryContext" /> have <see cref="ITemporaryContext.Reset" /> called.</para>
        /// </summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="drawerInstance">An instance of the drawer type linked to the context value to get.</param>
        /// <param name="key">The key of the context value to get.</param>
        /// <returns>
        /// The found context.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">drawerInstance is null</exception>
        public TemporaryPropertyContext <TValue> GetTemporary <TValue>(OdinDrawer drawerInstance, string key) where TValue : new()
        {
            if (drawerInstance == null)
            {
                throw new ArgumentNullException("drawerInstance");
            }

            return(this.GetTemporary <TValue>(drawerInstance.GetType(), key));
        }
        /// <summary>
        /// Gets a context value local to a drawer type for a given key, and creates a new instance of <see cref="T" />
        /// as a default value if the context doesn't already exist.
        /// </summary>
        /// <returns>Returns true if a new context was created.</returns>
        public bool Get <TValue>(OdinDrawer drawerInstance, string key, out TValue context)
            where TValue : class, new()
        {
            PropertyContext <TValue> pContext;
            bool isNew;

            this.TryGetDrawerContext(drawerInstance.GetType(), key, out pContext, out isNew);

            if (isNew)
            {
                pContext.Value = new TValue();
            }

            context = pContext.Value;

            return(isNew);
        }
        /// <summary>
        /// Gets a persistent value that will survive past multiple Unity Editor Application sessions.
        /// The value is stored in the PersistentContextCache, which has a customizable max cache size.
        /// </summary>
        public static LocalPersistentContext <T> GetPersistentValue <T>(this OdinDrawer drawer, string key, T defaultValue = default(T))
        {
            var a = TwoWaySerializationBinder.Default.BindToName(drawer.GetType());
            var b = TwoWaySerializationBinder.Default.BindToName(drawer.Property.Tree.TargetType);
            var c = drawer.Property.Path;
            var d = new DrawerStateSignature(drawer.Property.RecursiveDrawDepth, InlineEditorAttributeDrawer.CurrentInlineEditorDrawDepth, drawer.Property.DrawerChainIndex);
            var e = key;

            GlobalPersistentContext <T> global;

            if (PersistentContext.Get(a, b, c, d, e, out global))
            {
                global.Value = defaultValue;
            }

            return(LocalPersistentContext <T> .Create(global));
        }
        /// <summary>
        /// Gets a <see cref="GlobalPersistentContext{T}"/> object and creates a <see cref="LocalPersistentContext{T}"/> object for it.
        /// Returns <c>true</c> when the <see cref="GlobalPersistentContext{T}"/> is first created. Otherwise <c>false</c>.
        /// </summary>
        /// <typeparam name="TValue">The type of the value of the context.</typeparam>
        /// <param name="drawer">The instance of the drawer.</param>
        /// <param name="key">The key for the context.</param>
        /// <param name="context">The <see cref="LocalPersistentContext{T}"/> object.</param>
        /// <returns>Returns <c>true</c> when the <see cref="GlobalPersistentContext{T}"/> is first created. Otherwise <c>false</c>.</returns>
        public bool GetPersistent <TValue>(OdinDrawer drawer, string key, out LocalPersistentContext <TValue> context)
        {
            bool isNew = false;
            PropertyContext <LocalPersistentContext <TValue> > propertyContext;

            if (this.Get <LocalPersistentContext <TValue> >(drawer, key, out propertyContext))
            {
                GlobalPersistentContext <TValue> global;
                isNew = PersistentContext.Get(
                    TwoWaySerializationBinder.Default.BindToName(drawer.GetType()),
                    TwoWaySerializationBinder.Default.BindToName(this.property.Tree.TargetType),
                    this.property.Path,
                    new DrawerStateSignature(this.property.RecursiveDrawDepth, InlineEditorAttributeDrawer.CurrentInlineEditorDrawDepth, this.property.DrawerChainIndex),
                    key,
                    out global);

                propertyContext.Value = LocalPersistentContext <TValue> .Create(global);
            }

            context = propertyContext.Value;

            return(isNew);
        }
        /// <summary>
        /// Gets a <see cref="GlobalPersistentContext{T}"/> object and creates a <see cref="LocalPersistentContext{T}"/> object for it.
        /// </summary>
        /// <typeparam name="TValue">The type of the value of the context.</typeparam>
        /// <param name="drawer">The instance of the drawer.</param>
        /// <param name="key">The key for the context.</param>
        /// <param name="defaultValue">The default value for the context.</param>
        public LocalPersistentContext <TValue> GetPersistent <TValue>(OdinDrawer drawer, string key, TValue defaultValue)
        {
            PropertyContext <LocalPersistentContext <TValue> > context;

            if (this.Get <LocalPersistentContext <TValue> >(drawer, key, out context))
            {
                context.Value = LocalPersistentContext <TValue> .Create(PersistentContext.Get(
                                                                            TwoWaySerializationBinder.Default.BindToName(drawer.GetType()),
                                                                            TwoWaySerializationBinder.Default.BindToName(this.property.Tree.TargetType),
                                                                            this.property.Path,
                                                                            new DrawerStateSignature(this.property.RecursiveDrawDepth, InlineEditorAttributeDrawer.CurrentInlineEditorDrawDepth, this.property.DrawerChainIndex),
                                                                            key,
                                                                            defaultValue));
            }

            return(context.Value);
        }