/// <summary> /// This is called when the state of the wiimote changes and a new state report is available. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void handleWiimoteChanged(object sender, WiimoteChangedEventArgs e) { // Obtain mutual excluseion. pDeviceMutex.WaitOne(); // If we not running then leave. if (!bRunning) { pDeviceMutex.ReleaseMutex(); return; } Queue <WiiContact> lFrame = new Queue <WiiContact>(1); // Store the state. WiimoteState pState = e.WiimoteState; // Contain active sensor data. List <SpatioTemporalInput> lInputs = new List <SpatioTemporalInput>(); bool pointerOutOfReach = false; WiimoteLib.Point newpoint = lastpoint; CursorPos temp = screenPositionCalculator.CalculateCursorPos(e); newpoint.X = temp.X; newpoint.Y = temp.Y; if (newpoint.X < 0 || newpoint.Y < 0) { newpoint = lastpoint; pointerOutOfReach = true; } //Temporary solution to the "diamond cursor" problem. /* * if (this.changeSystemCursor) * { * try * { * MouseSimulator.RefreshMainCursor(); * } * catch (Exception error) * { * Console.WriteLine(error.ToString()); * } * } */ WiimoteState ws = e.WiimoteState; keyMapper.processWiimoteState(ws); if (!pointerOutOfReach) { if (this.touchDownMaster) { duoTouch.setContactMaster(); } else { duoTouch.releaseContactMaster(); } duoTouch.setMasterPosition(new System.Windows.Point(newpoint.X, newpoint.Y)); if (this.touchDownSlave) { duoTouch.setSlavePosition(new System.Windows.Point(newpoint.X, newpoint.Y)); duoTouch.setContactSlave(); } else { duoTouch.releaseContactSlave(); } lastpoint = newpoint; lFrame = duoTouch.getFrame(); FrameEventArgs pFrame = new FrameEventArgs((ulong)Stopwatch.GetTimestamp(), lFrame); this.OnNewFrame(this, pFrame); if (mouseMode && !this.touchDownMaster && !this.touchDownSlave && this.showPointer) //Mouse mode { this.inputSimulator.Mouse.MoveMouseToPositionOnVirtualDesktop((65535 * newpoint.X) / this.ScreenSize.X, (65535 * newpoint.Y) / this.ScreenSize.Y); MouseSimulator.WakeCursor(); //MouseSimulator.SetCursorPosition(newpoint.X, newpoint.Y); } } this.BatteryState = (pState.Battery > 0xc8 ? 0xc8 : (int)pState.Battery); // Release mutual exclusion. pDeviceMutex.ReleaseMutex(); }
public bool handleWiimoteChanged(object sender, WiimoteChangedEventArgs e) { // Obtain mutual excluseion. WiimoteMutex.WaitOne(); bool significant = false; try { this.screenBounds = Util.ScreenBounds; this.duoTouch.screenBounds = Util.ScreenBounds; Queue <WiiContact> lFrame = new Queue <WiiContact>(1); // Store the state. WiimoteState pState = e.WiimoteState; this.Status.Battery = (pState.Battery > 0xc8 ? 0xc8 : (int)pState.Battery); bool pointerOutOfReach = false; CursorPos newpoint = lastpoint; newpoint = screenPositionCalculator.CalculateCursorPos(e); if (newpoint.X < 0 || newpoint.Y < 0) { newpoint = lastpoint; pointerOutOfReach = true; } WiimoteState ws = e.WiimoteState; if (keyMapper.processWiimoteState(ws)) { significant = true; this.lastWiimoteState = ws; } if (!pointerOutOfReach) { if (this.usingCursors() && !mouseMode && showPointer) { this.masterCursor.Show(); } significant = true; if (this.touchDownMaster) { duoTouch.setContactMaster(); } else { duoTouch.releaseContactMaster(); } duoTouch.setMasterPosition(new System.Windows.Point(newpoint.X, newpoint.Y)); if (this.touchDownSlave) { if (this.usingCursors() && !mouseMode && showPointer) { this.slaveCursor.Show(); } duoTouch.setSlavePosition(new System.Windows.Point(newpoint.X, newpoint.Y)); duoTouch.setContactSlave(); } else { duoTouch.releaseContactSlave(); if (this.usingCursors() && !mouseMode) { this.slaveCursor.Hide(); } } lastpoint = newpoint; lFrame = duoTouch.getFrame(); if (this.usingCursors() && !mouseMode) { WiiContact master = null; WiiContact slave = null; foreach (WiiContact contact in lFrame) { if (master == null) { master = contact; } else if (master.Priority > contact.Priority) { slave = master; master = contact; } else { slave = contact; } } if (master != null) { this.masterCursor.SetPosition(master.Position); this.masterCursor.SetRotation(newpoint.Rotation); } if (slave != null) { this.slaveCursor.SetPosition(slave.Position); this.slaveCursor.SetRotation(newpoint.Rotation); } } FrameEventArgs pFrame = new FrameEventArgs((ulong)Stopwatch.GetTimestamp(), lFrame); this.FrameQueue.Enqueue(pFrame); this.LastFrameEvent = pFrame; if (mouseMode && !this.touchDownMaster && !this.touchDownSlave && this.showPointer) //Mouse mode { if (gamingMouse) { double deltaX = (newpoint.X - ((double)this.screenBounds.Width / 2.0)) / (double)this.screenBounds.Width; double deltaY = (newpoint.Y - ((double)this.screenBounds.Height / 2.0)) / (double)this.screenBounds.Height; deltaX = Math.Sign(deltaX) * deltaX * deltaX * 50; deltaY = Math.Sign(deltaY) * deltaY * deltaY * 50 * ((double)this.screenBounds.Width / (double)this.screenBounds.Height); deltaXBuffer += deltaX % 1; deltaYBuffer += deltaY % 1; int roundDeltaX = (int)deltaX; int roundDeltaY = (int)deltaY; if (deltaXBuffer > 1 || deltaXBuffer < -1) { roundDeltaX += Math.Sign(deltaXBuffer); deltaXBuffer -= Math.Sign(deltaXBuffer); } if (deltaYBuffer > 1 || deltaYBuffer < -1) { roundDeltaY += Math.Sign(deltaYBuffer); deltaYBuffer -= Math.Sign(deltaYBuffer); } this.inputSimulator.Mouse.MoveMouseBy(roundDeltaX, roundDeltaY); } else { this.inputSimulator.Mouse.MoveMouseToPositionOnVirtualDesktop((65535 * newpoint.X) / this.screenBounds.Width, (65535 * newpoint.Y) / this.screenBounds.Height); } MouseSimulator.WakeCursor(); } } else //pointer out of reach { if (this.usingCursors() && !mouseMode) { this.masterCursor.Hide(); this.masterCursor.SetPosition(new System.Windows.Point(lastpoint.X, lastpoint.Y)); } } if (significant) { this.LastSignificantWiimoteEventTime = DateTime.Now; } } catch (Exception ex) { Console.WriteLine("Error handling Wiimote in WiimoteControl: " + ex.Message); return(significant); } //this.BatteryState = (pState.Battery > 0xc8 ? 0xc8 : (int)pState.Battery); // Release mutual exclusion. WiimoteMutex.ReleaseMutex(); return(significant); }