コード例 #1
0
ファイル: SetVariableLogic.cs プロジェクト: gitter-badger/Gum
        private void ReactIfChangedMemberIsSourceFile(ElementSave parentElement, InstanceSave instance, string changedMember, object oldValue)
        {
            string variableFullName;

            if (instance != null)
            {
                variableFullName = $"{instance.Name}.{changedMember}";
            }
            else
            {
                variableFullName = changedMember;
            }

            VariableSave variable = SelectedState.Self.SelectedStateSave?.GetVariableSave(variableFullName);

            bool isSourcefile = variable?.GetRootName() == "SourceFile";

            string errorMessage = null;

            if (isSourcefile)
            {
                errorMessage = GetWhySourcefileIsInvalid(variable.Value as string);

                if (!string.IsNullOrEmpty(errorMessage))
                {
                    MessageBox.Show(errorMessage);

                    variable.Value = oldValue;
                }
                else
                {
                    string value;

                    value = variable.Value as string;

                    if (!string.IsNullOrEmpty(value))
                    {
                        // See if this is relative to the project
                        var isRelativeToProject = FileManager.IsRelativeTo(
                            value,
                            ProjectState.Self.ProjectDirectory);

                        if (!isRelativeToProject)
                        {
                            bool shouldCopy = AskIfShouldCopy(variable, value);
                            if (shouldCopy)
                            {
                                PerformCopy(variable, value);
                            }
                        }
                    }

                    StateSave stateSave = SelectedState.Self.SelectedStateSave;

                    RecursiveVariableFinder rvf = new RecursiveVariableFinder(stateSave);

                    stateSave.SetValue("AnimationFrames", new List <string>());
                }
            }
        }
コード例 #2
0
ファイル: CodeGenerator.cs プロジェクト: derKosi/Gum
        private static string TryGetFullXamarinFormsLineReplacement(InstanceSave instance, ElementSave container, VariableSave variable, StateSave state)
        {
            var rootName = variable.GetRootName();

            if (rootName == "IsXamarinFormsControl" ||
                rootName == "Name" ||
                rootName == "X Origin" ||
                rootName == "XOrigin" ||
                rootName == "Y Origin" ||
                rootName == "YOrigin")
            {
                return(" "); // Don't do anything with these variables::
            }
            else if (rootName == "Parent")
            {
                var parentName = variable.Value as string;

                var parentInstance = container.GetInstance(parentName);

                var hasContent =
                    parentInstance?.BaseType.EndsWith("/ScrollView") == true ||
                    parentInstance?.BaseType.EndsWith("/StickyScrollView") == true;
                if (hasContent)
                {
                    return($"{parentName}.Content = {instance.Name};");
                }
                else
                {
                    return($"{parentName}.Children.Add({instance.Name});");
                }
            }


            return(null);
        }
コード例 #3
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;");
            }
        }
コード例 #4
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";
                }
                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)
                    {
                        variableType = FlatRedBall.IO.FileManager.RemovePath(elementSave.Name) + "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);
                        }
                    }
                }
            }
        }
コード例 #5
0
        private bool TryHandleCustomGetter(VariableSave variable, ElementSave elementSave, ICodeBlock getter)
        {
            if (variable.GetRootName() == "SourceFile" && elementSave.Name == "NineSlice")
            {
                getter.Line("return ContainedNineSlice.TopLeftTexture;");

                return(true);
            }
            // handle colors:

            if (elementSave.Name == "Circle" || elementSave.Name == "Rectangle" ||
                elementSave.Name == "Polygon")
            {
                string containedObject;

                if (elementSave.Name == "Circle")
                {
                    containedObject = "ContainedCircle";
                }
                else if (elementSave.Name == "Rectangle")
                {
                    containedObject = "ContainedRectangle";
                }
                else
                {
                    containedObject = "ContainedPolygon";
                }
                if (variable.Name == "Alpha")
                {
                    getter.Line($"return {containedObject}.Color.A;");
                    return(true);
                }
                else if (variable.Name == "Red")
                {
                    getter.Line($"return {containedObject}.Color.R;");
                    return(true);
                }
                else if (variable.Name == "Green")
                {
                    getter.Line($"return {containedObject}.Color.G;");
                    return(true);
                }
                else if (variable.Name == "Blue")
                {
                    getter.Line($"return {containedObject}.Color.B;");
                    return(true);
                }
            }
            return(false);
        }
コード例 #6
0
ファイル: CodeGenerator.cs プロジェクト: derKosi/Gum
        private static string GetSuffixCodeLine(InstanceSave instance, VariableSave variable, VisualApi visualApi)
        {
            if (visualApi == VisualApi.XamarinForms)
            {
                var rootName = variable.GetRootName();

                //switch(rootName)
                //{
                // We don't do this anymore now that we are stuffing forms objects in absolute layouts
                //case "Width": return $"{instance.Name}.HorizontalOptions = LayoutOptions.Start;";
                //case "Height": return $"{instance.Name}.VerticalOptions = LayoutOptions.Start;";
                //}
            }

            return(null);
        }
コード例 #7
0
        public static bool GetIsFileFromRoot(this VariableSave variable, InstanceSave instance)
        {
            if (string.IsNullOrEmpty(variable.SourceObject))
            {
                ElementSave root = ObjectFinder.Self.GetRootStandardElementSave(instance);

                var variableInRoot = root.DefaultState.Variables.FirstOrDefault(item => item.Name == variable.GetRootName());

                if (variableInRoot != null)
                {
                    return(variableInRoot.IsFile);
                }
            }
            else
            {
                ElementSave elementForInstance = ObjectFinder.Self.GetElementSave(instance.BaseType);

                string       rootName        = variable.GetRootName();
                VariableSave exposedVariable = elementForInstance.DefaultState.Variables.FirstOrDefault(item => item.ExposedAsName == rootName);

                if (exposedVariable != null)
                {
                    InstanceSave subInstance = elementForInstance.Instances.FirstOrDefault(item => item.Name == exposedVariable.SourceObject);

                    if (subInstance != null)
                    {
                        return(exposedVariable.GetIsFileFromRoot(subInstance));
                    }
                }
                else
                {
                    // it's not exposed, so let's just get to the root of it:

                    ElementSave root = ObjectFinder.Self.GetRootStandardElementSave(instance);

                    var variableInRoot = root.DefaultState.Variables.FirstOrDefault(item => item.Name == variable.GetRootName());

                    if (variableInRoot != null)
                    {
                        return(variableInRoot.IsFile);
                    }
                }
            }
            return(false);
        }
コード例 #8
0
ファイル: CodeGenerator.cs プロジェクト: derKosi/Gum
        private static string TryGetFullGumLineReplacement(InstanceSave instance, VariableSave variable)
        {
            var rootName = variable.GetRootName();

            if (rootName == "Parent")
            {
                return($"{variable.Value}.Children.Add({instance.Name});");
            }
            // ignored variables:
            else if (rootName == "IsXamarinFormsControl" ||
                     rootName == "ClipsChildren" ||
                     rootName == "ExposeChildrenEvents" ||
                     rootName == "HasEvents")
            {
                return(" ");
            }
            return(null);
        }
コード例 #9
0
        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);
            }
        }
コード例 #10
0
ファイル: CodeGenerator.cs プロジェクト: derKosi/Gum
 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") + "\"");
         }
     }
コード例 #11
0
        private bool TryHandleCustomSetter(VariableSave variable, ElementSave elementSave, ICodeBlock setter)
        {
            if (variable.GetRootName() == "SourceFile" && elementSave.Name == "NineSlice")
            {
                setter.Line("ContainedNineSlice.SetSingleTexture(value);");
                return(true);
            }

            if (elementSave.Name == "Circle" && variable.GetRootName() == "Radius")
            {
                setter.Line("mWidth = value/2;");
                setter.Line("mHeight = value/2;");
                setter.Line("ContainedCircle.Radius = value;");
                return(true);
            }

            if (elementSave.Name == "Circle" ||
                elementSave.Name == "Rectangle" ||
                elementSave.Name == "Polygon")
            {
                string containedObject = null;

                if (elementSave.Name == "Circle")
                {
                    containedObject = "ContainedCircle";
                }
                else if (elementSave.Name == "Rectangle")
                {
                    containedObject = "ContainedRectangle";
                }
                else if (elementSave.Name == "Polygon")
                {
                    containedObject = "ContainedPolygon";
                }

                string colorComponent = null;

                if (variable.Name == "Alpha")
                {
                    colorComponent = "A";
                }
                else if (variable.Name == "Red")
                {
                    colorComponent = "R";
                }
                else if (variable.Name == "Green")
                {
                    colorComponent = "G";
                }
                else if (variable.Name == "Blue")
                {
                    colorComponent = "B";
                }

                if (!string.IsNullOrEmpty(colorComponent))
                {
                    setter.Line($"var color = {containedObject}.Color;");
                    setter.Line($"color.{colorComponent} = (byte)value;");
                    setter.Line($"{containedObject}.Color = color;");
                    return(true);
                }
            }

            return(false);
        }
コード例 #12
0
        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));
        }
コード例 #13
0
        public static bool GetIsFileFromRoot(this VariableSave variable, ElementSave element)
        {
            var variableInRoot = element.DefaultState.Variables.FirstOrDefault(item => item.Name == variable.GetRootName());

            if (variableInRoot != null)
            {
                return(variableInRoot.IsFile);
            }
            else
            {
                // unknown so assume no
                return(false);
            }
        }
コード例 #14
0
        public static bool IsState(this VariableSave variableSave, ElementSave container, out ElementSave categoryContainer, out StateSaveCategory category, bool recursive = true)
        {
            category          = null;
            categoryContainer = null;

            var variableName = variableSave.GetRootName();

            // This is called a lot so let's try to make it faster:
            bool endsWithState = variableName.Length >= 5 &&
                                 variableName[variableName.Length - 1] == 'e' &&
                                 variableName[variableName.Length - 2] == 't' &&
                                 variableName[variableName.Length - 3] == 'a' &&
                                 variableName[variableName.Length - 4] == 't' &&
                                 variableName[variableName.Length - 5] == 'S';

            ///////////////Early Out
            if (endsWithState == false && string.IsNullOrEmpty(variableSave.SourceObject))
            {
                return(false);
            }
            /////////////End early out

            // what about uncategorized

            string categoryName = null;

            if (endsWithState)
            {
                categoryName = variableName.Substring(0, variableName.Length - "State".Length);
            }

            if (string.IsNullOrEmpty(variableSave.SourceObject) == false)
            {
                var instanceSave = container.GetInstance(variableSave.SourceObject);

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

                    if (element != null)
                    {
                        var defaultState = element.DefaultState;
                        if (defaultState == null)
                        {
                            throw new NullReferenceException(
                                      $"Could not find a default state for {element} - this happens if the element wasn't initialized, or if its file was not loaded properly.");
                        }
                        // let's try going recursively:
                        var subVariable = element.DefaultState.Variables.FirstOrDefault(item => item.ExposedAsName == variableSave.GetRootName());

                        if (subVariable != null && recursive)
                        {
                            return(subVariable.IsState(element, out categoryContainer, out category));
                        }
                        else
                        {
                            if (variableName == "State")
                            {
                                categoryContainer = element;
                                category          = null;
                                return(true);
                            }
                            else
                            {
                                category = element.GetStateSaveCategoryRecursively(categoryName, out categoryContainer);
                                return(category != null);
                            }
                        }
                    }
                }
            }
            else
            {
                if (variableName == "State")
                {
                    return(true);
                }
                else
                {
                    category = container.GetStateSaveCategoryRecursively(categoryName, out categoryContainer);

                    return(category != null);
                }
            }

            return(false);
        }
コード例 #15
0
        public static bool IsState(this VariableSave variableSave, ElementSave container, out ElementSave categoryContainer, out StateSaveCategory category, bool recursive = true)
        {
            category          = null;
            categoryContainer = null;

            var variableName = variableSave.GetRootName();

            ///////////////Early Out
            if (variableName.EndsWith("State") == false && string.IsNullOrEmpty(variableSave.SourceObject))
            {
                return(false);
            }
            /////////////End early out

            // what about uncategorized

            string categoryName = null;

            if (variableName.EndsWith("State"))
            {
                categoryName = variableName.Substring(0, variableName.Length - "State".Length);
            }

            if (string.IsNullOrEmpty(variableSave.SourceObject) == false)
            {
                var instanceSave = container.GetInstance(variableSave.SourceObject);

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

                    if (element != null)
                    {
                        // let's try going recursively:
                        var subVariable = element.DefaultState.Variables.FirstOrDefault(item => item.ExposedAsName == variableSave.GetRootName());

                        if (subVariable != null && recursive)
                        {
                            return(subVariable.IsState(element, out categoryContainer, out category));
                        }
                        else
                        {
                            if (variableName == "State")
                            {
                                categoryContainer = element;
                                category          = null;
                                return(true);
                            }
                            else
                            {
                                category = element.GetStateSaveCategoryRecursively(categoryName, out categoryContainer);
                                return(category != null);
                            }
                        }
                    }
                }
            }
            else
            {
                if (variableName == "State")
                {
                    return(true);
                }
                else
                {
                    category = container.GetStateSaveCategoryRecursively(categoryName, out categoryContainer);

                    return(category != null);
                }
            }

            return(false);
        }
コード例 #16
0
ファイル: SetVariableLogic.cs プロジェクト: patridge/Gum
        private void ReactIfChangedMemberIsSourceFile(ElementSave parentElement, InstanceSave instance, string changedMember, object oldValue)
        {
            ////////////Early Out /////////////////////////////
            string variableFullName;

            var instancePrefix = instance != null ? $"{instance.Name}." : "";

            variableFullName = $"{instancePrefix}{changedMember}";

            VariableSave variable = SelectedState.Self.SelectedStateSave?.GetVariableSave(variableFullName);

            bool isSourcefile = variable?.GetRootName() == "SourceFile";

            if (!isSourcefile)
            {
                return;
            }

            string errorMessage = GetWhySourcefileIsInvalid(variable.Value as string);

            if (!string.IsNullOrEmpty(errorMessage))
            {
                MessageBox.Show(errorMessage);

                variable.Value = oldValue;
            }
            else
            {
                string value;

                value = variable.Value as string;
                StateSave stateSave = SelectedState.Self.SelectedStateSave;

                if (!string.IsNullOrEmpty(value))
                {
                    var filePath = new FilePath(ProjectState.Self.ProjectDirectory + value);

                    // See if this is relative to the project
                    var shouldAskToCopy = !FileManager.IsRelativeTo(
                        filePath.FullPath,
                        ProjectState.Self.ProjectDirectory);

                    if (shouldAskToCopy &&
                        !string.IsNullOrEmpty(ProjectState.Self.GumProjectSave?.ParentProjectRoot) &&
                        FileManager.IsRelativeTo(filePath.FullPath, ProjectState.Self.ProjectDirectory + ProjectState.Self.GumProjectSave.ParentProjectRoot))
                    {
                        shouldAskToCopy = false;
                    }

                    if (shouldAskToCopy)
                    {
                        bool shouldCopy = AskIfShouldCopy(variable, value);
                        if (shouldCopy)
                        {
                            PerformCopy(variable, value);
                        }
                    }

                    if (filePath.Extension == "achx")
                    {
                        stateSave.SetValue($"{instancePrefix}Texture Address", Gum.Managers.TextureAddress.Custom);
                        GumCommands.Self.GuiCommands.RefreshPropertyGrid(force: true);
                    }
                }


                stateSave.SetValue($"{instancePrefix}AnimationFrames", new List <string>());
            }
        }
コード例 #17
0
        private void ReactIfChangedMemberIsTexture(ElementSave parentElement, string changedMember, object oldValue)
        {
            VariableSave variable = SelectedState.Self.SelectedVariableSave;

            // Eventually need to handle tunneled variables
            if (variable != null && variable.GetRootName() == "SourceFile")
            {
                string value = variable.Value as string;

                if (!string.IsNullOrEmpty(value))
                {
                    // See if this is relative to the project
                    bool isRelativeToProject = !value.StartsWith("../") && !value.StartsWith("..\\");

                    if (!isRelativeToProject)
                    {
                        // Ask the user what to do - make it relative?
                        MultiButtonMessageBox mbmb = new
                                                     MultiButtonMessageBox();

                        mbmb.MessageText = "The file\n" + value + "\nis not relative to the project.  What would you like to do?";
                        mbmb.AddButton("Reference the file in its current location", DialogResult.OK);
                        mbmb.AddButton("Copy the file relative to the Gum project and reference the copy", DialogResult.Yes);

                        var dialogResult = mbmb.ShowDialog();

                        bool shouldCopy = false;

                        string directory          = FileManager.GetDirectory(ProjectManager.Self.GumProjectSave.FullFileName);
                        string targetAbsoluteFile = directory + FileManager.RemovePath(value);

                        if (dialogResult == DialogResult.Yes)
                        {
                            shouldCopy = true;

                            // If the destination already exists, we gotta ask the user what they want to do.
                            if (System.IO.File.Exists(targetAbsoluteFile))
                            {
                                mbmb             = new MultiButtonMessageBox();
                                mbmb.MessageText = "The destination file already exists.  Would you like to overwrite it?";
                                mbmb.AddButton("Yes", DialogResult.Yes);
                                mbmb.AddButton("No, use the original file", DialogResult.No);

                                shouldCopy = mbmb.ShowDialog() == DialogResult.Yes;
                            }
                        }

                        if (shouldCopy)
                        {
                            try
                            {
                                string sourceAbsoluteFile =
                                    directory + value;
                                sourceAbsoluteFile = FileManager.RemoveDotDotSlash(sourceAbsoluteFile);

                                System.IO.File.Copy(sourceAbsoluteFile, targetAbsoluteFile, overwrite: true);

                                variable.Value = FileManager.RemovePath(value);
                            }
                            catch (Exception e)
                            {
                                MessageBox.Show("Error copying file:\n" + e.ToString());
                            }
                        }
                    }
                }


                StateSave stateSave = SelectedState.Self.SelectedStateSave;

                RecursiveVariableFinder rvf = new RecursiveVariableFinder(stateSave);

                stateSave.SetValue("AnimationFrames", new List <string>());
            }
        }
コード例 #18
0
        public static void PasteInstanceSaves(List <InstanceSave> instancesToCopy, StateSave copiedState, ElementSave targetElement)
        {
            Dictionary <string, string> oldNewNameDictionary = new Dictionary <string, string>();



            List <InstanceSave> newInstances = new List <InstanceSave>();

            foreach (var sourceInstance in instancesToCopy)
            {
                ElementSave sourceElement = sourceInstance.ParentContainer;

                InstanceSave newInstance = sourceInstance.Clone();

                // the original may have been defined in a base component. The new instance will not be
                // derived in the base, so let's get rid of that:
                newInstance.DefinedByBase = false;

                newInstances.Add(newInstance);

                if (targetElement != null)
                {
                    var oldName = newInstance.Name;
                    newInstance.Name = StringFunctions.MakeStringUnique(newInstance.Name, targetElement.Instances.Select(item => item.Name));
                    var newName = newInstance.Name;

                    oldNewNameDictionary[oldName] = newName;

                    if (targetElement == sourceElement)
                    {
                        var original = sourceElement.Instances.FirstOrDefault(item => item.Name == sourceInstance.Name);
                        int newIndex = -1;
                        if (original != null)
                        {
                            newIndex = sourceElement.Instances.IndexOf(original);
                        }
                        if (newIndex != -1)
                        {
                            targetElement.Instances.Insert(newIndex + 1, newInstance);
                        }
                        else
                        {
                            targetElement.Instances.Add(newInstance);
                        }
                    }
                    else
                    {
                        targetElement.Instances.Add(newInstance);
                    }
                }
            }

            foreach (var sourceInstance in instancesToCopy)
            {
                ElementSave sourceElement = sourceInstance.ParentContainer;
                var         newInstance   = newInstances.First(item => item.Name == oldNewNameDictionary[sourceInstance.Name]);

                if (targetElement != null)
                {
                    StateSave stateSave = copiedState;
                    StateSave targetState;
                    // We now have to copy over the states
                    if (targetElement != sourceElement)
                    {
                        if (sourceElement.States.Count != 1)
                        {
                            MessageBox.Show("Only the default state variables will be copied since the source and target elements differ.");
                        }

                        targetState = targetElement.DefaultState;
                    }
                    else
                    {
                        targetState = SelectedState.Self.SelectedStateSave ?? SelectedState.Self.SelectedElement.DefaultState;
                    }

                    // why reverse loop?
                    for (int i = stateSave.Variables.Count - 1; i > -1; i--)
                    {
                        // We may have copied over a group of instances.  If so
                        // the copied state may have variables for multiple instances.
                        // We only want to apply the variables that work for the selected
                        // object.
                        VariableSave sourceVariable = stateSave.Variables[i];
                        if (sourceVariable.SourceObject == sourceInstance.Name)
                        {
                            VariableSave copiedVariable = sourceVariable.Clone();
                            copiedVariable.Name = newInstance.Name + "." + copiedVariable.GetRootName();

                            var valueAsString = copiedVariable.Value as string;

                            if (copiedVariable.GetRootName() == "Parent" &&
                                string.IsNullOrWhiteSpace(valueAsString) == false &&
                                oldNewNameDictionary.ContainsKey(valueAsString))
                            {
                                // this is a parent and it may be attached to a copy, so update the value
                                var newValue = oldNewNameDictionary[valueAsString];
                                copiedVariable.Value = newValue;
                            }

                            // We don't want to copy exposed variables.
                            // If we did, the user would have 2 variables exposed with the same.
                            copiedVariable.ExposedAsName = null;

                            targetState.Variables.Add(copiedVariable);
                        }
                    }
                    // Copy over the VariableLists too
                    for (int i = stateSave.VariableLists.Count - 1; i > -1; i--)
                    {
                        VariableListSave sourceVariableList = stateSave.VariableLists[i];
                        if (sourceVariableList.SourceObject == sourceInstance.Name)
                        {
                            VariableListSave copiedList = sourceVariableList.Clone();
                            copiedList.Name = newInstance.Name + "." + copiedList.GetRootName();

                            targetState.VariableLists.Add(copiedList);
                        }
                    }

                    // This used to be done here when we paste, but now we're
                    // going to remove it when the cut happens - just like text
                    // editors.  Undo will handle this if we mess up.
                    // bool shouldSaveSource = false;
                    //if (mIsCtrlXCut)
                    //{
                    //    if (sourceElement.Instances.Contains(sourceInstance))
                    //    {
                    //        // Not sure why we weren't just using
                    //        // ElementCommands here - maybe an oversight?
                    //        // This should improve things like
                    //        //sourceElement.Instances.Remove(sourceInstance);

                    //        ElementCommands.Self.RemoveInstance(sourceInstance, sourceElement);
                    //        shouldSaveSource = true;
                    //    }
                    //}

                    newInstance.ParentContainer = targetElement;
                    // We need to call InstanceAdd before we select the new object - the Undo manager expects it
                    // This includes before other managers refresh
                    PluginManager.Self.InstanceAdd(targetElement, newInstance);
                }
            }


            WireframeObjectManager.Self.RefreshAll(true);
            GumCommands.Self.GuiCommands.RefreshElementTreeView(targetElement);
            GumCommands.Self.FileCommands.TryAutoSaveElement(targetElement);
            SelectedState.Self.SelectedInstances = newInstances;
        }
コード例 #19
0
        private void PastedCopiedInstance(InstanceSave sourceInstance, ElementSave sourceElement, ElementSave targetElement, InstanceSave targetInstance, StateSave copiedState)
        {
            targetInstance.Name = StringFunctions.MakeStringUnique(targetInstance.Name, targetElement.Instances.Select(item => item.Name));

            targetElement.Instances.Add(targetInstance);

            StateSave stateSave = copiedState;
            StateSave targetState;

            // We now have to copy over the states
            if (targetElement != sourceElement)
            {
                if (sourceElement.States.Count != 1)
                {
                    MessageBox.Show("Only the default state variables will be copied since the source and target elements differ.");
                }

                targetState = targetElement.DefaultState;
            }
            else
            {
                targetState = SelectedState.Self.SelectedStateSave;
            }

            // why reverse loop?
            for (int i = stateSave.Variables.Count - 1; i > -1; i--)
            {
                // We may have copied over a group of instances.  If so
                // the copied state may have variables for multiple instances.
                // We only want to apply the variables that work for the selected
                // object.
                VariableSave sourceVariable = stateSave.Variables[i];
                if (sourceVariable.SourceObject == sourceInstance.Name)
                {
                    VariableSave copiedVariable = sourceVariable.Clone();
                    copiedVariable.Name = targetInstance.Name + "." + copiedVariable.GetRootName();

                    // We don't want to copy exposed variables.
                    // If we did, the user would have 2 variables exposed with the same.
                    copiedVariable.ExposedAsName = null;

                    targetState.Variables.Add(copiedVariable);
                }
            }
            // Copy over the VariableLists too
            for (int i = stateSave.VariableLists.Count - 1; i > -1; i--)
            {
                VariableListSave sourceVariableList = stateSave.VariableLists[i];
                if (sourceVariableList.SourceObject == sourceInstance.Name)
                {
                    VariableListSave copiedList = sourceVariableList.Clone();
                    copiedList.Name = targetInstance.Name + "." + copiedList.GetRootName();

                    targetState.VariableLists.Add(copiedList);
                }
            }

            // This used to be done here when we paste, but now we're
            // going to remove it when the cut happens - just like text
            // editors.  Undo will handle this if we mess up.
            // bool shouldSaveSource = false;
            //if (mIsCtrlXCut)
            //{
            //    if (sourceElement.Instances.Contains(sourceInstance))
            //    {
            //        // Not sure why we weren't just using
            //        // ElementCommands here - maybe an oversight?
            //        // This should improve things like
            //        //sourceElement.Instances.Remove(sourceInstance);

            //        ElementCommands.Self.RemoveInstance(sourceInstance, sourceElement);
            //        shouldSaveSource = true;
            //    }
            //}

            targetInstance.ParentContainer = targetElement;
            // We need to call InstanceAdd before we select the new object - the Undo manager expects it
            // This includes before other managers refresh
            PluginManager.Self.InstanceAdd(targetElement, targetInstance);
            WireframeObjectManager.Self.RefreshAll(true);



            GumCommands.Self.GuiCommands.RefreshElementTreeView(targetElement);


            SelectedState.Self.SelectedInstance = targetInstance;

            GumCommands.Self.FileCommands.TryAutoSaveElement(targetElement);
        }
コード例 #20
0
        private bool TryHandleCustomSetter(VariableSave variable, ElementSave elementSave, ICodeBlock setter)
        {
            if (variable.GetRootName() == "SourceFile" && elementSave.Name == "NineSlice")
            {
                //setter.Line("bool usePattern = RenderingLibrary.Graphics.NineSlice.GetIfShouldUsePattern(value);");

                //var ifBlock = setter.If("usePattern");
                //{
                //    ifBlock.Line("this.SetTexturesUsingPattern(value, null);");
                //}
                //var elseBlock = ifBlock.End().Else();
                //{
                //    var internalIf = elseBlock.If("if (!string.IsNullOrEmpty(value))");
                //    {
                //        internalIf.Line("this.SetSingleTexture(RenderingLibrary.Content.LoaderManager.Self.Load(value, RenderingLibrary.SystemManagers.Default));");
                //    }

                //}

                //return true;


                setter.Line("ContainedNineSlice.SetSingleTexture(value);");


                return(true);
            }



            if (elementSave.Name == "Circle" || elementSave.Name == "Rectangle")
            {
                string containedObject;

                if (elementSave.Name == "Circle")
                {
                    containedObject = "ContainedCircle";
                }
                else
                {
                    containedObject = "ContainedRectangle";
                }

                string colorComponent = null;

                if (variable.Name == "Alpha")
                {
                    colorComponent = "A";
                }
                else if (variable.Name == "Red")
                {
                    colorComponent = "R";
                }
                else if (variable.Name == "Green")
                {
                    colorComponent = "G";
                }
                else if (variable.Name == "Blue")
                {
                    colorComponent = "B";
                }

                if (!string.IsNullOrEmpty(colorComponent))
                {
                    setter.Line($"var color = {containedObject}.Color;");
                    setter.Line($"color.{colorComponent} = (byte)value;");
                    setter.Line($"{containedObject}.Color = color;");
                    return(true);
                }
            }

            return(false);
        }