1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 06:28:35 +01:00

allow a lot of mem classes to have custom data added to them

This commit is contained in:
Ellpeck 2020-03-21 00:49:43 +01:00
parent 9f43924e47
commit 26264bf576
13 changed files with 56 additions and 16 deletions

View file

@ -3,8 +3,9 @@ using System.Collections.Generic;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions; using MLEM.Extensions;
using MonoGame.Extended; using MLEM.Misc;
using MonoGame.Extended.Tiled; using MonoGame.Extended.Tiled;
using RectangleF = MonoGame.Extended.RectangleF;
namespace MLEM.Extended.Tiled { namespace MLEM.Extended.Tiled {
public class IndividualTiledMapRenderer { public class IndividualTiledMapRenderer {
@ -84,7 +85,7 @@ namespace MLEM.Extended.Tiled {
public delegate float GetDepth(TiledMapTile tile, TiledMapTileLayer layer, int layerIndex, Point position); 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 IndividualTiledMapRenderer renderer;
private readonly TiledMapTileset tileset; private readonly TiledMapTileset tileset;

View file

@ -4,8 +4,9 @@ using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using MLEM.Extensions; using MLEM.Extensions;
using MonoGame.Extended; using MLEM.Misc;
using MonoGame.Extended.Tiled; using MonoGame.Extended.Tiled;
using RectangleF = MonoGame.Extended.RectangleF;
namespace MLEM.Extended.Tiled { namespace MLEM.Extended.Tiled {
public class TiledMapCollisions { public class TiledMapCollisions {
@ -79,7 +80,7 @@ namespace MLEM.Extended.Tiled {
public delegate void CollectCollisions(List<RectangleF> collisions, TileCollisionInfo tile); public delegate void CollectCollisions(List<RectangleF> collisions, TileCollisionInfo tile);
public class TileCollisionInfo { public class TileCollisionInfo : GenericDataHolder {
public readonly TiledMap Map; public readonly TiledMap Map;
public readonly Vector2 Position; public readonly Vector2 Position;

View file

@ -13,7 +13,7 @@ using MLEM.Textures;
using MLEM.Ui.Style; using MLEM.Ui.Style;
namespace MLEM.Ui.Elements { namespace MLEM.Ui.Elements {
public abstract class Element { public abstract class Element : GenericDataHolder {
protected readonly List<Element> Children = new List<Element>(); protected readonly List<Element> Children = new List<Element>();
private readonly List<Element> sortedChildren = new List<Element>(); private readonly List<Element> sortedChildren = new List<Element>();

View file

@ -2,10 +2,11 @@ using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Audio;
using MLEM.Font; using MLEM.Font;
using MLEM.Formatting; using MLEM.Formatting;
using MLEM.Misc;
using MLEM.Textures; using MLEM.Textures;
namespace MLEM.Ui.Style { namespace MLEM.Ui.Style {
public class UiStyle { public class UiStyle : GenericDataHolder {
public NinePatch SelectionIndicator; public NinePatch SelectionIndicator;
public NinePatch ButtonTexture; public NinePatch ButtonTexture;

View file

@ -1,10 +1,11 @@
using System; using System;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using MLEM.Misc;
using MLEM.Textures; using MLEM.Textures;
namespace MLEM.Animations { namespace MLEM.Animations {
public class SpriteAnimation { public class SpriteAnimation : GenericDataHolder {
private AnimationFrame[] frames; private AnimationFrame[] frames;
public AnimationFrame this[int index] => this.frames[index]; public AnimationFrame this[int index] => this.frames[index];

View file

@ -1,10 +1,11 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using MLEM.Misc;
using MLEM.Textures; using MLEM.Textures;
namespace MLEM.Animations { namespace MLEM.Animations {
public class SpriteAnimationGroup { public class SpriteAnimationGroup : GenericDataHolder{
private readonly List<ConditionedAnimation> animations = new List<ConditionedAnimation>(); private readonly List<ConditionedAnimation> animations = new List<ConditionedAnimation>();
private ConditionedAnimation currAnimation; private ConditionedAnimation currAnimation;

View file

@ -1,7 +1,8 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using MLEM.Misc;
namespace MLEM.Formatting { namespace MLEM.Formatting {
public class FormatSettings { public class FormatSettings : GenericDataHolder {
public static readonly FormatSettings Default = new FormatSettings(); public static readonly FormatSettings Default = new FormatSettings();

View file

@ -1,12 +1,13 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using MLEM.Misc;
namespace MLEM.Formatting { namespace MLEM.Formatting {
public class FormattingCodeCollection : Dictionary<int, List<FormattingCodeData>> { public class FormattingCodeCollection : Dictionary<int, List<FormattingCodeData>> {
} }
public class FormattingCodeData { public class FormattingCodeData : GenericDataHolder {
public readonly FormattingCode Code; public readonly FormattingCode Code;
public readonly Match Match; public readonly Match Match;

View file

@ -0,0 +1,29 @@
using System.Collections.Generic;
namespace MLEM.Misc {
public class GenericDataHolder {
private Dictionary<string, object> data;
public void SetData(string key, object data) {
if (this.data == null)
this.data = new Dictionary<string, object>();
if (data == default) {
this.data.Remove(key);
} else {
this.data[key] = data;
}
}
public T GetData<T>(string key) {
if (this.data != null && this.data.TryGetValue(key, out var val) && val is T t)
return t;
return default;
}
public IReadOnlyCollection<string> GetDataKeys() {
return this.data.Keys;
}
}
}

View file

@ -6,7 +6,7 @@ using MLEM.Extensions;
using MLEM.Misc; using MLEM.Misc;
namespace MLEM.Textures { namespace MLEM.Textures {
public class NinePatch { public class NinePatch : GenericDataHolder {
public readonly TextureRegion Region; public readonly TextureRegion Region;
public readonly Padding Padding; public readonly Padding Padding;

View file

@ -4,7 +4,7 @@ using MLEM.Extensions;
using MLEM.Misc; using MLEM.Misc;
namespace MLEM.Textures { namespace MLEM.Textures {
public class TextureRegion { public class TextureRegion : GenericDataHolder {
public readonly Texture2D Texture; public readonly Texture2D Texture;
public readonly Rectangle Area; public readonly Rectangle Area;

View file

@ -2,9 +2,10 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using MLEM.Misc;
namespace MLEM.Textures { namespace MLEM.Textures {
public class UniformTextureAtlas { public class UniformTextureAtlas : GenericDataHolder {
public readonly Texture2D Texture; public readonly Texture2D Texture;
public readonly int RegionAmountX; public readonly int RegionAmountX;

View file

@ -50,7 +50,7 @@ namespace Sandbox {
MaxScale = 4 MaxScale = 4
}; };
/*var tex = LoadContent<Texture2D>("Textures/Test"); var tex = LoadContent<Texture2D>("Textures/Test");
this.UiSystem.Style = new UntexturedStyle(this.SpriteBatch) { this.UiSystem.Style = new UntexturedStyle(this.SpriteBatch) {
Font = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont")), Font = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont")),
TextScale = 0.1F, TextScale = 0.1F,
@ -64,7 +64,10 @@ namespace Sandbox {
var panel = new Panel(Anchor.Center, new Vector2(0, 100), Vector2.Zero) {SetWidthBasedOnChildren = true}; 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.AutoLeft, new Vector2(100, 10)));
panel.AddChild(new Button(Anchor.AutoCenter, new Vector2(80, 10))); panel.AddChild(new Button(Anchor.AutoCenter, new Vector2(80, 10)));
this.UiSystem.Add("Panel", panel);*/ this.UiSystem.Add("Panel", panel);
panel.SetData("TestKey", new Vector2(10, 2));
Console.WriteLine(panel.GetData<Vector2>("TestKey"));
var obj = new Test { var obj = new Test {
Vec = new Vector2(10, 20), Vec = new Vector2(10, 20),