Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm trying to make a graphics display on a touchscreen device in Mathematica. This means that there will not be a keyboard available for functions such as Panning and Zooming. Because of this, I have created a scaling system to allow zooming, and a series of Events that are bound to aspects of panning. My code worked fine on a simple object, using for example a green cylinder and just moving the center of view:

EventHandler[
Dynamic[Graphics3D[{color, Cylinder[]}, 
ViewCenter -> {{1/2, 1/2, 
  1/2}, {x = CurrentPosition[[1]] + value[[1]], 
  y = CurrentPosition[[2]] - value[[2]]}}, 
RotationAction -> "Clip"], 
Initialization :> (color = Green; value = {0, 0}; 
CurrentPosition = {1/2, 1/2})],
{{"MouseDown", 2} :> (a = CurrentValue[MousePosition]; 
pt = CurrentValue[ImageSize]), {"MouseDragged", 
2} :> {b = CurrentValue[MousePosition], drag = b - a, 
val1 = drag[[1]]/(pt[[1]]), val2 = drag[[2]]/(pt[[2]]), 
value = {val1, val2}}, {"MouseUp", 2} :> {value = {0, 0}, 
CurrentPosition[[1]] = x, CurrentPosition[[2]] = y}}]

(As a side note, I realize that most of you will probably find my code very sloppy, but I'm just a student and coding isn't really my forte) Above, I establish the Graphic wrapped in Dynamic, and make it so that your mouse position is initialized upon a right-click, the difference between your mouse and the initial click is recorded, and this distance is scaled and changes the two-dimensional center of view.

However, when I go to a much more complex manipulate function to allow for several objects that can be toggled on and off, zoom functionality and this panning system, everything works fine until the image is rotated. Following the rotation, my pan system does not work, and I was hoping that someone could tell me how to go about fixing my code. Sorry for this huge chunk of code, but here is the graphic that is not working.

    EventHandler[Dynamic[Manipulate[Show[
{If[x1, obj1, blankobj], If[x2, obj2, blankobj], 
 If[x3, obj3, blankobj], If[x4, obj4, blankobj], 
 If[x5, obj5, blankobj]},
RotationAction -> "Clip", 
PlotRange -> {{scaling[[1]], -scaling[[1]]}, {scaling[[
    2]], -scaling[[2]]}, {-zscaling, zscaling}}, Axes -> False, 
ViewCenter -> {{1/2, 1/2, 
   1/2}, {x = CurrentPosition[[1]] + value[[1]], 
   y = CurrentPosition[[2]] - value[[2]]}}],
 Style["Example Graphic", 22, Bold],
Delimiter, Style["Component Toggle", 16, Bold],
Button["Red Sphere", x1 = Not[x1]],
Button["Blue Cylinder", x2 = Not[x2]],
Button["Black Line", x3 = Not[x3]],
Button["Yellow Polygon", x4 = Not[x4]],
Button["Green Cuboid", x5 = Not[x5]],
Delimiter, Style["Zoom Options", 16, Bold],
{{scaling, {-3, -3}, "Horizontal Zoom"}, {-5, -5}, {-1, -1}, 
ControlType -> Slider2D},
{{zscaling, -7, "Vertical Zoom"}, -10, -4},
ControlPlacement -> Left,
Initialization :> (value = {0, 0}; CurrentPosition = {1/2, 1/2};
 x1 = True; x2 = True; x3 = True; x4 = True; x5 = True;
 obj1 = {Graphics3D[{Red, Sphere[{0, 0, 2}]}]};
 obj2 = Graphics3D[{Blue, Cylinder[]}];
 obj3 = 
  Graphics3D[{Black, Thick, Dashed, 
    Line[{{-2, 0, 2}, {2, 0, 2}, {0, 0, 4}, {-2, 0, 2}}]}];
 obj4 = 
  Graphics3D[{Yellow, 
    Polygon[{{-3, -3, -2}, {-3, 3, -2}, {3, 
       3, -2}, {3, -3, -2}}]}];
 obj5 = 
  Graphics3D[{Green, Opacity[.3], 
    Cuboid[{-2, -2, -2}, {2, 2, -1}]}];
 blankobj = Plot3D[r, {x, 0, 5}, {y, 0, 5}];
 )]], {{"MouseDown", 2} :> (a = CurrentValue[MousePosition]; 
pt = CurrentValue[ImageSize]), {"MouseDragged", 
2} :> {b = CurrentValue[MousePosition], drag = b - a, 
val1 = drag[[1]]/(pt[[1]]), val2 = drag[[2]]/(pt[[2]]), 
value = {val1, val2}}, {"MouseUp", 2} :> {value = {0, 0}, 
CurrentPosition[[1]] = x, CurrentPosition[[2]] = y}}]
share|improve this question
    
I must be misunderstanding. How does your first block of code differ from Graphics3D[{Green, Cylinder[]}]? In both, you can rotate the cylinder with the mouse. –  bill s Jul 10 '13 at 14:36
    
@bills The only real difference is not the rotation, I'm trying to make it so that a right click and a drag with the right mouse button depressed causes a panning of the graphic. You click the button, drag, it dynamically changes your center of view, and then as you release the second mouse button, the center changes to wherever it was at that time. My goal isn't about rotation, I'm trying to make panning possible without the use of the shift key. –  Matt Jul 10 '13 at 15:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.