From 75deef068a981d3b868b640c2b67fe795ab7ef01 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 14 Aug 2019 14:15:07 +0200 Subject: [PATCH] renamed examples to demos and added an auto tiling demo --- Demos/AutoTilingDemo.cs | 74 ++++++++++++++++++ {Examples => Demos}/Content/Content.mgcb | 12 +++ .../Content/Fonts/TestFont.spritefont | 0 Demos/Content/Textures/AutoTiling.png | Bin 0 -> 604 bytes {Examples => Demos}/Content/Textures/Test.png | Bin .../Examples.csproj => Demos/Demos.csproj | 0 Demos/Program.cs | 39 +++++++++ Examples/GameImpl.cs => Demos/UiDemo.cs | 22 +++++- Examples/Program.cs | 10 --- MLEM.sln | 2 +- 10 files changed, 145 insertions(+), 14 deletions(-) create mode 100644 Demos/AutoTilingDemo.cs rename {Examples => Demos}/Content/Content.mgcb (70%) rename {Examples => Demos}/Content/Fonts/TestFont.spritefont (100%) create mode 100644 Demos/Content/Textures/AutoTiling.png rename {Examples => Demos}/Content/Textures/Test.png (100%) rename Examples/Examples.csproj => Demos/Demos.csproj (100%) create mode 100644 Demos/Program.cs rename Examples/GameImpl.cs => Demos/UiDemo.cs (79%) delete mode 100644 Examples/Program.cs diff --git a/Demos/AutoTilingDemo.cs b/Demos/AutoTilingDemo.cs new file mode 100644 index 0000000..5e36d79 --- /dev/null +++ b/Demos/AutoTilingDemo.cs @@ -0,0 +1,74 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using MLEM.Misc; +using MLEM.Startup; + +namespace Demos { + /// + /// This is a demo for . + /// + public class AutoTilingDemo : MlemGame { + + private Texture2D texture; + private string[] layout; + + protected override void LoadContent() { + base.LoadContent(); + // The layout of the texture is important for auto tiling to work correctly. + // It needs to be laid out as follows: Five tiles aligned horizontally within the texture file, with the following information + // 1. The texture used for filling big areas + // 2. The texture used for straight, horizontal borders, with the borders facing away from the center + // 3. The texture used for outer corners, with the corners facing away from the center + // 4. The texture used for straight, vertical borders, with the borders facing away from the center + // 5. The texture used for inner corners, with the corners facing away from the center + // Note that the AutoTiling.png texture contains an example of this layout + this.texture = LoadContent("Textures/AutoTiling"); + + // in this example, a simple string array is used for layout purposes. As the AutoTiling method allows any kind of + // comparison, the actual implementation can vary (for instance, based on a more in-depth tile map) + this.layout = new[] { + "XXX X", + "XXXXX", + "XX ", + "XXXX ", + " X " + }; + } + + protected override void Draw(GameTime gameTime) { + this.GraphicsDevice.Clear(Color.Black); + + // the texture region supplied to the AutoTiling method should only encompass the first filler tile's location and size + const int tileSize = 8; + var region = new Rectangle(0, 0, tileSize, tileSize); + + // drawing the auto tiles + this.SpriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, transformMatrix: Matrix.CreateScale(10)); + for (var x = 0; x < 5; x++) { + for (var y = 0; y < 5; y++) { + // don't draw non-grass tiles ( ) + if (this.layout[y][x] != 'X') + continue; + + var x1 = x; + var y1 = y; + // the connectsTo function determines for any given tile if it should connect to, that is, auto-tile with the + // neighbor in the supplied offset direction. In this example, the layout determines where grass tiles are (X) + // and where there are none ( ). + AutoTiling.ConnectsTo connectsTo = (xOff, yOff) => { + // don't auto-tile out of bounds + if (x1 + xOff < 0 || y1 + yOff < 0 || x1 + xOff >= 5 || y1 + yOff >= 5) + return false; + // check if the neighboring tile is also grass (X) + return this.layout[y1 + yOff][x1 + xOff] == 'X'; + }; + AutoTiling.DrawAutoTile(this.SpriteBatch, new Vector2(x, y) * tileSize, this.texture, region, connectsTo, Color.White); + } + } + this.SpriteBatch.End(); + + base.Draw(gameTime); + } + + } +} \ No newline at end of file diff --git a/Examples/Content/Content.mgcb b/Demos/Content/Content.mgcb similarity index 70% rename from Examples/Content/Content.mgcb rename to Demos/Content/Content.mgcb index ff7a781..dd14290 100644 --- a/Examples/Content/Content.mgcb +++ b/Demos/Content/Content.mgcb @@ -20,6 +20,18 @@ /processorParam:TextureFormat=Compressed /build:Fonts/TestFont.spritefont +#begin Textures/AutoTiling.png +/importer:TextureImporter +/processor:TextureProcessor +/processorParam:ColorKeyColor=255,0,255,255 +/processorParam:ColorKeyEnabled=True +/processorParam:GenerateMipmaps=False +/processorParam:PremultiplyAlpha=True +/processorParam:ResizeToPowerOfTwo=False +/processorParam:MakeSquare=False +/processorParam:TextureFormat=Color +/build:Textures/AutoTiling.png + #begin Textures/Test.png /importer:TextureImporter /processor:TextureProcessor diff --git a/Examples/Content/Fonts/TestFont.spritefont b/Demos/Content/Fonts/TestFont.spritefont similarity index 100% rename from Examples/Content/Fonts/TestFont.spritefont rename to Demos/Content/Fonts/TestFont.spritefont diff --git a/Demos/Content/Textures/AutoTiling.png b/Demos/Content/Textures/AutoTiling.png new file mode 100644 index 0000000000000000000000000000000000000000..5496de78aeab0b65c3992ee5b4c7d09410603430 GIT binary patch literal 604 zcmeAS@N?(olHy`uVBq!ia0vp^4Is?H1|$#LC7uRSjKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1quc4Ie#S978JN-rjJ`5^)q^xG+!RjLVDz4SFpu z_a$cPHKfP{6io_fbHI2`5BB8PS zmrMQnUlP9j`=6)pcFayr-g{s6ZSCog-}Zieb9lLa`sIr*N7(+}zWd^bNq=_C^Uyy9 z-_zynEq9m-^Ut5}`^$a(^>dG_^Y48tDq`Gw@6d9it!8iaC3t?{oIgi4y-2I^YSz1t z=jPh15BZ~YYpb`2S8V&!Q%oyb!t1S0>4g?4X*TYK)a(_C zn5Wa=;dSTF>64jT{aaseJMlHL@3ZZJJ+tMdw`JVl|L=vL+v6Nl@y9n-9Bo_MryVzY zhyBsy!{Wso>pCCL*#H04qqNDd%{O!I`6Jio63VoCYw=pMeJje|Cde0TPP1cve0lvm zwuwE5#pR3sY%gnz{<-zSJIPm?AKpC@s9*fLj(KhMm*q> Demos = new Dictionary>(); + + static Program() { + Demos.Add("Ui", () => new UiDemo()); + Demos.Add("AutoTiling", () => new AutoTilingDemo()); + } + + public static void Main(string[] args) { + Func demoUsed; + if (args.Length <= 0 || !Demos.ContainsKey(args[0])) { + beforeDemo: + Console.WriteLine("Please type the name of the demo you want to see and press Enter."); + Console.WriteLine("The following demos are available: " + string.Join(", ", Demos.Keys)); + Console.WriteLine("(Alternatively, you can supply the name of the demo you want to see as the first argument)"); + var demo = Console.ReadLine(); + if (!Demos.ContainsKey(demo)) { + Console.WriteLine("Not a valid demo."); + goto beforeDemo; + } else { + demoUsed = Demos[demo]; + } + } else { + demoUsed = Demos[args[0]]; + } + + using (var game = demoUsed.Invoke()) + game.Run(); + } + + } +} \ No newline at end of file diff --git a/Examples/GameImpl.cs b/Demos/UiDemo.cs similarity index 79% rename from Examples/GameImpl.cs rename to Demos/UiDemo.cs index cf72e48..1ec1c52 100644 --- a/Examples/GameImpl.cs +++ b/Demos/UiDemo.cs @@ -11,13 +11,20 @@ using MLEM.Ui; using MLEM.Ui.Elements; using MLEM.Ui.Style; -namespace Examples { - public class GameImpl : MlemGame { +namespace Demos { + /// + /// NOTE: This ui demo derives from . To use MLEM.Ui, it's not required to use MLEM.Startup (which MlemGame is a part of). + /// If using your own game class that derives from , however, you will have to do a few additional things to get MLEM.Ui up and running: + /// - Create an instance of + /// - Call the instance's Update method in your game's Update method + /// - Call the instance's Draw method in your game's Draw method + /// + public class UiDemo : MlemGame { private Texture2D testTexture; private NinePatch testPatch; - public GameImpl() { + public UiDemo() { this.IsMouseVisible = true; } @@ -26,7 +33,11 @@ namespace Examples { this.testPatch = new NinePatch(new TextureRegion(this.testTexture, 0, 8, 24, 24), 8); base.LoadContent(); + // create a new style + // this one derives form UntexturedStyle so that stuff like the hover colors don't have to be set again var style = new UntexturedStyle(this.SpriteBatch) { + // when using a SpriteFont, use GenericSpriteFont. When using a MonoGame.Extended BitmapFont, use GenericBitmapFont. + // Wrapping fonts like this allows for both types to be usable within MLEM.Ui easily Font = new GenericSpriteFont(LoadContent("Fonts/TestFont")), TextScale = 0.1F, PanelTexture = this.testPatch, @@ -40,9 +51,12 @@ namespace Examples { RadioCheckmark = new TextureRegion(this.testTexture, 32, 0, 8, 8) }; var untexturedStyle = this.UiSystem.Style; + // set the defined style as the current one this.UiSystem.Style = style; + // scale every ui up by 5 this.UiSystem.GlobalScale = 5; + // create the root panel that all the other components sit on and add it to the ui system var root = new Panel(Anchor.Center, new Vector2(80, 100), Vector2.Zero, false, true, new Point(5, 10)); this.UiSystem.Add("Test", root); @@ -62,6 +76,8 @@ namespace Examples { this.UiSystem.Style = this.UiSystem.Style == untexturedStyle ? style : untexturedStyle; }, PositionOffset = new Vector2(0, 1), + // set HasCustomStyle to true before changing style information so that, when changing the style globally + // (like above), these custom values don't get undone HasCustomStyle = true, Texture = this.testPatch, HoveredColor = Color.LightGray diff --git a/Examples/Program.cs b/Examples/Program.cs deleted file mode 100644 index 4b07ab5..0000000 --- a/Examples/Program.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Examples { - public static class Program { - - public static void Main() { - using (var game = new GameImpl()) - game.Run(); - } - - } -} \ No newline at end of file diff --git a/MLEM.sln b/MLEM.sln index f3c27ee..508a743 100644 --- a/MLEM.sln +++ b/MLEM.sln @@ -6,7 +6,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLEM.Extended", "MLEM.Exten EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLEM.Startup", "MLEM.Startup\MLEM.Startup.csproj", "{997F4739-7BEC-4621-B9CA-68DEB2D74412}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Examples", "Examples\Examples.csproj", "{1BC4682B-AA14-4937-B5C7-707E20FE88FF}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demos", "Demos\Demos.csproj", "{1BC4682B-AA14-4937-B5C7-707E20FE88FF}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLEM.Ui", "MLEM.Ui\MLEM.Ui.csproj", "{6F00629A-8B87-4264-8896-19983285E32F}" EndProject