Exemple #1
0
        public bool GenerateStandardElementSaveCodeFor(StandardElementSave standardElementSave, ICodeBlock codeBlock)
        {
            ///////////// EARLY OUT//////////////
            if (GueDerivingClassCodeGenerator.Self.ShouldGenerateRuntimeFor(standardElementSave) == false)
            {
                return(false);
            }
            //////////////END EARLY OUT///////////

            string runtimeClassName = GueDerivingClassCodeGenerator.GetUnqualifiedRuntimeTypeFor(standardElementSave);

            // This needs to be public because it can be exposed as public in a public class
            //ICodeBlock currentBlock = codeBlock.Class("partial", runtimeClassName, " : Gum.Wireframe.GraphicalUiElement");
            ICodeBlock currentBlock = codeBlock.Class("public partial", runtimeClassName, " : Gum.Wireframe.GraphicalUiElement");

            GueDerivingClassCodeGenerator.Self.GenerateConstructor(standardElementSave, currentBlock, runtimeClassName);

            string containedGraphicalObjectName = CreateContainedObjectMembers(currentBlock, standardElementSave);

            GenerateStates(standardElementSave, currentBlock);

            GenerateVariableProperties(standardElementSave, currentBlock, containedGraphicalObjectName);

            GenerateAssignDefaultState(standardElementSave, currentBlock);

            return(true);
        }
        private static void AddRegisterCode(ICodeBlock codeBlock, Gum.DataTypes.ElementSave element)
        {
            string elementNameString = element.Name.Replace("\\", "\\\\");

            codeBlock.Line(
                "GumRuntime.ElementSaveExtensions.RegisterGueInstantiationType(\"" +
                elementNameString +
                "\", typeof(" +
                GueDerivingClassCodeGenerator.GetQualifiedRuntimeTypeFor(element) +
                "));");
        }
        private void AddFormsAssociations(ICodeBlock currentBlock)
        {
            List <AssociationFulfillment> associationFulfillments = new List <AssociationFulfillment>();

            var loadedElements = AppState.Self.AllLoadedElements.ToList();

            foreach (var element in loadedElements)
            {
                var elementAsComponent = element as ComponentSave;

                if (elementAsComponent != null)
                {
                    foreach (var behavior in elementAsComponent.Behaviors)
                    {
                        var formsControlInfo = FormsControlInfo.AllControls
                                               .First(item => item.BehaviorName == behavior.BehaviorName);

                        string controlType = formsControlInfo.ControlName;

                        AssociationFulfillment matchingFulfillment = null;

                        if (controlType != null)
                        {
                            matchingFulfillment = associationFulfillments.FirstOrDefault(item => item.ControlType == controlType);
                        }

                        // Here we try to get the "most fulfilled" version of an object to set it as the default.
                        // For example, Button text is optional, and two Gum objects may have the Button behavior.
                        // If one of them has text properties then we should favor that over the one that doens't.
                        // Of coruse, the user can still change the defaults at runtime or manually create the visual
                        // for a form if they don't want the default, but this will hopefully give the "best fit"
                        // default.
                        if (matchingFulfillment == null || matchingFulfillment.IsCompletelyFulfilled == false)
                        {
                            bool isCompleteFulfillment = GetIfIsCompleteFulfillment(element, controlType);

                            if (matchingFulfillment == null)
                            {
                                var newFulfillment = new AssociationFulfillment();
                                newFulfillment.Element = element;
                                newFulfillment.IsCompletelyFulfilled = isCompleteFulfillment;
                                newFulfillment.ControlType           = controlType;

                                associationFulfillments.Add(newFulfillment);
                            }
                            else if (isCompleteFulfillment)
                            {
                                matchingFulfillment.Element = element;
                                matchingFulfillment.IsCompletelyFulfilled = isCompleteFulfillment;
                                matchingFulfillment.ControlType           = controlType;
                            }
                        }
                    }
                }
            }

            // Here we add controls that don't have explicit visual definitions (yet)
            var userControlFulfillment = associationFulfillments.FirstOrDefault(item => item.ControlType == "UserControl");

            // StackPanel doesn't have any visuals, it's invisible so it will just be a regular GraphicalUiElement.
            //if(userControlFulfillment != null)
            //{
            //    associationFulfillments.Add(new AssociationFulfillment
            //    {
            //        Element = userControlFulfillment.Element,
            //        IsCompletelyFulfilled = true,
            //        ControlType = "StackPanel"
            //    });
            //}

            foreach (var fulfillment in associationFulfillments)
            {
                var qualifiedControlType = "FlatRedBall.Forms.Controls." + fulfillment.ControlType;

                var gumRuntimeType = GueDerivingClassCodeGenerator.GetQualifiedRuntimeTypeFor(fulfillment.Element);

                var line =
                    $"FlatRedBall.Forms.Controls.FrameworkElement.DefaultFormsComponents[typeof({qualifiedControlType})] = typeof({gumRuntimeType});";

                currentBlock.Line(line);
            }
        }
        private void CreateStartingValueVariables(ElementSave element, IEnumerable <StateSave> states, ICodeBlock curBlock, Dictionary <VariableSave, InterpolationCharacteristic> interpolationCharacteristics)
        {
            foreach (StateSave state in states)
            {
                var variables = state.Variables.Where(item => GetIfShouldGenerateStateVariable(item, element)).ToList();

                foreach (VariableSave variableSave in variables)
                {
                    string member = variableSave.MemberNameInCode(element, VariableNamesToReplaceForStates);

                    if (!ContainsKey(interpolationCharacteristics, member, element))
                    {
                        InterpolationCharacteristic interpolationCharacteristic =
                            GetInterpolationCharacteristic(variableSave, element);

                        interpolationCharacteristics.Add(variableSave, interpolationCharacteristic);

                        if (interpolationCharacteristic != InterpolationCharacteristic.CantInterpolate)
                        {
                            string stringSuffix = variableSave.MemberNameInCode(element, VariableNamesToReplaceForStates).Replace(".", "");
                            curBlock.Line("bool set" + stringSuffix + FirstValue + " = false;");
                            curBlock.Line("bool set" + stringSuffix + SecondValue + " = false;");

                            string defaultStartingValue = "";

                            string type = variableSave.Type;

                            try
                            {
                                ElementSave       categoryContainer;
                                StateSaveCategory stateSaveCategory;
                                if (variableSave.IsState(element, out categoryContainer, out stateSaveCategory, recursive:false))
                                {
                                    var qualifiedCategoryContainerRuntimeName = GueDerivingClassCodeGenerator.GetQualifiedRuntimeTypeFor(categoryContainer);
                                    type = qualifiedCategoryContainerRuntimeName + ".VariableState";
                                    string defaultValue = "Default";
                                    if (stateSaveCategory != null)
                                    {
                                        type         = qualifiedCategoryContainerRuntimeName + "." + stateSaveCategory.Name;
                                        defaultValue = stateSaveCategory.States.First().MemberNameInCode();
                                    }

                                    defaultStartingValue = FlatRedBall.IO.FileManager.RemovePath(type) + "." + defaultValue;
                                }
                                else
                                {
                                    defaultStartingValue = FlatRedBall.Glue.Parsing.TypeManager.GetDefaultForType(variableSave.Type);
                                }
                            }
                            catch
                            {
                                throw new Exception("Could not get a default value for " + variableSave.GetRootName() + " of type " + variableSave.Type);
                            }



                            curBlock.Line(type + " " + member.Replace(".", "") + FirstValue + "= " + defaultStartingValue + ";");
                            curBlock.Line(type + " " + member.Replace(".", "") + SecondValue + "= " + defaultStartingValue + ";");
                        }
                    }
                }
            }
        }