Skip to content

Commit

Permalink
initial editing in TEditAvalonia
Browse files Browse the repository at this point in the history
  • Loading branch information
BinaryConstruct committed Jul 10, 2024
1 parent 5ea155f commit 5123140
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/TEdit.Desktop/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public override void Initialize()
services.AddSingleton<IDocumentService, DocumentService>();

services.AddSingleton<IMouseTool, ArrowTool>();
services.AddSingleton<IMouseTool, BrushTool>();
//services.AddSingleton<IMouseTool, BrushTool>();
services.AddSingleton<IMouseTool, PencilTool>();
services.AddSingleton<IMouseTool, SelectTool>();

Expand Down
8 changes: 6 additions & 2 deletions src/TEdit.Desktop/Controls/SkiaWorldRenderBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,7 @@ private void ViewPortOnPointerPressed(object? sender, PointerPressedEventArgs e)
if (ActiveTool != null)
{
ActiveTool?.Press(WorldEditor, pointer, WorldCoordinate);

_pixelTileCache.SetPixelDirty((int)WorldCoordinate.X, (int)WorldCoordinate.Y);
}

if (SelectionMode != SelectionModes.None)
Expand Down Expand Up @@ -1314,7 +1314,11 @@ private void ViewPortOnPointerMoved(object? sender, PointerEventArgs e)
(double wcX, double wcY) = PointToImage(_pointerPosition, true);
WorldCoordinate = new Point((int)wcX, (int)wcY); // cast to int

ActiveTool?.Move(WorldEditor, pointer, WorldCoordinate);
if (ActiveTool != null)
{
ActiveTool?.Move(WorldEditor, pointer, WorldCoordinate);
_pixelTileCache.SetPixelDirty((int)WorldCoordinate.X, (int)WorldCoordinate.Y);
}

if (!_isPanning && !_isSelecting)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public interface IRasterTileCache : IDisposable
RasterTile? GetTile(int x, int y);
void Clear();

void SetPixelDirty(int x, int y);

int TileSize { get; }

int TilesX { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ private static TEditColor GetBackgroundColor(World _world, int y)
return WorldConfiguration.GlobalColors["Sky"];
}

public static SKColor GetBlockColor(World _world, int currentBlockX, int currentBlockY)
{
var block = _world.Tiles[currentBlockX, currentBlockY];
var bgColor = GetBackgroundColor(_world, currentBlockY);
return PixelMap.GetTileColor(block, bgColor).ToSKColor().WithAlpha(255);
}

public static SKBitmap CreateBitmapTile(World _world, int xTile, int yTile, int tileSize)
{
ArgumentNullException.ThrowIfNull(_world);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public int PixelToTileIndex(int worldPixelX, int worldPixelY)
return tileIndex;
}

public void SetPixelDirty(int x, int y)
{
(int tileIndex, int tilePixelX, int tilePixelY) = PixelToTilePixelIndex(x, y);
_tiles[tileIndex].IsDirty = true;
}

private (int tileIndex, int tilePixelX, int tilePixelY) PixelToTilePixelIndex(int worldPixelX, int worldPixelY)
{
int curTileX = worldPixelX / TileSize;
Expand Down
85 changes: 83 additions & 2 deletions src/TEdit.Desktop/Editor/IMouseTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using TEdit.Desktop.Services;
using TEdit.Desktop.ViewModels;
using TEdit.Editor;
using TEdit.Geometry;
using static TEdit.Desktop.Controls.SkiaWorldRenderBox;

namespace TEdit.Desktop.Editor;
Expand Down Expand Up @@ -69,11 +71,90 @@ public PencilTool(IDocumentService documentService)
[Reactive] public bool IsActive { get; set; }
public string IconName { get; } = "mdi-pencil";

private bool _isLeftDown;
private bool _isRightDown;
private Vector2Int32 _startPoint;
private Vector2Int32 _endPoint;
private WorldEditor _editor;

private void CheckDirectionandDraw(Vector2Int32 tile)
{
Vector2Int32 p = tile;
Vector2Int32 p2 = tile;
if (_isRightDown)
{
if (_isLeftDown)
p.X = _startPoint.X;
else
p.Y = _startPoint.Y;

DrawLine(p);
_startPoint = p;
System.Diagnostics.Debug.WriteLine($"Update _startpoint {_startPoint} CheckDirectionandDraw _isRightDown");
}
else if (_isLeftDown)
{
DrawLine(p);
_startPoint = p;
System.Diagnostics.Debug.WriteLine($"Update _startpoint {_startPoint} CheckDirectionandDraw _isLeftDown");
_endPoint = p;
}
}

private void DrawLine(Vector2Int32 to)
{
foreach (Vector2Int32 pixel in Shape.DrawLineTool(_startPoint, to))
{
_editor.SetPixel(pixel.X, pixel.Y);
//BlendRules.ResetUVCache(_wvm, pixel.X, pixel.Y, 1, 1);
}
}

private void DrawLineP2P(Vector2Int32 endPoint)
{
foreach (Vector2Int32 pixel in Shape.DrawLineTool(_startPoint, _endPoint))
{
_editor.SetPixel(pixel.X, pixel.Y);
}
}

public void Press(WorldEditor editor, PointerPoint buttons, Point worldCoordinate)
{
if (IsActive)
_editor = editor;
_editor.BeginOperationAsync().Wait();

if (!_isRightDown && !_isLeftDown)
{
editor.SetPixel((int)worldCoordinate.X, (int)worldCoordinate.Y);
_startPoint = new Vector2Int32((int)worldCoordinate.X, (int)worldCoordinate.Y);
}

_isLeftDown = buttons.Properties.IsLeftButtonPressed;
_isRightDown = buttons.Properties.IsRightButtonPressed;

CheckDirectionandDraw(new Vector2Int32((int)worldCoordinate.X, (int)worldCoordinate.Y));
}

public void Move(WorldEditor editor, PointerPoint buttons, Point worldCoordinate)
{
_isLeftDown = buttons.Properties.IsLeftButtonPressed;
_isRightDown = buttons.Properties.IsRightButtonPressed;

CheckDirectionandDraw(new Vector2Int32((int)worldCoordinate.X, (int)worldCoordinate.Y));
}
public void Release(WorldEditor editor, PointerPoint buttons, Point worldCoordinate)
{
CheckDirectionandDraw(new Vector2Int32((int)worldCoordinate.X, (int)worldCoordinate.Y));

_isLeftDown = buttons.Properties.IsLeftButtonPressed;
_isRightDown = buttons.Properties.IsRightButtonPressed;

if (!_isLeftDown && !_isRightDown)
{
_editor.EndOperationAsync().Wait();
}
}

public void LeaveWindow(WorldEditor editor, PointerPoint buttons, Point worldCoordinate)
{
}
}
42 changes: 21 additions & 21 deletions src/TEdit.Editor/ToolDefaultData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ namespace TEdit.Editor;

public partial class ToolDefaultData
{
private static PaintMode _paintMode;

private static int _brushWidth;
private static int _brushHeight;
private static int _brushOutline;
private static BrushShape _brushShape;

private static int _paintTile;
private static int _paintTileMask;
private static bool _paintTileActive;
private static MaskMode _paintTileMaskMode;

private static int _paintWall;
private static int _paintWallMask;
private static bool _paintWallActive;
private static MaskMode _paintWallMaskMode;

private static bool _redWire;
private static bool _greenWire;
private static bool _blueWire;
private static bool _yellowWire;
private static PaintMode _paintMode = PaintMode.TileAndWall;

private static int _brushWidth = 20;
private static int _brushHeight = 20;
private static int _brushOutline = 1;
private static BrushShape _brushShape = BrushShape.Square;

private static int _paintTile = 0;
private static int _paintTileMask = 0;
private static bool _paintTileActive = true;
private static MaskMode _paintTileMaskMode = MaskMode.Off;

private static int _paintWall = 0;
private static int _paintWallMask = 0;
private static bool _paintWallActive = true;
private static MaskMode _paintWallMaskMode = MaskMode.Off;

private static bool _redWire = false;
private static bool _greenWire = false;
private static bool _blueWire = false;
private static bool _yellowWire = false;

// Invoked from World.Settings
public static void LoadSettings(IEnumerable<XElement> xmlToolSettings)
Expand Down
12 changes: 12 additions & 0 deletions src/TEdit.Editor/WorldEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ public async Task BeginOperationAsync()

public async Task EndOperationAsync()
{
if (_undo == null) return;

await _undo.SaveUndoAsync();

}

public BrushSettings Brush { get; set; } = new BrushSettings();
Expand All @@ -67,6 +70,10 @@ public void SetPixel(int x, int y, PaintMode? mode = null, bool? erase = null)
if (_world == null) return;
if (TilePicker == null) return;

int index = GetTileIndex(x, y);
if (_checkTiles[index]) { return; }
else { _checkTiles[index] = true; }

Tile curTile = _world.Tiles[x, y];
if (curTile == null) return;

Expand Down Expand Up @@ -187,6 +194,11 @@ public void SetPixel(int x, int y, PaintMode? mode = null, bool? erase = null)
}
}

private int GetTileIndex(int x, int y)
{
return x + y * _world.TilesWide;
}

private void SetWall(Tile curTile, bool erase)
{
if (TilePicker.WallMaskMode == MaskMode.Off ||
Expand Down

0 comments on commit 5123140

Please sign in to comment.