Skip to content

Commit

Permalink
Add Zoom and Pan.
Browse files Browse the repository at this point in the history
  • Loading branch information
TopazTK committed Sep 11, 2023
1 parent ca10e0b commit 9016799
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 18 deletions.
7 changes: 5 additions & 2 deletions OpenKh.Engine/Renders/LayoutRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using OpenKh.Engine.Renders;
using OpenKh.Engine.Renders;
using OpenKh.Kh2;
using OpenKh.Kh2.Extensions;
using System;
Expand All @@ -15,6 +15,9 @@ public class LayoutRenderer
private int selectedSequenceGroupIndex;
private IDebugLayoutRenderer _debugLayoutRenderer;

public int PanFactorX;
public int PanFactorY;

public int SelectedSequenceGroupIndex
{
get => selectedSequenceGroupIndex;
Expand Down Expand Up @@ -68,7 +71,7 @@ private void DrawLayout(Layout.SequenceProperty l1)
var sequence = layout.SequenceItems[l1.SequenceIndex];
var surface = surfaces[l1.TextureIndex];
var sequenceRenderer = new SequenceRenderer(sequence, drawing, surface);
sequenceRenderer.Draw(l1.AnimationGroup, currentFrameIndex, l1.PositionX, l1.PositionY);
sequenceRenderer.Draw(l1.AnimationGroup, currentFrameIndex, l1.PositionX + PanFactorX, l1.PositionY + PanFactorY);
}
}
}
8 changes: 5 additions & 3 deletions OpenKh.Engine/Renders/SequenceRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public enum TextAnchor

public class SequenceRenderer
{
public int PanFactorX;
public int PanFactorY;
public class ChildContext
{
public float PositionX { get; set; }
Expand All @@ -30,7 +32,7 @@ public class ChildContext
public float UiPadding { get; set; }
}

private class Context
public class Context
{
public int GlobalFrameIndex { get; set; }
public int FrameIndex { get; set; }
Expand Down Expand Up @@ -93,8 +95,8 @@ public bool Draw(int animationGroupIndex, int frameIndex, float positionX, float
{
GlobalFrameIndex = frameIndex,
FrameIndex = frameIndex,
PositionX = positionX,
PositionY = positionY,
PositionX = positionX + PanFactorX,
PositionY = positionY + PanFactorY,
Color = new ColorF(1f, 1f, 1f, alpha)
}, Sequence.AnimationGroups[animationGroupIndex]);

Expand Down
51 changes: 42 additions & 9 deletions OpenKh.Tools.LayoutEditor/AppLayoutEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using static OpenKh.Tools.Common.CustomImGui.ImGuiEx;
using OpenKh.Engine.Extensions;
using OpenKh.Engine.Renders;
Expand All @@ -33,6 +34,16 @@ public class AppLayoutEditor : IApp, ISaveBar, ITextureBinder, IDisposable
private readonly List<ISpriteTexture> _spriteTextures;
private readonly LayoutRenderer _renderer;

private float ZoomFactor = 1;

private float PanFactorX = 0;
private float PanFactorY = 0;

private int PastScrollValue = 0;

private float PastMouseX = 0;
private float PastMouseY = 0;

private int _selectedLayoutIndex;
private int _animationFrameCurrent;
private bool _isOpeningSequenceEditor;
Expand Down Expand Up @@ -68,7 +79,7 @@ public AppLayoutEditor(
_graphics = bootstrap.GraphicsDevice;
_shader = new KingdomShader(bootstrap.Content);
_drawing = new MonoSpriteDrawing(_graphics, _shader);
_destinationTexture = _drawing.CreateSpriteTexture(1024, 1024);
_destinationTexture = _drawing.CreateSpriteTexture(2048, 2048);
_destinationTextureId = this.BindTexture(_destinationTexture);

_debugRender = new DefaultDebugLayoutRenderer();
Expand Down Expand Up @@ -154,10 +165,10 @@ private void LayoutPreview()

ForChild("Preview", 0, 0, false, () =>
{
const float PositionX = 128f;
const float PositionY = 32f;
const float ViewportWidth = 1024f;
const float ViewportHeight = 1024f;
float PositionX = 128f;
float PositionY = 32f;
const float ViewportWidth = 2048;
const float ViewportHeight = 2048;
var width = ImGui.GetWindowContentRegionMax().X;
var height = ImGui.GetWindowHeight();
var backgroundColorInverse = new ColorF(
Expand All @@ -168,9 +179,31 @@ private void LayoutPreview()
_drawing.DestinationTexture = _destinationTexture;
_drawing.Clear(_settings.EditorBackground);
_drawing.SetViewport(-PositionX, ViewportWidth - PositionX, -PositionY, ViewportHeight - PositionY);
if (Mouse.GetState().ScrollWheelValue - PastScrollValue > 0)
ZoomFactor -= 0.05F;
if (Mouse.GetState().ScrollWheelValue - PastScrollValue < 0)
ZoomFactor += 0.05F;
PastScrollValue = Mouse.GetState().ScrollWheelValue;
if (Keyboard.GetState().IsKeyDown(Keys.Right))
_renderer.PanFactorX += 5;
if (Keyboard.GetState().IsKeyDown(Keys.Left))
_renderer.PanFactorX -= 5;
if (Keyboard.GetState().IsKeyDown(Keys.Up))
_renderer.PanFactorY -= 5;
if (Keyboard.GetState().IsKeyDown(Keys.Down))
_renderer.PanFactorY += 5;
_drawing.SetViewport(-PositionX, ViewportWidth - PositionX, -PositionY, ViewportHeight - PositionY);
_drawing.SetProjection(ViewportWidth - PositionX, ViewportHeight - PositionY, (ViewportWidth - PositionX) * ZoomFactor, (ViewportHeight - PositionY) * ZoomFactor, 1);
_renderer.FrameIndex = _animationFrameCurrent;
_renderer.SelectedSequenceGroupIndex = SelectedLayoutIndex;
Expand All @@ -197,12 +230,12 @@ private void DrawGameViewport(ColorF backgroundColorInverse)
const float ViewportHeight = 416f;

if (_settings.ShowViewportOriginal)
_drawing.DrawRectangle(-1, -1, OriginalViewportWidth + 2, ViewportHeight + 2, backgroundColorInverse);
_drawing.DrawRectangle(-1 + _renderer.PanFactorX, -1 + _renderer.PanFactorY, OriginalViewportWidth + 2, ViewportHeight + 2, backgroundColorInverse, 1);

if (_settings.ShowViewportRemix)
_drawing.DrawRectangle(
-(RemixViewportWidth - OriginalViewportWidth) / 2 - 1, -1,
RemixViewportWidth + 2, ViewportHeight + 2, backgroundColorInverse);
-(RemixViewportWidth - OriginalViewportWidth) / 2 - 1 + _renderer.PanFactorX, -1 + _renderer.PanFactorY,
RemixViewportWidth + 2, ViewportHeight + 2, backgroundColorInverse, 1);

_drawing.Flush();
}
Expand Down
44 changes: 40 additions & 4 deletions OpenKh.Tools.LayoutEditor/AppSequenceEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using OpenKh.Tools.LayoutEditor.Interfaces;
using System.Numerics;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using OpenKh.Tools.Common.CustomImGui;
using OpenKh.Engine.Extensions;
using OpenKh.Engine.MonoGame;
Expand Down Expand Up @@ -47,6 +48,17 @@ public class AppSequenceEditor : IApp, ISaveBar, ITextureBinder, IDisposable
int _sequencerSelectedAnimation = 0;
int _sequencerFirstFrame = 0;


private float ZoomFactor = 1;

private float PanFactorX = 0;
private float PanFactorY = 0;

private int PastScrollValue = 0;

private float PastMouseX = 0;
private float PastMouseY = 0;

private bool _isSpriteEditDialogOpen;
private SpriteEditDialog _spriteEditDialog;
private string SpriteEditDialogTitle => $"Sprite edit";
Expand Down Expand Up @@ -252,8 +264,8 @@ private unsafe void DrawAnimation()

ForChild("Preview", 0, 0, false, () =>
{
const float ViewportWidth = 1024f;
const float ViewportHeight = 1024f;
const float ViewportWidth = 2048;
const float ViewportHeight = 2048;
const float Infinite = 65536f;
var width = ImGui.GetWindowContentRegionMax().X;
var height = ImGui.GetWindowHeight();
Expand All @@ -270,11 +282,35 @@ private unsafe void DrawAnimation()
_drawing.SetViewport(0, ViewportWidth, 0, ViewportHeight);
// This draws the origin
_drawing.FillRectangle(originX - 1, 0, 1, Infinite, backgroundColorInverse);
_drawing.FillRectangle(0, originY - 1, Infinite, 1, backgroundColorInverse);
_drawing.FillRectangle(originX - 1 + _renderer.PanFactorX, -2048 + _renderer.PanFactorX, 1, Infinite, backgroundColorInverse);
_drawing.FillRectangle(0 + _renderer.PanFactorX - 2048, originY - 1 + _renderer.PanFactorY, Infinite, 1, backgroundColorInverse);
if (Mouse.GetState().ScrollWheelValue - PastScrollValue > 0)
ZoomFactor -= 0.05F;
if (Mouse.GetState().ScrollWheelValue - PastScrollValue < 0)
ZoomFactor += 0.05F;
PastScrollValue = Mouse.GetState().ScrollWheelValue;
if (Keyboard.GetState().IsKeyDown(Keys.Right))
_renderer.PanFactorX += 5;
if (Keyboard.GetState().IsKeyDown(Keys.Left))
_renderer.PanFactorX -= 5;
if (Keyboard.GetState().IsKeyDown(Keys.Up))
_renderer.PanFactorY -= 5;
if (Keyboard.GetState().IsKeyDown(Keys.Down))
_renderer.PanFactorY += 5;
_drawing.SetProjection(ViewportWidth, ViewportHeight, 1024 * ZoomFactor, 1024 * ZoomFactor, 1);
_drawing.Flush();
_renderer.Draw(_selectedAnimGroup, _animationFrameCurrent, originX, originY);
_drawing.Flush();
_drawing.DestinationTexture = null;
Expand Down

0 comments on commit 9016799

Please sign in to comment.