|
| 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 | + ) |
0 commit comments