2021-08-01 16:01:57 -04:00
|
|
|
#include "shared-bindings/vectorio/__init__.h"
|
2020-05-02 05:21:35 -04:00
|
|
|
#include "shared-module/vectorio/__init__.h"
|
|
|
|
#include "shared-bindings/vectorio/Polygon.h"
|
2021-08-01 16:01:57 -04:00
|
|
|
#include "shared-bindings/vectorio/VectorShape.h"
|
2020-05-02 05:21:35 -04:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "py/obj.h"
|
|
|
|
#include "py/objproperty.h"
|
|
|
|
#include "py/objtype.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define VECTORIO_POLYGON_DEBUG(...) (void)0
|
|
|
|
// #define VECTORIO_POLYGON_DEBUG(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__)
|
|
|
|
|
|
|
|
|
2020-05-12 21:40:02 -04:00
|
|
|
//| class Polygon:
|
2022-09-27 16:21:42 -04:00
|
|
|
//| def __init__(
|
|
|
|
//| self,
|
|
|
|
//| pixel_shader: Union[displayio.ColorConverter, displayio.Palette],
|
|
|
|
//| points: List[Tuple[int, int]],
|
|
|
|
//| x: int,
|
|
|
|
//| y: int,
|
|
|
|
//| ) -> None:
|
2021-09-11 18:37:41 -04:00
|
|
|
//| """Represents a closed shape by ordered vertices. The path will be treated as
|
|
|
|
//| 'closed', the last point will connect to the first point.
|
2020-05-02 05:21:35 -04:00
|
|
|
//|
|
2021-09-11 18:37:41 -04:00
|
|
|
//| :param Union[~displayio.ColorConverter,~displayio.Palette] pixel_shader: The pixel
|
|
|
|
//| shader that produces colors from values
|
|
|
|
//| :param List[Tuple[int,int]] points: Vertices for the polygon
|
|
|
|
//| :param int x: Initial screen x position of the 0,0 origin in the points list.
|
2022-03-20 10:51:42 -04:00
|
|
|
//| :param int y: Initial screen y position of the 0,0 origin in the points list.
|
2023-02-01 03:08:41 -05:00
|
|
|
//| :param int color_index: Initial color_index to use when selecting color from the palette.
|
|
|
|
//| """
|
2021-10-15 14:43:12 -04:00
|
|
|
static mp_obj_t vectorio_polygon_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
|
2022-03-19 12:30:37 -04:00
|
|
|
enum { ARG_pixel_shader, ARG_points_list, ARG_x, ARG_y, ARG_color_index };
|
2020-05-02 05:21:35 -04:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
2021-08-01 16:01:57 -04:00
|
|
|
{ MP_QSTR_pixel_shader, MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
|
2020-05-02 05:21:35 -04:00
|
|
|
{ MP_QSTR_points, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
2021-08-01 16:01:57 -04:00
|
|
|
{ MP_QSTR_x, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
|
|
|
|
{ MP_QSTR_y, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
|
2022-03-19 12:30:37 -04:00
|
|
|
{ MP_QSTR_color_index, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
|
2020-05-02 05:21:35 -04:00
|
|
|
};
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
2021-10-15 14:43:12 -04:00
|
|
|
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2020-05-02 05:21:35 -04:00
|
|
|
|
2021-10-10 02:46:14 -04:00
|
|
|
mp_obj_t points_list = mp_arg_validate_type(args[ARG_points_list].u_obj, &mp_type_list, MP_QSTR_points);
|
2020-05-02 05:21:35 -04:00
|
|
|
|
2023-08-07 20:45:57 -04:00
|
|
|
vectorio_polygon_t *self = mp_obj_malloc(vectorio_polygon_t, &vectorio_polygon_type);
|
2020-05-02 05:21:35 -04:00
|
|
|
|
2022-03-19 12:30:37 -04:00
|
|
|
uint16_t color_index = args[ARG_color_index].u_int;
|
|
|
|
common_hal_vectorio_polygon_construct(self, points_list, color_index);
|
2020-05-02 05:21:35 -04:00
|
|
|
|
2021-08-01 16:01:57 -04:00
|
|
|
// VectorShape parts
|
|
|
|
mp_obj_t pixel_shader = args[ARG_pixel_shader].u_obj;
|
2021-09-10 04:24:33 -04:00
|
|
|
int32_t x = args[ARG_x].u_int;
|
|
|
|
int32_t y = args[ARG_y].u_int;
|
2021-08-01 16:01:57 -04:00
|
|
|
mp_obj_t vector_shape = vectorio_vector_shape_make_new(self, pixel_shader, x, y);
|
|
|
|
self->draw_protocol_instance = vector_shape;
|
|
|
|
|
2020-05-02 05:21:35 -04:00
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
2021-08-01 16:01:57 -04:00
|
|
|
STATIC const vectorio_draw_protocol_t polygon_draw_protocol = {
|
|
|
|
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_draw)
|
|
|
|
.draw_get_protocol_self = (draw_get_protocol_self_fun)common_hal_vectorio_polygon_get_draw_protocol,
|
|
|
|
.draw_protocol_impl = &vectorio_vector_shape_draw_protocol_impl
|
|
|
|
};
|
|
|
|
|
2020-05-02 05:21:35 -04:00
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| points: List[Tuple[int, int]]
|
2021-09-11 18:37:41 -04:00
|
|
|
//| """Vertices for the polygon."""
|
2020-05-02 05:21:35 -04:00
|
|
|
STATIC mp_obj_t vectorio_polygon_obj_get_points(mp_obj_t self_in) {
|
|
|
|
vectorio_polygon_t *self = MP_OBJ_TO_PTR(self_in);
|
2020-07-19 15:27:35 -04:00
|
|
|
return common_hal_vectorio_polygon_get_points(self);
|
2020-05-02 05:21:35 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(vectorio_polygon_get_points_obj, vectorio_polygon_obj_get_points);
|
|
|
|
|
|
|
|
STATIC mp_obj_t vectorio_polygon_obj_set_points(mp_obj_t self_in, mp_obj_t points) {
|
|
|
|
vectorio_polygon_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
2020-05-13 00:55:18 -04:00
|
|
|
common_hal_vectorio_polygon_set_points(self, points);
|
2020-05-02 05:21:35 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(vectorio_polygon_set_points_obj, vectorio_polygon_obj_set_points);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETSET(vectorio_polygon_points_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&vectorio_polygon_get_points_obj,
|
|
|
|
(mp_obj_t)&vectorio_polygon_set_points_obj);
|
2020-05-02 05:21:35 -04:00
|
|
|
|
2022-09-27 16:21:42 -04:00
|
|
|
//| color_index: int
|
2022-03-19 12:30:37 -04:00
|
|
|
//| """The color_index of the polygon as 0 based index of the palette."""
|
|
|
|
STATIC mp_obj_t vectorio_polygon_obj_get_color_index(mp_obj_t self_in) {
|
|
|
|
vectorio_polygon_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
return mp_obj_new_int(common_hal_vectorio_polygon_get_color_index(self));
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(vectorio_polygon_get_color_index_obj, vectorio_polygon_obj_get_color_index);
|
|
|
|
|
|
|
|
STATIC mp_obj_t vectorio_polygon_obj_set_color_index(mp_obj_t self_in, mp_obj_t color_index) {
|
|
|
|
vectorio_polygon_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
common_hal_vectorio_polygon_set_color_index(self, mp_obj_get_int(color_index));
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(vectorio_polygon_set_color_index_obj, vectorio_polygon_obj_set_color_index);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETSET(vectorio_polygon_color_index_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&vectorio_polygon_get_color_index_obj,
|
|
|
|
(mp_obj_t)&vectorio_polygon_set_color_index_obj);
|
2022-03-19 12:30:37 -04:00
|
|
|
|
2021-09-11 18:37:41 -04:00
|
|
|
|
|
|
|
// Documentation for properties inherited from VectorShape.
|
|
|
|
|
2022-09-27 16:21:42 -04:00
|
|
|
//| x: int
|
2021-09-13 12:04:46 -04:00
|
|
|
//| """X position of the 0,0 origin in the points list."""
|
2021-09-11 18:37:41 -04:00
|
|
|
//|
|
2022-09-27 16:21:42 -04:00
|
|
|
//| y: int
|
2021-09-13 12:04:46 -04:00
|
|
|
//| """Y position of the 0,0 origin in the points list."""
|
2021-09-11 18:37:41 -04:00
|
|
|
//|
|
2022-11-11 21:18:42 -05:00
|
|
|
//| hidden: bool
|
2022-11-11 18:50:07 -05:00
|
|
|
//| """Hide the polygon or not."""
|
|
|
|
//|
|
2022-09-27 16:21:42 -04:00
|
|
|
//| location: Tuple[int, int]
|
2021-09-13 12:04:46 -04:00
|
|
|
//| """(X,Y) position of the 0,0 origin in the points list."""
|
2021-09-11 18:37:41 -04:00
|
|
|
//|
|
2022-09-27 16:21:42 -04:00
|
|
|
//| pixel_shader: Union[displayio.ColorConverter, displayio.Palette]
|
2021-09-11 18:37:41 -04:00
|
|
|
//| """The pixel shader of the polygon."""
|
2022-09-29 20:22:32 -04:00
|
|
|
//|
|
2021-09-11 18:37:41 -04:00
|
|
|
|
2020-05-02 05:21:35 -04:00
|
|
|
STATIC const mp_rom_map_elem_t vectorio_polygon_locals_dict_table[] = {
|
vectorio contains(x, y)
new utility function for all vectorio shape specializations for testing
whether a screen-space x,y point falls within a shape's x,y.
This respects the current orientation of the screen in the manner of
displayio and vectorio - so your x,y requests are in the same coordinate
domain as your x,y locations and your width/height etc. properties that
ou set on other shapes. I.e., if you're using this for touch points then
you will need to make sure the touch events are in the same x,y domain as
your display.
```
contains(2, 4) -> true
------------------
| |
| |
| -- |
| | \ |
| |. \ |
| | \ |
| |____\ |
| |
------------------
contains(5, 4) -> false
------------------
| |
| |
| -- |
| | \ |
| | \. |
| | \ |
| |____\ |
| |
------------------
```
This helps provide low overhead introspection of shape coverage on screen.
It's envisioned that this will be used for things like touch-and-drag
widget controls, touch "areas" and may help with random ornament placement
on toy Christmas trees.
2021-12-27 16:31:18 -05:00
|
|
|
// Functions
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_contains), MP_ROM_PTR(&vectorio_vector_shape_contains_obj) },
|
2021-08-01 16:01:57 -04:00
|
|
|
// Properties
|
2020-05-02 05:21:35 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_points), MP_ROM_PTR(&vectorio_polygon_points_obj) },
|
2021-08-01 16:01:57 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_x), MP_ROM_PTR(&vectorio_vector_shape_x_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_y), MP_ROM_PTR(&vectorio_vector_shape_y_obj) },
|
2022-11-11 18:50:07 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_hidden), MP_ROM_PTR(&vectorio_vector_shape_hidden_obj) },
|
2022-03-19 12:30:37 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_color_index), MP_ROM_PTR(&vectorio_polygon_color_index_obj) },
|
2021-08-08 03:14:53 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_location), MP_ROM_PTR(&vectorio_vector_shape_location_obj) },
|
2021-08-01 16:01:57 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_pixel_shader), MP_ROM_PTR(&vectorio_vector_shape_pixel_shader_obj) },
|
2020-05-02 05:21:35 -04:00
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(vectorio_polygon_locals_dict, vectorio_polygon_locals_dict_table);
|
|
|
|
|
2023-09-19 21:09:29 -04:00
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
vectorio_polygon_type,
|
|
|
|
MP_QSTR_Polygon,
|
2023-10-19 17:27:20 -04:00
|
|
|
MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS,
|
2023-09-19 21:09:29 -04:00
|
|
|
make_new, vectorio_polygon_make_new,
|
|
|
|
locals_dict, &vectorio_polygon_locals_dict,
|
|
|
|
protocol, &polygon_draw_protocol
|
|
|
|
);
|