public static bool IsState(this VariableSave variableSave, ElementSave container)
        {
            ElementSave       throwaway1;
            StateSaveCategory throwaway2;

            return(variableSave.IsState(container, out throwaway1, out throwaway2));
        }
예제 #2
0
        private void AddAssignmentForInterpolationForVariable(ICodeBlock curBlock, VariableSave variable, ElementSave container)
        {
            string            memberNameInCode = variable.MemberNameInCode(container, VariableNamesToReplaceForStates);
            ElementSave       categoryContainer;
            StateSaveCategory stateSaveCategory;

            if (variable.IsState(container, out categoryContainer, out stateSaveCategory, recursive: false) && !string.IsNullOrEmpty(variable.SourceObject))
            {
                string line = string.Format(
                    "{0}.InterpolateBetween({1}FirstValue, {1}SecondValue, interpolationValue);",
                    SaveObjectExtensionMethods.InstanceNameInCode(variable.SourceObject), memberNameInCode.Replace(".", ""));
                curBlock.Line(line);
            }
            else
            {
                switch (variable.Type)
                {
                case "int":
                    curBlock.Line(string.Format("{0} = FlatRedBall.Math.MathFunctions.RoundToInt({1}FirstValue* (1 - interpolationValue) + {1}SecondValue * interpolationValue);",
                                                memberNameInCode, memberNameInCode.Replace(".", "")));

                    break;

                case "float":
                case "double":
                    curBlock.Line(string.Format("{0} = {1}FirstValue * (1 - interpolationValue) + {1}SecondValue * interpolationValue;",
                                                memberNameInCode, memberNameInCode.Replace(".", "")));
                    break;
                }
            }
        }
예제 #3
0
        private InterpolationCharacteristic GetInterpolationCharacteristic(VariableSave variableSave, ElementSave container)
        {
            string variableType = null;

            if (variableSave != null)
            {
                variableType = variableSave.Type;
            }


            if (variableSave != null && variableSave.IsState(container))
            {
                ElementSave       categoryContainer;
                StateSaveCategory stateSaveCategory;

                if (variableSave.IsState(container, out categoryContainer, out stateSaveCategory, recursive: false))
                {
                    return(InterpolationCharacteristic.CanInterpolate);
                }
                else
                {
                    // it's an exposed variable which cant be interpolated currently
                    return(InterpolationCharacteristic.CantInterpolate);
                }
            }

            if (variableSave == null ||
                variableType == null ||
                variableType == "string" ||
                variableType == "bool" ||
                variableType == "Color" ||
                variableSave.IsFile
                )
            {
                return(InterpolationCharacteristic.CantInterpolate);
            }

            if (variableType == "float" || variableType == "int" || variableType == "double" || variableType == "byte")
            {
                return(InterpolationCharacteristic.CanInterpolate);
            }
            return(InterpolationCharacteristic.CantInterpolate);
        }
        public static string MemberNameInCode(this VariableSave variableSave, ElementSave container, Dictionary <string, string> replacements = null)
        {
            if (replacements == null)
            {
                replacements = StateCodeGenerator.VariableNamesToReplaceForStates;
            }
            var rootName   = variableSave.GetRootName();
            var objectName = variableSave.SourceObject;

            if (replacements.ContainsKey(rootName))
            {
                rootName = replacements[rootName];
            }
            else
            {
                rootName = rootName.Replace(" ", "_");
            }

            ElementSave       throwaway1;
            StateSaveCategory throwaway2;

            // recursive is false because we only want to prepend "Current" if it's not an exposed variable
            if (variableSave.IsState(container, out throwaway1, out throwaway2, recursive: false))
            {
                if (rootName == "State")
                {
                    rootName = "CurrentVariableState";
                }
                else
                {
                    rootName = "Current" + rootName;
                }
            }

            if (string.IsNullOrEmpty(objectName))
            {
                return(rootName);
            }
            else
            {
                objectName = InstanceNameInCode(objectName);
                return(objectName + '.' + rootName);
            }
        }
예제 #5
0
        private static void FillWithExposedVariable(VariableSave exposedVariable, ElementSave container, StringBuilder stringBuilder, int tabCount)
        {
            var type = exposedVariable.Type;

            if (exposedVariable.IsState(container, out ElementSave stateContainer, out StateSaveCategory category))
            {
                var stateContainerType = GetClassNameForType(stateContainer.Name, VisualApi.Gum);
                type = $"{stateContainerType}.{category.Name}";
            }

            stringBuilder.AppendLine(ToTabs(tabCount) + $"public {type} {exposedVariable.ExposedAsName}");
            stringBuilder.AppendLine(ToTabs(tabCount) + "{");
            tabCount++;
            stringBuilder.AppendLine(ToTabs(tabCount) + $"get => {exposedVariable.Name};");
            stringBuilder.AppendLine(ToTabs(tabCount) + $"set => {exposedVariable.Name} = value;");
            tabCount--;

            stringBuilder.AppendLine(ToTabs(tabCount) + "}");
        }
예제 #6
0
 private static string VariableValueToGumCodeValue(VariableSave variable, ElementSave container)
 {
     if (variable.Value is float asFloat)
     {
         return(asFloat.ToString(CultureInfo.InvariantCulture) + "f");
     }
     else if (variable.Value is string asString)
     {
         if (variable.GetRootName() == "Parent")
         {
             return(asString);
         }
         else if (variable.IsState(container, out ElementSave categoryContainer, out StateSaveCategory category))
         {
             var containerClassName = GetClassNameForType(categoryContainer.Name, VisualApi.Gum);
             return($"{containerClassName}.{category.Name}.{asString}");
         }
         else
         {
             return("\"" + asString.Replace("\n", "\\n") + "\"");
         }
     }
        public static TypeConverter GetTypeConverter(this VariableSave variableSave, ElementSave container = null)
        {
            ElementSave       categoryContainer;
            StateSaveCategory category;

            if (variableSave.CustomTypeConverter != null)
            {
                return(variableSave.CustomTypeConverter);
            }
            else if (variableSave.IsFont)
            {
                return(new FontTypeConverter());
            }
            else if (variableSave.Name == "Guide")
            {
                AvailableGuidesTypeConverter availableGuidesTypeConverter = new AvailableGuidesTypeConverter();
                availableGuidesTypeConverter.GumProjectSave = ObjectFinder.Self.GumProjectSave;
                availableGuidesTypeConverter.ShowNewGuide   = false;
                return(availableGuidesTypeConverter);
            }
            else if (variableSave.IsState(container, out categoryContainer, out category))
            {
                string categoryName = null;

                if (category != null)
                {
                    categoryName = category.Name;
                }

                AvailableStatesConverter converter = new AvailableStatesConverter(categoryName);
                converter.ElementSave = categoryContainer;
                return(converter);
            }
            else
            {
                // We should see if it's an exposed variable, and if so, let's look to the source object's type converters
                bool foundInRoot = false;
                if (!string.IsNullOrEmpty(variableSave.SourceObject) && container != null)
                {
                    InstanceSave instance = container.GetInstance(variableSave.SourceObject);

                    if (instance != null)
                    {
                        // see if the instance has a variable
                        var foundElementSave = ObjectFinder.Self.GetRootStandardElementSave(instance);

                        if (foundElementSave != null)
                        {
                            VariableSave rootVariableSave = foundElementSave.DefaultState.GetVariableSave(variableSave.GetRootName());

                            if (rootVariableSave != null)
                            {
                                return(rootVariableSave.GetTypeConverter((ElementSave)null));
                            }
                        }
                    }
                }
            }
            Type type = variableSave.GetRuntimeType();

            return(variableSave.GetTypeConverter(type));
        }
예제 #8
0
        private static bool GetShouldIncludeBasedOnBaseType(VariableSave defaultVariable, ElementSave container, InstanceSave instanceSave, StandardElementSave rootElementSave)
        {
            bool shouldInclude = false;

            if (string.IsNullOrEmpty(defaultVariable.SourceObject))
            {
                if (container is ScreenSave)
                {
                    // If it's a Screen, then the answer is "yes" because
                    // Screens don't have a base type that they can switch,
                    // so any variable that's part of the Screen is always part
                    // of the Screen.
                    // do nothing to shouldInclude

                    shouldInclude = true;
                }
                else
                {
                    if (container is ComponentSave)
                    {
                        // See if it's defined in the standards list
                        var foundInstance = StandardElementsManager.Self.GetDefaultStateFor("Component").Variables.FirstOrDefault(
                            item => item.Name == defaultVariable.Name);

                        shouldInclude = foundInstance != null;
                    }
                    // If the defaultVariable's
                    // source object is null then
                    // that means that the variable
                    // is being set on "this".  However,
                    // variables that are set on "this" may
                    // not actually be valid for the type, but
                    // they may still exist because the object type
                    // was switched.  Therefore, we want to make sure
                    // that the variable is valid given the type of object
                    // that "this" currently is by checking the default state
                    // on the rootElementSave
                    if (!shouldInclude && rootElementSave != null)
                    {
                        shouldInclude = rootElementSave.DefaultState.GetVariableSave(defaultVariable.Name) != null;
                    }

                    string nameWithoutState = null;
                    if (!shouldInclude && defaultVariable.Name.EndsWith("State") && instanceSave != null)
                    {
                        nameWithoutState = defaultVariable.Name.Substring(0, defaultVariable.Name.Length - "State".Length);

                        var instanceElement = ObjectFinder.Self.GetElementSave(instanceSave.BaseType);


                        // See if this is a category:
                        if (!string.IsNullOrEmpty(nameWithoutState) &&
                            instanceElement != null && instanceElement.Categories.Any(item => item.Name == nameWithoutState))
                        {
                            shouldInclude = true;
                        }
                    }

                    if (!shouldInclude && instanceSave == null && defaultVariable.IsState(container))
                    {
                        return(true);
                    }
                }
            }

            else
            {
                shouldInclude = SelectedState.Self.SelectedInstance != null || !string.IsNullOrEmpty(defaultVariable.ExposedAsName);
            }
            return(shouldInclude);
        }
예제 #9
0
        //private void ReactIfChangedMemberIsAnimation(ElementSave parentElement, string changedMember, object oldValue, out bool saveProject)
        //{
        //    const string sourceFileString = "SourceFile";
        //    if (changedMember == sourceFileString)
        //    {
        //        StateSave stateSave = SelectedState.Self.SelectedStateSave;

        //        string value = (string)stateSave.GetValueRecursive(sourceFileString);

        //        if (!FileManager.IsRelative)
        //        {

        //        }

        //        saveProject = true;
        //    }

        //    saveProject = false;
        //}

        /// <summary>
        /// Called when the user clicks the "Make Default" menu item
        /// </summary>
        /// <param name="variableName">The variable to make default.</param>
        private void ResetVariableToDefault(StateReferencingInstanceMember srim)
        {
            string variableName = srim.Name;

            bool shouldReset     = false;
            bool affectsTreeView = false;

            var selectedElement = SelectedState.Self.SelectedElement;

            if (SelectedState.Self.SelectedInstance != null)
            {
                affectsTreeView = variableName == "Parent";
                //variableName = SelectedState.Self.SelectedInstance.Name + "." + variableName;

                shouldReset = true;
            }
            else if (selectedElement != null)
            {
                shouldReset =
                    // Don't let the user reset standard element variables, they have to have some actual value
                    (selectedElement is StandardElementSave) == false ||
                    // ... unless it's not the default
                    SelectedState.Self.SelectedStateSave != SelectedState.Self.SelectedElement.DefaultState;
            }

            // now we reset, but we don't remove the variable:
            //if(shouldReset)
            //{
            //    // If the variable is part of a category, then we don't allow setting the variable to default - they gotta do it through the cateory itself

            //    if (isPartOfCategory)
            //    {
            //        var window = new DeletingVariablesInCategoriesMessageBox();
            //        window.ShowDialog();

            //        shouldReset = false;
            //    }
            //}

            if (shouldReset)
            {
                bool isPartOfCategory = srim.StateSaveCategory != null;

                StateSave    state         = SelectedState.Self.SelectedStateSave;
                bool         wasChangeMade = false;
                VariableSave variable      = state.GetVariableSave(variableName);
                if (variable != null)
                {
                    // Don't remove the variable if it's part of an element - we still want it there
                    // so it can be set, we just don't want it to set a value
                    // Update August 13, 2013
                    // Actually, we do want to remove it if it's part of an element but not the
                    // default state
                    // Update October 17, 2017
                    // Now that components do not
                    // necessarily need to have all
                    // of their variables, we can remove
                    // the variable now. In fact, we should
                    //bool shouldRemove = SelectedState.Self.SelectedInstance != null ||
                    //    SelectedState.Self.SelectedStateSave != SelectedState.Self.SelectedElement.DefaultState;
                    // Also, don't remove it if it's an exposed variable, this un-exposes things
                    bool shouldRemove = string.IsNullOrEmpty(variable.ExposedAsName) && !isPartOfCategory;

                    // Update October 7, 2019
                    // Actually, we can remove any variable so long as the current state isn't the "base definition" for it
                    // For elements - no variables are the base variable definitions except for variables that are categorized
                    // state variables for categories defined in this element
                    if (shouldRemove)
                    {
                        var isState = variable.IsState(selectedElement, out ElementSave categoryContainer, out StateSaveCategory categoryForVariable);

                        if (isState)
                        {
                            var isDefinedHere = categoryForVariable != null && categoryContainer == selectedElement;

                            shouldRemove = !isDefinedHere;
                        }
                    }


                    if (shouldRemove)
                    {
                        state.Variables.Remove(variable);
                    }
                    else if (isPartOfCategory)
                    {
                        var variableInDefault = SelectedState.Self.SelectedElement.DefaultState.GetVariableSave(variable.Name);
                        if (variableInDefault != null)
                        {
                            GumCommands.Self.GuiCommands.PrintOutput(
                                $"The variable {variable.Name} is part of the category {srim.StateSaveCategory.Name} so it cannot be removed. Instead, the value has been set to the value in the default state");

                            variable.Value = variableInDefault.Value;
                        }
                        else
                        {
                            GumCommands.Self.GuiCommands.PrintOutput("Could not set value to default because the default state doesn't set this value");
                        }
                    }
                    else
                    {
                        variable.Value     = null;
                        variable.SetsValue = false;
                    }

                    wasChangeMade = true;
                    // We need to refresh the property grid and the wireframe display
                }
                else
                {
                    // Maybe this is a variable list?
                    VariableListSave variableList = state.GetVariableListSave(variableName);
                    if (variableList != null)
                    {
                        state.VariableLists.Remove(variableList);

                        // We don't support this yet:
                        // variableList.SetsValue = false; // just to be safe
                        wasChangeMade = true;
                    }
                }

                if (wasChangeMade)
                {
                    RefreshUI(force: true);
                    WireframeObjectManager.Self.RefreshAll(true);
                    SelectionManager.Self.Refresh();

                    if (affectsTreeView)
                    {
                        GumCommands.Self.GuiCommands.RefreshElementTreeView(SelectedState.Self.SelectedElement);
                    }

                    if (ProjectManager.Self.GeneralSettingsFile.AutoSave)
                    {
                        ProjectManager.Self.SaveElement(SelectedState.Self.SelectedElement);
                    }
                }
            }
            else
            {
                srim.IsDefault = false;
            }
        }
예제 #10
0
        private void ModifyVariableTypeForProperty(ref string variableType, VariableSave variableSave, ElementSave elementSave)
        {
            if (mTypeToQualifiedTypes.ContainsKey(variableType))
            {
                variableType = mTypeToQualifiedTypes[variableType];
            }

            if (string.IsNullOrEmpty(variableSave.SourceObject))
            {
                if (variableType == "State")
                {
                    // Not sure why this was returning CurrentVariableState, as that is the property name,
                    // not the property type, and here we want the property type.
                    //variableType = elementSave.Name + "Runtime.CurrentVariableState";
                    //variableType = FlatRedBall.IO.FileManager.RemovePath(elementSave.Name) + "Runtime.VariableState";
                    // Actually we want to include the prefix for namespace:
                    //variableType = FlatRedBall.IO.FileManager.RemovePath(elementSave.Name) + "Runtime.VariableState";
                    variableType = elementSave.Name.Replace('/', '.').Replace('\\', '.') + "Runtime.VariableState";
                }
                else if (variableSave.Type.EndsWith("State"))
                {
                    var typeWithoutState = variableSave.Type.Substring(0, variableSave.Type.Length - "State".Length);
                    var foundCategory    = elementSave.Categories.FirstOrDefault(item => item.Name == typeWithoutState);
                    if (foundCategory != null)
                    {
                        // categorized state enums are nullable
                        variableType = $"{elementSave.Name.Replace('/', '.').Replace('\\', '.')}Runtime.{foundCategory.Name}?";
                    }
                }
                else if (variableSave.IsState(elementSave))
                {
                    var foundCategory = elementSave.Categories.FirstOrDefault(item => item.Name == variableSave.Type);
                    if (foundCategory != null)
                    {
                        // categorized state enums are nullable
                        variableType = $"{elementSave.Name.Replace('/', '.').Replace('\\', '.')}Runtime.{foundCategory.Name}?";
                    }
                }
            }
            else if (variableSave.IsFile)
            {
                variableType = "Microsoft.Xna.Framework.Graphics.Texture2D";
            }
            else
            {
                var instance = elementSave.Instances.FirstOrDefault(item => item.Name == variableSave.SourceObject);

                if (instance != null)
                {
                    var element = ObjectFinder.Self.GetElementSave(instance);

                    if (element != null)
                    {
                        var rootName = variableSave.GetRootName();
                        var variableInInstanceElement = element.DefaultState.Variables.FirstOrDefault(item => item.Name == rootName || item.ExposedAsName == rootName);

                        if (variableInInstanceElement != null)
                        {
                            ModifyVariableTypeForProperty(ref variableType, variableInInstanceElement, element);
                        }
                    }
                }
            }
        }