Debian Codegraph @

doc/programming_guide/shapes.rst

raw
1 Drawing Shapes
2 ==============
3
4 .. _guide_shapes:
5
6
7 The :py:mod:`~pyglet.shapes` module is an easy to use option for creating
8 and manipulating colored shapes, such as rectangles, circles, and
9 lines. Shapes can be resized, positioned, and rotated where applicable,
10 and their color and opacity can be changed. All shapes are implemented
11 using OpenGL primitives, so they can be drawn efficiently with :ref:`guide_batched-rendering`.
12 In the following examples `Batch` will be omitted for brevity, but in
13 general you always want to use Batched rendering for performance.
14
15 For drawing more complex shapes, see the :ref:`guide_graphics` module.
16
17
18 Creating a Shape
19 ----------------
20
21 Various shapes can be constructed with a specific position, size, and color::
22
23 circle = shapes.Circle(x=100, y=150, radius=100, color=(50, 225, 30))
24 square = shapes.Rectangle(x=200, y=200, width=200, height=200, color=(55, 55, 255))
25
26 You can also change the color, or set the opacity after creation. The opacity
27 can be set on a scale of 0-255, for various levels of transparency::
28
29 circle.opacity = 120
30
31 The size of Shapes can also be adjusted after creation::
32
33 square.width = 200
34 circle.radius = 99
35
36
37 Anchor Points
38 ^^^^^^^^^^^^^
39
40 Similar to images in pyglet, the "anchor point" of a Shape can be set.
41 This relates to the center of the shape on the x and y axis. For Circles,
42 the default anchor point is the center of the circle. For Rectangles,
43 it is the bottom left corner. Depending on how you need to position your
44 Shapes, this can be changed. For Rectangles this is especially useful if
45 you will rotate it, since Shapes will rotate around the anchor point. In
46 this example, a Rectangle is created, and the anchor point is then set to
47 the center::
48
49 rectangle = shapes.Rectangle(x=400, y=400, width=100, height=50)
50 rectangle.anchor_x = 50
51 rectangle.anchor_y = 25
52 # or, set at the same time:
53 rectangle.anchor_position = 50, 25
54
55 # The rectangle is then rotated around its anchor point:
56 rectangle.rotation = 45
57
58 If you plan to create a large number of shapes, you can optionally set the
59 default anchor points::
60
61 shapes.Rectangle._anchor_x = 100
62 shapes.Rectangle._anchor_y = 50
63
64 Advanced Operation
65 ------------------
66
67 .. _guide_shapes-bounding:
68
69 Shape Bounding Checks
70 ^^^^^^^^^^^^^^^^^^^^^
71
72 Some shapes can check whether an instance contains a 2D point via Python's ``in`` operator.
73
74 The point can be any :py:class:`tuple` of two numbers:
75
76 .. code-block:: python
77
78 from pyglet.shapes import Circle
79
80 # Create a circle
81 circle = shapes.Circle(x=100, y=100, radius=50)
82
83 # Turn the circle red if (200, 200) is inside it
84 if (200, 200) in circle:
85 circle.color = (255, 0, 0)
86
87
88 Bounding Checks Are In World Space
89 """"""""""""""""""""""""""""""""""
90
91 If you move or alter your viewport, you must account for this when
92 checking coordinates from :ref:`mouse events <guide_mouse_events>`.
93 You may find :py:class:`~pyglet.math.Vec2` helpful for this since
94 it is a subclass of :py:class:`tuple`.
95
96 .. _guide_shapes-bounding-supported:
97
98 Which Shapes Support Bounding Checks?
99 """""""""""""""""""""""""""""""""""""
100
101 Instances of the following shape classes support the ``in`` operator:
102
103 - :py:class:`~pyglet.shapes.Circle`
104 - :py:class:`~pyglet.shapes.Ellipse`
105 - :py:class:`~pyglet.shapes.Sector`
106 - :py:class:`~pyglet.shapes.Line`
107 - :py:class:`~pyglet.shapes.Rectangle`
108 - :py:class:`~pyglet.shapes.BorderedRectangle`
109 - :py:class:`~pyglet.shapes.Triangle`
110 - :py:class:`~pyglet.shapes.Polygon`
111 - :py:class:`~pyglet.shapes.Star`
112 (acts like a :py:class:`~pyglet.shapes.Circle`
113 with a radius of ``(outer_radius + inner_radius) / 2``).
114
115
116 .. _guide_shapes-bounding-details:
117
118 How Does It Work?
119 """""""""""""""""
120
121 Each of these classes implements a :py:meth:`~object.__contains__` method which
122 accounts for an instance's:
123
124 * :py:attr:`~pyglet.shapes.ShapeBase.rotation` angle
125 * :py:attr:`~pyglet.shapes.ShapeBase.anchor_position`
126
127
128 .. _guide_shapes-custom:
129
130 Custom Shapes
131 ^^^^^^^^^^^^^
132
133 .. tip:: For complex features and performance, please see :ref:`guide_graphics`.
134
135 You can import and subclass :py:class:`~pyglet.shapes.ShapeBase` to create
136 custom shapes:
137
138 .. code-block:: python
139
140 from pyglet.shapes import ShapeBase
141
142
143 class MyCustomShape(ShapeBase):
144 """A custom shape class. Implementation details omitted."""
145 ...
146
147
148 This is the base class for shapes in the :py:mod:`~pyglet.shapes` module,
149 which means you can use the other shapes in the module as reference. Please
150 note that this class has the following caveats:
151
152 * The ``in`` operator requires you to add a :py:meth:`~object.__contains__` method which:
153
154 * accounts for the following values on an instance:
155
156 * :py:attr:`~pyglet.shapes.ShapeBase.rotation` angle
157 * :py:attr:`~pyglet.shapes.ShapeBase.anchor_position`
158
159 * returns a :py:class:`bool`
160
161 * It will conflict with other metaclass-based helpers due to using :py:class:`abc.ABC`
162