-
Notifications
You must be signed in to change notification settings - Fork 0
/
DragAnchor.cs
50 lines (41 loc) · 1.33 KB
/
DragAnchor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using Godot;
namespace TonstudioDiscoball;
public partial class DragAnchor : Control {
[Export] private SaveSystem _saveSystem;
private bool _hasMouseFocus;
private bool _dragging;
private Vector2 _dragPoint;
public override void _Ready() {
MouseEntered += () => {
if (Visible) {
_hasMouseFocus = true;
}
};
MouseExited += () => { _hasMouseFocus = false; };
}
public override void _Input(InputEvent ev) {
if (ev.IsActionPressed("LeftClick") && _hasMouseFocus)
StartDrag();
else if (ev.IsActionReleased("LeftClick"))
StopDrag();
else if (_dragging && ev is InputEventMouseMotion) {
UpdateWindowPosition();
}
}
private void StartDrag() {
_dragPoint = GetGlobalMousePosition();
_dragging = true;
}
private void StopDrag() {
_dragging = false;
_dragPoint = default;
}
private void UpdateWindowPosition() {
var positionChange = GetGlobalMousePosition() - _dragPoint;
var window = GetWindow();
var newPosition = window.Position + new Vector2I((int)positionChange.X, (int)positionChange.Y);
window.Position = newPosition;
_saveSystem.SaveWindowPosition();
}
}