Skip to content

Commit 7825724

Browse files
authored
Implement piano practice sequence functionality (#6)
* Lesson almost working * Getting closer * Initial working sequence * Improve layout * Ability to fine-tune finger numbers * Improve code * Add C Major chord inversions * Fixed chord (inversion) sequences * Clean up finger numbers * Cleanup signal handlers * Clean up
1 parent 8440074 commit 7825724

File tree

8 files changed

+498
-102
lines changed

8 files changed

+498
-102
lines changed

app/scenes/piano.gd

Lines changed: 0 additions & 99 deletions
This file was deleted.

app/scenes/piano.tscn

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
[gd_scene load_steps=4 format=3 uid="uid://deocrbvduwvbf"]
1+
[gd_scene load_steps=6 format=3 uid="uid://deocrbvduwvbf"]
22

3-
[ext_resource type="Script" path="res://scenes/piano.gd" id="1_16djv"]
4-
[ext_resource type="Script" path="res://scenes/note_display.gd" id="2_4lvi2"]
3+
[ext_resource type="Script" path="res://scripts/piano.gd" id="1_16djv"]
4+
[ext_resource type="Script" path="res://scripts/note_display.gd" id="2_4lvi2"]
55
[ext_resource type="PackedScene" uid="uid://qnn08ykran7h" path="res://scenes/metronome.tscn" id="3_m4k2l"]
6+
[ext_resource type="Script" path="res://scripts/finger_display.gd" id="4_k3m9p"]
7+
[ext_resource type="Script" path="res://scripts/sequence_manager.gd" id="5_r2n4b"]
68

79
[node name="Piano" type="Control"]
810
layout_mode = 3
@@ -26,5 +28,11 @@ script = ExtResource("2_4lvi2")
2628
[node name="PianoKeys" type="Node2D" parent="."]
2729
position = Vector2(0, 100)
2830

31+
[node name="FingerDisplay" type="Node2D" parent="PianoKeys"]
32+
script = ExtResource("4_k3m9p")
33+
34+
[node name="SequenceManager" type="Node" parent="."]
35+
script = ExtResource("5_r2n4b")
36+
2937
[node name="CanvasLayer#Metronome" parent="." instance=ExtResource("3_m4k2l")]
3038
layout_mode = 1

app/scripts/finger_display.gd

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
class_name FingerDisplay
2+
extends Node2D
3+
4+
const LEFT_HAND_COLOR = Color(0.8, 0.8, 0.8, 1.0)
5+
const RIGHT_HAND_COLOR = Color(0.8, 0.8, 0.8, 1.0)
6+
const LESSON_COLOR = Color(0.3, 0.8, 0.3, 1.0)
7+
const BACKGROUND_COLOR = Color(0.1, 0.1, 0.1, 1.0)
8+
const BACKGROUND_HEIGHT = 40
9+
10+
const KEY_ADJUSTMENTS = {
11+
"C": -2, # Slight left adjustment
12+
"D": -3,
13+
"E": -2,
14+
"F": -2,
15+
"G": -3,
16+
"A": -3,
17+
"B": -2,
18+
"C#": -4, # Black keys
19+
"D#": -4,
20+
"F#": -4,
21+
"G#": -4,
22+
"A#": -4
23+
}
24+
25+
var background_rect: ColorRect
26+
var finger_labels: Array[Label] = []
27+
28+
func _ready():
29+
setup_background()
30+
31+
func setup_background():
32+
background_rect = ColorRect.new()
33+
background_rect.color = BACKGROUND_COLOR
34+
background_rect.z_index = -1
35+
add_child(background_rect)
36+
update_background_size()
37+
38+
func _process(delta):
39+
update_background_size()
40+
41+
func update_background_size():
42+
if background_rect:
43+
var viewport_size = get_viewport_rect().size
44+
background_rect.size = Vector2(viewport_size.x, BACKGROUND_HEIGHT)
45+
background_rect.position = Vector2(0, -BACKGROUND_HEIGHT)
46+
47+
func clear_indicators():
48+
for label in finger_labels:
49+
label.queue_free()
50+
finger_labels.clear()
51+
52+
func add_finger_indicator(note: PianoNote, key_rect: Rect2, is_current: bool = false):
53+
var label = Label.new()
54+
label.text = str(note.finger)
55+
label.add_theme_font_size_override("font_size", 24)
56+
57+
var color = LESSON_COLOR if is_current else (RIGHT_HAND_COLOR if note.hand == "R" else LEFT_HAND_COLOR)
58+
label.add_theme_color_override("font_color", color)
59+
60+
add_child(label)
61+
finger_labels.append(label)
62+
label.size = label.get_minimum_size()
63+
64+
var note_name = note.pitch.left(len(note.pitch) - 1)
65+
var x_adjustment = KEY_ADJUSTMENTS.get(note_name, 0)
66+
67+
label.position = Vector2(
68+
key_rect.position.x + (key_rect.size.x / 2) - (label.size.x / 2) + x_adjustment,
69+
-BACKGROUND_HEIGHT/2 - label.size.y/2
70+
)
File renamed without changes.

0 commit comments

Comments
 (0)