2020-05-02 05:21:35 -04:00
|
|
|
|
|
|
|
#include "shared-module/vectorio/__init__.h"
|
|
|
|
#include "shared-bindings/vectorio/VectorShape.h"
|
|
|
|
#include "shared-bindings/vectorio/Circle.h"
|
|
|
|
#include "shared-bindings/vectorio/Polygon.h"
|
|
|
|
#include "shared-bindings/vectorio/Rectangle.h"
|
|
|
|
|
|
|
|
#include "shared-bindings/displayio/ColorConverter.h"
|
|
|
|
#include "shared-bindings/displayio/Palette.h"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "lib/utils/context_manager_helpers.h"
|
|
|
|
|
|
|
|
#include "py/binary.h"
|
|
|
|
#include "py/objproperty.h"
|
|
|
|
#include "py/objtype.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "supervisor/shared/translate.h"
|
|
|
|
|
|
|
|
|
2021-08-01 16:01:57 -04:00
|
|
|
// shape: The shape implementation to draw.
|
|
|
|
// 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.
|
|
|
|
// x: Initial x position of the center axis of the shape within the parent.
|
|
|
|
// y: Initial y position of the center axis of the shape within the parent."""
|
|
|
|
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) {
|
2021-04-22 20:55:39 -04:00
|
|
|
if (!mp_obj_is_type(pixel_shader, &displayio_colorconverter_type) &&
|
|
|
|
!mp_obj_is_type(pixel_shader, &displayio_palette_type)) {
|
2020-05-02 05:21:35 -04:00
|
|
|
mp_raise_TypeError_varg(translate("unsupported %q type"), MP_QSTR_pixel_shader);
|
|
|
|
}
|
|
|
|
|
|
|
|
vectorio_ishape_t ishape;
|
|
|
|
// Wire up shape functions
|
2021-04-22 20:55:39 -04:00
|
|
|
if (mp_obj_is_type(shape, &vectorio_polygon_type)) {
|
2020-05-02 05:21:35 -04:00
|
|
|
ishape.shape = shape;
|
|
|
|
ishape.get_area = &common_hal_vectorio_polygon_get_area;
|
|
|
|
ishape.get_pixel = &common_hal_vectorio_polygon_get_pixel;
|
2021-04-22 20:55:39 -04:00
|
|
|
} else if (mp_obj_is_type(shape, &vectorio_rectangle_type)) {
|
2020-05-02 05:21:35 -04:00
|
|
|
ishape.shape = shape;
|
|
|
|
ishape.get_area = &common_hal_vectorio_rectangle_get_area;
|
|
|
|
ishape.get_pixel = &common_hal_vectorio_rectangle_get_pixel;
|
2021-04-22 20:55:39 -04:00
|
|
|
} else if (mp_obj_is_type(shape, &vectorio_circle_type)) {
|
2020-05-02 05:21:35 -04:00
|
|
|
ishape.shape = shape;
|
|
|
|
ishape.get_area = &common_hal_vectorio_circle_get_area;
|
|
|
|
ishape.get_pixel = &common_hal_vectorio_circle_get_pixel;
|
|
|
|
} else {
|
|
|
|
mp_raise_TypeError_varg(translate("unsupported %q type"), MP_QSTR_shape);
|
|
|
|
}
|
|
|
|
|
|
|
|
vectorio_vector_shape_t *self = m_new_obj(vectorio_vector_shape_t);
|
|
|
|
self->base.type = &vectorio_vector_shape_type;
|
|
|
|
common_hal_vectorio_vector_shape_construct(self,
|
2020-05-09 02:03:15 -04:00
|
|
|
ishape, pixel_shader, x, y
|
2021-03-15 09:57:36 -04:00
|
|
|
);
|
2020-05-02 05:21:35 -04:00
|
|
|
|
|
|
|
// Wire up event callbacks
|
|
|
|
vectorio_event_t on_dirty = {
|
|
|
|
.obj = self,
|
|
|
|
.event = &common_hal_vectorio_vector_shape_set_dirty
|
|
|
|
};
|
|
|
|
|
2021-04-22 20:55:39 -04:00
|
|
|
if (mp_obj_is_type(shape, &vectorio_polygon_type)) {
|
2020-05-02 05:21:35 -04:00
|
|
|
common_hal_vectorio_polygon_set_on_dirty(self->ishape.shape, on_dirty);
|
2021-04-22 20:55:39 -04:00
|
|
|
} else if (mp_obj_is_type(shape, &vectorio_rectangle_type)) {
|
|
|
|
} else if (mp_obj_is_type(shape, &vectorio_circle_type)) {
|
2020-05-12 00:21:05 -04:00
|
|
|
common_hal_vectorio_circle_set_on_dirty(self->ishape.shape, on_dirty);
|
2020-05-02 05:21:35 -04:00
|
|
|
} else {
|
|
|
|
mp_raise_TypeError_varg(translate("unsupported %q type"), MP_QSTR_shape);
|
|
|
|
}
|
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
2021-08-01 16:01:57 -04:00
|
|
|
vectorio_draw_protocol_impl_t vectorio_vector_shape_draw_protocol_impl = {
|
|
|
|
.draw_fill_area = (draw_fill_area_fun)vectorio_vector_shape_fill_area,
|
|
|
|
.draw_get_dirty_area = (draw_get_dirty_area_fun)vectorio_vector_shape_get_dirty_area,
|
|
|
|
.draw_update_transform = (draw_update_transform_fun)vectorio_vector_shape_update_transform,
|
|
|
|
.draw_finish_refresh = (draw_finish_refresh_fun)vectorio_vector_shape_finish_refresh,
|
|
|
|
.draw_get_refresh_areas = (draw_get_refresh_areas_fun)vectorio_vector_shape_get_refresh_areas,
|
|
|
|
};
|
|
|
|
|
2020-05-02 05:21:35 -04:00
|
|
|
|
2021-08-05 02:55:03 -04:00
|
|
|
// Stub checker does not approve of these shared properties.
|
|
|
|
// x: int
|
|
|
|
// """X position of the center point of the shape in the parent."""
|
|
|
|
//
|
2021-08-01 16:01:57 -04:00
|
|
|
STATIC mp_obj_t vectorio_vector_shape_obj_get_x(mp_obj_t wrapper_shape) {
|
|
|
|
// Relies on the fact that only vector_shape impl gets matched with a VectorShape.
|
|
|
|
const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, wrapper_shape);
|
|
|
|
vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape));
|
2021-08-03 14:25:31 -04:00
|
|
|
|
2020-05-02 05:21:35 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(common_hal_vectorio_vector_shape_get_x(self));
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(vectorio_vector_shape_get_x_obj, vectorio_vector_shape_obj_get_x);
|
|
|
|
|
2021-08-01 16:01:57 -04:00
|
|
|
STATIC mp_obj_t vectorio_vector_shape_obj_set_x(mp_obj_t wrapper_shape, mp_obj_t x_obj) {
|
|
|
|
// Relies on the fact that only vector_shape impl gets matched with a VectorShape.
|
|
|
|
const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, wrapper_shape);
|
|
|
|
vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape));
|
2020-05-02 05:21:35 -04:00
|
|
|
|
|
|
|
mp_int_t x = mp_obj_get_int(x_obj);
|
|
|
|
common_hal_vectorio_vector_shape_set_x(self, x);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(vectorio_vector_shape_set_x_obj, vectorio_vector_shape_obj_set_x);
|
|
|
|
|
|
|
|
const mp_obj_property_t vectorio_vector_shape_x_obj = {
|
|
|
|
.base.type = &mp_type_property,
|
|
|
|
.proxy = {(mp_obj_t)&vectorio_vector_shape_get_x_obj,
|
|
|
|
(mp_obj_t)&vectorio_vector_shape_set_x_obj,
|
2021-05-05 20:51:52 -04:00
|
|
|
MP_ROM_NONE},
|
2020-05-02 05:21:35 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-08-05 02:55:03 -04:00
|
|
|
// y: int
|
|
|
|
// """Y position of the center point of the shape in the parent."""
|
|
|
|
//
|
2021-08-01 16:01:57 -04:00
|
|
|
STATIC mp_obj_t vectorio_vector_shape_obj_get_y(mp_obj_t wrapper_shape) {
|
|
|
|
// Relies on the fact that only vector_shape impl gets matched with a VectorShape.
|
|
|
|
const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, wrapper_shape);
|
|
|
|
vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape));
|
|
|
|
|
2020-05-02 05:21:35 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(common_hal_vectorio_vector_shape_get_y(self));
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(vectorio_vector_shape_get_y_obj, vectorio_vector_shape_obj_get_y);
|
|
|
|
|
2021-08-01 16:01:57 -04:00
|
|
|
STATIC mp_obj_t vectorio_vector_shape_obj_set_y(mp_obj_t wrapper_shape, mp_obj_t y_obj) {
|
|
|
|
// Relies on the fact that only vector_shape impl gets matched with a VectorShape.
|
|
|
|
const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, wrapper_shape);
|
|
|
|
vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape));
|
2020-05-02 05:21:35 -04:00
|
|
|
|
|
|
|
mp_int_t y = mp_obj_get_int(y_obj);
|
|
|
|
common_hal_vectorio_vector_shape_set_y(self, y);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(vectorio_vector_shape_set_y_obj, vectorio_vector_shape_obj_set_y);
|
|
|
|
|
|
|
|
const mp_obj_property_t vectorio_vector_shape_y_obj = {
|
|
|
|
.base.type = &mp_type_property,
|
|
|
|
.proxy = {(mp_obj_t)&vectorio_vector_shape_get_y_obj,
|
|
|
|
(mp_obj_t)&vectorio_vector_shape_set_y_obj,
|
2021-05-05 20:51:52 -04:00
|
|
|
MP_ROM_NONE},
|
2020-05-02 05:21:35 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-08-05 03:13:10 -04:00
|
|
|
// pixel_shader: Union[ColorConverter, Palette]
|
2021-08-05 02:55:03 -04:00
|
|
|
// """The pixel shader of the shape."""
|
|
|
|
//
|
2021-08-01 16:01:57 -04:00
|
|
|
STATIC mp_obj_t vectorio_vector_shape_obj_get_pixel_shader(mp_obj_t wrapper_shape) {
|
|
|
|
// Relies on the fact that only vector_shape impl gets matched with a VectorShape.
|
|
|
|
const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, wrapper_shape);
|
|
|
|
vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape));
|
|
|
|
|
2020-05-02 05:21:35 -04:00
|
|
|
return common_hal_vectorio_vector_shape_get_pixel_shader(self);
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(vectorio_vector_shape_get_pixel_shader_obj, vectorio_vector_shape_obj_get_pixel_shader);
|
|
|
|
|
2021-08-01 16:01:57 -04:00
|
|
|
STATIC mp_obj_t vectorio_vector_shape_obj_set_pixel_shader(mp_obj_t wrapper_shape, mp_obj_t pixel_shader) {
|
|
|
|
// Relies on the fact that only vector_shape impl gets matched with a VectorShape.
|
|
|
|
const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, wrapper_shape);
|
|
|
|
vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape));
|
|
|
|
|
2021-04-22 20:55:39 -04:00
|
|
|
if (!mp_obj_is_type(pixel_shader, &displayio_palette_type) && !mp_obj_is_type(pixel_shader, &displayio_colorconverter_type)) {
|
2020-05-02 05:21:35 -04:00
|
|
|
mp_raise_TypeError(translate("pixel_shader must be displayio.Palette or displayio.ColorConverter"));
|
|
|
|
}
|
|
|
|
|
|
|
|
common_hal_vectorio_vector_shape_set_pixel_shader(self, pixel_shader);
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(vectorio_vector_shape_set_pixel_shader_obj, vectorio_vector_shape_obj_set_pixel_shader);
|
|
|
|
|
|
|
|
const mp_obj_property_t vectorio_vector_shape_pixel_shader_obj = {
|
|
|
|
.base.type = &mp_type_property,
|
|
|
|
.proxy = {(mp_obj_t)&vectorio_vector_shape_get_pixel_shader_obj,
|
|
|
|
(mp_obj_t)&vectorio_vector_shape_set_pixel_shader_obj,
|
2021-05-05 20:51:52 -04:00
|
|
|
MP_ROM_NONE},
|
2020-05-02 05:21:35 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
STATIC const mp_rom_map_elem_t vectorio_vector_shape_locals_dict_table[] = {
|
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(vectorio_vector_shape_locals_dict, vectorio_vector_shape_locals_dict_table);
|
|
|
|
|
|
|
|
const mp_obj_type_t vectorio_vector_shape_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_VectorShape,
|
2021-03-15 09:57:36 -04:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&vectorio_vector_shape_locals_dict,
|
2020-05-02 05:21:35 -04:00
|
|
|
};
|