-
Notifications
You must be signed in to change notification settings - Fork 0
/
Discoball.cs
129 lines (112 loc) · 4.28 KB
/
Discoball.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using Godot;
namespace TonstudioDiscoball;
public partial class Discoball : Node {
[Export] private Curve _curve;
private CanvasLayer _ui;
private Sprite2D _circle;
private DiscoShape _rect;
private ConfigWindow _configWindow;
private bool _uiMode = true;
private bool _animationPlaying;
private double _animationDelta;
private double _animationTimingDelta;
private float _rectOffset;
private float _rectHeight;
private double _animationDuration;
private int _midiClocks;
private bool _midiAnimationTriggered;
public override void _Ready() {
GetViewport().TransparentBg = true;
_ui = GetNode<CanvasLayer>("UI");
_circle = GetNode<Sprite2D>("Circle");
_rect = GetNode<DiscoShape>("Circle/Rect");
_configWindow = GetNode<ConfigWindow>("ConfigWindow");
_configWindow.Visible = true;
_configWindow.MoveToForeground();
_configWindow.GrabFocus();
OS.OpenMidiInputs();
Console.WriteLine("midi:");
foreach (var connectedMidiInput in OS.GetConnectedMidiInputs()) {
Console.WriteLine(connectedMidiInput);
}
}
public override void _Process(double delta) {
if (_uiMode) return;
if (_animationPlaying) {
Animate(delta, _animationDuration);
} else {
if (DiscoConfig.CurrentConfig.MidiMode) return;
_animationTimingDelta += delta;
var targetDelta = 60.0 / DiscoConfig.CurrentConfig.Bpm;
if (_animationTimingDelta >= targetDelta) {
_animationTimingDelta %= targetDelta;
InitAnimation();
}
}
}
private ulong _lastMidiTs;
public override void _Input(InputEvent ev) {
if (ev.IsActionPressed("Quit")) {
GetTree().Quit();
}
if (DiscoConfig.CurrentConfig.MidiMode && ev is InputEventMidi { Message: MidiMessage.Start }) {
_midiClocks = 0;
_midiAnimationTriggered = false;
}
if (DiscoConfig.CurrentConfig.MidiMode && ev is InputEventMidi { Message: MidiMessage.TimingClock }) {
_midiClocks++;
if (_midiClocks >= 24) {
_midiClocks -= 24;
_midiAnimationTriggered = false;
}
if (_midiClocks >= (24 - DiscoConfig.CurrentConfig.AnimationOffset) % 24 && !_midiAnimationTriggered) {
_midiAnimationTriggered = true;
InitAnimation();
}
}
if (ev.IsActionPressed("TestButton")) {
if (_uiMode && !DiscoConfig.CurrentConfig.MidiMode) return;
InitAnimation();
}
if (ev.IsActionPressed("UiMode")) {
_uiMode = !_uiMode;
_ui.Visible = _uiMode;
if (_uiMode) {
_circle.SelfModulate = Colors.White;
_animationTimingDelta = 0;
_configWindow.Show();
} else {
_circle.SelfModulate = Colors.Black;
}
}
}
private void Animate(double delta, double animationDuration) {
_animationDelta += delta;
var progress = _animationDelta / animationDuration;
var adjustedProgress = _curve.Sample((float)progress);
var currentRectPosition = Tween.InterpolateValue(
-_rectOffset,
_rectOffset * 2 + _rectHeight,
adjustedProgress * animationDuration,
animationDuration,
Tween.TransitionType.Linear,
Tween.EaseType.InOut);
_rect.Position = new Vector2(_rect.Position.X, (float)currentRectPosition);
if (_animationDelta > animationDuration) {
_animationPlaying = false;
_animationDelta = 0;
}
}
private void InitAnimation() {
_rect.SetRandomColor();
var circleHeight = _circle.Texture.GetHeight();
_rectHeight = circleHeight / 100.0f * DiscoConfig.CurrentConfig.BarHeight;
_rect.Scale = new Vector2(_rect.Scale.X, _rectHeight);
_rectOffset = circleHeight / 2.0f + _rectHeight / 2.0f;
_rect.Position = new Vector2(_rect.Position.X, -_rectOffset);
_animationDuration = DiscoConfig.CurrentConfig.AnimationDuration;
_animationPlaying = true;
_animationDelta = 0;
}
}