/*! * Events are moved from the Vixen SharedWorld to the C# * application through a shared event queue. Events * observed and handled by the Vixen SharedWorld object * are put into a queue. The Canvas3D class maintains a separate * thread which waits on this queue and converts any Vixen Events * it finds into C# Vixen.EventArgs and raises a .NET event * which can be handled in the C# application. */ protected void EventLoop() { SharedWorld world = SharedWorld.Get(); StopEvents = false; if (world.FileName != null) { world.LoadAsync(world.FileName, world.GetScene()); } while (!StopEvents && (SharedWorld.Get() != null)) { try { Event ev = SharedWorld.Get().NextEvent(); if (ev == null) { break; } if (ev.GetType() == typeof(TrackEvent)) { if (ev.Code != Event.TRACK) { Console.Write("ERROR: TrackEvent has bad opcode"); } } string name = ev.GetName(); EventArgs args = new VixenEventArgs(ev); object[] list = { this, args }; VixenEventHandler handler = new VixenEventHandler(PostVixenEvent); Trace("Canvas3D:Event " + name + "\n"); Invoke(handler, list); } catch (Exception ex) { LogError("exception in event loop " + ex.Message); } } if (SharedWorld.Get() != null) { SharedWorld.Get().Stop(); } }
/*! * Shuts down 3D processing and releases all 3D resources. * This routine is called automatically when the window closes. */ public virtual void StopVixen() { SharedWorld world = SharedWorld.Get(); if (world != null) { Scene scene = world.GetScene(); if (scene != null) { scene.Window = 0; } world.StopEvents(); if (UsePhysics) { Physics.Shutdown(); } } }
/*! * Routes a mouse event from this window to Vixen. * Any Vixen objects which observe Event.MOUSE will get these events. * If DispatchMouseEvents is false, this functionality is disabled. * @see DispatchMouseEvents */ private void DispatchMouse(MouseEventArgs e) { int buttons = 0; Keys modifiers = Control.ModifierKeys; SharedWorld world = SharedWorld.Get(); Scene scene = SharedWorld.MainScene; if ((scene == null) || (world == null) || !world.IsRunning()) { return; } if (!DispatchMouseEvents) { return; } if ((e.Button & MouseButtons.Left) != 0) { buttons |= MouseEvent.LEFT; } if ((e.Button & MouseButtons.Right) != 0) { buttons |= MouseEvent.RIGHT; } if ((e.Button & MouseButtons.Middle) != 0) { buttons |= MouseEvent.MIDDLE; } if ((modifiers & Keys.Control) != 0) { buttons |= MouseEvent.CONTROL; } if ((modifiers & Keys.Shift) != 0) { buttons |= MouseEvent.SHIFT; } world.OnMouse(e.X, e.Y, buttons, 0); OnMouse((float)e.X, (float)e.Y, buttons, 0); }
/*! * Start up Vixen 3D display and event processing. * This function should not be called until the * underlying Window has been created and the * OS window handle is available. Vixen will display * the 3D content in this window. */ public virtual bool RunVixen() { SharedWorld world = SharedWorld.Get(); IntPtr windowHandle = Handle; Scene scene = null; try { if (windowHandle == null) { LogError("Cannot get window handle for parent window"); return(false); } if ((world != null) && world.IsRunning()) { return(false); } //world.SetDebugLevel(1); world.Run((uint)windowHandle); } catch (Exception ex) { LogError("exception starting 3D " + ex.Message); } if (world.IsRunning()) { ThreadStart eventloop = new ThreadStart(EventLoop); Thread thread = new Thread(eventloop); bool loadasync = World.DoAsyncLoad; if (MediaDir != null) { world.SetMediaDir(MediaDir); } if (ContentFile != null) { world.FileName = GetMediaPath(ContentFile); } else { loadasync = false; } thread.Start(); if (!loadasync) { try { scene = MakeScene(); if (scene != null) { world.SetScene(scene); } } catch (Exception ex) { SharedWorld.LogError("exception making initial scene " + ex.Message); } } scene = world.GetScene(); if (scene != null) { scene.OnResize(); } return(true); } return(false); }