Beispiel #1
0
        private void GenerateExposedVariableProperty(ElementSave elementSave, ICodeBlock currentBlock, VariableSave variable)
        {
            string variableType = variable.Type;

            ModifyVariableTypeForProperty(ref variableType, variable, elementSave);

            string propertyName = variable.ExposedAsName.Replace(" ", "_");

            ICodeBlock property = currentBlock.Property("public " + variableType, propertyName);

            string whatToGetOrSet = variable.Name;

            // If this is an exposed property on a standard element, then we just need to kill all spaces and replace
            // them with nothing
            var instance = elementSave.GetInstance(variable.SourceObject);

            if (instance != null)
            {
                var baseElement = Gum.Managers.ObjectFinder.Self.GetElementSave(instance.BaseType);

                if (baseElement != null && baseElement is StandardElementSave)
                {
                    whatToGetOrSet = whatToGetOrSet.Replace(" ", "");
                }

                var rootName = variable.GetRootName();
                if (rootName.EndsWith("State"))
                {
                    var withoutState = rootName.Substring(0, rootName.Length - "State".Length);
                    if (rootName == "State")
                    {
                        whatToGetOrSet = variable.SourceObject + "." + "CurrentVariableState";
                    }
                    else if (baseElement != null && baseElement.Categories.Any(item => item.Name == withoutState))
                    {
                        whatToGetOrSet = variable.SourceObject + ".Current" + withoutState + "State";
                    }
                }
            }

            property.Get()
            .Line("return " + whatToGetOrSet + ";");
            var setter = property.Set();

            if (EventCodeGenerator.Self.GetIfShouldGenerateEventOnVariableSet(elementSave, variable))
            {
                string eventName = EventCodeGenerator.Self.GetEventName(variable, elementSave);

                setter.If($"{whatToGetOrSet} != value")
                .Line(whatToGetOrSet + " = value;")
                .Line($"{eventName}?.Invoke(this, null);");
            }
            else
            {
                setter.Line(whatToGetOrSet + " = value;");
            }
        }
Beispiel #2
0
        private void GenerateGetter(string propertyName, VariableSave variable,
                                    ICodeBlock property, string variableName, string whatToGetOrSet, ElementSave elementSave)
        {
            var getter = property.Get();

            bool wasHandled = TryHandleCustomGetter(variable, elementSave, getter);

            if (!wasHandled)
            {
                if (mStandardGetterReplacements.ContainsKey(variableName))
                {
                    mStandardGetterReplacements[propertyName](getter);
                }
                else
                {
                    string whatToReturn = whatToGetOrSet;
                    whatToReturn = AdjustStandardElementVariableGetIfNecessary(variable, whatToReturn);

                    getter.Line("return " + whatToReturn + ";");
                }
            }
        }
        private static void WriteGetterForProperty(CustomVariable customVariable, IElement element, ICodeBlock prop)
        {
            var getter = prop.Get();

            if (customVariable.GetIsSourceFile(element))
            {
                getter.Line("return " + customVariable.SourceObject +";");
            }
            else
            {

                string relativeVersionOfProperty = InstructionManager.GetRelativeForAbsolute(customVariable.SourceObjectProperty);

                if (!string.IsNullOrEmpty(relativeVersionOfProperty))
                {
                    getter = getter.If(customVariable.SourceObject + ".Parent == null");
                }

                string valueToReturn = customVariable.SourceObject + "." + customVariable.SourceObjectProperty;
                if (!string.IsNullOrEmpty(customVariable.OverridingPropertyType) &&
                    // If overriding to the same type, no conversion can be performed
                    customVariable.OverridingPropertyType != customVariable.Type)
                {
                    valueToReturn = TypeConverterHelper.Convert(
                        customVariable, GetterOrSetter.Getter, valueToReturn);
                }
                if (!string.IsNullOrEmpty(valueToReturn))
                {
                    getter.Line("return " + valueToReturn + ";");
                }
                else
                {
                    getter.Line("throw new System.Exception();");
                }



                if (!string.IsNullOrEmpty(relativeVersionOfProperty))
                {
                    getter = getter.End().Else();

                    valueToReturn = customVariable.SourceObject + "." + relativeVersionOfProperty;
                    if (!string.IsNullOrEmpty(customVariable.OverridingPropertyType))
                    {
                        valueToReturn = TypeConverterHelper.Convert(
                            customVariable, GetterOrSetter.Getter, valueToReturn);
                    }


                    getter.Line("return " + valueToReturn + ";");
                    getter.End();
                }
            }
        }