A9VG电玩部落论坛

 找回密码
 注册
搜索
查看: 9965|回复: 2

[心得] [心得]让Wiimote当PC遥控器的GlovePIE脚本

[复制链接]

精华
0
帖子
10
威望
0 点
积分
19 点
种子
0 点
注册时间
2009-5-1
最后登录
2016-7-1
 楼主| 发表于 2009-5-1 14:06  ·  河北 | 显示全部楼层 |阅读模式
/*本脚本目标是让Wiimote成为一个好用的PC遥控器
按键功能如下:按Wiimote上1+2钮英文读出Wiimote里电池电量,并Wiimote上灯表示电量,
4灯表示75%-100%电量,3灯表示50%-75%电量,2灯表示25%-50%电量,1灯表示0%-25%电量;
当电量低于8%时Wiimote上1号灯会闪烁提示;
按Wiimote上1钮来切换模拟鼠标指针和滚轮模式,刚开始为静止模式,按一下1钮切换为
鼠标模式,晃动Wiimote就移动鼠标指针,再按一下1钮切换为鼠标滚轮模式,晃动Wiimote
就模拟滚轮上下左右的动作,再按一下1钮回到静止模式;
Wiimote上B钮设定为功能钮,双击B钮就在2个模式间切换。默认模式1下,Wiimote上A钮
模拟鼠标左键。按住B钮后A钮就模拟成鼠标右键。

编写者:Sosei
本脚本以GNU通用公共许可证形式发布
*/

//全脚本初始化部分开始
if var.initializer=false then
  /*提示部分相关变量
  每级优先级最多并发2个事件
  */
  Wiimote.Leds=0
  var.LEDsLock=false
  var.Prompt1=false  //优先0级。指示电量
  var.Prompt2=false  //优先0级。
  var.Prompt3=false  //优先1级。按下WiimoteB钮提示
  var.Prompt4=false  //优先1级。模拟鼠标指针和滚轮模式
  var.Prompt5=false  //优先1级。模拟鼠标指针和滚轮模式
  var.Prompt6=false  //优先1级。模拟鼠标指针和滚轮模式
  var.Prompt7=false  //优先2级。低电量提示
  var.Prompt8=true  //优先2级。WiimoteB钮切换状态
  var.Prompt9=false  //优先2级。WiimoteB钮切换状态
  var.Prompt10=true  //优先3级。灯全灭的初始状态
  var.LEDsShowPriority=3  //事件优先级变量初始化为最低优先级
  var.pLoop=true

  //Wiimote水平静止放置下的x,y,z轴固有偏差量补偿值
  var.xOffset = -1.96
  var.yOffset = -5.81
  var.zOffset = 0.11

  //模拟鼠标的移动速度
  var.speed = 1

  //如果手不稳就增大数值
  var.xCutoff = 4
  var.zCutoff = 4
  var.WheelDeadZone=0.003

  var.MouseMoveMode=0
  var.WiimoteBDoubleClickedState=true
  var.MouseLeftButtonSwitch=false
  var.MouseRightButtonSwitch=false

  //低电量提示线
  var.LowBattery=round(8%*192)

  var.WaitSwitch=false

  var.initializer = true
end if
//全脚本初始化部分结束

//判断Wiimote 1和2钮的状态程序段开始
if Wiimote.one then
  if Wiimote.two then  //按Wiimote 2+1钮指示电池电量
   var.Prompt1=true
   wait 1s  //延迟1秒后结束消息
   var.Prompt1=false
  else if SingleClicked(Wiimote.one) then  //单击Wiimote 1钮切换模拟鼠标指针和滚轮模式。
   var.MouseMoveMode = (var.MouseMoveMode+1) mod 3
   if var.MouseMoveMode=0 then
    var.Prompt4=true
    var.Prompt5=false
    var.Prompt6=false
    wait 0.5s  //延迟0.5秒后结束消息
    var.Prompt4=false
   else if var.MouseMoveMode=1 then
    var.Prompt4=false
    var.Prompt5=true
    var.Prompt6=false
    wait 0.5s  //延迟0.5秒后结束消息
    var.Prompt5=false
   else if var.MouseMoveMode=2 then
    var.Prompt4=false
    var.Prompt5=false
    var.Prompt6=true
    wait 0.5s  //延迟0.5秒后结束消息
    var.Prompt6=false
   end if
  end if
else
  //单击Wiimote 2钮映射……
  if SingleClicked(Wiimote.two) then
  end if
end if
//判断Wiimote 1和2钮的状态程序段结束

/*模拟鼠标指针和滚轮运动程序段开始。
MouseMoveMode=1就是Wiimote控制鼠标指针运动;
MouseMoveMode=2就是Wiimote控制滚轮运动;
MouseMoveMode=0就是静止不动了。
*/
if var.MouseMoveMode=1 then
  //Wiimote的x,y,z轴运动量转换成鼠标移动量
  var.xRot = (Wiimote.RawAccX + var.xOffset)*2.633
  var.yRot = (Wiimote.RawAccY + var.yOffset)*2.633
  var.zRot = (Wiimote.RawAccZ + var.zOffset)*2.633
  if var.xRot > var.xCutoff then mouse.x = mouse.x - .001 * var.speed * (var.xRot - var.xCutoff)
  if var.xRot < -var.xCutoff then mouse.x = mouse.x - .001 * var.speed * (var.xRot + var.xCutoff)
  if var.zRot > var.zCutoff then mouse.y = mouse.y - .001 * var.speed * (var.zRot - var.zCutoff)
  if var.zRot < -var.zCutoff then mouse.y = mouse.y - .001 * var.speed * (var.zRot + var.zCutoff)
else if var.MouseMoveMode=2 then
  var.xRot = (Wiimote.RawAccX + var.xOffset)*2.633
  var.yRot = (Wiimote.RawAccY + var.yOffset)*2.633
  var.zRot = (Wiimote.RawAccZ + var.zOffset)*2.633
  if var.xRot > var.xCutoff then
   var.xMoveQuantity=0.001 * var.speed * (var.xRot - var.xCutoff)
   if var.xMoveQuantity>var.WheelDeadZone then
    Mouse.WheelLeft=true
   else if var.xMoveQuantity<-var.WheelDeadZone then
    Mouse.WheelRight=true
   end if
  end if
  if var.xRot < -var.xCutoff then
   var.xMoveQuantity=0.001 * var.speed * (var.xRot + var.xCutoff)
   if var.xMoveQuantity>var.WheelDeadZone then
    Mouse.WheelLeft=true
   else if var.xMoveQuantity<-var.WheelDeadZone then
    Mouse.WheelRight=true
   end if
  end if
  if var.zRot > var.zCutoff then
   var.yMoveQuantity=0.001 * var.speed * (var.zRot - var.zCutoff)
   if var.yMoveQuantity>var.WheelDeadZone then
    Mouse.WheelUp=true
   else if var.yMoveQuantity<-var.WheelDeadZone then
    Mouse.WheelDown=true
   end if
  end if
  if var.zRot < -var.zCutoff then
   var.yMoveQuantity=0.001 * var.speed * (var.zRot + var.zCutoff)
   if var.yMoveQuantity>var.WheelDeadZone then
    Mouse.WheelUp=true
   else if var.yMoveQuantity<-var.WheelDeadZone then
    Mouse.WheelDown=true
   end if
  end if
end if
//模拟鼠标指针和滚轮运动程序段结束。

//双击WiimoteB钮切换状态
if DoubleClicked(Wiimote.B) then
  var.WiimoteBDoubleClickedState=not var.WiimoteBDoubleClickedState
  if var.WiimoteBDoubleClickedState=true then
   var.Prompt8=true
   var.Prompt9=false
  else if var.WiimoteBDoubleClickedState=false then
   var.Prompt8=false
   var.Prompt9=true
  end if
end if
if var.WiimoteBDoubleClickedState then
  var.WiimoteB=Wiimote.B
  //按下WiimoteB钮提示
  if var.WiimoteB then
   var.Prompt3=true
  else
   var.Prompt3=false
  end if
  /*鼠标左右键程序段开始。
  没按WiimoteB钮时WiimoteA钮模拟鼠标左键,按住WiimoteB钮时WiimoteA钮模拟鼠标右键。
  可以模拟左右键同时按住(先按住A钮不松再按B钮,或先按住B钮再按住A钮不松然后释放B钮。)。
  可以控制左右键同时按住或释放的顺序。*/
  var.WiimoteA=Wiimote.A
  //单按WiimoteA钮,并且记录的鼠标左键是未按状态,那末设置鼠标左键按下。
  if (var.WiimoteA and (not var.WiimoteB)) and (not var.MouseLeftButtonSwitch) then
   Mouse.LeftButton=true
   var.MouseLeftButtonSwitch=true
  //WiimoteA钮为松开状态、WiimoteB钮也未按状态,并且记录的鼠标左键是按下状态,那末设置鼠标左键释放。
  else if ((not var.WiimoteA) and (not var.WiimoteB)) and var.MouseLeftButtonSwitch then
   Mouse.LeftButton=false
   var.MouseLeftButtonSwitch=false
  end if
  //WiimoteB钮为按下状态,按WiimoteA钮,并且记录的鼠标右键是未按状态,那末设置鼠标右键按下。
  if (var.WiimoteA and var.WiimoteB) and (not var.MouseRightButtonSwitch) then
   Mouse.RightButton=true
   var.MouseRightButtonSwitch=true
  //WiimoteA钮为松开状态,并且记录的鼠标右键是按下状态,那末设置鼠标右键释放。
  else if (not var.WiimoteA) and var.MouseRightButtonSwitch then
   Mouse.RightButton=false
   var.MouseRightButtonSwitch=false
  end if
  //鼠标左右键程序段结束
  /*模拟键盘程序段开始。
  Wiimote上的上下左右钮、HOME钮、+钮、-钮映射到键盘。
  */
  //模拟键盘程序段结束
else
  /*模拟键盘程序段开始。
  Wiimote上的上下左右钮、HOME钮、+钮、-钮映射到键盘。
  */
  //模拟键盘程序段结束
end if

//低电量提示
if var.WaitSwitch then
  wait 20s  //每20秒检测一次
  var.WaitSwitch=false
else
  if wiimote.Battery<var.LowBattery then
   var.Prompt7=true
  else
   var.Prompt7=false
  end if
  var.WaitSwitch=true
end if

//Wiimote提示程序段开始
if var.Prompt1 or var.Prompt2 then  //这显示优先级为0
  if 0<var.LEDsShowPriority then var.LEDsLock=false  //高于当前优先级则解锁
  if not var.LEDsLock then
   while var.pLoop do
    if (var.p=0) and var.Prompt1 then  //注意参数一致
      var.PromptNo=1  //注意参数一致
      var.pLoop=false
    else if (var.p=1) and var.Prompt2 then  //注意参数一致
      var.PromptNo=2  //注意参数一致
      var.pLoop=false
    end if
    var.p=(var.p+1) mod 2 //在Prompt 1-2间循环
   end while
   var.pLoop=true
   var.LEDsLock=true
   var.LEDsShowPriority=0  //设置当前优先级为0
   var.i=1
  end if
else if var.Prompt3 or var.Prompt4 or var.Prompt5 or var.Prompt6 then  //这显示优先级为1
  if 1<var.LEDsShowPriority then var.LEDsLock=false  //高于当前优先级则解锁
  if not var.LEDsLock then
   while var.pLoop do
    if (var.p=0) and var.Prompt3 then  //注意参数一致
      var.PromptNo=3  //注意参数一致
      var.pLoop=false
    else if (var.p=1) and var.Prompt4 then  //注意参数一致
      var.PromptNo=4  //注意参数一致
      var.pLoop=false
    else if (var.p=2) and var.Prompt5 then  //注意参数一致
      var.PromptNo=5  //注意参数一致
      var.pLoop=false
    else if (var.p=3) and var.Prompt6 then  //注意参数一致
      var.PromptNo=6  //注意参数一致
      var.pLoop=false
    end if
    var.p=(var.p+1) mod 4 //在Prompt 1-4间循环
   end while
   var.pLoop=true
   var.LEDsLock=true
   var.LEDsShowPriority=1  //设置当前优先级为1
   var.i=1
  end if
else if var.Prompt7 or var.Prompt8 or var.Prompt9 then  //这显示优先级为2
  if 2<var.LEDsShowPriority then var.LEDsLock=false  //高于当前优先级则解锁
  if not var.LEDsLock then
   while var.pLoop do
    if (var.p=0) and var.Prompt7 then  //注意参数一致
      var.PromptNo=7  //注意参数一致
      var.pLoop=false
    else if (var.p=1) and var.Prompt8 then  //注意参数一致
      var.PromptNo=8  //注意参数一致
      var.pLoop=false
    else if (var.p=2) and var.Prompt9 then  //注意参数一致
      var.PromptNo=9  //注意参数一致
      var.pLoop=false
    end if
    var.p=(var.p+1) mod 3 //在Prompt 1-3间循环
   end while
   var.pLoop=true
   var.LEDsLock=true
   var.LEDsShowPriority=2  //设置当前优先级为2
   var.i=1
  end if
else if var.Prompt10 then
  if not var.LEDsLock then
   var.PromptNo=10
   var.LEDsLock=true
   var.LEDsShowPriority=3  //设置当前优先级为3
  end if
end if

if var.PromptNo=1 then
  //用Wiimote上的灯指示电池电量,并语音读出具体百分比。
  var.Battery = wiimote.Battery/192*100
  say "Wiimote battery is "+var.Battery+"%."
  Wiimote.Leds=2^ceil(var.Battery/25)-1 //从左到右灯亮
  wait 3.6s
  var.LEDsLock=false
else if var.PromptNo=2 then
  //0优先级的第二事件程序段
else if var.PromptNo=3 then
   Wiimote.Leds=3
   wait 38ms
   Wiimote.Leds=1
   wait 61ms
   var.LEDsLock=false
else if var.PromptNo=4 then
  if var.i<=6 then
   Wiimote.Leds=3
   wait 38ms
   Wiimote.Leds=2
   wait 61ms
   var.i=var.i+1
  else
   var.LEDsLock=false
  end if
else if var.PromptNo=5 then
  if var.i<=6 then
   Wiimote.Leds=7
   wait 38ms
   Wiimote.Leds=6
   wait 61ms
   var.i=var.i+1
  else
   var.LEDsLock=false
  end if
else if var.PromptNo=6 then
  if var.i<=6 then
   Wiimote.Leds=15
   wait 38ms
   Wiimote.Leds=14
   wait 61ms
   var.i=var.i+1
  else
   var.LEDsLock=false
  end if
else if var.PromptNo=7 then
  if var.i<=3 then
   Wiimote.Leds=1
   wait 38ms
   Wiimote.Leds=0
   wait 61ms
   var.i=var.i+1
  else
   var.LEDsLock=false
  end if
else if var.PromptNo=8 then
  Wiimote.Leds=8
  wait(0.3s)
  var.LEDsLock=false
else if var.PromptNo=9 then
  Wiimote.Leds=12
  wait(0.3s)
  var.LEDsLock=false
else if var.PromptNo=10 then
  Wiimote.Leds=0
  var.LEDsLock=false
end if
//Wiimote提示程序段结束.

终结者

--- WW ---

精华
3
帖子
7700
威望
10 点
积分
8262 点
种子
5 点
注册时间
2006-11-19
最后登录
2023-12-21
发表于 2009-5-1 15:37  ·  北京 | 显示全部楼层
感谢LZ分享了,但是这个感觉不早就有了吗?

精华
0
帖子
10
威望
0 点
积分
19 点
种子
0 点
注册时间
2009-5-1
最后登录
2016-7-1
 楼主| 发表于 2009-5-1 15:43  ·  河北 | 显示全部楼层
相关Wiimote的脚本是不少,基本都是外国人写的。
不过我用了几个不顺手,就新写了这个。主要好处是顺便写了详细的中文注释,可以方便大伙自行更改扩充。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|A9VG电玩部落 川公网安备 51019002005286号

GMT+8, 2024-5-15 15:00 , Processed in 0.170320 second(s), 18 queries , Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

返回顶部