Skip to content

Commit

Permalink
Add multiplatform builds
Browse files Browse the repository at this point in the history
  • Loading branch information
BinaryConstruct committed Jan 13, 2024
1 parent 0db9271 commit 3030e42
Show file tree
Hide file tree
Showing 15 changed files with 523 additions and 247 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ build/
*.wix
*.msi
/src/publish/*
/publish/
43 changes: 43 additions & 0 deletions build-avalonia.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
param(
[string] $VersionPrefix = "5.0.0",
[string] $VersionSuffix = "alpha2"
)

$platforms = $(
# linux builds
"linux-arm64",
"linux-musl-x64",
"linux-x64",
"linux-musl-arm64",
# windows
"win-x64",
"win-arm64",
# mac
"osx-x64",
"osx-arm64"
)

if (Test-Path -Path ".\publish\TEdit*.zip") { Remove-Item -Path ".\publish\TEdit*.zip" }

$platforms | ForEach-Object {
$buildArgs = @(
"publish"
"-c"
"Release"
"-r"
$_
"--self-contained"
"true"
"-p:PublishSingleFile=true"
"-o"
".\publish\$_"
"/p:VersionPrefix=""$VersionPrefix"""
"--version-suffix"
"$VersionSuffix"
".\src\TEdit.Desktop\TEdit.Desktop.csproj"
)

& dotnet $buildArgs

Compress-Archive -Path ".\publish\$_\*" -DestinationPath ".\publish\TEdit-$VersionPrefix-$VersionSuffix-$_-x64.zip"
}
112 changes: 103 additions & 9 deletions src/TEdit.Desktop/Controls/SkiaWorldRenderBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
using Avalonia.Input;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using Avalonia.Skia;
using Avalonia.Threading;
using SkiaSharp;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using TEdit.Desktop.Controls.WorldRenderEngine;
using TEdit.Desktop.Controls.WorldRenderEngine.Layers;
using TEdit.Geometry;
Expand Down Expand Up @@ -430,7 +433,7 @@ public enum SelectionModes
private Point _pointerPosition;
private bool _isPanning;
private bool _isSelecting;
private IRasterTileCache _pixelTileCache = new RasterTileCache();
private IRasterTileCache _pixelTileCache = new RasterTileCache(0, 0);
private int _oldZoom = 100;

static SkiaWorldRenderBox()
Expand Down Expand Up @@ -526,14 +529,16 @@ public void TriggerRender(bool renderOnlyCursorTracker = false)
InvalidateVisual();
}

Brush redBrush = new Avalonia.Media.SolidColorBrush(Colors.Red, 0.5d);

public override void Render(DrawingContext context)
{
base.Render(context);

var imageViewPort = GetImageViewPort();


var viewport = Bounds;

context.Custom(new NoSkiaCustomDrawOp(viewport, _noSkia));
context.Custom(new BackgroundGridCustomDrawOp(viewport));

Expand All @@ -548,6 +553,27 @@ public override void Render(DrawingContext context)
var cursorTile = GetScaledRectangle(WorldCoordinate, new(1, 1));
context.DrawRectangle(Brushes.Aqua, null, cursorTile);

// draw out of bounds
int borderTop = 41;
int borderLeft = 41;
int borderRight = 42;
int borderBottom = 42;

if (World != null)
{
int sidebarHeight = World.TilesHigh - borderTop - borderBottom;

var topRect = GetScaledRectangle(0, 0, World.TilesWide, borderTop);
var leftRect = GetScaledRectangle(0, borderTop, borderLeft, sidebarHeight);
var rightRect = GetScaledRectangle(World.TilesWide - borderRight, borderTop, borderRight, sidebarHeight);
var bottomRect = GetScaledRectangle(0, World.TilesHigh - borderBottom, World.TilesWide, borderBottom);

context.DrawRectangle(redBrush, null, topRect);
context.DrawRectangle(redBrush, null, leftRect);
context.DrawRectangle(redBrush, null, rightRect);
context.DrawRectangle(redBrush, null, bottomRect);
}

Dispatcher.UIThread.InvokeAsync(InvalidateVisual, DispatcherPriority.Background);
}

Expand All @@ -571,17 +597,60 @@ public World? World
get => _world;
set
{
// SetValue(WorldProperty, value);

if (_world == value) return;

// clear the render tile cache when switching worlds
_world = null;
var oldCache = _pixelTileCache;

_world = value;
Zoom = 100;
_pixelTileCache?.Clear();

_pixelTileCache = new RasterTileCache(_world.TilesHigh, _world.TilesWide);
oldCache?.Dispose();
StartFullRender();

UpdateViewPort();
TriggerRender();
}
}

private void StartFullRender()
{
Task.Factory.StartNew(() =>
{
int numX = _pixelTileCache.TilesX;
int numY = _pixelTileCache.TilesY;

for (int x = 0; x < numX; x++)
{
Parallel.For(
0,
numY,
new ParallelOptions { MaxDegreeOfParallelism = 4 },
y =>
{
RenderTile(y, x);
});
}
});
}

private void RenderTile(int y, int x)
{
var tile = new RasterTile
{
Bitmap = RasterTileRenderer.CreateBitmapTile(_world, x, y, _pixelTileCache.TileSize),
TileX = x,
TileY = y,
PixelX = x * _pixelTileCache.TileSize,
PixelY = y * _pixelTileCache.TileSize,
IsDirty = false
};

_pixelTileCache.SetTile(tile, x, y);
}

/// <summary>
/// Gets the size of the scaled image.
/// </summary>
Expand Down Expand Up @@ -827,7 +896,7 @@ public Vector Offset
}

/// <inheritdoc />
public Size Viewport => ViewPort.Bounds.Size;
public Size Viewport => this.Bounds.Size;
#endregion

#region Properties
Expand Down Expand Up @@ -1319,7 +1388,7 @@ public Point PointToImage(Point point, bool fitToBounds = true)
x = (point.X + Offset.X - viewport.X) / (Zoom / 100d);
y = (point.Y + Offset.Y - viewport.Y) / (Zoom / 100d);

var size = World?.Size ?? Vector2Int32.Zero;
var size = World?.Size ?? Vector2Int32.One;
if (fitToBounds)
{
x = Math.Clamp(x, 0, size.X - 1);
Expand Down Expand Up @@ -1522,7 +1591,18 @@ public Point GetScaledPoint(int x, int y)
/// <returns>A <see cref="Point"/> which has been scaled to match the current zoom level</returns>
public Point GetScaledPoint(Point source)
{
return new(source.X * (Zoom / 100d) - Offset.X, source.Y * (Zoom / 100d) - Offset.Y);
var viewPortSize = Viewport;

double xOffset = HorizontalScrollBar.Value;
double yOffset = VerticalScrollBar.Value;

if (AutoCenter)
{
xOffset = (!IsHorizontalBarVisible ? (viewPortSize.Width - ScaledWidth) / 2 : 0);
yOffset = (!IsVerticalBarVisible ? (viewPortSize.Height - ScaledHeight) / 2 : 0);
}

return new(source.X * (Zoom / 100d) - xOffset, source.Y * (Zoom / 100d) - yOffset);
}

/// <summary>
Expand Down Expand Up @@ -1558,7 +1638,21 @@ public Rect GetScaledRectangle(Point location, Size size)
/// <returns>A <see cref="Rectangle"/> which has been scaled to match the current zoom level</returns>
public Rect GetScaledRectangle(Rect source)
{
return new(source.Left * (Zoom / 100d) - Offset.X, source.Top * (Zoom / 100d) - Offset.Y, source.Width * (Zoom / 100d), source.Height * (Zoom / 100d));
var viewPortSize = Viewport;
double xOffset = HorizontalScrollBar.Value;
double yOffset = VerticalScrollBar.Value;

if (AutoCenter)
{
xOffset = (!IsHorizontalBarVisible ? -(viewPortSize.Width - ScaledWidth) / 2 : xOffset);
yOffset = (!IsVerticalBarVisible ? -(viewPortSize.Height - ScaledHeight) / 2 : yOffset);
}

return new(
source.Left * (Zoom / 100d) - xOffset,
source.Top * (Zoom / 100d) - yOffset,
source.Width * (Zoom / 100d),
source.Height * (Zoom / 100d));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ namespace TEdit.Desktop.Controls.WorldRenderEngine;

public interface IRasterTileCache : IDisposable
{
Dictionary<SKPointI, RasterTile> Tiles { get; }
void AddOrUpdate(RasterTile tile);
void SetTile(RasterTile tile, int x, int y);
RasterTile? GetTile(int x, int y);
void Clear();

int TileSize { get; }

int TilesX { get; }
int TilesY { get; }
}
Loading

0 comments on commit 3030e42

Please sign in to comment.