add: Selectable Tile Map Layer/Mouse Controlled Camera
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user