Ejemplo n.º 1
0
def test_no_shared_state():
    """
    See issue #11.
    """
    grid_1 = drawing.Grid()
    grid_2 = drawing.Grid()
    i, j = 0, 0

    grid_1.set_row_height(i, 1)
    grid_2.set_row_height(i, 2)

    assert grid_1.get_requested_row_height(i) == 1
    assert grid_2.get_requested_row_height(i) == 2

    grid_1.set_col_width(j, 1)
    grid_2.set_col_width(j, 2)

    assert grid_1.get_requested_col_width(j) == 1
    assert grid_2.get_requested_col_width(j) == 2

    rect_1, rect_2 = Rect.from_size(1, 1), Rect.from_size(2, 2)
    assert rect_1 is not rect_2

    grid_1.set_min_cell_rect(i, j, rect_1)
    grid_2.set_min_cell_rect(i, j, rect_2)

    assert grid_1.get_min_cell_rect(i, j) is rect_1
    assert grid_2.get_min_cell_rect(i, j) is rect_2
Ejemplo n.º 2
0
def test_bounding_rect_too_small():
    # Just big enough, should not raise.
    drawing.make_grid(
            Rect.from_size(10, 10),
            num_rows=1,
            num_cols=1,
            default_row_height=10,
            default_col_width=10,
    )
    # Too narrow.
    with pytest.raises(UsageError):
        drawing.make_grid(
                Rect.from_size(9, 10),
                num_rows=1,
                num_cols=1,
                default_row_height=10,
                default_col_width=10,
        )
    # Too short.
    with pytest.raises(UsageError):
        drawing.make_grid(
                Rect.from_size(10, 9),
                num_rows=1,
                num_cols=1,
                default_row_height=10,
                default_col_width=10,
        )
Ejemplo n.º 3
0
def test_bounding_rect_too_small():
    # Just big enough, should not raise.
    drawing.make_grid(
        Rect.from_size(10, 10),
        num_rows=1,
        num_cols=1,
        default_row_height=10,
        default_col_width=10,
    )
    # Too narrow.
    with pytest.raises(UsageError):
        drawing.make_grid(
            Rect.from_size(9, 10),
            num_rows=1,
            num_cols=1,
            default_row_height=10,
            default_col_width=10,
        )
    # Too short.
    with pytest.raises(UsageError):
        drawing.make_grid(
            Rect.from_size(10, 9),
            num_rows=1,
            num_cols=1,
            default_row_height=10,
            default_col_width=10,
        )
Ejemplo n.º 4
0
def test_no_shared_state():
    """
    See issue #11.
    """
    grid_1 = drawing.Grid()
    grid_2 = drawing.Grid()
    i, j = 0, 0

    grid_1.set_row_height(i, 1)
    grid_2.set_row_height(i, 2)

    assert grid_1.get_requested_row_height(i) == 1
    assert grid_2.get_requested_row_height(i) == 2

    grid_1.set_col_width(j, 1)
    grid_2.set_col_width(j, 2)

    assert grid_1.get_requested_col_width(j) == 1
    assert grid_2.get_requested_col_width(j) == 2

    rect_1, rect_2 = Rect.from_size(1, 1), Rect.from_size(2, 2)
    assert rect_1 is not rect_2

    grid_1.set_min_cell_rect(i, j, rect_1)
    grid_2.set_min_cell_rect(i, j, rect_2)

    assert grid_1.get_min_cell_rect(i, j) is rect_1
    assert grid_2.get_min_cell_rect(i, j) is rect_2
Ejemplo n.º 5
0
def test_inner_padding():
    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            num_rows=1,
            num_cols=1,
            inner_padding=1,
    )
    assert cells == {
            (0,0): Rect(0, 0, 10, 10),
    }

    cells = drawing.make_grid(
            Rect.from_size(10, 11),
            num_rows=2,
            num_cols=1,
            inner_padding=1,
    )
    assert cells == {
            (0,0): Rect(0, 6, 10, 5),
            (1,0): Rect(0, 0, 10, 5),
    }

    cells = drawing.make_grid(
            Rect.from_size(11, 10),
            num_rows=1,
            num_cols=2,
            inner_padding=1
    )
    assert cells == {
            (0,0): Rect(0, 0, 5, 10),
            (0,1): Rect(6, 0, 5, 10),
    }
Ejemplo n.º 6
0
def test_padding():
    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            num_rows=1,
            num_cols=1,
            padding=1,
    )
    assert cells == {
            (0,0): Rect(1, 1, 8, 8),
    }

    cells = drawing.make_grid(
            Rect.from_size(10, 11),
            num_rows=2,
            num_cols=1,
            padding=1,
    )
    assert cells == {
            (0,0): Rect(1, 6, 8, 4),
            (1,0): Rect(1, 1, 8, 4),
    }

    cells = drawing.make_grid(
            Rect.from_size(11, 10),
            num_rows=1,
            num_cols=2,
            padding=1
    )
    assert cells == {
            (0,0): Rect(1, 1, 4, 8),
            (0,1): Rect(6, 1, 4, 8),
    }
Ejemplo n.º 7
0
    def do_draw_hands(self):
        # We're hard-coding the radii of the hands here.  Probably it would be 
        # better to make separate attributes for these, but I think that would 
        # start to detract from the clarity of the example.

        rects = {
            'hour': Rect.from_size(self.custom_hour_hand_width, self.radius/2),
            'min': Rect.from_size(self.custom_minute_hand_width, self.radius),
            'sec': Rect.from_size(self.custom_second_hand_width, self.radius),
        }

        # The clock hands all start pointing towards 12:00, and the rotations 
        # are clockwise, so 90° is 3:00, 180° is 6:00, 270° is 9:00, etc.

        now = datetime.datetime.now()
        angles = {
            'hour': 360 * now.hour / 12,
            'min': 360 * now.minute / 60,
            'sec': 360 * now.second / 60,
        }

        for k in self._hands:
            rects[k].bottom = 0
            rects[k].center_x = 0

            self._hands[k].rect = rects[k]
            self._hands[k].group.angle = angles[k]
            self._hands[k].color = self._color
            self._hands[k].show()
Ejemplo n.º 8
0
    def do_draw_hands(self):
        # We're hard-coding the radii of the hands here.  Probably it would be
        # better to make separate attributes for these, but I think that would
        # start to detract from the clarity of the example.

        rects = {
            'hour': Rect.from_size(self.custom_hour_hand_width,
                                   self.radius / 2),
            'min': Rect.from_size(self.custom_minute_hand_width, self.radius),
            'sec': Rect.from_size(self.custom_second_hand_width, self.radius),
        }

        # The clock hands all start pointing towards 12:00, and the rotations
        # are clockwise, so 90° is 3:00, 180° is 6:00, 270° is 9:00, etc.

        now = datetime.datetime.now()
        angles = {
            'hour': 360 * now.hour / 12,
            'min': 360 * now.minute / 60,
            'sec': 360 * now.second / 60,
        }

        for k in self._hands:
            rects[k].bottom = 0
            rects[k].center_x = 0

            self._hands[k].rect = rects[k]
            self._hands[k].group.angle = angles[k]
            self._hands[k].color = self._color
            self._hands[k].show()
Ejemplo n.º 9
0
def test_parent_changed():
    child, parent = Rect.null(), Rect.null()

    def change_parent(child_rect, parent_rect):
        parent_rect.left += 1

    with pytest.raises(RuntimeError, match='change_parent'):
        glooey.drawing.align(change_parent, child, parent)
Ejemplo n.º 10
0
def test_parent_changed():
    child, parent = Rect.null(), Rect.null()

    def change_parent(child_rect, parent_rect):
        parent_rect.left += 1

    with pytest.raises(RuntimeError, match='change_parent'):
        glooey.drawing.align(change_parent, child, parent)
Ejemplo n.º 11
0
def test_one_cell():
    cells = drawing.make_grid(
        Rect.from_size(10, 10),
        num_rows=1,
        num_cols=1,
    )
    assert cells == {
        (0, 0): Rect(0, 0, 10, 10),
    }
Ejemplo n.º 12
0
def test_not_enough_cols():
    with pytest.raises(UsageError):
        drawing.make_grid(
            Rect.null(),
            cells={
                (0, 1): Rect.null(),
            },
            num_rows=1,
            num_cols=1,
        )
Ejemplo n.º 13
0
def test_not_enough_cols():
    with pytest.raises(UsageError):
        drawing.make_grid(
                Rect.null(),
                cells={
                    (0,1): Rect.null(),
                },
                num_rows=1,
                num_cols=1,
        )
Ejemplo n.º 14
0
def test_negative_sizes():
    # Initially I thought this should be an error, but then I decided that it 
    # probably just works as you'd expect it to, and it might be a useful way 
    # to create an overlapping effect or to get rid of padding in certain 
    # places.

    # row_heights can be negative.
    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            num_rows=2,
            num_cols=1,
            row_heights={0: -2},
    )
    assert cells == {
            (0,0): Rect(0, 12, 10, -2),
            (1,0): Rect(0,  0, 10, 12),
    }

    # default_row_height can be negative.
    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            num_rows=2,
            num_cols=1,
            row_heights={1: 'expand'},
            default_row_height=-2,
    )
    assert cells == {
            (0,0): Rect(0, 12, 10, -2),
            (1,0): Rect(0,  0, 10, 12),
    }

    # col_widths can be negative.
    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            num_rows=1,
            num_cols=2,
            col_widths={0: -2},
    )
    assert cells == {
            (0,0): Rect(0, 0, -2, 10),
            (0,1): Rect(-2, 0, 12, 10),
    }

    # default_col_width can be negative.
    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            num_rows=1,
            num_cols=2,
            col_widths={1: 'expand'},
            default_col_width=-2,
    )
    assert cells == {
            (0,0): Rect(0, 0, -2, 10),
            (0,1): Rect(-2, 0, 12, 10),
    }
Ejemplo n.º 15
0
def test_child_outside_parent():
    child = Rect.from_square(5)
    parent = Rect.from_square(6)

    def move_1px_right(child_rect, parent_rect):
        child_rect.left += 1

    # This should be fine the first time...
    glooey.drawing.align(move_1px_right, child, parent)

    # ...but out-of-bounds the second time.
    with pytest.raises(RuntimeError, match='move_1px_right'):
        glooey.drawing.align(move_1px_right, child, parent)
Ejemplo n.º 16
0
def test_padding():
    cells = drawing.make_grid(
        Rect.from_size(10, 10),
        num_rows=1,
        num_cols=1,
        padding=1,
    )
    assert cells == {
        (0, 0): Rect(1, 1, 8, 8),
    }

    cells = drawing.make_grid(
        Rect.from_size(10, 11),
        num_rows=2,
        num_cols=1,
        padding=1,
    )
    assert cells == {
        (0, 0): Rect(1, 6, 8, 4),
        (1, 0): Rect(1, 1, 8, 4),
    }

    cells = drawing.make_grid(Rect.from_size(11, 10),
                              num_rows=1,
                              num_cols=2,
                              padding=1)
    assert cells == {
        (0, 0): Rect(1, 1, 4, 8),
        (0, 1): Rect(6, 1, 4, 8),
    }
Ejemplo n.º 17
0
def test_inner_padding():
    cells = drawing.make_grid(
        Rect.from_size(10, 10),
        num_rows=1,
        num_cols=1,
        inner_padding=1,
    )
    assert cells == {
        (0, 0): Rect(0, 0, 10, 10),
    }

    cells = drawing.make_grid(
        Rect.from_size(10, 11),
        num_rows=2,
        num_cols=1,
        inner_padding=1,
    )
    assert cells == {
        (0, 0): Rect(0, 6, 10, 5),
        (1, 0): Rect(0, 0, 10, 5),
    }

    cells = drawing.make_grid(Rect.from_size(11, 10),
                              num_rows=1,
                              num_cols=2,
                              inner_padding=1)
    assert cells == {
        (0, 0): Rect(0, 0, 5, 10),
        (0, 1): Rect(6, 0, 5, 10),
    }
Ejemplo n.º 18
0
    def set_images(self, *, color=None, center=None, top=None, bottom=None, 
            left=None, right=None, top_left=None, top_right=None, 
            bottom_left=None, bottom_right=None, vtile=None, htile=None):

        self._color = color
        self._tile_images = {
                ij: img for ij, img in {
                    (0,0): top_left,
                    (0,1): top,
                    (0,2): top_right,
                    (1,0): left,
                    (1,1): center,
                    (1,2): right,
                    (2,0): bottom_left,
                    (2,1): bottom,
                    (2,2): bottom_right,
                }.items()
                if img is not None}

        self._grid.min_cell_rects={
            ij: Rect.from_size(img.width, img.height)
            for ij, img in self._tile_images.items()}

        if vtile is not None: self._vtile = vtile
        if htile is not None: self._htile = htile

        self._update_tiles()
Ejemplo n.º 19
0
def test_child_outside_parent():
    child = Rect.from_square(4.5)
    parent = Rect.from_square(6)

    def move_1px_right(child_rect, parent_rect):
        child_rect.left += 1

    # This should be fine the first time...
    glooey.drawing.align(move_1px_right, child, parent)

    # ...and also fine the second time, because the child is allowed to exceed
    # its parent by 1 px to account for rounding errors...
    glooey.drawing.align(move_1px_right, child, parent)

    # ...but out-of-bounds the third time.
    with pytest.raises(RuntimeError, match='move_1px_right'):
        glooey.drawing.align(move_1px_right, child, parent)
Ejemplo n.º 20
0
    def __init__(self, rect=None, color='green', *,
            batch=None, group=None, usage='static', hidden=False):

        self._rect = rect or Rect.null()
        self._color = Color.from_anything(color)

        data = 'v2f/' + usage, 'c4B/' + usage
        super().__init__(batch, group, 4, GL_QUADS, data, hidden)
Ejemplo n.º 21
0
def test_one_cell():
    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            num_rows=1,
            num_cols=1,
    )
    assert cells == {
            (0,0): Rect(0, 0, 10, 10),
    }
Ejemplo n.º 22
0
    def __init__(self,
                 *,
                 bounding_rect=None,
                 min_cell_rects=None,
                 num_rows=0,
                 num_cols=0,
                 padding=None,
                 inner_padding=None,
                 outer_padding=None,
                 row_heights=None,
                 col_widths=None,
                 default_row_height='expand',
                 default_col_width='expand'):

        # Attributes that the user can set to affect the shape of the grid.
        self._bounding_rect = bounding_rect or Rect.null()
        self._min_cell_rects = min_cell_rects or {}
        self._requested_num_rows = num_rows
        self._requested_num_cols = num_cols
        self._inner_padding = first_not_none((inner_padding, padding, 0))
        self._outer_padding = first_not_none((outer_padding, padding, 0))
        self._requested_row_heights = row_heights or {}
        self._requested_col_widths = col_widths or {}
        self._default_row_height = default_row_height
        self._default_col_width = default_col_width

        # Read-only attributes that reflect the current state of the grid.
        self._num_rows = 0
        self._num_cols = 0
        self._max_cell_heights = {}
        self._max_cell_widths = {}
        self._fixed_rows = set()
        self._expandable_rows = set()
        self._fixed_cols = set()
        self._expandable_cols = set()
        self._fixed_row_heights = {}
        self._fixed_col_widths = {}
        self._min_expandable_row_heights = {}
        self._min_expandable_col_widths = {}
        self._padding_height = 0
        self._padding_width = 0
        self._min_height = 0
        self._min_width = 0
        self._row_heights = {}
        self._col_widths = {}
        self._width = 0
        self._height = 0
        self._row_tops = {}
        self._col_lefts = {}
        self._cell_rects = {}

        # Attributes that manage the cache.
        self._is_shape_stale = True
        self._is_claim_stale = True
        self._are_cells_stale = True
Ejemplo n.º 23
0
def test_setters():
    grid = drawing.Grid()
    assert grid.make_claim() == (0, 0)

    grid.num_rows = 1
    grid.default_row_height = 10
    assert grid.make_claim() == (0, 10)

    grid.num_cols = 1
    grid.default_col_width = 10
    assert grid.make_claim() == (10, 10)

    grid.padding = 1
    assert grid.make_claim() == (12, 12)

    grid.num_rows = 2
    grid.num_cols = 2
    assert grid.make_claim() == (23, 23)

    grid.set_min_cell_rects({(0, 0): Rect.from_size(20, 20)})
    assert grid.make_claim() == (33, 33)

    grid.del_min_cell_rects()
    assert grid.make_claim() == (23, 23)

    grid.set_min_cell_rect(0, 0, Rect.from_size(20, 20))
    assert grid.make_claim() == (33, 33)

    grid.del_min_cell_rect(0, 0)
    assert grid.make_claim() == (23, 23)

    grid.set_row_height(0, 20)
    assert grid.make_claim() == (23, 33)

    grid.del_row_height(0)
    assert grid.make_claim() == (23, 23)

    grid.set_col_width(0, 20)
    assert grid.make_claim() == (33, 23)

    grid.del_col_width(0)
    assert grid.make_claim() == (23, 23)
Ejemplo n.º 24
0
def test_setters():
    grid = drawing.Grid()
    assert grid.make_claim() == (0, 0)

    grid.num_rows = 1
    grid.default_row_height = 10
    assert grid.make_claim() == (0, 10)

    grid.num_cols = 1
    grid.default_col_width = 10
    assert grid.make_claim() == (10, 10)

    grid.padding = 1
    assert grid.make_claim() == (12, 12)

    grid.num_rows = 2
    grid.num_cols = 2
    assert grid.make_claim() == (23, 23)

    grid.set_min_cell_rects({(0,0): Rect.from_size(20, 20)})
    assert grid.make_claim() == (33, 33)

    grid.del_min_cell_rects()
    assert grid.make_claim() == (23, 23)

    grid.set_min_cell_rect(0, 0, Rect.from_size(20, 20))
    assert grid.make_claim() == (33, 33)

    grid.del_min_cell_rect(0, 0)
    assert grid.make_claim() == (23, 23)

    grid.set_row_height(0, 20)
    assert grid.make_claim() == (23, 33)

    grid.del_row_height(0)
    assert grid.make_claim() == (23, 23)

    grid.set_col_width(0, 20)
    assert grid.make_claim() == (33, 23)

    grid.del_col_width(0)
    assert grid.make_claim() == (23, 23)
Ejemplo n.º 25
0
def test_no_expandable_cells():
    # If the grid can't fill all the space made available to it, it will pack 
    # against the top-left corner.  I chose this corner arbitrarily (although 
    # other corners would've been slightly harder to implement).  I decided 
    # against adding the ability to control how the grid fits in its bounding 
    # box for two reasons.  First, you can get the same effect by simply adding 
    # expandable rows or columns.  Second, you can just change the bounding 
    # box.  Still, it's possible I'll change my mind about this behavior.

    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            num_rows=1,
            num_cols=1,
            row_heights={0: 2},
            col_widths={0: 2},
    )
    assert cells == {
            (0,0): Rect(0, 8, 2, 2),
    }

    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            num_rows=1,
            num_cols=1,
            default_row_height=2,
            default_col_width=2,
    )
    assert cells == {
            (0,0): Rect(0, 8, 2, 2),
    }

    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            cells={
                (0,0): Rect.from_size(2, 2),
            },
            default_row_height=0,
            default_col_width=0,
    )
    assert cells == {
            (0,0): Rect(0, 8, 2, 2),
    }
Ejemplo n.º 26
0
def test_two_cells():
    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            num_rows=2,
            num_cols=1,
    )
    assert cells == {
            (0,0): Rect(0, 5, 10, 5),
            (1,0): Rect(0, 0, 10, 5),
    }

    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            num_rows=1,
            num_cols=2,
    )
    assert cells == {
            (0,0): Rect(0, 0, 5, 10),
            (0,1): Rect(5, 0, 5, 10),
    }
Ejemplo n.º 27
0
def test_make_claim():
    grid = drawing.Grid()
    cells = {
        (0,0): Rect.from_size(1, 2),
        (1,1): Rect.from_size(3, 4),
    }
    assert grid.make_claim(cells) == (6, 8)
    assert grid.make_claim() == (6, 8)
    assert grid.min_width == 6
    assert grid.min_height == 8
    assert grid.min_bounding_rect == Rect(0, 0, 6, 8)

    grid.default_row_height = 0
    grid.default_col_width = 0

    assert grid.make_claim(cells) == (4, 6)
    assert grid.make_claim() == (4, 6)
    assert grid.min_width == 4
    assert grid.min_height == 6
    assert grid.min_bounding_rect == Rect(0, 0, 4, 6)
Ejemplo n.º 28
0
def test_make_claim():
    grid = drawing.Grid()
    cells = {
        (0, 0): Rect.from_size(1, 2),
        (1, 1): Rect.from_size(3, 4),
    }
    assert grid.make_claim(cells) == (6, 8)
    assert grid.make_claim() == (6, 8)
    assert grid.min_width == 6
    assert grid.min_height == 8
    assert grid.min_bounding_rect == Rect(0, 0, 6, 8)

    grid.default_row_height = 0
    grid.default_col_width = 0

    assert grid.make_claim(cells) == (4, 6)
    assert grid.make_claim() == (4, 6)
    assert grid.min_width == 4
    assert grid.min_height == 6
    assert grid.min_bounding_rect == Rect(0, 0, 4, 6)
Ejemplo n.º 29
0
def test_real_examples():
    cells = drawing.make_grid(
        Rect.from_size(640, 480),
        cells={
            (0, 0): Rect(0, 0, 10, 10),
            (0, 1): Rect(0, 0, 310, 10),
            (0, 2): Rect(0, 0, 10, 10),
            (0, 3): Rect(0, 0, 10, 10),
        },
        col_widths={1: 0},
    )
    assert cells == {
        (0, 0): Rect(0, 0, 110, 480),
        (0, 1): Rect(110, 0, 310, 480),
        (0, 2): Rect(420, 0, 110, 480),
        (0, 3): Rect(530, 0, 110, 480),
    }
Ejemplo n.º 30
0
    def set_appearance(self, *, color=None, outline=None, image=None, 
            center=None, top=None, bottom=None, left=None, right=None, 
            top_left=None, top_right=None, bottom_left=None, bottom_right=None, 
            vtile='auto', htile='auto'):

        if image and center:
            raise UsageError("""\
specifying both 'image' and 'center' is ambiguous.

Both of these options specify an image that should go in the middle of the 
frame.  The only difference is that, if 'htile' or 'vtile' are set to 'auto' 
(the default value), 'center' enables tiling and 'image' doesn't.""")

        # Decide whether the background should tile in either dimension.

        auto_vtile = False
        auto_htile = False

        if top or bottom:
            auto_vtile = True
        if left or right:
            auto_htile = True
        if center and not (top or left or bottom or right):
            auto_vtile = True
            auto_htile = True

        self._vtile = auto_vtile if vtile == 'auto' else vtile
        self._htile = auto_htile if htile == 'auto' else htile

        # Store the images in a grid-like data structure.

        self._color = color
        self._outline = outline
        self._tile_images = {
                ij: img for ij, img in {
                    (0,0): top_left,
                    (0,1): top,
                    (0,2): top_right,
                    (1,0): left,
                    (1,1): center or image,
                    (1,2): right,
                    (2,0): bottom_left,
                    (2,1): bottom,
                    (2,2): bottom_right,
                }.items()
                if img is not None}

        self._grid.min_cell_rects = {
            ij: Rect.from_size(img.width, img.height)
            for ij, img in self._tile_images.items()}

        self._update_tiles()
Ejemplo n.º 31
0
        def do_resize_children(self):
            grip_width, grip_height = self.grip.claimed_size

            if self.bar.scale_grip:
                scaled_width, scaled_height = self.bar._get_scaled_grip_size()
                grip_width = clamp(scaled_width, grip_width, self.width)
                grip_height = clamp(scaled_height, grip_height, self.height)

            # Copied from `Mover.do_resize_children()`; refer there for 
            # details.  Maybe should be more DRY...
            child_rect = Rect.from_size(grip_width, grip_height)
            self.child._resize(child_rect)
            self._keep_child_in_rect()
Ejemplo n.º 32
0
def test_request_col_width():
    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            num_rows=1,
            num_cols=2,
            col_widths={0: 2},
    )
    assert cells == {
            (0,0): Rect(0, 0, 2, 10),
            (0,1): Rect(2, 0, 8, 10),
    }

    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            num_rows=1,
            num_cols=2,
            col_widths={1: 2},
    )
    assert cells == {
            (0,0): Rect(0, 0, 8, 10),
            (0,1): Rect(8, 0, 2, 10),
    }
Ejemplo n.º 33
0
def test_request_row_height():
    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            num_rows=2,
            num_cols=1,
            row_heights={0: 2},
    )
    assert cells == {
            (0,0): Rect(0, 8, 10, 2),
            (1,0): Rect(0, 0, 10, 8),
    }

    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            num_rows=2,
            num_cols=1,
            row_heights={1: 2},
    )
    assert cells == {
            (0,0): Rect(0, 2, 10, 8),
            (1,0): Rect(0, 0, 10, 2),
    }
Ejemplo n.º 34
0
    def __init__(self,
                 rect=None,
                 color='green',
                 *,
                 batch=None,
                 group=None,
                 usage='static',
                 hidden=False):

        self._rect = rect or Rect.null()
        self._color = Color.from_anything(color)

        data = 'v2f/' + usage, 'c4B/' + usage
        super().__init__(batch, group, 4, GL_QUADS, data, hidden)
Ejemplo n.º 35
0
def test_make_cells():
    grid = drawing.Grid()
    grid.num_rows = 2
    grid.num_cols = 2
    
    expected_results = [(
        Rect.from_size(10, 10), {
            (0, 0): Rect(0, 5, 5, 5),
            (0, 1): Rect(5, 5, 5, 5),
            (1, 0): Rect(0, 0, 5, 5),
            (1, 1): Rect(5, 0, 5, 5),
        }),(
        Rect.from_size(20, 20), {
            (0, 0): Rect(0,  10, 10, 10),
            (0, 1): Rect(10, 10, 10, 10),
            (1, 0): Rect(0,   0, 10, 10),
            (1, 1): Rect(10,  0, 10, 10),
        }),
    ]
    for bbox, cells in expected_results:
        assert grid.make_cells(bbox) == cells
        assert grid.make_cells() == cells
        assert grid.cell_rects == cells
        assert grid.bounding_rect == bbox
Ejemplo n.º 36
0
def test_min_cell_rect_vs_row_height():
    # If there is a cell larger than the specified row height, make the row big 
    # enough to fit that cell.
    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            cells={
                (0,0): Rect.from_size(2,2),
            },
            num_rows=2,
            num_cols=1,
            row_heights={0: 0},
    )
    assert cells == {
            (0,0): Rect(0, 8, 10, 2),
            (1,0): Rect(0, 0, 10, 8),
    }

    # If the specified row height is larger than any of the cells in the row, 
    # make the row the specified height.
    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            cells={
                (0,0): Rect.from_size(2,2),
            },
            num_rows=2,
            num_cols=1,
            row_heights={0: 4},
    )
    assert cells == {
            (0,0): Rect(0, 6, 10, 4),
            (1,0): Rect(0, 0, 10, 6),
    }

    # If there are multiple cells in the row, the row should be big enough for 
    # all of them.
    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            cells={
                (0,0): Rect.from_size(2,2),
                (0,1): Rect.from_size(4,4),
            },
            num_rows=2,
            num_cols=2,
            row_heights={0: 0},
    )
    assert cells == {
            (0,0): Rect(0, 6, 5, 4),
            (0,1): Rect(5, 6, 5, 4),
            (1,0): Rect(0, 0, 5, 6),
            (1,1): Rect(5, 0, 5, 6),
    }
Ejemplo n.º 37
0
def test_make_cells():
    grid = drawing.Grid()
    grid.num_rows = 2
    grid.num_cols = 2

    expected_results = [
        (Rect.from_size(10, 10), {
            (0, 0): Rect(0, 5, 5, 5),
            (0, 1): Rect(5, 5, 5, 5),
            (1, 0): Rect(0, 0, 5, 5),
            (1, 1): Rect(5, 0, 5, 5),
        }),
        (Rect.from_size(20, 20), {
            (0, 0): Rect(0, 10, 10, 10),
            (0, 1): Rect(10, 10, 10, 10),
            (1, 0): Rect(0, 0, 10, 10),
            (1, 1): Rect(10, 0, 10, 10),
        }),
    ]
    for bbox, cells in expected_results:
        assert grid.make_cells(bbox) == cells
        assert grid.make_cells() == cells
        assert grid.cell_rects == cells
        assert grid.bounding_rect == bbox
Ejemplo n.º 38
0
        def do_resize_children(self):
            grip_width, grip_height = self.grip.claimed_size

            # Don't try to calculate a scaled grip size if the scroll pane
            # doesn't have a child yet.  See #36.
            if self.bar.scale_grip and self.pane.child:
                scaled_width, scaled_height = self.bar._get_scaled_grip_size()
                grip_width = clamp(scaled_width, grip_width, self.width)
                grip_height = clamp(scaled_height, grip_height, self.height)

            # Copied from `Mover.do_resize_children()`; refer there for
            # details.  Maybe should be more DRY...
            child_rect = Rect.from_size(grip_width, grip_height)
            self.child._resize(child_rect)
            self._keep_child_in_rect()
Ejemplo n.º 39
0
def test_default_col_width():
    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            num_rows=1,
            num_cols=2,
            col_widths={0: 'expand'},
            default_col_width=2,
    )
    assert cells == {
            (0,0): Rect(0, 0, 8, 10),
            (0,1): Rect(8, 0, 2, 10),
    }

    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            num_rows=1,
            num_cols=2,
            col_widths={1: 'expand'},
            default_col_width=2,
    )
    assert cells == {
            (0,0): Rect(0, 0, 2, 10),
            (0,1): Rect(2, 0, 8, 10),
    }
Ejemplo n.º 40
0
def test_default_row_height():
    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            num_rows=2,
            num_cols=1,
            row_heights={0: 'expand'},
            default_row_height=2,
    )
    assert cells == {
            (0,0): Rect(0, 2, 10, 8),
            (1,0): Rect(0, 0, 10, 2),
    }

    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            num_rows=2,
            num_cols=1,
            row_heights={1: 'expand'},
            default_row_height=2,
    )
    assert cells == {
            (0,0): Rect(0, 8, 10, 2),
            (1,0): Rect(0, 0, 10, 8),
    }
Ejemplo n.º 41
0
    def __init__(self):
        super().__init__()

        self.field = Rect.from_size(*self.field_size)
        self.cards = {} # do not remove items
        
        self.starter_card = None
        self.pegging_stack = []

        self.players = []
        self.dealer = None
        self.pone = None
        self.active_player = None
        self.phase = None

        self.phase = None
Ejemplo n.º 42
0
def test_real_examples():
    cells = drawing.make_grid(
            Rect.from_size(640, 480),
            cells={
                (0,0): Rect(0, 0,  10, 10),
                (0,1): Rect(0, 0, 310, 10),
                (0,2): Rect(0, 0,  10, 10),
                (0,3): Rect(0, 0,  10, 10),
            },
            col_widths={1: 0},
    )
    assert cells == {
            (0,0): Rect(  0, 0, 110, 480),
            (0,1): Rect(110, 0, 310, 480),
            (0,2): Rect(420, 0, 110, 480),
            (0,3): Rect(530, 0, 110, 480),
    }
Ejemplo n.º 43
0
    def __init__(self, *, bounding_rect=None, min_cell_rects=None,
            num_rows=0, num_cols=0, padding=None, inner_padding=None, 
            outer_padding=None, row_heights=None, col_widths=None,
            default_row_height='expand', default_col_width='expand'):

        # Attributes that the user can set to affect the shape of the grid.  
        self._bounding_rect = bounding_rect or Rect.null()
        self._min_cell_rects = min_cell_rects or {}
        self._requested_num_rows = num_rows
        self._requested_num_cols = num_cols
        self._inner_padding = first_not_none((inner_padding, padding, 0))
        self._outer_padding = first_not_none((outer_padding, padding, 0))
        self._requested_row_heights = row_heights or {}
        self._requested_col_widths = col_widths or {}
        self._default_row_height = default_row_height
        self._default_col_width = default_col_width

        # Read-only attributes that reflect the current state of the grid.
        self._num_rows = 0
        self._num_cols = 0
        self._max_cell_heights = {}
        self._max_cell_widths = {}
        self._fixed_rows = set()
        self._expandable_rows = set()
        self._fixed_cols = set()
        self._expandable_cols = set()
        self._fixed_row_heights = {}
        self._fixed_col_widths = {}
        self._min_expandable_row_heights = {}
        self._min_expandable_col_widths = {}
        self._padding_height = 0
        self._padding_width = 0
        self._min_height = 0
        self._min_width = 0
        self._row_heights = {}
        self._col_widths = {}
        self._width = 0
        self._height = 0
        self._row_tops = {}
        self._col_lefts = {}
        self._cell_rects = {}

        # Attributes that manage the cache.
        self._is_shape_stale = True
        self._is_claim_stale = True
        self._are_cells_stale = True
Ejemplo n.º 44
0
    def __init__(self):
        super().__init__()

        # Initialize the game objects.

        self.field = Rect.from_size(*self.field_size)
        self.players = []
        self.bullets = []
        self.targets = []
        self.final_target = None
        self.obstacles = []
        self.winner = None

        # Initialize the 2D physics simulator.

        self.space = pymunk.Space()
        self.space.gravity = 0, 0
        self.make_boundary()
Ejemplo n.º 45
0
    def _find_cell_rects(self):
        self._cell_rects = {}
        top_cursor = self._bounding_rect.top

        for i in range(self._num_rows):
            top_cursor -= self._padding
            row_height = self._row_heights[i]
            left_cursor = self._bounding_rect.left

            for j in range(self._num_cols):
                left_cursor += self._padding
                col_width = self._col_widths[j]

                self._cell_rects[i,j] = Rect.from_size(col_width, row_height)
                self._cell_rects[i,j].top_left = left_cursor, top_cursor

                left_cursor += col_width
            top_cursor -= row_height
Ejemplo n.º 46
0
def test_min_cell_rect_vs_col_width():
    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            cells={
                (0,0): Rect.from_size(2,2),
            },
            num_rows=1,
            num_cols=2,
            col_widths={0: 0},
    )
    assert cells == {
            (0,0): Rect(0, 0, 2, 10),
            (0,1): Rect(2, 0, 8, 10),
    }

    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            cells={
                (0,0): Rect.from_size(2,2),
            },
            num_rows=1,
            num_cols=2,
            col_widths={0: 4},
    )
    assert cells == {
            (0,0): Rect(0, 0, 4, 10),
            (0,1): Rect(4, 0, 6, 10),
    }

    # If there are multiple cells in the column, the column should be big 
    # enough for all of them.
    cells = drawing.make_grid(
            Rect.from_size(10, 10),
            cells={
                (0,0): Rect.from_size(2,2),
                (1,0): Rect.from_size(4,4),
            },
            num_rows=2,
            num_cols=2,
            col_widths={0: 0},
    )
    assert cells == {
            (0,0): Rect(0, 5, 4, 5),
            (1,0): Rect(0, 0, 4, 5),
            (0,1): Rect(4, 5, 6, 5),
            (1,1): Rect(4, 0, 6, 5),
    }
Ejemplo n.º 47
0
def test_no_expandable_cells():
    # If the grid can't fill all the space made available to it, it will pack
    # against the top-left corner.  I chose this corner arbitrarily (although
    # other corners would've been slightly harder to implement).  I decided
    # against adding the ability to control how the grid fits in its bounding
    # box for two reasons.  First, you can get the same effect by simply adding
    # expandable rows or columns.  Second, you can just change the bounding
    # box.  Still, it's possible I'll change my mind about this behavior.

    cells = drawing.make_grid(
        Rect.from_size(10, 10),
        num_rows=1,
        num_cols=1,
        row_heights={0: 2},
        col_widths={0: 2},
    )
    assert cells == {
        (0, 0): Rect(0, 8, 2, 2),
    }

    cells = drawing.make_grid(
        Rect.from_size(10, 10),
        num_rows=1,
        num_cols=1,
        default_row_height=2,
        default_col_width=2,
    )
    assert cells == {
        (0, 0): Rect(0, 8, 2, 2),
    }

    cells = drawing.make_grid(
        Rect.from_size(10, 10),
        cells={
            (0, 0): Rect.from_size(2, 2),
        },
        default_row_height=0,
        default_col_width=0,
    )
    assert cells == {
        (0, 0): Rect(0, 8, 2, 2),
    }
Ejemplo n.º 48
0
    def _find_cell_rects(self):
        self._row_tops = {}
        self._col_lefts = {}
        self._cell_rects = {}

        top_cursor = self._bounding_rect.top

        for i in range(self._num_rows):
            top_cursor -= self._get_row_padding(i)
            left_cursor = self._bounding_rect.left
            row_height = self._row_heights[i]
            self._row_tops[i] = top_cursor

            for j in range(self._num_cols):
                left_cursor += self._get_col_padding(j)
                col_width = self._col_widths[j]

                self._cell_rects[i, j] = Rect.from_size(col_width, row_height)
                self._cell_rects[i, j].top_left = left_cursor, top_cursor
                self._col_lefts[j] = left_cursor

                left_cursor += col_width
            top_cursor -= row_height
Ejemplo n.º 49
0
    def do_resize_children(self):
        # Consult the ``expand_horz`` and ``expand_vert`` member variables to
        # decide how much space to give the child.  If expansion is enabled,
        # the child can occupy the whole mover depending on its alignment.  The
        # advantage of this is that the child can control its initial position
        # using alignment.  The downside is that widgets with the default
        # "fill" alignment can't move, which is a gotcha.  If expansion is
        # disabled, the child is made as small as possible.
        if self.expand_horz:
            child_width = self.rect.width
        else:
            child_width = self.child.claimed_width

        if self.expand_vert:
            child_height = self.rect.height
        else:
            child_height = self.child.claimed_height

        # Put the bottom left corner of the child's rectangle at the origin.
        # This simplifies the offset calculations, relative to having the
        # child's rectangle where the parent's is.
        child_rect = Rect.from_size(child_width, child_height)
        self.child._resize(child_rect)
        self._keep_child_in_rect()
Ejemplo n.º 50
0
def test_find_cell_under_mouse():
    grid = drawing.Grid()
    grid.num_rows = 2
    grid.num_cols = 2
    grid.padding = 1
    grid.make_cells(Rect.from_size(5, 5))

    expected_results = {
        (0, 0): None,
        (0, 1): None,
        (0, 2): None,
        (0, 3): None,
        (0, 4): None,
        (1, 0): None,
        (1, 1): (1, 0),
        (1, 2): (1, 0),
        (1, 3): (0, 0),
        (1, 4): (0, 0),
        (2, 0): None,
        (2, 1): (1, 0),
        (2, 2): (1, 0),
        (2, 3): (0, 0),
        (2, 4): (0, 0),
        (3, 0): None,
        (3, 1): (1, 1),
        (3, 2): (1, 1),
        (3, 3): (0, 1),
        (3, 4): (0, 1),
        (4, 0): None,
        (4, 1): (1, 1),
        (4, 2): (1, 1),
        (4, 3): (0, 1),
        (4, 4): (0, 1),
    }
    for mouse, cell in expected_results.items():
        assert grid.find_cell_under_mouse(*mouse) == cell
Ejemplo n.º 51
0
def test_two_cells():
    cells = drawing.make_grid(
        Rect.from_size(10, 10),
        num_rows=2,
        num_cols=1,
    )
    assert cells == {
        (0, 0): Rect(0, 5, 10, 5),
        (1, 0): Rect(0, 0, 10, 5),
    }

    cells = drawing.make_grid(
        Rect.from_size(10, 10),
        num_rows=1,
        num_cols=2,
    )
    assert cells == {
        (0, 0): Rect(0, 0, 5, 10),
        (0, 1): Rect(5, 0, 5, 10),
    }
Ejemplo n.º 52
0
def test_request_row_height():
    cells = drawing.make_grid(
        Rect.from_size(10, 10),
        num_rows=2,
        num_cols=1,
        row_heights={0: 2},
    )
    assert cells == {
        (0, 0): Rect(0, 8, 10, 2),
        (1, 0): Rect(0, 0, 10, 8),
    }

    cells = drawing.make_grid(
        Rect.from_size(10, 10),
        num_rows=2,
        num_cols=1,
        row_heights={1: 2},
    )
    assert cells == {
        (0, 0): Rect(0, 2, 10, 8),
        (1, 0): Rect(0, 0, 10, 2),
    }
Ejemplo n.º 53
0
def test_request_col_width():
    cells = drawing.make_grid(
        Rect.from_size(10, 10),
        num_rows=1,
        num_cols=2,
        col_widths={0: 2},
    )
    assert cells == {
        (0, 0): Rect(0, 0, 2, 10),
        (0, 1): Rect(2, 0, 8, 10),
    }

    cells = drawing.make_grid(
        Rect.from_size(10, 10),
        num_rows=1,
        num_cols=2,
        col_widths={1: 2},
    )
    assert cells == {
        (0, 0): Rect(0, 0, 8, 10),
        (0, 1): Rect(8, 0, 2, 10),
    }
Ejemplo n.º 54
0
def test_default_row_height():
    cells = drawing.make_grid(
        Rect.from_size(10, 10),
        num_rows=2,
        num_cols=1,
        row_heights={0: 'expand'},
        default_row_height=2,
    )
    assert cells == {
        (0, 0): Rect(0, 2, 10, 8),
        (1, 0): Rect(0, 0, 10, 2),
    }

    cells = drawing.make_grid(
        Rect.from_size(10, 10),
        num_rows=2,
        num_cols=1,
        row_heights={1: 'expand'},
        default_row_height=2,
    )
    assert cells == {
        (0, 0): Rect(0, 8, 10, 2),
        (1, 0): Rect(0, 0, 10, 8),
    }
Ejemplo n.º 55
0
def test_default_col_width():
    cells = drawing.make_grid(
        Rect.from_size(10, 10),
        num_rows=1,
        num_cols=2,
        col_widths={0: 'expand'},
        default_col_width=2,
    )
    assert cells == {
        (0, 0): Rect(0, 0, 8, 10),
        (0, 1): Rect(8, 0, 2, 10),
    }

    cells = drawing.make_grid(
        Rect.from_size(10, 10),
        num_rows=1,
        num_cols=2,
        col_widths={1: 'expand'},
        default_col_width=2,
    )
    assert cells == {
        (0, 0): Rect(0, 0, 2, 10),
        (0, 1): Rect(2, 0, 8, 10),
    }
Ejemplo n.º 56
0
    def set_appearance(self,
                       *,
                       color=None,
                       outline=None,
                       image=None,
                       center=None,
                       top=None,
                       bottom=None,
                       left=None,
                       right=None,
                       top_left=None,
                       top_right=None,
                       bottom_left=None,
                       bottom_right=None,
                       vtile='auto',
                       htile='auto'):

        if image and center:
            raise UsageError("""\
specifying both 'image' and 'center' is ambiguous.

Both of these options specify an image that should go in the middle of the 
frame.  The only difference is that, if 'htile' or 'vtile' are set to 'auto' 
(the default value), 'center' enables tiling and 'image' doesn't.""")

        # Decide whether the background should tile in either dimension.

        auto_vtile = False
        auto_htile = False

        if top or bottom:
            auto_vtile = True
        if left or right:
            auto_htile = True
        if center and not (top or left or bottom or right):
            auto_vtile = True
            auto_htile = True

        self._vtile = auto_vtile if vtile == 'auto' else vtile
        self._htile = auto_htile if htile == 'auto' else htile

        # Store the images in a grid-like data structure.

        self._color = color
        self._outline = outline
        self._tile_images = {
            ij: img
            for ij, img in {
                (0, 0): top_left,
                (0, 1): top,
                (0, 2): top_right,
                (1, 0): left,
                (1, 1): center or image,
                (1, 2): right,
                (2, 0): bottom_left,
                (2, 1): bottom,
                (2, 2): bottom_right,
            }.items() if img is not None
        }

        self._grid.min_cell_rects = {
            ij: Rect.from_size(img.width, img.height)
            for ij, img in self._tile_images.items()
        }

        self._update_tiles()
Ejemplo n.º 57
0
#!/usr/bin/env python3

import pyglet
import glooey
import run_demos
from vecrec import Vector, Rect

window = pyglet.window.Window()
batch = pyglet.graphics.Batch()

rects = { #
        'big': Rect.from_size(64*8, 64*6),
        'small': Rect.from_size(64*4, 64*3),
}
images = { #
        'green': pyglet.image.load('assets/64x64/green.png'),
        'orange': pyglet.image.load('assets/64x64/orange.png'),
}

window_rect = Rect.from_pyglet_window(window)
for rect in rects.values():
    rect.center = window_rect.center

artist = glooey.drawing.Tile(rects['big'],
                             images['green'],
                             vtile=True,
                             htile=True,
                             batch=batch)


@run_demos.on_space(window, batch)
Ejemplo n.º 58
0
#!/usr/bin/env python3

import pyglet
import glooey
import run_demos
from vecrec import Rect
from pyglet.image import load

window = pyglet.window.Window()
batch = pyglet.graphics.Batch()
rect = Rect.from_size(64 * 8, 64 * 6)
rect.center = Rect.from_pyglet_window(window).center
bg = glooey.drawing.Background(rect=rect, batch=batch)


@run_demos.on_space(window, batch)
def test_background():
    # Make sure colors can be set, updated, and removed.
    bg.set_appearance(color='green')
    yield "Show a solid green background."

    bg.set_appearance(color='orange')
    yield "Change to a solid orange background."

    # Make sure outlines can be set, updated, and removed.
    bg.set_appearance(outline='green')
    yield "Show a solid green outline."

    bg.set_appearance(outline='orange')
    yield "Change to a solid orange outline."
Ejemplo n.º 59
0
#!/usr/bin/env python3

import pyglet
import glooey
import run_demos
from vecrec import Vector, Rect

window = pyglet.window.Window()
batch = pyglet.graphics.Batch()

full = Rect.from_pyglet_window(window)
left = Rect(full.left, full.bottom, full.width/2, full.height)
right = Rect(full.left, full.bottom, full.width/2, full.height)
right.left = left.right
left.shrink(50)
right.shrink(50)


@run_demos.on_space(window, batch)
def test_outline():
    a = glooey.drawing.Outline(left, batch=batch)
    b = glooey.drawing.Outline(right, batch=batch)
    yield "Show two unconnected outlines."
    a.hide()
    b.hide()

    c = glooey.drawing.Outline(full, batch=batch)
    yield "Put an outline just inside the window."
    c.hide()

@window.event