Skip to content

Commit

Permalink
Completely remove Xe.Drawing dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Xeeynamo committed Jun 2, 2020
1 parent 95f14e6 commit 6abb324
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 55 deletions.
16 changes: 16 additions & 0 deletions OpenKh.Engine/Extensions/SpriteDrawingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,21 @@ public static void SetProjection(this ISpriteDrawing spriteDrawing,
var left = (internalWidth - width) / 2;
spriteDrawing.SetViewport(left, width + left, 0, height);
}

public static void FillRectangle(this ISpriteDrawing drawing, float x, float y, float width, float height, ColorF color)
{
drawing.AppendSprite(new SpriteDrawingContext()
.Source(0, 0, 1, 1)
.Position(x, y)
.DestinationSize(width, height));
}

public static void DrawRectangle(this ISpriteDrawing drawing, float x, float y, float width, float height, ColorF color, float thickness = 1.0f)
{
drawing.FillRectangle(x, y, width, thickness, color);
drawing.FillRectangle(x, y + height - 1, width + width - 1, thickness, color);
drawing.FillRectangle(x, y, thickness, height, color);
drawing.FillRectangle(x + width - 1, y, thickness, height, color);
}
}
}
3 changes: 3 additions & 0 deletions OpenKh.Engine/Renders/ISpriteDrawing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace OpenKh.Engine.Renders
{
public struct ColorF
{
public static readonly ColorF Black = new ColorF(0.0f, 0.0f, 0.0f, 1.0f);
public static readonly ColorF White = new ColorF(1.0f, 1.0f, 1.0f, 1.0f);

public float R, G, B, A;

public ColorF(float r, float g, float b, float a)
Expand Down
14 changes: 8 additions & 6 deletions OpenKh.Engine/Renders/Kh2MessageRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OpenKh.Imaging;
using OpenKh.Engine.Extensions;
using OpenKh.Imaging;
using OpenKh.Kh2;
using OpenKh.Kh2.Messages;
using System;
Expand Down Expand Up @@ -99,11 +100,12 @@ private void Draw(DrawContext context, MessageCommandModel command)
{
context.NewLine(_msgContext.FontHeight);
context.y += 4;
//_drawing.FillRectangle(new RectangleF(
// 8,
// (float)context.y,
// Math.Max(1.0f, (float)(context.WindowWidth - 16)),
// 2), Color.White);
_drawing.FillRectangle(
8,
(float)context.y,
Math.Max(1.0f, (float)(context.WindowWidth - 16)),
2,
ColorF.White);
context.y += 4;
}
else if (command.Command == MessageCommand.Position)
Expand Down
34 changes: 19 additions & 15 deletions OpenKh.Tools.CtdEditor/Interfaces/CtdDrawHandler.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using OpenKh.Bbs;
using OpenKh.Bbs.Messages;
using OpenKh.Tools.Common;
using OpenKh.Engine.Extensions;
using OpenKh.Engine.Renders;
using OpenKh.Tools.Common.Rendering;
using System.Drawing;
using System.Linq;
using Xe.Drawing;

namespace OpenKh.Tools.CtdEditor.Interfaces
{
Expand All @@ -14,10 +15,10 @@ public class CtdDrawHandler : IDrawHandler

public CtdDrawHandler()
{
DrawingContext = new DrawingDirect3D();
DrawingContext = new SpriteDrawingDirect3D();
}

public IDrawing DrawingContext { get; }
public ISpriteDrawing DrawingContext { get; }

public void DrawHandler(
ICtdMessageEncoder encoder,
Expand All @@ -32,8 +33,8 @@ public void DrawHandler(
int BeginY = layout.DialogY + layout.TextY;
var x = BeginX;
var y = BeginY;
var texture1 = DrawingContext.CreateSurface(fontContext.Image1);
var texture2 = DrawingContext.CreateSurface(fontContext.Image2);
var texture1 = DrawingContext.CreateSpriteTexture(fontContext.Image1);
var texture2 = DrawingContext.CreateSpriteTexture(fontContext.Image2);
foreach (var ch in encoder.ToUcs(message.Data))
{
if (ch >= 0x20)
Expand All @@ -55,7 +56,11 @@ public void DrawHandler(
Width = chInfo.Width,
Height = fontContext.Info.CharacterHeight
};
DrawingContext.DrawSurface(texture, source, x, y);
DrawingContext.AppendSprite(new SpriteDrawingContext()
.SpriteTexture(texture)
.Source(chInfo.PositionX, chInfo.PositionY, chInfo.Width, fontContext.Info.CharacterHeight)
.MatchSourceSize()
.Position(x, y));

x += source.Width + layout.HorizontalSpace;
}
Expand All @@ -73,14 +78,13 @@ public void DrawHandler(
}

private void DrawPspScreen() =>
DrawingContext.FillRectangle(new RectangleF(0, 0, PspScreenWidth, PspScreenHeight), Color.Black);
DrawingContext.FillRectangle(0, 0, PspScreenWidth, PspScreenHeight, ColorF.Black);

private void DrawDialog(Ctd.Layout layout) => DrawingContext.DrawRectangle(new RectangleF
{
X = layout.DialogX - 1,
Y = layout.DialogY - 1,
Width = layout.DialogWidth + 1,
Height = layout.DialogHeight + 1,
}, Color.Cyan);
private void DrawDialog(Ctd.Layout layout) => DrawingContext.DrawRectangle(
layout.DialogX - 1,
layout.DialogY - 1,
layout.DialogWidth + 1,
layout.DialogHeight + 1,
ColorF.FromRgba(Color.Cyan.ToArgb()));
}
}
4 changes: 2 additions & 2 deletions OpenKh.Tools.CtdEditor/Interfaces/IDrawHandler.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using OpenKh.Bbs;
using OpenKh.Bbs.Messages;
using Xe.Drawing;
using OpenKh.Engine.Renders;

namespace OpenKh.Tools.CtdEditor.Interfaces
{
public interface IDrawHandler
{
IDrawing DrawingContext { get; }
ISpriteDrawing DrawingContext { get; }

void DrawHandler(
ICtdMessageEncoder encoder,
Expand Down
1 change: 0 additions & 1 deletion OpenKh.Tools.CtdEditor/OpenKh.Tools.CtdEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<ProjectReference Include="..\OpenKh.Common\OpenKh.Common.csproj" />
<ProjectReference Include="..\OpenKh.Bbs\OpenKh.Bbs.csproj" />
<ProjectReference Include="..\OpenKh.Tools.Common\OpenKh.Tools.Common.csproj" />
<ProjectReference Include="..\XeEngine.Tools.Public\Xe.Drawing.Direct3D\Xe.Drawing.Direct3D.csproj" />
<ProjectReference Include="..\XeEngine.Tools.Public\Xe.Tools.Wpf\Xe.Tools.Wpf.csproj" />
<ProjectReference Include="..\XeEngine.Tools.Public\Xe.Tools\Xe.Tools.csproj" />
</ItemGroup>
Expand Down
42 changes: 16 additions & 26 deletions OpenKh.Tools.CtdEditor/ViewModels/FontEditorViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using OpenKh.Bbs;
using OpenKh.Tools.Common;
using OpenKh.Engine.Renders;
using OpenKh.Tools.Common.Rendering;
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Drawing;
using System.Linq;
using Xe.Drawing;
using Xe.Tools;
using Xe.Tools.Wpf.Commands;

Expand All @@ -16,8 +17,8 @@ public class FontEditorViewModel : BaseNotifyPropertyChanged
private CharacterViewModel[] _characters;
private FontEntryViewModel _selectedFont;
private CharacterViewModel _selectedCharacter;
private ISurface _surface1;
private ISurface _surface2;
private ISpriteTexture _surface1;
private ISpriteTexture _surface2;
private bool orderCharacters;

public FontEditorViewModel(FontsArc fonts)
Expand All @@ -32,7 +33,7 @@ public FontEditorViewModel(FontsArc fonts)
new FontEntryViewModel(fonts.FontNumeral)
};

DrawingContext = new DrawingDirect3D();
DrawingContext = new SpriteDrawingDirect3D();
DrawBegin = new RelayCommand(_ =>
{
if (_selectedFont?.Font1 == null ||
Expand All @@ -45,7 +46,7 @@ public FontEditorViewModel(FontsArc fonts)
if (_surface1 == null)
return;
DrawingContext.Clear(Color.Black);
DrawingContext.Clear(new ColorF(0.0f, 0.0f, 0.0f, 1.0f));
PrintCharacter(SelectedCharacter);
DrawingContext.Flush();
});
Expand Down Expand Up @@ -93,13 +94,13 @@ public bool OrderCharacters
}
}

public IDrawing DrawingContext { get; }
public ISpriteDrawing DrawingContext { get; }
public RelayCommand DrawBegin { get; }

private void CreateFontSurfaces()
{
_surface1 = DrawingContext.CreateSurface(_selectedFont.Font1);
_surface2 = DrawingContext.CreateSurface(_selectedFont.Font2);
_surface1 = DrawingContext.CreateSpriteTexture(_selectedFont.Font1);
_surface2 = DrawingContext.CreateSpriteTexture(_selectedFont.Font2);
}

private void DestroyFontSurfaces()
Expand All @@ -113,7 +114,7 @@ private void DestroyFontSurfaces()

private void PrintCharacter(CharacterViewModel characterViewModel)
{
ISurface surface;
ISpriteTexture surface;

switch (characterViewModel.Palette)
{
Expand All @@ -127,22 +128,11 @@ private void PrintCharacter(CharacterViewModel characterViewModel)
return;
}

var src = new Rectangle()
{
X = characterViewModel.PositionX,
Y = characterViewModel.PositionY,
Width = characterViewModel.Width,
Height = SelectedFont.Info.CharacterHeight,
};
var dst = new Rectangle()
{
X = 0,
Y = 0,
Width = src.Width * 2,
Height = src.Height * 2
};

DrawingContext.DrawSurface(surface, src, dst);
DrawingContext.AppendSprite(new SpriteDrawingContext()
.SpriteTexture(surface)
.Source(characterViewModel.PositionX, characterViewModel.PositionY, characterViewModel.Width, SelectedFont.Info.CharacterHeight)
.MatchSourceSize()
.ScaleSize(2));
}
}
}
3 changes: 2 additions & 1 deletion OpenKh.Tools.CtdEditor/ViewModels/MessageViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using OpenKh.Bbs;
using OpenKh.Bbs.Messages;
using OpenKh.Engine.Renders;
using OpenKh.Tools.CtdEditor.Interfaces;
using System.Linq;
using Xe.Drawing;
Expand Down Expand Up @@ -61,7 +62,7 @@ public string Text

public string Title => $"{Id}: {Text}";

public IDrawing DrawingContext => _drawHandler.DrawingContext;
public ISpriteDrawing DrawingContext => _drawHandler.DrawingContext;
public RelayCommand DrawHandler { get; }
public FontsArc.Font FontContext { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<ProjectReference Include="..\OpenKh.Common\OpenKh.Common.csproj" />
<ProjectReference Include="..\OpenKh.Kh2\OpenKh.Kh2.csproj" />
<ProjectReference Include="..\OpenKh.Tools.Common\OpenKh.Tools.Common.csproj" />
<ProjectReference Include="..\XeEngine.Tools.Public\Xe.Drawing.Direct3D\Xe.Drawing.Direct3D.csproj" />
<ProjectReference Include="..\XeEngine.Tools.Public\Xe.Tools.Wpf\Xe.Tools.Wpf.csproj" />
<ProjectReference Include="..\XeEngine.Tools.Public\Xe.Tools\Xe.Tools.csproj" />
</ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions OpenKh.Tools.Kh2TextEditor/ViewModels/TextEditorViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using OpenKh.Engine.Renders;
using OpenKh.Kh2;
using OpenKh.Kh2.Messages;
using OpenKh.Tools.Common.Rendering;
using OpenKh.Tools.Kh2TextEditor.Interfaces;
using OpenKh.Tools.Kh2TextEditor.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Xe.Drawing;
using Xe.Tools;

namespace OpenKh.Tools.Kh2TextEditor.ViewModels
Expand All @@ -33,7 +33,7 @@ public List<Msg.Entry> MessageEntries
}
}

public IDrawing Drawing { get; }
public ISpriteDrawing Drawing { get; }

public MessagesModel Messages
{
Expand Down Expand Up @@ -124,7 +124,7 @@ public bool ShowErrors

public TextEditorViewModel()
{
Drawing = new DrawingDirect3D();
Drawing = new SpriteDrawingDirect3D();
CurrentMessageEncoder = Encoders.InternationalSystem;
_messages = new MessagesModel(this, this, new Msg.Entry[] { });
}
Expand Down

0 comments on commit 6abb324

Please sign in to comment.