GenerateEventRaisingCode() static private method

static private GenerateEventRaisingCode ( ICodeBlock codeBlock, BeforeOrAfter beforeOrAfter, string variableName, IElement saveObject ) : void
codeBlock ICodeBlock
beforeOrAfter BeforeOrAfter
variableName string
saveObject IElement
return void
        private static void GenerateVisibleProperty(ICodeBlock codeBlock, IElement element, EntitySave entitySave)
        {
            bool inheritsFromIVisible = entitySave.GetInheritsFromIVisible();

            #region Get whether we use virtual or override

            if (!inheritsFromIVisible)
            {
                codeBlock.Line("protected bool mVisible = true;");
            }

            #endregion

            var prop = codeBlock.Property("Visible", Public: true, Override: entitySave.GetInheritsFromIVisible(), Virtual: !entitySave.GetInheritsFromIVisible(), Type: "bool");

            if (inheritsFromIVisible)
            {
                prop.Get()
                .Line("return base.Visible;");
            }
            else
            {
                prop.Get()
                .Line("return mVisible;");
            }
            var set = prop.Set();

            #region History on the before set code

            // See comment above about why we no longer
            // check to see if it creates an event.
            //if (createsVisibleEvent)
            //{
            // Keep hasChanged around just in case we want to make a Changed event.  It won't be used by the Set event
            // Update November 27, 2011
            // This is just polluting code.  Let's remove it for now
            //set.Line("bool hasChanged = value != mVisible;");
            #endregion

            EventCodeGenerator.GenerateEventRaisingCode(set, BeforeOrAfter.Before, "Visible", entitySave);

            if (entitySave.GetInheritsFromIVisible())
            {
                set.Line("base.Visible = value;");
            }
            else
            {
                set.Line("mVisible = value;");
            }

            // May 6, 2012
            // We used to manually
            // set the Visible of all
            // children, but now we no
            // longer do that because there
            // is a concept of relative visibility.
            // WriteVisibleSetForNamedObjectSaves(set, entitySave);

            EventCodeGenerator.GenerateEventRaisingCode(set, BeforeOrAfter.After, "Visible", entitySave);
        }
        private static void GenerateEnabledVariable(ICodeBlock codeBlock, IElement element)
        {
            CustomVariable exposedEnabledVariable  = element.CustomVariables.FirstOrDefault(item => item.Name == "Enabled" && item.Type == "bool");
            bool           isEnableVariableExposed = exposedEnabledVariable != null;

            bool hasEvents = exposedEnabledVariable != null && exposedEnabledVariable.CreatesEvent && element.Events.Any(item => item.SourceVariable == exposedEnabledVariable.Name);

            if (hasEvents)
            {
                EventCodeGenerator.GenerateEventsForVariable(codeBlock, exposedEnabledVariable.Name);
            }

            string prefix;
            string propertyName;

            if (isEnableVariableExposed)
            {
                prefix       = "public bool";
                propertyName = "Enabled";
            }
            else
            {
                prefix       = "bool";
                propertyName = "FlatRedBall.Gui.IWindow.Enabled";
            }

            codeBlock.Line(Resources.Resource1.IWindowTemplate);

            var property = codeBlock.Property(prefix, propertyName);

            property.Get().Line("return mEnabled;");

            var setBlock = property.Set();

            if (hasEvents)
            {
                EventCodeGenerator.GenerateEventRaisingCode(setBlock, BeforeOrAfter.Before, exposedEnabledVariable.Name, element);
            }

            var setIf = setBlock.If("mEnabled != value");

            setIf.Line("mEnabled = value;");
            setIf.Line("EnabledChange?.Invoke(this);");
            if (hasEvents)
            {
                EventCodeGenerator.GenerateEventRaisingCode(setIf, BeforeOrAfter.After, exposedEnabledVariable.Name, element);
            }
        }
示例#3
0
        private static ICodeBlock GenerateCurrentStateProperty(IElement element, ICodeBlock codeBlock, string enumType, List <StateSave> states)
        {
            // early out
            if (states.Count == 0)
            {
                return(codeBlock);
            }

            string variableNameModifier = enumType;

            if (enumType == "VariableState")
            {
                variableNameModifier = "";
            }

            string qualifiedEnumType = element.Name.Replace("\\", ".").Replace("/", ".") + "." + enumType;

            string         variableToLookFor = "Current" + variableNameModifier + "State";
            CustomVariable customVariable    = element.GetCustomVariable(variableToLookFor);
            bool           hasEvent          = customVariable != null && customVariable.CreatesEvent;


            #region Header and Getter stuff - simple stuff with no logic


            codeBlock
            .Line($"private {enumType} mCurrent{variableNameModifier}State = null;");

            string publicWithOptionalNew = "public";
            if (IsStateDefinedInBase(element, enumType))
            {
                publicWithOptionalNew += " new";
            }

            // This is inclusive (-1), but includes 0 and 1 values (+2), which means the net is +1
            int maxIntValueInclusive = states.Count + 1;

            var setBlock = codeBlock
                           .Property(publicWithOptionalNew + " " + qualifiedEnumType, "Current" + variableNameModifier + "State")
                           .Get()
                           .Line(string.Format("return mCurrent{1}State;", enumType, variableNameModifier))
                           .End()
                           .Set();

            #endregion

            #region Set the state value and call an event if necessary

            bool stillNeedsToAssignValue = true;

            if (hasEvent)
            {
                EventCodeGenerator.GenerateEventRaisingCode(setBlock, BeforeOrAfter.Before, variableToLookFor, element);
            }

            if (stillNeedsToAssignValue)
            {
                setBlock.Line("mCurrent" + variableNameModifier + "State = value;");
            }

            #endregion

            bool isElse = false;
            foreach (StateSave stateSave in states)
            {
                GenerateVariableAssignmentForState(element, setBlock, stateSave, enumType, isElse);
                isElse = true;
            }

            if ((enumType == "VariableState" && DoesBaseHaveUncategorizedStates(element)) ||
                (!string.IsNullOrEmpty(element.BaseElement) && GetAllStateCategoryNames(ObjectFinder.Self.GetIElement(element.BaseElement), true).Any(category => category == enumType)))
            {
                setBlock.Else()
                .Line("base.Current" + variableNameModifier + "State = base.Current" + variableNameModifier + "State;");
            }

            if (hasEvent)
            {
                EventCodeGenerator.GenerateEventRaisingCode(setBlock, BeforeOrAfter.After, variableToLookFor, element);
            }

            return(codeBlock);
        }
        private static ICodeBlock GenerateCurrentStateProperty(IElement element, ICodeBlock codeBlock, string enumType, List <StateSave> states)
        {
            var createField = false;

            if (enumType == "VariableState" && !DoesBaseHaveUncategorizedStates(element))  //Uncategorized and not base
            {
                createField = true;
            }
            else if (enumType != "VariableState")    //Check if this state category exists in a parent entity
            {
                if (element.BaseElement != null)
                {
                    var categories = GetAllStateCategoryNames(ObjectFinder.Self.GetIElement(element.BaseElement), true);

                    if (!categories.Any(category => category == enumType))
                    {
                        createField = true;
                    }
                }
                else
                {
                    createField = true;
                }
            }

            string variableNameModifier = enumType;

            if (enumType == "VariableState")
            {
                variableNameModifier = "";
            }

            string qualifiedEnumType = element.Name.Replace("\\", ".").Replace("/", ".") + "." + enumType;

            if (states.Count != 0)
            {
                string         variableToLookFor = "Current" + variableNameModifier + "State";
                CustomVariable customVariable    = element.GetCustomVariable(variableToLookFor);
                bool           hasEvent          = customVariable != null && customVariable.CreatesEvent;


                #region Header and Getter stuff - simple stuff with no logic

                if (createField)
                {
                    codeBlock
                    .Line(string.Format("protected int mCurrent{0}State = 0;", variableNameModifier));
                }

                string publicWithOptionalNew = "public";
                if (ShouldUseNewKeyword(element, enumType))
                {
                    publicWithOptionalNew += " new";
                }
                var setBlock = codeBlock
                               .Property(publicWithOptionalNew + " " + qualifiedEnumType, "Current" + variableNameModifier + "State")
                               .Get()
                               .If(string.Format("System.Enum.IsDefined(typeof({0}), mCurrent{1}State)", enumType, variableNameModifier))
                               .Line(string.Format("return ({0})mCurrent{1}State;", enumType, variableNameModifier))
                               .End()
                               .Else()
                               .Line(string.Format("return {0}.Unknown;", enumType))
                               .End()
                               .End()
                               .Set();

                #endregion

                #region Set the state value and call an event if necessary

                bool stillNeedsToAssignValue = true;

                if (element is EntitySave)
                {
                    EntitySave asEntitySave = element as EntitySave;

                    //if (!string.IsNullOrEmpty(asEntitySave.CurrentStateChange))
                    //{
                    //    setBlock
                    //        .If("value != mCurrent" + variableNameModifier + "State")
                    //            .Line("mCurrent" + variableNameModifier + "State = value;")
                    //            .Line(asEntitySave.CurrentStateChange + "(this, null);");

                    //    stillNeedsToAssignValue = false;
                    //}
                }

                if (hasEvent)
                {
                    EventCodeGenerator.GenerateEventRaisingCode(setBlock, BeforeOrAfter.Before, variableToLookFor, element);
                }

                if (stillNeedsToAssignValue)
                {
                    setBlock.Line("mCurrent" + variableNameModifier + "State = (int)value;");
                }

                #endregion

                var switchBlock = setBlock.Switch("Current" + variableNameModifier + "State");

                switchBlock.Case(enumType + ".Uninitialized");
                switchBlock.Case(enumType + ".Unknown");

                foreach (StateSave stateSave in states)
                {
                    GenerateCurrentStateCodeForIndividualState(element, switchBlock, stateSave, enumType);
                }

                if ((enumType == "VariableState" && DoesBaseHaveUncategorizedStates(element)) ||
                    (!string.IsNullOrEmpty(element.BaseElement) && GetAllStateCategoryNames(ObjectFinder.Self.GetIElement(element.BaseElement), true).Any(category => category == enumType)))
                {
                    switchBlock.Default()
                    .Line("base.Current" + variableNameModifier + "State = base.Current" + variableNameModifier + "State;");
                }

                if (hasEvent)
                {
                    EventCodeGenerator.GenerateEventRaisingCode(setBlock, BeforeOrAfter.After, variableToLookFor, element);
                }
            }
            return(codeBlock);
        }