19
19
#include "supervisor/shared/translate.h"
20
20
21
21
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 ) {
44
27
if (!mp_obj_is_type (pixel_shader , & displayio_colorconverter_type ) &&
45
28
!mp_obj_is_type (pixel_shader , & displayio_palette_type )) {
46
29
mp_raise_TypeError_varg (translate ("unsupported %q type" ), MP_QSTR_pixel_shader );
47
30
}
48
31
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 ;
53
32
vectorio_ishape_t ishape ;
54
33
// Wire up shape functions
55
34
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
92
71
return MP_OBJ_FROM_PTR (self );
93
72
}
94
73
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
+
95
82
96
83
//| x: int
97
84
//| """X position of the center point of the shape in the parent."""
98
85
//|
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
+
101
91
return MP_OBJ_NEW_SMALL_INT (common_hal_vectorio_vector_shape_get_x (self ));
102
92
}
103
93
MP_DEFINE_CONST_FUN_OBJ_1 (vectorio_vector_shape_get_x_obj , vectorio_vector_shape_obj_get_x );
104
94
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 ));
107
99
108
100
mp_int_t x = mp_obj_get_int (x_obj );
109
101
common_hal_vectorio_vector_shape_set_x (self , x );
@@ -122,14 +114,19 @@ const mp_obj_property_t vectorio_vector_shape_x_obj = {
122
114
//| y: int
123
115
//| """Y position of the center point of the shape in the parent."""
124
116
//|
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
+
127
122
return MP_OBJ_NEW_SMALL_INT (common_hal_vectorio_vector_shape_get_y (self ));
128
123
}
129
124
MP_DEFINE_CONST_FUN_OBJ_1 (vectorio_vector_shape_get_y_obj , vectorio_vector_shape_obj_get_y );
130
125
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 ));
133
130
134
131
mp_int_t y = mp_obj_get_int (y_obj );
135
132
common_hal_vectorio_vector_shape_set_y (self , y );
@@ -148,14 +145,20 @@ const mp_obj_property_t vectorio_vector_shape_y_obj = {
148
145
//| pixel_shader: Union[displayio.ColorConverter, displayio.Palette]
149
146
//| """The pixel shader of the shape."""
150
147
//|
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
+
153
153
return common_hal_vectorio_vector_shape_get_pixel_shader (self );
154
154
}
155
155
MP_DEFINE_CONST_FUN_OBJ_1 (vectorio_vector_shape_get_pixel_shader_obj , vectorio_vector_shape_obj_get_pixel_shader );
156
156
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
+
159
162
if (!mp_obj_is_type (pixel_shader , & displayio_palette_type ) && !mp_obj_is_type (pixel_shader , & displayio_colorconverter_type )) {
160
163
mp_raise_TypeError (translate ("pixel_shader must be displayio.Palette or displayio.ColorConverter" ));
161
164
}
@@ -175,16 +178,11 @@ const mp_obj_property_t vectorio_vector_shape_pixel_shader_obj = {
175
178
176
179
177
180
STATIC 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 ) },
182
181
};
183
182
STATIC MP_DEFINE_CONST_DICT (vectorio_vector_shape_locals_dict , vectorio_vector_shape_locals_dict_table );
184
183
185
184
const mp_obj_type_t vectorio_vector_shape_type = {
186
185
{ & mp_type_type },
187
186
.name = MP_QSTR_VectorShape ,
188
- .make_new = vectorio_vector_shape_make_new ,
189
187
.locals_dict = (mp_obj_dict_t * )& vectorio_vector_shape_locals_dict ,
190
188
};
0 commit comments