How to control Trigger state (Pause, Play) using code (not just buttons)

巧了我就是萌 提交于 2019-12-13 16:03:59

问题


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.

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}
]

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.

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

thanks


回答1:


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]
]

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



来源:https://stackoverflow.com/questions/7008087/how-to-control-trigger-state-pause-play-using-code-not-just-buttons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!