Hi both,

Using Thunderbird, and no idea how to format the code properly...

Anyways, here goes, thank you so much for looking:


"""
Pyglet HUD.

Each square is represented with a Square instance.

This square stores resource objects created by Pyglet in it's
resources attribute.

Getting the current resource with square.resource will either return None if
there aren't any, or the last one in the list.

The HUD works by showing you n squares around the centre, where n is HUG.zoom,
and the centre is HUG.centre.

On the right hand side of the HUD is the text pane where a title is in
HUD.title_label.text, the most recent incoming message is in
HUD.message_label.text, and the main text is in HUD.layout.document.text.
"""

from pyglet.text import document, layout, Label
from math import floor
from attr import attrs, attrib, Factory


@attrs
class Square:
    """A cell on the HUD grid."""

    x = attrib()
    y = attrib()
    resources = attrib(init=False, default=Factory(list))

    @property
    def resource(self):
        """Get the most recent resource."""
        if self.resources:
            return self.resources[-1]

    def add_resource(self, resource):
"""Append a resource to self.resources. The resource object must have a
        blit method."""
        self.resources.append(resource)

    def remove_resource(self, resource):
        """Remove the specified resource from this square."""
        if resource in self.resources:
            self.resources.remove(resource)


@attrs
class HUD:
    window = attrib()
    centre = attrib(default=Factory(lambda: (0, 0)))
    zoom = attrib(default=Factory(lambda: 2))
    grid = attrib(init=False, repr=False, default=Factory(dict))
    title_label = attrib(default=Factory(lambda: None))
    title_label_height = attrib(default=100)
    layout = attrib(init=False, default=Factory(lambda: None))
    message_label = attrib(default=Factory(lambda: None))
    message_label_height = attrib(default=100)

    def __attrs_post_init__(self):
        self.draw()

    def add_resource(self, resource, x, y):
        """Add the provided resource object at (x, y)."""
        s = self.grid.get((x, y))
        if s is None:
            s = Square(x, y)
        s.add_resource(resource)
        self.grid[(x, y)] = s
        self.draw()

    def clear(self):
        """Clear all squares."""
        self.grid.clear()
        self.draw()

    def zoom_in(self):
        """Decrease the size of the HUD, making the squares that do show
        bigger."""
        if self.zoom > 1:
            self.zoom -= 1
            self.draw()
        else:
            raise RuntimeError('Cannot zoom in anymore.')

    def zoom_out(self):
"""Zoom out, making more squares fit on the screen but at the cost of
        size."""
        if self.zoom < min(self.window.width, self.window.height):
            self.zoom += 1
            self.draw()
        else:
            raise RuntimeError('Cannot zoom out anymore.')

    def draw(self):
        """Draw the grid."""
        # Establish the smallest side of the screen:
        least = min(self.window.width, self.window.height)
        most = max(self.window.width, self.window.height)
        width = most - least
        if self.title_label is None:
            # Draw the title label.
            self.title_label = Label(
                x=least,
                y=self.window.height - self.title_label_height,
                width=width,
                height=self.title_label_height
            )
        if self.layout is None:
            # Draw the layout.
            layout_height = self.window.height - self.title_label_height
            layout_height -= self.message_label_height
            self.layout = layout.ScrollableTextLayout(
                document.UnformattedDocument(),
                width,
                layout_height,
                multiline=True,
            )
            self.layout.x = least
            self.y = self.message_label_height
        if self.message_label is None:
            # Draw the message label.
            self.message_label = Label(
                x=least,
                y=0,
                width=width,
                height=self.message_label_height
            )
        # Clear the window first.
        self.window.clear()
        # Draw the right pane.
        self.title_label.draw()
        self.layout.draw()
        self.message_label.draw()
        # Establish the width and height of the resulting blits:
        unit = floor(least / self.zoom)
        x, y = self.centre
        # Determine minimum and maximum coordinates:
        min_x = max(0, x - self.zoom)
        max_x = min_x + (self.zoom * 2)
        min_y = max(0, y - self.zoom)
        max_y = min_y + (self.zoom * 2)
        self.window.clear()
        for square in self.grid.values():
            res = square.resource
if square.x >= min_x and square.y >= min_y and square.x <= max_x \
               and square.y <= max_y and res is not None:
                res.blit(
                    (square.x * unit) + x,
                    (square.y * unit) + y,
                    width=unit,
                    height=unit
                )


if __name__ == '__main__':
    import pyglet
pyglet.options['shadow_window'] = False # Save possible problems with NVDA
    window = pyglet.window.Window(caption='HUD Test', fullscreen=True)
    hud = HUD(window)
    hud.title_label.text = 'This should te showing top right of the screen'
    hud.layout.document.insert_text(
        -1,
        'This text should be just under the top right text.'
    )
hud.message_label.text = 'This should show up on the bottom right of the \
        screen.'
    hud.draw()
    pyglet.app.run()


On 09/12/2016 00:46, Benjamin Moran wrote:
Same link error for me. If your example isn't too big, maybe you could paste it 
here directly using the code formatting tag.


--
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/pyglet-users.
For more options, visit https://groups.google.com/d/optout.

Reply via email to