add: Selectable Tile Map Layer/Mouse Controlled Camera

This commit is contained in:
h z
2025-02-26 20:17:37 +00:00
parent 92770d3425
commit 8df4ebb6d5
6 changed files with 145 additions and 8 deletions

View File

@@ -0,0 +1,48 @@
using Godot;
using Polonium.Attributes;
// ReSharper disable once CheckNamespace
namespace GlobalClasses;
[ProxyNode]
[GlobalClass]
[Tool]
public partial class MouseControlledCamera : Camera2D
{
private Vector2 DragStartPosition { get; set; }
private bool IsDragging { get; set; }
[Signal]
public delegate void ZoomInEventHandler();
[Signal]
public delegate void ZoomOutEventHandler();
[Export]
public MouseButton DragButton { get; set; } = MouseButton.Middle;
public override void _UnhandledInput(InputEvent @event)
{
if (@event is InputEventMouseButton mouseButtonEvent)
{
if(mouseButtonEvent.ButtonIndex == MouseButton.WheelUp)
EmitSignalZoomIn();
else if(mouseButtonEvent.ButtonIndex == MouseButton.WheelDown)
EmitSignalZoomOut();
else if (mouseButtonEvent.ButtonIndex == DragButton)
{
IsDragging = mouseButtonEvent.Pressed;
if(IsDragging)
DragStartPosition = GetGlobalMousePosition();
}
}
else if (@event is InputEventMouseMotion mouseMotionEvent && IsDragging)
{
Vector2 mousePos = GetGlobalMousePosition();
Vector2 offset = DragStartPosition - mousePos;
Position += offset;
DragStartPosition = mousePos;
}
}
}

View File

@@ -7,12 +7,12 @@ namespace GlobalClasses;
[Tool]
public partial class CameraScene : Scene
{
private Camera2D Camera { get; set; }
protected Camera2D Camera { get; set; }
[Export]
public float MaxZoom { get; set; }
[Export]
public float MinZoom { get; set; }
[Export]
public float ZoomRate { get; set; }
private float Zoom
@@ -26,13 +26,18 @@ public partial class CameraScene : Scene
{
}
public sealed override void _Ready()
protected virtual void _Ready_()
{
Camera = GetNode<Camera2D>("Camera");
}
public sealed override void _Ready()
{
_Ready_();
__Ready();
base._Ready();
}
protected void ZoomIn() => Zoom = Mathf.Max(Zoom * (1 + ZoomRate), MaxZoom);
protected void ZoomOut() => Zoom = Mathf.Min(Zoom * (1 - ZoomRate), MinZoom);
protected void ZoomIn() => Zoom = Mathf.Min(Zoom * (1 + ZoomRate), MaxZoom);
protected void ZoomOut() => Zoom = Mathf.Max(Zoom * (1 - ZoomRate), MinZoom);
protected void ZoomAt(Vector2 pos) => Camera.Position = pos;
}

View File

@@ -0,0 +1,24 @@
using Godot;
using Polonium.Attributes;
// ReSharper disable once CheckNamespace
namespace GlobalClasses;
[ProxyNode]
[GlobalClass]
[Tool]
public partial class MouseControlledCameraScene : CameraScene
{
protected new MouseControlledCamera Camera
{
get => base.Camera as MouseControlledCamera;
set => base.Camera = value;
}
protected sealed override void _Ready_()
{
Camera = GetNode<MouseControlledCamera>("Camera");
Camera.ZoomIn += ZoomIn;
Camera.ZoomOut += ZoomOut;
}
}

View File

@@ -0,0 +1,42 @@
using Godot;
using Polonium.Attributes;
namespace GlobalClasses;
[ProxyNode]
[GlobalClass]
[Tool]
public partial class SelectableTileMapLayer : TileMapLayer
{
[Signal]
public delegate void CellSelectedEventHandler(Vector2I pos);
[Signal]
public delegate void CellEnteredEventHandler(Vector2I pos);
private Vector2I HoveredCell { get; set; } = new Vector2I(-1, -1);
public sealed override void _Process(double delta)
{
Vector2 mousePos = GetGlobalMousePosition();
Vector2 localMousePos = ToLocal(mousePos);
Vector2I cell = LocalToMap(localMousePos);
if(cell != HoveredCell)
{
EmitSignalCellEntered(cell);
HoveredCell = cell;
}
__Process(delta);
base._Process(delta);
}
public override void _UnhandledInput(InputEvent @event)
{
if (@event is InputEventMouseButton mouseEvent)
if (mouseEvent.ButtonIndex == MouseButton.Left && mouseEvent.Pressed)
EmitSignalCellSelected(HoveredCell);
}
[ProxyMethod]
public virtual void __Process(double delta)
{
}
}

View File

@@ -0,0 +1,12 @@
using Godot;
namespace Polonium.Extensions;
public static class LinqExtensions
{
public static T RandomSelect<T>(this IEnumerable<T> source)
{
T[] s = source.ToArray();
return s[GD.Randi() % s.Length];
}
}

View File

@@ -33,8 +33,13 @@ public static class Utils
{
if (!fname.EndsWith(".png"))
continue;
AnimatedTextureCache[path].SetFrameTexture(f, ResourceLoader.Load<Texture2D>(fname));
AnimatedTextureCache[path].SetFrameTexture(f, ResourceLoader.Load<Texture2D>($"{path}/{fname}"));
f += 1;
}
AnimatedTextureCache[path].SetFrames(f + 1);
AnimatedTextureCache[path].SetOneShot(false);
AnimatedTextureCache[path].SetSpeedScale(4);
}
return AnimatedTextureCache[path];
@@ -44,6 +49,7 @@ public static class Utils
return null;
}
}
}
#pragma warning restore CS0618
#pragma warning restore CS0168