IGameInput を使用してゲームのネイティブ タッチ インターフェイスを構築する
プレイヤーがゲームをモバイル デバイスにストリーミングする場合、ゲームの楽しみを増やすための最良の方法の 1 つは、タッチ コントロールを使用してゲームを操作できるようにすることです。 Xbox ゲーム ストリーミングは、タッチ アダプテーション キットを使用してゲームにタッチ コントロールをオーバーレイする機能をサポートしています。これは、仮想ゲーム パッドを持つことがゲームをプレイする自然な方法であるゲームの多くの画面に最適です。 ただし、メニュー、マップ、インベントリ画面などのゲームの一部では、タッチ入力を使用してゲームを直接操作する方が自然な場合があります。
たとえば、プレイヤーがメニュー オプションをタップして操作したり、携帯電話のジェスチャーを使用してズーム インしたり、地図をスクロールしたりするのは非常に自然なことです。 Microsoft Game Development Kit (GDK) は、このネイティブ タッチ インターフェイスをゲームに組み込むことを可能にする API を提供し、真のモバイル エクスペリエンスのように感じさせます。
タッチ アダプテーション キットとネイティブ タッチは相互に排他的ではありません。タイトルに最も適したタッチ入力の各形式を使用して、ゲームで両方を使用できます。 たとえば、メインのゲーム プレイ ループにタッチ アダプテーション キットを使用しているときにネイティブのタッチ メニューを用意することは、タイトルのタッチ入力戦略を構築するための合理的な方法である可能性があります。
注意
ネイティブ タッチでゲームを構築している場合は、アカウント マネージャーに通知してください。 Xbox Game Streaming バック エンド サービスは、ゲームが小売環境に展開されたときにゲームがタッチ イベントを受信できるように構成する必要があります。
ネイティブ タッチは、IGameInputReading::GetTouchState API を介してゲームで利用できるようになります。 この API は、プレイヤーが画面に触れている現在の指のセットを表す現在のタッチ ポイントのセットを提供します。
これらのタッチ ポイントをプレス、移動、およびリリース イベントに変換するには、各フレームでそれらの状態を追跡すると便利です。
- 前のフレームには存在しなかったが、現在のフレームには存在するタッチ ポイントは、新しいタッチ プレスです。
- 前のフレームに存在し、現在のフレームにまだ存在するタッチ ポイントは、移動、またはタッチの座標が変更されていない場合は無操作のいずれかです。
- 前のフレームに存在していたが存在しなくなったタッチ ポイントは、タッチ リリースです。
これらのプレス、移動、リリース イベントを構築するための非常に基本的な入力ループは、次のようになります。
struct TouchPoint
{
// GameInputTouchState.touchId corresponding to this point
uint64_t Id = 0;
// X pixel being touched
uint32_t X = 0;
// Y pixel being touched
uint32_t Y = 0;
// Is this entry in the array of touch points currently being used to track a touch
bool IsInUse = false;
// Is this TouchPoint tracking a finger which is currently touching the screen
bool IsTouchActive = false;
};
// Touch points currently being tracked
std::array<TouchPoint, 10> g_touchPoints = {};
extern IGameInput* g_gameInput;
extern uint32_t g_frameWidth;
extern uint32_t g_frameHeight;
void TouchPointPressed(const TouchPoint& touchPoint);
void TouchPointMoved(const TouchPoint& touchPoint);
void TouchPointReleased(const TouchPoint& touchPoint);
void ProcessTouchInput()
{
// Reset the active touch state from the previous frame
for (TouchPoint& touchPoint : g_touchPoints)
{
touchPoint.IsTouchActive = false;
}
// Process any new touch points for this frame
Microsoft::WRL::ComPtr<IGameInputReading> reading = nullptr;
if (SUCCEEDED(g_gameInput->GetCurrentReading(GameInputKindTouch, nullptr, &reading)))
{
uint32_t touchCount = reading->GetTouchCount();
if (touchCount > 0)
{
std::vector<GameInputTouchState> touchStates(touchCount);
touchCount = reading->GetTouchState(touchCount, touchStates.data());
for (const GameInputTouchState& touchState : touchStates)
{
const uint32_t x = static_cast<uint32_t>(touchState.positionX * g_frameWidth);
const uint32_t y = static_cast<uint32_t>(touchState.positionY * g_frameHeight);
// Check to see if we are already tracking this touch point
auto existingPoint = std::find_if(std::begin(g_touchPoints), std::end(g_touchPoints), [id = touchState.touchId](const TouchPoint& point)
{
return point.IsInUse && point.Id == id;
});
if (existingPoint != g_touchPoints.end())
{
// We were already tracking the point - it is still alive this frame, but it may have
// also moved position.
existingPoint->IsTouchActive = true;
if (existingPoint->X != x || existingPoint->Y != y)
{
existingPoint->X = x;
existingPoint->Y = y;
TouchPointMoved(*existingPoint);
}
}
else
{
// This is a new touch point. Start tracking it and treat it as a press
auto insertPoint = std::find_if(std::begin(g_touchPoints), std::end(g_touchPoints), [](const TouchPoint& point)
{
return !point.IsInUse;
});
if (insertPoint != std::end(g_touchPoints))
{
insertPoint->Id = touchState.touchId;
insertPoint->X = x;
insertPoint->Y = y;
insertPoint->IsInUse = true;
insertPoint->IsTouchActive = true;
TouchPointPressed(*insertPoint);
}
}
}
}
// Look for any points which were pressed last frame but are no longer pressed this frame
// and treat those as touch releases
for (TouchPoint& touchPoint : g_touchPoints)
{
if (touchPoint.IsInUse && !touchPoint.IsTouchActive)
{
TouchPointReleased(touchPoint);
touchPoint = TouchPoint{};
}
}
}
}