1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 04:53:29 +01:00

cleaned up code

This commit is contained in:
Ell 2022-08-20 11:39:28 +02:00
parent 6a271af017
commit 0a696941dc
30 changed files with 128 additions and 89 deletions

View file

@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<ApplicationIcon>Icon.ico</ApplicationIcon>
<AssemblyName>MLEM Desktop Demos</AssemblyName>
<RootNamespace>Demos.DesktopGL</RootNamespace>

View file

@ -1,9 +1,10 @@
using System;
using Microsoft.Xna.Framework;
using MLEM.Misc;
#if FNA
using Microsoft.Xna.Framework.Input;
using MLEM.Misc;
#endif
namespace Demos.DesktopGL;
namespace Demos.DesktopGL {
public static class Program {
public static void Main() {
@ -17,4 +18,3 @@ namespace Demos.DesktopGL {
}
}
}

View file

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
@ -11,7 +11,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.263">
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

View file

@ -43,7 +43,7 @@ namespace Demos {
this.GraphicsDevice.Clear(Color.CornflowerBlue);
base.DoDraw(time);
this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null);
this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise);
var view = this.GraphicsDevice.Viewport;
// graph the easing function

View file

@ -9,9 +9,6 @@ using MLEM.Textures;
using MLEM.Ui;
using MLEM.Ui.Elements;
using MLEM.Ui.Style;
#if !FNA
using MonoGame.Framework.Utilities;
#endif
namespace Demos {
public class GameImpl : MlemGame {

View file

@ -50,7 +50,7 @@ namespace Demos {
public override void DoDraw(GameTime time) {
this.GraphicsDevice.Clear(Color.DarkSlateGray);
this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null);
this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise);
// we draw the tokenized text in the center of the screen
// since the text is already center-aligned, we only need to align it on the y axis here

2
FNA

@ -1 +1 @@
Subproject commit 31ff5cd8b6d62b0c10b627c67cc66db1d4def4e7
Subproject commit dfa9ce8cfd46846ad1df81283e84cfe5cee4faca

View file

@ -13,9 +13,9 @@ namespace MLEM.Data.Content {
if (existing != null) {
existing.Reload(stream);
return existing;
} else
}
#endif
{
// premultiply the texture's color to be in line with the pipeline's texture reader
using (var texture = Texture2D.FromStream(manager.GraphicsDevice, stream)) {
var ret = new Texture2D(manager.GraphicsDevice, texture.Width, texture.Height);
@ -30,7 +30,6 @@ namespace MLEM.Data.Content {
return ret;
}
}
}
/// <inheritdoc />
public override string[] GetFileExtensions() {

View file

@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
@ -6,8 +5,10 @@ using System.Text.RegularExpressions;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions;
using MLEM.Textures;
#if FNA
using MLEM.Extensions;
#endif
namespace MLEM.Data {
/// <summary>

View file

@ -267,7 +267,7 @@ namespace MLEM.Data {
srcColor = Color.Transparent;
} else {
// otherwise, we just use the closest pixel that is actually in bounds, causing the border pixels to be doubled up
var src = new Point((int) MathHelper.Clamp(x, 0, request.Texture.Width - 1), (int) MathHelper.Clamp(y, 0, request.Texture.Height - 1));
var src = new Point((int) MathHelper.Clamp(x, 0F, request.Texture.Width - 1), (int) MathHelper.Clamp(y, 0F, request.Texture.Height - 1));
srcColor = data[request.Texture.Position + src];
}
destination[location + new Point(x, y)] = srcColor;

View file

@ -10,9 +10,9 @@ using MLEM.Extensions;
using MLEM.Graphics;
using MLEM.Input;
using MLEM.Misc;
using MLEM.Sound;
using MLEM.Textures;
using MLEM.Ui.Style;
using SoundEffectInfo = MLEM.Sound.SoundEffectInfo;
namespace MLEM.Ui.Elements {
/// <summary>

View file

@ -1,11 +1,13 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions;
using MLEM.Graphics;
using MLEM.Misc;
using MLEM.Textures;
using MLEM.Ui.Style;
#if FNA
using MLEM.Extensions;
#endif
namespace MLEM.Ui.Elements {
/// <summary>

View file

@ -3,12 +3,14 @@ using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using MLEM.Extensions;
using MLEM.Graphics;
using MLEM.Input;
using MLEM.Misc;
using MLEM.Textures;
using MLEM.Ui.Style;
#if FNA
using MLEM.Extensions;
#endif
namespace MLEM.Ui.Elements {
/// <summary>

View file

@ -1,4 +1,3 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Font;

View file

@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using MLEM.Extensions;
using MLEM.Input;
using MLEM.Ui.Style;
#if FNA
using MLEM.Extensions;
#endif
namespace MLEM.Ui.Elements {
/// <summary>

View file

@ -5,9 +5,9 @@ using MLEM.Font;
using MLEM.Formatting;
using MLEM.Formatting.Codes;
using MLEM.Misc;
using MLEM.Sound;
using MLEM.Textures;
using MLEM.Ui.Elements;
using SoundEffectInfo = MLEM.Sound.SoundEffectInfo;
namespace MLEM.Ui.Style {
/// <summary>

View file

@ -1,5 +1,4 @@
using System;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Ui.Elements;
namespace MLEM.Ui {

View file

@ -561,7 +561,7 @@ namespace MLEM.Ui {
/// This property returns <see langword="true"/> if <see cref="CanSelectContent"/> is <see langword="true"/> and the <see cref="Element"/> is not hidden, or if it has been set to <see langword="true"/> manually.
/// </summary>
public bool CanBeActive {
get => this.canBeActive || (!this.Element.IsHidden && this.CanSelectContent);
get => this.canBeActive || !this.Element.IsHidden && this.CanSelectContent;
set => this.canBeActive = value;
}

View file

@ -304,6 +304,41 @@ namespace MLEM.Extensions {
public static Vector2 ToVector2(this Point point) {
return new Vector2(point.X, point.Y);
}
/// <summary>
/// Deconstruction method for <see cref="Point"/>.
/// </summary>
/// <param name="point">The point to deconstruct.</param>
/// <param name="x"></param>
/// <param name="y"></param>
public static void Deconstruct(this Point point, out int x, out int y) {
x = point.X;
y = point.Y;
}
/// <summary>
/// Deconstruction method for <see cref="Vector2"/>.
/// </summary>
/// <param name="vector">The vector to deconstruct.</param>
/// <param name="x"></param>
/// <param name="y"></param>
public static void Deconstruct(this Vector2 vector, out float x, out float y) {
x = vector.X;
y = vector.Y;
}
/// <summary>
/// Deconstruction method for <see cref="Vector3"/>.
/// </summary>
/// <param name="vector">The vector to deconstruct.</param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="z"></param>
public static void Deconstruct(this Vector3 vector, out float x, out float y, out float z) {
x = vector.X;
y = vector.Y;
z = vector.Z;
}
#endif
}

View file

@ -1,8 +1,11 @@
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions;
#if !FNA
using System.Linq;
#endif
namespace MLEM.Font {
/// <inheritdoc/>
public class GenericSpriteFont : GenericFont {

View file

@ -88,7 +88,7 @@ namespace MLEM.Graphics {
var orig = origin ?? Vector2.Zero;
var sc = scale ?? Vector2.One;
var od = layerDepth + overlayDepthOffset;
var (r1, r2, r3, r4) = AutoTiling.CalculateExtendedAutoTile(pos, overlayTexture.Area, connectsTo, sc);
var (r1, r2, r3, r4) = AutoTiling.CalculateExtendedAutoTile(overlayTexture.Area, connectsTo);
if (backgroundTexture != null)
batch.Draw(backgroundTexture, pos, backgroundColor, 0, orig, sc, SpriteEffects.None, layerDepth);
if (r1 != Rectangle.Empty)
@ -106,7 +106,7 @@ namespace MLEM.Graphics {
var orig = origin ?? Vector2.Zero;
var sc = scale ?? Vector2.One;
var od = layerDepth + overlayDepthOffset;
var (r1, r2, r3, r4) = AutoTiling.CalculateExtendedAutoTile(pos, overlayTexture.Area, connectsTo, sc);
var (r1, r2, r3, r4) = AutoTiling.CalculateExtendedAutoTile(overlayTexture.Area, connectsTo);
if (backgroundTexture != null) {
var background = batch.Add(backgroundTexture, pos, backgroundColor, 0, orig, sc, SpriteEffects.None, layerDepth);
items?.Add(background);
@ -147,7 +147,7 @@ namespace MLEM.Graphics {
new Vector2(pos.X + w2 * scale.X, pos.Y + h2 * scale.Y), new Rectangle(textureRegion.X + w2 + xDr * w, textureRegion.Y + h2, w2, h2));
}
private static (Rectangle, Rectangle, Rectangle, Rectangle) CalculateExtendedAutoTile(Vector2 pos, Rectangle textureRegion, ConnectsTo connectsTo, Vector2 scale) {
private static (Rectangle, Rectangle, Rectangle, Rectangle) CalculateExtendedAutoTile(Rectangle textureRegion, ConnectsTo connectsTo) {
var up = connectsTo(0, -1);
var down = connectsTo(0, 1);
var left = connectsTo(-1, 0);

View file

@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#if FNA
using MLEM.Extensions;
using System.IO;
#endif
namespace MLEM.Graphics {
/// <summary>

View file

@ -73,7 +73,7 @@ namespace MLEM.Input {
/// </summary>
public bool InvertPressBehavior;
/// <summary>
/// If your project already handles the processing of MonoGame's gestures elsewhere, you can set this field to true to ensure that this input handler's gesture handling does not override your own, since <see cref="GestureSample"/> objects can only be retrieved once and are then removed from the <see cref="TouchPanelState"/>'s queue.
/// If your project already handles the processing of MonoGame's gestures elsewhere, you can set this field to true to ensure that this input handler's gesture handling does not override your own, since <see cref="GestureSample"/> objects can only be retrieved once and are then removed from the <see cref="TouchPanel"/>'s queue.
/// If this value is set to true, but you still want to be able to use <see cref="Gestures"/>, <see cref="GetGesture"/>, and <see cref="GetViewportGesture"/>, you can make this input handler aware of a gesture for the duration of the update frame that you added it on by using <see cref="AddExternalGesture"/>.
/// For more info, see https://mlem.ellpeck.de/articles/input.html#external-gesture-handling.
/// </summary>

View file

@ -31,7 +31,7 @@ public class GameImpl : MlemGame {
public GameImpl() {
this.IsMouseVisible = true;
this.Window.ClientSizeChanged += (o, args) => {
this.Window.ClientSizeChanged += (_, _) => {
Console.WriteLine("Size changed");
};
}
@ -121,7 +121,7 @@ public class GameImpl : MlemGame {
var res = this.Content.LoadJson<Test>("Test");
Console.WriteLine("The res is " + res);
var gradient = this.SpriteBatch.GenerateGradientTexture(Color.Green, Color.Red, Color.Blue, Color.Yellow);
//var gradient = this.SpriteBatch.GenerateGradientTexture(Color.Green, Color.Red, Color.Blue, Color.Yellow);
/*this.OnDraw += (game, time) => {
this.SpriteBatch.Begin();
this.SpriteBatch.Draw(this.SpriteBatch.GetBlankTexture(), new Rectangle(640 - 4, 360 - 4, 8, 8), Color.Green);
@ -139,9 +139,9 @@ public class GameImpl : MlemGame {
var sc = 4;
var formatter = new TextFormatter();
formatter.AddImage("Test", new TextureRegion(tex, 0, 8, 24, 24));
formatter.Macros.Add(new Regex("<testmacro>"), (f, m, r) => "<test1>");
formatter.Macros.Add(new Regex("<test1>"), (f, m, r) => "<test2> blue");
formatter.Macros.Add(new Regex("<test2>"), (f, m, r) => "<c Blue>");
formatter.Macros.Add(new Regex("<testmacro>"), (_, _, _) => "<test1>");
formatter.Macros.Add(new Regex("<test1>"), (_, _, _) => "<test2> blue");
formatter.Macros.Add(new Regex("<test2>"), (_, _, _) => "<c Blue>");
var strg = "This text uses a bunch of non-breaking~spaces to see if macros work. Additionally, it uses a macro that resolves into a bunch of other macros and then, at the end, into <testmacro> text</c>.";
//var strg = "Lorem Ipsum <i Test> is simply dummy text of the <i Test> printing and typesetting <i Test> industry. Lorem Ipsum has been the industry's standard dummy text <i Test> ever since the <i Test> 1500s, when <i Test><i Test><i Test><i Test><i Test><i Test><i Test> an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
//var strg = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
@ -149,7 +149,7 @@ public class GameImpl : MlemGame {
this.tokenized = formatter.Tokenize(font, strg);
this.tokenized.Split(font, 400, sc);
var square = this.SpriteBatch.GenerateSquareTexture(Color.Yellow);
//var square = this.SpriteBatch.GenerateSquareTexture(Color.Yellow);
var round = this.SpriteBatch.GenerateCircleTexture(Color.Green, 128);
var region = new TextureRegion(round) {Pivot = new Vector2(0.5F)};
@ -159,7 +159,7 @@ public class GameImpl : MlemGame {
foreach (var r in atlas.Regions)
Console.WriteLine(r.Name + ": " + r.U + " " + r.V + " " + r.Width + " " + r.Height + " " + r.PivotPixels);
this.OnDraw += (g, time) => {
this.OnDraw += (_, _) => {
this.SpriteBatch.Begin(samplerState: SamplerState.PointClamp);
//this.SpriteBatch.Draw(square, new Rectangle(10, 10, 400, 400), Color.White);
//this.SpriteBatch.Draw(round, new Rectangle(10, 10, 400, 400), Color.White);
@ -173,7 +173,7 @@ public class GameImpl : MlemGame {
//this.SpriteBatch.DrawGrid(new Vector2(30, 30), new Vector2(40, 60), new Point(10, 5), Color.Yellow, 3);
this.SpriteBatch.End();
};
this.OnUpdate += (g, time) => {
this.OnUpdate += (_, time) => {
if (this.InputHandler.IsPressed(Keys.W)) {
this.tokenized = formatter.Tokenize(font, strg);
this.tokenized.Split(font, this.InputHandler.IsModifierKeyDown(ModifierKey.Shift) ? 400 : 500, sc);
@ -259,7 +259,7 @@ public class GameImpl : MlemGame {
var rotation = 0F;
var effects = SpriteEffects.None;
this.OnDraw += (g, time) => {
this.OnDraw += (_, _) => {
const string testString = "This is a\ntest string\n\twith long lines.\nLet's write some more stuff. Let's\r\nsplit lines weirdly.";
if (MlemGame.Input.IsKeyPressed(Keys.I)) {
index++;
@ -335,15 +335,16 @@ public class GameImpl : MlemGame {
};*/
var widthPanel = new Panel(Anchor.Center, Vector2.One, Vector2.Zero, true) {SetWidthBasedOnChildren = true};
for (var i = 0; i < 5; i++)
for (var i = 0; i < 5; i++) {
widthPanel.AddChild(new Paragraph(Anchor.AutoCenter, 100000, "Test String " + Math.Pow(10, i), true) {
OnUpdated = (e, time) => {
if (Input.IsPressed(Keys.A)) {
OnUpdated = (e, _) => {
if (MlemGame.Input.IsPressed(Keys.A)) {
e.Anchor = (Anchor) (((int) e.Anchor + 1) % EnumHelper.GetValues<Anchor>().Length);
Console.WriteLine(e.Anchor);
}
}
});
}
this.UiSystem.Add("WidthTest", widthPanel);
}

View file

@ -1,5 +1,4 @@
using Microsoft.Xna.Framework;
using MLEM.Misc;
using MLEM.Misc;
namespace Sandbox;

View file

@ -12,11 +12,11 @@ using Vector2 = Microsoft.Xna.Framework.Vector2;
namespace Tests {
public class DataTests {
private readonly TestObject testObject = new(Vector2.One, "test") {
private readonly TestObject testObject = new() {
Vec = new Vector2(10, 20),
Point = new Point(20, 30),
Dir = Direction2.Left,
OtherTest = new TestObject(Vector2.One, "other") {
OtherTest = new TestObject {
Vec = new Vector2(70, 30),
Dir = Direction2.Right
}
@ -95,8 +95,6 @@ namespace Tests {
public Direction2 Dir { get; set; }
public TestObject OtherTest;
public TestObject(Vector2 test, string test2) {}
protected bool Equals(TestObject other) {
return this.Vec.Equals(other.Vec) && this.Point.Equals(other.Point) && object.Equals(this.OtherTest, other.OtherTest) && this.Dir == other.Dir;
}

View file

@ -102,16 +102,16 @@ namespace Tests {
[Test]
public void TestMacros() {
this.formatter.Macros.Add(new Regex("<testmacro>"), (f, m, r) => "<test1>");
this.formatter.Macros.Add(new Regex("<test1>"), (f, m, r) => "<test2>blue");
this.formatter.Macros.Add(new Regex("<test2>"), (f, m, r) => "<c Blue>");
this.formatter.Macros.Add(new Regex("<testmacro>"), (_, _, _) => "<test1>");
this.formatter.Macros.Add(new Regex("<test1>"), (_, _, _) => "<test2>blue");
this.formatter.Macros.Add(new Regex("<test2>"), (_, _, _) => "<c Blue>");
const string strg = "This text uses a bunch of non-breaking~spaces to see if macros work. Additionally, it uses a macro that resolves into a bunch of other macros and then, at the end, into <testmacro> text</c>.";
const string goal = "This text uses a bunch of non-breaking\u00A0spaces to see if macros work. Additionally, it uses a macro that resolves into a bunch of other macros and then, at the end, into <c Blue>blue text</c>.";
Assert.AreEqual(this.formatter.ResolveMacros(strg), goal);
// test recursive macros
this.formatter.Macros.Add(new Regex("<rec1>"), (f, m, r) => "<rec2>");
this.formatter.Macros.Add(new Regex("<rec2>"), (f, m, r) => "<rec1>");
this.formatter.Macros.Add(new Regex("<rec1>"), (_, _, _) => "<rec2>");
this.formatter.Macros.Add(new Regex("<rec2>"), (_, _, _) => "<rec1>");
Assert.Throws<ArithmeticException>(() => this.formatter.ResolveMacros("Test <rec1> string"));
}

View file

@ -66,7 +66,7 @@ namespace Tests {
'X' => float.PositiveInfinity,
_ => (float) char.GetNumericValue(c)
}).ToArray()).ToArray();
var pathFinder = new AStar2((p1, p2) => costs[p2.Y][p2.X], allowDiagonals);
var pathFinder = new AStar2((_, p2) => costs[p2.Y][p2.X], allowDiagonals);
return pathFinder.FindPath(start, end);
}

View file

@ -122,12 +122,12 @@ namespace Tests {
for (var i = 1; i <= 100; i++) {
var totalUpdates = 0;
var main = new Group(Anchor.TopLeft, new Vector2(50)) {
OnAreaUpdated = e => totalUpdates++
OnAreaUpdated = _ => totalUpdates++
};
var group = main;
for (var g = 0; g < i; g++) {
group = group.AddChild(new Group(Anchor.TopLeft, Vector2.One) {
OnAreaUpdated = e => totalUpdates++
OnAreaUpdated = _ => totalUpdates++
});
}
stopwatch.Restart();