Skip to content

Commit

Permalink
Merge branch 'feature/level-zoom'
Browse files Browse the repository at this point in the history
  • Loading branch information
maxim-zhao committed Oct 15, 2024
2 parents af3c591 + 2a991d6 commit 3bdfdaf
Show file tree
Hide file tree
Showing 4 changed files with 299 additions and 64 deletions.
126 changes: 113 additions & 13 deletions editor/Controls/FloorEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@ public sealed partial class FloorEditor : ScrollableControl
private Palette _palette;
private BlockMapping _blockMapping;
private LevelObjectSet _objects;
private int _blockSize;
private int _width;
private int _height;
private int _tileSize;
private int _blockSize; // The size of one block in screen pixels
private int _width; // Data width in blocks
private int _height; // Data height in blocks
private int _tileSize; // The size of one tile in screen pixels
private int _zoom; // Scaling factor for zooming
private TileSet _tileSet;
private Level _level;
private bool _levelBounds;
private bool _withObjects;
private bool _blockNumbers;
private bool _blockGaps;
private bool _tileGaps;
private bool _doDraw = true;

public FloorEditor()
{
Expand All @@ -35,6 +37,8 @@ public FloorEditor()
Paint += OnPaint;
MouseDown += OnMouseDown;
MouseMove += OnMouseMove;
// Default to the nearest zoom level to the screen scaling factor
_zoom = (int)Math.Round(DeviceDpi / 96.0);
}

public void SetData(Level level)
Expand Down Expand Up @@ -65,23 +69,38 @@ private void UpdateSize()
return;
}
// Set bounds for scrolling
_tileSize = 8 + (TileGaps ? 1 : 0);
_tileSize = (8 * _zoom) + (TileGaps ? 1 : 0);
_blockSize = _tileSize * 4 + (BlockGaps ? 1 : 0);
AutoScrollMinSize = new Size(_width * _blockSize, _height * _blockSize);
}

private void OnPaint(object sender, PaintEventArgs e)
{
if (!_doDraw)
{
// We have suspended drawing to reduce flicker
return;
}
// We apply the scroll offset to the graphics to make it draw in the right place
e.Graphics.TranslateTransform(AutoScrollPosition.X, AutoScrollPosition.Y);
Draw(e.Graphics, e.ClipRectangle);
}

public void Draw(Graphics g, Rectangle clipRectangle)
{
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;

using var f = new Font(SystemFonts.MessageBoxFont.FontFamily, 8.0f);
// Figure out the font size
var defaultFont = SystemFonts.MessageBoxFont ?? SystemFonts.DefaultFont;
var size = g.MeasureString("0", defaultFont);
var fontScale = 1.0f;
if (size.Height > _blockSize * 0.5)
{
fontScale = _blockSize * 0.5f/ size.Height;
}

using var f = new Font((SystemFonts.MessageBoxFont ?? SystemFonts.DefaultFont).FontFamily, defaultFont.SizeInPoints * fontScale);
g.Clear(SystemColors.Window);

if (_level == null)
Expand Down Expand Up @@ -110,7 +129,8 @@ public void Draw(Graphics g, Rectangle clipRectangle)
{
var x = blockX * _blockSize + tileX * _tileSize;
var y = blockY * _blockSize + tileY * _tileSize;
g.DrawImageUnscaled(_tileSet.GetImageWithRings(tileIndex, _palette), x, y);
var tile = _tileSet.GetImageWithRings(tileIndex, _palette);
g.DrawImage(tile, x, y, (tile.Width * _zoom), (tile.Height * _zoom));
}
}
}
Expand All @@ -136,9 +156,11 @@ void DrawObject(int x, int y, string label)
{
x *= _blockSize;
y *= _blockSize;
var zoomedWidth = image.Width * _zoom;
var zoomedHeight = image.Height * _zoom;
g.DrawRectangle(Pens.Blue, x, y, _blockSize, _blockSize);
g.DrawImageUnscaled(image, x + _blockSize / 2 - image.Width / 2,
y + _blockSize / 2 - image.Height / 2);
g.DrawImage(image, x + _blockSize / 2 - zoomedWidth / 2,
y + _blockSize / 2 - zoomedHeight / 2, zoomedWidth, zoomedHeight);

var dims = g.MeasureString(label, f).ToSize();

Expand Down Expand Up @@ -166,10 +188,11 @@ void DrawObject(int x, int y, string label)

if (LevelBounds)
{
var left = _level.LeftPixels + _level.LeftPixels / 32 * (_blockSize - 32) + 8;
var top = _level.TopPixels + _level.TopPixels / 32 * (_blockSize - 32);
var right = _level.RightEdgeFactor * _blockSize * 8 + 256 / 32 * _blockSize;
var bottom = _level.BottomEdgeFactor * _blockSize * 8 + 192 / 32 * _blockSize + _level.ExtraHeight;
// Compute the bounds in pixel space first...
var left = PixelToScreen(_level.LeftPixels);
var top = PixelToScreen(_level.TopPixels);
var right = PixelToScreen((_level.RightEdgeFactor * 8 + 14) * 32);
var bottom = PixelToScreen((_level.BottomEdgeFactor * 8 + 6) * 32 + _level.ExtraHeight);
var rect = new Rectangle(left, top, right - left, bottom - top);
// Draw the grey region
using var brush = new SolidBrush(Color.FromArgb(128, Color.Black));
Expand All @@ -182,6 +205,28 @@ void DrawObject(int x, int y, string label)
}
}

private int PixelToScreen(int i)
{
// First compute the block and tile offsets
var block = i / 32;
i %= 32;
var tile = i / 8;
var pixels = i % 8;
// Then add them back together based on the current draw state
return block * _blockSize + tile * _tileSize + pixels * _zoom;
}

private int ScreenToPixel(int i)
{
// First compute the block and tile offsets
var block = i / _blockSize;
i %= _blockSize;
var tile = i / _tileSize;
var pixels = i / _tileSize / _zoom;
// Then add them back together based on the current draw state
return block * 32 + tile * 8 + pixels;
}

private void OnMouseDown(object sender, MouseEventArgs e)
{
LastClickedBlockIndex = GetClickedBlockIndex(e.X, e.Y);
Expand Down Expand Up @@ -305,6 +350,55 @@ private void OnMouseMove(object sender, MouseEventArgs e)
SetBlockIndex(index, BlockChooser.SelectedIndex);
}

protected override void OnMouseWheel(MouseEventArgs e)
{
if ((ModifierKeys & Keys.Control) != 0)
{
// Ctrl key pressed allows zooming
var newZoom = _zoom + (e.Delta > 0 ? 1 : -1);
ChangeZoom(newZoom, e.X, e.Y);
}
else
{
base.OnMouseWheel(e);
}
}

private void ChangeZoom(int newZoom, int screenX, int screenY)
{
newZoom = Math.Clamp(newZoom, 1, 10);
if (newZoom == _zoom)
{
// No change
return;
}

if (_level == null)
{
// Nothing to do, just accept it
_zoom = newZoom;
return;
}

// Remember the scroll position in terms of the mouse position
var x = ScreenToPixel(screenX - AutoScrollPosition.X);
var y = ScreenToPixel(screenY - AutoScrollPosition.Y);

_zoom = newZoom;

_doDraw = false;
UpdateSize();

// Then restore the position post-zoom
x = PixelToScreen(x) - screenX;
y = PixelToScreen(y) - screenY;
AutoScrollPosition = new Point(x, y);

_doDraw = true;
Invalidate();
FloorChanged?.Invoke();
}

public bool LevelBounds
{
get => _levelBounds;
Expand Down Expand Up @@ -347,6 +441,12 @@ public enum Modes
public Modes DrawingMode { get; set; }
public int LastClickedBlockIndex { get; set; }

public int Zoom
{
get => _zoom;
set => ChangeZoom(value, Width / 2, Height / 2);
}

public event Action FloorChanged;
}
}
120 changes: 120 additions & 0 deletions editor/Controls/FloorEditor.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>
Loading

0 comments on commit 3bdfdaf

Please sign in to comment.