Skip to content

Commit

Permalink
feat: map editor cursor sprites (#1797)
Browse files Browse the repository at this point in the history
* feat: map editor cursor sprites

* Feature can be toggled within the editor -> File -> Options -> General
 - Cursor sprite changes when the user switches between tools.
 - Moving the cursor out of the map editor sets the cursor back to normal.
 - Editor tool's custom sprites are modifiable.
 - Editor tool's cursor click-points are configurable.

* Fixed issues when switching from selection tool to other tools.

* March 14th, 2023: Updated to NET7 Branch and reviews:
 - ToolCursor doesn't needs IConfigurable.
 - ToolCursor uses a dictionary for improved performance.
 - Better performance and structure as overall.
 - Code clean up according to previous reviews.

* review: defer ToolCursor initialization in Load() method
  • Loading branch information
Arufonsu committed Aug 27, 2023
1 parent 07b65aa commit 9d832c9
Show file tree
Hide file tree
Showing 18 changed files with 387 additions and 252 deletions.
58 changes: 58 additions & 0 deletions Intersect.Editor/Configuration/ToolCursor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using Intersect.Editor.General;

namespace Intersect.Editor.Configuration
{
public sealed class ToolCursor
{
public Point CursorClickPoint { get; set; }

private static readonly Dictionary<EditingTool, ToolCursor> _toolCursorDict;

public static IReadOnlyDictionary<EditingTool, ToolCursor> ToolCursorDict => _toolCursorDict;

static ToolCursor()
{
_toolCursorDict = new Dictionary<EditingTool, ToolCursor>
{
{ EditingTool.Brush, new ToolCursor() },
{ EditingTool.Dropper, new ToolCursor() },
{ EditingTool.Erase, new ToolCursor() },
{ EditingTool.Fill, new ToolCursor() },
{ EditingTool.Rectangle, new ToolCursor() },
{ EditingTool.Selection, new ToolCursor() }
};
}

public static void Load()
{
foreach (EditingTool tool in Enum.GetValues(typeof(EditingTool)))
{
var fileName = $"resources/cursors/editor_{tool.ToString().ToLowerInvariant()}.json";
ToolCursor toolCursor;

if (File.Exists(fileName))
{
if (!_toolCursorDict.TryGetValue(tool, out toolCursor))
{
continue;
}

var json = File.ReadAllText(fileName);
toolCursor = JsonConvert.DeserializeObject<ToolCursor>(json);
}
else
{
toolCursor = new ToolCursor();
var json = JsonConvert.SerializeObject(toolCursor);
File.WriteAllText(fileName, json);
}

_toolCursorDict[tool] = toolCursor;
}
}
}
}
3 changes: 2 additions & 1 deletion Intersect.Editor/Core/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.IO;

using Intersect.Configuration;

using Intersect.Editor.Configuration;
using Mono.Data.Sqlite;

namespace Intersect.Editor
Expand Down Expand Up @@ -57,6 +57,7 @@ public static void LoadOptions()

/* Load configuration */
ClientConfiguration.LoadAndSave(ClientConfiguration.DefaultPath);
ToolCursor.Load();
}

//Map Cache DB
Expand Down
24 changes: 12 additions & 12 deletions Intersect.Editor/Core/Graphics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,8 @@ RenderTarget2D renderTarget2D

int x1 = 0, y1 = 0, x2 = 0, y2 = 0, xoffset = 0, yoffset = 0;
int dragxoffset = 0, dragyoffset = 0;
if (Globals.CurrentTool == (int) EditingTool.Rectangle ||
Globals.CurrentTool == (int) EditingTool.Selection)
if (Globals.CurrentTool == EditingTool.Rectangle ||
Globals.CurrentTool == EditingTool.Selection)
{
if (selW < 0)
{
Expand Down Expand Up @@ -585,19 +585,19 @@ RenderTarget2D renderTarget2D
//Lets Create the Preview
//Mimic Mouse Down
tmpMap = TilePreviewStruct;
if (Globals.CurrentTool == (int) EditingTool.Selection && Globals.Dragging)
if (Globals.CurrentTool == EditingTool.Selection && Globals.Dragging)
{
Globals.MapEditorWindow.ProcessSelectionMovement(tmpMap, false, true);
}
else
{
if (Globals.CurrentLayer == LayerOptions.Attributes)
{
if (Globals.CurrentTool == (int) EditingTool.Brush)
if (Globals.CurrentTool == EditingTool.Brush)
{
Globals.MapLayersWindow.PlaceAttribute(tmpMap, Globals.CurTileX, Globals.CurTileY);
}
else if (Globals.CurrentTool == (int) EditingTool.Rectangle)
else if (Globals.CurrentTool == EditingTool.Rectangle)
{
for (var x = selX; x < selX + selW + 1; x++)
{
Expand Down Expand Up @@ -626,7 +626,7 @@ RenderTarget2D renderTarget2D
}
else if (Globals.CurrentTileset != null)
{
if (Globals.CurrentTool == (int) EditingTool.Brush)
if (Globals.CurrentTool == EditingTool.Brush)
{
if (Globals.Autotilemode == 0)
{
Expand Down Expand Up @@ -659,7 +659,7 @@ RenderTarget2D renderTarget2D

tmpMap.Autotiles.UpdateCliffAutotiles(tmpMap, Globals.CurrentLayer);
}
else if (Globals.CurrentTool == (int) EditingTool.Rectangle)
else if (Globals.CurrentTool == EditingTool.Rectangle)
{
var x = 0;
var y = 0;
Expand Down Expand Up @@ -861,8 +861,8 @@ private static void DrawSelectionRect()
selH = Globals.CurMapSelH;

int dragxoffset = 0, dragyoffset = 0;
if (Globals.CurrentTool == (int) EditingTool.Rectangle ||
Globals.CurrentTool == (int) EditingTool.Selection)
if (Globals.CurrentTool == EditingTool.Rectangle ||
Globals.CurrentTool == EditingTool.Selection)
{
if (selW < 0)
{
Expand Down Expand Up @@ -1007,7 +1007,7 @@ private static void DrawSelectionRect()
}
}

if (Globals.CurrentTool == (int) EditingTool.Selection && Globals.Dragging)
if (Globals.CurrentTool == EditingTool.Selection && Globals.Dragging)
{
DrawBoxOutline(
CurrentView.Left + Globals.CurTileX * Options.TileWidth,
Expand All @@ -1016,8 +1016,8 @@ private static void DrawSelectionRect()
);
}

if (Globals.CurrentTool == (int) EditingTool.Rectangle ||
Globals.CurrentTool == (int) EditingTool.Selection)
if (Globals.CurrentTool == EditingTool.Rectangle ||
Globals.CurrentTool == EditingTool.Selection)
{
DrawBoxOutline(
CurrentView.Left + (selX + dragxoffset) * Options.TileWidth,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9d832c9

Please sign in to comment.