1
+ import math
1
2
from collections .abc import Callable , Sequence
2
3
from typing import Optional
3
4
@@ -50,6 +51,9 @@ class Renderer:
50
51
_group_id : Optional [int ] = None
51
52
_current_renders : list [flat .RenderMessage ] = []
52
53
54
+ _screen_width_factor = 1.0
55
+ _screen_height_factor = 1.0
56
+
53
57
def __init__ (self , game_interface : SocketRelay ):
54
58
self ._render_group : Callable [[flat .RenderGroup ], None ] = (
55
59
game_interface .send_render_group
@@ -59,10 +63,37 @@ def __init__(self, game_interface: SocketRelay):
59
63
game_interface .remove_render_group
60
64
)
61
65
66
+ def set_resolution (self , screen_width : float , screen_height : float ):
67
+ """
68
+ By default, the renderer uses screen-space coordinates for 2d, e.g. 0.1 is 10% of screen width.
69
+ Use this function to declare the screen's size in pixels, if you prefer working in pixel coordinates.
70
+ After setting this, `draw_string_2d('Hi', 100, 200, ...)` will draw 'Hi' at pixel coordinates (100, 200).
71
+ """
72
+ self ._screen_width_factor = 1.0 / screen_width
73
+ self ._screen_height_factor = 1.0 / screen_height
74
+
62
75
@staticmethod
63
76
def create_color (red : int , green : int , blue : int , alpha : int = 255 ) -> flat .Color :
64
77
return flat .Color (red , green , blue , alpha )
65
78
79
+ @staticmethod
80
+ def create_color_hsv (hue : float , saturation : float , value : float ) -> flat .Color :
81
+ i = math .floor (hue * 6 )
82
+ f = hue * 6 - i
83
+ p = value * (1 - saturation )
84
+ q = value * (1 - f * saturation )
85
+ t = value * (1 - (1 - f ) * saturation )
86
+
87
+ match i % 6 :
88
+ case 0 : r , g , b = value , t , p
89
+ case 1 : r , g , b = q , value , p
90
+ case 2 : r , g , b = p , value , t
91
+ case 3 : r , g , b = p , q , value
92
+ case 4 : r , g , b = t , p , value
93
+ case 5 : r , g , b = value , p , q
94
+
95
+ return flat .Color (math .floor (r * 255 ), math .floor (g * 255 ), math .floor (b * 255 ))
96
+
66
97
@staticmethod
67
98
def team_color (team : int , alt_color : bool = False ) -> flat .Color :
68
99
"""
@@ -215,13 +246,14 @@ def draw_string_2d(
215
246
"""
216
247
Draws text in 2d space.
217
248
X and y uses screen-space coordinates, i.e. 0.1 is 10% of the screen width/height.
249
+ Use `set_resolution` to change to pixel coordinates.
218
250
Characters of the font are 20 pixels tall and 10 pixels wide when `scale == 1.0`.
219
251
"""
220
252
self .draw (
221
253
flat .String2D (
222
254
text ,
223
- x ,
224
- y ,
255
+ x * self . _screen_width_factor ,
256
+ y * self . _screen_height_factor ,
225
257
scale ,
226
258
foreground ,
227
259
background ,
@@ -243,14 +275,15 @@ def draw_rect_2d(
243
275
"""
244
276
Draws a rectangle anchored in 2d space.
245
277
X, y, width, and height uses screen-space coordinates, i.e. 0.1 is 10% of the screen width/height.
278
+ Use `set_resolution` to change to pixel coordinates.
246
279
"""
247
280
248
281
self .draw (
249
282
flat .Rect2D (
250
- x ,
251
- y ,
252
- width ,
253
- height ,
283
+ x * self . _screen_width_factor ,
284
+ y * self . _screen_height_factor ,
285
+ width * self . _screen_width_factor ,
286
+ height * self . _screen_height_factor ,
254
287
color ,
255
288
h_align ,
256
289
v_align ,
@@ -269,13 +302,14 @@ def draw_rect_3d(
269
302
"""
270
303
Draws a rectangle anchored in 3d space.
271
304
Width and height are screen-space sizes, i.e. 0.1 is 10% of the screen width/height.
305
+ Use `set_resolution` to change to pixel coordinates.
272
306
The size does not change based on distance to the camera.
273
307
"""
274
308
self .draw (
275
309
flat .Rect3D (
276
310
_get_anchor (anchor ),
277
- width ,
278
- height ,
311
+ width * self . _screen_width_factor ,
312
+ height * self . _screen_height_factor ,
279
313
color ,
280
314
h_align ,
281
315
v_align ,
0 commit comments