// left mouse = move ball
// right mouse = reset to center
// ctrl + left mouse = X-axis only
// alt + left mouse = Y-axis only

// use with MIDItoReaControlPath

slider1:1<1,16,1>output Channel X
slider2:20<1,119,1>output CC Number X
slider3:1<1,16,1>output Channel Y
slider4:21<1,119,1>output CC Number Y
slider5:0<0,2,1{Off,Send X (Channel+CC Number),Send Y (Channel+CC Number)}>learn

in_pin:none
out_pin:none

@init
ext_noinit = 1;

gfx_clear = 0x202020;

xvalue = 63;
yvalue = 63;
offset = 0;
click_onx = 0;
click_ony = 0;
OFF = 0;

@gfx 652 652
gfx_getchar();

gCenterX = gfx_w * 0.5;
gCenterY = gfx_h * 0.5;

(mouse_cap == 1) ? (
xval = (mouse_x - gCenterX) / gCenterX;
yval = -(mouse_y - gCenterY) / gCenterY;
click_onx = 1;
click_ony = 1;
);
(mouse_cap == 2) ? (
xval = 0;
yval = 0;
click_onx = 1;
click_ony = 1;
);
(mouse_cap == 5) ? (
xval = (mouse_x - gCenterX) / gCenterX;
click_onx = 1;
click_ony = 0;
);
(mouse_cap == 17) ? (
yval = -(mouse_y - gCenterY) / gCenterY;
click_onx = 0;
click_ony = 1;
);
(mouse_cap == 0) ? (
click_onx = 0;
click_ony = 0;
);

xval = min(max(xval,-1),1);
yval = min(max(yval,-1),1);
xvalue = floor((xval+1)*63.5);
yvalue = floor((yval+1)*63.5);

small_mode = gfx_w<110 || gfx_h<110;

// Draw readout
small_mode ? font_size = 15 : font_size = 50;
gfx_setfont(1, "Lucida Sans Unicode", font_size);
gfx_r=1; gfx_g=0.8; gfx_b=0.2; gfx_a=0.5;
gfx_x = gCenterX-floor(font_size/2);
gfx_y = gCenterY-floor(font_size/2)+25;
gfx_drawnumber(xvalue,0);
gfx_x = gCenterX-floor(font_size/2);
gfx_y = gCenterY-floor(font_size/2)-25;
gfx_drawnumber(yvalue,0);

// Draw grid
gfx_r=gfx_g=gfx_b=0.25;
gfx_line(0, gfx_h/2, gfx_w, gfx_h/2);
gfx_line(gfx_w/2, 0, gfx_w/2, gfx_h);

// Draw circle
gfx_r=0.8; gfx_g=0.2; gfx_b=0.2; gfx_a=1;
small_mode ? gfx_circle((xval+1)*gCenterX,(-yval+1)*gCenterY,8,1,1) : gfx_circle((xval+1)*gCenterX,(-yval+1)*gCenterY,20,1,1);

@block
xchannel = slider1-1;
xCC = slider2;

ychannel = slider3-1;
yCC = slider4;

click_onx ? (
// send the message
msg1 = xchannel + 176;
msg23 = xCC + (xvalue * 256);
midisend(offset, msg1, msg23);
click_onx = 0;
);

click_ony ? (
// send the message
msg1 = ychannel + 176;
msg23 = yCC + (yvalue * 256);
midisend(offset, msg1, msg23);
click_ony = 0;
);

(slider5 == 1) ? (
msg1 = xchannel + 176;
msg23 = xCC | (63*256);
midisend(offset, msg1, msg23);
count +=1;
);
count >= 1 ? (
count = 0;
slider5 = OFF;
sliderchange(-1);
);

(slider5 == 2) ? (
msg1 = ychannel + 176;
msg23 = yCC | (63*256);
midisend(offset, msg1, msg23);
count +=1;
);
count >= 1 ? (
count = 0;
slider5 = OFF;
sliderchange(-1);
);
