// Creates EventManager3D and subscribes the 3d objects to events private void SubscribeEvents() { // Create EventManager3D with MainViewport as constructor parameter var eventManager = new Ab3d.Utilities.EventManager3D(MainViewport); // Create EventSource3D for the Landscape // Each 3D object that needs to participate in the events needs to be registered by EventSource3D // The Landscape object will not be subscribed to any event, but instead it is only registered as DragSurface // This means that other objects can be drag over this 3D model. var eventSource = new Ab3d.Utilities.ModelEventSource3D(); eventSource.TargetModel3D = _landscapeModel; eventSource.IsDragSurface = true; // Register the Landscape's EventSource3D eventManager.RegisterEventSource3D(eventSource); // Now for each wind generator create its own EventSource3D object for (int i = 0; i < _windGenerators.Count; i++) { // Create new EventSource3D eventSource = new Ab3d.Utilities.ModelEventSource3D(); // Set the target object to the 3D model eventSource.TargetModel3D = _windGenerators[i].Model; // We can assign any custom data (object) to the EventSource3D // This is very useful because in event handler we can access this custom data eventSource.CustomData = _windGenerators[i]; // Each wind generator is subscribed to the following events: eventSource.MouseEnter += OnWindGeneratorMouseEnter; eventSource.MouseLeave += OnWindGeneratorMouseLeave; eventSource.BeginMouseDrag += OnWindGeneratorBeginMouseDrag; eventSource.MouseDrag += OnWindGeneratorMouseDrag; eventSource.EndMouseDrag += OnWindGeneratorEndMouseDrag; // Not used events: //eventSource.MouseClick += new eventSource_MouseClick; //eventSource.MouseDown += new eventSource_MouseDown; //eventSource.MouseUp += eventSource_MouseUp; // Register the EventSource3D to the EventManager3D eventManager.RegisterEventSource3D(eventSource); } }
private void SubscribeEvents() { // Create an instance of EventManager3D for out Viewport3D // Important: // It is highly recommended not to have more than one EventManager3D object per Viewport3D. // Having multiple EventManager3D object can greatly reduce the performance because each time the Viewport3D camera is changed, // each EventManager3D must perform a full 3D hit testing from the current mouse position. // This operation is very CPU intensive and can affect performance when there are many 3D objects in the scene. // When multiple EventManager3D object are defined, then the 3D hit testing is performed multiple times. // Therefore it is recommended to have only one EventManager3D object per Viewport3D. // // It is also recommended to remove registered event sources after they are not used any more. // This can be done with RemoveEventSource3D method. _eventManager = new Ab3d.Utilities.EventManager3D(MainViewport); // Create a NamedObjects dictionary and set it to eventManager. // This way we can use names on EventSource objects to identify Model3D or Visual3D. // This is very usefull if we read the 3d objects with Reader3ds or ReaderObj and already have the NamedObjects dictionry. // When names are used in EventSource objects, we get the HitModelName or HitVisualName property set so we can know which object was hit. var namedObjects = new Dictionary <string, object>(); namedObjects.Add("Landscape", _landscapeModel); namedObjects.Add("Base", _windGenarator.BaseModel); namedObjects.Add("Blades", _windGenarator.BladesModel); namedObjects.Add("Turbine", _windGenarator.TurbineModel); _eventManager.NamedObjects = namedObjects; // subscribe the Landscape as the drag surface var modelEventSource = new Ab3d.Utilities.ModelEventSource3D(); // Instead of using TargetModelName and NamedObjects, we could also specify the model with TargetModel3D //modelEventSource.TargetModel3D = _landscapeModel; modelEventSource.TargetModelName = "Landscape"; modelEventSource.IsDragSurface = true; _eventManager.RegisterEventSource3D(modelEventSource); // subscribe other objects var multiModelEventSource = new Ab3d.Utilities.MultiModelEventSource3D(); // Instead of using TargetModelNames and NamedObjects, we could specify the models with model instances //multiModelEventSource.TargetModels3D = new Model3D[] { _windGenarator.BaseModel, // _windGenarator.BladesModel, // _windGenarator.TurbineModel }; // Because we have set the NamedObjects dictionary to _eventManager we can simply specify the models by their names: multiModelEventSource.TargetModelNames = "Base, Blades, Turbine"; multiModelEventSource.IsDragSurface = false; // Subscribe to all events multiModelEventSource.MouseEnter += eventSource_MouseEnter; multiModelEventSource.MouseLeave += eventSource_MouseLeave; multiModelEventSource.MouseMove += eventSource_MouseMove; multiModelEventSource.MouseUp += eventSource_MouseUp; multiModelEventSource.MouseDown += eventSource_MouseDown; multiModelEventSource.MouseClick += eventSource_MouseClick; multiModelEventSource.MouseWheel += multiModelEventSource_MouseWheel; multiModelEventSource.MouseDoubleClick += eventSource_MouseDoubleClick; multiModelEventSource.BeginMouseDrag += eventSource_BeginMouseDrag; multiModelEventSource.MouseDrag += eventSource_MouseDrag; multiModelEventSource.EndMouseDrag += eventSource_EndMouseDrag; multiModelEventSource.TouchEnter += eventSource_TouchEnter; multiModelEventSource.TouchDown += eventSource_TouchDown; multiModelEventSource.TouchMove += eventSource_TouchMove; multiModelEventSource.TouchUp += eventSource_TouchUp; multiModelEventSource.TouchLeave += eventSource_TouchLeave; // NOTE: // It is also possible to subscribe to touch manipulations events (pinch scale and rotate) // But this requires to set IsManipulationEnabled to true and that disables some other events // See the TouchManipulationsSample for more information _eventManager.RegisterEventSource3D(multiModelEventSource); }