File size: 4,985 Bytes
806d4b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207

I64 box_x_min,box_x_max,box_y_min,box_y_max;

class Arrow
{
  Arrow *next,*last;
  F64 x,y,dx,dy;
} head;

   <1>/* Graphics Not Rendered in HTML */

Bool bow_drawn;
F64 bow_x,bow_y,bow_theta;

U0 DrawIt(CTask *task,CDC *dc)
{
  F64 theta,x,y,dx,dy,
        str_w,str_h,draw_len;
  Arrow *tmpa;
  CD3I32 ctrl[5];
  dc->color=RED;
  GrBorder(dc,box_x_min,box_y_min,box_x_max,box_y_max);

  x=ClampI64(ms.pos.x-task->pix_left-task->scroll_x,box_x_min,box_x_max);
  y=ClampI64(ms.pos.y-task->pix_top-task->scroll_y, box_y_min,box_y_max);
  dx=bow_x-x;
  dy=bow_y-y;

  if (bow_drawn && (dx|dy))
    bow_theta=Arg(dx,dy);
  else {
    bow_x=x;
    bow_y=y;
  }

  draw_len=Sqrt(dx*dx+dy*dy);
  str_w=draw_len/3;
  str_h=Sqrt(60*60-str_w*str_w);

  dc->color=BLACK;
  GrLine(dc,x-str_h/2*Cos(bow_theta+pi/2)+str_w*Cos(bow_theta),
        y-str_h/2*Sin(bow_theta+pi/2)+str_w*Sin(bow_theta),
        x,y);
  GrLine(dc,x+str_h/2*Cos(bow_theta+pi/2)+str_w*Cos(bow_theta),
        y+str_h/2*Sin(bow_theta+pi/2)+str_w*Sin(bow_theta),
        x,y);

  MemSet(ctrl,0,sizeof(ctrl));
  ctrl[0].x=x-str_h/2*Cos(bow_theta+pi/2)+str_w*Cos(bow_theta);
  ctrl[0].y=y-str_h/2*Sin(bow_theta+pi/2)+str_w*Sin(bow_theta);
  ctrl[1].x=x-0.75*str_h/2*Cos(bow_theta+pi/2)+draw_len/2*Cos(bow_theta)+
        str_w*Cos(bow_theta);
  ctrl[1].y=y-0.75*str_h/2*Sin(bow_theta+pi/2)+draw_len/2*Sin(bow_theta)+
        str_w*Sin(bow_theta);
  ctrl[2].x=x+draw_len/2*Cos(bow_theta)+str_w*Cos(bow_theta);
  ctrl[2].y=y+draw_len/2*Sin(bow_theta)+str_w*Sin(bow_theta);
  ctrl[3].x=x+0.75*str_h/2*Cos(bow_theta+pi/2)+draw_len/2*Cos(bow_theta)+
        str_w*Cos(bow_theta);
  ctrl[3].y=y+0.75*str_h/2*Sin(bow_theta+pi/2)+draw_len/2*Sin(bow_theta)+
        str_w*Sin(bow_theta);
  ctrl[4].x=x+str_h/2*Cos(bow_theta+pi/2)+str_w*Cos(bow_theta);
  ctrl[4].y=y+str_h/2*Sin(bow_theta+pi/2)+str_w*Sin(bow_theta);

  dc->color=BROWN;
  dc->thick=2;
  Gr2BSpline3(dc,ctrl,5);
  dc->thick=1;

  if (bow_drawn)
    Sprite3ZB(dc,x,y,0,<1>,bow_theta);

  tmpa=head.next;
  while (tmpa!=&head) {
    theta=Arg(tmpa->dx,tmpa->dy);
    Sprite3ZB(dc,tmpa->x,tmpa->y,0,<1>,theta);
    tmpa=tmpa->next;
  }
}

#define ANIMATE_SLEEP_MS        10

U0 AnimateTask(I64)
{
  I64 x,y;
  Arrow *tmpa,*tmpa1;
  F64 dt,t0=tS;
  while (TRUE) {
    dt=tS-t0;
    t0=tS;

    x=ClampI64(ms.pos.x-Fs->parent_task->pix_left-Fs->parent_task->scroll_x,
          box_x_min,box_x_max)+Fs->parent_task->pix_left+
          Fs->parent_task->scroll_x;
    y=ClampI64(ms.pos.y-Fs->parent_task->pix_top-Fs->parent_task->scroll_y,
          box_y_min,box_y_max)+Fs->parent_task->pix_top+
          Fs->parent_task->scroll_y;
    if (ms.pos.x!=x || ms.pos.y!=y)
      MsSet(x,y);

    tmpa=head.next;
    while (tmpa!=&head) {
      tmpa1=tmpa->next;
      tmpa->x+=tmpa->dx*dt;
      tmpa->y+=tmpa->dy*dt;
      if (!(-Fs->parent_task->scroll_x<=
            tmpa->x<Fs->parent_task->pix_width-Fs->parent_task->scroll_x) ||
            !(-Fs->parent_task->scroll_y<=
            tmpa->y<Fs->parent_task->pix_height-Fs->parent_task->scroll_y)) {
        QueRem(tmpa);
        Free(tmpa);
      }
      tmpa=tmpa1;
    }
    Refresh;
  }
}

U0 Init()
{
  I64 w=Fs->pix_width,
        h=Fs->pix_height;
  QueInit(&head);
  bow_drawn=FALSE;
  box_x_min=7*w/16;
  box_y_min=6*h/8;
  box_x_max=9*w/16;
  box_y_max=7*h/8;
  bow_theta=-pi/2;
  bow_x=(box_x_min+box_x_max)/2;
  bow_y=(box_y_min+box_y_max)/2;
  MsSet(bow_x+Fs->pix_left+Fs->scroll_x,
        bow_y+Fs->pix_top+Fs->scroll_y);
}

U0 CleanUp()
{
  QueDel(&head,TRUE);
}

U0 Zing()
{
  I64 arg1,arg2;
  Arrow *tmpa;
  MenuPush(
        "File {"
        "  Abort(,CH_SHIFT_ESC);"
        "  Exit(,CH_ESC);"
        "}"
        "Play {"
        "  Restart(,'\n');"
        "}"
        );
  SettingsPush; //See SettingsPush
  AutoComplete;
  WinBorder;
  WinMax;
  DocCursor;
  DocClear;

  Init;
  Fs->animate_task=Spawn(&AnimateTask,NULL,"Animate",,Fs);
  Fs->draw_it=&DrawIt;
  Fs->win_inhibit=WIG_TASK_DFT-WIF_SELF_FOCUS-WIF_SELF_GRAB_SCROLL;
  try {
    while (TRUE)
      switch (GetMsg(&arg1,&arg2,
            1<<MSG_KEY_DOWN|1<<MSG_MS_L_DOWN|1<<MSG_MS_L_UP)) {
        case MSG_KEY_DOWN:
          switch (arg1) {
            case '\n':
              CleanUp;
              Init;
              break;
            case CH_ESC:
            case CH_SHIFT_ESC:
              goto zi_done;
          }
          break;
        case MSG_MS_L_DOWN:
          bow_x=arg1;
          bow_y=arg2;
          bow_drawn=TRUE;
          break;
        case MSG_MS_L_UP:
          if(arg1-bow_x || arg2-bow_y) {
            tmpa=MAlloc(sizeof(Arrow));
            tmpa->dx=10.0*(bow_x-arg1);
            tmpa->dy=10.0*(bow_y-arg2);
            tmpa->x=arg1;
            tmpa->y=arg2;
            QueIns(tmpa,head.last);
            Noise(50,110,114);
          }
          bow_drawn=FALSE;
          break;
      }
zi_done:
    GetMsg(,,1<<MSG_KEY_UP);
  } catch
    PutExcept;
  SettingsPop;
  CleanUp;
  MenuPop;
}

Zing;