Skip to content

Commit

Permalink
rendering using skia
Browse files Browse the repository at this point in the history
  • Loading branch information
Walper committed Nov 24, 2023
1 parent 71d236f commit a47e871
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 161 deletions.
65 changes: 0 additions & 65 deletions LanguageParser.Tests/tests/test1.ll.c

This file was deleted.

36 changes: 32 additions & 4 deletions SimpleExecutor/Models/Executor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Avalonia;
using Avalonia.Media;
using ReactiveUI;
Expand All @@ -8,17 +9,39 @@ namespace SimpleExecutor.Models;

public sealed class Executor : ViewModelBase
{
private Point _position;
private double _angle;
private double _pixelHeight;
private double _pixelWidth;
private Point _position;
private IImmutableSolidColorBrush _background = Brushes.White;
public List<(Point point, IImmutableSolidColorBrush brush)> Trace { get; } = new();

public double PixelWidth
{
get => _pixelWidth;
set => this.RaiseAndSetIfChanged(ref _pixelWidth, value);
}

public double PixelHeight
{
get => _pixelHeight;
set => this.RaiseAndSetIfChanged(ref _pixelHeight, value);
}

public IImmutableSolidColorBrush Background
{
get => _background;
set => this.RaiseAndSetIfChanged(ref _background, value);
}

public IImmutableSolidColorBrush TraceColor { get; set; } = Brushes.Blue;

public IBrush TraceColor { get; set; } = Brushes.Blue;

public Point Position
{
get => _position;
set => this.RaiseAndSetIfChanged(ref _position, value);
}

public double Angle
{
get => _angle;
Expand All @@ -30,6 +53,8 @@ public void Move(double length)
var direction = new Vector(-Math.Sin(Angle * Math.PI / 180), Math.Cos(Angle * Math.PI / 180));

Position += direction * length;

Trace.Add((new Point(Position.X, Position.Y), TraceColor));
}

public void Rotate(double angle)
Expand All @@ -41,12 +66,15 @@ public void Rotate(double angle)

public void Reset()
{
Trace.Clear();
Position = default;
Angle = 0;
}

public void Jump(double x, double y)
{
Position = new Point(x, y);

Trace.Add((new Point(Position.X, Position.Y), TraceColor));
}
}
48 changes: 48 additions & 0 deletions SimpleExecutor/Models/SkiaCanvasDrawOperation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using Avalonia;
using Avalonia.Media;
using Avalonia.Platform;
using Avalonia.Rendering.SceneGraph;
using Avalonia.Skia;
using SkiaSharp;

namespace SimpleExecutor.Models;

internal sealed class SkiaCanvasDrawOperation : ICustomDrawOperation
{
public Action<SKCanvas, SKSurface>? OnRender;

public bool Equals(ICustomDrawOperation? other)
{
return other == this;
}

public void Dispose()
{
// do nothing
}

public bool HitTest(Point p)
{
return true;
}

public void Render(ImmediateDrawingContext context)
{
var leaseFeature = context.TryGetFeature<ISkiaSharpApiLeaseFeature>();
if (leaseFeature == null)
return;

using var lease = leaseFeature.Lease();
var canvas = lease.SkCanvas;

Render(canvas, lease.SkSurface!);
}

public Rect Bounds { get; set; }

private void Render(SKCanvas canvas, SKSurface surface)
{
OnRender?.Invoke(canvas, surface);
}
}
77 changes: 71 additions & 6 deletions SimpleExecutor/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using System.Windows.Input;
using Avalonia.Media;
using Avalonia.Threading;
using LanguageInterpreter;
using LanguageParser.Common;
using ReactiveUI;
Expand Down Expand Up @@ -35,32 +36,39 @@ public MainViewModel()
var moveFunction = new Function("move", new[] { new Variable("length", typeof(double)) },
args =>
{
Executor.Move((double)args[0]);
Dispatcher.UIThread.Invoke(() => Executor.Move((double)args[0]));
Task.Delay(StepDuration).Wait();
});

var rotateFunction = new Function("rotate", new[] { new Variable("angle", typeof(double)) },
args =>
{
Executor.Rotate((double)args[0]);
Dispatcher.UIThread.Invoke(() => Executor.Rotate((double)args[0]));
Task.Delay(StepDuration).Wait();
});

var resetFunction = new Function("reset", Array.Empty<Variable>(),
_ =>
{
Executor.Reset();
Dispatcher.UIThread.Invoke(() => Executor.Reset());
Task.Delay(StepDuration).Wait();
});

var jumpFunction = new Function("jump",
new[] { new Variable("x", typeof(double)), new Variable("y", typeof(double)) },
args =>
{
Executor.Jump((double)args[0], (double)args[1]);
Dispatcher.UIThread.Invoke(() => Executor.Jump((double)args[0], (double)args[1]));
Task.Delay(StepDuration).Wait();
});

var widthFunction = new Function<double>("getWidth", Array.Empty<Variable>(), args => Executor.PixelWidth);

var heightFunction = new Function<double>("getHeight", Array.Empty<Variable>(), args => Executor.PixelHeight);

var timeFunction =
new Function<double>("getTime", Array.Empty<Variable>(), args => DateTime.UtcNow.Millisecond);

var delayFunction = new Function("delay", new[] { new Variable("milliseconds", typeof(double)) }, args =>
{
var time = (double)args[0];
Expand All @@ -73,9 +81,9 @@ public MainViewModel()

var setColorFunction = new Function("setColor", new[] { new Variable("color", typeof(string)) }, args =>
{
var brush = Brush.Parse((string)args[0]);
var brush = (ISolidColorBrush)Brush.Parse((string)args[0]);

Executor.TraceColor = brush;
Executor.TraceColor = (IImmutableSolidColorBrush)brush.ToImmutable();
});

TokensSyntaxColorizer = new TokensSyntaxColorizer(this);
Expand All @@ -93,6 +101,9 @@ public MainViewModel()
.WithPredefinedFunction(delayFunction)
.WithPredefinedFunction(setStepDurationFunction)
.WithPredefinedFunction(setColorFunction)
.WithPredefinedFunction(widthFunction)
.WithPredefinedFunction(heightFunction)
.WithPredefinedFunction(timeFunction)
.Build();
}

Expand Down Expand Up @@ -196,4 +207,58 @@ public string Output
// rotate(90)
// size = size + 5
// }
// }

// reset();
// number sunX = getWidth() / 2;
// number sunY = getHeight() / 2;
//
// // Этот странный код для того, чтобы нарисовать "солнце"
// setColor('transparent');
// jump(sunX - 0.5, sunY - 0.5);
//
// setColor('orange');
// for(number i = 0; i < 60; i = i + 1)
// {
// move(1);
// rotate(6)
// }
// setColor('transparent');
// move(0);
//
// // Проинициализировали параметры
// number sunWeight = 100;
//
// number x = sunX;
// number y = 20;
//
// number planetWeight = 1;
//
// number velocityX = 0-0.25;
// number velocityY = 0-0.1;
//
// // Поместили "планету" на место
// setColor('transparent');
// jump(x,y);
//
// number deltaTime = 10;
//
// setStepDuration(deltaTime);
// setColor('blue');
//
// // Основной цикл, тут все считается
// while(true)
// {
// number rX = x - sunX;
// number rY = y - sunY;
//
// number r = sqrt(rX * rX + rY * rY);
//
// velocityX = velocityX - (sunWeight * rX / (r * r * r)) * deltaTime;
// velocityY = velocityY - (sunWeight * rY / (r * r * r)) * deltaTime;
//
// x = x + velocityX * deltaTime;
// y = y + velocityY * deltaTime;
//
// jump(x, y);
// }
26 changes: 0 additions & 26 deletions SimpleExecutor/Views/DrawingCanvas.axaml

This file was deleted.

Loading

0 comments on commit a47e871

Please sign in to comment.