Exemple #1
0
        protected override void OnUpdateBinding(UTinyEntity entity, UTinyObject component)
        {
            var renderer = GetComponent <SpriteRenderer>(entity);

            renderer.size     = component.GetProperty <Vector2>("size");
            renderer.drawMode = Translate(component.GetProperty <TileMode>("mode"));
        }
        public override void Create(UTinyEntityView view, SpriteRenderer spriteRenderer)
        {
            var sr = new UTinyObject(Registry, GetMainTinyType());

            SyncRenderer(spriteRenderer, sr);

            UTinyObject srt = null;

            if (spriteRenderer.drawMode != SpriteDrawMode.Simple)
            {
                srt = new UTinyObject(Registry, Registry.GetSprite2DRendererOptionsType());
                SyncRendererOptions(spriteRenderer, srt);
            }

            var entity           = view.EntityRef.Dereference(Registry);
            var sprite2DRenderer = entity.GetOrAddComponent(GetMainTinyType());

            sprite2DRenderer.CopyFrom(sr);
            BindingsHelper.RunBindings(entity, sprite2DRenderer);

            if (null != srt)
            {
                var rendererOptions = entity.GetOrAddComponent(Registry.GetSprite2DRendererOptionsType());
                rendererOptions.CopyFrom(srt);
                BindingsHelper.RunBindings(entity, rendererOptions);
            }
        }
Exemple #3
0
        private static void SetColorProperty(UTinyObject rendererComponent, MaterialPropertyBlock block)
        {
            Color color = Color.white;

            color = rendererComponent.GetProperty <Color>("color");
            block.SetColor("_Color", color);
        }
Exemple #4
0
            protected override void OnUpdateBinding(UTinyEntity entity, UTinyObject component)
            {
                var particleSystem = GetComponent <ParticleSystem>(entity);
                var registry       = entity.Registry;

                // [MP] @TODO: At some point, we'll have more than one type of source.
                var shape = particleSystem.shape;

                shape.shapeType = ParticleSystemShapeType.Box;

                var boxRect  = component.GetProperty <Rect>("rect");
                var boxScale = shape.scale;

                boxScale.x  = boxRect.width;
                boxScale.y  = boxRect.height;
                boxScale.z  = 1.0f;
                shape.scale = boxScale;

                var boxPosition = shape.position;

                boxPosition.x  = boxRect.x;
                boxPosition.y  = boxRect.y;
                boxPosition.z  = 0.0f;
                shape.position = boxPosition;

                var emission = particleSystem.emission;

                emission.enabled = boxRect.width != 0 || boxRect.height != 0;
            }
Exemple #5
0
        private void Rebind(UTinyObject property, List <UTinyEntity> source, List <UTinyEntity> duplicate)
        {
            var typeCode = property.Type.Dereference(Registry).TypeCode;

            if (typeCode != UTinyTypeCode.Struct && typeCode != UTinyTypeCode.Component)
            {
                return;
            }

            foreach (var value in property.Properties.PropertyBag.Properties)
            {
                var container = property.Properties as IPropertyContainer;

                // Component or Struct, look in their properties.
                if (value.ValueType == typeof(UTinyObject))
                {
                    var tinyObject = (UTinyObject)value.GetObjectValue(container);
                    Rebind(tinyObject, source, duplicate);
                    continue;
                }

                // Non-array Entity reference
                if (value.ValueType == typeof(UTinyEntity.Reference))
                {
                    var entityRef = (UTinyEntity.Reference)value.GetObjectValue(container);
                    var index     = source.IndexOf(entityRef.Dereference(Registry));
                    if (index >= 0)
                    {
                        value.SetObjectValue(container, (UTinyEntity.Reference)duplicate[index]);
                    }

                    continue;
                }

                // Array of Struct or of Entity references
                if (value.ValueType == typeof(UTinyList))
                {
                    var list = (UTinyList)value.GetObjectValue(container);

                    for (var i = 0; i < list.Count; ++i)
                    {
                        var item = list[i];

                        if (item is UTinyObject)
                        {
                            Rebind(item as UTinyObject, source, duplicate);
                        }
                        else if (item is UTinyEntity.Reference)
                        {
                            var entityRef = (UTinyEntity.Reference)item;
                            var index     = source.IndexOf(entityRef.Dereference(Registry));
                            if (index >= 0)
                            {
                                list[i] = (UTinyEntity.Reference)duplicate[index];
                            }
                        }
                    }
                }
            }
        }
        public sealed override void Create(UTinyEntityView view, Transform t)
        {
            UTinyEntity.Reference parentRef = UTinyEntity.Reference.None;
            if (t.parent)
            {
                parentRef = t.parent.GetComponent <UTinyEntityView>()?.EntityRef ?? UTinyEntity.Reference.None;
            }

            var graph = EntityGroupManager.GetSceneGraph(
                parentRef.Equals(UTinyEntity.Reference.None) ?
                EntityGroupManager.ActiveEntityGroup
                                        : (UTinyEntityGroup.Reference)parentRef.Dereference(Registry).EntityGroup);

            if (null == graph)
            {
                return;
            }

            var transform = new UTinyObject(Registry, GetMainTinyType());

            SyncTransform(t, transform);

            var entityNode = graph.CreateFromExisting(t, t.parent);
            var entity     = entityNode.Entity.Dereference(Registry);

            var tiny = entity.GetOrAddComponent(GetMainTinyType());

            tiny.CopyFrom(transform);

            BindingsHelper.RunBindings(entity, tiny);
        }
        /// <summary>
        /// Resets all values to thier initial/default state
        /// </summary>
        public void Reset(UTinyObject defaultValue = null)
        {
            Refresh();
            var type = Type.Dereference(Registry);

            m_Properties.Reset(type, defaultValue);
        }
Exemple #8
0
        /// <summary>
        /// Adds an item to the list
        ///
        /// * If the list has no type the type will be infered from the given object
        /// * If the list has no type and `null` is added the type will be set as `object`
        /// </summary>
        /// <param name="obj"></param>
        /// <exception cref="Exception">If the given object is not assignable to the list type</exception>
        public void Add(object obj)
        {
            if (null == m_Items)
            {
                // Special case when adding an element and we have no items
                // Dynamically create the list and properties to be strongly typed. We must use activator in this situation
                var type = obj?.GetType() ?? typeof(object);
                m_Items         = Activator.CreateInstance(typeof(List <>).MakeGenericType(type));
                m_ItemsProperty = CreateItemsProperty(type);
                m_PropertyBag.AddProperty(m_ItemsProperty);
            }

            if (obj is UTinyObject)
            {
                // Special case for tiny object. We DON'T want to retain the given instance.
                // Instead we create a new object and deep copy the values in. This way the object
                // Will propegate version changes to this list
                var v = new UTinyObject(m_Registry, m_Type, this);
                v.CopyFrom((UTinyObject)obj);
                var typedList = (IListProperty <UTinyList, UTinyObject>)m_ItemsProperty;
                typedList.Add(this, v);
            }
            else
            {
                try
                {
                    var converted = Convert(obj, m_ItemsProperty.ItemType);
                    m_ItemsProperty.AddObject(this, converted);
                }
                catch (Exception e)
                {
                    throw new Exception($"UTinyList.Add Type mismatch expected instance of Type=[{m_ItemsProperty.ItemType}] received Type=[{obj?.GetType()}]", e);
                }
            }
        }
Exemple #9
0
        protected override void OnUpdateBinding(UTinyEntity entity, UTinyObject component)
        {
            component.Refresh();
            OnAddBinding(entity, component);
            var camera        = entity.View.gameObject.GetComponent <Camera>();
            var clearFlagsRef = component.GetProperty <UTinyEnum.Reference>("clearFlags");

            if (clearFlagsRef.Name == "SolidColor")
            {
                camera.clearFlags = CameraClearFlags.SolidColor;
            }
            else
            {
                camera.clearFlags = CameraClearFlags.Depth;
            }
            var backgroundColor = component.GetProperty <Color>("backgroundColor");

            camera.backgroundColor     = backgroundColor;
            camera.orthographic        = true;
            camera.orthographicSize    = component.GetProperty <float>("halfVerticalSize");
            camera.nearClipPlane       = 0;
            camera.depth               = -101.0f;
            camera.useOcclusionCulling = false;
            camera.allowHDR            = false;
            camera.allowMSAA           = false;
#if UNITY_2017_3_OR_NEWER
            camera.allowDynamicResolution = false;
#endif
            camera.cullingMask = component.GetProperty <int>("layerMask");
            camera.rect        = component.GetProperty <Rect>("rect");
            camera.depth       = component.GetProperty <float>("depth");
        }
        public override bool VisitComponent(UTinyObject tinyObject)
        {
            var gradient = tinyObject.As <Gradient>();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel("gradient");
            var rect = EditorGUILayout.GetControlRect();

            EditorGUI.BeginChangeCheck();
            var method = typeof(EditorGUI)
                         .GetMethods(BindingFlags.NonPublic | BindingFlags.Static)
                         .First(t => t.Name == "GradientField");

            gradient = (Gradient)method.Invoke(null, new object[] { rect, gradient });
            EditorGUILayout.EndHorizontal();
            if (EditorGUI.EndChangeCheck())
            {
                tinyObject.AssignFrom(gradient);
                var container = tinyObject.Properties;
                PushChange(container, container.PropertyBag.FindProperty("mode"));
                PushChange(container, container.PropertyBag.FindProperty("stops"));
            }

            return(true);
        }
Exemple #11
0
        private void DrawText(UTinyObject tinyObject)
        {
            var textProperty = tinyObject.Properties.PropertyBag.FindProperty("text") as IProperty <UTinyObject.PropertiesContainer, string>;

            EditorGUI.BeginChangeCheck();
            var mixed = EditorGUI.showMixedValue;

            EditorGUI.showMixedValue = HasMixedValues <string>(tinyObject.Properties, textProperty);
            var isOverriden = (textProperty as IUTinyValueProperty)?.IsOverridden(tinyObject.Properties) ?? true;

            UTinyEditorUtility.SetEditorBoldDefault(isOverriden);
            try
            {
                var container = tinyObject.Properties;
                var newText   = EditorGUILayout.TextField(textProperty.Name, textProperty.GetValue(container));

                if (EditorGUI.EndChangeCheck())
                {
                    textProperty.SetValue(container, newText);
                    PushChange(container, textProperty);
                }
            }
            finally
            {
                UTinyEditorUtility.SetEditorBoldDefault(false);
                EditorGUI.showMixedValue = mixed;
            }
        }
Exemple #12
0
        public override bool VisitStruct(UTinyObject tinyObject, GUIContent label)
        {
            EditorGUILayout.BeginHorizontal();

            if (Screen.width < 400)
            {
                EditorGUIUtility.labelWidth = Mathf.Max(EditorGUIUtility.labelWidth - (400 - Screen.width), 70);
            }

            if (!string.IsNullOrEmpty(label.text))
            {
                EditorGUILayout.PrefixLabel(label);
            }

            var indent = EditorGUI.indentLevel;

            try
            {
                EditorGUIUtility.labelWidth = 15;
                EditorGUIUtility.fieldWidth = 30;
                EditorGUI.indentLevel       = 0;

                tinyObject.Properties.Visit(this);
            }
            finally
            {
                EditorGUILayout.EndHorizontal();
                EditorGUI.indentLevel       = indent;
                EditorGUIUtility.fieldWidth = 0;
                EditorGUIUtility.labelWidth = 0;
            }
            return(true);
        }
        private void DrawCullingMask(UTinyObject tinyObject)
        {
            var cullingMask = tinyObject.Properties.PropertyBag.FindProperty("layerMask") as IProperty <UTinyObject.PropertiesContainer, int>;

            EditorGUI.BeginChangeCheck();
            var mixed = EditorGUI.showMixedValue;

            EditorGUI.showMixedValue = HasMixedValues <int>(tinyObject.Properties, cullingMask);
            var isOverriden = (cullingMask as IUTinyValueProperty)?.IsOverridden(tinyObject.Properties) ?? true;

            UTinyEditorUtility.SetEditorBoldDefault(isOverriden);
            try
            {
                var container  = tinyObject.Properties;
                var layerNames = GetLayerNames();
                var newLayer   = EditorGUILayout.MaskField("cullingMask", GetCurrentEditorLayer(layerNames, cullingMask.GetValue(container)), layerNames.ToArray());
                if (EditorGUI.EndChangeCheck())
                {
                    cullingMask.SetValue(container, GetLayers(layerNames, newLayer));
                    PushChange(container, cullingMask);
                }
            }
            finally
            {
                UTinyEditorUtility.SetEditorBoldDefault(false);
                EditorGUI.showMixedValue = mixed;
            }
        }
Exemple #14
0
            public void Reset(UTinyType type, UTinyObject defaultValue)
            {
                if (null == type)
                {
                    return;
                }

                // The default value for this type
                var typeDefaultValue = !m_Object.IsDefaultValue ? defaultValue ?? type.DefaultValue as UTinyObject : null;

                var fields = type.Fields;

                for (var i = 0; i < fields.Count; i++)
                {
                    var field      = fields[i];
                    var fieldType  = field.FieldType.Dereference(m_Object.Registry);
                    var fieldValue = m_FieldValues[i];
                    fieldValue.Overridden = false;

                    // The default value for this field
                    var fieldDefaultValue = typeDefaultValue?[field.Name];

                    if (fieldType.IsPrimitive || fieldType.IsEnum)
                    {
                        fieldValue.Value = fieldDefaultValue;
                    }
                    else
                    {
                        (fieldValue.Value as UTinyObject)?.Reset(fieldDefaultValue as UTinyObject);
                    }
                }
            }
            protected override void OnUpdateBinding(UTinyEntity entity, UTinyObject component)
            {
                var module = entity.View.GetComponent <ParticleSystem>().main;
                var scale  = component.GetProperty <Range>("scale");

                SetScaleRange(module, scale);
            }
Exemple #16
0
 protected override void OnAddBinding(UTinyEntity entity, UTinyObject component)
 {
     AddMissingComponent <SpriteRenderer>(entity, r =>
     {
         r.sharedMaterial = new Material(Shader.Find("UTiny/Sprite2D"));
     });
 }
        protected static void AssignIfDifferent(UTinyObject tiny, string propertyName, Vector2 value)
        {
            var v = tiny[propertyName] as UTinyObject;

            AssignIfDifferent(v, "x", value.x);
            AssignIfDifferent(v, "y", value.y);
        }
            protected override void OnUpdateBinding(UTinyEntity entity, UTinyObject component)
            {
                var module   = entity.View.GetComponent <ParticleSystem>().main;
                var rotation = component.GetProperty <Range>("rotation");

                SetRotationRange(module, rotation);
            }
Exemple #19
0
        protected override void OnUpdateBinding(UTinyEntity entity, UTinyObject component)
        {
            var renderer = GetComponent <SpriteRenderer>(entity);

            renderer.size = component.GetProperty <Vector2>("size");
            SetDrawMode(renderer, component.GetProperty <DrawMode>("drawMode"));
        }
        public static void SyncCamera(Camera from, [NotNull] UTinyObject camera)
        {
            switch (from.clearFlags)
            {
            case CameraClearFlags.Color:
            case CameraClearFlags.Skybox:
                from.clearFlags = CameraClearFlags.SolidColor;
                break;

            case CameraClearFlags.Nothing:
            case CameraClearFlags.Depth:
                from.clearFlags = CameraClearFlags.Nothing;
                break;
            }

            from.orthographic        = true;
            from.nearClipPlane       = 0;
            from.useOcclusionCulling = false;
            from.allowHDR            = false;
            from.allowMSAA           = false;
#if UNITY_2017_3_OR_NEWER
            from.allowDynamicResolution = false;
#endif
            AssignIfDifferent(camera, "clearFlags", from.clearFlags);
            AssignIfDifferent(camera, "backgroundColor", from.backgroundColor);
            AssignIfDifferent(camera, "layerMask", from.cullingMask);
            AssignIfDifferent(camera, "halfVerticalSize", from.orthographicSize);
            AssignIfDifferent(camera, "rect", from.rect);
            AssignIfDifferent(camera, "depth", from.depth);
        }
Exemple #21
0
 protected override void OnAddBinding(UTinyEntity entity, UTinyObject component)
 {
     AddMissingComponent <MeshRenderer>(entity, renderer =>
     {
         renderer.sharedMaterial = new Material(Shader.Find("GUI/Text Shader"));
     });
     AddMissingComponent <TextMesh>(entity);
 }
Exemple #22
0
 protected override bool ValidateObject(UTinyObject tiny, UTinyType type)
 {
     if (type.TypeCode == UTinyTypeCode.Component)
     {
         return(Targets.Cast <UTinyEntity>().All(e => e.Components.Any(c => c.Type.Id == tiny.Type.Id)));
     }
     return(base.ValidateObject(tiny, type));
 }
        protected override void OnUpdateBinding(UTinyEntity entity, UTinyObject component)
        {
            OnAddBinding(entity, component);

            var behaviour = GetComponent <RectHitBox2D>(entity);

            behaviour.Box = component.GetProperty <Rect>("box");
        }
Exemple #24
0
        protected override void OnUpdateBinding(UTinyEntity entity, UTinyObject component)
        {
            var transform = entity.View.transform;

            transform.localPosition = component.GetProperty <Vector3>("localPosition");
            transform.localRotation = component.GetProperty <Quaternion>("localRotation");
            transform.localScale    = component.GetProperty <Vector3>("localScale");
        }
 public override bool VisitComponent(UTinyObject tinyObject)
 {
     if (TargetType == typeof(UTinyEntity) && UsesLifetimeColor(Targets.OfType <UTinyEntity>().FirstOrDefault()))
     {
         EditorGUILayout.HelpBox("This component will be ignore since the entity also has a LifetimeColor component.", MessageType.Info);
     }
     return(base.VisitComponent(tinyObject));
 }
        protected static void AssignIfDifferent(UTinyObject tiny, string propertyName, AudioClip value)
        {
            var current = tiny.GetProperty <AudioClip>(propertyName);

            if (current != value)
            {
                tiny.AssignPropertyFrom(propertyName, value);
            }
        }
        protected static void AssignIfDifferent(UTinyObject tiny, string propertyName, Quaternion value)
        {
            var v = tiny[propertyName] as UTinyObject;

            AssignIfDifferent(v, "x", value.x);
            AssignIfDifferent(v, "y", value.y);
            AssignIfDifferent(v, "z", value.z);
            AssignIfDifferent(v, "w", value.w);
        }
        protected static void AssignIfDifferent(UTinyObject tiny, string propertyName, Rect value)
        {
            var v = tiny[propertyName] as UTinyObject;

            AssignIfDifferent(v, "x", value.x);
            AssignIfDifferent(v, "y", value.y);
            AssignIfDifferent(v, "width", value.width);
            AssignIfDifferent(v, "height", value.height);
        }
        protected static void AssignIfDifferent <TValue>(UTinyObject tiny, string propertyName, TValue value)
        {
            var current = tiny.GetProperty <TValue>(propertyName);

            if (!current.Equals(value))
            {
                tiny.AssignPropertyFrom(propertyName, value);
            }
        }
 public override bool VisitComponent(UTinyObject tinyObject)
 {
     DrawClearFlags(tinyObject);
     DrawCullingMask(tinyObject);
     DoField <float>(tinyObject, "halfVerticalSize");
     DrawSubObject(tinyObject, "rect");
     DoField <float>(tinyObject, "depth");
     return(true);
 }