Skip to content

Commit

Permalink
Metaコントローラーのスティックの処理がいくつか正しくなかった問題修正/Gripアナログ値を無視
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-akira committed Dec 30, 2024
1 parent ae2cc73 commit 4c1eae3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 20 deletions.
34 changes: 29 additions & 5 deletions Assets/Scripts/Input/InputManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,23 @@ private async void ControllerAction_KeyDown(object sender, OVRKeyEventArgs e)
config.isTouch = e.IsTouch;
if (e.IsAxis)
{
if (config.keyIndex < 0) return;
//if (config.keyIndex < 0) return; スティック真ん中をタッチしたときは反応させることにしたのでコメントアウト
if (e.IsLeft)
{
if (isStick) lastStickLeftAxisPoint = config.keyIndex;
if (isStick)
{
lastStickLeftAxisPoint = config.keyIndex;
isStickLeftTouchDown = true;
}
else lastTouchpadLeftAxisPoint = config.keyIndex;
}
else
{
if (isStick) lastStickRightAxisPoint = config.keyIndex;
if (isStick)
{
lastStickRightAxisPoint = config.keyIndex;
isStickRightTouchDown = true;
}
else lastTouchpadRightAxisPoint = config.keyIndex;
}
}
Expand Down Expand Up @@ -218,29 +226,38 @@ private async void ControllerAction_KeyUp(object sender, OVRKeyEventArgs e)
if (doKeyConfig) { }// await server.SendCommandAsync(new PipeCommands.KeyUp { Config = config });
else CheckKey(config, false);
config.keyIndex = newindex;
if (config.keyIndex < 0) return;
//if (config.keyIndex < 0) return; スティック真ん中をタッチしたときは反応させることにしたのでコメントアウト
//新しいキーを押す
if (doKeyConfig) await controlWPFWindow.server.SendCommandAsync(new PipeCommands.KeyDown { Config = config });
else CheckKey(config, true);
}
if (doKeyConfig || doKeySend) { }// await server.SendCommandAsync(new PipeCommands.KeyUp { Config = config });
if (!doKeyConfig) CheckKey(config, false);
if (e.IsAxis && isStick)
{
if (e.IsLeft) isStickLeftTouchDown = false;
else isStickRightTouchDown = false;
}
}

private int lastTouchpadLeftAxisPoint = -1;
private int lastTouchpadRightAxisPoint = -1;
private int lastStickLeftAxisPoint = -1;
private int lastStickRightAxisPoint = -1;
private bool isStickLeftTouchDown = false;
private bool isStickRightTouchDown = false;

private bool isSendingKey = false;
//タッチパッドやアナログスティックの変動
private async void ControllerAction_AxisChanged(object sender, OVRKeyEventArgs e)
{
if (e.IsAxis == false) return;
//if (e.Axis == Vector3.zero) return;
var keyName = e.Name;
if (keyName.Contains("Trigger")) return; //トリガーは現時点ではアナログ入力無効
if (keyName.Contains("Position")) keyName = keyName.Replace("Position", "Touch"); //ポジションはいったんタッチと同じにする
bool isStick = keyName.Contains("Stick");
//Debug.Log($"ControllerAction_AxisChanged[{e.Name}] IsLeft:{e.IsLeft} isStick:{isStick} Axis:({e.Axis.x}, {e.Axis.y}, {e.Axis.z})");
var newindex = NearestPointIndex(e.IsLeft, e.Axis.x, e.Axis.y, isStick);
if ((isStick ? (e.IsLeft ? lastStickLeftAxisPoint : lastStickRightAxisPoint) : (e.IsLeft ? lastTouchpadLeftAxisPoint : lastTouchpadRightAxisPoint)) != newindex)
{//ドラッグで隣の領域に入った場合
Expand All @@ -266,7 +283,14 @@ private async void ControllerAction_AxisChanged(object sender, OVRKeyEventArgs e
isSendingKey = false;
}
}
if (!doKeyConfig) CheckKey(config, true);
if (newindex == -1 && isStick && ((e.IsLeft && isStickLeftTouchDown == false) || (e.IsLeft == false && isStickRightTouchDown == false)))
{
// スティックにタッチしておらず、センターに戻った時は何もしない
}
else
{
if (!doKeyConfig) CheckKey(config, true);
}
if (e.IsLeft)
{
if (isStick) lastStickLeftAxisPoint = newindex;
Expand Down
34 changes: 22 additions & 12 deletions Assets/Scripts/SteamVRWrapper2.0/SteamVR2Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,17 @@ public class SteamVRActions
public List<SteamVRDefaultBinding> default_bindings;
}

private Dictionary<string, Vector3> LastPositions = new Dictionary<string, Vector3>();
private Dictionary<ulong, (string name, Vector3 axis)> LastPositions = new Dictionary<ulong, (string name, Vector3 axis)>();

private Vector3 GetLastPosition(string shortName)
private Vector3 GetLastPosition(string shortName, bool isLeft)
{
Vector3 axis = Vector3.zero;
var partname = shortName.Substring("Touch".Length);
var key = LastPositions.Keys.FirstOrDefault(d => d.Contains(partname));
if (key != null)
var startsWith = isLeft ? "Left" : "Right";
var value = LastPositions.Values.FirstOrDefault(d => d.name.StartsWith(startsWith) && d.name.Contains(partname));
if (value.name != null)
{
axis = LastPositions[key];
axis = value.axis;
}
return axis;
}
Expand Down Expand Up @@ -215,16 +216,18 @@ void Update()
Debug.Log($"<b>[SteamVR]</b> GetDigitalActionData IsKeyDown ({action.name}): {err} handle: {action.handle}");

bool isTouch = action.ShortName.StartsWith("Touch") && action.ShortName.Contains("Trigger") == false;
Vector3 axis = isTouch ? GetLastPosition(action.ShortName) : Vector3.zero;
KeyDownEvent?.Invoke(this, new OVRKeyEventArgs(action.ShortName, axis, actionset.IsLeft != handSwap, axis != Vector3.zero, isTouch));
bool isStick = action.ShortName.Contains("Stick");
Vector3 axis = isTouch ? GetLastPosition(action.ShortName, actionset.IsLeft) : Vector3.zero;
KeyDownEvent?.Invoke(this, new OVRKeyEventArgs(action.ShortName, axis, actionset.IsLeft != handSwap, axis != Vector3.zero || isStick, isTouch));
}
if (IsKeyUp(action.digitalActionData))
{
Debug.Log($"<b>[SteamVR]</b> GetDigitalActionData IsKeyUp ({action.name}): {err} handle: {action.handle}");

bool isTouch = action.ShortName.StartsWith("Touch") && action.ShortName.Contains("Trigger") == false;
Vector3 axis = isTouch ? GetLastPosition(action.ShortName) : Vector3.zero;
KeyUpEvent?.Invoke(this, new OVRKeyEventArgs(action.ShortName, axis, actionset.IsLeft != handSwap, axis != Vector3.zero, isTouch));
bool isStick = action.ShortName.Contains("Stick");
Vector3 axis = isTouch ? GetLastPosition(action.ShortName, actionset.IsLeft) : Vector3.zero;
KeyUpEvent?.Invoke(this, new OVRKeyEventArgs(action.ShortName, axis, actionset.IsLeft != handSwap, axis != Vector3.zero || isStick, isTouch));
}
}
else if (action.type == "vector1" || action.type == "vector2" || action.type == "vector3")
Expand All @@ -237,11 +240,18 @@ void Update()
Debug.LogWarning($"<b>[SteamVR]</b> GetAnalogActionData error ({action.name}): {err} handle: {action.handle}");
continue;
}
//Debug.Log($"<b>[SteamVR]</b> GetAnalogActionData Position:{action.analogActionData.x},{action.analogActionData.y} ({action.name}): {err} handle: {action.handle}");
if (action.analogActionData.bActive == false || action.ShortName.Contains("Grip")) //QuestのGripアナログは捨てる
{
continue;
}
var axis = new Vector3(action.analogActionData.x, action.analogActionData.y, action.analogActionData.z);
if (axis != Vector3.zero)
var startsWith = actionset.IsLeft ? "Left" : "Right";
var name = startsWith + action.name;
// 初めてか、axisがゼロじゃない時か、前回がゼロ以外だったら1発ゼロは取得する(Oculusでスティックを倒したまま指を離したとき対策)
// ここで名前ではなくhandleで取り回さないとLeftHandとRightHand以外のActionSetの分でゼロが入って狂う
if (axis != Vector3.zero || (LastPositions.ContainsKey(action.handle) == true && LastPositions[action.handle].axis != Vector3.zero))
{
LastPositions[action.name] = axis;
LastPositions[action.handle] = (name, axis);
AxisChangedEvent?.Invoke(this, new OVRKeyEventArgs(action.ShortName, axis, actionset.IsLeft != handSwap, true, false));
}
}
Expand Down
6 changes: 3 additions & 3 deletions ControlWindowWPF/ControlWindowWPF/ShortcutKeyWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private void UpdateView(WriteableBitmap wb)
{
var cwidth = width * 2 / 5;
var cheight = height * 2 / 5;
FillEllipse(wb, (int)(width / 2.0), (int)(height / 2.0), (int)(cwidth / 2), (int)(cheight / 2), isTouchPad ? CenterColor : Color.FromRgb(80, 80, 80));
FillEllipse(wb, (int)(width / 2.0), (int)(height / 2.0), (int)(cwidth / 2), (int)(cheight / 2), CenterColor);
}
var isChecked = false;
TextBlock centerIndexTextBlock;
Expand All @@ -264,10 +264,10 @@ private void UpdateView(WriteableBitmap wb)
isChecked = RightCenterKeyCheckBox.IsChecked.Value;
centerIndexTextBlock = RightCenterIndexTextBlock;
}
centerIndexTextBlock.Text = isChecked ? (points.Count + 1).ToString() : "";
}
else
{
isChecked = true;
if (isLeft)
{
centerIndexTextBlock = LeftStickCenterIndexTextBlock;
Expand All @@ -276,8 +276,8 @@ private void UpdateView(WriteableBitmap wb)
{
centerIndexTextBlock = RightStickCenterIndexTextBlock;
}
centerIndexTextBlock.Text = "";
}
centerIndexTextBlock.Text = isChecked ? (points.Count + 1).ToString() : "" ;
//円形に切り取り
FillEllipse(wb, (int)(width / 2), (int)(height / 2), (int)(width / 2), (int)(height / 2), Colors.White, true);
wb.AddDirtyRect(new Int32Rect(0, 0, wb.PixelWidth, wb.PixelHeight));
Expand Down

0 comments on commit 4c1eae3

Please sign in to comment.