Esempio n. 1
0
 public static XElement SerializeImage(EditorImageContainer image)
 {
     return(new XElement("image",
                         new XAttribute("pos", XMLExtensions.Vector2ToString(image.Position)),
                         new XAttribute("rotation", image.Rotation),
                         new XAttribute("opacity", image.Opacity),
                         new XAttribute("path", image.Path),
                         new XAttribute("scale", image.Scale),
                         new XAttribute("drawtarget", image.DrawTarget.ToString())));
 }
Esempio n. 2
0
        public void SaveToXElement(XElement element)
        {
            element.Add(new XAttribute("name", name));
            element.Add(new XAttribute("description", Description ?? ""));
            element.Add(new XAttribute("tags", tags.ToString()));

            Rectangle dimensions = CalculateDimensions();

            element.Add(new XAttribute("dimensions", XMLExtensions.Vector2ToString(dimensions.Size.ToVector2())));
            element.Add(new XAttribute("recommendedcrewsizemin", RecommendedCrewSizeMin));
            element.Add(new XAttribute("recommendedcrewsizemax", RecommendedCrewSizeMax));
            element.Add(new XAttribute("recommendedcrewexperience", RecommendedCrewExperience ?? ""));
            element.Add(new XAttribute("compatiblecontentpackages", string.Join(", ", CompatibleContentPackages)));

            foreach (MapEntity e in MapEntity.mapEntityList)
            {
                if (e.linkedTo == null)
                {
                    continue;
                }
                for (int i = e.linkedTo.Count - 1; i >= 0; i--)
                {
                    if (!e.linkedTo[i].ShouldBeSaved)
                    {
                        e.linkedTo.RemoveAt(i);
                    }
                }
            }

            foreach (MapEntity e in MapEntity.mapEntityList)
            {
                if (e.MoveWithLevel || e.Submarine != this || !e.ShouldBeSaved)
                {
                    continue;
                }
                e.Save(element);
            }
        }
Esempio n. 3
0
        private bool SaveSprites(IEnumerable <Sprite> sprites)
        {
            if (selectedTexture == null)
            {
                return(false);
            }
            if (sprites.None())
            {
                return(false);
            }
            HashSet <XDocument> docsToSave = new HashSet <XDocument>();

            foreach (Sprite sprite in sprites)
            {
                if (sprite.Texture != selectedTexture)
                {
                    continue;
                }
                var element = sprite.SourceElement;
                if (element == null)
                {
                    continue;
                }
                element.SetAttributeValue("sourcerect", XMLExtensions.RectToString(sprite.SourceRect));
                element.SetAttributeValue("origin", XMLExtensions.Vector2ToString(sprite.RelativeOrigin));
                docsToSave.Add(element.Document);
            }
            xmlPathText.Text = "All changes saved to:";
            foreach (XDocument doc in docsToSave)
            {
                string xmlPath = doc.ParseContentPathFromUri();
                xmlPathText.Text += "\n" + xmlPath;
                doc.Save(xmlPath);
            }
            xmlPathText.TextColor = Color.LightGreen;
            return(true);
        }
Esempio n. 4
0
        public override XElement Save(XElement parentElement)
        {
            XElement saveElement = null;

            if (sub == null)
            {
                if (this.saveElement == null)
                {
                    var doc = SubmarineInfo.OpenFile(filePath);
                    saveElement      = doc.Root;
                    saveElement.Name = "LinkedSubmarine";
                    saveElement.Add(new XAttribute("filepath", filePath));
                }
                else
                {
                    saveElement = this.saveElement;
                }

                if (saveElement.Attribute("pos") != null)
                {
                    saveElement.Attribute("pos").Remove();
                }
                saveElement.Add(new XAttribute("pos", XMLExtensions.Vector2ToString(Position - Submarine.HiddenSubPosition)));

                var linkedPort = linkedTo.FirstOrDefault(lt => (lt is Item) && ((Item)lt).GetComponent <DockingPort>() != null);
                if (linkedPort != null)
                {
                    saveElement.Attribute("linkedto")?.Remove();
                    saveElement.Add(new XAttribute("linkedto", linkedPort.ID));
                }
            }
            else
            {
                saveElement = new XElement("LinkedSubmarine");
                sub.SaveToXElement(saveElement);
            }

            saveElement.Attribute("originallinkedto")?.Remove();
            saveElement.Add(new XAttribute("originallinkedto", originalLinkedPort != null ? originalLinkedPort.Item.ID : originalLinkedToID));
            saveElement.Attribute("originalmyport")?.Remove();
            saveElement.Add(new XAttribute("originalmyport", originalMyPortID));

            if (sub != null)
            {
                bool leaveBehind = false;
                if (!sub.DockedTo.Contains(Submarine.MainSub))
                {
                    System.Diagnostics.Debug.Assert(Submarine.MainSub.AtEndPosition || Submarine.MainSub.AtStartPosition);
                    if (Submarine.MainSub.AtEndPosition)
                    {
                        leaveBehind = sub.AtEndPosition != Submarine.MainSub.AtEndPosition;
                    }
                    else
                    {
                        leaveBehind = sub.AtStartPosition != Submarine.MainSub.AtStartPosition;
                    }
                }

                if (leaveBehind)
                {
                    saveElement.SetAttributeValue("location", Level.Loaded.Seed);
                    saveElement.SetAttributeValue("worldpos", XMLExtensions.Vector2ToString(sub.SubBody.Position));
                }
                else
                {
                    if (saveElement.Attribute("location") != null)
                    {
                        saveElement.Attribute("location").Remove();
                    }
                    if (saveElement.Attribute("worldpos") != null)
                    {
                        saveElement.Attribute("worldpos").Remove();
                    }
                }
                saveElement.SetAttributeValue("pos", XMLExtensions.Vector2ToString(Position - Submarine.HiddenSubPosition));
            }

            parentElement.Add(saveElement);

            return(saveElement);
        }
        public static void SerializeProperties(ISerializableEntity obj, XElement element, bool saveIfDefault = false)
        {
            var saveProperties = GetProperties <Serialize>(obj);

            foreach (var property in saveProperties)
            {
                object value = property.GetValue();
                if (value == null)
                {
                    continue;
                }

                if (!saveIfDefault)
                {
                    //only save
                    //  - if the attribute is saveable and it's different from the default value
                    //  - or can be changed in-game or in the editor
                    bool save = false;
                    foreach (var attribute in property.Attributes.OfType <Serialize>())
                    {
                        if ((attribute.isSaveable && !attribute.defaultValue.Equals(value)) ||
                            property.Attributes.OfType <Editable>().Any())
                        {
                            save = true;
                            break;
                        }
                    }

                    if (!save)
                    {
                        continue;
                    }
                }

                string stringValue;
                string typeName;
                if (!supportedTypes.TryGetValue(value.GetType(), out typeName))
                {
                    if (property.PropertyType.IsEnum)
                    {
                        stringValue = value.ToString();
                    }
                    else
                    {
                        DebugConsole.ThrowError("Failed to serialize the property \"" + property.Name + "\" of \"" + obj + "\" (type " + property.PropertyType + " not supported)");
                        continue;
                    }
                }
                else
                {
                    switch (typeName)
                    {
                    case "float":
                        //make sure the decimal point isn't converted to a comma or anything else
                        stringValue = ((float)value).ToString("G", CultureInfo.InvariantCulture);
                        break;

                    case "vector2":
                        stringValue = XMLExtensions.Vector2ToString((Vector2)value);
                        break;

                    case "vector3":
                        stringValue = XMLExtensions.Vector3ToString((Vector3)value);
                        break;

                    case "vector4":
                        stringValue = XMLExtensions.Vector4ToString((Vector4)value);
                        break;

                    case "color":
                        stringValue = XMLExtensions.ColorToString((Color)value);
                        break;

                    case "rectangle":
                        stringValue = XMLExtensions.RectToString((Rectangle)value);
                        break;

                    default:
                        stringValue = value.ToString();
                        break;
                    }
                }

                element.SetAttributeValue(property.Name.ToLowerInvariant(), stringValue);
            }
        }
Esempio n. 6
0
        private void CreateGUIElements()
        {
            topPanel = new GUIFrame(new RectTransform(new Vector2(1.0f, 0.1f), Frame.RectTransform)
            {
                MinSize = new Point(0, 60)
            }, "GUIFrameTop");
            topPanelContents = new GUIFrame(new RectTransform(new Vector2(0.95f, 0.8f), topPanel.RectTransform, Anchor.Center), style: null);

            new GUIButton(new RectTransform(new Vector2(0.12f, 0.4f), topPanelContents.RectTransform, Anchor.TopLeft)
            {
                RelativeOffset = new Vector2(0, 0.1f)
            }, "Reload Texture")
            {
                OnClicked = (button, userData) =>
                {
                    if (!(textureList.SelectedData is Texture2D selectedTexture))
                    {
                        return(false);
                    }
                    var    selected      = selectedSprites;
                    Sprite firstSelected = selected.First();
                    selected.ForEach(s => s.ReloadTexture());
                    RefreshLists();
                    textureList.Select(firstSelected.Texture, autoScroll: false);
                    selected.ForEachMod(s => spriteList.Select(s, autoScroll: false));
                    texturePathText.Text      = "Textures reloaded from " + firstSelected.FilePath;
                    texturePathText.TextColor = Color.LightGreen;
                    return(true);
                }
            };
            new GUIButton(new RectTransform(new Vector2(0.12f, 0.4f), topPanelContents.RectTransform, Anchor.BottomLeft)
            {
                RelativeOffset = new Vector2(0, 0.1f)
            }, "Reset Changes")
            {
                OnClicked = (button, userData) =>
                {
                    if (selectedTexture == null)
                    {
                        return(false);
                    }
                    foreach (Sprite sprite in loadedSprites)
                    {
                        if (sprite.Texture != selectedTexture)
                        {
                            continue;
                        }
                        var element = sprite.SourceElement;
                        if (element == null)
                        {
                            continue;
                        }
                        // Not all sprites have a sourcerect defined, in which case we'll want to use the current source rect instead of an empty rect.
                        sprite.SourceRect     = element.GetAttributeRect("sourcerect", sprite.SourceRect);
                        sprite.RelativeOrigin = element.GetAttributeVector2("origin", new Vector2(0.5f, 0.5f));
                    }
                    ResetWidgets();
                    xmlPathText.Text      = "Changes successfully reset";
                    xmlPathText.TextColor = Color.LightGreen;
                    return(true);
                }
            };
            new GUIButton(new RectTransform(new Vector2(0.12f, 0.4f), topPanelContents.RectTransform, Anchor.TopLeft)
            {
                RelativeOffset = new Vector2(0.15f, 0.1f)
            }, "Save Selected Sprites")
            {
                OnClicked = (button, userData) =>
                {
                    if (selectedSprites.None())
                    {
                        return(false);
                    }
                    foreach (var sprite in selectedSprites)
                    {
                        var element = sprite.SourceElement;
                        if (element == null)
                        {
                            xmlPathText.Text      = "No xml element defined for the sprite";
                            xmlPathText.TextColor = Color.Red;
                            return(false);
                        }
                        element.SetAttributeValue("sourcerect", XMLExtensions.RectToString(sprite.SourceRect));
                        element.SetAttributeValue("origin", XMLExtensions.Vector2ToString(sprite.RelativeOrigin));
                    }
                    var      firstSprite = selectedSprites.First();
                    XElement e           = firstSprite.SourceElement;
                    var      d           = XMLExtensions.TryLoadXml(xmlPath);
                    if (d == null || d.BaseUri != e.Document.BaseUri)
                    {
                        xmlPathText.Text      = "Failed to save to " + xmlPath;
                        xmlPathText.TextColor = Color.Red;
                        return(false);
                    }
                    e.Document.Save(xmlPath);
                    xmlPathText.Text      = "Selected sprites saved to " + xmlPath;
                    xmlPathText.TextColor = Color.LightGreen;
                    return(true);
                }
            };
            new GUIButton(new RectTransform(new Vector2(0.12f, 0.4f), topPanelContents.RectTransform, Anchor.BottomLeft)
            {
                RelativeOffset = new Vector2(0.15f, 0.1f)
            }, "Save All Sprites")
            {
                OnClicked = (button, userData) =>
                {
                    if (selectedTexture == null)
                    {
                        return(false);
                    }
                    XDocument doc = null;
                    foreach (Sprite sprite in loadedSprites)
                    {
                        if (sprite.Texture != selectedTexture)
                        {
                            continue;
                        }
                        var element = sprite.SourceElement;
                        if (element == null)
                        {
                            continue;
                        }
                        element.SetAttributeValue("sourcerect", XMLExtensions.RectToString(sprite.SourceRect));
                        element.SetAttributeValue("origin", XMLExtensions.Vector2ToString(sprite.RelativeOrigin));
                        doc = element.Document;
                    }
                    if (doc != null)
                    {
                        var d = XMLExtensions.TryLoadXml(xmlPath);
                        if (d == null || d.BaseUri != doc.BaseUri)
                        {
                            xmlPathText.Text      = "Failed to save to " + xmlPath;
                            xmlPathText.TextColor = Color.Red;
                            return(false);
                        }
                        doc.Save(xmlPath);
                        xmlPathText.Text      = "All changes saved to " + xmlPath;
                        xmlPathText.TextColor = Color.LightGreen;
                        return(true);
                    }
                    else
                    {
                        xmlPathText.Text      = "Failed to save to " + xmlPath;
                        xmlPathText.TextColor = Color.Red;
                        return(false);
                    }
                }
            };
            new GUITextBlock(new RectTransform(new Vector2(0.2f, 0.2f), topPanelContents.RectTransform, Anchor.TopCenter, Pivot.CenterRight)
            {
                RelativeOffset = new Vector2(0, 0.3f)
            }, "Zoom: ");
            zoomBar = new GUIScrollBar(new RectTransform(new Vector2(0.2f, 0.35f), topPanelContents.RectTransform, Anchor.TopCenter, Pivot.CenterRight)
            {
                RelativeOffset = new Vector2(0.05f, 0.3f)
            }, barSize: 0.1f)
            {
                BarScroll = GetBarScrollValue(),
                Step      = 0.01f,
                OnMoved   = (scrollBar, value) =>
                {
                    zoom = MathHelper.Lerp(minZoom, maxZoom, value);
                    return(true);
                }
            };
            new GUIButton(new RectTransform(new Vector2(0.05f, 0.35f), topPanelContents.RectTransform, Anchor.TopCenter, Pivot.CenterLeft)
            {
                RelativeOffset = new Vector2(0.055f, 0.3f)
            }, "Reset Zoom")
            {
                OnClicked = (box, data) =>
                {
                    ResetZoom();
                    return(true);
                }
            };

            texturePathText = new GUITextBlock(new RectTransform(new Vector2(0.5f, 0.4f), topPanelContents.RectTransform, Anchor.Center, Pivot.BottomCenter)
            {
                RelativeOffset = new Vector2(0.4f, 0)
            }, "", Color.LightGray);
            xmlPathText = new GUITextBlock(new RectTransform(new Vector2(0.5f, 0.4f), topPanelContents.RectTransform, Anchor.Center, Pivot.TopCenter)
            {
                RelativeOffset = new Vector2(0.4f, 0)
            }, "", Color.LightGray);

            leftPanel = new GUIFrame(new RectTransform(new Vector2(0.25f, 1.0f - topPanel.RectTransform.RelativeSize.Y), Frame.RectTransform, Anchor.BottomLeft)
            {
                MinSize = new Point(150, 0)
            }, style: "GUIFrameLeft");
            var paddedLeftPanel = new GUILayoutGroup(new RectTransform(new Vector2(0.9f, 0.95f), leftPanel.RectTransform, Anchor.CenterLeft)
            {
                RelativeOffset = new Vector2(0.02f, 0.0f)
            })
            {
                Stretch = true
            };

            textureList = new GUIListBox(new RectTransform(new Vector2(1.0f, 1.0f), paddedLeftPanel.RectTransform))
            {
                OnSelected = (listBox, userData) =>
                {
                    var previousTexture = selectedTexture;
                    selectedTexture = userData as Texture2D;
                    if (previousTexture != selectedTexture)
                    {
                        ResetZoom();
                    }
                    foreach (GUIComponent child in spriteList.Content.Children)
                    {
                        var textBlock = (GUITextBlock)child;
                        var sprite    = (Sprite)textBlock.UserData;
                        textBlock.TextColor = new Color(textBlock.TextColor, sprite.Texture == selectedTexture ? 1.0f : 0.4f);
                    }
                    if (selectedSprites.None(s => s.Texture == selectedTexture))
                    {
                        spriteList.Select(loadedSprites.First(s => s.Texture == selectedTexture), false);
                        UpdateScrollBar(spriteList);
                    }
                    var firstSprite = selectedSprites.First();
                    texturePathText.Text      = firstSprite.FilePath;
                    texturePathText.TextColor = Color.LightGray;
                    var element = firstSprite.SourceElement;
                    if (element == null)
                    {
                        xmlPathText.Text = string.Empty;
                    }
                    else
                    {
                        xmlPath               = element.ParseContentPathFromUri();
                        xmlPathText.Text      = xmlPath;
                        xmlPathText.TextColor = Color.LightGray;
                    }
                    topPanelContents.Visible = true;
                    return(true);
                }
            };

            rightPanel = new GUIFrame(new RectTransform(new Vector2(0.25f, 1.0f - topPanel.RectTransform.RelativeSize.Y), Frame.RectTransform, Anchor.BottomRight)
            {
                MinSize = new Point(150, 0)
            },
                                      style: "GUIFrameRight");
            var paddedRightPanel = new GUILayoutGroup(new RectTransform(new Vector2(0.95f, 0.95f), rightPanel.RectTransform, Anchor.Center)
            {
                RelativeOffset = new Vector2(0.02f, 0.0f)
            })
            {
                Stretch         = true,
                RelativeSpacing = 0.01f
            };

            spriteList = new GUIListBox(new RectTransform(new Vector2(1.0f, 1.0f), paddedRightPanel.RectTransform))
            {
                OnSelected = (listBox, userData) =>
                {
                    Sprite sprite = userData as Sprite;
                    if (sprite == null)
                    {
                        return(false);
                    }
                    if (selectedSprites.Any(s => s.Texture != selectedTexture))
                    {
                        ResetWidgets();
                    }
                    if (Widget.EnableMultiSelect)
                    {
                        if (selectedSprites.Contains(sprite))
                        {
                            selectedSprites.Remove(sprite);
                        }
                        else
                        {
                            selectedSprites.Add(sprite);
                            dirtySprites.Add(sprite);
                        }
                    }
                    else
                    {
                        selectedSprites.Clear();
                        selectedSprites.Add(sprite);
                        dirtySprites.Add(sprite);
                    }
                    if (selectedTexture != sprite.Texture)
                    {
                        textureList.Select(sprite.Texture, autoScroll: false);
                        UpdateScrollBar(textureList);
                    }
                    return(true);
                }
            };
        }