Skip to content

Commit

Permalink
add more code
Browse files Browse the repository at this point in the history
  • Loading branch information
LoneWandererProductions committed Dec 27, 2023
1 parent 3ab8ee1 commit d23b100
Show file tree
Hide file tree
Showing 5 changed files with 342 additions and 114 deletions.
17 changes: 8 additions & 9 deletions Aurorae/Aurora.xaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<UserControl x:Class="Aurorae.Aurora"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Aurorae"
mc:Ignorable="d"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Image Name="LayerOne"/>
<Image Name="LayerTwo"/>
<Image Name="LayerThree"/>
<Label Name="Touch" MouseDown="Touch_MouseDown"/>
<Image Name="LayerOne" />
<Image Name="LayerTwo" />
<Image Name="LayerThree" />
<Label Name="Touch" MouseDown="Touch_MouseDown" />
</Grid>
</UserControl>
</UserControl>
145 changes: 40 additions & 105 deletions Aurorae/Aurora.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
using Imaging;
// ReSharper disable MemberCanBeInternal
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global

using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ExtendedSystemObjects;

namespace Aurorae
{
/// <inheritdoc cref="UserControl" />
/// <summary>
/// Generate a playing field
/// Generate a playing field
/// </summary>
public partial class Aurora
public sealed partial class Aurora
{
private readonly ImageRender _render = new();

public static readonly DependencyProperty MapHeight = DependencyProperty.Register(nameof(MapHeight),
typeof(int),
typeof(Aurora), null);
Expand All @@ -41,15 +39,20 @@ public partial class Aurora
typeof(bool),
typeof(Aurora), null);

public static readonly DependencyProperty Number = DependencyProperty.Register(nameof(Number),
typeof(bool),
typeof(Aurora), null);
//todo add a List Property for the Movement
//todo add an Image for the Avatar
//todo add starting point for Avatar
//todo add a block Property
//todo add animation code in Helper

private Bitmap _LayerOne;
private Bitmap _LayerTwo;
private Bitmap _LayerThree;
private Cursor _cursor;

public Aurora()
{
InitializeComponent();
Initiate();
}

public int DependencyHeight
{
get => (int)GetValue(MapHeight);
Expand Down Expand Up @@ -84,121 +87,53 @@ public List<Texture> DependencyTextures
public bool DependencyGrid
{
get => (bool)GetValue(Grid);
set => SetValue(Grid, value);
}

public bool DependencyNumber
{
get => (bool)GetValue(Number);
set => SetValue(Number, value);
}

public Aurora()
{
InitializeComponent();
Initiate();
}

private void Initiate()
{
if (DependencyWidth == 0 || DependencyHeight == 0 || DependencyTextureSize == 0) return;

_LayerOne = new Bitmap(DependencyWidth * DependencyTextureSize, DependencyHeight * DependencyTextureSize);
Generate();

if (DependencyGrid) _LayerTwo = new Bitmap(DependencyWidth * DependencyTextureSize, DependencyHeight * DependencyTextureSize);

_LayerThree = new Bitmap(DependencyWidth * DependencyTextureSize, DependencyHeight * DependencyTextureSize);

Touch.Height = _LayerOne.Height;
Touch.Width = _LayerOne.Width;
}

private void Generate()
{
DrawMap();
LayerOne.Source = _LayerOne.ToBitmapImage();

if (DependencyGrid)
set
{
DrawGrid();
LayerTwo.Source = _LayerTwo.ToBitmapImage();
}

if (DependencyNumber)
{
DrawNumbers();
LayerThree.Source = _LayerThree.ToBitmapImage();
SetValue(Grid, value);
LayerTwo.Source = !DependencyGrid
? null
: Helper.GenerateGrid(DependencyWidth, DependencyHeight, DependencyTextureSize);
}
}

private void DrawMap()
private void Initiate()
{
var boxes = new List<Box>();

var layers = DependencyMap.ChunkBy(DependencyHeight);


for (var y = 0; y < layers.Count; y++)
if (DependencyWidth == 0 || DependencyHeight == 0 || DependencyTextureSize == 0)
{
var slice = layers[y];

for (var x = 0; x < slice.Count; x++)
{
var id = slice[x];
if (id <= 0) continue;

var texture = DependencyTextures[id];
var image = _render.GetBitmapFile(texture.Path);

var box = new Box
{
X = x * DependencyTextureSize,
Y = y * DependencyTextureSize,
Image = image,
Layer = texture.Layer
};

boxes.Add(box);
}
return;
}

foreach (var slice in boxes.OrderBy(layer => layer.Layer).ToList())
{
_LayerOne = _render.CombineBitmap(_LayerOne, slice.Image, slice.X, slice.Y);
}
}
Touch.Height = DependencyHeight * DependencyTextureSize;
Touch.Width = DependencyWidth * DependencyTextureSize;

private void DrawGrid()
{
for (int y = 0; y < DependencyHeight; y++)
LayerOne.Source = Helper.GenerateImage(DependencyWidth, DependencyHeight, DependencyTextureSize,
DependencyTextures, DependencyMap);

if (DependencyGrid)
{
for (int x = 0; x < DependencyWidth; x++)
{
using Graphics graphics = Graphics.FromImage(_LayerTwo);
graphics.DrawRectangle(Pens.Black, x * DependencyTextureSize, y * DependencyTextureSize, DependencyTextureSize, DependencyTextureSize);
}
LayerTwo.Source = Helper.GenerateGrid(DependencyWidth, DependencyHeight, DependencyTextureSize);
}
}

private void DrawNumbers()
{
throw new System.NotImplementedException();
}

private void Touch_MouseDown(object sender, MouseButtonEventArgs e)
{
_cursor = new Cursor();
var position = e.GetPosition(Touch);


if (position.X < DependencyTextureSize) _cursor.X = 0;
if (position.X < DependencyTextureSize)
{
_cursor.X = 0;
}
else
{
_cursor.X = (int)position.X / DependencyTextureSize;
}

if (position.Y < DependencyTextureSize) _cursor.Y = 0;
if (position.Y < DependencyTextureSize)
{
_cursor.Y = 0;
}
else
{
_cursor.Y = (int)position.X / DependencyTextureSize;
Expand Down
124 changes: 124 additions & 0 deletions Aurorae/Helper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using ExtendedSystemObjects;
using Imaging;
using Brushes = System.Drawing.Brushes;

namespace Aurorae
{
/// <summary>
/// Handle all the Image generation Tasks
/// </summary>
internal static class Helper
{
/// <summary>
/// The padding for the numbers
/// </summary>
private const int Padding = 2;

/// <summary>
/// The render Interface
/// </summary>
private static readonly ImageRender Render = new();

/// <summary>
/// Generates the image.
/// </summary>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="textureSize">Size of the texture.</param>
/// <param name="textures">The textures.</param>
/// <param name="map">The map.</param>
/// <returns>The generated Map</returns>
internal static BitmapImage GenerateImage(int width, int height, int textureSize, List<Texture> textures,
IEnumerable<int> map)
{
var background = new Bitmap(width * textureSize, height * textureSize);

var boxes = new List<Box>();

var layers = map.ChunkBy(height);

for (var y = 0; y < layers.Count; y++)
{
var slice = layers[y];

for (var x = 0; x < slice.Count; x++)
{
var id = slice[x];
if (id <= 0)
{
continue;
}

var texture = textures[id];
var image = Render.GetBitmapFile(texture.Path);

var box = new Box {X = x * textureSize, Y = y * textureSize, Image = image, Layer = texture.Layer};

boxes.Add(box);
}
}

background = boxes.OrderBy(layer => layer.Layer).ToList().Aggregate(background,
(current, slice) => Render.CombineBitmap(current, slice.Image, slice.X, slice.Y));

return background.ToBitmapImage();
}

/// <summary>
/// Generates the grid.
/// </summary>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="textureSize">Size of the texture.</param>
/// <returns>A grid overlay</returns>
internal static ImageSource GenerateGrid(int width, int height, int textureSize)
{
var background = new Bitmap(width * textureSize, height * textureSize);

for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
using var graphics = Graphics.FromImage(background);
graphics.DrawRectangle(Pens.Black, x * textureSize, y * textureSize, textureSize, textureSize);
}
}

return background.ToBitmapImage();
}

/// <summary>
/// Generates the numbers.
/// </summary>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="textureSize">Size of the texture.</param>
/// <returns>A number overlay</returns>
public static ImageSource GenerateNumbers(int width, int height, int textureSize)
{
var background = new Bitmap(width * textureSize, height * textureSize);

var count = -1;

for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
count++;

using var graphics = Graphics.FromImage(background);
var rectangle = new RectangleF((x * textureSize) + Padding, (y * textureSize) + Padding,
textureSize - Padding, textureSize - Padding);
graphics.DrawString(count.ToString(), new Font("Tahoma", 8), Brushes.Black, rectangle);
}
}

return background.ToBitmapImage();
}
}
}
14 changes: 14 additions & 0 deletions Aurorae/Polaris.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<UserControl x:Class="Aurorae.Polaris"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Image Name="LayerOne" />
<Image Name="LayerTwo" />
<Image Name="LayerThree" />
<Label Name="Touch" MouseDown="Touch_MouseDown" />
</Grid>
</UserControl>
Loading

0 comments on commit d23b100

Please sign in to comment.