An editor overlay for tilemap selection. It activates when the user holds Alt and clicks in the viewport, allowing selection of a specific tile at the cursor location.
using System;
namespace Saandy.Editor.Tilemapper;
/// <summary>Reserve ALT + LMB before the scene viewport handles camera orbit input.</summary>
internal sealed class TilemapEyedropperOverlay : Widget
{
private readonly SceneViewportWidget Viewport;
private readonly Action<Ray> Pick;
public TilemapEyedropperOverlay( SceneViewportWidget viewport, Action<Ray> pick ) : base( viewport.Overlay )
{
Viewport = viewport;
Pick = pick;
NoSystemBackground = true;
TranslucentBackground = true;
Cursor = CursorShape.Cross;
ToolTip = "ALT + LMB — eyedrop tile";
Position = Vector2.Zero;
Size = viewport.Overlay.Size;
viewport.Renderer.MouseLeftPress += OnViewportLeftPress;
Visible = false;
}
public override void OnDestroyed()
{
if ( Viewport.IsValid() && Viewport.Renderer.IsValid() ) { Viewport.Renderer.MouseLeftPress -= OnViewportLeftPress; }
base.OnDestroyed();
}
private void OnViewportLeftPress()
{
// Catch a chord pressed between frames, before the ALT overlay has appeared.
if ( Game.IsPlaying || Viewport.IsGameView
|| !global::Editor.Application.KeyboardModifiers.HasFlag( KeyboardModifiers.Alt )
|| global::Editor.Application.MouseButtons.HasFlag( MouseButtons.Right ) ) { return; }
Visible = true;
PickAtCursor();
}
[EditorEvent.Frame]
private void Frame()
{
if ( !Parent.IsValid() || !Viewport.IsValid() ) { return; }
Position = Vector2.Zero;
Size = Parent.Size;
Visible = !Game.IsPlaying && !Viewport.IsGameView
&& global::Editor.Application.KeyboardModifiers.HasFlag( KeyboardModifiers.Alt )
&& !global::Editor.Application.MouseButtons.HasFlag( MouseButtons.Right );
}
protected override void OnPaint() { }
protected override void OnMousePress( MouseEvent e )
{
if ( !e.LeftMouseButton ) { base.OnMousePress( e ); return; }
e.Accepted = true;
PickAtCursor();
}
private void PickAtCursor()
{
var renderer = Viewport.Renderer;
if ( renderer?.Camera == null || !renderer.Camera.IsValid() ) { return; }
var pixel = renderer.FromScreen( global::Editor.Application.CursorPosition ) * renderer.DpiScale;
Pick( renderer.Camera.ScreenPixelToRay( pixel ) );
}
}