Skip to content

Commit

Permalink
refactor(Studio): Setup editor-rendering with Skia
Browse files Browse the repository at this point in the history
  • Loading branch information
psyGamer committed Oct 26, 2024
1 parent 904deb7 commit be58a60
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Studio/CelesteStudio.GTK/CelesteStudio.GTK.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

<!-- Use a CI-build, since that contains a fix for some issues -->
<PackageReference Include="Eto.Platform.Gtk" Version="2.8.4-ci-20240710.9879198421" />

<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.80.3" />
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions Studio/CelesteStudio.Mac/CelesteStudio.Mac.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

<!-- Use an CI-build, since it contains a fix for using context menu hotkeys inside the editor -->
<PackageReference Include="Eto.Platform.Mac64" Version="2.8.4-ci-20240710.9879198421" />

<PackageReference Include="SkiaSharp.NativeAssets.macOS" Version="2.80.3" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions Studio/CelesteStudio.WPF/CelesteStudio.WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
<ProjectReference Include="..\CelesteStudio\CelesteStudio.csproj"/>
<PackageReference Include="Eto.Platform.Wpf" Version="2.8.3"/>
<PackageReference Include="DarkNet" Version="2.3.0" />

<PackageReference Include="SkiaSharp.NativeAssets.Win32" Version="2.80.3" />
</ItemGroup>

</Project>
12 changes: 7 additions & 5 deletions Studio/CelesteStudio/CelesteStudio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Eto.Forms" Version="2.8.3" />
<PackageReference Include="Samboy063.Tomlet" Version="5.3.1" />
<PackageReference Include="Markdig" Version="0.37.0" />
<PackageReference Include="Eto.Forms" Version="2.8.3"/>
<PackageReference Include="Eto.SkiaDraw" Version="0.2.0"/>

<ProjectReference Include="..\..\StudioCommunication\StudioCommunication.csproj" />
<ProjectReference Include="..\..\external\Featherline\Featherline.csproj" />
<PackageReference Include="Samboy063.Tomlet" Version="5.3.1"/>
<PackageReference Include="Markdig" Version="0.37.0"/>

<ProjectReference Include="..\..\StudioCommunication\StudioCommunication.csproj"/>
<ProjectReference Include="..\..\external\Featherline\Featherline.csproj"/>
</ItemGroup>

<ItemGroup>
Expand Down
14 changes: 10 additions & 4 deletions Studio/CelesteStudio/Editing/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
using CelesteStudio.Util;
using Eto.Drawing;
using Eto.Forms;
using Eto.SkiaDraw;
using SkiaSharp;
using StudioCommunication;
using StudioCommunication.Util;
using WrapLine = (string Line, int Index);
using WrapEntry = (int StartOffset, (string Line, int Index)[] Lines);

namespace CelesteStudio.Editing;

public sealed class Editor : Drawable {
public sealed class Editor : SkiaDrawable {
/// Reports insertions and deletions of the underlying document
public event Action<Document, Dictionary<int, string>, Dictionary<int, string>> TextChanged = (_, _, _) => {};

Expand Down Expand Up @@ -3618,10 +3620,13 @@ private void UpdateMouseCursor(PointF location, Keys modifiers) {

#region Drawing

protected override void OnPaint(PaintEventArgs e) {
// Doing this in Recalc() seems to cause issues for some reason, but it needs to happen regularly
//Studio.Instance.GameInfoPanel.UpdateGameInfo();
protected override void OnPaint(SKPaintEventArgs e) {
e.Surface.Canvas.Clear();

var font = FontManager.SKEditorFontRegular;
e.Surface.Canvas.DrawText("Hello World", 10.0f, 10.0f, font, new SKPaint { Color = SKColors.Aqua, IsAntialias = true });

/*
e.Graphics.AntiAlias = true;
// To be reused below. Kinda annoying how C# handles out parameter conflicts
Expand Down Expand Up @@ -3896,6 +3901,7 @@ protected override void OnPaint(PaintEventArgs e) {
}
base.OnPaint(e);
*/
}

#endregion
Expand Down
23 changes: 23 additions & 0 deletions Studio/CelesteStudio/FontManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Linq;
using System.Reflection;
using Eto.Drawing;
using SkiaSharp;
using System;

namespace CelesteStudio;

Expand All @@ -15,6 +17,7 @@ public static class FontManager {
#endif

private static Font? editorFontRegular, editorFontBold, editorFontItalic, editorFontBoldItalic, statusFont, popupFont;
private static SKFont? skEditorFontRegular;

public static Font EditorFontRegular => editorFontRegular ??= CreateEditor(FontStyle.None);
public static Font EditorFontBold => editorFontBold ??= CreateEditor(FontStyle.Bold);
Expand All @@ -23,6 +26,8 @@ public static class FontManager {
public static Font StatusFont => statusFont ??= CreateStatus();
public static Font PopupFont => popupFont ??= CreatePopup();

public static SKFont SKEditorFontRegular => skEditorFontRegular ??= CreateSKFont(Settings.Instance.FontFamily, Settings.Instance.EditorFontSize * Settings.Instance.FontZoom);

private static FontFamily? builtinFontFamily;
public static Font CreateFont(string fontFamily, float size, FontStyle style = FontStyle.None) {
if (Platform.Instance.IsMac && fontFamily == FontFamilyBuiltin) {
Expand All @@ -42,6 +47,20 @@ public static Font CreateFont(string fontFamily, float size, FontStyle style = F
}
}

public static SKFont CreateSKFont(string fontFamily, float size) {
if (Platform.Instance.IsMac && fontFamily == FontFamilyBuiltin) {
// The built-in font is broken on macOS for some reason, so fallback to a system font
fontFamily = "Monaco";
}

if (fontFamily == FontFamilyBuiltin) {
using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("JetBrainsMono/JetBrainsMono-Regular");
return new SKFont(SKTypeface.FromStream(stream), size);
} else {
return new SKFont(SKTypeface.FromFamilyName(fontFamily), size);
}
}

private static readonly Dictionary<Font, float> charWidthCache = new();
public static float CharWidth(this Font font) {
if (charWidthCache.TryGetValue(font, out float width)) {
Expand Down Expand Up @@ -79,6 +98,10 @@ public static void OnFontChanged() {
charWidthCache.Clear();

editorFontRegular = editorFontBold = editorFontItalic = editorFontBoldItalic = statusFont = popupFont = null;

skEditorFontRegular?.Dispose();

skEditorFontRegular = null;
}

private static Font CreateEditor(FontStyle style) => CreateFont(Settings.Instance.FontFamily, Settings.Instance.EditorFontSize * Settings.Instance.FontZoom, style);
Expand Down

0 comments on commit be58a60

Please sign in to comment.