Debian Codegraph @

jupyter-widgets-controls/lib/widget_controller.js

raw
1 // Copyright (c) Jupyter Development Team.
2 // Distributed under the terms of the Modified BSD License.
3 import { CoreDOMWidgetModel } from './widget_core';
4 import { DOMWidgetView, unpack_models, ViewList, JupyterLuminoPanelWidget, reject, } from '@jupyter-widgets/base';
5 import { Widget, Panel } from '@lumino/widgets';
6 import { ArrayExt } from '@lumino/algorithm';
7 import * as utils from './utils';
8 import $ from 'jquery';
9 export class ControllerButtonModel extends CoreDOMWidgetModel {
10 defaults() {
11 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'ControllerButtonModel', _view_name: 'ControllerButtonView', value: 0.0, pressed: false });
12 }
13 }
14 /**
15 * Very simple view for a gamepad button.
16 */
17 export class ControllerButtonView extends DOMWidgetView {
18 render() {
19 this.el.classList.add('jupyter-widgets');
20 this.el.classList.add('widget-controller-button');
21 this.el.style.width = 'fit-content';
22 this.support = document.createElement('div');
23 this.support.style.position = 'relative';
24 this.support.style.margin = '1px';
25 this.support.style.width = '16px';
26 this.support.style.height = '16px';
27 this.support.style.border = '1px solid black';
28 this.support.style.background = 'lightgray';
29 this.el.appendChild(this.support);
30 this.bar = document.createElement('div');
31 this.bar.style.position = 'absolute';
32 this.bar.style.width = '100%';
33 this.bar.style.bottom = '0px';
34 this.bar.style.background = 'gray';
35 this.support.appendChild(this.bar);
36 this.update();
37 this.label = document.createElement('div');
38 this.label.textContent = this.model.get('description');
39 this.label.style.textAlign = 'center';
40 this.el.appendChild(this.label);
41 }
42 update() {
43 this.bar.style.height = 100 * this.model.get('value') + '%';
44 }
45 }
46 export class ControllerAxisModel extends CoreDOMWidgetModel {
47 defaults() {
48 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'ControllerAxisModel', _view_name: 'ControllerAxisView', value: 0.0 });
49 }
50 }
51 /**
52 * Very simple view for a gamepad axis.
53 */
54 export class ControllerAxisView extends DOMWidgetView {
55 render() {
56 this.el.classList.add('jupyter-widgets');
57 this.el.classList.add('widget-controller-axis');
58 this.el.style.width = '16px';
59 this.el.style.padding = '4px';
60 this.support = document.createElement('div');
61 this.support.style.position = 'relative';
62 this.support.style.margin = '1px';
63 this.support.style.width = '4px';
64 this.support.style.height = '64px';
65 this.support.style.border = '1px solid black';
66 this.support.style.background = 'lightgray';
67 this.bullet = document.createElement('div');
68 this.bullet.style.position = 'absolute';
69 this.bullet.style.margin = '-3px';
70 this.bullet.style.boxSizing = 'unset';
71 this.bullet.style.width = '10px';
72 this.bullet.style.height = '10px';
73 this.bullet.style.background = 'gray';
74 this.label = document.createElement('div');
75 this.label.textContent = this.model.get('description');
76 this.label.style.textAlign = 'center';
77 this.support.appendChild(this.bullet);
78 this.el.appendChild(this.support);
79 this.el.appendChild(this.label);
80 this.update();
81 }
82 update() {
83 this.bullet.style.top = 50 * (this.model.get('value') + 1) + '%';
84 }
85 }
86 export class ControllerModel extends CoreDOMWidgetModel {
87 defaults() {
88 return Object.assign(Object.assign({}, super.defaults()), { _model_name: 'ControllerModel', _view_name: 'ControllerView', index: 0, name: '', mapping: '', connected: false, timestamp: 0, buttons: [], axes: [] });
89 }
90 initialize(attributes, options) {
91 super.initialize(attributes, options);
92 if (navigator.getGamepads === void 0) {
93 // Checks if the browser supports the gamepad API
94 this.readout = 'This browser does not support gamepads.';
95 console.error(this.readout);
96 }
97 else {
98 // Start the wait loop, and listen to updates of the only
99 // user-provided attribute, the gamepad index.
100 this.readout = 'Connect gamepad and press any button.';
101 if (this.get('connected')) {
102 // No need to re-create Button and Axis widgets, re-use
103 // the models provided by the backend which may already
104 // be wired to other things.
105 this.update_loop();
106 }
107 else {
108 // Wait for a gamepad to be connected.
109 this.wait_loop();
110 }
111 }
112 }
113 /**
114 * Waits for a gamepad to be connected at the provided index.
115 * Once one is connected, it will start the update loop, which
116 * populates the update of axes and button values.
117 */
118 wait_loop() {
119 const index = this.get('index');
120 const pad = navigator.getGamepads()[index];
121 if (pad) {
122 this.setup(pad).then((controls) => {
123 this.set(controls);
124 this.save_changes();
125 window.requestAnimationFrame(this.update_loop.bind(this));
126 });
127 }
128 else {
129 window.requestAnimationFrame(this.wait_loop.bind(this));
130 }
131 }
132 /**
133 * Given a native gamepad object, returns a promise for a dictionary of
134 * controls, of the form
135 * {
136 * buttons: list of Button models,
137 * axes: list of Axis models,
138 * }
139 */
140 setup(pad) {
141 // Set up the main gamepad attributes
142 this.set({
143 name: pad.id,
144 mapping: pad.mapping,
145 connected: pad.connected,
146 timestamp: pad.timestamp,
147 });
148 // Create buttons and axes. When done, start the update loop
149 return utils.resolvePromisesDict({
150 buttons: Promise.all(pad.buttons.map((btn, index) => {
151 return this._create_button_model(index);
152 })),
153 axes: Promise.all(pad.axes.map((axis, index) => {
154 return this._create_axis_model(index);
155 })),
156 });
157 }
158 /**
159 * Update axes and buttons values, until the gamepad is disconnected.
160 * When the gamepad is disconnected, this.reset_gamepad is called.
161 */
162 update_loop() {
163 const index = this.get('index');
164 const id = this.get('name');
165 const pad = navigator.getGamepads()[index];
166 if (pad && index === pad.index && id === pad.id) {
167 this.set({
168 timestamp: pad.timestamp,
169 connected: pad.connected,
170 });
171 this.save_changes();
172 this.get('buttons').forEach(function (model, index) {
173 model.set({
174 value: pad.buttons[index].value,
175 pressed: pad.buttons[index].pressed,
176 });
177 model.save_changes();
178 });
179 this.get('axes').forEach(function (model, index) {
180 model.set('value', pad.axes[index]);
181 model.save_changes();
182 });
183 window.requestAnimationFrame(this.update_loop.bind(this));
184 }
185 else {
186 this.reset_gamepad();
187 }
188 }
189 /**
190 * Resets the gamepad attributes, and start the wait_loop.
191 */
192 reset_gamepad() {
193 this.get('buttons').forEach(function (button) {
194 button.close();
195 });
196 this.get('axes').forEach(function (axis) {
197 axis.close();
198 });
199 this.set({
200 name: '',
201 mapping: '',
202 connected: false,
203 timestamp: 0.0,
204 buttons: [],
205 axes: [],
206 });
207 this.save_changes();
208 window.requestAnimationFrame(this.wait_loop.bind(this));
209 }
210 /**
211 * Creates a gamepad button widget.
212 */
213 _create_button_model(index) {
214 return this.widget_manager
215 .new_widget({
216 model_name: 'ControllerButtonModel',
217 model_module: '@jupyter-widgets/controls',
218 model_module_version: this.get('_model_module_version'),
219 view_name: 'ControllerButtonView',
220 view_module: '@jupyter-widgets/controls',
221 view_module_version: this.get('_view_module_version'),
222 })
223 .then(function (model) {
224 model.set('description', index);
225 return model;
226 });
227 }
228 /**
229 * Creates a gamepad axis widget.
230 */
231 _create_axis_model(index) {
232 return this.widget_manager
233 .new_widget({
234 model_name: 'ControllerAxisModel',
235 model_module: '@jupyter-widgets/controls',
236 model_module_version: this.get('_model_module_version'),
237 view_name: 'ControllerAxisView',
238 view_module: '@jupyter-widgets/controls',
239 view_module_version: this.get('_view_module_version'),
240 })
241 .then(function (model) {
242 model.set('description', index);
243 return model;
244 });
245 }
246 }
247 ControllerModel.serializers = Object.assign(Object.assign({}, CoreDOMWidgetModel.serializers), { buttons: { deserialize: unpack_models }, axes: { deserialize: unpack_models } });
248 /**
249 * A simple view for a gamepad.
250 */
251 export class ControllerView extends DOMWidgetView {
252 _createElement(tagName) {
253 this.luminoWidget = new JupyterLuminoPanelWidget({ view: this });
254 return this.luminoWidget.node;
255 }
256 _setElement(el) {
257 if (this.el || el !== this.luminoWidget.node) {
258 // Boxes don't allow setting the element beyond the initial creation.
259 throw new Error('Cannot reset the DOM element.');
260 }
261 this.el = this.luminoWidget.node;
262 this.$el = $(this.luminoWidget.node);
263 }
264 initialize(parameters) {
265 super.initialize(parameters);
266 this.button_views = new ViewList(this.add_button, null, this);
267 this.listenTo(this.model, 'change:buttons', (model, value) => {
268 this.button_views.update(value);
269 });
270 this.axis_views = new ViewList(this.add_axis, null, this);
271 this.listenTo(this.model, 'change:axes', (model, value) => {
272 this.axis_views.update(value);
273 });
274 this.listenTo(this.model, 'change:name', this.update_label);
275 }
276 render() {
277 this.el.classList.add('jupyter-widgets');
278 this.el.classList.add('widget-controller');
279 this.label = document.createElement('div');
280 this.el.appendChild(this.label);
281 this.axis_box = new Panel();
282 this.axis_box.node.style.display = 'flex';
283 this.luminoWidget.addWidget(this.axis_box);
284 this.button_box = new Panel();
285 this.button_box.node.style.display = 'flex';
286 this.luminoWidget.addWidget(this.button_box);
287 this.button_views.update(this.model.get('buttons'));
288 this.axis_views.update(this.model.get('axes'));
289 this.update_label();
290 }
291 update_label() {
292 this.label.textContent = this.model.get('name') || this.model.readout;
293 }
294 add_button(model) {
295 // we insert a dummy element so the order is preserved when we add
296 // the rendered content later.
297 const dummy = new Widget();
298 this.button_box.addWidget(dummy);
299 return this.create_child_view(model)
300 .then((view) => {
301 // replace the dummy widget with the new one.
302 const i = ArrayExt.firstIndexOf(this.button_box.widgets, dummy);
303 this.button_box.insertWidget(i, view.luminoWidget);
304 dummy.dispose();
305 return view;
306 })
307 .catch(reject('Could not add child button view to controller', true));
308 }
309 add_axis(model) {
310 // we insert a dummy element so the order is preserved when we add
311 // the rendered content later.
312 const dummy = new Widget();
313 this.axis_box.addWidget(dummy);
314 return this.create_child_view(model)
315 .then((view) => {
316 // replace the dummy widget with the new one.
317 const i = ArrayExt.firstIndexOf(this.axis_box.widgets, dummy);
318 this.axis_box.insertWidget(i, view.luminoWidget);
319 dummy.dispose();
320 return view;
321 })
322 .catch(reject('Could not add child axis view to controller', true));
323 }
324 remove() {
325 super.remove();
326 this.button_views.remove();
327 this.axis_views.remove();
328 }
329 }
330 //# sourceMappingURL=widget_controller.js.map