From 26264bf5767811bdfd6e2bbdf2b19634bf11d787 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 21 Mar 2020 00:49:43 +0100 Subject: [PATCH] allow a lot of mem classes to have custom data added to them --- .../Tiled/IndividualTiledMapRenderer.cs | 5 ++-- MLEM.Extended/Tiled/TiledMapCollisions.cs | 5 ++-- MLEM.Ui/Elements/Element.cs | 2 +- MLEM.Ui/Style/UiStyle.cs | 3 +- MLEM/Animations/SpriteAnimation.cs | 3 +- MLEM/Animations/SpriteAnimationGroup.cs | 3 +- MLEM/Formatting/FormatSettings.cs | 3 +- MLEM/Formatting/FormattingCodeCollection.cs | 3 +- MLEM/Misc/GenericDataHolder.cs | 29 +++++++++++++++++++ MLEM/Textures/NinePatch.cs | 2 +- MLEM/Textures/TextureRegion.cs | 2 +- MLEM/Textures/UniformTextureAtlas.cs | 3 +- Sandbox/GameImpl.cs | 9 ++++-- 13 files changed, 56 insertions(+), 16 deletions(-) create mode 100644 MLEM/Misc/GenericDataHolder.cs diff --git a/MLEM.Extended/Tiled/IndividualTiledMapRenderer.cs b/MLEM.Extended/Tiled/IndividualTiledMapRenderer.cs index f143a0b..0bce8d7 100644 --- a/MLEM.Extended/Tiled/IndividualTiledMapRenderer.cs +++ b/MLEM.Extended/Tiled/IndividualTiledMapRenderer.cs @@ -3,8 +3,9 @@ using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using MLEM.Extensions; -using MonoGame.Extended; +using MLEM.Misc; using MonoGame.Extended.Tiled; +using RectangleF = MonoGame.Extended.RectangleF; namespace MLEM.Extended.Tiled { public class IndividualTiledMapRenderer { @@ -84,7 +85,7 @@ namespace MLEM.Extended.Tiled { public delegate float GetDepth(TiledMapTile tile, TiledMapTileLayer layer, int layerIndex, Point position); - private class TileDrawInfo { + private class TileDrawInfo : GenericDataHolder { private readonly IndividualTiledMapRenderer renderer; private readonly TiledMapTileset tileset; diff --git a/MLEM.Extended/Tiled/TiledMapCollisions.cs b/MLEM.Extended/Tiled/TiledMapCollisions.cs index 09fa655..55461ec 100644 --- a/MLEM.Extended/Tiled/TiledMapCollisions.cs +++ b/MLEM.Extended/Tiled/TiledMapCollisions.cs @@ -4,8 +4,9 @@ using System.Collections.ObjectModel; using System.Linq; using Microsoft.Xna.Framework; using MLEM.Extensions; -using MonoGame.Extended; +using MLEM.Misc; using MonoGame.Extended.Tiled; +using RectangleF = MonoGame.Extended.RectangleF; namespace MLEM.Extended.Tiled { public class TiledMapCollisions { @@ -79,7 +80,7 @@ namespace MLEM.Extended.Tiled { public delegate void CollectCollisions(List collisions, TileCollisionInfo tile); - public class TileCollisionInfo { + public class TileCollisionInfo : GenericDataHolder { public readonly TiledMap Map; public readonly Vector2 Position; diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index f48fd4a..2317261 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -13,7 +13,7 @@ using MLEM.Textures; using MLEM.Ui.Style; namespace MLEM.Ui.Elements { - public abstract class Element { + public abstract class Element : GenericDataHolder { protected readonly List Children = new List(); private readonly List sortedChildren = new List(); diff --git a/MLEM.Ui/Style/UiStyle.cs b/MLEM.Ui/Style/UiStyle.cs index 5f0e440..f261fff 100644 --- a/MLEM.Ui/Style/UiStyle.cs +++ b/MLEM.Ui/Style/UiStyle.cs @@ -2,10 +2,11 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using MLEM.Font; using MLEM.Formatting; +using MLEM.Misc; using MLEM.Textures; namespace MLEM.Ui.Style { - public class UiStyle { + public class UiStyle : GenericDataHolder { public NinePatch SelectionIndicator; public NinePatch ButtonTexture; diff --git a/MLEM/Animations/SpriteAnimation.cs b/MLEM/Animations/SpriteAnimation.cs index 615fadc..405ea80 100644 --- a/MLEM/Animations/SpriteAnimation.cs +++ b/MLEM/Animations/SpriteAnimation.cs @@ -1,10 +1,11 @@ using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using MLEM.Misc; using MLEM.Textures; namespace MLEM.Animations { - public class SpriteAnimation { + public class SpriteAnimation : GenericDataHolder { private AnimationFrame[] frames; public AnimationFrame this[int index] => this.frames[index]; diff --git a/MLEM/Animations/SpriteAnimationGroup.cs b/MLEM/Animations/SpriteAnimationGroup.cs index 082345d..53140c0 100644 --- a/MLEM/Animations/SpriteAnimationGroup.cs +++ b/MLEM/Animations/SpriteAnimationGroup.cs @@ -1,10 +1,11 @@ using System; using System.Collections.Generic; using Microsoft.Xna.Framework; +using MLEM.Misc; using MLEM.Textures; namespace MLEM.Animations { - public class SpriteAnimationGroup { + public class SpriteAnimationGroup : GenericDataHolder{ private readonly List animations = new List(); private ConditionedAnimation currAnimation; diff --git a/MLEM/Formatting/FormatSettings.cs b/MLEM/Formatting/FormatSettings.cs index 96db82a..aae442b 100644 --- a/MLEM/Formatting/FormatSettings.cs +++ b/MLEM/Formatting/FormatSettings.cs @@ -1,7 +1,8 @@ using Microsoft.Xna.Framework; +using MLEM.Misc; namespace MLEM.Formatting { - public class FormatSettings { + public class FormatSettings : GenericDataHolder { public static readonly FormatSettings Default = new FormatSettings(); diff --git a/MLEM/Formatting/FormattingCodeCollection.cs b/MLEM/Formatting/FormattingCodeCollection.cs index af3380b..e6a6fea 100644 --- a/MLEM/Formatting/FormattingCodeCollection.cs +++ b/MLEM/Formatting/FormattingCodeCollection.cs @@ -1,12 +1,13 @@ using System.Collections.Generic; using System.Text.RegularExpressions; +using MLEM.Misc; namespace MLEM.Formatting { public class FormattingCodeCollection : Dictionary> { } - public class FormattingCodeData { + public class FormattingCodeData : GenericDataHolder { public readonly FormattingCode Code; public readonly Match Match; diff --git a/MLEM/Misc/GenericDataHolder.cs b/MLEM/Misc/GenericDataHolder.cs new file mode 100644 index 0000000..d9660bd --- /dev/null +++ b/MLEM/Misc/GenericDataHolder.cs @@ -0,0 +1,29 @@ +using System.Collections.Generic; + +namespace MLEM.Misc { + public class GenericDataHolder { + + private Dictionary data; + + public void SetData(string key, object data) { + if (this.data == null) + this.data = new Dictionary(); + if (data == default) { + this.data.Remove(key); + } else { + this.data[key] = data; + } + } + + public T GetData(string key) { + if (this.data != null && this.data.TryGetValue(key, out var val) && val is T t) + return t; + return default; + } + + public IReadOnlyCollection GetDataKeys() { + return this.data.Keys; + } + + } +} \ No newline at end of file diff --git a/MLEM/Textures/NinePatch.cs b/MLEM/Textures/NinePatch.cs index dcb15fb..0b8e6e6 100644 --- a/MLEM/Textures/NinePatch.cs +++ b/MLEM/Textures/NinePatch.cs @@ -6,7 +6,7 @@ using MLEM.Extensions; using MLEM.Misc; namespace MLEM.Textures { - public class NinePatch { + public class NinePatch : GenericDataHolder { public readonly TextureRegion Region; public readonly Padding Padding; diff --git a/MLEM/Textures/TextureRegion.cs b/MLEM/Textures/TextureRegion.cs index d99e2a4..7f976aa 100644 --- a/MLEM/Textures/TextureRegion.cs +++ b/MLEM/Textures/TextureRegion.cs @@ -4,7 +4,7 @@ using MLEM.Extensions; using MLEM.Misc; namespace MLEM.Textures { - public class TextureRegion { + public class TextureRegion : GenericDataHolder { public readonly Texture2D Texture; public readonly Rectangle Area; diff --git a/MLEM/Textures/UniformTextureAtlas.cs b/MLEM/Textures/UniformTextureAtlas.cs index d2e0643..8998b83 100644 --- a/MLEM/Textures/UniformTextureAtlas.cs +++ b/MLEM/Textures/UniformTextureAtlas.cs @@ -2,9 +2,10 @@ using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using MLEM.Misc; namespace MLEM.Textures { - public class UniformTextureAtlas { + public class UniformTextureAtlas : GenericDataHolder { public readonly Texture2D Texture; public readonly int RegionAmountX; diff --git a/Sandbox/GameImpl.cs b/Sandbox/GameImpl.cs index e90a64c..00df559 100644 --- a/Sandbox/GameImpl.cs +++ b/Sandbox/GameImpl.cs @@ -50,7 +50,7 @@ namespace Sandbox { MaxScale = 4 }; - /*var tex = LoadContent("Textures/Test"); + var tex = LoadContent("Textures/Test"); this.UiSystem.Style = new UntexturedStyle(this.SpriteBatch) { Font = new GenericSpriteFont(LoadContent("Fonts/TestFont")), TextScale = 0.1F, @@ -64,9 +64,12 @@ namespace Sandbox { var panel = new Panel(Anchor.Center, new Vector2(0, 100), Vector2.Zero) {SetWidthBasedOnChildren = true}; panel.AddChild(new Button(Anchor.AutoLeft, new Vector2(100, 10))); panel.AddChild(new Button(Anchor.AutoCenter, new Vector2(80, 10))); - this.UiSystem.Add("Panel", panel);*/ + this.UiSystem.Add("Panel", panel); - var obj = new Test{ + panel.SetData("TestKey", new Vector2(10, 2)); + Console.WriteLine(panel.GetData("TestKey")); + + var obj = new Test { Vec = new Vector2(10, 20), Point = new Point(20, 30), Rectangle = new Rectangle(1, 2, 3, 4),