/// <summary> /// Construct a new wiimote provider. /// </summary> public WiiPointerProvider() { lastpoint = new WiimoteLib.Point(); lastpoint.X = 0; lastpoint.Y = 0; this.settingsControl = new WiiPointerProviderSettings(); this.ScreenSize = new Vector(Util.ScreenWidth, Util.ScreenHeight); this.smoothingBuffer = new SmoothingBuffer(3); this.duoTouch = new DuoTouch(Util.ScreenBounds, 3, 1); this.keyMapper = new WiiKeyMapper(1); this.keyMapper.OnButtonDown += WiiButton_Down; this.keyMapper.OnButtonUp += WiiButton_Up; this.keyMapper.OnConfigChanged += WiiKeyMap_ConfigChanged; this.keyMapper.SendConfigChangedEvt(); this.inputSimulator = new InputSimulator(); this.showPointer = true; if (this.showPointer && !this.mouseMode) { this.duoTouch.enableHover(); } else { this.duoTouch.disableHover(); } screenPositionCalculator = new ScreenPositionCalculator(); }
/// <summary> /// Calculates the Cursor Position on Screen by using the Midpoint of the 2 Leds in the sensor bar /// </summary> /// <param name="args"></param> /// <returns></returns> public static void SetPosition(WiimoteChangedEventArgs args) { double minXPos = 0; double maxXPos = System.Windows.SystemParameters.PrimaryScreenWidth; double maxWidth = maxXPos - minXPos; double x; double minYPos = 0; double maxYPos = System.Windows.SystemParameters.PrimaryScreenHeight; double maxHeight = maxYPos - minYPos; double y; PointF relativePosition = new PointF(); if (args.WiimoteState.IRState.IRSensors[0].Found && args.WiimoteState.IRState.IRSensors[1].Found) { relativePosition = args.WiimoteState.IRState.Midpoint; } else if (args.WiimoteState.IRState.IRSensors[0].Found) { relativePosition.X = m_MidSensorPos.X + (args.WiimoteState.IRState.IRSensors[0].Position.X - m_FirstSensorPos.X); relativePosition.Y = m_MidSensorPos.Y + (args.WiimoteState.IRState.IRSensors[0].Position.Y - m_FirstSensorPos.Y); } else if (args.WiimoteState.IRState.IRSensors[1].Found) { relativePosition.X = m_MidSensorPos.X + (args.WiimoteState.IRState.IRSensors[1].Position.X - m_SecondSensorPos.X); relativePosition.Y = m_MidSensorPos.Y + (args.WiimoteState.IRState.IRSensors[1].Position.Y - m_SecondSensorPos.Y); } //Remember for next run m_FirstSensorPos = args.WiimoteState.IRState.IRSensors[0].Position; m_SecondSensorPos = args.WiimoteState.IRState.IRSensors[1].Position; m_MidSensorPos = relativePosition; x = Convert.ToInt32((float)maxWidth * (1.0F - relativePosition.X)) + minXPos; y = Convert.ToInt32((float)maxHeight * relativePosition.Y) + minYPos; if (x < 0) { x = 0; } else if (x > System.Windows.SystemParameters.PrimaryScreenWidth) { x = System.Windows.SystemParameters.PrimaryScreenWidth; } if (y < 0) { y = 0; } else if (y > System.Windows.SystemParameters.PrimaryScreenHeight) { y = System.Windows.SystemParameters.PrimaryScreenHeight; } WiimoteLib.Point point = new WiimoteLib.Point(); point.X = (int)x; point.Y = (int)y; Win32.SetCursorPos((int)x, (int)y); //set mouse position to wii point }
/// <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(); }
private void UpdateWiimoteChanged(object args) { var eventArgs = args as WiimoteChangedEventArgs; if (args == null) { return; } WiimoteState ws = eventArgs.WiimoteState; if (ws == null) { return; } var actions = new List <string>(); //Now it is decided which actions should be taken if (ws.ButtonState.A) { actions.Add(CurrentConfiguration.Keys.A); } if (ws.ButtonState.B) { actions.Add(CurrentConfiguration.Keys.B); } if (ws.ButtonState.Down) { actions.Add(CurrentConfiguration.Keys.Down); } if (ws.ButtonState.Home) { actions.Add(CurrentConfiguration.Keys.Home); } if (ws.ButtonState.Left) { actions.Add(CurrentConfiguration.Keys.Left); } if (ws.ButtonState.Minus) { actions.Add(CurrentConfiguration.Keys.Minus); } if (ws.ButtonState.One) { actions.Add(CurrentConfiguration.Keys.One); } if (ws.ButtonState.Plus) { actions.Add(CurrentConfiguration.Keys.Plus); } if (ws.ButtonState.Right) { actions.Add(CurrentConfiguration.Keys.Right); } if (ws.ButtonState.Two) { actions.Add(CurrentConfiguration.Keys.Two); } if (ws.ButtonState.Up) { actions.Add(CurrentConfiguration.Keys.Up); } StringBuilder sb = new StringBuilder(); actions.ForEach(action => { sb.Append(action); sb.Append('+'); }); WiimoteLib.Point pos = ScreenPositionCalculator.GetPosition(eventArgs); bool buttonStateChanged = ButtonStateChanged(ws.ButtonState); using (ResponseActionController controller = new ResponseActionController(sb.ToString(), pos.X, pos.Y)) { controller.Execute(buttonStateChanged); } }
/// <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(); }