public void Global_Variables_In_Different_SmartFormatters()
        {
            const string formatString = "{global.theVariable}";

            var globalGrp = new VariablesGroup
            {
                { "theVariable", new StringVariable("val-from-global-source") }
            };

            GlobalVariablesSource.Instance.Add("global", globalGrp);

            var smart1 = new SmartFormatter();

            smart1.FormatterExtensions.Add(new DefaultFormatter());
            smart1.InsertExtension(0, GlobalVariablesSource.Instance);

            var smart2 = new SmartFormatter();

            smart2.FormatterExtensions.Add(new DefaultFormatter());
            smart2.InsertExtension(0, GlobalVariablesSource.Instance);

            var result1 = smart1.Format(formatString);
            var result2 = smart2.Format(formatString);

            Assert.That(result1, Is.EqualTo(result2));
        }
Exemple #2
0
        public void Format_Args_Should_Override_Persistent_Vars()
        {
            const string formatString = "{global.theVariable}";

            // Setup PersistentVariablesSource

            var persistentGrp = new VariablesGroup
            {
                { "theVariable", new StringVariable("val-from-persistent-source") }
            };

            var smart = new SmartFormatter();

            smart.FormatterExtensions.Add(new DefaultFormatter());
            smart.InsertExtension(0, new PersistentVariablesSource {
                { "global", persistentGrp }
            });

            // Setup equivalent VariablesGroup to use as an argument to Smart.Format(...)

            var argumentGrp = new VariablesGroup
            {
                { "global", new VariablesGroup {
                      { "theVariable", new StringVariable("val-from-argument") }
                  } }
            };

            // Arguments override PersistentVariablesSource
            var argumentResult = smart.Format(formatString, argumentGrp);
            // Without arguments, the variable from PersistentVariablesSource is used
            var persistentResult = smart.Format(formatString);

            Assert.That(argumentResult, Is.EqualTo("val-from-argument"));
            Assert.That(persistentResult, Is.EqualTo("val-from-persistent-source"));
        }
Exemple #3
0
        public void Add_And_Get_Items()
        {
            const string var1Name = "var1name";
            const string var2Name = "var2name";
            const string var3Name = "var3name";

            var var1 = new IntVariable(1234);
            var var2 = new StringVariable("theValue");
            var var3 = new BoolVariable(true);

            var vg = new VariablesGroup
            {
                { var1Name, var1 },
                new KeyValuePair <string, IVariable>(var2Name, var2)
            };

            vg[var3Name] = var3;

            Assert.That(vg.Count, Is.EqualTo(3));
            Assert.That((int)vg[var1Name].GetValue() !, Is.EqualTo(1234));
            Assert.That((string)vg[var2Name].GetValue() !, Is.EqualTo("theValue"));
            Assert.That(vg.Keys.Count, Is.EqualTo(3));
            Assert.That(vg.ContainsKey(var1Name));
            Assert.That(vg.Values.Count, Is.EqualTo(3));
            Assert.That(vg.Values.Contains(var1));
            Assert.That(vg.Values.Contains(var2));
            Assert.That(vg.Values.Contains(var3));
            Assert.That(vg.ContainsKey(var2Name));
            Assert.That(vg.TryGetValue(var1Name + "False", out _), Is.False);
        }
Exemple #4
0
        public void Should_Handle_null_for_Nullable(object?valueToUse, string expected)
        {
            if (valueToUse is "not-null")
            {
                valueToUse = new KeyValuePair <string, object?>("Anything", "not-null");
            }

            // The group with just one nullable variable
            var varGroup = new VariablesGroup {
                { "NullableVar", new ObjectVariable(valueToUse) }
            };

            var smart = new SmartFormatter();

            smart.FormatterExtensions.Add(new DefaultFormatter());
            var pvs = new PersistentVariablesSource
            {
                // top container's name
                { "Global", varGroup }
            };

            smart.AddExtensions(new KeyValuePairSource());
            smart.InsertExtension(0, pvs);

            var result = smart.Format("{Global.NullableVar?.Anything}");

            Assert.That(result, Is.EqualTo(expected));
        }
Exemple #5
0
        public void Add_Item_With_Illegal_Name_Should_Throw()
        {
            var vg = new VariablesGroup();

            // Name must not be empty
            Assert.That(code: () => vg.Add(string.Empty, new IntVariable(1)), Throws.ArgumentException);
        }
Exemple #6
0
        public void Add_Key_Value_Pair()
        {
            var vg  = new VariablesGroup();
            var kvp = new KeyValuePair <string, IVariable>("theKey", new IntVariable(9876));

            vg.Add(kvp);

            Assert.That(vg.Contains(new KeyValuePair <string, IVariable>(kvp.Key, kvp.Value)));
        }
Exemple #7
0
        public void Clear_All_Items()
        {
            var vg  = new VariablesGroup();
            var kvp = new KeyValuePair <string, IVariable>("theKey", new IntVariable(135));

            vg.Add(kvp);
            vg.Clear();

            Assert.That(vg.Count, Is.EqualTo(0));
        }
Exemple #8
0
        public void Remove_Key_Value_Pair()
        {
            var vg = new VariablesGroup {
                { "theKey", new IntVariable(12) }
            };

            vg.Remove("theKey");

            Assert.That(vg.Count, Is.EqualTo(0));
            Assert.That(vg.Remove("non-existent"), Is.EqualTo(false));
        }
Exemple #9
0
        public void Remove_Item()
        {
            var vg  = new VariablesGroup();
            var kvp = new KeyValuePair <string, IVariable>("theKey", new IntVariable(12));

            vg.Add(kvp);
            vg.Remove(kvp);

            Assert.That(vg.Count, Is.EqualTo(0));
            Assert.That(vg.Remove("non-existent"), Is.EqualTo(false));
        }
Exemple #10
0
        public void Shallow_Copy()
        {
            var vg   = new VariablesGroup();
            var kvp1 = new KeyValuePair <string, IVariable>("theKey1", new ObjectVariable("123"));
            var kvp2 = new KeyValuePair <string, IVariable>("theKey2", new FloatVariable(987.654f));

            vg.Add(kvp1);
            vg.Add(kvp2);
            var vgCopy = vg.Clone();

            Assert.That(vgCopy.Count, Is.EqualTo(vg.Count));
            Assert.That(vgCopy.Values, Is.EquivalentTo(vg.Values));
            Assert.That(vgCopy.Keys, Is.EquivalentTo(vg.Keys));
        }
    private TransformCache CloneTransformCache(VariablesGroup data)
    {
        TransformCache rst = new TransformCache();

        rst.m_LocalPosition = data.localPositionProp.vector3Value;
        rst.m_LocalRotation = data.localRotationProp.quaternionValue;
        rst.m_LocalScale    = data.localScaleProp.vector3Value;
        rst.m_AnchorMin     = data.anchorMinProp.vector2Value;
        rst.m_AnchorMax     = data.anchorMaxProp.vector2Value;
        rst.m_SizeDelta     = data.sizeDeltaProp.vector2Value;
        rst.m_Pivot         = data.pivotProp.vector2Value;
        rst.m_TransformRef  = (TransformCache.eTransformRef)data.tranRefProp.intValue;
        return(rst);
    }
Exemple #12
0
        public void NonExisting_GroupVariable_Should_Throw()
        {
            var varGrp = new VariablesGroup {
                { "existing", new StringVariable("existing-value") }
            };

            var smart = new SmartFormatter();

            smart.FormatterExtensions.Add(new DefaultFormatter());
            smart.InsertExtension(0, new PersistentVariablesSource());
            var resultExisting = smart.Format("{existing}", varGrp);

            Assert.That(resultExisting, Is.EqualTo("existing-value"));
            Assert.That(code: () => smart.Format("{non-existing}", varGrp),
                        Throws.InstanceOf <FormattingException>().And.Message.Contains("non-existing"));
        }
        public void Reset_Should_Create_A_New_Instance()
        {
            var globalGrp = new VariablesGroup
            {
                { "theVariable", new StringVariable("val-from-global-source") }
            };

            GlobalVariablesSource.Instance.Add("global", globalGrp);

            var ref1 = GlobalVariablesSource.Instance;

            GlobalVariablesSource.Reset();
            var ref2 = GlobalVariablesSource.Instance;

            Assert.That(!ReferenceEquals(ref1, ref2), "Different references after ResetInstance()");
            Assert.That(ref2.Count, Is.EqualTo(0));
        }
Exemple #14
0
        public void KeyValuePairs_By_Enumerators()
        {
            var vg  = new VariablesGroup();
            var kvp = new KeyValuePair <string, IVariable>("theKey", new IntVariable(9876));

            vg.Add(kvp);
            var kvpFromEnumerator = vg.FirstOrDefault(keyValuePair => keyValuePair.Key.Equals("theKey"));

            // Test GetEnumerator()
            foreach (var keyValuePair in vg)
            {
                Assert.That(keyValuePair, Is.EqualTo(kvp));
            }

            // Test IEnumerator<KeyValuePair<string, VariablesGroup>>
            Assert.That(kvpFromEnumerator, Is.EqualTo(kvp));
        }
Exemple #15
0
        public void Use_Globals_Without_Args_To_Formatter()
        {
            // The top container
            // It gets its name later, when being added to the PersistentVariablesSource
            var varGroup = new VariablesGroup();

            // Add a (nested) VariablesGroup named 'group' to the top container
            varGroup.Add("group", new VariablesGroup
            {
                // Add variables to the nested group
                { "groupString", new StringVariable("groupStringValue") },
                { "groupDateTime", new Variable <DateTime>(new DateTime(2024, 12, 31)) }
            });
            // Add more variables to the top group container
            varGroup.Add(new KeyValuePair <string, IVariable>("topInteger", new IntVariable(12345)));
            var stringVar = new StringVariable("topStringValue");

            varGroup.Add("topString", stringVar);

            // The formatter for persistent variables requires only 2 extensions
            var smart = new SmartFormatter();

            smart.FormatterExtensions.Add(new DefaultFormatter());
            // Add as the first in the source extensions list
            var pvs = new PersistentVariablesSource
            {
                // Here, the top container gets its name
                { "global", varGroup }
            };

            smart.InsertExtension(0, pvs);

            // Act
            // Note: We don't need args to the formatter for globals
            var globalGroup = smart.Format(CultureInfo.InvariantCulture,
                                           "{global.group.groupString} {global.group.groupDateTime:'groupDateTime='yyyy-MM-dd}");
            var topInteger = smart.Format("{global.topInteger}");
            var topString  = smart.Format("{global.topString}");

            // Assert
            Assert.That(globalGroup, Is.EqualTo("groupStringValue groupDateTime=2024-12-31"));
            Assert.That(topString, Is.EqualTo(stringVar.ToString()));
            Assert.That(topInteger, Is.EqualTo("12345"));
        }
Exemple #16
0
        public void Copy_To_Array()
        {
            var vg   = new VariablesGroup();
            var kvp1 = new KeyValuePair <string, IVariable>("theKey1", new IntVariable(135));
            var kvp2 = new KeyValuePair <string, IVariable>("theKey2", new IntVariable(987));

            vg.Add(kvp1);
            vg.Add(kvp2);

            var array = new KeyValuePair <string, IVariable> [vg.Count];

            vg.CopyTo(array, 0);

            Assert.That(vg.Count, Is.EqualTo(array.Length));
            for (var i = 0; i < array.Length; i++)
            {
                Assert.That(vg.ContainsKey(array[i].Key));
            }
        }
Exemple #17
0
        public void Shallow_Copy()
        {
            var pvs = new PersistentVariablesSource();

            var kvp1 = new KeyValuePair <string, IVariable>("theKey1", new ObjectVariable("123"));
            var kvp2 = new KeyValuePair <string, IVariable>("theKey2", new FloatVariable(987.654f));

            var vg1 = new VariablesGroup {
                kvp1
            };
            var vg2 = new VariablesGroup {
                kvp2
            };

            pvs.Add(vg1.First().Key, vg1);
            pvs.Add(vg2.First().Key, vg2);

            var pvsCopy = pvs.Clone();

            Assert.That(pvsCopy.Count, Is.EqualTo(pvs.Count));
            Assert.That(pvsCopy.Values, Is.EquivalentTo(pvs.Values));
            Assert.That(pvsCopy.Keys, Is.EquivalentTo(pvs.Keys));
        }
Exemple #18
0
        public void Add_And_Get_Items()
        {
            const string groupName1 = "global";
            const string groupName2 = "secret";

            var pvs = new PersistentVariablesSource();
            var vg1 = new VariablesGroup();
            var vg2 = new VariablesGroup();

            pvs.Add(groupName1, vg1);
            pvs[groupName2] = vg2;

            Assert.That(pvs.Count, Is.EqualTo(2));
            Assert.That(pvs[groupName1], Is.EqualTo(vg1));
            Assert.That(pvs[groupName2], Is.EqualTo(vg2));
            Assert.That(pvs.Keys.Count, Is.EqualTo(2));
            Assert.That(pvs.ContainsKey(groupName1));
            Assert.That(pvs.Values.Count, Is.EqualTo(2));
            Assert.That(pvs.Values.Contains(vg1));
            Assert.That(pvs.Values.Contains(vg2));
            Assert.That(pvs.ContainsKey(groupName2));
            Assert.That(pvs.TryGetValue(groupName1, out _), Is.True);
            Assert.That(pvs.TryGetValue(groupName1 + "False", out _), Is.False);
        }
Exemple #19
0
 /// <summary>
 /// CTOR.
 /// </summary>
 /// <param name="name">The name of the <see cref="VariablesGroup"/>.</param>
 /// <param name="group">The <see cref="VariablesGroup"/>.</param>
 public NameGroupPair(string name, VariablesGroup group)
 {
     Name  = name;
     Group = group;
 }
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        property.serializedObject.UpdateIfRequiredOrScript();
        EditorGUI.BeginProperty(position, label, property);
        Rect line = position.Clone(height: EditorGUIUtility.singleLineHeight + singleLine);

        Rect[] left     = line.SplitLeft(80f);
        Rect[] cols     = left[1].SplitLeft(80f);
        Color  orgColor = GUI.color;

        GUI.color = Color.yellow;
        EditorGUI.BeginDisabledGroup(Application.isPlaying);
        EditorGUI.BeginChangeCheck();
        if (GUI.Button(left[0], l_recordButton))
        {
            // We know the propertyPath, let's apply on multiple targets
            string _propertyPath = property.propertyPath;
            for (int i = 0; i < property.serializedObject.targetObjects.Length; i++)
            {
                Component          _component        = (Component)property.serializedObject.targetObjects[i];
                SerializedObject   _serializedObject = new SerializedObject(_component);
                SerializedProperty _property         = _serializedObject.FindProperty(_propertyPath);
                VariablesGroup     _data             = new VariablesGroup(_property);
                Undo.RecordObjects(new[] { _component, _component.transform }, "Record Transform");
                _data.localPositionProp.vector3Value = _component.transform.localPosition;
                if (_component.transform is RectTransform)
                {
                    RectTransform obj = (RectTransform)_component.transform;
                    _data.anchorMinProp.vector2Value     = obj.anchorMin;
                    _data.anchorMaxProp.vector2Value     = obj.anchorMax;
                    _data.sizeDeltaProp.vector2Value     = obj.sizeDelta;
                    _data.pivotProp.vector2Value         = obj.pivot;
                    _data.localPositionProp.vector3Value = obj.anchoredPosition3D;
                }
                _data.localRotationProp.quaternionValue = _component.transform.localRotation;
                _data.localScaleProp.vector3Value       = _component.transform.localScale;
                _property.serializedObject.ApplyModifiedProperties();

                InvokeMethod(
                    _component,
                    TransformCache.Editor_Record_Callback,
                    BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
                    new[] { _propertyPath });
            }
        }
        GUI.color = Color.green;
        if (GUI.Button(cols[0], l_AssignButton))
        {
            // We know the propertyPath, let's apply on multiple targets
            string _propertyPath = property.propertyPath;
            for (int i = 0; i < property.serializedObject.targetObjects.Length; i++)
            {
                Component          _component        = (Component)property.serializedObject.targetObjects[i];
                SerializedObject   _serializedObject = new SerializedObject(_component);
                SerializedProperty _property         = _serializedObject.FindProperty(_propertyPath);
                TransformCache     cache             = CloneTransformCache(new VariablesGroup(_property));

                Undo.RecordObjects(new[] { _component, _component.transform }, "Load Transform");
                cache.AssignTo(_component.transform);
                _property.serializedObject.ApplyModifiedProperties();

                InvokeMethod(
                    _component,
                    TransformCache.Editor_Load_Callback,
                    BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
                    new[] { _propertyPath });
            }
        }
        if (EditorGUI.EndChangeCheck() && !Application.isPlaying)
        {
            EditorUtility.SetDirty(property.serializedObject.targetObject);
        }
        EditorGUI.EndDisabledGroup();

        VariablesGroup data      = new VariablesGroup(property);
        Component      component = (Component)property.serializedObject.targetObject;

        GUI.color           = orgColor;
        property.isExpanded = EditorGUI.Foldout(cols[1].Adjust(x: 15f).Clone(width: 50f), property.isExpanded, l_Detail, true);
        TransformCache.eTransformRef tranRef = (TransformCache.eTransformRef)data.tranRefProp.enumValueIndex;

        if (property.isExpanded)
        {
            if (component.transform is RectTransform)
            {
                RectTransform rectTran = (RectTransform)component.transform;
                line = line.GetRectBottom(height: singleLine);
                DrawRectButton(line, data.tranRefProp,
                               TransformCache.eTransformRef.AnchorMin, data.anchorMinProp,
                               () => { data.anchorMinProp.vector2Value = rectTran.anchorMin; });

                line = line.GetRectBottom(height: singleLine);
                DrawRectButton(line, data.tranRefProp,
                               TransformCache.eTransformRef.AnchorMax, data.anchorMaxProp,
                               () => { data.anchorMaxProp.vector2Value = rectTran.anchorMax; });

                line = line.GetRectBottom(height: singleLine);
                DrawRectButton(line, data.tranRefProp,
                               TransformCache.eTransformRef.SizeDelta, data.sizeDeltaProp,
                               () => { data.sizeDeltaProp.vector2Value = rectTran.sizeDelta; });


                line = line.GetRectBottom(height: singleLine);
                DrawRectButton(line, data.tranRefProp,
                               TransformCache.eTransformRef.Pivot, data.pivotProp,
                               () => { data.pivotProp.vector2Value = rectTran.pivot; });
            }

            // position
            line = line.GetRectBottom(height: singleLine);
            DrawRectButton(line, data.tranRefProp,
                           TransformCache.eTransformRef.LocalPosition, data.localPositionProp,
                           () => {
                if (component.transform is RectTransform)
                {
                    data.localPositionProp.vector3Value = ((RectTransform)component.transform).anchoredPosition3D;
                }
                else
                {
                    data.localPositionProp.vector3Value = component.transform.localPosition;
                }
            });

            // rotation
            line = line.GetRectBottom(height: singleLine);
            DrawRectButton(line, data.tranRefProp,
                           TransformCache.eTransformRef.LocalRotation, data.localRotationProp,
                           () => { data.localRotationProp.quaternionValue = component.transform.localRotation; },
                           DrawQuaternionParam);
            // TODO:
            // Vector3 rotate = EditorGUI.Vector3Field(cols[1], l_LocalRotation, data.localRotationProp.quaternionValue.eulerAngles);
            //	data.localRotationProp.quaternionValue = Quaternion.Euler(rotate);

            // scale
            line = line.GetRectBottom(height: singleLine);
            DrawRectButton(line, data.tranRefProp,
                           TransformCache.eTransformRef.LocalScale, data.localScaleProp,
                           () => { data.localScaleProp.vector3Value = component.transform.localScale; });
        }

        EditorGUI.EndProperty();
    }