private void SanitizeProperty() { sanitizedProperty = NewProperties switch { float f => f.FormatSingleDecimal(), Point point => XMLExtensions.PointToString(point), Vector2 vector2 => vector2.FormatZeroDecimal(), Vector3 vector3 => vector3.FormatSingleDecimal(), Vector4 vector4 => vector4.FormatSingleDecimal(), Color color => XMLExtensions.ColorToString(color), Rectangle rectangle => XMLExtensions.RectToString(rectangle), _ => NewProperties.ToString() }; }
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); }
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); } }
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); } }; }