Files
TempleOS-Rijn/LT/Demo/Graphics/Pick.CPZ
T
2018-09-08 21:45:20 +02:00

103 lines
3.2 KiB
Plaintext

$PI,"<1>",1$
#define NUM_APPLES 128
I8 x[NUM_APPLES],y[NUM_APPLES],
cur_apple,cur_sub_element,cur_dist,
cur_x=0,cur_y=0;
I1 buf[128];
void DrawIt(TssStruct *tss)
{
I8 i,best_dist=MAX_I8,best_apple=0,best_sub_element=0;
GrBitMap *base=GrAlias(grbase2,tss);
ClearWinText(tss);
base->flags|=BMF_LOCATE_NEAREST;
base->cur_x=cur_x;
base->cur_y=cur_y;
base->cur_z=0;
for (i=0;i<NUM_APPLES;i++) {
base->nearest_sub_grelem=0;
base->nearest_dist=MAX_I8;
GrElemsPlot(base,x[i],y[i],0,$IB,"<1>",1$);
if (base->nearest_dist<=best_dist) {
//$FG,2$distance to nearest pixel, squared$FG$
//$FG,2$$LK,"Sqrt","MN:Sqrt"$() is expensive so we work with distance squared.$FG$
best_dist=base->nearest_dist;
best_apple=i;
//$FG,2$Sub elements are the individual lines or whatever of the GrElem.$FG$
best_sub_element=base->nearest_sub_grelem;
}
}
//$FG,2$Note: window manager does not get preempted and$FG$
//$FG,2$this code runs in the window manger task.$FG$
cur_apple=best_apple;
cur_dist=best_dist;
cur_sub_element=best_sub_element;
base->color=WHITE;
GrBox(base,5,5,StrLen(buf)*FONT_WIDTH,FONT_HEIGHT);
base->color=BLACK;
GrText(base,5,5,buf);
}
void DoIt()
{
I8 i,cmd,p1,p2,best_i=0,old_x,old_y;
void *old_update=Fs->update_win;
BoolU4 old_ip_double=Bts(&Fs->crt_flags,CRTf_NO_DOUBLE_CLICK);
BoolU4 old_inhibit=ip_inhibit_win_operations;
ip_inhibit_win_operations=TRUE;
for (i=0;i<NUM_APPLES;i++) {
x[i]=RandU2%GR_WIDTH;
y[i]=RandU2%GR_HEIGHT;
}
*buf=0;
Fs->update_win=&DrawIt;
do {
cmd=GetMsg(&p1,&p2,1<<MSG_KEY_DOWN|1<<MSG_IP_L_DOWN|1<<MSG_IP_R_DOWN);
//$FG,2$Use screen coordinates, not window (p1,p2)$FG$
//$FG,2$$LK,"ipx","MN:ipx"$ and $LK,"ipy","MN:ipy"$ are global variables updated$FG$
//$FG,2$updated by the window manager to hold the $FG$
//$FG,2$screen coordinates of the input pointer (mouse or joystick).$FG$
cur_x=ipx; cur_y=ipy;
if (cmd==MSG_IP_L_DOWN) {
//$FG,2$Wait for window manager to call $FG,4$DrawIt$FG$
WinSync;
best_i=cur_apple;
old_x=x[best_i];
old_y=y[best_i];
SPrintF(buf,"Sub Elem #%d Dist Squared:%d",cur_sub_element,cur_dist);
do {
cmd=GetMsg(&p1,&p2,1<<MSG_KEY_DOWN|1<<MSG_IP_MOVE|1<<MSG_IP_L_UP|1<<MSG_IP_R_DOWN);
if (cmd!=MSG_KEY_DOWN) {
x[best_i]=ipx-cur_x+old_x;
y[best_i]=ipy-cur_y+old_y;
}
} while (cmd!=MSG_KEY_DOWN && cmd!=MSG_IP_L_UP && cmd!=MSG_IP_R_DOWN);
}
} while (cmd!=MSG_KEY_DOWN && cmd!=MSG_IP_R_DOWN);
Fs->update_win=old_update;
AssignBit(&Fs->crt_flags,CRTf_NO_DOUBLE_CLICK,old_ip_double);
ip_inhibit_win_operations=old_inhibit;
}
DoIt;
¿      
   


    ûÿÿÿûÿÿÿúÿÿÿÿÿÿÿüÿÿÿ