Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions src.csharp/AlphaTab.Test/VisualTests/VisualTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,27 +186,12 @@ private static void CompareVisualResult(double totalWidth, double totalHeight,
(int) totalHeight,
SKImageInfo.PlatformColorType, SKAlphaType.Premul)))
{
var point = new SKPoint();
var rowHeight = 0;
foreach (var partialResult in result)
{
var partialCanvas = partialResult.RenderResult;
if (partialCanvas is SKImage img)
{
finalImageSurface.Canvas.DrawImage(img, point);
if (partialResult.Height > rowHeight)
{
rowHeight = img.Height;
}

point.X += img.Width;

if (point.X >= totalWidth)
{
point.X = 0;
point.Y += rowHeight;
rowHeight = 0;
}
finalImageSurface.Canvas.DrawImage(img, (float)partialResult.X, (float)partialResult.Y);
}
}

Expand Down
41 changes: 0 additions & 41 deletions src.csharp/AlphaTab.Windows/WinForms/AlphaTabLayoutPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,10 @@ namespace AlphaTab.WinForms
{
internal class AlphaTabLayoutPanel : Panel
{
private AlphaTabLayoutEngine? _laoyutEngine;

public override LayoutEngine LayoutEngine =>
_laoyutEngine ??= new AlphaTabLayoutEngine();

public AlphaTabLayoutPanel()
{
base.DoubleBuffered = true;
ResizeRedraw = true;
}

private class AlphaTabLayoutEngine : LayoutEngine
{
public override bool Layout(object container, LayoutEventArgs layoutEventArgs)
{
var parent = (Control) container;

var xChild = 0;
var yChild = 0;

var rowHeight = 0;

foreach (Control? child in parent.Controls)
{
if (child != null)
{
child.Location = new Point(xChild, yChild);

xChild += child.Width;
if (child.Height > rowHeight)
{
rowHeight = child.Height;
}

if (xChild >= parent.Width)
{
xChild = 0;
yChild += rowHeight;
rowHeight = 0;
}
}
}

return false;
}
}
}
}
131 changes: 78 additions & 53 deletions src.csharp/AlphaTab.Windows/WinForms/WinFormsUiFacade.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
Expand All @@ -14,6 +15,10 @@ namespace AlphaTab.WinForms
internal class WinFormsUiFacade : ManagedUiFacade<AlphaTabControl>
{
private readonly AlphaTabLayoutPanel _layoutPanel;

private readonly Dictionary<string, PictureBox> _resultIdToElementLookup =
new Dictionary<string, PictureBox>();

private event Action? InternalRootContainerBecameVisible;

public override IContainer RootContainer { get; }
Expand Down Expand Up @@ -67,7 +72,7 @@ void OnVisibilityChanged(object? sender, EventArgs e)
protected override Stream? OpenDefaultSoundFont()
{
return typeof(NAudioSynthOutput).Assembly.GetManifestResourceStream(
typeof(NAudioSynthOutput), "default.sf2");
typeof(NAudioSynthOutput), "default.sf2");
}

public override void Initialize(AlphaTabApiBase<AlphaTabControl> api,
Expand Down Expand Up @@ -106,14 +111,57 @@ public override IContainer CreateCanvasElement()
return new ControlContainer(_layoutPanel);
}

public override void InitialRender()
{
Api.Renderer.PreRender.On(_ => { _resultIdToElementLookup.Clear(); });
base.InitialRender();
}

public override void TriggerEvent(IContainer container, string eventName,
object? details = null, IMouseEventArgs? originalEvent = null)
{
}

public override void BeginUpdateRenderResults(RenderFinishedEventArgs? r)
{
SettingsContainer.BeginInvoke((Action<RenderFinishedEventArgs?>)(renderResult =>
{

if (renderResult == null ||
!_resultIdToElementLookup.TryGetValue(renderResult.Id, out var placeholder))
{
return;
}

var body = renderResult.RenderResult;

Bitmap? source = null;
switch (body)
{
case string _:
// TODO: svg support
return;
case SKImage skiaImage:
using (skiaImage)
{
source = SkiaUtil.ToBitmap(skiaImage);
}

break;
case Bitmap image:
source = image;
break;
}

var oldImage = placeholder.Image;
placeholder.Image = source;
oldImage?.Dispose();
}), r);
}

public override void BeginAppendRenderResults(RenderFinishedEventArgs? r)
{
SettingsContainer.BeginInvoke((Action<RenderFinishedEventArgs>) (renderResult =>
SettingsContainer.BeginInvoke((Action<RenderFinishedEventArgs?>)(renderResult =>
{
var panel = _layoutPanel;

Expand All @@ -134,58 +182,35 @@ public override void BeginAppendRenderResults(RenderFinishedEventArgs? r)
// NOTE: here we try to replace existing children
else
{
var body = renderResult.RenderResult;

Bitmap? source = null;
if (body is string)
{
// TODO: svg support
return;
}

if (body is SKImage skiaImage)
if (TotalResultCount.TryPeek(out var counter))
{
using (skiaImage)
PictureBox placeholder;
if (counter.Count < panel.Controls.Count)
{
source = SkiaUtil.ToBitmap(skiaImage);
placeholder = (PictureBox)panel.Controls[counter.Count];
}
}
else if (body is Bitmap image)
{
source = image;
}

if (source != null)
{
if (TotalResultCount.TryPeek(out var counter))
else
{
if (counter.Count < panel.Controls.Count)
{
var img = (PictureBox) panel.Controls[counter.Count];
img.Width = (int) renderResult.Width;
img.Height = (int) renderResult.Height;
var oldImg = img.Image;
img.Image = source;
oldImg?.Dispose();
}
else
placeholder = new PictureBox
{
var img = new PictureBox
{
AutoSize = false,
BackColor = _layoutPanel.ForeColor,
Width = (int) renderResult.Width,
Height = (int) renderResult.Height,
Image = source,
Padding = Padding.Empty,
Margin = Padding.Empty,
BorderStyle = BorderStyle.None
};
panel.Controls.Add(img);
}

counter.Count++;
AutoSize = false,
BackColor = _layoutPanel.ForeColor,
Padding = Padding.Empty,
Margin = Padding.Empty,
BorderStyle = BorderStyle.None
};
panel.Controls.Add(placeholder);
}

placeholder.Left = (int)renderResult.X;
placeholder.Top = (int)renderResult.Y;
placeholder.Width = (int)renderResult.Width;
placeholder.Height = (int)renderResult.Height;

_resultIdToElementLookup[renderResult.Id] = placeholder;
Api.Renderer.RenderResult(renderResult.Id);

counter.Count++;
}
}
}), r);
Expand Down Expand Up @@ -228,7 +253,7 @@ public override IContainer GetScrollContainer()

public override Bounds GetOffset(IContainer? relativeTo, IContainer container)
{
var containerWinForms = ((ControlContainer) container).Control;
var containerWinForms = ((ControlContainer)container).Control;

var left = 0;
var top = 0;
Expand All @@ -252,14 +277,14 @@ public override Bounds GetOffset(IContainer? relativeTo, IContainer container)

public override void ScrollToY(IContainer scrollElement, double offset, double speed)
{
var c = ((ControlContainer) scrollElement).Control;
c.AutoScrollOffset = new Point(c.AutoScrollOffset.X, (int) offset);
var c = ((ControlContainer)scrollElement).Control;
c.AutoScrollOffset = new Point(c.AutoScrollOffset.X, (int)offset);
}

public override void ScrollToX(IContainer scrollElement, double offset, double speed)
{
var c = ((ControlContainer) scrollElement).Control;
c.AutoScrollOffset = new Point((int) offset, c.AutoScrollOffset.Y);
var c = ((ControlContainer)scrollElement).Control;
c.AutoScrollOffset = new Point((int)offset, c.AutoScrollOffset.Y);
}
}
}
53 changes: 0 additions & 53 deletions src.csharp/AlphaTab.Windows/Wpf/AlphaTabLayoutPanel.cs

This file was deleted.

5 changes: 3 additions & 2 deletions src.csharp/AlphaTab.Windows/Wpf/GdiImageSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ internal static class GdiImageSource
{
public static BitmapSource Create(Bitmap image)
{
var bitmapData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly,
var bitmapData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
ImageLockMode.ReadOnly,
image.PixelFormat);

var bitmapSource = BitmapSource.Create(
bitmapData.Width, bitmapData.Height, 96, 96, PixelFormats.Pbgra32, null,
bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, bitmapData.Stride);

image.UnlockBits(bitmapData);

bitmapSource.Freeze();
return bitmapSource;
}
}
Expand Down
1 change: 1 addition & 0 deletions src.csharp/AlphaTab.Windows/Wpf/SkImageSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static BitmapSource Create(object data)
}
bitmap.AddDirtyRect(new Int32Rect(0, 0, info.Width, info.Height));
bitmap.Unlock();
bitmap.Freeze();
return bitmap;
}
}
Expand Down
Loading