GlovePIE:Mode Switch Script (one button)

From WiiLi

Jump to: navigation, search


//Mode Switch Script (one button)   [by marsuten]
*|Switch between modes/scripts (2,3 or 4), contained in a
*|single script,using a single button.

*------------------------------------------------------*
var.countnum = 4  //Number of modes you want ( 1 to 4 )
*------------------------------------------------------*

//Starting
if starting then var.count=1

//A single button alternates between modes
if pressed(helddown(wiimote.home, 0ms)) then Inc(var.count)

//Limiter
if var.count = var.countnum+1 then var.count=1

//Led switcher
if var.count = 1 then wiimote.Leds=1 elseif var.count = 2 then wiimote.Leds=2 elseif var.count = 3 then wiimote.Leds=4 elseif var.count = 4 then wiimote.Leds=8 else wiimote.Leds=0

debug= "Mode " + var.count

if var.count=1 then
*--------------------------(MODE 1)----------------------------------*


*-----(END 1)--------------------------------------------------------*
endif
if var.count=2 then
*--------------------------(MODE 2)----------------------------------*


*-----(END 2)--------------------------------------------------------*
endif
if var.count=3 then
*--------------------------(MODE 3)----------------------------------*


*-----(END 3)--------------------------------------------------------*
endif
if var.count=4 then
*--------------------------(MODE 4)----------------------------------*


*-----(END 4)--------------------------------------------------------*
endif

I had problems with the mouse buttons inside conditionals so I had to adapt the Carl's IR Mouse to work properly, with some modifications (i remove the debug, I add the middle button,some navigation keys, mouse wheel and modified the ButtonFreezeTime to make better the doubleclick) . Here is, in the mode 4:

//Mode Switch Script (one button)   [by marsuten]
*|Switch between modes/scripts (2,3 or 4), contained in a
*|single script,using a single button.
//Carl's IR Mouse adapted in mode 4

//Begining in a mode
if var.count= 0 then
var.count=1
endif

//A single button alternates between modes
if wiimote.home then
var.count = var.count+1
wait 300ms
endif

*------------------------------------------------------*
var.countnum= 4  //Number of modes you want ( 1 to 4 )
*------------------------------------------------------*
var.countlim=var.countnum+1

//Limiter
if var.count = var.countlim then
var.count=1
endif

//Led switcher
if var.count = 1 then
wiimote.led1= true
else
wiimote.led1=false
endif
if var.count = 2 then
wiimote.led2= true
else
wiimote.led2=false
endif
if var.count = 3 then
wiimote.led3= true
else
wiimote.led3=false
endif
if var.count = 4 then
wiimote.led4= true
else
wiimote.led4=false
endif

debug= "Mode " + var.count

//Mouse buttons vars
mouse.LeftButton = var.IRLeftButton and (not KeepDown(pressed(var.IRLeftButton), 40ms))
mouse.RightButton = var.IRRightButton and (not KeepDown(pressed(var.IRRightButton), 40ms))
mouse.MiddleButton = var.IRMiddleButton and (not KeepDown(pressed(var.IRMiddleButton), 40ms))



if var.count=1 then

*--------------------------(MODE 1)----------------------------------*


*-----(END 1)--------------------------------------------------------*

endif
if var.count=2 then

*--------------------------(MODE 2)----------------------------------*


*-----(END 2)--------------------------------------------------------*

endif
if var.count=3 then

*--------------------------(MODE 3)----------------------------------*


*-----(END 3)--------------------------------------------------------*

endif
if var.count=4 then

*--------------------------(MODE 4)----------------------------------*

// My best IR Mouse Script, with 5DOF Tracking
// By Carl Kenner

// Change these values:
var.SensorBarSeparation = 7.5 inches  // distance between middles of two sensor bar dots
var.NoYawAllowed = true  // Calculates X if no yaw is allowed, otherwise calculates Yaw but not X
var.IRMulX = 1.2
var.IRMulY = 1.2
var.IROffsetX = 0  // add to mouse.x
var.IROffsetY = 0  // add to mouse.y
var.IRLeftButton = wiimote.A
var.IRRightButton = Wiimote.B
var.IRMiddleButton = wiimote.Minus

Key.WebBack = Wiimote.left
Key.WebForward = Wiimote.right
Key.LeftWindows = Wiimote.Two
Key.WebHome = Wiimote.One

if wiimote.Up then
  mouse.WheelUp = true
  wait 30ms
  mouse.WheelUp = false
  wait 30ms
end if
if wiimote.Down then
  mouse.WheelDown = true
  wait 30ms
  mouse.WheelDown = false
  wait 30ms
end if

// Compensate for roll
var.c = cos(Smooth(wiimote.roll, 10))
var.s = sin(Smooth(wiimote.roll, 10))
if wiimote.dot1vis then
  var.dot1x = var.c*(511.5-wiimote.dot1x)/511.5 - var.s*(wiimote.dot1y-383.5)/511.5
  var.dot1y = var.s*(511.5-wiimote.dot1x)/511.5 + var.c*(wiimote.dot1y-383.5)/511.5
end if
if wiimote.dot2vis then
  var.dot2x = var.c*(511.5-wiimote.dot2x)/511.5 - var.s*(wiimote.dot2y-383.5)/511.5
  var.dot2y = var.s*(511.5-wiimote.dot2x)/511.5 + var.c*(wiimote.dot2y-383.5)/511.5
end if

// if both dots are visible check which is which and how far apart
if wiimote.dot1vis and wiimote.dot2vis then
  if var.dot1x <= var.dot2x then
    var.leftdot = 1
    var.dotdeltay = var.dot2y - var.dot1y
  else
    var.leftdot = 2
    var.dotdeltay = var.dot1y - var.dot2y
  end if
  var.dotdeltax = abs(var.dot1x-var.dot2x)
  var.DotSep = hypot(var.dotdeltax, var.dotdeltay) * 511.5
  var.IRDistance = var.SensorBarSeparation * 1320 / var.DotSep
end if

// sort out the position of the left and right dots
if var.leftdot = 1 then
  if wiimote.dot1vis and wiimote.dot2vis then
    var.LeftDotX = var.dot1x
    var.LeftDotY = var.dot1y
    var.LeftDotVis = true
    var.RightDotX = var.dot2x
    var.RightDotY = var.dot2y
    var.RightDotVis = true
  else if wiimote.dot1vis then
    if hypot(var.leftdotx-var.dot1x,var.leftdoty-var.dot1y) <= hypot(var.rightdotx-var.dot1x,var.rightdoty-var.dot1y) then
      // is the real dot 1
      var.LeftDotX = var.dot1x
      var.LeftDotY = var.dot1y
      var.RightDotX = var.dot1x + var.dotdeltax
      var.RightDotY = var.dot1y + var.dotdeltay
      var.LeftDotVis = true
      var.RightDotVis = false
    else
      // was originally dot 2, but now called dot 1.
      var.leftdot = 2 // this dot (1) is actually the right dot
      var.LeftDotX = var.dot1x - var.dotdeltax
      var.LeftDotY = var.dot1y - var.dotdeltay
      var.RightDotX = var.dot1x
      var.RightDotY = var.dot1y
      var.RightDotVis = true
      var.LeftDotVis = false
    end if
  else if wiimote.dot2vis then
    var.LeftDotX = var.dot2x - var.dotdeltax
    var.LeftDotY = var.dot2y - var.dotdeltay
    var.RightDotX = var.dot2x
    var.RightDotY = var.dot2y
    var.RightDotVis = true
    var.LeftDotVis = false
  end if
else if var.leftdot = 2 then
  if wiimote.dot1vis and wiimote.dot2vis then
    var.LeftDotX = var.dot2x
    var.LeftDotY = var.dot2y
    var.LeftDotVis = true
    var.RightDotX = var.dot1x
    var.RightDotY = var.dot1y
    var.RightDotVis = true
  else if wiimote.dot1vis then
    if hypot(var.leftdotx-var.dot1x,var.leftdoty-var.dot1y) <= hypot(var.rightdotx-var.dot1x,var.rightdoty-var.dot1y) then
      var.leftdot = 1 // dot 1 is now the left dot
      var.LeftDotX = var.dot1x
      var.LeftDotY = var.dot1y
      var.RightDotX = var.dot1x + var.dotdeltax
      var.RightDotY = var.dot1y + var.dotdeltay
      var.LeftDotVis = true
      var.RightDotVis = false
    else
      // the real dot 1 (on the right)
      var.LeftDotX = var.dot1x - var.dotdeltax
      var.LeftDotY = var.dot1y - var.dotdeltay
      var.RightDotX = var.dot1x
      var.RightDotY = var.dot1y
      var.RightDotVis = true
      var.LeftDotVis = false
    end if
  else if wiimote.dot2vis then
    var.RightDotX = var.dot2x + var.dotdeltax
    var.RightDotY = var.dot2y + var.dotdeltay
    var.LeftDotX = var.dot2x
    var.LeftDotY = var.dot2y
    var.LeftDotVis = true
    var.RightDotVis = false
  end if
else
  var.LeftDotX = var.dot1x
  var.LeftDotY = var.dot1y
  var.RightDotX = var.LeftDotX
  var.RightDotY = var.LeftDotY
  var.LeftDotVis = true
  var.RightDotVis = true
end if


// Find the imaginary middle dot
var.MiddleDotX = (var.leftdotx + var.rightdotx)/2
var.MiddleDotY = (var.leftdoty + var.rightdoty)/2
var.MiddleDotVis = wiimote.dot1vis or wiimote.dot2vis

if var.MiddleDotVis then
  var.TotalPitch = atan2(511.5*var.MiddleDotY,1320) + Wiimote.Pitch
  var.DotYaw = atan2(-511.5*var.MiddleDotX,1320) // assume yaw is 0
  var.WiimoteYawNoX = atan2(511.5*var.MiddleDotX,1320)
  var.WiimoteXNoYaw = -sin(var.dotyaw)*var.IRDistance
  var.WiimoteY = -sin(var.totalpitch)*var.IRDistance
  var.WiimoteZ = (-sqrt(sqr(var.IRDistance) - sqr(var.WiimoteY)))*var.IRDistance/RemoveUnits(var.IRDistance)
end if

// scale it to the screen range 0 to 1
var.IRx = var.IRMulX*var.middledotx/2 + 0.5
var.IRy = var.IRMulY*var.middledoty*1023/767/2 + 0.5
var.IRvis = wiimote.dot1vis or wiimote.dot2vis
var.IROnScreen = 0 <= var.IRx <= 1  and  0 <= var.IRy <= 1

// is it off the screen?
var.IRTooFarLeft = var.IRx < 0 or (var.IRx < 0.1 and (not var.IRvis))
var.IRTooFarRight = var.IRx > 1 or (var.IRx > 1-0.1 and (not var.IRvis))
var.IRTooFarUp = var.IRy < 0 or (var.IRy < 0.1 and (not var.IRvis))
var.IRTooFarDown = var.IRy > 1 or (var.IRy > 1-0.1 and (not var.IRvis))

// Heavily smooth small movements, but do zero lag for quick movements
var.MoveAmount = 1024*hypot(delta(var.IRx), delta(var.IRy))
if smooth(var.MoveAmount) > 12 then
  var.SmoothX = var.IRx
  var.SmoothY = var.IRy
  var.LastSureFrame = PIE.Frame
else if (PIE.frame-var.LastSureFrame) > 18 then
  var.SmoothX = Smooth(var.IRx, 18, 4/1024)
  var.SmoothY = Smooth(var.IRy, 18, 4/1024)
else if (PIE.frame-var.LastSureFrame) > 14 then
  var.SmoothX = Smooth(var.IRx, 14, 4/1024)
  var.SmoothY = Smooth(var.IRy, 14, 4/1024)
else if (PIE.frame-var.LastSureFrame) > 10 then
  var.SmoothX = Smooth(var.IRx, 10, 4/1024)
  var.SmoothY = Smooth(var.IRy, 10, 4/1024)
else if (PIE.frame-var.LastSureFrame) > 6 then
  var.SmoothX = Smooth(var.IRx, 6, 4/1024)
  var.SmoothY = Smooth(var.IRy, 6, 4/1024)
else if (PIE.frame-var.LastSureFrame) > 2 then
  var.SmoothX = Smooth(var.IRx, 2, 4/1024)
  var.SmoothY = Smooth(var.IRy, 2, 4/1024)
end if

// Freeze the mouse cursor while they start pressing the button
// otherwise it will make the cursor jump
var.Freeze = (var.IRLeftButton or var.IRRightButton or var.IRMiddleButton) and KeepDown(pressed(var.IRLeftButton) or pressed(var.IRRightButton) or pressed(var.IRMiddleButton) , 600ms)
var.ButtonFreezeTime = 300ms
var.freeze = KeepDown(Pressed(var.IRLeftButton),var.ButtonFreezeTime) or KeepDown(Pressed(var.IRMiddlebutton),var.ButtonFreezeTime) or KeepDown(Pressed(var.IRRightButton),var.ButtonFreezeTime)

// Only change the mouse position if pointing at the screen
// otherwise they can still use a real mouse
if var.IRvis and (not var.Freeze) then
  mouse.x = var.SmoothX
  mouse.y = var.SmoothY
end if


*-----(END 4)--------------------------------------------------------*

endif
Personal tools