~ubuntu-branches/ubuntu/karmic/scilab/karmic

« back to all changes in this revision

Viewing changes to macros/scicos/do_stupidmove.sci

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2002-03-21 16:57:43 UTC
  • Revision ID: james.westby@ubuntu.com-20020321165743-e9mv12c1tb1plztg
Tags: upstream-2.6
ImportĀ upstreamĀ versionĀ 2.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
function [%pt,scs_m]=do_stupidmove(%pt,scs_m)
 
2
// Copyright INRIA
 
3
rela=.1
 
4
// get a scicos object to move, and move it with connected objects
 
5
//!
 
6
//get block to move
 
7
while %t
 
8
  
 
9
  
 
10
  
 
11
  if %pt==[] then
 
12
    [btn,xc,yc,win,Cmenu]=cosclick()
 
13
    if Cmenu<>[] then
 
14
      %pt=[]
 
15
      [Cmenu]=resume(Cmenu)
 
16
    elseif btn>31 then
 
17
      Cmenu=%tableau(min(100,btn-31));%pt=[xc;yc];
 
18
      if Cmenu==emptystr() then Cmenu=[];%pt=[];end
 
19
      [%win,Cmenu]=resume(win,Cmenu)
 
20
    end
 
21
  else
 
22
    xc=%pt(1);yc=%pt(2);win=%win;%pt=[]
 
23
  end
 
24
 
 
25
  [k,wh,scs_m]=stupid_getobj(scs_m,[xc;yc])
 
26
  if k<>[] then break,end
 
27
end
 
28
 
 
29
scs_m_save=scs_m
 
30
if scs_m(k)(1)=='Block'|scs_m(k)(1)=='Text' then
 
31
  needreplay=replayifnecessary()
 
32
  scs_m=stupid_moveblock(scs_m,k,xc,yc)
 
33
elseif scs_m(k)(1)=='Link' then
 
34
    scs_m=stupid_movecorner(scs_m,k,xc,yc,wh)
 
35
end
 
36
[scs_m_save,enable_undo,edited,nc_save,needreplay]=resume(scs_m_save,%t,%t,needcompile,needreplay)
 
37
 
 
38
function scs_m=stupid_moveblock(scs_m,k,xc,yc)
 
39
// Move  block k and modify connected links if any
 
40
//!
 
41
//look at connected links
 
42
dr=driver()
 
43
connectedi=[get_connected(scs_m,k,'in'),get_connected(scs_m,k,'clkin')];
 
44
connectedo=[get_connected(scs_m,k,'out'),get_connected(scs_m,k,'clkout')];
 
45
o=scs_m(k)
 
46
xx=[];yy=[];ii=[];clr=[];mx=[];my=[]
 
47
 
 
48
// build movable segments for all connected links
 
49
//===============================================
 
50
xm=[];ym=[];
 
51
for i=connectedi
 
52
  oi=scs_m(i)
 
53
  driver(dr)
 
54
 
 
55
  draw_link_seg(oi,$-1:$) //erase link
 
56
  if pixmap then xset('wshow'),end
 
57
  [xl,yl,ct,from,to]=oi([2,3,7:9])
 
58
  clr=[clr ct(1)]
 
59
  nl=prod(size(xl))
 
60
  
 
61
  if dr=='Rec' then driver('X11'),end
 
62
//  xpolys(xl($-1:$),yl($-1:$),ct(1))// redraw thin link
 
63
 
 
64
  xm=[xm,xl($-1:$)];ym=[ym,yl($-1:$)];
 
65
end
 
66
 
 
67
for i=connectedo
 
68
  oi=scs_m(i)
 
69
  driver(dr)
 
70
  draw_link_seg(oi,1:2) //erase link
 
71
  if pixmap then xset('wshow'),end
 
72
  [xl,yl,ct,from,to]=oi([2,3,7:9])
 
73
  clr=[clr ct(1)]
 
74
  nl=prod(size(xl))
 
75
  
 
76
  if dr=='Rec' then driver('X11'),end
 
77
//  xpolys(xl,yl,default_color(0))// redraw thin link
 
78
 
 
79
  xm=[xm,[xl(2);xl(1)]];ym=[ym,[yl(2);yl(1)]];
 
80
end
 
81
 
 
82
xmt=xm;ymt=ym;
 
83
// move a block and connected links
 
84
//=================================
 
85
[ff,nii]=size(connectedi)
 
86
[ff,noo]=size(connectedo)
 
87
//clr=ones(mxx,nxx)*default_color(0)
 
88
if nii+noo>0 then // move a block and connected links
 
89
  [xmin,ymin]=getorigin(o)
 
90
  xco=xc;yco=yc;
 
91
  rep(3)=-1
 
92
  [xy,sz]=o(2)(1:2)
 
93
  // clear block
 
94
  driver(dr)
 
95
  drawobj(o)
 
96
  if dr=='Rec' then driver('X11'),end
 
97
  dx=xc-xmin;dy=yc-ymin;
 
98
//  xpolys(xmt,ymt,clr)// erase moving part of links
 
99
  while rep(3)==-1 ,  // move loop
 
100
    xmt(2,:)=xm(2,:)-xco+xc; ymt(2,:)=ym(2,:)-yco+yc;
 
101
    // draw block shape
 
102
    xrect(xc-dx,yc+sz(2)-dy,sz(1),sz(2))
 
103
    // draw moving links
 
104
  xpolys(xmt,ymt,clr)// draw moving part of links
 
105
    // get new position
 
106
    if pixmap then xset('wshow'),end    
 
107
    rep=xgetmouse(0);
 
108
    // clear block shape
 
109
    xrect(xc-dx,yc+sz(2)-dy,sz(1),sz(2))
 
110
    // clear moving part of links
 
111
  xpolys(xmt,ymt,clr)// erase moving part of links
 
112
    xc=rep(1);yc=rep(2)
 
113
  end
 
114
    xy=[xc-dx,yc-dy];
 
115
 
 
116
  
 
117
  // update and draw block
 
118
  if rep(3)<>2 then o(2)(1)=xy;  scs_m(k)=o;end
 
119
  driver(dr)
 
120
  drawobj(o)
 
121
  if pixmap then xset('wshow'),end
 
122
 
 
123
j=0
 
124
for i=connectedi
 
125
  oi=scs_m(i)
 
126
  if rep(3)<>2 then 
 
127
  j=j+1
 
128
  oi(2)($-1:$)=xmt(:,j)
 
129
  oi(3)($-1:$)=ymt(:,j)
 
130
  scs_m(i)=oi
 
131
  end
 
132
  draw_link_seg(oi,$-1:$) //draw link
 
133
  if pixmap then xset('wshow'),end
 
134
end
 
135
 
 
136
for i=connectedo
 
137
  oi=scs_m(i)
 
138
  if rep(3)<>2 then 
 
139
  j=j+1
 
140
  oi(2)(1:2)=xmt([2,1],j)
 
141
  oi(3)(1:2)=ymt([2,1],j)
 
142
  scs_m(i)=oi
 
143
end
 
144
  draw_link_seg(oi,1:2) //draw link
 
145
  if pixmap then xset('wshow'),end
 
146
end
 
147
 
 
148
else // move an unconnected block
 
149
  rep(3)=-1
 
150
  [xy,sz]=o(2)(1:2)
 
151
  // clear block
 
152
  drawobj(o)
 
153
  dr=driver()
 
154
  if dr=='Rec' then driver('X11'),end
 
155
  while rep(3)==-1 , //move loop
 
156
    // draw block shape
 
157
    xrect(xc,yc+sz(2),sz(1),sz(2))
 
158
    if pixmap then xset('wshow'),end
 
159
    // get new position
 
160
    rep=xgetmouse(0)
 
161
    // clear block shape
 
162
    xrect(xc,yc+sz(2),sz(1),sz(2))
 
163
    xc=rep(1);yc=rep(2)
 
164
    xy=[xc,yc];
 
165
 
 
166
  end
 
167
  // update and draw block
 
168
  if rep(3)<>2 then o(2)(1)=xy,scs_m(k)=o,end
 
169
  driver(dr)
 
170
  drawobj(o)
 
171
  if pixmap then xset('wshow'),end
 
172
end
 
173
 
 
174
function scs_m=stupid_movecorner(scs_m,k,xc,yc,wh)
 
175
o=scs_m(k)
 
176
[xx,yy,ct]=o([2 3 7])
 
177
dr=driver()
 
178
 
 
179
//wh=[-wh-1 -wh]
 
180
 
 
181
seg=[-wh-1:-wh+1]
 
182
 
 
183
if dr=='Rec' then driver('X11'),end
 
184
 
 
185
//draw_link_seg(o,seg)
 
186
 
 
187
xpolys(xx(seg),yy(seg),ct(1)) //draw thin link
 
188
if pixmap then xset('wshow'),end
 
189
X1=xx(seg)
 
190
Y1=yy(seg)
 
191
x1=X1;y1=Y1;
 
192
 
 
193
xpolys(x1,y1,ct(1)) //erase moving part of the link
 
194
rep(3)=-1
 
195
 
 
196
while rep(3)==-1 do
 
197
  xpolys(x1,y1,ct(1))//draw moving part of the link
 
198
  rep=xgetmouse(0);
 
199
  if pixmap then xset('wshow'),end
 
200
  xpolys(x1,y1,ct(1))//erase moving part of the link
 
201
  xc1=rep(1);yc1=rep(2)
 
202
  x1(2)=X1(2)-(xc-xc1)
 
203
  y1(2)=Y1(2)-(yc-yc1)
 
204
end
 
205
if rep(3)<>2 then
 
206
//[frect1,frect]=xgetech();
 
207
//eps=0.04*min(abs(frect(3)-frect(1)),abs(frect(4)-frect(2)))
 
208
if abs(x1(1)-x1(2))<rela*abs(y1(1)-y1(2)) then
 
209
  x1(2)=x1(1)
 
210
elseif abs(x1(2)-x1(3))<rela*abs(y1(2)-y1(3))then
 
211
  x1(2)=x1(3)
 
212
end  
 
213
if abs(y1(1)-y1(2))<rela*abs(x1(1)-x1(2)) then
 
214
  y1(2)=y1(1)
 
215
elseif abs(y1(2)-y1(3))<rela*abs(x1(2)-x1(3)) then
 
216
  y1(2)=y1(3)
 
217
end  
 
218
d=projaff([x1(1);x1(3)],[y1(1);y1(3)],[x1(2);y1(2)])
 
219
if norm(d(:)-[x1(2);y1(2)])<..
 
220
        rela*max(norm(d(:)-[x1(3);y1(3)]),norm(d(:)-[x1(1);y1(1)])) then
 
221
  xx(seg)=x1
 
222
  yy(seg)=y1
 
223
  xx(seg(2))=[]
 
224
  yy(seg(2))=[]
 
225
  x1(2)=[];y1(2)=[];seg(3)=[]
 
226
else
 
227
  xx(seg)=x1
 
228
  yy(seg)=y1
 
229
end
 
230
 
 
231
//xpolys(x1,y1,ct(1))//draw moving part of the link
 
232
//xpolys(xx,yy,ct(1)) //erase thin link
 
233
//if rep(3)<>2 then
 
234
  o(2)=xx;o(3)=yy
 
235
  scs_m(k)=o
 
236
end
 
237
driver(dr)
 
238
draw_link_seg(o,seg)
 
239
if pixmap then xset('wshow'),end
 
240
 
 
241
function [k,wh,objs]=stupid_getobj(objs,pt)
 
242
n=size(objs)
 
243
wh=[];
 
244
x=pt(1);y=pt(2)
 
245
data=[]
 
246
k=[]
 
247
for i=2:n //loop on objects
 
248
  o=objs(i)
 
249
  if o(1)=='Block' then
 
250
    graphics=o(2)
 
251
    [orig,sz]=graphics(1:2)
 
252
    data=[(orig(1)-x)*(orig(1)+sz(1)-x),(orig(2)-y)*(orig(2)+sz(2)-y)]
 
253
    if data(1)<0&data(2)<0 then k=i,break,end
 
254
  elseif o(1)=='Link' then
 
255
    [frect1,frect]=xgetech();
 
256
    eps=4     //0.01*min(abs(frect(3)-frect(1)),abs(frect(4)-frect(2)))
 
257
    xx=o(2);yy=o(3);
 
258
    [d,ptp,ind]=stupid_dist2polyline(xx,yy,pt,.85)
 
259
    if d<eps then 
 
260
       if ind==-1 then k=o(8)(1),break,
 
261
       elseif ind==-size(xx,1) then k=o(9)(1),break,
 
262
       elseif ind>0 then 
 
263
 
 
264
          draw_link_seg(o,[ind,ind+1])
 
265
          o(2)=[xx(1:ind);ptp(1);xx(ind+1:$)];o(3)=[yy(1:ind);ptp(2);yy(ind+1:$)];
 
266
//          draw_link_seg(o,[ind,ind+2])
 
267
          objs(i)=o
 
268
       k=i,wh=-ind-1,break,
 
269
       else k=i,wh=ind,draw_link_seg(o,[-ind-1:-ind+1]);break,end
 
270
    end
 
271
  elseif o(1)=='Text' then
 
272
    graphics=o(2)
 
273
    [orig,sz]=graphics(1:2)
 
274
    data=[(orig(1)-x)*(orig(1)+sz(1)-x),(orig(2)-y)*(orig(2)+sz(2)-y)]
 
275
    if data(1)<0&data(2)<0 then k=i,break,end
 
276
  end
 
277
end
 
278
 
 
279
function [d,pt,ind]=stupid_dist2polyline(xp,yp,pt,pereps)
 
280
// computes minimum distance from a point to a polyline
 
281
//d    minimum distance to polyline
 
282
//pt   coordinate of the polyline closest point
 
283
//ind  
 
284
//     if negative polyline closest point is a polyline corner:pt=[xp(-ind) yp(-ind)]
 
285
//     if positive pt lies on segment [ind ind+1]
 
286
 
 
287
x=pt(1)
 
288
y=pt(2)
 
289
xp=xp(:);yp=yp(:)
 
290
cr=4*sign((xp(1:$-1)-x).*(xp(1:$-1)-xp(2:$))+..
 
291
          (yp(1:$-1)-y).*(yp(1:$-1)-yp(2:$)))+..
 
292
    sign((xp(2:$)-x).*(xp(2:$)-xp(1:$-1))+..
 
293
          (yp(2:$)-y).*(yp(2:$)-yp(1:$-1)))
 
294
 
 
295
ki=find(cr==5) // index of segments for which projection fall inside
 
296
np=size(xp,'*')
 
297
if ki<>[] then
 
298
  //projection on segments
 
299
  x=[xp(ki) xp(ki+1)]
 
300
  y=[yp(ki) yp(ki+1)]
 
301
  dx=x(:,2)-x(:,1)
 
302
  dy=y(:,2)-y(:,1)
 
303
  d_d=dx.^2+dy.^2
 
304
  d_x=( dy.*(-x(:,2).*y(:,1)+x(:,1).*y(:,2))+dx.*(dx*pt(1)+dy*pt(2)))./d_d
 
305
  d_y=(-dx.*(-x(:,2).*y(:,1)+x(:,1).*y(:,2))+dy.*(dx*pt(1)+dy*pt(2)))./d_d
 
306
  xp=[xp;d_x]
 
307
  yp=[yp;d_y]
 
308
end
 
309
 
 
310
zzz=[ones(np,1);zeros(size(ki,'*'),1)]*eps
 
311
zz=[ones(np,1)*pereps;ones(size(ki,'*'),1)]
 
312
[d,k]=min(sqrt((xp-pt(1)).^2+(yp-pt(2)).^2).*zz-zzz) 
 
313
pt(1)=xp(k)
 
314
pt(2)=yp(k)
 
315
if k>np then ind=ki(k-np),else ind=-k,end
 
316
 
 
317
 
 
318
 
 
319
 
 
320
function draw_link_seg(o,seg)
 
321
  ct=o(7);c=ct(1),pos=o(6)
 
322
  if pos(2)>=0 then
 
323
    d=xget('dashes')
 
324
    thick=xget('thickness')
 
325
    t=maxi(pos(1),1)*maxi(pos(2),1)
 
326
    xset('thickness',t)
 
327
    xset('dashes',c)
 
328
    xpoly(o(2)(seg),o(3)(seg),'lines')
 
329
    xset('dashes',d)
 
330
    xset('thickness',thick)
 
331
  end
 
332
 
 
333
 
 
334
 
 
335
 
 
336
 
 
337
 
 
338
 
 
339
 
 
340
 
 
341
 
 
342
 
 
343
 
 
344