qt gamepad
This article is part of a web dev series from Microsoft. Thank you for supporting the partners who make SitePoint possible.
本文是Microsoft的Web开发系列的一部分。 感谢您支持使SitePoint成为可能的合作伙伴。
Gaming on the Web has come a long way with HTML5 technologies like Canvas, WebGL, and WebAudio. It’s now possible to produce high-fidelity graphics and sound within the browser. However, to provide a true gaming experience, you need input devices designed for gaming. The Gamepad API is a proposed standard of the W3C, and is designed to provide a consistent API across browsers.
借助Canvas,WebGL和WebAudio等HTML5技术,网络游戏已经走了很长一段路。 现在可以在浏览器中生成高保真图形和声音。 但是,为了提供真正的游戏体验,您需要为游戏设计的输入设备。 Gamepad API是W3C的拟议标准,旨在提供跨浏览器的一致API。
The Gamepad API allows users to connect devices like an Xbox Controller to a computer and use them for browser-based experiences! If you have a gamepad, try plugging it into your computer and then press a button. You’ll see the Xbox controller below light up to mirror each movement you make!
Gamepad API允许用户将Xbox控制器等设备连接到计算机,并将其用于基于浏览器的体验! 如果您有游戏板,请尝试将其插入计算机,然后按一个按钮。 您会看到下面的Xbox控制器点亮,以反映您所做的每个动作!
Try it out interactively here.
在这里以交互方式进行尝试 。
This tutorial is the third in a series on Flight Arcade – built to demonstrate what’s possible on the web platform and in the new Microsoft Edge browser and EdgeHTML rendering engine. You can find the first two articles on WebGL and Web API, plus interactive code and examples for this article at flightarcade.com.
本教程是Flight Arcade系列文章中的第三篇,旨在演示在Web平台以及新的Microsoft Edge浏览器和EdgeHTML渲染引擎中可能实现的功能 。 您可以在flightarcade.com上找到有关WebGL和Web API的前两篇文章,以及本文的交互式代码和示例。
The Gamepad API is intelligently designed with flexibility in mind. At a basic level, it provides access to buttons and axes. Button values range from [0 .. 1] while axes range from [-1 .. 1]. All values are normalized to these ranges so developers can expect consistent behavior between devices.
Gamepad API的设计巧妙考虑了灵活性。 在基本级别上,它提供对按钮和轴的访问。 按钮值的范围是[0 .. 1] ,轴的范围是[-1 .. 1] 。 所有值都被标准化为这些范围,因此开发人员可以期望设备之间的行为一致。
The Gamepad object provides detailed information about the manufacturer and model of the connected gamepad. More useful is a mapping property which describes the general type of gamepad. Currently the only supported mapping is standard which corresponds to the controller layout used by many popular game consoles like the Xbox.
Gamepad对象提供有关所连接游戏板的制造商和型号的详细信息。 更有用的是一个mapping属性,它描述游戏手柄的常规类型。 当前唯一支持的映射是standard ,它对应于许多流行的游戏机(如Xbox)使用的控制器布局。
The standard controller mapping has two sticks, each of which is represented by 2 axes (x and y). It also includes a D-pad, 4 game buttons, top buttons, and triggers: all represented as buttons in the Gamepad API.
标准控制器映射有两个摇杆,每个摇杆由2个轴( x和y )表示。 它还包括一个D-pad,4个游戏按钮,顶部按钮和触发器:在Gamepad API中均表示为按钮。
Current Xbox controllers report button state as either 0 (normal state) or 1 (pressed). However, you could imagine that future controllers could report the amount of force applied to each button press.
当前的Xbox控制器报告按钮状态为0(正常状态)或1(按下)。 但是,您可以想象将来的控制器可以报告每次按键所施加的力。
The Xbox D-pad also reports discrete values (0 or 1), but the sticks provide continuous values across the entire axis range [-1 .. 1]. This additional precision makes it much easier to fly the airplane in our Flight Arcade missions.
Xbox D-pad还会报告离散值( 0或1 ),但控制杆在整个轴范围内提供连续值[-1 .. 1] 。 这种额外的精度使在我们的“飞行街机”任务中驾驶飞机变得更加容易。
The array of buttons and axes provided by the Gamepad API is forward thinking and perfect as a low level API. However, when writing a game, it’s nice to have a higher level representation of a standard gamepad like the Xbox One controller. We created a helper class named PxGamepad that maps the button and axis indices to the more familiar names as labeled on the Xbox controller.
Gamepad API提供的按钮和轴的排列具有前瞻性,是低级API的理想选择。 但是,编写游戏时,最好具有Xbox One控制器之类的标准游戏手柄的高级表示。 我们创建了一个名为PxGamepad的帮助程序类,该类将按钮和轴索引映射到Xbox控制器上标记的更熟悉的名称。
We’ll walk through a few interesting pieces of the library, but the full source code (MIT License) is available here: https://github.com/thinkpixellab/PxGamepad
我们将逐步介绍该库的一些有趣部分,但是完整的源代码(MIT许可证)可以在这里找到: https : //github.com/thinkpixellab/PxGamepad
The standard Gamepad API provides button state as an array of buttons. Again, this API is designed for flexibility allowing controllers with various button counts. However, when writing a game, it’s much easier to write and read code that uses the standard mapped button names.
标准的Gamepad API将按钮状态作为按钮数组提供。 同样,此API的设计具有灵活性,允许具有不同按钮计数的控制器。 但是,在编写游戏时,编写和读取使用标准映射按钮名称的代码要容易得多。
For example, with the HTML5 gamepad api, here is the code to check whether the left trigger is currently pressed:
例如,使用HTML5游戏手柄API,以下代码可检查当前是否按下了左触发器:
The PxGamepad class contains an update method that will gather the state for all the standard mapped buttons and axes. So determining whether the leftTrigger is pressed is as simple as accessing a boolean property:
PxGamepad类包含一个update方法,该方法将收集所有标准映射按钮和轴的状态。 因此,确定是否按下leftTrigger就像访问布尔属性一样简单:
Axes in the standard Gamepad API are also provided as an array of numerical values. For example, here is the code to get the normalized x and y values for the left stick:
标准Gamepad API中的轴也以数值数组形式提供。 例如,下面是获取左摇杆的标准化x和y值的代码:
The D-pad is a special case, because it considered a set of four buttons by the HTML5 Gamepad API (indices 12, 13, 14, and 15). However, its common for developers to allow the dpad to be used in the same way as one of the sticks. PxGamepad provides button infomation for the D-pad, but also sythesizes axis information as though the D-pad were a stick:
D-pad是一种特殊情况,因为它通过HTML5 Gamepad API(索引12、13、14和15)考虑了一组四个按钮。 但是,对于开发人员来说,常见的做法是允许dpad的使用方式与其中之一相同。 PxGamepad提供了D-pad的按钮信息,而且还合成了轴信息,就像D-pad是摇杆一样:
Another limitation of the HTML5 Gamepad API is that is doesn’t provide button level events. It’s common for a game developer to want to activate a single event for a button press. In flight arcade, the ignition and brake buttons are good examples. PxGamepad watches button state and allows callers to register for notifications on button release.
HTML5 Gamepad API的另一个限制是它不提供按钮级事件。 对于游戏开发人员来说,想激活一个按钮按下的事件是很常见的。 在飞行拱廊中,点火按钮和制动按钮就是很好的例子。 PxGamepad监视按钮状态,并允许呼叫者注册按钮释放的通知。
Here is the full list of named buttons supported by PxGamepad:
这是PxGamepad支持的命名按钮的完整列表:
a 一个 b b x X y ÿ leftTop leftTop rightTop rightTop leftTrigger leftTrigger rightTrigger rightTrigger select 选择 start 开始 leftStick leftStick rightStick 右棒 dpadUp dpadUp dpadDown dpadDown dpadLeft dpadLeft dpadRight dpad右There are two methods for retrieving the gamepad object. The Gamepad API adds a method to the navigator object named getGamepads() which returns an array of all connected gamepads. There are also new gamepadconnected and gamepaddisconnected events that are fired whenever a new gamepad has been connected or disconnected. For example, here is how the PxGamepad helper stores the last connected gamepad:
有两种方法可以检索游戏手柄对象。 Gamepad API向名为getGamepads()的导航器对象添加了一个方法,该方法返回所有已连接游戏板的数组。 每当连接或断开新游戏手柄时,也会触发新的gamepadconnected和gamepaddisconnected事件。 例如,以下是PxGamepad帮助程序存储最后连接的游戏板的方式:
And here is the helper to retrieve the first standard gamepad using the navigator.getGamepads() API:
这是使用navigator.getGamepads() API检索第一个标准游戏手柄的助手:
The PxGamepad helper class is designed for the simple scenario where a single user is playing a game with a standard mapped gamepad. The latest browsers, like Microsoft Edge, fully support the W3C Gampepad API. However, older versions of some other browsers only supported pieces of the emerging specification. The PxGamepad listens for the gamepadconnected events and falls back to querying for the list of all gamepads if needed.
PxGamepad帮助程序类是为简单的场景而设计的,其中单个用户正在使用标准映射的游戏手柄来玩游戏。 最新的浏览器,例如Microsoft Edge ,完全支持W3C Gampepad API。 但是,某些其他浏览器的旧版本仅支持该新兴规范。 PxGamepad侦听gamepadconnected事件,并在需要时返回查询所有Gamepad的列表。
While PxGamepad is focused on the simple, most common scenario, the Gamepad API is fully capable of supporting multiple players, each with their own gamepad. One possible improvement for PxGamepad might be to provide a manager-style class which tracks connection of multiple gamepads and maps them to multiple players in a game. Another might be to allow users to remap or customize the button functions on their gamepads.
虽然PxGamepad专注于简单,最常见的场景,但Gamepad API完全能够支持多个玩家,每个玩家都有自己的游戏板。 PxGamepad的一项可能的改进可能是提供一种管理器样式的类,该类跟踪多个游戏板的连接并将它们映射到游戏中的多个玩家。 另一个可能是允许用户重新映射或自定义游戏板上的按钮功能。
We’re also excited about the potential of the Gamepad for non-game scenarios. With the rise of WebGL, we’re seeing a variety of innovative uses for 3D on the web. That might mean exploring the Mt. Everest region in 3D with GlacierWorks. Or viewing the Assyrian Collection of the British Museum thanks to CyArk’s efforts to digitally preserve important world sites and artefacts.
我们也对Gamepad在非游戏场景中的潜力感到兴奋。 随着WebGL的兴起,我们在网络上看到了3D的各种创新用途。 那可能意味着探索山。 使用GlacierWorks进行3D 珠穆朗玛峰地区 。 或感谢CyArk致力于以数字方式保存重要的世界遗迹和文物的方法,从而查看大英博物馆的亚述收藏 。
During the development of Flight Arcade, we frequently used Blender and other 3D tools to process models for Babylon.JS. Some developers and artists use a device called a 3D mouse to help manipulate and navigate 3D models. These devices track movement of a single knob through six axes! They make it really easy and quick to manipulate models. Beyond gaming, they’re used in a variety of interesting applications from engineering to medical imaging. While adding gamepad support to Flight Arcade, we were surprised to learn that the Gamepad API detected our 3D SpaceMouse and provided movement data for all six axes!
在Flight Arcade的开发过程中,我们经常使用Blender和其他3D工具处理Babylon.JS的模型。 一些开发人员和美术师使用一种称为3D鼠标的设备来帮助操纵和导航3D模型。 这些设备跟踪单个旋钮在六个轴上的运动! 它们使操作模型变得非常容易和快捷。 除了游戏之外,它们还用于从工程到医学成像的各种有趣应用。 在为Flight Arcade添加游戏板支持时,我们惊讶地发现Gamepad API检测到我们的3D SpaceMouse并提供了所有六个轴的运动数据!
It’s exciting to imagine all the possibilities that the new Gamepad API offers. Now is a great time to experiment with the new Gamepad API and add precision control and a lot of fun to your next game or application!
想象新的Gamepad API提供的所有可能性令人兴奋。 现在是尝试新的Gamepad API并为下一个游戏或应用程序添加精度控制和很多乐趣的好时机!
Microsoft has a bunch of free learning on many open source JavaScript topics and we’re on a mission to create a lot more with Microsoft Edge. Here are some to check-out:
微软在许多开源JavaScript主题上有大量免费学习知识,我们的使命是利用Microsoft Edge创造更多内容。 这里有一些要退房:
Microsoft Edge Web Summit 2015 (a complete series of what to expect with the new browser, new web platform features, and guest speakers from the community)
Microsoft Edge Web Summit 2015 (关于新浏览器,新的Web平台功能以及社区来宾演讲者的期望的完整系列)
Build of //BUILD/ and Windows 10 (including the new JavaScript engine for sites and apps)
// BUILD /和Windows 10的构建 (包括用于站点和应用程序的新JavaScript引擎)
Advancing JavaScript without Breaking the Web (Christian Heilmann’s recent keynote)
在不中断网络的情况下推进JavaScript (Christian Heilmann最近的主题演讲)
Hosted Web Apps and Web Platform Innovations (a deep-dive on topics like manifold.JS)
托管的Web应用程序和Web平台创新 (深入研究诸如歧管(JS)之类的主题)
Practical Performance Tips to Make your HTML/JavaScript Faster (a 7-part series from responsive design to casual games to performance optimization)
使您HTML / JavaScript更快的实用性能技巧 (从响应设计到休闲游戏到性能优化的7个系列文章)
The Modern Web Platform JumpStart (the fundamentals of HTML, CSS, and JS)
现代Web平台JumpStart (HTML,CSS和JS的基础知识)
And some free tools to get started: Visual Studio Code, Azure Trial, and cross-browser testing tools – all available for Mac, Linux, or Windows.
还有一些免费的入门工具: Visual Studio Code , Azure试用版和跨浏览器测试工具 -所有这些工具都可用于Mac,Linux或Windows。
This article is part of the web dev tech series from Microsoft. We’re excited to share Microsoft Edge and the new EdgeHTML rendering engine with you. Get free virtual machines or test remotely on your Mac, iOS, Android, or Windows device @ modern.IE.
本文是Microsoft的Web开发技术系列的一部分。 我们很高兴与您共享Microsoft Edge和新的EdgeHTML渲染引擎 。 获取免费的虚拟机或在Mac,iOS,Android或Windows设备@ modern.IE上进行远程测试。
翻译自: https://www.sitepoint.com/a-true-gaming-experience-with-the-gamepad-api/
qt gamepad
相关资源:QtGamepad模块与游戏手柄交互示例.rar