您好,我一直在努力嘗試製作類似(JMP Pro)SEM 和可靠性框圖中使用的拖放介面。
我的想法是在螢幕左側放置一排“方框”。我希望能夠選中其中一個方框,把它拖到螢幕中央區域,放下,然後再選中另一個方框,繼續拖放,以此類推。
我試過好幾種方法,包括用把手和捕鼠器。我能做到一部分,但做不到全部。
下面展示的是我使用 Mousetrap 編寫的最新程式碼。我可以抓住 GraphBox 左上角的方框,把它拖曳到螢幕上的任何位置,然後放到我想要的位置(放置後會變成黑色)。但之後我就無法再抓住另一個方框了。我只能移動剛剛放置的方框,無法新增新的方框。
我目前正在將其開發成應用程序,但如果這樣有幫助,我很樂意更改。
以下腳本展示了我在應用程式建構器中編寫的程式碼。附件包含完整的應用程式。
我擔心我對 Mousetrap、GraphBoxes 和添加圖形腳本的某些方面還不理解。有人能幫幫我嗎?
// This script is executed when a new module instance is
// created. The pre-defined object "thisModuleInstance" refers
// to the instance object, but other objects such as boxes and
// scripts have not yet been created. Variables declared here are
// scoped to the ModuleInstance namespace.
// This special function will receive parameters passed to CreateInstance()
OnModuleLoad({},
);
thisModuleInstance << Create Objects;
// After this point your module instance objects have been created
// and can be referred to by name (for example, "Button1").
x0=5;
y0=95;
winref = (this module instance << Get Box);
// set graph box scales
winref[framebox(1)] << xaxis(0,100);
winref[framebox(1)] << yaxis(0,100);
// draw box in upper left corner.
pen color("Green");
winref[framebox(1)] << add graphics script(
pen color("Green");
Line({0,90}, {10,90},{10,100}, {0,100},{0,90}));
winref[framebox(1)] << Add Graphics Script(
Mousetrap(
// drag script (mouse down)
(
// if mouse location is within box in upper left corner, then OK to drag.
if(
and(
x>=x0-5,
y>=y0-5,
x<=x0+5,
y<=y0+5
),
(
// set x0 and y0 if that were true
x0 = x;
y0 = y;
// redraw the box at current mouse location while mouse button is down
winref[framebox(1)] << add graphics script(
"Front",
pen color("Green");
Line({x0-5, y0-5}, {x0+5, y0-5}, {x0+5, y0+5}, {x0-5, y0+5}, {x0-5, y0-5}));
)
);
),
(
// mouse up script
// draw a polygon centered at the current mouse location
winref[framebox(1)] << add graphics script(
polygon({x0-5,y0-5},{x0+5,y0-5},{x0+5,y0+5},{x0-5,y0+5},{x0-5,y0-5})
);
// at this point I want to manually reposition the mouse over the original box in the
// upper left, and grab another box.
// Instead, all I can do is grab the box I just created and drag it somewhere else.
)
);
);
您很可能不想一直添加新的圖形腳本,因為這最終會覆蓋您的 If() 函數並「淹沒」幀。

你可以用更好的方式來實現這個功能,但以下有一個你可以嘗試的範例。
Names Default To Here(1);
rects = {};
x0 = 5;
y0 = 95;
x = .;
y = .;
nw = New Window("Example",
Graph Box(
Frame Size(200, 200),
Mousetrap(
x0 = x;
y0 = y;
,
p = EvalExpr(Polygon(
{Expr(x0+5), Expr(y0-5)},
{Expr(x0+5), Expr(y0+5)},
{Expr(x0-5), Expr(y0+5)},
{Expr(x0-5), Expr(y0-5)}
));
Insert Into(rects, Name Expr(p));
);
Pen color("Green");
Line({0,90}, {10,90},{10,100}, {0,100},{0,90});
If(x >= x0 - 5 & y >= y0 - 5 & x <= x0 + 5 & y <= y0 + 5,
Pen color("Green");
Line({x0-5, y0-5}, {x0+5, y0-5}, {x0+5, y0+5}, {x0-5, y0+5}, {x0-5, y0-5});
);
For(i = 1, i <= N Items(rects), i++,
Pen Color("Black");
Eval(rects[i]);
);
);
);
</CODE></P>
<P><SPAN class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jthi_1-1766523967638.png" style="width: 400px;"><SPAN class="lia-inline-image-display-wrapper" image-alt="jthi_1-1766523967638.png" style="width: 260px;"><SPAN class="lia-inline-image-display-wrapper" image-alt="jthi_1-1766523967638.png" style="width: 260px;"><SPAN class="lia-inline-image-display-wrapper" image-alt="jthi_1-1766523967638.png" style="width: 260px;"><SPAN class="lia-inline-image-display-wrapper" image-alt="jthi_1-1766523967638.png" style="width: 260px;"><SPAN class="lia-inline-image-display-wrapper" image-alt="jthi_1-1766523967638.png" style="width: 260px;"><SPAN class="lia-inline-image-display-wrapper" image-alt="jthi_1-1766523967638.png" style="width: 260px;"><SPAN class="lia-inline-image-display-wrapper" image-alt="jthi_1-1766523967638.png" style="width: 260px;"><span class="lia-inline-image-display-wrapper" image-alt="jthi_1-1766523967638.png" style="width: 260px;"><img src="https://us.v-cdn.net/6038925/uploads/images/89563i0F98FCB07A4441BC/jthi_1-1766523967638.png" role="button" title="jthi_1-1766523967638.png" alt="jthi_1-1766523967638.png" /></span></SPAN></SPAN></SPAN></SPAN></SPAN></SPAN></SPAN></SPAN></P>
<BR />
<P>編輯:新增了 x 和 y 的初始化</P>
</LINGO-BODY>
<LINGO-SUB id="lingo-sub-921160" slang="en-US" mode="UPDATE" translate="no">回覆:製作拖放腳本</LINGO-SUB>
<LINGO-BODY id="lingo-body-921160" slang="en-US" mode="UPDATE">
<P>我得到:</P>
<P><FONT face="courier new,courier">顯示框[EvalContextBox]</FONT><BR /><FONT face="courier new,courier">名稱未解析:存取或評估「x」時出現 x 錯誤,x/*###*/</FONT></P>
<P><FONT face="courier new,courier">第 26 行</FONT><BR /><BR /></P>
<P>x&y 在捕鼠器之外是否仍然存在?</P>
<P><SPAN class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="hogi_0-1766525524905.png" style="width: 999px;"><SPAN class="lia-inline-image-display-wrapper" image-alt="hogi_0-1766525524905.png" style="width: 784px;"><span class="lia-inline-image-display-wrapper" image-alt="hogi_0-1766525524905.png" style="width: 784px;"><img src="https://us.v-cdn.net/6038925/uploads/images/89564iE967B41113B5D41E/hogi_0-1766525524905.png" role="button" title="hogi_0-1766525524905.png" alt="hogi_0-1766525524905.png" /></span></SPAN></SPAN></P>
<P><STRONG>編輯</STRONG>:<BR />它們“在捕鼠器之外仍然存在”<BR />但並非永久如此。似乎當滑鼠按鈕鬆開時,它們就會重置:<BR /> <A href="https://community.jmp.com/t5/Discussions/Can-JMP-graphics-have-this-effect/m-p/921213/highlight/true#M107999" target="_blank">https://community.jmp.com/t5/Discussions/Can-JMP-graphics-have-this-effect/mp/921213/highlight/true#M107999</A></P>
<BR />
<P>太棒了,有了綠色矩形,拖曳的錯覺非常逼真;)<BR />如果沒有綠色矩形,感覺就像是:每次滑鼠點擊都會新增一個黑色矩形。</P>
<P></P>
<iframe src="https://players.brightcove.net/6058004218001/default_default/index.html?videoId=6386912122112" width="280" height="280" allowfullscreen allow="encrypted-media"></iframe>
<P></P>
</LINGO-BODY>
<LINGO-SUB id="lingo-sub-921187" slang="en-US" mode="CREATE" translate="no">回覆:製作拖放腳本</LINGO-SUB>
<LINGO-BODY id="lingo-body-921187" slang="en-US" mode="CREATE">
<P>我之前很可能在記憶體中初始化了 x 和 y 的值,所以腳本才無法運作。我已經更新了腳本,重新初始化了 x 和 y 的值,現在在新的 JMP 會話中似乎可以正常工作了。</P>
</LINGO-BODY>
<LINGO-SUB id="lingo-sub-921211" slang="en-US" mode="CREATE" translate="no">回覆:製作拖放腳本</LINGO-SUB>
<LINGO-BODY id="lingo-body-921211" slang="en-US" mode="CREATE">
<P>感謝<A href="https://community.jmp.com/t5/user/viewprofilepage/user-id/14366" target="_blank">@jthi</A> (以及<A href="https://community.jmp.com/t5/user/viewprofilepage/user-id/26800" target="_blank">@hogi</A>的貢獻)!我應該能夠根據我的需求進行調整。在這個過程中,我還了解了一些關於 Mousetrap、Add Graphics Script 和 Name Expr 的知識。</P>
<P>JMP社群的反應速度和專業知識總是讓我驚嘆不已。非常感謝!</P>
</LINGO-BODY>
<LINGO-SUB id="lingo-sub-921219" slang="en-US" mode="UPDATE" translate="no">回覆:製作拖放腳本</LINGO-SUB>
<LINGO-BODY id="lingo-body-921219" slang="en-US" mode="UPDATE">
<P>這是<A href="https://community.jmp.com/t5/user/viewprofilepage/user-id/14366" target="_blank">@jthi</A>腳本的高級版本:</P>
<P>- 現有框可以拖曳。</P>
<P>- 當使用者點擊沒有方框的區域時,不會建立或移動任何方框。</P>
<P>- 頂部的初始盒子就像一個無限的盒子庫。</P>
<P></P>
<iframe src="https://players.brightcove.net/6058004218001/default_default/index.html?videoId=6386914707112" width="276" height="280" allowfullscreen allow="encrypted-media"></iframe>
<P></P>
<BR />
<PRE><CODE class=" language-jsl">Names Default To Here( 1 );
squares = {{5, 95}};
x = .;
y = .;
mouseDown = 1;
foundOne = 0;
nw = New Window( "Example",
Graph Box(
Frame Size( 200, 200 ),
Mousetrap(
// find a neighboring square
If( mouseDown,
x0 = x;
y0 = y;
If( N Items( squares ),
kt = KDTable( Matrix( squares ) );
{row, dist} = kt << K nearest rows( {1, 10}, Matrix( {x0, y0} ) );
If( N Items( row ) & dist[1] < 10,
If( row > 1, // keep the one at the top corner
Remove From( squares, row[1] )
);
foundOne = 1;
);
);
);
mouseDown = 0;,
// mouse relesed -> save the square
If( foundOne,
Insert Into( squares, Eval List( {Eval List( {x, y} )} ) )
);
//reset
mouseDown = 1;
foundOne = 0;
);
Pen Color( "Green" );
Line( {0, 90}, {10, 90}, {10, 100}, {0, 100}, {0, 90} ); // 1st square to start with
// dragged square
If( Not( Is Missing( x ) ) & foundOne,
Line( {x - 5, y - 5}, {x + 5, y - 5}, {x + 5, y + 5}, {x - 5, y + 5}, {x - 5, y - 5} )
);
For( i = 1, i <= N Items( squares ), i++,
Pen Color( "Black" );
Eval(
{x0, y0} = squares[i];
Polygon( {x0 + 5, y0 - 5}, {x0 + 5, y0 + 5}, {x0 - 5, y0 + 5}, {x0 - 5, y0 - 5} );
);
);
)
);