1919#include "supervisor/shared/translate.h"
2020
2121
22- //| class VectorShape:
23- //| def __init__(self, shape: Union[Polygon, Rectangle, Circle], pixel_shader: Union[displayio.ColorConverter, displayio.Palette], x: int=0, y: int=0) -> None:
24- //| """Binds a vector shape to a location and pixel shader. The shader can be a displayio.Palette(1); it will be asked to color pixel value 0.
25- //|
26- //| :param shape: The shape to draw.
27- //| :param pixel_shader: The pixel shader that produces colors from values
28- //| :param x: Initial x position of the center axis of the shape within the parent.
29- //| :param y: Initial y position of the center axis of the shape within the parent."""
30- //| ...
31- //|
32- STATIC mp_obj_t vectorio_vector_shape_make_new (const mp_obj_type_t * type , size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
33- enum { ARG_shape , ARG_pixel_shader , ARG_x , ARG_y };
34- static const mp_arg_t allowed_args [] = {
35- { MP_QSTR_shape , MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
36- { MP_QSTR_pixel_shader , MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
37- { MP_QSTR_x , MP_ARG_INT | MP_ARG_KW_ONLY , {.u_int = 0 } },
38- { MP_QSTR_y , MP_ARG_INT | MP_ARG_KW_ONLY , {.u_int = 0 } },
39- };
40- mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
41- mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
42-
43- mp_obj_t pixel_shader = args [ARG_pixel_shader ].u_obj ;
22+ // shape: The shape implementation to draw.
23+ // pixel_shader: The pixel shader that produces colors from values. The shader can be a displayio.Palette(1); it will be asked to color pixel value 0.
24+ // x: Initial x position of the center axis of the shape within the parent.
25+ // y: Initial y position of the center axis of the shape within the parent."""
26+ mp_obj_t vectorio_vector_shape_make_new (const mp_obj_t shape , const mp_obj_t pixel_shader , int16_t x , int16_t y ) {
4427 if (!mp_obj_is_type (pixel_shader , & displayio_colorconverter_type ) &&
4528 !mp_obj_is_type (pixel_shader , & displayio_palette_type )) {
4629 mp_raise_TypeError_varg (translate ("unsupported %q type" ), MP_QSTR_pixel_shader );
4730 }
4831
49- int16_t x = args [ARG_x ].u_int ;
50- int16_t y = args [ARG_y ].u_int ;
51-
52- mp_obj_t shape = args [ARG_shape ].u_obj ;
5332 vectorio_ishape_t ishape ;
5433 // Wire up shape functions
5534 if (mp_obj_is_type (shape , & vectorio_polygon_type )) {
@@ -92,18 +71,31 @@ STATIC mp_obj_t vectorio_vector_shape_make_new(const mp_obj_type_t *type, size_t
9271 return MP_OBJ_FROM_PTR (self );
9372}
9473
74+ vectorio_draw_protocol_impl_t vectorio_vector_shape_draw_protocol_impl = {
75+ .draw_fill_area = (draw_fill_area_fun )vectorio_vector_shape_fill_area ,
76+ .draw_get_dirty_area = (draw_get_dirty_area_fun )vectorio_vector_shape_get_dirty_area ,
77+ .draw_update_transform = (draw_update_transform_fun )vectorio_vector_shape_update_transform ,
78+ .draw_finish_refresh = (draw_finish_refresh_fun )vectorio_vector_shape_finish_refresh ,
79+ .draw_get_refresh_areas = (draw_get_refresh_areas_fun )vectorio_vector_shape_get_refresh_areas ,
80+ };
81+
9582
9683//| x: int
9784//| """X position of the center point of the shape in the parent."""
9885//|
99- STATIC mp_obj_t vectorio_vector_shape_obj_get_x (mp_obj_t self_in ) {
100- vectorio_vector_shape_t * self = MP_OBJ_TO_PTR (self_in );
86+ STATIC mp_obj_t vectorio_vector_shape_obj_get_x (mp_obj_t wrapper_shape ) {
87+ // Relies on the fact that only vector_shape impl gets matched with a VectorShape.
88+ const vectorio_draw_protocol_t * draw_protocol = mp_proto_get (MP_QSTR_protocol_draw , wrapper_shape );
89+ vectorio_vector_shape_t * self = MP_OBJ_TO_PTR (draw_protocol -> draw_get_protocol_self (wrapper_shape ));
90+
10191 return MP_OBJ_NEW_SMALL_INT (common_hal_vectorio_vector_shape_get_x (self ));
10292}
10393MP_DEFINE_CONST_FUN_OBJ_1 (vectorio_vector_shape_get_x_obj , vectorio_vector_shape_obj_get_x );
10494
105- STATIC mp_obj_t vectorio_vector_shape_obj_set_x (mp_obj_t self_in , mp_obj_t x_obj ) {
106- vectorio_vector_shape_t * self = MP_OBJ_TO_PTR (self_in );
95+ STATIC mp_obj_t vectorio_vector_shape_obj_set_x (mp_obj_t wrapper_shape , mp_obj_t x_obj ) {
96+ // Relies on the fact that only vector_shape impl gets matched with a VectorShape.
97+ const vectorio_draw_protocol_t * draw_protocol = mp_proto_get (MP_QSTR_protocol_draw , wrapper_shape );
98+ vectorio_vector_shape_t * self = MP_OBJ_TO_PTR (draw_protocol -> draw_get_protocol_self (wrapper_shape ));
10799
108100 mp_int_t x = mp_obj_get_int (x_obj );
109101 common_hal_vectorio_vector_shape_set_x (self , x );
@@ -122,14 +114,19 @@ const mp_obj_property_t vectorio_vector_shape_x_obj = {
122114//| y: int
123115//| """Y position of the center point of the shape in the parent."""
124116//|
125- STATIC mp_obj_t vectorio_vector_shape_obj_get_y (mp_obj_t self_in ) {
126- vectorio_vector_shape_t * self = MP_OBJ_TO_PTR (self_in );
117+ STATIC mp_obj_t vectorio_vector_shape_obj_get_y (mp_obj_t wrapper_shape ) {
118+ // Relies on the fact that only vector_shape impl gets matched with a VectorShape.
119+ const vectorio_draw_protocol_t * draw_protocol = mp_proto_get (MP_QSTR_protocol_draw , wrapper_shape );
120+ vectorio_vector_shape_t * self = MP_OBJ_TO_PTR (draw_protocol -> draw_get_protocol_self (wrapper_shape ));
121+
127122 return MP_OBJ_NEW_SMALL_INT (common_hal_vectorio_vector_shape_get_y (self ));
128123}
129124MP_DEFINE_CONST_FUN_OBJ_1 (vectorio_vector_shape_get_y_obj , vectorio_vector_shape_obj_get_y );
130125
131- STATIC mp_obj_t vectorio_vector_shape_obj_set_y (mp_obj_t self_in , mp_obj_t y_obj ) {
132- vectorio_vector_shape_t * self = MP_OBJ_TO_PTR (self_in );
126+ STATIC mp_obj_t vectorio_vector_shape_obj_set_y (mp_obj_t wrapper_shape , mp_obj_t y_obj ) {
127+ // Relies on the fact that only vector_shape impl gets matched with a VectorShape.
128+ const vectorio_draw_protocol_t * draw_protocol = mp_proto_get (MP_QSTR_protocol_draw , wrapper_shape );
129+ vectorio_vector_shape_t * self = MP_OBJ_TO_PTR (draw_protocol -> draw_get_protocol_self (wrapper_shape ));
133130
134131 mp_int_t y = mp_obj_get_int (y_obj );
135132 common_hal_vectorio_vector_shape_set_y (self , y );
@@ -148,14 +145,20 @@ const mp_obj_property_t vectorio_vector_shape_y_obj = {
148145//| pixel_shader: Union[displayio.ColorConverter, displayio.Palette]
149146//| """The pixel shader of the shape."""
150147//|
151- STATIC mp_obj_t vectorio_vector_shape_obj_get_pixel_shader (mp_obj_t self_in ) {
152- vectorio_vector_shape_t * self = MP_OBJ_TO_PTR (self_in );
148+ STATIC mp_obj_t vectorio_vector_shape_obj_get_pixel_shader (mp_obj_t wrapper_shape ) {
149+ // Relies on the fact that only vector_shape impl gets matched with a VectorShape.
150+ const vectorio_draw_protocol_t * draw_protocol = mp_proto_get (MP_QSTR_protocol_draw , wrapper_shape );
151+ vectorio_vector_shape_t * self = MP_OBJ_TO_PTR (draw_protocol -> draw_get_protocol_self (wrapper_shape ));
152+
153153 return common_hal_vectorio_vector_shape_get_pixel_shader (self );
154154}
155155MP_DEFINE_CONST_FUN_OBJ_1 (vectorio_vector_shape_get_pixel_shader_obj , vectorio_vector_shape_obj_get_pixel_shader );
156156
157- STATIC mp_obj_t vectorio_vector_shape_obj_set_pixel_shader (mp_obj_t self_in , mp_obj_t pixel_shader ) {
158- vectorio_vector_shape_t * self = MP_OBJ_TO_PTR (self_in );
157+ STATIC mp_obj_t vectorio_vector_shape_obj_set_pixel_shader (mp_obj_t wrapper_shape , mp_obj_t pixel_shader ) {
158+ // Relies on the fact that only vector_shape impl gets matched with a VectorShape.
159+ const vectorio_draw_protocol_t * draw_protocol = mp_proto_get (MP_QSTR_protocol_draw , wrapper_shape );
160+ vectorio_vector_shape_t * self = MP_OBJ_TO_PTR (draw_protocol -> draw_get_protocol_self (wrapper_shape ));
161+
159162 if (!mp_obj_is_type (pixel_shader , & displayio_palette_type ) && !mp_obj_is_type (pixel_shader , & displayio_colorconverter_type )) {
160163 mp_raise_TypeError (translate ("pixel_shader must be displayio.Palette or displayio.ColorConverter" ));
161164 }
@@ -175,16 +178,11 @@ const mp_obj_property_t vectorio_vector_shape_pixel_shader_obj = {
175178
176179
177180STATIC const mp_rom_map_elem_t vectorio_vector_shape_locals_dict_table [] = {
178- // Properties
179- { MP_ROM_QSTR (MP_QSTR_x ), MP_ROM_PTR (& vectorio_vector_shape_x_obj ) },
180- { MP_ROM_QSTR (MP_QSTR_y ), MP_ROM_PTR (& vectorio_vector_shape_y_obj ) },
181- { MP_ROM_QSTR (MP_QSTR_pixel_shader ), MP_ROM_PTR (& vectorio_vector_shape_pixel_shader_obj ) },
182181};
183182STATIC MP_DEFINE_CONST_DICT (vectorio_vector_shape_locals_dict , vectorio_vector_shape_locals_dict_table );
184183
185184const mp_obj_type_t vectorio_vector_shape_type = {
186185 { & mp_type_type },
187186 .name = MP_QSTR_VectorShape ,
188- .make_new = vectorio_vector_shape_make_new ,
189187 .locals_dict = (mp_obj_dict_t * )& vectorio_vector_shape_locals_dict ,
190188};
0 commit comments