desc:Animated MCP Playback State Monitor

//======================================
// INITIALIZATION & PERSISTENT VARIABLES
//======================================
@init

// Animation state
angle = 0;
reel_speed = -0.7;

// Absolute Reel posisions within 4:3 Design space
reel_y = 150;
left_reel_x = 120;
right_reel_x = 280;
hub_radius = 0.3;

// Debug toggle
debug = 0;

//==============================
// AUDIO BLOCK (animation logic)
//==============================

@block

// Read Transport state
play_pos = play_position;
play_state_local = play_state;

// Determine playback status
is_playing = (play_state_local == 1 || play_state_local == 5);

// Advance Animation if playing
is_playing ? (
  delta = samplesblock / srate;
  angle += delta * 3;
);

//=======================================
// GRAPHICAL CONSTRAINTS (Draw Everything Every Frame)
//=======================================
@gfx 400 300

// --- Global Color ----

// Default Idle Color (white)
col_r = 0.9;
col_g = 0.9;
col_b = 0.9;
col_a = 0.9;

// Transport state dependent coloring
play_state_local == 2 ? (
//Paused - faint blue
col_r = 0.3;
col_g = 0.7;
col_b = 0.42;
);
play_state_local == 1 ? (
//Playing - Green
col_r = 0.3;
col_g = 0.7;
col_b = 0.42;
);
play_state_local == 5 ? (
//Recording - Red
col_r = 0.89;
col_g = 0.35;
col_b = 0.2;
);
play_state_local == 6 ? (
//Paused While Recording - Red
col_r = 0.89;
col_g = 0.35;
col_b = 0.2;
);

// --- Resolution ----
design_w = 400;
design_h = 300;

// --- Scaling System ---
scale = min(gfx_w/design_w, gfx_h/design_h);

// --- Scale computing ---
draw_w = design_w * scale;
draw_h = design_h * scale;
offset_x = (gfx_w - draw_w) * 0.5;
offset_y = (gfx_h - draw_h) * 0.5;

// --- Clear background ---
gfx_set(0,0,0,1);
gfx_rect(0,0,gfx_w,gfx_h,1);

// --- (DEBUG) 4:3 green outline ---
debug ? (
  gfx_set(0,1,0,1);
  gfx_rect(offset_x, offset_y, draw_w, draw_h, 0);
);

// Reel positioning in design space
screen_y = offset_y + (reel_y_design / design_h) * draw_h;
screen_radius = (reel_radius_design / design_h) * draw_h;
screen_left_x = offset_x + (left_reel_x / design_w) * draw_w;
screen_right_x = offset_x + (right_reel_x / design_w) * draw_w;

// Reel position based on design space
reel_y_design = 110; //y offset
reel_radius_design = 77; // reel radius

// Tape guide design
guide_offset_y = 70;
guide_radius = 10;

// --- Tape Guide Helpers ---
screen_guide_left_x = screen_left_x;
screen_guide_left_y = offset_y + ((reel_y + guide_offset_y) / design_h) * draw_h;
screen_guide_radius = (guide_radius / design_h) * draw_h;

screen_guide_right_x = screen_right_x;
screen_guide_right_y = offset_y + ((reel_y + guide_offset_y) / design_h) * draw_h;

// Draw Reel Helper
function draw_reel(cx, cy, r)
(
  // radial definitions
  rim_fraction = 0.78;
  hub_fraction = 0.31;
  spoke_count = 6;
  spoke_offset = $pi/6;
  seg_count = 3; 
  seg_hub = seg_count;
  seg_angle = $pi/3;
   
  r_hub = r * hub_fraction;
  r_outer = r * rim_fraction;

  
  gfx_set(col_r, col_g, col_b, col_a);
  
  //Outer rim
  gfx_circle(cx, cy, r, 0);
  
  //Segmented inner rim
  gfx_linethickness = 4*scale;
  i = 0;
  loop(seg_count,
    start_a = i*(2*$pi/seg_count) + angle*reel_speed;
    end_a = start_a + seg_angle;
    gfx_arc(cx, cy, r_outer, start_a, end_a);
    i += 1;
  );
  //Segmented inner hub
  loop(spoke_count,
    a = i*(2*$pi/spoke_count) + angle*reel_speed + spoke_offset;
    xh = cx + cos(a) * r_hub;
    yh = cy + sin(a) * r_hub;
    xo = cx + cos(a) * r_outer;
    yo = cy + sin(a) * r_outer;
    gfx_line(xh, yh, xo, yo);
    i += 1;
  );
  loop(seg_hub,
    start_a = i*(2*$pi/seg_hub) + angle*reel_speed;
    end_a = start_a + seg_angle;
    gfx_arc(cx, cy, r_hub, start_a, end_a);
    i += 1;
  );
);

// Call draw_reel
draw_reel(screen_left_x, screen_y, screen_radius);
draw_reel(screen_right_x, screen_y, screen_radius);

// Draw guide helper
function draw_guide(cx, cy, r)
(
  gfx_set(col_r, col_g, col_b, col_a);
  gfx_linethickness = 3*scale;
  gfx_circle(cx, cy, r, 0);
);
draw_guide(screen_guide_left_x, screen_guide_left_y, screen_guide_radius);
draw_guide(screen_guide_right_x, screen_guide_left_y, screen_guide_radius);

// --- Draw "tape path" between reels and guides ---

// Horizontal line connecting tape guides
left_bottom_x = screen_guide_left_x;
left_bottom_y = screen_guide_left_y + screen_guide_radius;

right_bottom_x = screen_guide_right_x;
right_bottom_y = screen_guide_right_y + screen_guide_radius;

gfx_line(left_bottom_x, left_bottom_y, right_bottom_x, right_bottom_y);

// - Define edge points on the tape reels using angles
exit_offset = 0.7; //radians
right_angle = exit_offset;
left_angle = $pi - exit_offset;

// Left reel edge point
left_reel_edge_x = screen_left_x + cos(left_angle) * screen_radius;
left_reel_edge_y = screen_y + sin(left_angle) * screen_radius;
// Left guide edge point
left_guide_edge_x = screen_guide_left_x + cos(left_angle) * screen_guide_radius;
left_guide_edge_y = screen_guide_left_y + sin(left_angle) * screen_guide_radius;

gfx_line(left_reel_edge_x, left_reel_edge_y, left_guide_edge_x, left_guide_edge_y);

// Right reel edge point
right_reel_edge_x = screen_right_x + cos(right_angle) * screen_radius;
right_reel_edge_y = screen_y + sin(right_angle) * screen_radius;
// Right guide edge point
right_guide_edge_x = screen_guide_right_x + cos(right_angle) * screen_guide_radius;
right_guide_edge_y = screen_guide_right_y + sin(right_angle) * screen_guide_radius;

  
gfx_line(right_reel_edge_x, right_reel_edge_y, right_guide_edge_x, right_guide_edge_y);

// --- Tape heads (white outlines only) ---
head_offset_design = 12;
head_w_design    = 110;
head_h_design    = 25;

// Convert to screen space
head_w  = (head_w_design / design_w) * draw_w;
head_h  = (head_h_design / design_h) * draw_h;
head_y_offset = (head_offset_design / design_h) * draw_h;

// Horizontal center along tape line
head_x = left_bottom_x + (right_bottom_x - left_bottom_x)/2 - head_w/2;

// Vertical positions above and below the tape line
head_y_top    = left_bottom_y - head_y_offset - head_h/2;
head_y_bottom = left_bottom_y + head_y_offset - head_h/2;

// Draw only white outlines
gfx_set(col_r, col_g, col_b, col_a);
gfx_linethickness = 2*scale;
gfx_rect(head_x, head_y_top, head_w, head_h, 0);
gfx_rect(head_x, head_y_bottom, head_w, head_h, 0);
