1
1
import math
2
2
from collections .abc import Callable , Sequence
3
+ from contextlib import contextmanager
3
4
from typing import Optional
4
5
5
6
from rlbot import flat
@@ -51,6 +52,8 @@ class Renderer:
51
52
_group_id : Optional [int ] = None
52
53
_current_renders : list [flat .RenderMessage ] = []
53
54
55
+ _default_color = white
56
+
54
57
_screen_width_factor = 1.0
55
58
_screen_height_factor = 1.0
56
59
@@ -72,6 +75,12 @@ def set_resolution(self, screen_width: float, screen_height: float):
72
75
self ._screen_width_factor = 1.0 / screen_width
73
76
self ._screen_height_factor = 1.0 / screen_height
74
77
78
+ def set_default_color (self , color : flat .Color ):
79
+ """
80
+ Set which color to use when no other color is provided.
81
+ """
82
+ self ._default_color = color
83
+
75
84
@staticmethod
76
85
def create_color (red : int , green : int , blue : int , alpha : int = 255 ) -> flat .Color :
77
86
return flat .Color (red , green , blue , alpha )
@@ -111,6 +120,30 @@ def team_color(team: int, alt_color: bool = False) -> flat.Color:
111
120
def _get_group_id (group_id : str ) -> int :
112
121
return hash (str (group_id ).encode ("utf-8" )) % MAX_INT
113
122
123
+ @contextmanager
124
+ def context (self , group_id : str = DEFAULT_GROUP_ID , default_color = None ):
125
+ """
126
+ Starts rendering as a context usable in with-statements.
127
+ After the with-statement the rendering is automatically ended.
128
+ Note, the is not possible to have two nested renderings started.
129
+
130
+ Example:
131
+
132
+ ```
133
+ with renderer.context(default_color=renderer.red):
134
+ renderer.draw_line_3d(car.pos, ball.pos)
135
+ renderer.draw_line_3d(car.pos, goal.pos)
136
+ renderer.draw_line_3d(ball.pos, goal.pos)
137
+ ```
138
+ """
139
+ try :
140
+ self .begin_rendering (group_id )
141
+ if default_color :
142
+ self .set_default_color (default_color )
143
+ yield
144
+ finally :
145
+ self .end_rendering ()
146
+
114
147
def begin_rendering (self , group_id : str = DEFAULT_GROUP_ID ):
115
148
"""
116
149
Begins a new render group. All render messages added after this call will be part of this group.
@@ -189,29 +222,29 @@ def draw_line_3d(
189
222
self ,
190
223
start : flat .RenderAnchor | flat .BallAnchor | flat .CarAnchor | flat .Vector3 ,
191
224
end : flat .RenderAnchor | flat .BallAnchor | flat .CarAnchor | flat .Vector3 ,
192
- color : flat .Color ,
225
+ color : flat .Color | None = None ,
193
226
):
194
227
"""
195
228
Draws a line between two anchors in 3d space.
196
229
"""
197
- self .draw (flat .Line3D (_get_anchor (start ), _get_anchor (end ), color ))
230
+ self .draw (flat .Line3D (_get_anchor (start ), _get_anchor (end ), color or self . _default_color ))
198
231
199
232
def draw_polyline_3d (
200
233
self ,
201
234
points : Sequence [flat .Vector3 ],
202
- color : flat .Color ,
235
+ color : flat .Color | None = None ,
203
236
):
204
237
"""
205
238
Draws a line going through each of the provided points.
206
239
"""
207
- self .draw (flat .PolyLine3D (points , color ))
240
+ self .draw (flat .PolyLine3D (points , color or self . _default_color ))
208
241
209
242
def draw_string_3d (
210
243
self ,
211
244
text : str ,
212
245
anchor : flat .RenderAnchor | flat .BallAnchor | flat .CarAnchor | flat .Vector3 ,
213
246
scale : float ,
214
- foreground : flat .Color ,
247
+ foreground : flat .Color | None = None ,
215
248
background : flat .Color = flat .Color (a = 0 ),
216
249
h_align : flat .TextHAlign = flat .TextHAlign .Left ,
217
250
v_align : flat .TextVAlign = flat .TextVAlign .Top ,
@@ -225,7 +258,7 @@ def draw_string_3d(
225
258
text ,
226
259
_get_anchor (anchor ),
227
260
scale ,
228
- foreground ,
261
+ foreground or self . _default_color ,
229
262
background ,
230
263
h_align ,
231
264
v_align ,
@@ -238,7 +271,7 @@ def draw_string_2d(
238
271
x : float ,
239
272
y : float ,
240
273
scale : float ,
241
- foreground : flat .Color ,
274
+ foreground : flat .Color | None = None ,
242
275
background : flat .Color = flat .Color (a = 0 ),
243
276
h_align : flat .TextHAlign = flat .TextHAlign .Left ,
244
277
v_align : flat .TextVAlign = flat .TextVAlign .Top ,
@@ -255,7 +288,7 @@ def draw_string_2d(
255
288
x * self ._screen_width_factor ,
256
289
y * self ._screen_height_factor ,
257
290
scale ,
258
- foreground ,
291
+ foreground or self . _default_color ,
259
292
background ,
260
293
h_align ,
261
294
v_align ,
@@ -268,7 +301,7 @@ def draw_rect_2d(
268
301
y : float ,
269
302
width : float ,
270
303
height : float ,
271
- color : flat .Color ,
304
+ color : flat .Color | None = None ,
272
305
h_align : flat .TextHAlign = flat .TextHAlign .Left ,
273
306
v_align : flat .TextVAlign = flat .TextVAlign .Top ,
274
307
):
@@ -284,7 +317,7 @@ def draw_rect_2d(
284
317
y * self ._screen_height_factor ,
285
318
width * self ._screen_width_factor ,
286
319
height * self ._screen_height_factor ,
287
- color ,
320
+ color or self . _default_color ,
288
321
h_align ,
289
322
v_align ,
290
323
)
@@ -295,7 +328,7 @@ def draw_rect_3d(
295
328
anchor : flat .RenderAnchor | flat .BallAnchor | flat .CarAnchor | flat .Vector3 ,
296
329
width : float ,
297
330
height : float ,
298
- color : flat .Color ,
331
+ color : flat .Color | None = None ,
299
332
h_align : flat .TextHAlign = flat .TextHAlign .Left ,
300
333
v_align : flat .TextVAlign = flat .TextVAlign .Top ,
301
334
):
@@ -310,7 +343,7 @@ def draw_rect_3d(
310
343
_get_anchor (anchor ),
311
344
width * self ._screen_width_factor ,
312
345
height * self ._screen_height_factor ,
313
- color ,
346
+ color or self . _default_color ,
314
347
h_align ,
315
348
v_align ,
316
349
)
0 commit comments