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 0000000..5496de7 Binary files /dev/null and b/Demos/Content/Textures/AutoTiling.png differ diff --git a/Examples/Content/Textures/Test.png b/Demos/Content/Textures/Test.png similarity index 100% rename from Examples/Content/Textures/Test.png rename to Demos/Content/Textures/Test.png diff --git a/Examples/Examples.csproj b/Demos/Demos.csproj similarity index 100% rename from Examples/Examples.csproj rename to Demos/Demos.csproj diff --git a/Demos/Program.cs b/Demos/Program.cs new file mode 100644 index 0000000..2eb4fb0 --- /dev/null +++ b/Demos/Program.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Xna.Framework; + +namespace Demos { + public static class Program { + + private static readonly Dictionary> 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