1
$\begingroup$

The widget below is just a toy that I put together so that I could ask this question.

DynamicModule[
 {c = {0, 0}, a = 1, da = 0.1, f},
 f = -Round[Log10[FractionalPart[da]]];
 Manipulate[
  Graphics[
   GeometricTransformation[Rectangle[c - 1, c + 1], 
    RotationTransform[a, c]]
   , ImageSize -> Small
   , AspectRatio -> Automatic
   , PlotRange -> Transpose[{c - 1.5, c + 1.5}]
  ],
  Row[{
    InputField[
     Dynamic[
      ToString[NumberForm[a, {Infinity, f}, ExponentFunction -> (Null &)]]
      , If[StringMatchQ[#, FrontEnd`Private`ValidNumberRegex], a = ToExpression[#]] &
      ]
     , String
     , ImageSize -> 50
     ]
    , Spacer[10]
    , EventHandler[Button["\[DownArrow]", Null],
        {"MouseDown" :> While[CurrentValue["MouseButtons"] != {}, a -= da; Pause[0.2]]}
      , Method -> "Queued"
     ]
    , Spacer[5]
    , EventHandler[Button["\[UpArrow]", Null],
        {"MouseDown" :> While[CurrentValue["MouseButtons"] != {}, a += da; Pause[0.2]]}
      , Method -> "Queued"
     ]
    }]
  ]
]

Mathematica graphics


The widget's intended functionality is to let the user

  1. specify, via the input field, a "base point" value for the square's rotation angle (in radians); and then
  2. perturb this value (and therefore the square's rotation) in small increments, up or down, by clicking on the buttons.

(The idea is to have the functionality of an "infinite slider".)


In the current version of the widget, to perturb the angle by more than a single increment, one can hold down the appropriate button (instead of just clicking it). This will cause the angle to get updated every 0.2 seconds, until the button is released.

It is essential that the square's rotation visibly reflect the current value of the angle value at all times.

Unfortunately, even though the value gets updated repeatedly while the button remains pressed, and this is reflected in the contents of the InputField, the square's rotation does not reflect the change until the mouse is released.


Q1: How come the value in the the square's rotation does not change until the mouse is released, even though the input field reflects the repeated updates to the value, as they happen?

Q2: How can I have the square's rotation reflect the updates to the value as they happen?

$\endgroup$
1
$\begingroup$

Wrap the Graphics in Dynamic:

DynamicModule[{c = {0, 0}, a = 1, da = 0.1, f}, 
 f = -Round[Log10[FractionalPart[da]]];
 Manipulate[
  Dynamic@Graphics[
    GeometricTransformation[Rectangle[c - 1, c + 1], 
     RotationTransform[a, c]], ImageSize -> Small, 
    AspectRatio -> Automatic, 
    PlotRange -> Transpose[{c - 1.5, c + 1.5}]], 
  Row[{InputField[
     Dynamic[ToString[
       NumberForm[a, {Infinity, f}, ExponentFunction -> (Null &)]], 
      If[StringMatchQ[#, FrontEnd`Private`ValidNumberRegex], 
        a = ToExpression[#]] &], String, ImageSize -> 50], Spacer[10],
     EventHandler[
     Button["↓", 
      Null], {"MouseDown" :> 
       While[CurrentValue["MouseButtons"] != {}, a -= da; 
        Pause[0.2]]}, Method -> "Queued"], Spacer[5], 
    EventHandler[
     Button["↑", 
      Null], {"MouseDown" :> 
       While[CurrentValue["MouseButtons"] != {}, a += da; 
        Pause[0.2]]}, Method -> "Queued"]}]]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.