Пример #1
0
class CameraWatcher(Watcher):
    def __init__(self, camera, **kwargs):
        super().__init__()
        self.camera = camera
        if "max_width" not in kwargs:
            kwargs["max_width"] = "100%"
        if "height" not in kwargs:
            kwargs["height"] = "auto"
        if "width" not in kwargs:
            kwargs["width"] = "500px"

        self.widget = Image(layout=Layout(**kwargs))
        self.widget.add_class("pixelated")
        # Update and draw:
        self.draw()

    def draw(self):
        picture = self.camera.get_image()
        self.widget.value = image_to_png(picture)

    def update(self):
        pass

    def reset(self):
        pass
Пример #2
0
class RobotWatcher(Watcher):
    def __init__(self, robot, size=100, show_robot=True, attributes="all"):
        super().__init__()
        self.robot = robot
        self.size = size
        self.show_robot = show_robot
        self.map = {}
        self.all_attrs = [
            "name", "x", "y", "a", "stalled", "tvx", "tva", "state"
        ]
        if attributes == "all":
            self.visible = self.all_attrs[:]
        else:
            self.visible = attributes[:]
        self.labels = [
            "Name:",
            "X:",
            "Y:",
            "A:",
            "Stalled:",
            "Trans vel:",
            "Rotate vel:",
            "State:",
        ]
        widget = make_attr_widget(self.robot, self.map, None, self.all_attrs,
                                  self.labels)
        self.image = Image(layout=Layout(
            width="-webkit-fill-available",
            height="auto",
        ))
        self.image.add_class("pixelated")
        if not self.show_robot:
            self.image.layout.display = "none"
        widget.children = [self.image] + list(widget.children)

        self.widget = widget
        self.update()
        self.draw()

    def set_arguments(self, size=None, show_robot=None, attributes=None):
        if size is not None:
            self.size = size
        if show_robot is not None:
            self.show_robot = show_robot

        if show_robot is not None:
            if not self.show_robot:
                self.image.layout.display = "none"
            else:
                self.image.layout.display = "block"

        if attributes is not None:
            if attributes == "all":
                self.visible = self.all_attrs[:]
            else:
                self.visible = attributes[:]

    def draw(self):
        if self.robot.world is None:
            print("This robot is not in a world")
            return

        if self.show_robot:
            image = self.robot.get_image(size=self.size)
            self.image.value = image_to_png(image)
        for i in range(len(self.all_attrs)):
            attr_name = self.all_attrs[i]
            attr = getattr(self.robot, attr_name)
            if isinstance(attr, dict):
                string = json.dumps(attr, sort_keys=True, indent=2)
            else:
                string = str(attr)
            if attr_name in self.visible:
                self.map[self.labels[i]].value = string
                self.map[self.labels[i]].layout.display = "flex"
            else:
                self.map[self.labels[i]].layout.display = "none"

    def update(self):
        pass

    def reset(self):
        pass