|
9 | 9 | import spatialmath as sm
|
10 | 10 | import time
|
11 | 11 | from queue import Queue
|
| 12 | +import json |
12 | 13 |
|
13 | 14 | _sw = None
|
14 | 15 | sw = None
|
@@ -72,6 +73,12 @@ def __init__(self, realtime=True, display=True):
|
72 | 73 | self.outq = Queue()
|
73 | 74 | self.inq = Queue()
|
74 | 75 |
|
| 76 | + # Number of custom html elements added to page for id purposes |
| 77 | + self.elementid = 0 |
| 78 | + |
| 79 | + # Element dict which holds the callback functions for form updates |
| 80 | + self.elements = {} |
| 81 | + |
75 | 82 | self.realtime = realtime
|
76 | 83 | self.display = display
|
77 | 84 |
|
@@ -383,6 +390,46 @@ def stop_recording(self):
|
383 | 390 | "You must call swift.start_recording(file_name) before trying"
|
384 | 391 | " to stop the recording")
|
385 | 392 |
|
| 393 | + def add_slider( |
| 394 | + self, cb, min=0, max=100, step=1, value=0, desc='', unit=''): |
| 395 | + """ |
| 396 | + Add a range-slider to the page |
| 397 | +
|
| 398 | + :param cb: A callback function which is executed when the value of the |
| 399 | + slider changes. The callback should accept one argument which |
| 400 | + represents the new value of the slider |
| 401 | + :type cb: function |
| 402 | + :param min: the minimum value of the slider, optional |
| 403 | + :type min: float |
| 404 | + :param max: the maximum value of the slider, optional |
| 405 | + :type max: float |
| 406 | + :param step: the step size of the slider, optional |
| 407 | + :type step: float |
| 408 | + :param desc: add a description of the slider, optional |
| 409 | + :type desc: str |
| 410 | + :param unit: add a unit to the slider value, optional |
| 411 | + :type unit: str |
| 412 | +
|
| 413 | + ``env.add_slider()`` adds a slider to the graphical |
| 414 | + environment. |
| 415 | +
|
| 416 | + """ |
| 417 | + |
| 418 | + id = 'customelement' + str(self.elementid) |
| 419 | + self.elementid += 1 |
| 420 | + self.elements[id] = cb |
| 421 | + |
| 422 | + self._send_socket( |
| 423 | + 'add_element', |
| 424 | + ['slider', id, min, max, step, value, desc, unit]) |
| 425 | + |
| 426 | + def process_events(self): |
| 427 | + events = self._send_socket('check_elements') |
| 428 | + events = json.loads(events) |
| 429 | + |
| 430 | + for event in events: |
| 431 | + self.elements[event](events[event]) |
| 432 | + |
386 | 433 | def _step_robots(self, dt):
|
387 | 434 |
|
388 | 435 | for robot_object in self.robots:
|
|
0 commit comments