AB4D Forum

Full Version: Using MoveCameraConditions with TargetPositionCamera?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In https://www.ab4d.com/help/PowerToys/html...itions.htm

...it shows how to bind mouse movements to camera movements but I can't seem to get it to work.    I have . . .
Code:
        <cameras:TargetPositionCamera Name="Camera1"
                                      Heading="0" Attitude="0" Bank="0" TargetPosition="0 0 0"
                                      CameraType="OrthographicCamera"
                                      CameraWidth="936"
                                      ShowCameraLight="Always"
                                      TargetViewport3D="{Binding ElementName=MainViewport}"/>

        <ab3d:MouseCameraController Name="MouseCameraController1"
                                    RotateCameraConditions="RightMouseButtonPressed"
                                    MoveCameraConditions="RightMouseButtonPressed, AltKey"
                                    TargetCamera="{Binding ElementName=Camera1}"/>


... and nothing happens when I press the right mouse button or the right mouse button plus the Alt key and move the mouse.  (I'm using the left mouse button for something else, but if I comment that out and set the XAML to use the left mouse button instead, it still doesn't work.)

This code is very similar to the PolyLinesSample, which builds and works fine on the same PC. 

I also tried adding to the MouseCameraController . . .

Code:
EventsSourceElement="{Binding ElementName=MainViewport}"
...or...
Code:
EventsSourceElement="{Binding ElementName=ViewportBorder}"



... just because they're in your Polyline example, but I don't really understand it and neither one helped.


Do you have any suggestions on what I'm doing wrong or how to debug this?     Thanks in advance!
The reason that the MouseCameraController is not working is probably that it cannot find Viewport3D or any other element where it could subscribe to mouse events.

When using MouseCameraController it is recommended to explicitly set the EventsSourceElement property to an element that will be used as an event source. 

If you want to rotate and move the camera only when mouse is over 3D objects, then set EventsSourceElement to Viewport3D object. If you would also like to rotate and move the camera when mouse is on a area without 3D objects, then set EventsSourceElement to a parent Border or other parent element (that element need to have Background property set - can be also set to Transparent, but should not be null).

For example:


Code:
<Border Name="ViewportBorder" Background="Transparent">
    <Viewport3D Name="MainViewport">
<!-- ... -->
    </Viewport3D>
</Border>

<cameras:TargetPositionCamera Name="Camera1" 
                              Heading="30" Attitude="-20" Bank="0" 
                              Distance="200" TargetPosition="15 10 0" 
                              ShowCameraLight="Always"
                              TargetViewport3D="{Binding ElementName=MainViewport}"/>

<ab3d:MouseCameraController Name="MouseCameraController1"
                            RotateCameraConditions="LeftMouseButtonPressed"
                            MoveCameraConditions="LeftMouseButtonPressed, ControlKey"
                            EventsSourceElement="{Binding ElementName=ViewportBorder}"
                            TargetCamera="{Binding ElementName=Camera1}"/>
In my original question I said,

"I also tried adding to the MouseCameraController . . .

Code:
Code:
EventsSourceElement="{Binding ElementName=MainViewport}"
...or...
Code:
Code:
EventsSourceElement="{Binding ElementName=ViewportBorder}"


... just because they're in your Polyline example, but I don't really understand it and neither one helped."

... how is this different from what you suggested?
I've been studying the difference between the working example and my code, which doesn't work.

I have a Viewport3D with the geometry in it and I have a canvas over it to draw regions of interest.   The Canvas receives mouse events so I can draw outlines and highlight things of interest in the underlying Viewport.    So it's probably eating the mouse events


Code:
   <Grid>

        <dxControls:DXViewportView Name="MainViewportView" PresentationType="DirectXImage" Margin="230,70,20,170" ClipToBounds="true">
            <Viewport3D Name="MainViewport" >

            </Viewport3D>
        </dxControls:DXViewportView>



        <cameras:TargetPositionCamera Name="Camera1" Grid.Column="0" Heading="15" Attitude="7" Bank="0" Distance="350" TargetPosition="0 0 0"
                                      ShowCameraLight="Always"
                                      TargetViewport3D="{Binding ElementName=MainViewport}"/>

        <ab3d:MouseCameraController Name="MouseCameraController1" Grid.Column="0"
                                    RotateCameraConditions="LeftMouseButtonPressed"
                                    MoveCameraConditions="LeftMouseButtonPressed, ControlKey"
                                    EventsSourceElement="{Binding ElementName=ViewportBorder}"
                                    TargetCamera="{Binding ElementName=Camera1}"/>

        
        <Canvas Name="ROIcnv" Background ="Transparent" Canvas.Top="0" Canvas.Left="0" Margin="230,70,20,170" MouseDown="ROIcnvMouseDown"  MouseUp="ROIcnvMouseUp" MouseMove="ROIcnvMouseMove" ClipToBounds="True" MouseWheel="ROIcnvMouseWheel"/>


So how do I bind to the canvas to collect mouse events from there?   Thanks in advance!
Nevermind . . .  It was easy . . .  I used the canvas as the event source.   For some reason I didn't think I could do that.

All good now.  Thanks.
I am glad that this is solved.

You may also subscribe to PreviewMouseDown, PreviewMouseUp, ... events in your Canvas and then set e.Handled (e is parameter to event  handler) to true in case the event was handled by your code and you do not want to pass it to the MouseCameraController.

If you would be using EventManager3D, you could also set the UsePreviewEvents to true on EventManager3D to allow having the same mouse buttons to have multiple "tasks".