-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNote.py
More file actions
82 lines (68 loc) · 3.08 KB
/
Note.py
File metadata and controls
82 lines (68 loc) · 3.08 KB
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
import pygame
class Note:
def __init__(self, start_tick, pitch, length, velocity=64, channel=0):
# MIDI Data (The "Truth")
self.x = start_tick # Start time in ticks
self.y = pitch # Pitch (expressed as grid row)
self.length = length # Duration in ticks
self.velocity = velocity # Strike force (0-127)
self.channel = channel # MIDI Channel
# UI State (For Interaction)
self.dragging = False
self.resizing = False
self.editing_velocity=False
def start_drag(self, click_tick, click_grid_y, mode="move"):
# Store the note's position at the EXACT moment the mouse went down
self.start_x = self.x
self.start_y = self.y
self.start_length = self.length
self.start_velocity = self.velocity
# Store where the user clicked globally to calculate the movement delta
self.drag_start_tick = click_tick
self.drag_start_grid_y = click_grid_y
if mode == "move":
self.dragging = True
elif mode == "resize":
self.resizing = True
def stop_drag(self):
self.dragging = False
self.resizing = False
def draw(self, surface, x, y, w, h, color=(64, 224, 208), is_selected=False, alpha=255):
# 1. Pre-calculate colors to avoid list comprehensions/allocations
if is_selected:
r, g, b = min(255, color[0] + 50), min(255, color[1] + 50), min(255, color[2] + 50)
else:
r, g, b = color
# 2. Optimized Alpha Handling
# If no transparency is needed, draw directly to the main surface (much faster)
if alpha >= 255:
target_surf = surface
rect_pos = (x, y, w, h)
draw_color = (r, g, b)
highlight_color = (min(255, r + 50), min(255, g + 50), min(255, b + 50))
velocity_color = (int(r * 0.7), int(g * 0.7), int(b * 0.7))
border_color = (20, 20, 20)
else:
target_surf = pygame.Surface((w, h), pygame.SRCALPHA).convert_alpha()
rect_pos = (0, 0, w, h)
draw_color = (r, g, b, alpha)
highlight_color = (min(255, r + 50), min(255, g + 50), min(255, b + 50), alpha)
velocity_color = (int(r * 0.7), int(g * 0.7), int(b * 0.7), alpha)
border_color = (20, 20, 20, alpha)
# 3. Draw Body
pygame.draw.rect(target_surf, draw_color, rect_pos)
# 4. Top Highlight
if h > 4:
pygame.draw.rect(target_surf, highlight_color, (rect_pos[0], rect_pos[1], w, 2))
# 5. Velocity Line
if h > 6:
velocity_ratio = self.velocity / 127.0
line_h = max(1, min(int(h * 0.2), 8))
line_w = (w - 2) * velocity_ratio
pygame.draw.rect(target_surf, velocity_color,
(rect_pos[0] + 1, rect_pos[1] + h - line_h - 2, line_w, line_h))
# 6. Border
pygame.draw.rect(target_surf, border_color, rect_pos, 1)
# 7. Blit only if we used a temp surface
if alpha < 255:
surface.blit(target_surf, (x, y))