2

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

Nasser
  • 12,849
  • 6
  • 52
  • 104

1 Answers1

3

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.

Heike
  • 24,102
  • 2
  • 31
  • 45
  • It seems to be the most straightforward way to do this. I couldn't find a way to do it with `Trigger` itself. I have been looking for icons in Mathematica as a replacement for your strings "Pause", "Play", and "Reset", but couldn't find a specific set. The closest I get is `\[DoubleVerticalBar]` for Pause, `\[FilledRightTriangle]` for Play and `\[LeftArrowBar]` for reset, but they're not really nice. – Sjoerd C. de Vries Aug 10 '11 at 17:46
  • @Sjoerd: I had the same problem as you in that I couldn't find nice icons either. Originally I came up with `\[FirstPage]` for reset, `\[RightPointer]` for play and `"||"` for pause but they weren't very satisfactory either so I just left the button labels for what they were. I wonder where Mathematica stores its Play/Pause/Reset icons. – Heike Aug 10 '11 at 20:20