如何使用代码(不仅仅是按钮)控制触发状态(暂停、播放) [英] How to control Trigger state (Pause, Play) using code (not just buttons)

查看:24
本文介绍了如何使用代码(不仅仅是按钮)控制触发状态(暂停、播放)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

触发器对于动画很有用,但我无法找到一种方法来更改代码中触发器的状态(即无需自己点击暂停或播放按钮).

A trigger is useful to use for animation, but I am not able to find a way to change the state of the trigger in the code (i.e. without having to hit the Pause or Play button myself).

例如,假设我想做一个模拟,当某个事件发生时,我想让当前活动的触发器进入暂停状态,而当另一个事件发生时,我想让触发器进入播放状态.

For example, suppose I want to do a simulation where when some event happen, I want to make the currently active trigger go to a PAUSE state, and when another event happen, I want the trigger to go to PLAY state.

执行此操作的按钮仍然存在,但我还希望能够从代码中更改这些,而无需亲自动手.

The buttons to do that will still be there, but I want to also be able to change these from the code without having to physically do it.

原因是,我正在执行某些操作,并且在执行其他操作时触发器处于播放模式会使事情无法正常工作.

The reason is, I am doing some action, and having the trigger being in PLAY mode while I am doing this other action is making things not working.

所以我需要让它进入 PAUSE 状态,当我完成后,我可以将它设置回 PLAY 状态.

So I need to make it go to PAUSE state, and when I am done, I can set it back to PLAY state.

这是我的意思的一个小例子:

Here is a small example of what I mean:

Manipulate[

EventHandler[
   Dynamic@Graphics[
     {Circle[{0,0},1], Text[n,pt] },
     PlotRange->All,ImageSize->200,ImagePadding->10],
   {
     "MouseDown":>
     (
      (* What to do here to cause the trigger to become Paused?"*)
      pt=MousePosition["Graphics"]
     ),

     "MouseDragged":>
    (
     (* while dragging, the trigger remains in PAUSED state "*)
      Print["mouse dragged"];
      pt=MousePosition["Graphics"]
    ),

    "MouseUp":>
   (
     Print["MouseUp"]
     (* What to do here to cause the trigger to Play again?"*)
    )
  }
  ],
     Control[{{n,0,"Run"},0,100,0.01,
            ControlType->Trigger, DisplayAllSteps->True, AnimationRate->1,
            AppearanceElements->{"PlayPauseButton","ResetButton"}}
     ],

     {{pt,{0,0}},ControlType->None}
]

在上面,当我在显示器上拖动鼠标时,我希望触发器变为 PAUSED,以便在拖动时显示的数字不会改变.完成拖动后,如果需要,我可以再次使触发器播放.

In above, when I drag the mouse on the display, I want the trigger to become PAUSED so that the number shown is not changing while being dragged. When done with dragging, I can then make the trigger PLAY again if needed.

那么,我的问题是:有没有办法像上面代码那样改变触发器状态?

So, my question: Is there a way to change trigger state like the above in the code?

我当然可以完全不使用触发器,并以其他方式自己编写所有代码,但在我放弃之前想过问一下,因为触发器使用起来很方便.

I can ofcourse not use trigger at all, and code everything myself in other ways, but thought to ask before I give up, as trigger is convenient to use.

这里是更多触发器和按钮文档的链接.

Here is a link to more documentation of trigger and the buttons.

我发现最接近的是 Enabled-> 触发选项,但这只会使触发器本身不启用,并且不会影响触发器状态.即如果触发器正在触发,即使我禁用它也会保持触发.

The closest thing I found is the Enabled-> option to trigger, but this just makes the trigger itself enabled to not, and does not affect the trigger state. i.e. if trigger is firing, it will remain firing even if I make disabled.

http://reference.wolfram.com/mathematica/ref/Manipulator.html

http://reference.wolfram.com/mathematica/ref/Trigger.html

谢谢

推荐答案

可能有更简单的方法可以做到这一点,但这似乎有效.它基本上是通过创建计划任务并在按下或释放鼠标按钮或单击播放/暂停按钮时停止和启动它来模仿 Trigger.

There is probably an easier way to do this, but this seems to work. It's basically mimicking a Trigger by creating a scheduled task and stopping and starting it when a mouse button is pressed or released or when the Play/Pause button is clicked.

DynamicModule[{start = 0, finish = 100, dt = 0.01, running = False, task, n},
 n = start;
 Manipulate[
  EventHandler[
   Dynamic@
    Graphics[{Circle[{0, 0}, 1], Text[n, pt]}, PlotRange -> All, 
     ImageSize -> 200, ImagePadding -> 10],
   {
    "MouseDown" :>
     (StopScheduledTask[task]; pt = MousePosition["Graphics"]),

    "MouseDragged" :>
     (Print["mouse dragged"]; pt = MousePosition["Graphics"]),

    "MouseUp" :> 
     (If[running, StartScheduledTask[task]]; Print["MouseUp"])
   }],

  Control[Labeled[
   Row[{
    Button[
     Dynamic@If[running, Magnify["\[DoubleVerticalBar]", 1.5], 
       Magnify["\[RightPointer]", 1.5]], 
     (If[running, running = False; StopScheduledTask[task], 
       running = True; StartScheduledTask[task]]), 
     Appearance -> "Palette", ImageSize -> 15, ContentPadding -> False],

    Button[
     Magnify["\[FirstPage]", 1.5], 
     (n = start; ResetScheduledTask[task]), 
     Appearance -> "Palette", ImageSize -> 15, ContentPadding -> False]
   }], "Run", Left]
  ],

  {{pt, {0, 0}}, ControlType -> None}
 ], 

 Initialization :> (task = 
    CreateScheduledTask[n += dt, {dt, Floor[(finish - start)/dt]}]),
 Deinitialization :> RemoveScheduledTask[task]
]

编辑:更改了控件的外观,使其看起来更像传统的播放/暂停/重置按钮.

Changed the appearance of the controls to make them look more like traditional play/pause/reset buttons.

这篇关于如何使用代码(不仅仅是按钮)控制触发状态(暂停、播放)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆